validate_builtins.cpp 188 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380
  1. // Copyright (c) 2018 Google LLC.
  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 built-in variables.
  17. #include <array>
  18. #include <functional>
  19. #include <list>
  20. #include <map>
  21. #include <set>
  22. #include <sstream>
  23. #include <stack>
  24. #include <string>
  25. #include <unordered_map>
  26. #include <vector>
  27. #include "source/diagnostic.h"
  28. #include "source/opcode.h"
  29. #include "source/spirv_target_env.h"
  30. #include "source/util/bitutils.h"
  31. #include "source/val/instruction.h"
  32. #include "source/val/validate.h"
  33. #include "source/val/validation_state.h"
  34. namespace spvtools {
  35. namespace val {
  36. namespace {
  37. // Returns a short textual description of the id defined by the given
  38. // instruction.
  39. std::string GetIdDesc(const Instruction& inst) {
  40. std::ostringstream ss;
  41. ss << "ID <" << inst.id() << "> (Op" << spvOpcodeString(inst.opcode()) << ")";
  42. return ss.str();
  43. }
  44. // Gets underlying data type which is
  45. // - member type if instruction is OpTypeStruct
  46. // (member index is taken from decoration).
  47. // - data type if id creates a pointer.
  48. // - type of the constant if instruction is OpConst or OpSpecConst.
  49. //
  50. // Fails in any other case. The function is based on built-ins allowed by
  51. // the Vulkan spec.
  52. // TODO: If non-Vulkan validation rules are added then it might need
  53. // to be refactored.
  54. spv_result_t GetUnderlyingType(ValidationState_t& _,
  55. const Decoration& decoration,
  56. const Instruction& inst,
  57. uint32_t* underlying_type) {
  58. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  59. if (inst.opcode() != SpvOpTypeStruct) {
  60. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  61. << GetIdDesc(inst)
  62. << "Attempted to get underlying data type via member index for "
  63. "non-struct type.";
  64. }
  65. *underlying_type = inst.word(decoration.struct_member_index() + 2);
  66. return SPV_SUCCESS;
  67. }
  68. if (inst.opcode() == SpvOpTypeStruct) {
  69. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  70. << GetIdDesc(inst)
  71. << " did not find an member index to get underlying data type for "
  72. "struct type.";
  73. }
  74. if (spvOpcodeIsConstant(inst.opcode())) {
  75. *underlying_type = inst.type_id();
  76. return SPV_SUCCESS;
  77. }
  78. uint32_t storage_class = 0;
  79. if (!_.GetPointerTypeInfo(inst.type_id(), underlying_type, &storage_class)) {
  80. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  81. << GetIdDesc(inst)
  82. << " is decorated with BuiltIn. BuiltIn decoration should only be "
  83. "applied to struct types, variables and constants.";
  84. }
  85. return SPV_SUCCESS;
  86. }
  87. // Returns Storage Class used by the instruction if applicable.
  88. // Returns SpvStorageClassMax if not.
  89. SpvStorageClass GetStorageClass(const Instruction& inst) {
  90. switch (inst.opcode()) {
  91. case SpvOpTypePointer:
  92. case SpvOpTypeForwardPointer: {
  93. return SpvStorageClass(inst.word(2));
  94. }
  95. case SpvOpVariable: {
  96. return SpvStorageClass(inst.word(3));
  97. }
  98. case SpvOpGenericCastToPtrExplicit: {
  99. return SpvStorageClass(inst.word(4));
  100. }
  101. default: { break; }
  102. }
  103. return SpvStorageClassMax;
  104. }
  105. typedef enum VUIDError_ {
  106. VUIDErrorExecutionModel = 0,
  107. VUIDErrorStorageClass = 1,
  108. VUIDErrorType = 2,
  109. VUIDErrorMax,
  110. } VUIDError;
  111. const static uint32_t NumVUIDBuiltins = 36;
  112. typedef struct {
  113. SpvBuiltIn builtIn;
  114. uint32_t vuid[VUIDErrorMax]; // execution mode, storage class, type VUIDs
  115. } BuiltinVUIDMapping;
  116. std::array<BuiltinVUIDMapping, NumVUIDBuiltins> builtinVUIDInfo = {{
  117. // clang-format off
  118. {SpvBuiltInSubgroupEqMask, {0, 4370, 4371}},
  119. {SpvBuiltInSubgroupGeMask, {0, 4372, 4373}},
  120. {SpvBuiltInSubgroupGtMask, {0, 4374, 4375}},
  121. {SpvBuiltInSubgroupLeMask, {0, 4376, 4377}},
  122. {SpvBuiltInSubgroupLtMask, {0, 4378, 4379}},
  123. {SpvBuiltInSubgroupLocalInvocationId, {0, 4380, 4381}},
  124. {SpvBuiltInSubgroupSize, {0, 4382, 4383}},
  125. {SpvBuiltInGlobalInvocationId, {4236, 4237, 4238}},
  126. {SpvBuiltInLocalInvocationId, {4281, 4282, 4283}},
  127. {SpvBuiltInNumWorkgroups, {4296, 4297, 4298}},
  128. {SpvBuiltInNumSubgroups, {4293, 4294, 4295}},
  129. {SpvBuiltInSubgroupId, {4367, 4368, 4369}},
  130. {SpvBuiltInWorkgroupId, {4422, 4423, 4424}},
  131. {SpvBuiltInHitKindKHR, {4242, 4243, 4244}},
  132. {SpvBuiltInHitTNV, {4245, 4246, 4247}},
  133. {SpvBuiltInInstanceCustomIndexKHR, {4251, 4252, 4253}},
  134. {SpvBuiltInInstanceId, {4254, 4255, 4256}},
  135. {SpvBuiltInRayGeometryIndexKHR, {4345, 4346, 4347}},
  136. {SpvBuiltInObjectRayDirectionKHR, {4299, 4300, 4301}},
  137. {SpvBuiltInObjectRayOriginKHR, {4302, 4303, 4304}},
  138. {SpvBuiltInObjectToWorldKHR, {4305, 4306, 4307}},
  139. {SpvBuiltInWorldToObjectKHR, {4434, 4435, 4436}},
  140. {SpvBuiltInIncomingRayFlagsKHR, {4248, 4249, 4250}},
  141. {SpvBuiltInRayTminKHR, {4351, 4352, 4353}},
  142. {SpvBuiltInRayTmaxKHR, {4348, 4349, 4350}},
  143. {SpvBuiltInWorldRayDirectionKHR, {4428, 4429, 4430}},
  144. {SpvBuiltInWorldRayOriginKHR, {4431, 4432, 4433}},
  145. {SpvBuiltInLaunchIdKHR, {4266, 4267, 4268}},
  146. {SpvBuiltInLaunchSizeKHR, {4269, 4270, 4271}},
  147. {SpvBuiltInFragInvocationCountEXT, {4217, 4218, 4219}},
  148. {SpvBuiltInFragSizeEXT, {4220, 4221, 4222}},
  149. {SpvBuiltInFragStencilRefEXT, {4223, 4224, 4225}},
  150. {SpvBuiltInFullyCoveredEXT, {4232, 4233, 4234}},
  151. {SpvBuiltInCullMaskKHR, {6735, 6736, 6737}},
  152. {SpvBuiltInBaryCoordKHR, {4154, 4155, 4156}},
  153. {SpvBuiltInBaryCoordNoPerspKHR, {4160, 4161, 4162}},
  154. // clang-format off
  155. } };
  156. uint32_t GetVUIDForBuiltin(SpvBuiltIn builtIn, VUIDError type) {
  157. uint32_t vuid = 0;
  158. for (const auto& iter: builtinVUIDInfo) {
  159. if (iter.builtIn == builtIn) {
  160. assert(type < VUIDErrorMax);
  161. vuid = iter.vuid[type];
  162. break;
  163. }
  164. }
  165. return vuid;
  166. }
  167. bool IsExecutionModelValidForRtBuiltIn(SpvBuiltIn builtin,
  168. SpvExecutionModel stage) {
  169. switch (builtin) {
  170. case SpvBuiltInHitKindKHR:
  171. case SpvBuiltInHitTNV:
  172. if (stage == SpvExecutionModelAnyHitKHR ||
  173. stage == SpvExecutionModelClosestHitKHR) {
  174. return true;
  175. }
  176. break;
  177. case SpvBuiltInInstanceCustomIndexKHR:
  178. case SpvBuiltInInstanceId:
  179. case SpvBuiltInRayGeometryIndexKHR:
  180. case SpvBuiltInObjectRayDirectionKHR:
  181. case SpvBuiltInObjectRayOriginKHR:
  182. case SpvBuiltInObjectToWorldKHR:
  183. case SpvBuiltInWorldToObjectKHR:
  184. switch (stage) {
  185. case SpvExecutionModelIntersectionKHR:
  186. case SpvExecutionModelAnyHitKHR:
  187. case SpvExecutionModelClosestHitKHR:
  188. return true;
  189. default:
  190. return false;
  191. }
  192. break;
  193. case SpvBuiltInIncomingRayFlagsKHR:
  194. case SpvBuiltInRayTminKHR:
  195. case SpvBuiltInRayTmaxKHR:
  196. case SpvBuiltInWorldRayDirectionKHR:
  197. case SpvBuiltInWorldRayOriginKHR:
  198. case SpvBuiltInCullMaskKHR:
  199. switch (stage) {
  200. case SpvExecutionModelIntersectionKHR:
  201. case SpvExecutionModelAnyHitKHR:
  202. case SpvExecutionModelClosestHitKHR:
  203. case SpvExecutionModelMissKHR:
  204. return true;
  205. default:
  206. return false;
  207. }
  208. break;
  209. case SpvBuiltInLaunchIdKHR:
  210. case SpvBuiltInLaunchSizeKHR:
  211. switch (stage) {
  212. case SpvExecutionModelRayGenerationKHR:
  213. case SpvExecutionModelIntersectionKHR:
  214. case SpvExecutionModelAnyHitKHR:
  215. case SpvExecutionModelClosestHitKHR:
  216. case SpvExecutionModelMissKHR:
  217. case SpvExecutionModelCallableKHR:
  218. return true;
  219. default:
  220. return false;
  221. }
  222. break;
  223. default:
  224. break;
  225. }
  226. return false;
  227. }
  228. // Helper class managing validation of built-ins.
  229. // TODO: Generic functionality of this class can be moved into
  230. // ValidationState_t to be made available to other users.
  231. class BuiltInsValidator {
  232. public:
  233. BuiltInsValidator(ValidationState_t& vstate) : _(vstate) {}
  234. // Run validation.
  235. spv_result_t Run();
  236. private:
  237. // Goes through all decorations in the module, if decoration is BuiltIn
  238. // calls ValidateSingleBuiltInAtDefinition().
  239. spv_result_t ValidateBuiltInsAtDefinition();
  240. // Validates the instruction defining an id with built-in decoration.
  241. // Can be called multiple times for the same id, if multiple built-ins are
  242. // specified. Seeds id_to_at_reference_checks_ with decorated ids if needed.
  243. spv_result_t ValidateSingleBuiltInAtDefinition(const Decoration& decoration,
  244. const Instruction& inst);
  245. // The following section contains functions which are called when id defined
  246. // by |inst| is decorated with BuiltIn |decoration|.
  247. // Most functions are specific to a single built-in and have naming scheme:
  248. // ValidateXYZAtDefinition. Some functions are common to multiple kinds of
  249. // BuiltIn.
  250. spv_result_t ValidateClipOrCullDistanceAtDefinition(
  251. const Decoration& decoration, const Instruction& inst);
  252. spv_result_t ValidateFragCoordAtDefinition(const Decoration& decoration,
  253. const Instruction& inst);
  254. spv_result_t ValidateFragDepthAtDefinition(const Decoration& decoration,
  255. const Instruction& inst);
  256. spv_result_t ValidateFrontFacingAtDefinition(const Decoration& decoration,
  257. const Instruction& inst);
  258. spv_result_t ValidateHelperInvocationAtDefinition(
  259. const Decoration& decoration, const Instruction& inst);
  260. spv_result_t ValidateInvocationIdAtDefinition(const Decoration& decoration,
  261. const Instruction& inst);
  262. spv_result_t ValidateInstanceIndexAtDefinition(const Decoration& decoration,
  263. const Instruction& inst);
  264. spv_result_t ValidateLayerOrViewportIndexAtDefinition(
  265. const Decoration& decoration, const Instruction& inst);
  266. spv_result_t ValidatePatchVerticesAtDefinition(const Decoration& decoration,
  267. const Instruction& inst);
  268. spv_result_t ValidatePointCoordAtDefinition(const Decoration& decoration,
  269. const Instruction& inst);
  270. spv_result_t ValidatePointSizeAtDefinition(const Decoration& decoration,
  271. const Instruction& inst);
  272. spv_result_t ValidatePositionAtDefinition(const Decoration& decoration,
  273. const Instruction& inst);
  274. spv_result_t ValidatePrimitiveIdAtDefinition(const Decoration& decoration,
  275. const Instruction& inst);
  276. spv_result_t ValidateSampleIdAtDefinition(const Decoration& decoration,
  277. const Instruction& inst);
  278. spv_result_t ValidateSampleMaskAtDefinition(const Decoration& decoration,
  279. const Instruction& inst);
  280. spv_result_t ValidateSamplePositionAtDefinition(const Decoration& decoration,
  281. const Instruction& inst);
  282. spv_result_t ValidateTessCoordAtDefinition(const Decoration& decoration,
  283. const Instruction& inst);
  284. spv_result_t ValidateTessLevelOuterAtDefinition(const Decoration& decoration,
  285. const Instruction& inst);
  286. spv_result_t ValidateTessLevelInnerAtDefinition(const Decoration& decoration,
  287. const Instruction& inst);
  288. spv_result_t ValidateVertexIndexAtDefinition(const Decoration& decoration,
  289. const Instruction& inst);
  290. spv_result_t ValidateVertexIdAtDefinition(const Decoration& decoration,
  291. const Instruction& inst);
  292. spv_result_t ValidateLocalInvocationIndexAtDefinition(
  293. const Decoration& decoration, const Instruction& inst);
  294. spv_result_t ValidateWorkgroupSizeAtDefinition(const Decoration& decoration,
  295. const Instruction& inst);
  296. spv_result_t ValidateBaseInstanceOrVertexAtDefinition(
  297. const Decoration& decoration, const Instruction& inst);
  298. spv_result_t ValidateDrawIndexAtDefinition(const Decoration& decoration,
  299. const Instruction& inst);
  300. spv_result_t ValidateViewIndexAtDefinition(const Decoration& decoration,
  301. const Instruction& inst);
  302. spv_result_t ValidateDeviceIndexAtDefinition(const Decoration& decoration,
  303. const Instruction& inst);
  304. spv_result_t ValidateFragInvocationCountAtDefinition(const Decoration& decoration,
  305. const Instruction& inst);
  306. spv_result_t ValidateFragSizeAtDefinition(const Decoration& decoration,
  307. const Instruction& inst);
  308. spv_result_t ValidateFragStencilRefAtDefinition(const Decoration& decoration,
  309. const Instruction& inst);
  310. spv_result_t ValidateFullyCoveredAtDefinition(const Decoration& decoration,
  311. const Instruction& inst);
  312. // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId.
  313. spv_result_t ValidateComputeShaderI32Vec3InputAtDefinition(
  314. const Decoration& decoration, const Instruction& inst);
  315. spv_result_t ValidateSMBuiltinsAtDefinition(const Decoration& decoration,
  316. const Instruction& inst);
  317. // Used for BaryCoord, BaryCoordNoPersp.
  318. spv_result_t ValidateFragmentShaderF32Vec3InputAtDefinition(
  319. const Decoration& decoration, const Instruction& inst);
  320. // Used for SubgroupEqMask, SubgroupGeMask, SubgroupGtMask, SubgroupLtMask,
  321. // SubgroupLeMask.
  322. spv_result_t ValidateI32Vec4InputAtDefinition(const Decoration& decoration,
  323. const Instruction& inst);
  324. // Used for SubgroupLocalInvocationId, SubgroupSize.
  325. spv_result_t ValidateI32InputAtDefinition(const Decoration& decoration,
  326. const Instruction& inst);
  327. // Used for SubgroupId, NumSubgroups.
  328. spv_result_t ValidateComputeI32InputAtDefinition(const Decoration& decoration,
  329. const Instruction& inst);
  330. spv_result_t ValidatePrimitiveShadingRateAtDefinition(
  331. const Decoration& decoration, const Instruction& inst);
  332. spv_result_t ValidateShadingRateAtDefinition(const Decoration& decoration,
  333. const Instruction& inst);
  334. spv_result_t ValidateRayTracingBuiltinsAtDefinition(
  335. const Decoration& decoration, const Instruction& inst);
  336. // The following section contains functions which are called when id defined
  337. // by |referenced_inst| is
  338. // 1. referenced by |referenced_from_inst|
  339. // 2. dependent on |built_in_inst| which is decorated with BuiltIn
  340. // |decoration|. Most functions are specific to a single built-in and have
  341. // naming scheme: ValidateXYZAtReference. Some functions are common to
  342. // multiple kinds of BuiltIn.
  343. spv_result_t ValidateFragCoordAtReference(
  344. const Decoration& decoration, const Instruction& built_in_inst,
  345. const Instruction& referenced_inst,
  346. const Instruction& referenced_from_inst);
  347. spv_result_t ValidateFragDepthAtReference(
  348. const Decoration& decoration, const Instruction& built_in_inst,
  349. const Instruction& referenced_inst,
  350. const Instruction& referenced_from_inst);
  351. spv_result_t ValidateFrontFacingAtReference(
  352. const Decoration& decoration, const Instruction& built_in_inst,
  353. const Instruction& referenced_inst,
  354. const Instruction& referenced_from_inst);
  355. spv_result_t ValidateHelperInvocationAtReference(
  356. const Decoration& decoration, const Instruction& built_in_inst,
  357. const Instruction& referenced_inst,
  358. const Instruction& referenced_from_inst);
  359. spv_result_t ValidateInvocationIdAtReference(
  360. const Decoration& decoration, const Instruction& built_in_inst,
  361. const Instruction& referenced_inst,
  362. const Instruction& referenced_from_inst);
  363. spv_result_t ValidateInstanceIndexAtReference(
  364. const Decoration& decoration, const Instruction& built_in_inst,
  365. const Instruction& referenced_inst,
  366. const Instruction& referenced_from_inst);
  367. spv_result_t ValidatePatchVerticesAtReference(
  368. const Decoration& decoration, const Instruction& built_in_inst,
  369. const Instruction& referenced_inst,
  370. const Instruction& referenced_from_inst);
  371. spv_result_t ValidatePointCoordAtReference(
  372. const Decoration& decoration, const Instruction& built_in_inst,
  373. const Instruction& referenced_inst,
  374. const Instruction& referenced_from_inst);
  375. spv_result_t ValidatePointSizeAtReference(
  376. const Decoration& decoration, const Instruction& built_in_inst,
  377. const Instruction& referenced_inst,
  378. const Instruction& referenced_from_inst);
  379. spv_result_t ValidatePositionAtReference(
  380. const Decoration& decoration, const Instruction& built_in_inst,
  381. const Instruction& referenced_inst,
  382. const Instruction& referenced_from_inst);
  383. spv_result_t ValidatePrimitiveIdAtReference(
  384. const Decoration& decoration, const Instruction& built_in_inst,
  385. const Instruction& referenced_inst,
  386. const Instruction& referenced_from_inst);
  387. spv_result_t ValidateSampleIdAtReference(
  388. const Decoration& decoration, const Instruction& built_in_inst,
  389. const Instruction& referenced_inst,
  390. const Instruction& referenced_from_inst);
  391. spv_result_t ValidateSampleMaskAtReference(
  392. const Decoration& decoration, const Instruction& built_in_inst,
  393. const Instruction& referenced_inst,
  394. const Instruction& referenced_from_inst);
  395. spv_result_t ValidateSamplePositionAtReference(
  396. const Decoration& decoration, const Instruction& built_in_inst,
  397. const Instruction& referenced_inst,
  398. const Instruction& referenced_from_inst);
  399. spv_result_t ValidateTessCoordAtReference(
  400. const Decoration& decoration, const Instruction& built_in_inst,
  401. const Instruction& referenced_inst,
  402. const Instruction& referenced_from_inst);
  403. spv_result_t ValidateTessLevelAtReference(
  404. const Decoration& decoration, const Instruction& built_in_inst,
  405. const Instruction& referenced_inst,
  406. const Instruction& referenced_from_inst);
  407. spv_result_t ValidateLocalInvocationIndexAtReference(
  408. const Decoration& decoration, const Instruction& built_in_inst,
  409. const Instruction& referenced_inst,
  410. const Instruction& referenced_from_inst);
  411. spv_result_t ValidateVertexIndexAtReference(
  412. const Decoration& decoration, const Instruction& built_in_inst,
  413. const Instruction& referenced_inst,
  414. const Instruction& referenced_from_inst);
  415. spv_result_t ValidateLayerOrViewportIndexAtReference(
  416. const Decoration& decoration, const Instruction& built_in_inst,
  417. const Instruction& referenced_inst,
  418. const Instruction& referenced_from_inst);
  419. spv_result_t ValidateWorkgroupSizeAtReference(
  420. const Decoration& decoration, const Instruction& built_in_inst,
  421. const Instruction& referenced_inst,
  422. const Instruction& referenced_from_inst);
  423. spv_result_t ValidateClipOrCullDistanceAtReference(
  424. const Decoration& decoration, const Instruction& built_in_inst,
  425. const Instruction& referenced_inst,
  426. const Instruction& referenced_from_inst);
  427. spv_result_t ValidateBaseInstanceOrVertexAtReference(
  428. const Decoration& decoration, const Instruction& built_in_inst,
  429. const Instruction& referenced_inst,
  430. const Instruction& referenced_from_inst);
  431. spv_result_t ValidateDrawIndexAtReference(
  432. const Decoration& decoration, const Instruction& built_in_inst,
  433. const Instruction& referenced_inst,
  434. const Instruction& referenced_from_inst);
  435. spv_result_t ValidateViewIndexAtReference(
  436. const Decoration& decoration, const Instruction& built_in_inst,
  437. const Instruction& referenced_inst,
  438. const Instruction& referenced_from_inst);
  439. spv_result_t ValidateDeviceIndexAtReference(
  440. const Decoration& decoration, const Instruction& built_in_inst,
  441. const Instruction& referenced_inst,
  442. const Instruction& referenced_from_inst);
  443. spv_result_t ValidateFragInvocationCountAtReference(
  444. const Decoration& decoration, const Instruction& built_in_inst,
  445. const Instruction& referenced_inst,
  446. const Instruction& referenced_from_inst);
  447. spv_result_t ValidateFragSizeAtReference(
  448. const Decoration& decoration, const Instruction& built_in_inst,
  449. const Instruction& referenced_inst,
  450. const Instruction& referenced_from_inst);
  451. spv_result_t ValidateFragStencilRefAtReference(
  452. const Decoration& decoration, const Instruction& built_in_inst,
  453. const Instruction& referenced_inst,
  454. const Instruction& referenced_from_inst);
  455. spv_result_t ValidateFullyCoveredAtReference(
  456. const Decoration& decoration, const Instruction& built_in_inst,
  457. const Instruction& referenced_inst,
  458. const Instruction& referenced_from_inst);
  459. // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId.
  460. spv_result_t ValidateComputeShaderI32Vec3InputAtReference(
  461. const Decoration& decoration, const Instruction& built_in_inst,
  462. const Instruction& referenced_inst,
  463. const Instruction& referenced_from_inst);
  464. // Used for BaryCoord, BaryCoordNoPersp.
  465. spv_result_t ValidateFragmentShaderF32Vec3InputAtReference(
  466. const Decoration& decoration, const Instruction& built_in_inst,
  467. const Instruction& referenced_inst,
  468. const Instruction& referenced_from_inst);
  469. // Used for SubgroupId and NumSubgroups.
  470. spv_result_t ValidateComputeI32InputAtReference(
  471. const Decoration& decoration, const Instruction& built_in_inst,
  472. const Instruction& referenced_inst,
  473. const Instruction& referenced_from_inst);
  474. spv_result_t ValidateSMBuiltinsAtReference(
  475. const Decoration& decoration, const Instruction& built_in_inst,
  476. const Instruction& referenced_inst,
  477. const Instruction& referenced_from_inst);
  478. spv_result_t ValidatePrimitiveShadingRateAtReference(
  479. const Decoration& decoration, const Instruction& built_in_inst,
  480. const Instruction& referenced_inst,
  481. const Instruction& referenced_from_inst);
  482. spv_result_t ValidateShadingRateAtReference(
  483. const Decoration& decoration, const Instruction& built_in_inst,
  484. const Instruction& referenced_inst,
  485. const Instruction& referenced_from_inst);
  486. spv_result_t ValidateRayTracingBuiltinsAtReference(
  487. const Decoration& decoration, const Instruction& built_in_inst,
  488. const Instruction& referenced_inst,
  489. const Instruction& referenced_from_inst);
  490. // Validates that |built_in_inst| is not (even indirectly) referenced from
  491. // within a function which can be called with |execution_model|.
  492. //
  493. // |vuid| - Vulkan ID for the error, or a negative value if none.
  494. // |comment| - text explaining why the restriction was imposed.
  495. // |decoration| - BuiltIn decoration which causes the restriction.
  496. // |referenced_inst| - instruction which is dependent on |built_in_inst| and
  497. // defines the id which was referenced.
  498. // |referenced_from_inst| - instruction which references id defined by
  499. // |referenced_inst| from within a function.
  500. spv_result_t ValidateNotCalledWithExecutionModel(
  501. int vuid, const char* comment, SpvExecutionModel execution_model,
  502. const Decoration& decoration, const Instruction& built_in_inst,
  503. const Instruction& referenced_inst,
  504. const Instruction& referenced_from_inst);
  505. // The following section contains functions which check that the decorated
  506. // variable has the type specified in the function name. |diag| would be
  507. // called with a corresponding error message, if validation is not successful.
  508. spv_result_t ValidateBool(
  509. const Decoration& decoration, const Instruction& inst,
  510. const std::function<spv_result_t(const std::string& message)>& diag);
  511. spv_result_t ValidateI(
  512. const Decoration& decoration, const Instruction& inst,
  513. const std::function<spv_result_t(const std::string& message)>& diag);
  514. spv_result_t ValidateI32(
  515. const Decoration& decoration, const Instruction& inst,
  516. const std::function<spv_result_t(const std::string& message)>& diag);
  517. spv_result_t ValidateI32Vec(
  518. const Decoration& decoration, const Instruction& inst,
  519. uint32_t num_components,
  520. const std::function<spv_result_t(const std::string& message)>& diag);
  521. spv_result_t ValidateI32Arr(
  522. const Decoration& decoration, const Instruction& inst,
  523. const std::function<spv_result_t(const std::string& message)>& diag);
  524. spv_result_t ValidateOptionalArrayedI32(
  525. const Decoration& decoration, const Instruction& inst,
  526. const std::function<spv_result_t(const std::string& message)>& diag);
  527. spv_result_t ValidateI32Helper(
  528. const Decoration& decoration, const Instruction& inst,
  529. const std::function<spv_result_t(const std::string& message)>& diag,
  530. uint32_t underlying_type);
  531. spv_result_t ValidateF32(
  532. const Decoration& decoration, const Instruction& inst,
  533. const std::function<spv_result_t(const std::string& message)>& diag);
  534. spv_result_t ValidateOptionalArrayedF32(
  535. const Decoration& decoration, const Instruction& inst,
  536. const std::function<spv_result_t(const std::string& message)>& diag);
  537. spv_result_t ValidateF32Helper(
  538. const Decoration& decoration, const Instruction& inst,
  539. const std::function<spv_result_t(const std::string& message)>& diag,
  540. uint32_t underlying_type);
  541. spv_result_t ValidateF32Vec(
  542. const Decoration& decoration, const Instruction& inst,
  543. uint32_t num_components,
  544. const std::function<spv_result_t(const std::string& message)>& diag);
  545. spv_result_t ValidateOptionalArrayedF32Vec(
  546. const Decoration& decoration, const Instruction& inst,
  547. uint32_t num_components,
  548. const std::function<spv_result_t(const std::string& message)>& diag);
  549. spv_result_t ValidateF32VecHelper(
  550. const Decoration& decoration, const Instruction& inst,
  551. uint32_t num_components,
  552. const std::function<spv_result_t(const std::string& message)>& diag,
  553. uint32_t underlying_type);
  554. // If |num_components| is zero, the number of components is not checked.
  555. spv_result_t ValidateF32Arr(
  556. const Decoration& decoration, const Instruction& inst,
  557. uint32_t num_components,
  558. const std::function<spv_result_t(const std::string& message)>& diag);
  559. spv_result_t ValidateOptionalArrayedF32Arr(
  560. const Decoration& decoration, const Instruction& inst,
  561. uint32_t num_components,
  562. const std::function<spv_result_t(const std::string& message)>& diag);
  563. spv_result_t ValidateF32ArrHelper(
  564. const Decoration& decoration, const Instruction& inst,
  565. uint32_t num_components,
  566. const std::function<spv_result_t(const std::string& message)>& diag,
  567. uint32_t underlying_type);
  568. spv_result_t ValidateF32Mat(
  569. const Decoration& decoration, const Instruction& inst,
  570. uint32_t req_num_rows, uint32_t req_num_columns,
  571. const std::function<spv_result_t(const std::string& message)>& diag);
  572. // Generates strings like "Member #0 of struct ID <2>".
  573. std::string GetDefinitionDesc(const Decoration& decoration,
  574. const Instruction& inst) const;
  575. // Generates strings like "ID <51> (OpTypePointer) is referencing ID <2>
  576. // (OpTypeStruct) which is decorated with BuiltIn Position".
  577. std::string GetReferenceDesc(
  578. const Decoration& decoration, const Instruction& built_in_inst,
  579. const Instruction& referenced_inst,
  580. const Instruction& referenced_from_inst,
  581. SpvExecutionModel execution_model = SpvExecutionModelMax) const;
  582. // Generates strings like "ID <51> (OpTypePointer) uses storage class
  583. // UniformConstant".
  584. std::string GetStorageClassDesc(const Instruction& inst) const;
  585. // Updates inner working of the class. Is called sequentially for every
  586. // instruction.
  587. void Update(const Instruction& inst);
  588. ValidationState_t& _;
  589. // Mapping id -> list of rules which validate instruction referencing the
  590. // id. Rules can create new rules and add them to this container.
  591. // Using std::map, and not std::unordered_map to avoid iterator invalidation
  592. // during rehashing.
  593. std::map<uint32_t, std::list<std::function<spv_result_t(const Instruction&)>>>
  594. id_to_at_reference_checks_;
  595. // Id of the function we are currently inside. 0 if not inside a function.
  596. uint32_t function_id_ = 0;
  597. // Entry points which can (indirectly) call the current function.
  598. // The pointer either points to a vector inside to function_to_entry_points_
  599. // or to no_entry_points_. The pointer is guaranteed to never be null.
  600. const std::vector<uint32_t> no_entry_points;
  601. const std::vector<uint32_t>* entry_points_ = &no_entry_points;
  602. // Execution models with which the current function can be called.
  603. std::set<SpvExecutionModel> execution_models_;
  604. };
  605. void BuiltInsValidator::Update(const Instruction& inst) {
  606. const SpvOp opcode = inst.opcode();
  607. if (opcode == SpvOpFunction) {
  608. // Entering a function.
  609. assert(function_id_ == 0);
  610. function_id_ = inst.id();
  611. execution_models_.clear();
  612. entry_points_ = &_.FunctionEntryPoints(function_id_);
  613. // Collect execution models from all entry points from which the current
  614. // function can be called.
  615. for (const uint32_t entry_point : *entry_points_) {
  616. if (const auto* models = _.GetExecutionModels(entry_point)) {
  617. execution_models_.insert(models->begin(), models->end());
  618. }
  619. }
  620. }
  621. if (opcode == SpvOpFunctionEnd) {
  622. // Exiting a function.
  623. assert(function_id_ != 0);
  624. function_id_ = 0;
  625. entry_points_ = &no_entry_points;
  626. execution_models_.clear();
  627. }
  628. }
  629. std::string BuiltInsValidator::GetDefinitionDesc(
  630. const Decoration& decoration, const Instruction& inst) const {
  631. std::ostringstream ss;
  632. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  633. assert(inst.opcode() == SpvOpTypeStruct);
  634. ss << "Member #" << decoration.struct_member_index();
  635. ss << " of struct ID <" << inst.id() << ">";
  636. } else {
  637. ss << GetIdDesc(inst);
  638. }
  639. return ss.str();
  640. }
  641. std::string BuiltInsValidator::GetReferenceDesc(
  642. const Decoration& decoration, const Instruction& built_in_inst,
  643. const Instruction& referenced_inst, const Instruction& referenced_from_inst,
  644. SpvExecutionModel execution_model) const {
  645. std::ostringstream ss;
  646. ss << GetIdDesc(referenced_from_inst) << " is referencing "
  647. << GetIdDesc(referenced_inst);
  648. if (built_in_inst.id() != referenced_inst.id()) {
  649. ss << " which is dependent on " << GetIdDesc(built_in_inst);
  650. }
  651. ss << " which is decorated with BuiltIn ";
  652. ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  653. decoration.params()[0]);
  654. if (function_id_) {
  655. ss << " in function <" << function_id_ << ">";
  656. if (execution_model != SpvExecutionModelMax) {
  657. ss << " called with execution model ";
  658. ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_EXECUTION_MODEL,
  659. execution_model);
  660. }
  661. }
  662. ss << ".";
  663. return ss.str();
  664. }
  665. std::string BuiltInsValidator::GetStorageClassDesc(
  666. const Instruction& inst) const {
  667. std::ostringstream ss;
  668. ss << GetIdDesc(inst) << " uses storage class ";
  669. ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_STORAGE_CLASS,
  670. GetStorageClass(inst));
  671. ss << ".";
  672. return ss.str();
  673. }
  674. spv_result_t BuiltInsValidator::ValidateBool(
  675. const Decoration& decoration, const Instruction& inst,
  676. const std::function<spv_result_t(const std::string& message)>& diag) {
  677. uint32_t underlying_type = 0;
  678. if (spv_result_t error =
  679. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  680. return error;
  681. }
  682. if (!_.IsBoolScalarType(underlying_type)) {
  683. return diag(GetDefinitionDesc(decoration, inst) + " is not a bool scalar.");
  684. }
  685. return SPV_SUCCESS;
  686. }
  687. spv_result_t BuiltInsValidator::ValidateI(
  688. const Decoration& decoration, const Instruction& inst,
  689. const std::function<spv_result_t(const std::string& message)>& diag) {
  690. uint32_t underlying_type = 0;
  691. if (spv_result_t error =
  692. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  693. return error;
  694. }
  695. if (!_.IsIntScalarType(underlying_type)) {
  696. return diag(GetDefinitionDesc(decoration, inst) + " is not an int scalar.");
  697. }
  698. return SPV_SUCCESS;
  699. }
  700. spv_result_t BuiltInsValidator::ValidateI32(
  701. const Decoration& decoration, const Instruction& inst,
  702. const std::function<spv_result_t(const std::string& message)>& diag) {
  703. uint32_t underlying_type = 0;
  704. if (spv_result_t error =
  705. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  706. return error;
  707. }
  708. return ValidateI32Helper(decoration, inst, diag, underlying_type);
  709. }
  710. spv_result_t BuiltInsValidator::ValidateOptionalArrayedI32(
  711. const Decoration& decoration, const Instruction& inst,
  712. const std::function<spv_result_t(const std::string& message)>& diag) {
  713. uint32_t underlying_type = 0;
  714. if (spv_result_t error =
  715. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  716. return error;
  717. }
  718. // Strip the array, if present.
  719. if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
  720. underlying_type = _.FindDef(underlying_type)->word(2u);
  721. }
  722. return ValidateI32Helper(decoration, inst, diag, underlying_type);
  723. }
  724. spv_result_t BuiltInsValidator::ValidateI32Helper(
  725. const Decoration& decoration, const Instruction& inst,
  726. const std::function<spv_result_t(const std::string& message)>& diag,
  727. uint32_t underlying_type) {
  728. if (!_.IsIntScalarType(underlying_type)) {
  729. return diag(GetDefinitionDesc(decoration, inst) + " is not an int scalar.");
  730. }
  731. const uint32_t bit_width = _.GetBitWidth(underlying_type);
  732. if (bit_width != 32) {
  733. std::ostringstream ss;
  734. ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width
  735. << ".";
  736. return diag(ss.str());
  737. }
  738. return SPV_SUCCESS;
  739. }
  740. spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32(
  741. const Decoration& decoration, const Instruction& inst,
  742. const std::function<spv_result_t(const std::string& message)>& diag) {
  743. uint32_t underlying_type = 0;
  744. if (spv_result_t error =
  745. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  746. return error;
  747. }
  748. // Strip the array, if present.
  749. if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
  750. underlying_type = _.FindDef(underlying_type)->word(2u);
  751. }
  752. return ValidateF32Helper(decoration, inst, diag, underlying_type);
  753. }
  754. spv_result_t BuiltInsValidator::ValidateF32(
  755. const Decoration& decoration, const Instruction& inst,
  756. const std::function<spv_result_t(const std::string& message)>& diag) {
  757. uint32_t underlying_type = 0;
  758. if (spv_result_t error =
  759. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  760. return error;
  761. }
  762. return ValidateF32Helper(decoration, inst, diag, underlying_type);
  763. }
  764. spv_result_t BuiltInsValidator::ValidateF32Helper(
  765. const Decoration& decoration, const Instruction& inst,
  766. const std::function<spv_result_t(const std::string& message)>& diag,
  767. uint32_t underlying_type) {
  768. if (!_.IsFloatScalarType(underlying_type)) {
  769. return diag(GetDefinitionDesc(decoration, inst) +
  770. " is not a float scalar.");
  771. }
  772. const uint32_t bit_width = _.GetBitWidth(underlying_type);
  773. if (bit_width != 32) {
  774. std::ostringstream ss;
  775. ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width
  776. << ".";
  777. return diag(ss.str());
  778. }
  779. return SPV_SUCCESS;
  780. }
  781. spv_result_t BuiltInsValidator::ValidateI32Vec(
  782. const Decoration& decoration, const Instruction& inst,
  783. uint32_t num_components,
  784. const std::function<spv_result_t(const std::string& message)>& diag) {
  785. uint32_t underlying_type = 0;
  786. if (spv_result_t error =
  787. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  788. return error;
  789. }
  790. if (!_.IsIntVectorType(underlying_type)) {
  791. return diag(GetDefinitionDesc(decoration, inst) + " is not an int vector.");
  792. }
  793. const uint32_t actual_num_components = _.GetDimension(underlying_type);
  794. if (_.GetDimension(underlying_type) != num_components) {
  795. std::ostringstream ss;
  796. ss << GetDefinitionDesc(decoration, inst) << " has "
  797. << actual_num_components << " components.";
  798. return diag(ss.str());
  799. }
  800. const uint32_t bit_width = _.GetBitWidth(underlying_type);
  801. if (bit_width != 32) {
  802. std::ostringstream ss;
  803. ss << GetDefinitionDesc(decoration, inst)
  804. << " has components with bit width " << bit_width << ".";
  805. return diag(ss.str());
  806. }
  807. return SPV_SUCCESS;
  808. }
  809. spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32Vec(
  810. const Decoration& decoration, const Instruction& inst,
  811. uint32_t num_components,
  812. const std::function<spv_result_t(const std::string& message)>& diag) {
  813. uint32_t underlying_type = 0;
  814. if (spv_result_t error =
  815. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  816. return error;
  817. }
  818. // Strip the array, if present.
  819. if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
  820. underlying_type = _.FindDef(underlying_type)->word(2u);
  821. }
  822. return ValidateF32VecHelper(decoration, inst, num_components, diag,
  823. underlying_type);
  824. }
  825. spv_result_t BuiltInsValidator::ValidateF32Vec(
  826. const Decoration& decoration, const Instruction& inst,
  827. uint32_t num_components,
  828. const std::function<spv_result_t(const std::string& message)>& diag) {
  829. uint32_t underlying_type = 0;
  830. if (spv_result_t error =
  831. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  832. return error;
  833. }
  834. return ValidateF32VecHelper(decoration, inst, num_components, diag,
  835. underlying_type);
  836. }
  837. spv_result_t BuiltInsValidator::ValidateF32VecHelper(
  838. const Decoration& decoration, const Instruction& inst,
  839. uint32_t num_components,
  840. const std::function<spv_result_t(const std::string& message)>& diag,
  841. uint32_t underlying_type) {
  842. if (!_.IsFloatVectorType(underlying_type)) {
  843. return diag(GetDefinitionDesc(decoration, inst) +
  844. " is not a float vector.");
  845. }
  846. const uint32_t actual_num_components = _.GetDimension(underlying_type);
  847. if (_.GetDimension(underlying_type) != num_components) {
  848. std::ostringstream ss;
  849. ss << GetDefinitionDesc(decoration, inst) << " has "
  850. << actual_num_components << " components.";
  851. return diag(ss.str());
  852. }
  853. const uint32_t bit_width = _.GetBitWidth(underlying_type);
  854. if (bit_width != 32) {
  855. std::ostringstream ss;
  856. ss << GetDefinitionDesc(decoration, inst)
  857. << " has components with bit width " << bit_width << ".";
  858. return diag(ss.str());
  859. }
  860. return SPV_SUCCESS;
  861. }
  862. spv_result_t BuiltInsValidator::ValidateI32Arr(
  863. const Decoration& decoration, const Instruction& inst,
  864. const std::function<spv_result_t(const std::string& message)>& diag) {
  865. uint32_t underlying_type = 0;
  866. if (spv_result_t error =
  867. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  868. return error;
  869. }
  870. const Instruction* const type_inst = _.FindDef(underlying_type);
  871. if (type_inst->opcode() != SpvOpTypeArray) {
  872. return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
  873. }
  874. const uint32_t component_type = type_inst->word(2);
  875. if (!_.IsIntScalarType(component_type)) {
  876. return diag(GetDefinitionDesc(decoration, inst) +
  877. " components are not int scalar.");
  878. }
  879. const uint32_t bit_width = _.GetBitWidth(component_type);
  880. if (bit_width != 32) {
  881. std::ostringstream ss;
  882. ss << GetDefinitionDesc(decoration, inst)
  883. << " has components with bit width " << bit_width << ".";
  884. return diag(ss.str());
  885. }
  886. return SPV_SUCCESS;
  887. }
  888. spv_result_t BuiltInsValidator::ValidateF32Arr(
  889. const Decoration& decoration, const Instruction& inst,
  890. uint32_t num_components,
  891. const std::function<spv_result_t(const std::string& message)>& diag) {
  892. uint32_t underlying_type = 0;
  893. if (spv_result_t error =
  894. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  895. return error;
  896. }
  897. return ValidateF32ArrHelper(decoration, inst, num_components, diag,
  898. underlying_type);
  899. }
  900. spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32Arr(
  901. const Decoration& decoration, const Instruction& inst,
  902. uint32_t num_components,
  903. const std::function<spv_result_t(const std::string& message)>& diag) {
  904. uint32_t underlying_type = 0;
  905. if (spv_result_t error =
  906. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  907. return error;
  908. }
  909. // Strip an extra layer of arraying if present.
  910. if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
  911. uint32_t subtype = _.FindDef(underlying_type)->word(2u);
  912. if (_.GetIdOpcode(subtype) == SpvOpTypeArray) {
  913. underlying_type = subtype;
  914. }
  915. }
  916. return ValidateF32ArrHelper(decoration, inst, num_components, diag,
  917. underlying_type);
  918. }
  919. spv_result_t BuiltInsValidator::ValidateF32ArrHelper(
  920. const Decoration& decoration, const Instruction& inst,
  921. uint32_t num_components,
  922. const std::function<spv_result_t(const std::string& message)>& diag,
  923. uint32_t underlying_type) {
  924. const Instruction* const type_inst = _.FindDef(underlying_type);
  925. if (type_inst->opcode() != SpvOpTypeArray) {
  926. return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
  927. }
  928. const uint32_t component_type = type_inst->word(2);
  929. if (!_.IsFloatScalarType(component_type)) {
  930. return diag(GetDefinitionDesc(decoration, inst) +
  931. " components are not float scalar.");
  932. }
  933. const uint32_t bit_width = _.GetBitWidth(component_type);
  934. if (bit_width != 32) {
  935. std::ostringstream ss;
  936. ss << GetDefinitionDesc(decoration, inst)
  937. << " has components with bit width " << bit_width << ".";
  938. return diag(ss.str());
  939. }
  940. if (num_components != 0) {
  941. uint64_t actual_num_components = 0;
  942. if (!_.GetConstantValUint64(type_inst->word(3), &actual_num_components)) {
  943. assert(0 && "Array type definition is corrupt");
  944. }
  945. if (actual_num_components != num_components) {
  946. std::ostringstream ss;
  947. ss << GetDefinitionDesc(decoration, inst) << " has "
  948. << actual_num_components << " components.";
  949. return diag(ss.str());
  950. }
  951. }
  952. return SPV_SUCCESS;
  953. }
  954. spv_result_t BuiltInsValidator::ValidateF32Mat(
  955. const Decoration& decoration, const Instruction& inst,
  956. uint32_t req_num_rows, uint32_t req_num_columns,
  957. const std::function<spv_result_t(const std::string& message)>& diag) {
  958. uint32_t underlying_type = 0;
  959. uint32_t num_rows = 0;
  960. uint32_t num_cols = 0;
  961. uint32_t col_type = 0;
  962. uint32_t component_type = 0;
  963. if (spv_result_t error =
  964. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  965. return error;
  966. }
  967. if (!_.GetMatrixTypeInfo(underlying_type, &num_rows, &num_cols, &col_type,
  968. &component_type) ||
  969. num_rows != req_num_rows || num_cols != req_num_columns) {
  970. std::ostringstream ss;
  971. ss << GetDefinitionDesc(decoration, inst) << " has columns " << num_cols
  972. << " and rows " << num_rows << " not equal to expected "
  973. << req_num_columns << "x" << req_num_rows << ".";
  974. return diag(ss.str());
  975. }
  976. return ValidateF32VecHelper(decoration, inst, req_num_rows, diag, col_type);
  977. }
  978. spv_result_t BuiltInsValidator::ValidateNotCalledWithExecutionModel(
  979. int vuid, const char* comment, SpvExecutionModel execution_model,
  980. const Decoration& decoration, const Instruction& built_in_inst,
  981. const Instruction& referenced_inst,
  982. const Instruction& referenced_from_inst) {
  983. if (function_id_) {
  984. if (execution_models_.count(execution_model)) {
  985. const char* execution_model_str = _.grammar().lookupOperandName(
  986. SPV_OPERAND_TYPE_EXECUTION_MODEL, execution_model);
  987. const char* built_in_str = _.grammar().lookupOperandName(
  988. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0]);
  989. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  990. << (vuid < 0 ? std::string("") : _.VkErrorID(vuid)) << comment
  991. << " " << GetIdDesc(referenced_inst) << " depends on "
  992. << GetIdDesc(built_in_inst) << " which is decorated with BuiltIn "
  993. << built_in_str << "."
  994. << " Id <" << referenced_inst.id() << "> is later referenced by "
  995. << GetIdDesc(referenced_from_inst) << " in function <"
  996. << function_id_ << "> which is called with execution model "
  997. << execution_model_str << ".";
  998. }
  999. } else {
  1000. // Propagate this rule to all dependant ids in the global scope.
  1001. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  1002. std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  1003. vuid, comment, execution_model, decoration, built_in_inst,
  1004. referenced_from_inst, std::placeholders::_1));
  1005. }
  1006. return SPV_SUCCESS;
  1007. }
  1008. spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtDefinition(
  1009. const Decoration& decoration, const Instruction& inst) {
  1010. // Seed at reference checks with this built-in.
  1011. return ValidateClipOrCullDistanceAtReference(decoration, inst, inst, inst);
  1012. }
  1013. spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtReference(
  1014. const Decoration& decoration, const Instruction& built_in_inst,
  1015. const Instruction& referenced_inst,
  1016. const Instruction& referenced_from_inst) {
  1017. uint32_t operand = decoration.params()[0];
  1018. if (spvIsVulkanEnv(_.context()->target_env)) {
  1019. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1020. if (storage_class != SpvStorageClassMax &&
  1021. storage_class != SpvStorageClassInput &&
  1022. storage_class != SpvStorageClassOutput) {
  1023. uint32_t vuid = (decoration.params()[0] == SpvBuiltInClipDistance) ? 4190 : 4199;
  1024. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1025. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  1026. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  1027. operand)
  1028. << " to be only used for variables with Input or Output storage "
  1029. "class. "
  1030. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1031. referenced_from_inst)
  1032. << " " << GetStorageClassDesc(referenced_from_inst);
  1033. }
  1034. if (storage_class == SpvStorageClassInput) {
  1035. assert(function_id_ == 0);
  1036. uint32_t vuid = (decoration.params()[0] == SpvBuiltInClipDistance) ? 4188 : 4197;
  1037. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1038. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  1039. "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
  1040. "used for variables with Input storage class if execution model is "
  1041. "Vertex.",
  1042. SpvExecutionModelVertex, decoration, built_in_inst,
  1043. referenced_from_inst, std::placeholders::_1));
  1044. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1045. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  1046. "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
  1047. "used for variables with Input storage class if execution model is "
  1048. "Vertex.",
  1049. SpvExecutionModelMeshNV, decoration, built_in_inst,
  1050. referenced_from_inst, std::placeholders::_1));
  1051. }
  1052. if (storage_class == SpvStorageClassOutput) {
  1053. assert(function_id_ == 0);
  1054. uint32_t vuid = (decoration.params()[0] == SpvBuiltInClipDistance) ? 4189 : 4198;
  1055. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1056. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  1057. "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
  1058. "used for variables with Output storage class if execution model is "
  1059. "Fragment.",
  1060. SpvExecutionModelFragment, decoration, built_in_inst,
  1061. referenced_from_inst, std::placeholders::_1));
  1062. }
  1063. for (const SpvExecutionModel execution_model : execution_models_) {
  1064. switch (execution_model) {
  1065. case SpvExecutionModelFragment:
  1066. case SpvExecutionModelVertex: {
  1067. if (spv_result_t error = ValidateF32Arr(
  1068. decoration, built_in_inst, /* Any number of components */ 0,
  1069. [this, &decoration, &referenced_from_inst](
  1070. const std::string& message) -> spv_result_t {
  1071. uint32_t vuid =
  1072. (decoration.params()[0] == SpvBuiltInClipDistance)
  1073. ? 4191
  1074. : 4200;
  1075. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1076. << _.VkErrorID(vuid)
  1077. << "According to the Vulkan spec BuiltIn "
  1078. << _.grammar().lookupOperandName(
  1079. SPV_OPERAND_TYPE_BUILT_IN,
  1080. decoration.params()[0])
  1081. << " variable needs to be a 32-bit float array. "
  1082. << message;
  1083. })) {
  1084. return error;
  1085. }
  1086. break;
  1087. }
  1088. case SpvExecutionModelTessellationControl:
  1089. case SpvExecutionModelTessellationEvaluation:
  1090. case SpvExecutionModelGeometry:
  1091. case SpvExecutionModelMeshNV: {
  1092. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1093. // The outer level of array is applied on the variable.
  1094. if (spv_result_t error = ValidateF32Arr(
  1095. decoration, built_in_inst, /* Any number of components */ 0,
  1096. [this, &decoration, &referenced_from_inst](
  1097. const std::string& message) -> spv_result_t {
  1098. uint32_t vuid =
  1099. (decoration.params()[0] == SpvBuiltInClipDistance)
  1100. ? 4191
  1101. : 4200;
  1102. return _.diag(SPV_ERROR_INVALID_DATA,
  1103. &referenced_from_inst)
  1104. << _.VkErrorID(vuid)
  1105. << "According to the Vulkan spec BuiltIn "
  1106. << _.grammar().lookupOperandName(
  1107. SPV_OPERAND_TYPE_BUILT_IN,
  1108. decoration.params()[0])
  1109. << " variable needs to be a 32-bit float array. "
  1110. << message;
  1111. })) {
  1112. return error;
  1113. }
  1114. } else {
  1115. if (spv_result_t error = ValidateOptionalArrayedF32Arr(
  1116. decoration, built_in_inst, /* Any number of components */ 0,
  1117. [this, &decoration, &referenced_from_inst](
  1118. const std::string& message) -> spv_result_t {
  1119. uint32_t vuid =
  1120. (decoration.params()[0] == SpvBuiltInClipDistance)
  1121. ? 4191
  1122. : 4200;
  1123. return _.diag(SPV_ERROR_INVALID_DATA,
  1124. &referenced_from_inst)
  1125. << _.VkErrorID(vuid)
  1126. << "According to the Vulkan spec BuiltIn "
  1127. << _.grammar().lookupOperandName(
  1128. SPV_OPERAND_TYPE_BUILT_IN,
  1129. decoration.params()[0])
  1130. << " variable needs to be a 32-bit float array. "
  1131. << message;
  1132. })) {
  1133. return error;
  1134. }
  1135. }
  1136. break;
  1137. }
  1138. default: {
  1139. uint32_t vuid =
  1140. (decoration.params()[0] == SpvBuiltInClipDistance) ? 4187 : 4196;
  1141. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1142. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  1143. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  1144. operand)
  1145. << " to be used only with Fragment, Vertex, "
  1146. "TessellationControl, TessellationEvaluation or Geometry "
  1147. "execution models. "
  1148. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1149. referenced_from_inst, execution_model);
  1150. }
  1151. }
  1152. }
  1153. }
  1154. if (function_id_ == 0) {
  1155. // Propagate this rule to all dependant ids in the global scope.
  1156. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  1157. std::bind(&BuiltInsValidator::ValidateClipOrCullDistanceAtReference,
  1158. this, decoration, built_in_inst, referenced_from_inst,
  1159. std::placeholders::_1));
  1160. }
  1161. return SPV_SUCCESS;
  1162. }
  1163. spv_result_t BuiltInsValidator::ValidateFragCoordAtDefinition(
  1164. const Decoration& decoration, const Instruction& inst) {
  1165. if (spvIsVulkanEnv(_.context()->target_env)) {
  1166. if (spv_result_t error = ValidateF32Vec(
  1167. decoration, inst, 4,
  1168. [this, &inst](const std::string& message) -> spv_result_t {
  1169. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1170. << _.VkErrorID(4212) << "According to the "
  1171. << spvLogStringForEnv(_.context()->target_env)
  1172. << " spec BuiltIn FragCoord "
  1173. "variable needs to be a 4-component 32-bit float "
  1174. "vector. "
  1175. << message;
  1176. })) {
  1177. return error;
  1178. }
  1179. }
  1180. // Seed at reference checks with this built-in.
  1181. return ValidateFragCoordAtReference(decoration, inst, inst, inst);
  1182. }
  1183. spv_result_t BuiltInsValidator::ValidateFragCoordAtReference(
  1184. const Decoration& decoration, const Instruction& built_in_inst,
  1185. const Instruction& referenced_inst,
  1186. const Instruction& referenced_from_inst) {
  1187. if (spvIsVulkanEnv(_.context()->target_env)) {
  1188. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1189. if (storage_class != SpvStorageClassMax &&
  1190. storage_class != SpvStorageClassInput) {
  1191. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1192. << _.VkErrorID(4211) << spvLogStringForEnv(_.context()->target_env)
  1193. << " spec allows BuiltIn FragCoord to be only used for "
  1194. "variables with Input storage class. "
  1195. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1196. referenced_from_inst)
  1197. << " " << GetStorageClassDesc(referenced_from_inst);
  1198. }
  1199. for (const SpvExecutionModel execution_model : execution_models_) {
  1200. if (execution_model != SpvExecutionModelFragment) {
  1201. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1202. << _.VkErrorID(4210)
  1203. << spvLogStringForEnv(_.context()->target_env)
  1204. << " spec allows BuiltIn FragCoord to be used only with "
  1205. "Fragment execution model. "
  1206. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1207. referenced_from_inst, execution_model);
  1208. }
  1209. }
  1210. }
  1211. if (function_id_ == 0) {
  1212. // Propagate this rule to all dependant ids in the global scope.
  1213. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1214. &BuiltInsValidator::ValidateFragCoordAtReference, this, decoration,
  1215. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1216. }
  1217. return SPV_SUCCESS;
  1218. }
  1219. spv_result_t BuiltInsValidator::ValidateFragDepthAtDefinition(
  1220. const Decoration& decoration, const Instruction& inst) {
  1221. if (spvIsVulkanEnv(_.context()->target_env)) {
  1222. if (spv_result_t error = ValidateF32(
  1223. decoration, inst,
  1224. [this, &inst](const std::string& message) -> spv_result_t {
  1225. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1226. << _.VkErrorID(4215) << "According to the "
  1227. << spvLogStringForEnv(_.context()->target_env)
  1228. << " spec BuiltIn FragDepth "
  1229. "variable needs to be a 32-bit float scalar. "
  1230. << message;
  1231. })) {
  1232. return error;
  1233. }
  1234. }
  1235. // Seed at reference checks with this built-in.
  1236. return ValidateFragDepthAtReference(decoration, inst, inst, inst);
  1237. }
  1238. spv_result_t BuiltInsValidator::ValidateFragDepthAtReference(
  1239. const Decoration& decoration, const Instruction& built_in_inst,
  1240. const Instruction& referenced_inst,
  1241. const Instruction& referenced_from_inst) {
  1242. if (spvIsVulkanEnv(_.context()->target_env)) {
  1243. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1244. if (storage_class != SpvStorageClassMax &&
  1245. storage_class != SpvStorageClassOutput) {
  1246. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1247. << _.VkErrorID(4214) << spvLogStringForEnv(_.context()->target_env)
  1248. << " spec allows BuiltIn FragDepth to be only used for "
  1249. "variables with Output storage class. "
  1250. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1251. referenced_from_inst)
  1252. << " " << GetStorageClassDesc(referenced_from_inst);
  1253. }
  1254. for (const SpvExecutionModel execution_model : execution_models_) {
  1255. if (execution_model != SpvExecutionModelFragment) {
  1256. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1257. << _.VkErrorID(4213)
  1258. << spvLogStringForEnv(_.context()->target_env)
  1259. << " spec allows BuiltIn FragDepth to be used only with "
  1260. "Fragment execution model. "
  1261. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1262. referenced_from_inst, execution_model);
  1263. }
  1264. }
  1265. for (const uint32_t entry_point : *entry_points_) {
  1266. // Every entry point from which this function is called needs to have
  1267. // Execution Mode DepthReplacing.
  1268. const auto* modes = _.GetExecutionModes(entry_point);
  1269. if (!modes || !modes->count(SpvExecutionModeDepthReplacing)) {
  1270. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1271. << _.VkErrorID(4216)
  1272. << spvLogStringForEnv(_.context()->target_env)
  1273. << " spec requires DepthReplacing execution mode to be "
  1274. "declared when using BuiltIn FragDepth. "
  1275. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1276. referenced_from_inst);
  1277. }
  1278. }
  1279. }
  1280. if (function_id_ == 0) {
  1281. // Propagate this rule to all dependant ids in the global scope.
  1282. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1283. &BuiltInsValidator::ValidateFragDepthAtReference, this, decoration,
  1284. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1285. }
  1286. return SPV_SUCCESS;
  1287. }
  1288. spv_result_t BuiltInsValidator::ValidateFrontFacingAtDefinition(
  1289. const Decoration& decoration, const Instruction& inst) {
  1290. if (spvIsVulkanEnv(_.context()->target_env)) {
  1291. if (spv_result_t error = ValidateBool(
  1292. decoration, inst,
  1293. [this, &inst](const std::string& message) -> spv_result_t {
  1294. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1295. << _.VkErrorID(4231) << "According to the "
  1296. << spvLogStringForEnv(_.context()->target_env)
  1297. << " spec BuiltIn FrontFacing "
  1298. "variable needs to be a bool scalar. "
  1299. << message;
  1300. })) {
  1301. return error;
  1302. }
  1303. }
  1304. // Seed at reference checks with this built-in.
  1305. return ValidateFrontFacingAtReference(decoration, inst, inst, inst);
  1306. }
  1307. spv_result_t BuiltInsValidator::ValidateFrontFacingAtReference(
  1308. const Decoration& decoration, const Instruction& built_in_inst,
  1309. const Instruction& referenced_inst,
  1310. const Instruction& referenced_from_inst) {
  1311. if (spvIsVulkanEnv(_.context()->target_env)) {
  1312. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1313. if (storage_class != SpvStorageClassMax &&
  1314. storage_class != SpvStorageClassInput) {
  1315. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1316. << _.VkErrorID(4230) << spvLogStringForEnv(_.context()->target_env)
  1317. << " spec allows BuiltIn FrontFacing to be only used for "
  1318. "variables with Input storage class. "
  1319. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1320. referenced_from_inst)
  1321. << " " << GetStorageClassDesc(referenced_from_inst);
  1322. }
  1323. for (const SpvExecutionModel execution_model : execution_models_) {
  1324. if (execution_model != SpvExecutionModelFragment) {
  1325. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1326. << _.VkErrorID(4229)
  1327. << spvLogStringForEnv(_.context()->target_env)
  1328. << " spec allows BuiltIn FrontFacing to be used only with "
  1329. "Fragment execution model. "
  1330. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1331. referenced_from_inst, execution_model);
  1332. }
  1333. }
  1334. }
  1335. if (function_id_ == 0) {
  1336. // Propagate this rule to all dependant ids in the global scope.
  1337. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1338. &BuiltInsValidator::ValidateFrontFacingAtReference, this, decoration,
  1339. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1340. }
  1341. return SPV_SUCCESS;
  1342. }
  1343. spv_result_t BuiltInsValidator::ValidateHelperInvocationAtDefinition(
  1344. const Decoration& decoration, const Instruction& inst) {
  1345. if (spvIsVulkanEnv(_.context()->target_env)) {
  1346. if (spv_result_t error = ValidateBool(
  1347. decoration, inst,
  1348. [this, &inst](const std::string& message) -> spv_result_t {
  1349. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1350. << _.VkErrorID(4241)
  1351. << "According to the Vulkan spec BuiltIn HelperInvocation "
  1352. "variable needs to be a bool scalar. "
  1353. << message;
  1354. })) {
  1355. return error;
  1356. }
  1357. }
  1358. // Seed at reference checks with this built-in.
  1359. return ValidateHelperInvocationAtReference(decoration, inst, inst, inst);
  1360. }
  1361. spv_result_t BuiltInsValidator::ValidateHelperInvocationAtReference(
  1362. const Decoration& decoration, const Instruction& built_in_inst,
  1363. const Instruction& referenced_inst,
  1364. const Instruction& referenced_from_inst) {
  1365. if (spvIsVulkanEnv(_.context()->target_env)) {
  1366. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1367. if (storage_class != SpvStorageClassMax &&
  1368. storage_class != SpvStorageClassInput) {
  1369. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1370. << _.VkErrorID(4240)
  1371. << "Vulkan spec allows BuiltIn HelperInvocation to be only used "
  1372. "for variables with Input storage class. "
  1373. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1374. referenced_from_inst)
  1375. << " " << GetStorageClassDesc(referenced_from_inst);
  1376. }
  1377. for (const SpvExecutionModel execution_model : execution_models_) {
  1378. if (execution_model != SpvExecutionModelFragment) {
  1379. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1380. << _.VkErrorID(4239)
  1381. << "Vulkan spec allows BuiltIn HelperInvocation to be used only "
  1382. "with Fragment execution model. "
  1383. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1384. referenced_from_inst, execution_model);
  1385. }
  1386. }
  1387. }
  1388. if (function_id_ == 0) {
  1389. // Propagate this rule to all dependant ids in the global scope.
  1390. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  1391. std::bind(&BuiltInsValidator::ValidateHelperInvocationAtReference, this,
  1392. decoration, built_in_inst, referenced_from_inst,
  1393. std::placeholders::_1));
  1394. }
  1395. return SPV_SUCCESS;
  1396. }
  1397. spv_result_t BuiltInsValidator::ValidateInvocationIdAtDefinition(
  1398. const Decoration& decoration, const Instruction& inst) {
  1399. if (spvIsVulkanEnv(_.context()->target_env)) {
  1400. if (spv_result_t error = ValidateI32(
  1401. decoration, inst,
  1402. [this, &inst](const std::string& message) -> spv_result_t {
  1403. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1404. << _.VkErrorID(4259)
  1405. << "According to the Vulkan spec BuiltIn InvocationId "
  1406. "variable needs to be a 32-bit int scalar. "
  1407. << message;
  1408. })) {
  1409. return error;
  1410. }
  1411. }
  1412. // Seed at reference checks with this built-in.
  1413. return ValidateInvocationIdAtReference(decoration, inst, inst, inst);
  1414. }
  1415. spv_result_t BuiltInsValidator::ValidateInvocationIdAtReference(
  1416. const Decoration& decoration, const Instruction& built_in_inst,
  1417. const Instruction& referenced_inst,
  1418. const Instruction& referenced_from_inst) {
  1419. if (spvIsVulkanEnv(_.context()->target_env)) {
  1420. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1421. if (storage_class != SpvStorageClassMax &&
  1422. storage_class != SpvStorageClassInput) {
  1423. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1424. << _.VkErrorID(4258)
  1425. << "Vulkan spec allows BuiltIn InvocationId to be only used for "
  1426. "variables with Input storage class. "
  1427. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1428. referenced_from_inst)
  1429. << " " << GetStorageClassDesc(referenced_from_inst);
  1430. }
  1431. for (const SpvExecutionModel execution_model : execution_models_) {
  1432. if (execution_model != SpvExecutionModelTessellationControl &&
  1433. execution_model != SpvExecutionModelGeometry) {
  1434. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1435. << _.VkErrorID(4257)
  1436. << "Vulkan spec allows BuiltIn InvocationId to be used only "
  1437. "with TessellationControl or Geometry execution models. "
  1438. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1439. referenced_from_inst, execution_model);
  1440. }
  1441. }
  1442. }
  1443. if (function_id_ == 0) {
  1444. // Propagate this rule to all dependant ids in the global scope.
  1445. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1446. &BuiltInsValidator::ValidateInvocationIdAtReference, this, decoration,
  1447. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1448. }
  1449. return SPV_SUCCESS;
  1450. }
  1451. spv_result_t BuiltInsValidator::ValidateInstanceIndexAtDefinition(
  1452. const Decoration& decoration, const Instruction& inst) {
  1453. if (spvIsVulkanEnv(_.context()->target_env)) {
  1454. if (spv_result_t error = ValidateI32(
  1455. decoration, inst,
  1456. [this, &inst](const std::string& message) -> spv_result_t {
  1457. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1458. << _.VkErrorID(4265) << "According to the "
  1459. << spvLogStringForEnv(_.context()->target_env)
  1460. << " spec BuiltIn InstanceIndex "
  1461. "variable needs to be a 32-bit int scalar. "
  1462. << message;
  1463. })) {
  1464. return error;
  1465. }
  1466. }
  1467. // Seed at reference checks with this built-in.
  1468. return ValidateInstanceIndexAtReference(decoration, inst, inst, inst);
  1469. }
  1470. spv_result_t BuiltInsValidator::ValidateInstanceIndexAtReference(
  1471. const Decoration& decoration, const Instruction& built_in_inst,
  1472. const Instruction& referenced_inst,
  1473. const Instruction& referenced_from_inst) {
  1474. if (spvIsVulkanEnv(_.context()->target_env)) {
  1475. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1476. if (storage_class != SpvStorageClassMax &&
  1477. storage_class != SpvStorageClassInput) {
  1478. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1479. << _.VkErrorID(4264) << spvLogStringForEnv(_.context()->target_env)
  1480. << " spec allows BuiltIn InstanceIndex to be only used for "
  1481. "variables with Input storage class. "
  1482. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1483. referenced_from_inst)
  1484. << " " << GetStorageClassDesc(referenced_from_inst);
  1485. }
  1486. for (const SpvExecutionModel execution_model : execution_models_) {
  1487. if (execution_model != SpvExecutionModelVertex) {
  1488. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1489. << _.VkErrorID(4263)
  1490. << spvLogStringForEnv(_.context()->target_env)
  1491. << " spec allows BuiltIn InstanceIndex to be used only "
  1492. "with Vertex execution model. "
  1493. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1494. referenced_from_inst, execution_model);
  1495. }
  1496. }
  1497. }
  1498. if (function_id_ == 0) {
  1499. // Propagate this rule to all dependant ids in the global scope.
  1500. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1501. &BuiltInsValidator::ValidateInstanceIndexAtReference, this, decoration,
  1502. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1503. }
  1504. return SPV_SUCCESS;
  1505. }
  1506. spv_result_t BuiltInsValidator::ValidatePatchVerticesAtDefinition(
  1507. const Decoration& decoration, const Instruction& inst) {
  1508. if (spvIsVulkanEnv(_.context()->target_env)) {
  1509. if (spv_result_t error = ValidateI32(
  1510. decoration, inst,
  1511. [this, &inst](const std::string& message) -> spv_result_t {
  1512. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1513. << _.VkErrorID(4310)
  1514. << "According to the Vulkan spec BuiltIn PatchVertices "
  1515. "variable needs to be a 32-bit int scalar. "
  1516. << message;
  1517. })) {
  1518. return error;
  1519. }
  1520. }
  1521. // Seed at reference checks with this built-in.
  1522. return ValidatePatchVerticesAtReference(decoration, inst, inst, inst);
  1523. }
  1524. spv_result_t BuiltInsValidator::ValidatePatchVerticesAtReference(
  1525. const Decoration& decoration, const Instruction& built_in_inst,
  1526. const Instruction& referenced_inst,
  1527. const Instruction& referenced_from_inst) {
  1528. if (spvIsVulkanEnv(_.context()->target_env)) {
  1529. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1530. if (storage_class != SpvStorageClassMax &&
  1531. storage_class != SpvStorageClassInput) {
  1532. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1533. << _.VkErrorID(4309)
  1534. << "Vulkan spec allows BuiltIn PatchVertices to be only used for "
  1535. "variables with Input storage class. "
  1536. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1537. referenced_from_inst)
  1538. << " " << GetStorageClassDesc(referenced_from_inst);
  1539. }
  1540. for (const SpvExecutionModel execution_model : execution_models_) {
  1541. if (execution_model != SpvExecutionModelTessellationControl &&
  1542. execution_model != SpvExecutionModelTessellationEvaluation) {
  1543. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1544. << _.VkErrorID(4308)
  1545. << "Vulkan spec allows BuiltIn PatchVertices to be used only "
  1546. "with TessellationControl or TessellationEvaluation "
  1547. "execution models. "
  1548. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1549. referenced_from_inst, execution_model);
  1550. }
  1551. }
  1552. }
  1553. if (function_id_ == 0) {
  1554. // Propagate this rule to all dependant ids in the global scope.
  1555. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1556. &BuiltInsValidator::ValidatePatchVerticesAtReference, this, decoration,
  1557. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1558. }
  1559. return SPV_SUCCESS;
  1560. }
  1561. spv_result_t BuiltInsValidator::ValidatePointCoordAtDefinition(
  1562. const Decoration& decoration, const Instruction& inst) {
  1563. if (spvIsVulkanEnv(_.context()->target_env)) {
  1564. if (spv_result_t error = ValidateF32Vec(
  1565. decoration, inst, 2,
  1566. [this, &inst](const std::string& message) -> spv_result_t {
  1567. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1568. << _.VkErrorID(4313)
  1569. << "According to the Vulkan spec BuiltIn PointCoord "
  1570. "variable needs to be a 2-component 32-bit float "
  1571. "vector. "
  1572. << message;
  1573. })) {
  1574. return error;
  1575. }
  1576. }
  1577. // Seed at reference checks with this built-in.
  1578. return ValidatePointCoordAtReference(decoration, inst, inst, inst);
  1579. }
  1580. spv_result_t BuiltInsValidator::ValidatePointCoordAtReference(
  1581. const Decoration& decoration, const Instruction& built_in_inst,
  1582. const Instruction& referenced_inst,
  1583. const Instruction& referenced_from_inst) {
  1584. if (spvIsVulkanEnv(_.context()->target_env)) {
  1585. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1586. if (storage_class != SpvStorageClassMax &&
  1587. storage_class != SpvStorageClassInput) {
  1588. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1589. << _.VkErrorID(4312)
  1590. << "Vulkan spec allows BuiltIn PointCoord to be only used for "
  1591. "variables with Input storage class. "
  1592. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1593. referenced_from_inst)
  1594. << " " << GetStorageClassDesc(referenced_from_inst);
  1595. }
  1596. for (const SpvExecutionModel execution_model : execution_models_) {
  1597. if (execution_model != SpvExecutionModelFragment) {
  1598. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1599. << _.VkErrorID(4311)
  1600. << "Vulkan spec allows BuiltIn PointCoord to be used only with "
  1601. "Fragment execution model. "
  1602. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1603. referenced_from_inst, execution_model);
  1604. }
  1605. }
  1606. }
  1607. if (function_id_ == 0) {
  1608. // Propagate this rule to all dependant ids in the global scope.
  1609. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1610. &BuiltInsValidator::ValidatePointCoordAtReference, this, decoration,
  1611. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1612. }
  1613. return SPV_SUCCESS;
  1614. }
  1615. spv_result_t BuiltInsValidator::ValidatePointSizeAtDefinition(
  1616. const Decoration& decoration, const Instruction& inst) {
  1617. // Seed at reference checks with this built-in.
  1618. return ValidatePointSizeAtReference(decoration, inst, inst, inst);
  1619. }
  1620. spv_result_t BuiltInsValidator::ValidatePointSizeAtReference(
  1621. const Decoration& decoration, const Instruction& built_in_inst,
  1622. const Instruction& referenced_inst,
  1623. const Instruction& referenced_from_inst) {
  1624. if (spvIsVulkanEnv(_.context()->target_env)) {
  1625. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1626. if (storage_class != SpvStorageClassMax &&
  1627. storage_class != SpvStorageClassInput &&
  1628. storage_class != SpvStorageClassOutput) {
  1629. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1630. << _.VkErrorID(4316)
  1631. << "Vulkan spec allows BuiltIn PointSize to be only used for "
  1632. "variables with Input or Output storage class. "
  1633. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1634. referenced_from_inst)
  1635. << " " << GetStorageClassDesc(referenced_from_inst);
  1636. }
  1637. if (storage_class == SpvStorageClassInput) {
  1638. assert(function_id_ == 0);
  1639. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1640. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4315,
  1641. "Vulkan spec doesn't allow BuiltIn PointSize to be used for "
  1642. "variables with Input storage class if execution model is "
  1643. "Vertex.",
  1644. SpvExecutionModelVertex, decoration, built_in_inst,
  1645. referenced_from_inst, std::placeholders::_1));
  1646. }
  1647. for (const SpvExecutionModel execution_model : execution_models_) {
  1648. switch (execution_model) {
  1649. case SpvExecutionModelVertex: {
  1650. if (spv_result_t error = ValidateF32(
  1651. decoration, built_in_inst,
  1652. [this, &referenced_from_inst](
  1653. const std::string& message) -> spv_result_t {
  1654. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1655. << _.VkErrorID(4317)
  1656. << "According to the Vulkan spec BuiltIn PointSize "
  1657. "variable needs to be a 32-bit float scalar. "
  1658. << message;
  1659. })) {
  1660. return error;
  1661. }
  1662. break;
  1663. }
  1664. case SpvExecutionModelTessellationControl:
  1665. case SpvExecutionModelTessellationEvaluation:
  1666. case SpvExecutionModelGeometry:
  1667. case SpvExecutionModelMeshNV: {
  1668. // PointSize can be a per-vertex variable for tessellation control,
  1669. // tessellation evaluation and geometry shader stages. In such cases
  1670. // variables will have an array of 32-bit floats.
  1671. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1672. // The array is on the variable, so this must be a 32-bit float.
  1673. if (spv_result_t error = ValidateF32(
  1674. decoration, built_in_inst,
  1675. [this, &referenced_from_inst](
  1676. const std::string& message) -> spv_result_t {
  1677. return _.diag(SPV_ERROR_INVALID_DATA,
  1678. &referenced_from_inst)
  1679. << _.VkErrorID(4317)
  1680. << "According to the Vulkan spec BuiltIn "
  1681. "PointSize variable needs to be a 32-bit "
  1682. "float scalar. "
  1683. << message;
  1684. })) {
  1685. return error;
  1686. }
  1687. } else {
  1688. if (spv_result_t error = ValidateOptionalArrayedF32(
  1689. decoration, built_in_inst,
  1690. [this, &referenced_from_inst](
  1691. const std::string& message) -> spv_result_t {
  1692. return _.diag(SPV_ERROR_INVALID_DATA,
  1693. &referenced_from_inst)
  1694. << _.VkErrorID(4317)
  1695. << "According to the Vulkan spec BuiltIn "
  1696. "PointSize variable needs to be a 32-bit "
  1697. "float scalar. "
  1698. << message;
  1699. })) {
  1700. return error;
  1701. }
  1702. }
  1703. break;
  1704. }
  1705. default: {
  1706. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1707. << _.VkErrorID(4314)
  1708. << "Vulkan spec allows BuiltIn PointSize to be used only with "
  1709. "Vertex, TessellationControl, TessellationEvaluation or "
  1710. "Geometry execution models. "
  1711. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1712. referenced_from_inst, execution_model);
  1713. }
  1714. }
  1715. }
  1716. }
  1717. if (function_id_ == 0) {
  1718. // Propagate this rule to all dependant ids in the global scope.
  1719. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1720. &BuiltInsValidator::ValidatePointSizeAtReference, this, decoration,
  1721. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1722. }
  1723. return SPV_SUCCESS;
  1724. }
  1725. spv_result_t BuiltInsValidator::ValidatePositionAtDefinition(
  1726. const Decoration& decoration, const Instruction& inst) {
  1727. // Seed at reference checks with this built-in.
  1728. return ValidatePositionAtReference(decoration, inst, inst, inst);
  1729. }
  1730. spv_result_t BuiltInsValidator::ValidatePositionAtReference(
  1731. const Decoration& decoration, const Instruction& built_in_inst,
  1732. const Instruction& referenced_inst,
  1733. const Instruction& referenced_from_inst) {
  1734. if (spvIsVulkanEnv(_.context()->target_env)) {
  1735. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1736. if (storage_class != SpvStorageClassMax &&
  1737. storage_class != SpvStorageClassInput &&
  1738. storage_class != SpvStorageClassOutput) {
  1739. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1740. << _.VkErrorID(4320) << "Vulkan spec allows BuiltIn Position to be only used for "
  1741. "variables with Input or Output storage class. "
  1742. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1743. referenced_from_inst)
  1744. << " " << GetStorageClassDesc(referenced_from_inst);
  1745. }
  1746. if (storage_class == SpvStorageClassInput) {
  1747. assert(function_id_ == 0);
  1748. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1749. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4319,
  1750. "Vulkan spec doesn't allow BuiltIn Position to be used "
  1751. "for variables "
  1752. "with Input storage class if execution model is Vertex.",
  1753. SpvExecutionModelVertex, decoration, built_in_inst,
  1754. referenced_from_inst, std::placeholders::_1));
  1755. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1756. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4319,
  1757. "Vulkan spec doesn't allow BuiltIn Position to be used "
  1758. "for variables "
  1759. "with Input storage class if execution model is MeshNV.",
  1760. SpvExecutionModelMeshNV, decoration, built_in_inst,
  1761. referenced_from_inst, std::placeholders::_1));
  1762. }
  1763. for (const SpvExecutionModel execution_model : execution_models_) {
  1764. switch (execution_model) {
  1765. case SpvExecutionModelVertex: {
  1766. if (spv_result_t error = ValidateF32Vec(
  1767. decoration, built_in_inst, 4,
  1768. [this, &referenced_from_inst](
  1769. const std::string& message) -> spv_result_t {
  1770. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1771. << _.VkErrorID(4321)
  1772. << "According to the Vulkan spec BuiltIn Position "
  1773. "variable needs to be a 4-component 32-bit float "
  1774. "vector. "
  1775. << message;
  1776. })) {
  1777. return error;
  1778. }
  1779. break;
  1780. }
  1781. case SpvExecutionModelGeometry:
  1782. case SpvExecutionModelTessellationControl:
  1783. case SpvExecutionModelTessellationEvaluation:
  1784. case SpvExecutionModelMeshNV: {
  1785. // Position can be a per-vertex variable for tessellation control,
  1786. // tessellation evaluation, geometry and mesh shader stages. In such
  1787. // cases variables will have an array of 4-component 32-bit float
  1788. // vectors.
  1789. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1790. // The array is on the variable, so this must be a 4-component
  1791. // 32-bit float vector.
  1792. if (spv_result_t error = ValidateF32Vec(
  1793. decoration, built_in_inst, 4,
  1794. [this, &referenced_from_inst](
  1795. const std::string& message) -> spv_result_t {
  1796. return _.diag(SPV_ERROR_INVALID_DATA,
  1797. &referenced_from_inst)
  1798. << _.VkErrorID(4321)
  1799. << "According to the Vulkan spec BuiltIn Position "
  1800. "variable needs to be a 4-component 32-bit "
  1801. "float vector. "
  1802. << message;
  1803. })) {
  1804. return error;
  1805. }
  1806. } else {
  1807. if (spv_result_t error = ValidateOptionalArrayedF32Vec(
  1808. decoration, built_in_inst, 4,
  1809. [this, &referenced_from_inst](
  1810. const std::string& message) -> spv_result_t {
  1811. return _.diag(SPV_ERROR_INVALID_DATA,
  1812. &referenced_from_inst)
  1813. << _.VkErrorID(4321)
  1814. << "According to the Vulkan spec BuiltIn Position "
  1815. "variable needs to be a 4-component 32-bit "
  1816. "float vector. "
  1817. << message;
  1818. })) {
  1819. return error;
  1820. }
  1821. }
  1822. break;
  1823. }
  1824. default: {
  1825. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1826. << _.VkErrorID(4318)
  1827. << "Vulkan spec allows BuiltIn Position to be used only "
  1828. "with Vertex, TessellationControl, TessellationEvaluation"
  1829. " or Geometry execution models. "
  1830. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1831. referenced_from_inst, execution_model);
  1832. }
  1833. }
  1834. }
  1835. }
  1836. if (function_id_ == 0) {
  1837. // Propagate this rule to all dependant ids in the global scope.
  1838. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1839. &BuiltInsValidator::ValidatePositionAtReference, this, decoration,
  1840. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1841. }
  1842. return SPV_SUCCESS;
  1843. }
  1844. spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtDefinition(
  1845. const Decoration& decoration, const Instruction& inst) {
  1846. if (spvIsVulkanEnv(_.context()->target_env)) {
  1847. // PrimitiveId can be a per-primitive variable for mesh shader stage.
  1848. // In such cases variable will have an array of 32-bit integers.
  1849. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1850. // This must be a 32-bit int scalar.
  1851. if (spv_result_t error = ValidateI32(
  1852. decoration, inst,
  1853. [this, &inst](const std::string& message) -> spv_result_t {
  1854. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1855. << _.VkErrorID(4337)
  1856. << "According to the Vulkan spec BuiltIn PrimitiveId "
  1857. "variable needs to be a 32-bit int scalar. "
  1858. << message;
  1859. })) {
  1860. return error;
  1861. }
  1862. } else {
  1863. if (spv_result_t error = ValidateOptionalArrayedI32(
  1864. decoration, inst,
  1865. [this, &inst](const std::string& message) -> spv_result_t {
  1866. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1867. << _.VkErrorID(4337)
  1868. << "According to the Vulkan spec BuiltIn PrimitiveId "
  1869. "variable needs to be a 32-bit int scalar. "
  1870. << message;
  1871. })) {
  1872. return error;
  1873. }
  1874. }
  1875. }
  1876. // Seed at reference checks with this built-in.
  1877. return ValidatePrimitiveIdAtReference(decoration, inst, inst, inst);
  1878. }
  1879. spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtReference(
  1880. const Decoration& decoration, const Instruction& built_in_inst,
  1881. const Instruction& referenced_inst,
  1882. const Instruction& referenced_from_inst) {
  1883. if (spvIsVulkanEnv(_.context()->target_env)) {
  1884. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1885. if (storage_class != SpvStorageClassMax &&
  1886. storage_class != SpvStorageClassInput &&
  1887. storage_class != SpvStorageClassOutput) {
  1888. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1889. << "Vulkan spec allows BuiltIn PrimitiveId to be only used for "
  1890. "variables with Input or Output storage class. "
  1891. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1892. referenced_from_inst)
  1893. << " " << GetStorageClassDesc(referenced_from_inst);
  1894. }
  1895. if (storage_class == SpvStorageClassOutput) {
  1896. assert(function_id_ == 0);
  1897. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1898. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1899. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1900. "variables with Output storage class if execution model is "
  1901. "TessellationControl.",
  1902. SpvExecutionModelTessellationControl, decoration, built_in_inst,
  1903. referenced_from_inst, std::placeholders::_1));
  1904. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1905. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1906. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1907. "variables with Output storage class if execution model is "
  1908. "TessellationEvaluation.",
  1909. SpvExecutionModelTessellationEvaluation, decoration, built_in_inst,
  1910. referenced_from_inst, std::placeholders::_1));
  1911. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1912. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1913. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1914. "variables with Output storage class if execution model is "
  1915. "Fragment.",
  1916. SpvExecutionModelFragment, decoration, built_in_inst,
  1917. referenced_from_inst, std::placeholders::_1));
  1918. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1919. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1920. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1921. "variables with Output storage class if execution model is "
  1922. "IntersectionKHR.",
  1923. SpvExecutionModelIntersectionKHR, decoration, built_in_inst,
  1924. referenced_from_inst, std::placeholders::_1));
  1925. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1926. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1927. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1928. "variables with Output storage class if execution model is "
  1929. "AnyHitKHR.",
  1930. SpvExecutionModelAnyHitKHR, decoration, built_in_inst,
  1931. referenced_from_inst, std::placeholders::_1));
  1932. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1933. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1934. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1935. "variables with Output storage class if execution model is "
  1936. "ClosestHitKHR.",
  1937. SpvExecutionModelClosestHitKHR, decoration, built_in_inst,
  1938. referenced_from_inst, std::placeholders::_1));
  1939. }
  1940. for (const SpvExecutionModel execution_model : execution_models_) {
  1941. switch (execution_model) {
  1942. case SpvExecutionModelFragment:
  1943. case SpvExecutionModelTessellationControl:
  1944. case SpvExecutionModelTessellationEvaluation:
  1945. case SpvExecutionModelGeometry:
  1946. case SpvExecutionModelMeshNV:
  1947. case SpvExecutionModelIntersectionKHR:
  1948. case SpvExecutionModelAnyHitKHR:
  1949. case SpvExecutionModelClosestHitKHR: {
  1950. // Ok.
  1951. break;
  1952. }
  1953. default: {
  1954. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1955. << _.VkErrorID(4330)
  1956. << "Vulkan spec allows BuiltIn PrimitiveId to be used only "
  1957. "with Fragment, TessellationControl, "
  1958. "TessellationEvaluation, Geometry, MeshNV, "
  1959. "IntersectionKHR, "
  1960. "AnyHitKHR, and ClosestHitKHR execution models. "
  1961. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1962. referenced_from_inst, execution_model);
  1963. }
  1964. }
  1965. }
  1966. }
  1967. if (function_id_ == 0) {
  1968. // Propagate this rule to all dependant ids in the global scope.
  1969. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1970. &BuiltInsValidator::ValidatePrimitiveIdAtReference, this, decoration,
  1971. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1972. }
  1973. return SPV_SUCCESS;
  1974. }
  1975. spv_result_t BuiltInsValidator::ValidateSampleIdAtDefinition(
  1976. const Decoration& decoration, const Instruction& inst) {
  1977. if (spvIsVulkanEnv(_.context()->target_env)) {
  1978. if (spv_result_t error = ValidateI32(
  1979. decoration, inst,
  1980. [this, &inst](const std::string& message) -> spv_result_t {
  1981. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1982. << _.VkErrorID(4356)
  1983. << "According to the Vulkan spec BuiltIn SampleId "
  1984. "variable needs to be a 32-bit int scalar. "
  1985. << message;
  1986. })) {
  1987. return error;
  1988. }
  1989. }
  1990. // Seed at reference checks with this built-in.
  1991. return ValidateSampleIdAtReference(decoration, inst, inst, inst);
  1992. }
  1993. spv_result_t BuiltInsValidator::ValidateSampleIdAtReference(
  1994. const Decoration& decoration, const Instruction& built_in_inst,
  1995. const Instruction& referenced_inst,
  1996. const Instruction& referenced_from_inst) {
  1997. if (spvIsVulkanEnv(_.context()->target_env)) {
  1998. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1999. if (storage_class != SpvStorageClassMax &&
  2000. storage_class != SpvStorageClassInput) {
  2001. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2002. << _.VkErrorID(4355)
  2003. << "Vulkan spec allows BuiltIn SampleId to be only used for "
  2004. "variables with Input storage class. "
  2005. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2006. referenced_from_inst)
  2007. << " " << GetStorageClassDesc(referenced_from_inst);
  2008. }
  2009. for (const SpvExecutionModel execution_model : execution_models_) {
  2010. if (execution_model != SpvExecutionModelFragment) {
  2011. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2012. << _.VkErrorID(4354)
  2013. << "Vulkan spec allows BuiltIn SampleId to be used only with "
  2014. "Fragment execution model. "
  2015. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2016. referenced_from_inst, execution_model);
  2017. }
  2018. }
  2019. }
  2020. if (function_id_ == 0) {
  2021. // Propagate this rule to all dependant ids in the global scope.
  2022. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2023. &BuiltInsValidator::ValidateSampleIdAtReference, this, decoration,
  2024. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2025. }
  2026. return SPV_SUCCESS;
  2027. }
  2028. spv_result_t BuiltInsValidator::ValidateSampleMaskAtDefinition(
  2029. const Decoration& decoration, const Instruction& inst) {
  2030. if (spvIsVulkanEnv(_.context()->target_env)) {
  2031. if (spv_result_t error = ValidateI32Arr(
  2032. decoration, inst,
  2033. [this, &inst](const std::string& message) -> spv_result_t {
  2034. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2035. << _.VkErrorID(4359)
  2036. << "According to the Vulkan spec BuiltIn SampleMask "
  2037. "variable needs to be a 32-bit int array. "
  2038. << message;
  2039. })) {
  2040. return error;
  2041. }
  2042. }
  2043. // Seed at reference checks with this built-in.
  2044. return ValidateSampleMaskAtReference(decoration, inst, inst, inst);
  2045. }
  2046. spv_result_t BuiltInsValidator::ValidateSampleMaskAtReference(
  2047. const Decoration& decoration, const Instruction& built_in_inst,
  2048. const Instruction& referenced_inst,
  2049. const Instruction& referenced_from_inst) {
  2050. if (spvIsVulkanEnv(_.context()->target_env)) {
  2051. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2052. if (storage_class != SpvStorageClassMax &&
  2053. storage_class != SpvStorageClassInput &&
  2054. storage_class != SpvStorageClassOutput) {
  2055. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2056. << _.VkErrorID(4358)
  2057. << "Vulkan spec allows BuiltIn SampleMask to be only used for "
  2058. "variables with Input or Output storage class. "
  2059. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2060. referenced_from_inst)
  2061. << " " << GetStorageClassDesc(referenced_from_inst);
  2062. }
  2063. for (const SpvExecutionModel execution_model : execution_models_) {
  2064. if (execution_model != SpvExecutionModelFragment) {
  2065. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2066. << _.VkErrorID(4357)
  2067. << "Vulkan spec allows BuiltIn SampleMask to be used only "
  2068. "with "
  2069. "Fragment execution model. "
  2070. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2071. referenced_from_inst, execution_model);
  2072. }
  2073. }
  2074. }
  2075. if (function_id_ == 0) {
  2076. // Propagate this rule to all dependant ids in the global scope.
  2077. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2078. &BuiltInsValidator::ValidateSampleMaskAtReference, this, decoration,
  2079. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2080. }
  2081. return SPV_SUCCESS;
  2082. }
  2083. spv_result_t BuiltInsValidator::ValidateSamplePositionAtDefinition(
  2084. const Decoration& decoration, const Instruction& inst) {
  2085. if (spvIsVulkanEnv(_.context()->target_env)) {
  2086. if (spv_result_t error = ValidateF32Vec(
  2087. decoration, inst, 2,
  2088. [this, &inst](const std::string& message) -> spv_result_t {
  2089. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2090. << _.VkErrorID(4362)
  2091. << "According to the Vulkan spec BuiltIn SamplePosition "
  2092. "variable needs to be a 2-component 32-bit float "
  2093. "vector. "
  2094. << message;
  2095. })) {
  2096. return error;
  2097. }
  2098. }
  2099. // Seed at reference checks with this built-in.
  2100. return ValidateSamplePositionAtReference(decoration, inst, inst, inst);
  2101. }
  2102. spv_result_t BuiltInsValidator::ValidateSamplePositionAtReference(
  2103. const Decoration& decoration, const Instruction& built_in_inst,
  2104. const Instruction& referenced_inst,
  2105. const Instruction& referenced_from_inst) {
  2106. if (spvIsVulkanEnv(_.context()->target_env)) {
  2107. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2108. if (storage_class != SpvStorageClassMax &&
  2109. storage_class != SpvStorageClassInput) {
  2110. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2111. << _.VkErrorID(4361)
  2112. << "Vulkan spec allows BuiltIn SamplePosition to be only used "
  2113. "for "
  2114. "variables with Input storage class. "
  2115. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2116. referenced_from_inst)
  2117. << " " << GetStorageClassDesc(referenced_from_inst);
  2118. }
  2119. for (const SpvExecutionModel execution_model : execution_models_) {
  2120. if (execution_model != SpvExecutionModelFragment) {
  2121. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2122. << _.VkErrorID(4360)
  2123. << "Vulkan spec allows BuiltIn SamplePosition to be used only "
  2124. "with "
  2125. "Fragment execution model. "
  2126. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2127. referenced_from_inst, execution_model);
  2128. }
  2129. }
  2130. }
  2131. if (function_id_ == 0) {
  2132. // Propagate this rule to all dependant ids in the global scope.
  2133. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2134. &BuiltInsValidator::ValidateSamplePositionAtReference, this, decoration,
  2135. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2136. }
  2137. return SPV_SUCCESS;
  2138. }
  2139. spv_result_t BuiltInsValidator::ValidateTessCoordAtDefinition(
  2140. const Decoration& decoration, const Instruction& inst) {
  2141. if (spvIsVulkanEnv(_.context()->target_env)) {
  2142. if (spv_result_t error = ValidateF32Vec(
  2143. decoration, inst, 3,
  2144. [this, &inst](const std::string& message) -> spv_result_t {
  2145. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2146. << _.VkErrorID(4389)
  2147. << "According to the Vulkan spec BuiltIn TessCoord "
  2148. "variable needs to be a 3-component 32-bit float "
  2149. "vector. "
  2150. << message;
  2151. })) {
  2152. return error;
  2153. }
  2154. }
  2155. // Seed at reference checks with this built-in.
  2156. return ValidateTessCoordAtReference(decoration, inst, inst, inst);
  2157. }
  2158. spv_result_t BuiltInsValidator::ValidateTessCoordAtReference(
  2159. const Decoration& decoration, const Instruction& built_in_inst,
  2160. const Instruction& referenced_inst,
  2161. const Instruction& referenced_from_inst) {
  2162. if (spvIsVulkanEnv(_.context()->target_env)) {
  2163. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2164. if (storage_class != SpvStorageClassMax &&
  2165. storage_class != SpvStorageClassInput) {
  2166. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2167. << _.VkErrorID(4388)
  2168. << "Vulkan spec allows BuiltIn TessCoord to be only used for "
  2169. "variables with Input storage class. "
  2170. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2171. referenced_from_inst)
  2172. << " " << GetStorageClassDesc(referenced_from_inst);
  2173. }
  2174. for (const SpvExecutionModel execution_model : execution_models_) {
  2175. if (execution_model != SpvExecutionModelTessellationEvaluation) {
  2176. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2177. << _.VkErrorID(4387)
  2178. << "Vulkan spec allows BuiltIn TessCoord to be used only with "
  2179. "TessellationEvaluation execution model. "
  2180. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2181. referenced_from_inst, execution_model);
  2182. }
  2183. }
  2184. }
  2185. if (function_id_ == 0) {
  2186. // Propagate this rule to all dependant ids in the global scope.
  2187. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2188. &BuiltInsValidator::ValidateTessCoordAtReference, this, decoration,
  2189. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2190. }
  2191. return SPV_SUCCESS;
  2192. }
  2193. spv_result_t BuiltInsValidator::ValidateTessLevelOuterAtDefinition(
  2194. const Decoration& decoration, const Instruction& inst) {
  2195. if (spvIsVulkanEnv(_.context()->target_env)) {
  2196. if (spv_result_t error = ValidateF32Arr(
  2197. decoration, inst, 4,
  2198. [this, &inst](const std::string& message) -> spv_result_t {
  2199. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2200. << _.VkErrorID(4393)
  2201. << "According to the Vulkan spec BuiltIn TessLevelOuter "
  2202. "variable needs to be a 4-component 32-bit float "
  2203. "array. "
  2204. << message;
  2205. })) {
  2206. return error;
  2207. }
  2208. }
  2209. // Seed at reference checks with this built-in.
  2210. return ValidateTessLevelAtReference(decoration, inst, inst, inst);
  2211. }
  2212. spv_result_t BuiltInsValidator::ValidateTessLevelInnerAtDefinition(
  2213. const Decoration& decoration, const Instruction& inst) {
  2214. if (spvIsVulkanEnv(_.context()->target_env)) {
  2215. if (spv_result_t error = ValidateF32Arr(
  2216. decoration, inst, 2,
  2217. [this, &inst](const std::string& message) -> spv_result_t {
  2218. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2219. << _.VkErrorID(4397)
  2220. << "According to the Vulkan spec BuiltIn TessLevelOuter "
  2221. "variable needs to be a 2-component 32-bit float "
  2222. "array. "
  2223. << message;
  2224. })) {
  2225. return error;
  2226. }
  2227. }
  2228. // Seed at reference checks with this built-in.
  2229. return ValidateTessLevelAtReference(decoration, inst, inst, inst);
  2230. }
  2231. spv_result_t BuiltInsValidator::ValidateTessLevelAtReference(
  2232. const Decoration& decoration, const Instruction& built_in_inst,
  2233. const Instruction& referenced_inst,
  2234. const Instruction& referenced_from_inst) {
  2235. uint32_t operand = decoration.params()[0];
  2236. if (spvIsVulkanEnv(_.context()->target_env)) {
  2237. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2238. if (storage_class != SpvStorageClassMax &&
  2239. storage_class != SpvStorageClassInput &&
  2240. storage_class != SpvStorageClassOutput) {
  2241. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2242. << "Vulkan spec allows BuiltIn "
  2243. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2244. operand)
  2245. << " to be only used for variables with Input or Output storage "
  2246. "class. "
  2247. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2248. referenced_from_inst)
  2249. << " " << GetStorageClassDesc(referenced_from_inst);
  2250. }
  2251. if (storage_class == SpvStorageClassInput) {
  2252. assert(function_id_ == 0);
  2253. uint32_t vuid = (decoration.params()[0] == SpvBuiltInTessLevelOuter) ? 4391 : 4395;
  2254. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2255. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  2256. "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
  2257. "used "
  2258. "for variables with Input storage class if execution model is "
  2259. "TessellationControl.",
  2260. SpvExecutionModelTessellationControl, decoration, built_in_inst,
  2261. referenced_from_inst, std::placeholders::_1));
  2262. }
  2263. if (storage_class == SpvStorageClassOutput) {
  2264. assert(function_id_ == 0);
  2265. uint32_t vuid = (decoration.params()[0] == SpvBuiltInTessLevelOuter) ? 4392 : 4396;
  2266. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2267. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  2268. "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
  2269. "used "
  2270. "for variables with Output storage class if execution model is "
  2271. "TessellationEvaluation.",
  2272. SpvExecutionModelTessellationEvaluation, decoration, built_in_inst,
  2273. referenced_from_inst, std::placeholders::_1));
  2274. }
  2275. for (const SpvExecutionModel execution_model : execution_models_) {
  2276. switch (execution_model) {
  2277. case SpvExecutionModelTessellationControl:
  2278. case SpvExecutionModelTessellationEvaluation: {
  2279. // Ok.
  2280. break;
  2281. }
  2282. default: {
  2283. uint32_t vuid = (operand == SpvBuiltInTessLevelOuter) ? 4390 : 4394;
  2284. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2285. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2286. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2287. operand)
  2288. << " to be used only with TessellationControl or "
  2289. "TessellationEvaluation execution models. "
  2290. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2291. referenced_from_inst, execution_model);
  2292. }
  2293. }
  2294. }
  2295. }
  2296. if (function_id_ == 0) {
  2297. // Propagate this rule to all dependant ids in the global scope.
  2298. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2299. &BuiltInsValidator::ValidateTessLevelAtReference, this, decoration,
  2300. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2301. }
  2302. return SPV_SUCCESS;
  2303. }
  2304. spv_result_t BuiltInsValidator::ValidateVertexIndexAtDefinition(
  2305. const Decoration& decoration, const Instruction& inst) {
  2306. if (spvIsVulkanEnv(_.context()->target_env)) {
  2307. if (spv_result_t error = ValidateI32(
  2308. decoration, inst,
  2309. [this, &inst](const std::string& message) -> spv_result_t {
  2310. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2311. << _.VkErrorID(4400) << "According to the "
  2312. << spvLogStringForEnv(_.context()->target_env)
  2313. << " spec BuiltIn VertexIndex variable needs to be a "
  2314. "32-bit int scalar. "
  2315. << message;
  2316. })) {
  2317. return error;
  2318. }
  2319. }
  2320. // Seed at reference checks with this built-in.
  2321. return ValidateVertexIndexAtReference(decoration, inst, inst, inst);
  2322. }
  2323. spv_result_t BuiltInsValidator::ValidateVertexIdAtDefinition(
  2324. const Decoration& decoration, const Instruction& inst) {
  2325. (void)decoration;
  2326. if (spvIsVulkanEnv(_.context()->target_env)) {
  2327. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2328. << "Vulkan spec doesn't allow BuiltIn VertexId "
  2329. "to be used.";
  2330. }
  2331. return SPV_SUCCESS;
  2332. }
  2333. spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtDefinition(
  2334. const Decoration& decoration, const Instruction& inst) {
  2335. // Seed at reference checks with this built-in.
  2336. return ValidateLocalInvocationIndexAtReference(decoration, inst, inst, inst);
  2337. }
  2338. spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtReference(
  2339. const Decoration& decoration, const Instruction& built_in_inst,
  2340. const Instruction&,
  2341. const Instruction& referenced_from_inst) {
  2342. if (function_id_ == 0) {
  2343. // Propagate this rule to all dependant ids in the global scope.
  2344. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2345. std::bind(&BuiltInsValidator::ValidateLocalInvocationIndexAtReference,
  2346. this, decoration, built_in_inst, referenced_from_inst,
  2347. std::placeholders::_1));
  2348. }
  2349. return SPV_SUCCESS;
  2350. }
  2351. spv_result_t BuiltInsValidator::ValidateVertexIndexAtReference(
  2352. const Decoration& decoration, const Instruction& built_in_inst,
  2353. const Instruction& referenced_inst,
  2354. const Instruction& referenced_from_inst) {
  2355. if (spvIsVulkanEnv(_.context()->target_env)) {
  2356. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2357. if (storage_class != SpvStorageClassMax &&
  2358. storage_class != SpvStorageClassInput) {
  2359. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2360. << _.VkErrorID(4399) << spvLogStringForEnv(_.context()->target_env)
  2361. << " spec allows BuiltIn VertexIndex to be only used for "
  2362. "variables with Input storage class. "
  2363. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2364. referenced_from_inst)
  2365. << " " << GetStorageClassDesc(referenced_from_inst);
  2366. }
  2367. for (const SpvExecutionModel execution_model : execution_models_) {
  2368. if (execution_model != SpvExecutionModelVertex) {
  2369. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2370. << _.VkErrorID(4398)
  2371. << spvLogStringForEnv(_.context()->target_env)
  2372. << " spec allows BuiltIn VertexIndex to be used only with "
  2373. "Vertex execution model. "
  2374. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2375. referenced_from_inst, execution_model);
  2376. }
  2377. }
  2378. }
  2379. if (function_id_ == 0) {
  2380. // Propagate this rule to all dependant ids in the global scope.
  2381. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2382. &BuiltInsValidator::ValidateVertexIndexAtReference, this, decoration,
  2383. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2384. }
  2385. return SPV_SUCCESS;
  2386. }
  2387. spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtDefinition(
  2388. const Decoration& decoration, const Instruction& inst) {
  2389. if (spvIsVulkanEnv(_.context()->target_env)) {
  2390. // This can be a per-primitive variable for mesh shader stage.
  2391. // In such cases variable will have an array of 32-bit integers.
  2392. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2393. // This must be a 32-bit int scalar.
  2394. if (spv_result_t error = ValidateI32(
  2395. decoration, inst,
  2396. [this, &decoration,
  2397. &inst](const std::string& message) -> spv_result_t {
  2398. uint32_t vuid =
  2399. (decoration.params()[0] == SpvBuiltInLayer) ? 4276 : 4408;
  2400. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2401. << _.VkErrorID(vuid)
  2402. << "According to the Vulkan spec BuiltIn "
  2403. << _.grammar().lookupOperandName(
  2404. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
  2405. << "variable needs to be a 32-bit int scalar. "
  2406. << message;
  2407. })) {
  2408. return error;
  2409. }
  2410. } else {
  2411. if (spv_result_t error = ValidateOptionalArrayedI32(
  2412. decoration, inst,
  2413. [this, &decoration,
  2414. &inst](const std::string& message) -> spv_result_t {
  2415. uint32_t vuid =
  2416. (decoration.params()[0] == SpvBuiltInLayer) ? 4276 : 4408;
  2417. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2418. << _.VkErrorID(vuid)
  2419. << "According to the Vulkan spec BuiltIn "
  2420. << _.grammar().lookupOperandName(
  2421. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
  2422. << "variable needs to be a 32-bit int scalar. "
  2423. << message;
  2424. })) {
  2425. return error;
  2426. }
  2427. }
  2428. }
  2429. // Seed at reference checks with this built-in.
  2430. return ValidateLayerOrViewportIndexAtReference(decoration, inst, inst, inst);
  2431. }
  2432. spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtReference(
  2433. const Decoration& decoration, const Instruction& built_in_inst,
  2434. const Instruction& referenced_inst,
  2435. const Instruction& referenced_from_inst) {
  2436. uint32_t operand = decoration.params()[0];
  2437. if (spvIsVulkanEnv(_.context()->target_env)) {
  2438. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2439. if (storage_class != SpvStorageClassMax &&
  2440. storage_class != SpvStorageClassInput &&
  2441. storage_class != SpvStorageClassOutput) {
  2442. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2443. << "Vulkan spec allows BuiltIn "
  2444. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2445. operand)
  2446. << " to be only used for variables with Input or Output storage "
  2447. "class. "
  2448. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2449. referenced_from_inst)
  2450. << " " << GetStorageClassDesc(referenced_from_inst);
  2451. }
  2452. if (storage_class == SpvStorageClassInput) {
  2453. assert(function_id_ == 0);
  2454. for (const auto em :
  2455. {SpvExecutionModelVertex, SpvExecutionModelTessellationEvaluation,
  2456. SpvExecutionModelGeometry, SpvExecutionModelMeshNV}) {
  2457. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2458. std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel,
  2459. this, ((operand == SpvBuiltInLayer) ? 4274 : 4406),
  2460. "Vulkan spec doesn't allow BuiltIn Layer and "
  2461. "ViewportIndex to be "
  2462. "used for variables with Input storage class if "
  2463. "execution model is Vertex, TessellationEvaluation, "
  2464. "Geometry, or MeshNV.",
  2465. em, decoration, built_in_inst, referenced_from_inst,
  2466. std::placeholders::_1));
  2467. }
  2468. }
  2469. if (storage_class == SpvStorageClassOutput) {
  2470. assert(function_id_ == 0);
  2471. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2472. std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel,
  2473. this, ((operand == SpvBuiltInLayer) ? 4275 : 4407),
  2474. "Vulkan spec doesn't allow BuiltIn Layer and "
  2475. "ViewportIndex to be "
  2476. "used for variables with Output storage class if "
  2477. "execution model is "
  2478. "Fragment.",
  2479. SpvExecutionModelFragment, decoration, built_in_inst,
  2480. referenced_from_inst, std::placeholders::_1));
  2481. }
  2482. for (const SpvExecutionModel execution_model : execution_models_) {
  2483. switch (execution_model) {
  2484. case SpvExecutionModelGeometry:
  2485. case SpvExecutionModelFragment:
  2486. case SpvExecutionModelMeshNV:
  2487. // Ok.
  2488. break;
  2489. case SpvExecutionModelVertex:
  2490. case SpvExecutionModelTessellationEvaluation: {
  2491. if (!_.HasCapability(SpvCapabilityShaderViewportIndexLayerEXT)) {
  2492. if (operand == SpvBuiltInViewportIndex &&
  2493. _.HasCapability(SpvCapabilityShaderViewportIndex))
  2494. break; // Ok
  2495. if (operand == SpvBuiltInLayer &&
  2496. _.HasCapability(SpvCapabilityShaderLayer))
  2497. break; // Ok
  2498. const char* capability = "ShaderViewportIndexLayerEXT";
  2499. if (operand == SpvBuiltInViewportIndex)
  2500. capability = "ShaderViewportIndexLayerEXT or ShaderViewportIndex";
  2501. if (operand == SpvBuiltInLayer)
  2502. capability = "ShaderViewportIndexLayerEXT or ShaderLayer";
  2503. uint32_t vuid = (operand == SpvBuiltInLayer) ? 4273 : 4405;
  2504. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2505. << _.VkErrorID(vuid) << "Using BuiltIn "
  2506. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2507. operand)
  2508. << " in Vertex or Tessellation execution model requires the "
  2509. << capability << " capability.";
  2510. }
  2511. break;
  2512. }
  2513. default: {
  2514. uint32_t vuid = (operand == SpvBuiltInLayer) ? 4272 : 4404;
  2515. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2516. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2517. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2518. operand)
  2519. << " to be used only with Vertex, TessellationEvaluation, "
  2520. "Geometry, or Fragment execution models. "
  2521. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2522. referenced_from_inst, execution_model);
  2523. }
  2524. }
  2525. }
  2526. }
  2527. if (function_id_ == 0) {
  2528. // Propagate this rule to all dependant ids in the global scope.
  2529. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2530. std::bind(&BuiltInsValidator::ValidateLayerOrViewportIndexAtReference,
  2531. this, decoration, built_in_inst, referenced_from_inst,
  2532. std::placeholders::_1));
  2533. }
  2534. return SPV_SUCCESS;
  2535. }
  2536. spv_result_t BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtDefinition(
  2537. const Decoration& decoration, const Instruction& inst) {
  2538. if (spvIsVulkanEnv(_.context()->target_env)) {
  2539. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  2540. if (spv_result_t error = ValidateF32Vec(
  2541. decoration, inst, 3,
  2542. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2543. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2544. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2545. << _.VkErrorID(vuid) << "According to the "
  2546. << spvLogStringForEnv(_.context()->target_env)
  2547. << " spec BuiltIn "
  2548. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2549. builtin)
  2550. << " variable needs to be a 3-component 32-bit float "
  2551. "vector. "
  2552. << message;
  2553. })) {
  2554. return error;
  2555. }
  2556. }
  2557. // Seed at reference checks with this built-in.
  2558. return ValidateFragmentShaderF32Vec3InputAtReference(decoration, inst, inst,
  2559. inst);
  2560. }
  2561. spv_result_t BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtReference(
  2562. const Decoration& decoration, const Instruction& built_in_inst,
  2563. const Instruction& referenced_inst,
  2564. const Instruction& referenced_from_inst) {
  2565. if (spvIsVulkanEnv(_.context()->target_env)) {
  2566. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  2567. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2568. if (storage_class != SpvStorageClassMax &&
  2569. storage_class != SpvStorageClassInput) {
  2570. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2571. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2572. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  2573. << " spec allows BuiltIn "
  2574. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2575. << " to be only used for variables with Input storage class. "
  2576. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2577. referenced_from_inst)
  2578. << " " << GetStorageClassDesc(referenced_from_inst);
  2579. }
  2580. for (const SpvExecutionModel execution_model : execution_models_) {
  2581. if (execution_model != SpvExecutionModelFragment) {
  2582. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  2583. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2584. << _.VkErrorID(vuid)
  2585. << spvLogStringForEnv(_.context()->target_env)
  2586. << " spec allows BuiltIn "
  2587. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2588. << " to be used only with Fragment execution model. "
  2589. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2590. referenced_from_inst, execution_model);
  2591. }
  2592. }
  2593. }
  2594. if (function_id_ == 0) {
  2595. // Propagate this rule to all dependant ids in the global scope.
  2596. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2597. &BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtReference, this,
  2598. decoration, built_in_inst, referenced_from_inst,
  2599. std::placeholders::_1));
  2600. }
  2601. return SPV_SUCCESS;
  2602. }
  2603. spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtDefinition(
  2604. const Decoration& decoration, const Instruction& inst) {
  2605. if (spvIsVulkanEnv(_.context()->target_env)) {
  2606. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  2607. if (spv_result_t error = ValidateI32Vec(
  2608. decoration, inst, 3,
  2609. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2610. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2611. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2612. << _.VkErrorID(vuid) << "According to the "
  2613. << spvLogStringForEnv(_.context()->target_env)
  2614. << " spec BuiltIn "
  2615. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2616. builtin)
  2617. << " variable needs to be a 3-component 32-bit int "
  2618. "vector. "
  2619. << message;
  2620. })) {
  2621. return error;
  2622. }
  2623. }
  2624. // Seed at reference checks with this built-in.
  2625. return ValidateComputeShaderI32Vec3InputAtReference(decoration, inst, inst,
  2626. inst);
  2627. }
  2628. spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference(
  2629. const Decoration& decoration, const Instruction& built_in_inst,
  2630. const Instruction& referenced_inst,
  2631. const Instruction& referenced_from_inst) {
  2632. if (spvIsVulkanEnv(_.context()->target_env)) {
  2633. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  2634. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2635. if (storage_class != SpvStorageClassMax &&
  2636. storage_class != SpvStorageClassInput) {
  2637. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2638. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2639. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  2640. << " spec allows BuiltIn "
  2641. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2642. << " to be only used for variables with Input storage class. "
  2643. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2644. referenced_from_inst)
  2645. << " " << GetStorageClassDesc(referenced_from_inst);
  2646. }
  2647. for (const SpvExecutionModel execution_model : execution_models_) {
  2648. bool has_vulkan_model = execution_model == SpvExecutionModelGLCompute ||
  2649. execution_model == SpvExecutionModelTaskNV ||
  2650. execution_model == SpvExecutionModelMeshNV;
  2651. if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
  2652. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  2653. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2654. << _.VkErrorID(vuid)
  2655. << spvLogStringForEnv(_.context()->target_env)
  2656. << " spec allows BuiltIn "
  2657. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2658. << " to be used only with GLCompute, MeshNV, or TaskNV execution model. "
  2659. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2660. referenced_from_inst, execution_model);
  2661. }
  2662. }
  2663. }
  2664. if (function_id_ == 0) {
  2665. // Propagate this rule to all dependant ids in the global scope.
  2666. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2667. &BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference, this,
  2668. decoration, built_in_inst, referenced_from_inst,
  2669. std::placeholders::_1));
  2670. }
  2671. return SPV_SUCCESS;
  2672. }
  2673. spv_result_t BuiltInsValidator::ValidateComputeI32InputAtDefinition(
  2674. const Decoration& decoration, const Instruction& inst) {
  2675. if (spvIsVulkanEnv(_.context()->target_env)) {
  2676. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  2677. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2678. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2679. << "BuiltIn "
  2680. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2681. << " cannot be used as a member decoration ";
  2682. }
  2683. if (spv_result_t error = ValidateI32(
  2684. decoration, inst,
  2685. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2686. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2687. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2688. << _.VkErrorID(vuid)
  2689. << "According to the "
  2690. << spvLogStringForEnv(_.context()->target_env)
  2691. << " spec BuiltIn "
  2692. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2693. << " variable needs to be a 32-bit int "
  2694. "vector. "
  2695. << message;
  2696. })) {
  2697. return error;
  2698. }
  2699. }
  2700. // Seed at reference checks with this built-in.
  2701. return ValidateComputeI32InputAtReference(decoration, inst, inst, inst);
  2702. }
  2703. spv_result_t BuiltInsValidator::ValidateComputeI32InputAtReference(
  2704. const Decoration& decoration, const Instruction& built_in_inst,
  2705. const Instruction& referenced_inst,
  2706. const Instruction& referenced_from_inst) {
  2707. if (spvIsVulkanEnv(_.context()->target_env)) {
  2708. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  2709. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2710. if (storage_class != SpvStorageClassMax &&
  2711. storage_class != SpvStorageClassInput) {
  2712. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2713. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2714. << _.VkErrorID(vuid)
  2715. << spvLogStringForEnv(_.context()->target_env)
  2716. << " spec allows BuiltIn "
  2717. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2718. << " to be only used for variables with Input storage class. "
  2719. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2720. referenced_from_inst)
  2721. << " " << GetStorageClassDesc(referenced_from_inst);
  2722. }
  2723. for (const SpvExecutionModel execution_model : execution_models_) {
  2724. bool has_vulkan_model = execution_model == SpvExecutionModelGLCompute ||
  2725. execution_model == SpvExecutionModelTaskNV ||
  2726. execution_model == SpvExecutionModelMeshNV;
  2727. if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
  2728. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  2729. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2730. << _.VkErrorID(vuid)
  2731. << spvLogStringForEnv(_.context()->target_env)
  2732. << " spec allows BuiltIn "
  2733. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2734. << " to be used only with GLCompute, MeshNV, or TaskNV execution model. "
  2735. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2736. referenced_from_inst, execution_model);
  2737. }
  2738. }
  2739. }
  2740. if (function_id_ == 0) {
  2741. // Propagate this rule to all dependant ids in the global scope.
  2742. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2743. std::bind(&BuiltInsValidator::ValidateComputeI32InputAtReference, this,
  2744. decoration, built_in_inst, referenced_from_inst,
  2745. std::placeholders::_1));
  2746. }
  2747. return SPV_SUCCESS;
  2748. }
  2749. spv_result_t BuiltInsValidator::ValidateI32InputAtDefinition(
  2750. const Decoration& decoration, const Instruction& inst) {
  2751. if (spvIsVulkanEnv(_.context()->target_env)) {
  2752. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  2753. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2754. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2755. << "BuiltIn "
  2756. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2757. << " cannot be used as a member decoration ";
  2758. }
  2759. if (spv_result_t error = ValidateI32(
  2760. decoration, inst,
  2761. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2762. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2763. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2764. << _.VkErrorID(vuid)
  2765. << "According to the "
  2766. << spvLogStringForEnv(_.context()->target_env)
  2767. << " spec BuiltIn "
  2768. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2769. << " variable needs to be a 32-bit int. " << message;
  2770. })) {
  2771. return error;
  2772. }
  2773. const SpvStorageClass storage_class = GetStorageClass(inst);
  2774. if (storage_class != SpvStorageClassMax &&
  2775. storage_class != SpvStorageClassInput) {
  2776. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2777. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2778. << _.VkErrorID(vuid)
  2779. << spvLogStringForEnv(_.context()->target_env)
  2780. << " spec allows BuiltIn "
  2781. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2782. << " to be only used for variables with Input storage class. "
  2783. << GetReferenceDesc(decoration, inst, inst, inst) << " "
  2784. << GetStorageClassDesc(inst);
  2785. }
  2786. }
  2787. return SPV_SUCCESS;
  2788. }
  2789. spv_result_t BuiltInsValidator::ValidateI32Vec4InputAtDefinition(
  2790. const Decoration& decoration, const Instruction& inst) {
  2791. if (spvIsVulkanEnv(_.context()->target_env)) {
  2792. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  2793. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2794. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2795. << "BuiltIn "
  2796. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2797. << " cannot be used as a member decoration ";
  2798. }
  2799. if (spv_result_t error = ValidateI32Vec(
  2800. decoration, inst, 4,
  2801. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2802. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2803. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2804. << _.VkErrorID(vuid)
  2805. << "According to the "
  2806. << spvLogStringForEnv(_.context()->target_env)
  2807. << " spec BuiltIn "
  2808. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2809. << " variable needs to be a 4-component 32-bit int "
  2810. "vector. "
  2811. << message;
  2812. })) {
  2813. return error;
  2814. }
  2815. const SpvStorageClass storage_class = GetStorageClass(inst);
  2816. if (storage_class != SpvStorageClassMax &&
  2817. storage_class != SpvStorageClassInput) {
  2818. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2819. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2820. << _.VkErrorID(vuid)
  2821. << spvLogStringForEnv(_.context()->target_env)
  2822. << " spec allows BuiltIn "
  2823. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  2824. << " to be only used for variables with Input storage class. "
  2825. << GetReferenceDesc(decoration, inst, inst, inst) << " "
  2826. << GetStorageClassDesc(inst);
  2827. }
  2828. }
  2829. return SPV_SUCCESS;
  2830. }
  2831. spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtDefinition(
  2832. const Decoration& decoration, const Instruction& inst) {
  2833. if (spvIsVulkanEnv(_.context()->target_env)) {
  2834. if (spvIsVulkanEnv(_.context()->target_env) &&
  2835. !spvOpcodeIsConstant(inst.opcode())) {
  2836. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2837. << _.VkErrorID(4426)
  2838. << "Vulkan spec requires BuiltIn WorkgroupSize to be a "
  2839. "constant. "
  2840. << GetIdDesc(inst) << " is not a constant.";
  2841. }
  2842. if (spv_result_t error = ValidateI32Vec(
  2843. decoration, inst, 3,
  2844. [this, &inst](const std::string& message) -> spv_result_t {
  2845. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2846. << _.VkErrorID(4427) << "According to the "
  2847. << spvLogStringForEnv(_.context()->target_env)
  2848. << " spec BuiltIn WorkgroupSize variable needs to be a "
  2849. "3-component 32-bit int vector. "
  2850. << message;
  2851. })) {
  2852. return error;
  2853. }
  2854. }
  2855. // Seed at reference checks with this built-in.
  2856. return ValidateWorkgroupSizeAtReference(decoration, inst, inst, inst);
  2857. }
  2858. spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtReference(
  2859. const Decoration& decoration, const Instruction& built_in_inst,
  2860. const Instruction& referenced_inst,
  2861. const Instruction& referenced_from_inst) {
  2862. if (spvIsVulkanEnv(_.context()->target_env)) {
  2863. for (const SpvExecutionModel execution_model : execution_models_) {
  2864. if (execution_model != SpvExecutionModelGLCompute &&
  2865. execution_model != SpvExecutionModelTaskNV &&
  2866. execution_model != SpvExecutionModelMeshNV) {
  2867. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2868. << _.VkErrorID(4425)
  2869. << spvLogStringForEnv(_.context()->target_env)
  2870. << " spec allows BuiltIn "
  2871. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2872. decoration.params()[0])
  2873. << " to be used only with GLCompute, MeshNV, or TaskNV execution model. "
  2874. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2875. referenced_from_inst, execution_model);
  2876. }
  2877. }
  2878. }
  2879. if (function_id_ == 0) {
  2880. // Propagate this rule to all dependant ids in the global scope.
  2881. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2882. &BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, decoration,
  2883. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2884. }
  2885. return SPV_SUCCESS;
  2886. }
  2887. spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtDefinition(
  2888. const Decoration& decoration, const Instruction& inst) {
  2889. if (spvIsVulkanEnv(_.context()->target_env)) {
  2890. if (spv_result_t error = ValidateI32(
  2891. decoration, inst,
  2892. [this, &inst,
  2893. &decoration](const std::string& message) -> spv_result_t {
  2894. uint32_t vuid = (decoration.params()[0] == SpvBuiltInBaseInstance)
  2895. ? 4183
  2896. : 4186;
  2897. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2898. << _.VkErrorID(vuid)
  2899. << "According to the Vulkan spec BuiltIn "
  2900. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2901. decoration.params()[0])
  2902. << " variable needs to be a 32-bit int scalar. "
  2903. << message;
  2904. })) {
  2905. return error;
  2906. }
  2907. }
  2908. return ValidateBaseInstanceOrVertexAtReference(decoration, inst, inst, inst);
  2909. }
  2910. spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference(
  2911. const Decoration& decoration, const Instruction& built_in_inst,
  2912. const Instruction& referenced_inst,
  2913. const Instruction& referenced_from_inst) {
  2914. uint32_t operand = decoration.params()[0];
  2915. if (spvIsVulkanEnv(_.context()->target_env)) {
  2916. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2917. if (storage_class != SpvStorageClassMax &&
  2918. storage_class != SpvStorageClassInput) {
  2919. uint32_t vuid = (operand == SpvBuiltInBaseInstance) ? 4182 : 4185;
  2920. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2921. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2922. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2923. operand)
  2924. << " to be only used for variables with Input storage class. "
  2925. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2926. referenced_from_inst)
  2927. << " " << GetStorageClassDesc(referenced_from_inst);
  2928. }
  2929. for (const SpvExecutionModel execution_model : execution_models_) {
  2930. if (execution_model != SpvExecutionModelVertex) {
  2931. uint32_t vuid = (operand == SpvBuiltInBaseInstance) ? 4181 : 4184;
  2932. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2933. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2934. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2935. operand)
  2936. << " to be used only with Vertex execution model. "
  2937. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2938. referenced_from_inst, execution_model);
  2939. }
  2940. }
  2941. }
  2942. if (function_id_ == 0) {
  2943. // Propagate this rule to all dependant ids in the global scope.
  2944. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2945. std::bind(&BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference,
  2946. this, decoration, built_in_inst, referenced_from_inst,
  2947. std::placeholders::_1));
  2948. }
  2949. return SPV_SUCCESS;
  2950. }
  2951. spv_result_t BuiltInsValidator::ValidateDrawIndexAtDefinition(
  2952. const Decoration& decoration, const Instruction& inst) {
  2953. if (spvIsVulkanEnv(_.context()->target_env)) {
  2954. if (spv_result_t error = ValidateI32(
  2955. decoration, inst,
  2956. [this, &inst,
  2957. &decoration](const std::string& message) -> spv_result_t {
  2958. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2959. << _.VkErrorID(4209)
  2960. << "According to the Vulkan spec BuiltIn "
  2961. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2962. decoration.params()[0])
  2963. << " variable needs to be a 32-bit int scalar. "
  2964. << message;
  2965. })) {
  2966. return error;
  2967. }
  2968. }
  2969. return ValidateDrawIndexAtReference(decoration, inst, inst, inst);
  2970. }
  2971. spv_result_t BuiltInsValidator::ValidateDrawIndexAtReference(
  2972. const Decoration& decoration, const Instruction& built_in_inst,
  2973. const Instruction& referenced_inst,
  2974. const Instruction& referenced_from_inst) {
  2975. uint32_t operand = decoration.params()[0];
  2976. if (spvIsVulkanEnv(_.context()->target_env)) {
  2977. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2978. if (storage_class != SpvStorageClassMax &&
  2979. storage_class != SpvStorageClassInput) {
  2980. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2981. << _.VkErrorID(4208) << "Vulkan spec allows BuiltIn "
  2982. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2983. operand)
  2984. << " to be only used for variables with Input storage class. "
  2985. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2986. referenced_from_inst)
  2987. << " " << GetStorageClassDesc(referenced_from_inst);
  2988. }
  2989. for (const SpvExecutionModel execution_model : execution_models_) {
  2990. if (execution_model != SpvExecutionModelVertex &&
  2991. execution_model != SpvExecutionModelMeshNV &&
  2992. execution_model != SpvExecutionModelTaskNV) {
  2993. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2994. << _.VkErrorID(4207) << "Vulkan spec allows BuiltIn "
  2995. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2996. operand)
  2997. << " to be used only with Vertex, MeshNV, or TaskNV execution "
  2998. "model. "
  2999. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3000. referenced_from_inst, execution_model);
  3001. }
  3002. }
  3003. }
  3004. if (function_id_ == 0) {
  3005. // Propagate this rule to all dependant ids in the global scope.
  3006. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3007. &BuiltInsValidator::ValidateDrawIndexAtReference, this, decoration,
  3008. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3009. }
  3010. return SPV_SUCCESS;
  3011. }
  3012. spv_result_t BuiltInsValidator::ValidateViewIndexAtDefinition(
  3013. const Decoration& decoration, const Instruction& inst) {
  3014. if (spvIsVulkanEnv(_.context()->target_env)) {
  3015. if (spv_result_t error = ValidateI32(
  3016. decoration, inst,
  3017. [this, &inst,
  3018. &decoration](const std::string& message) -> spv_result_t {
  3019. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3020. << _.VkErrorID(4403)
  3021. << "According to the Vulkan spec BuiltIn "
  3022. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3023. decoration.params()[0])
  3024. << " variable needs to be a 32-bit int scalar. "
  3025. << message;
  3026. })) {
  3027. return error;
  3028. }
  3029. }
  3030. return ValidateViewIndexAtReference(decoration, inst, inst, inst);
  3031. }
  3032. spv_result_t BuiltInsValidator::ValidateViewIndexAtReference(
  3033. const Decoration& decoration, const Instruction& built_in_inst,
  3034. const Instruction& referenced_inst,
  3035. const Instruction& referenced_from_inst) {
  3036. uint32_t operand = decoration.params()[0];
  3037. if (spvIsVulkanEnv(_.context()->target_env)) {
  3038. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3039. if (storage_class != SpvStorageClassMax &&
  3040. storage_class != SpvStorageClassInput) {
  3041. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3042. << _.VkErrorID(4402) << "Vulkan spec allows BuiltIn "
  3043. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3044. operand)
  3045. << " to be only used for variables with Input storage class. "
  3046. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3047. referenced_from_inst)
  3048. << " " << GetStorageClassDesc(referenced_from_inst);
  3049. }
  3050. for (const SpvExecutionModel execution_model : execution_models_) {
  3051. if (execution_model == SpvExecutionModelGLCompute) {
  3052. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3053. << _.VkErrorID(4401) << "Vulkan spec allows BuiltIn "
  3054. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3055. operand)
  3056. << " to be not be used with GLCompute execution model. "
  3057. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3058. referenced_from_inst, execution_model);
  3059. }
  3060. }
  3061. }
  3062. if (function_id_ == 0) {
  3063. // Propagate this rule to all dependant ids in the global scope.
  3064. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3065. &BuiltInsValidator::ValidateViewIndexAtReference, this, decoration,
  3066. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3067. }
  3068. return SPV_SUCCESS;
  3069. }
  3070. spv_result_t BuiltInsValidator::ValidateDeviceIndexAtDefinition(
  3071. const Decoration& decoration, const Instruction& inst) {
  3072. if (spvIsVulkanEnv(_.context()->target_env)) {
  3073. if (spv_result_t error = ValidateI32(
  3074. decoration, inst,
  3075. [this, &inst,
  3076. &decoration](const std::string& message) -> spv_result_t {
  3077. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3078. << _.VkErrorID(4206)
  3079. << "According to the Vulkan spec BuiltIn "
  3080. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3081. decoration.params()[0])
  3082. << " variable needs to be a 32-bit int scalar. "
  3083. << message;
  3084. })) {
  3085. return error;
  3086. }
  3087. }
  3088. return ValidateDeviceIndexAtReference(decoration, inst, inst, inst);
  3089. }
  3090. spv_result_t BuiltInsValidator::ValidateDeviceIndexAtReference(
  3091. const Decoration& decoration, const Instruction& built_in_inst,
  3092. const Instruction& referenced_inst,
  3093. const Instruction& referenced_from_inst) {
  3094. uint32_t operand = decoration.params()[0];
  3095. if (spvIsVulkanEnv(_.context()->target_env)) {
  3096. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3097. if (storage_class != SpvStorageClassMax &&
  3098. storage_class != SpvStorageClassInput) {
  3099. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3100. << _.VkErrorID(4205) << "Vulkan spec allows BuiltIn "
  3101. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3102. operand)
  3103. << " to be only used for variables with Input storage class. "
  3104. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3105. referenced_from_inst)
  3106. << " " << GetStorageClassDesc(referenced_from_inst);
  3107. }
  3108. }
  3109. if (function_id_ == 0) {
  3110. // Propagate this rule to all dependant ids in the global scope.
  3111. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3112. &BuiltInsValidator::ValidateDeviceIndexAtReference, this, decoration,
  3113. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3114. }
  3115. return SPV_SUCCESS;
  3116. }
  3117. spv_result_t BuiltInsValidator::ValidateFragInvocationCountAtDefinition(const Decoration& decoration,
  3118. const Instruction& inst) {
  3119. if (spvIsVulkanEnv(_.context()->target_env)) {
  3120. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3121. if (spv_result_t error = ValidateI32(
  3122. decoration, inst,
  3123. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3124. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3125. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3126. << _.VkErrorID(vuid) << "According to the "
  3127. << spvLogStringForEnv(_.context()->target_env)
  3128. << " spec BuiltIn "
  3129. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3130. builtin)
  3131. << " variable needs to be a 32-bit int scalar. "
  3132. << message;
  3133. })) {
  3134. return error;
  3135. }
  3136. }
  3137. return ValidateFragInvocationCountAtReference(decoration, inst, inst, inst);
  3138. }
  3139. spv_result_t BuiltInsValidator::ValidateFragInvocationCountAtReference(
  3140. const Decoration& decoration, const Instruction& built_in_inst,
  3141. const Instruction& referenced_inst,
  3142. const Instruction& referenced_from_inst) {
  3143. if (spvIsVulkanEnv(_.context()->target_env)) {
  3144. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3145. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3146. if (storage_class != SpvStorageClassMax &&
  3147. storage_class != SpvStorageClassInput) {
  3148. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3149. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3150. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3151. << " spec allows BuiltIn "
  3152. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3153. << " to be only used for variables with Input storage class. "
  3154. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3155. referenced_from_inst)
  3156. << " " << GetStorageClassDesc(referenced_from_inst);
  3157. }
  3158. for (const SpvExecutionModel execution_model : execution_models_) {
  3159. if (execution_model != SpvExecutionModelFragment) {
  3160. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3161. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3162. << _.VkErrorID(vuid)
  3163. << spvLogStringForEnv(_.context()->target_env)
  3164. << " spec allows BuiltIn "
  3165. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3166. << " to be used only with Fragment execution model. "
  3167. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3168. referenced_from_inst, execution_model);
  3169. }
  3170. }
  3171. }
  3172. if (function_id_ == 0) {
  3173. // Propagate this rule to all dependant ids in the global scope.
  3174. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3175. &BuiltInsValidator::ValidateFragInvocationCountAtReference, this, decoration,
  3176. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3177. }
  3178. return SPV_SUCCESS;
  3179. }
  3180. spv_result_t BuiltInsValidator::ValidateFragSizeAtDefinition(const Decoration& decoration,
  3181. const Instruction& inst) {
  3182. if (spvIsVulkanEnv(_.context()->target_env)) {
  3183. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3184. if (spv_result_t error = ValidateI32Vec(
  3185. decoration, inst, 2,
  3186. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3187. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3188. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3189. << _.VkErrorID(vuid) << "According to the "
  3190. << spvLogStringForEnv(_.context()->target_env)
  3191. << " spec BuiltIn "
  3192. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3193. builtin)
  3194. << " variable needs to be a 2-component 32-bit int vector. "
  3195. << message;
  3196. })) {
  3197. return error;
  3198. }
  3199. }
  3200. return ValidateFragSizeAtReference(decoration, inst, inst, inst);
  3201. }
  3202. spv_result_t BuiltInsValidator::ValidateFragSizeAtReference(
  3203. const Decoration& decoration, const Instruction& built_in_inst,
  3204. const Instruction& referenced_inst,
  3205. const Instruction& referenced_from_inst) {
  3206. if (spvIsVulkanEnv(_.context()->target_env)) {
  3207. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3208. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3209. if (storage_class != SpvStorageClassMax &&
  3210. storage_class != SpvStorageClassInput) {
  3211. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3212. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3213. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3214. << " spec allows BuiltIn "
  3215. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3216. << " to be only used for variables with Input storage class. "
  3217. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3218. referenced_from_inst)
  3219. << " " << GetStorageClassDesc(referenced_from_inst);
  3220. }
  3221. for (const SpvExecutionModel execution_model : execution_models_) {
  3222. if (execution_model != SpvExecutionModelFragment) {
  3223. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3224. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3225. << _.VkErrorID(vuid)
  3226. << spvLogStringForEnv(_.context()->target_env)
  3227. << " spec allows BuiltIn "
  3228. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3229. << " to be used only with Fragment execution model. "
  3230. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3231. referenced_from_inst, execution_model);
  3232. }
  3233. }
  3234. }
  3235. if (function_id_ == 0) {
  3236. // Propagate this rule to all dependant ids in the global scope.
  3237. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3238. &BuiltInsValidator::ValidateFragSizeAtReference, this, decoration,
  3239. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3240. }
  3241. return SPV_SUCCESS;
  3242. }
  3243. spv_result_t BuiltInsValidator::ValidateFragStencilRefAtDefinition(const Decoration& decoration,
  3244. const Instruction& inst) {
  3245. if (spvIsVulkanEnv(_.context()->target_env)) {
  3246. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3247. if (spv_result_t error = ValidateI(
  3248. decoration, inst,
  3249. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3250. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3251. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3252. << _.VkErrorID(vuid) << "According to the "
  3253. << spvLogStringForEnv(_.context()->target_env)
  3254. << " spec BuiltIn "
  3255. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3256. builtin)
  3257. << " variable needs to be a int scalar. "
  3258. << message;
  3259. })) {
  3260. return error;
  3261. }
  3262. }
  3263. return ValidateFragStencilRefAtReference(decoration, inst, inst, inst);
  3264. }
  3265. spv_result_t BuiltInsValidator::ValidateFragStencilRefAtReference(
  3266. const Decoration& decoration, const Instruction& built_in_inst,
  3267. const Instruction& referenced_inst,
  3268. const Instruction& referenced_from_inst) {
  3269. if (spvIsVulkanEnv(_.context()->target_env)) {
  3270. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3271. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3272. if (storage_class != SpvStorageClassMax &&
  3273. storage_class != SpvStorageClassOutput) {
  3274. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3275. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3276. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3277. << " spec allows BuiltIn "
  3278. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3279. << " to be only used for variables with Output storage class. "
  3280. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3281. referenced_from_inst)
  3282. << " " << GetStorageClassDesc(referenced_from_inst);
  3283. }
  3284. for (const SpvExecutionModel execution_model : execution_models_) {
  3285. if (execution_model != SpvExecutionModelFragment) {
  3286. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3287. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3288. << _.VkErrorID(vuid)
  3289. << spvLogStringForEnv(_.context()->target_env)
  3290. << " spec allows BuiltIn "
  3291. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3292. << " to be used only with Fragment execution model. "
  3293. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3294. referenced_from_inst, execution_model);
  3295. }
  3296. }
  3297. }
  3298. if (function_id_ == 0) {
  3299. // Propagate this rule to all dependant ids in the global scope.
  3300. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3301. &BuiltInsValidator::ValidateFragStencilRefAtReference, this, decoration,
  3302. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3303. }
  3304. return SPV_SUCCESS;
  3305. }
  3306. spv_result_t BuiltInsValidator::ValidateFullyCoveredAtDefinition(const Decoration& decoration,
  3307. const Instruction& inst) {
  3308. if (spvIsVulkanEnv(_.context()->target_env)) {
  3309. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3310. if (spv_result_t error = ValidateBool(
  3311. decoration, inst,
  3312. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3313. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3314. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3315. << _.VkErrorID(vuid) << "According to the "
  3316. << spvLogStringForEnv(_.context()->target_env)
  3317. << " spec BuiltIn "
  3318. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3319. builtin)
  3320. << " variable needs to be a bool scalar. "
  3321. << message;
  3322. })) {
  3323. return error;
  3324. }
  3325. }
  3326. return ValidateFullyCoveredAtReference(decoration, inst, inst, inst);
  3327. }
  3328. spv_result_t BuiltInsValidator::ValidateFullyCoveredAtReference(
  3329. const Decoration& decoration, const Instruction& built_in_inst,
  3330. const Instruction& referenced_inst,
  3331. const Instruction& referenced_from_inst) {
  3332. if (spvIsVulkanEnv(_.context()->target_env)) {
  3333. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3334. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3335. if (storage_class != SpvStorageClassMax &&
  3336. storage_class != SpvStorageClassInput) {
  3337. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3338. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3339. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3340. << " spec allows BuiltIn "
  3341. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3342. << " to be only used for variables with Input storage class. "
  3343. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3344. referenced_from_inst)
  3345. << " " << GetStorageClassDesc(referenced_from_inst);
  3346. }
  3347. for (const SpvExecutionModel execution_model : execution_models_) {
  3348. if (execution_model != SpvExecutionModelFragment) {
  3349. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3350. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3351. << _.VkErrorID(vuid)
  3352. << spvLogStringForEnv(_.context()->target_env)
  3353. << " spec allows BuiltIn "
  3354. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3355. << " to be used only with Fragment execution model. "
  3356. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3357. referenced_from_inst, execution_model);
  3358. }
  3359. }
  3360. }
  3361. if (function_id_ == 0) {
  3362. // Propagate this rule to all dependant ids in the global scope.
  3363. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3364. &BuiltInsValidator::ValidateFullyCoveredAtReference, this, decoration,
  3365. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3366. }
  3367. return SPV_SUCCESS;
  3368. }
  3369. spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtDefinition(
  3370. const Decoration& decoration, const Instruction& inst) {
  3371. if (spvIsVulkanEnv(_.context()->target_env)) {
  3372. if (spv_result_t error = ValidateI32(
  3373. decoration, inst,
  3374. [this, &inst,
  3375. &decoration](const std::string& message) -> spv_result_t {
  3376. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3377. << "According to the "
  3378. << spvLogStringForEnv(_.context()->target_env)
  3379. << " spec BuiltIn "
  3380. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3381. decoration.params()[0])
  3382. << " variable needs to be a 32-bit int scalar. "
  3383. << message;
  3384. })) {
  3385. return error;
  3386. }
  3387. }
  3388. // Seed at reference checks with this built-in.
  3389. return ValidateSMBuiltinsAtReference(decoration, inst, inst, inst);
  3390. }
  3391. spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtReference(
  3392. const Decoration& decoration, const Instruction& built_in_inst,
  3393. const Instruction& referenced_inst,
  3394. const Instruction& referenced_from_inst) {
  3395. if (spvIsVulkanEnv(_.context()->target_env)) {
  3396. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3397. if (storage_class != SpvStorageClassMax &&
  3398. storage_class != SpvStorageClassInput) {
  3399. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3400. << spvLogStringForEnv(_.context()->target_env)
  3401. << " spec allows BuiltIn "
  3402. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3403. decoration.params()[0])
  3404. << " to be only used for "
  3405. "variables with Input storage class. "
  3406. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3407. referenced_from_inst)
  3408. << " " << GetStorageClassDesc(referenced_from_inst);
  3409. }
  3410. }
  3411. if (function_id_ == 0) {
  3412. // Propagate this rule to all dependant ids in the global scope.
  3413. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3414. &BuiltInsValidator::ValidateSMBuiltinsAtReference, this, decoration,
  3415. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3416. }
  3417. return SPV_SUCCESS;
  3418. }
  3419. spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtDefinition(
  3420. const Decoration& decoration, const Instruction& inst) {
  3421. if (spvIsVulkanEnv(_.context()->target_env)) {
  3422. if (spv_result_t error = ValidateI32(
  3423. decoration, inst,
  3424. [this, &inst,
  3425. &decoration](const std::string& message) -> spv_result_t {
  3426. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3427. << _.VkErrorID(4486)
  3428. << "According to the Vulkan spec BuiltIn "
  3429. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3430. decoration.params()[0])
  3431. << " variable needs to be a 32-bit int scalar. "
  3432. << message;
  3433. })) {
  3434. return error;
  3435. }
  3436. }
  3437. // Seed at reference checks with this built-in.
  3438. return ValidatePrimitiveShadingRateAtReference(decoration, inst, inst, inst);
  3439. }
  3440. spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtReference(
  3441. const Decoration& decoration, const Instruction& built_in_inst,
  3442. const Instruction& referenced_inst,
  3443. const Instruction& referenced_from_inst) {
  3444. if (spvIsVulkanEnv(_.context()->target_env)) {
  3445. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3446. if (storage_class != SpvStorageClassMax &&
  3447. storage_class != SpvStorageClassOutput) {
  3448. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3449. << _.VkErrorID(4485) << "Vulkan spec allows BuiltIn "
  3450. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3451. decoration.params()[0])
  3452. << " to be only used for variables with Output storage class. "
  3453. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3454. referenced_from_inst)
  3455. << " " << GetStorageClassDesc(referenced_from_inst);
  3456. }
  3457. for (const SpvExecutionModel execution_model : execution_models_) {
  3458. switch (execution_model) {
  3459. case SpvExecutionModelVertex:
  3460. case SpvExecutionModelGeometry:
  3461. case SpvExecutionModelMeshNV:
  3462. break;
  3463. default: {
  3464. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3465. << _.VkErrorID(4484) << "Vulkan spec allows BuiltIn "
  3466. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3467. decoration.params()[0])
  3468. << " to be used only with Vertex, Geometry, or MeshNV "
  3469. "execution models. "
  3470. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3471. referenced_from_inst, execution_model);
  3472. }
  3473. }
  3474. }
  3475. }
  3476. if (function_id_ == 0) {
  3477. // Propagate this rule to all dependant ids in the global scope.
  3478. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  3479. std::bind(&BuiltInsValidator::ValidatePrimitiveShadingRateAtReference,
  3480. this, decoration, built_in_inst, referenced_from_inst,
  3481. std::placeholders::_1));
  3482. }
  3483. return SPV_SUCCESS;
  3484. }
  3485. spv_result_t BuiltInsValidator::ValidateShadingRateAtDefinition(
  3486. const Decoration& decoration, const Instruction& inst) {
  3487. if (spvIsVulkanEnv(_.context()->target_env)) {
  3488. if (spv_result_t error = ValidateI32(
  3489. decoration, inst,
  3490. [this, &inst,
  3491. &decoration](const std::string& message) -> spv_result_t {
  3492. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3493. << _.VkErrorID(4492)
  3494. << "According to the Vulkan spec BuiltIn "
  3495. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3496. decoration.params()[0])
  3497. << " variable needs to be a 32-bit int scalar. "
  3498. << message;
  3499. })) {
  3500. return error;
  3501. }
  3502. }
  3503. // Seed at reference checks with this built-in.
  3504. return ValidateShadingRateAtReference(decoration, inst, inst, inst);
  3505. }
  3506. spv_result_t BuiltInsValidator::ValidateShadingRateAtReference(
  3507. const Decoration& decoration, const Instruction& built_in_inst,
  3508. const Instruction& referenced_inst,
  3509. const Instruction& referenced_from_inst) {
  3510. if (spvIsVulkanEnv(_.context()->target_env)) {
  3511. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3512. if (storage_class != SpvStorageClassMax &&
  3513. storage_class != SpvStorageClassInput) {
  3514. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3515. << _.VkErrorID(4491) << "Vulkan spec allows BuiltIn "
  3516. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3517. decoration.params()[0])
  3518. << " to be only used for variables with Input storage class. "
  3519. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3520. referenced_from_inst)
  3521. << " " << GetStorageClassDesc(referenced_from_inst);
  3522. }
  3523. for (const SpvExecutionModel execution_model : execution_models_) {
  3524. if (execution_model != SpvExecutionModelFragment) {
  3525. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3526. << _.VkErrorID(4490) << "Vulkan spec allows BuiltIn "
  3527. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3528. decoration.params()[0])
  3529. << " to be used only with the Fragment execution model. "
  3530. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3531. referenced_from_inst, execution_model);
  3532. }
  3533. }
  3534. }
  3535. if (function_id_ == 0) {
  3536. // Propagate this rule to all dependant ids in the global scope.
  3537. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3538. &BuiltInsValidator::ValidateShadingRateAtReference, this, decoration,
  3539. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3540. }
  3541. return SPV_SUCCESS;
  3542. }
  3543. spv_result_t BuiltInsValidator::ValidateRayTracingBuiltinsAtDefinition(
  3544. const Decoration& decoration, const Instruction& inst) {
  3545. if (spvIsVulkanEnv(_.context()->target_env)) {
  3546. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3547. switch (builtin) {
  3548. case SpvBuiltInHitTNV:
  3549. case SpvBuiltInRayTminKHR:
  3550. case SpvBuiltInRayTmaxKHR:
  3551. // f32 scalar
  3552. if (spv_result_t error = ValidateF32(
  3553. decoration, inst,
  3554. [this, &inst,
  3555. builtin](const std::string& message) -> spv_result_t {
  3556. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3557. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3558. << _.VkErrorID(vuid)
  3559. << "According to the Vulkan spec BuiltIn "
  3560. << _.grammar().lookupOperandName(
  3561. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3562. << " variable needs to be a 32-bit float scalar. "
  3563. << message;
  3564. })) {
  3565. return error;
  3566. }
  3567. break;
  3568. case SpvBuiltInHitKindKHR:
  3569. case SpvBuiltInInstanceCustomIndexKHR:
  3570. case SpvBuiltInInstanceId:
  3571. case SpvBuiltInRayGeometryIndexKHR:
  3572. case SpvBuiltInIncomingRayFlagsKHR:
  3573. case SpvBuiltInCullMaskKHR:
  3574. // i32 scalar
  3575. if (spv_result_t error = ValidateI32(
  3576. decoration, inst,
  3577. [this, &inst,
  3578. builtin](const std::string& message) -> spv_result_t {
  3579. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3580. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3581. << _.VkErrorID(vuid)
  3582. << "According to the Vulkan spec BuiltIn "
  3583. << _.grammar().lookupOperandName(
  3584. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3585. << " variable needs to be a 32-bit int scalar. "
  3586. << message;
  3587. })) {
  3588. return error;
  3589. }
  3590. break;
  3591. case SpvBuiltInObjectRayDirectionKHR:
  3592. case SpvBuiltInObjectRayOriginKHR:
  3593. case SpvBuiltInWorldRayDirectionKHR:
  3594. case SpvBuiltInWorldRayOriginKHR:
  3595. // f32 vec3
  3596. if (spv_result_t error = ValidateF32Vec(
  3597. decoration, inst, 3,
  3598. [this, &inst,
  3599. builtin](const std::string& message) -> spv_result_t {
  3600. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3601. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3602. << _.VkErrorID(vuid)
  3603. << "According to the Vulkan spec BuiltIn "
  3604. << _.grammar().lookupOperandName(
  3605. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3606. << " variable needs to be a 3-component 32-bit float "
  3607. "vector. "
  3608. << message;
  3609. })) {
  3610. return error;
  3611. }
  3612. break;
  3613. case SpvBuiltInLaunchIdKHR:
  3614. case SpvBuiltInLaunchSizeKHR:
  3615. // i32 vec3
  3616. if (spv_result_t error = ValidateI32Vec(
  3617. decoration, inst, 3,
  3618. [this, &inst,
  3619. builtin](const std::string& message) -> spv_result_t {
  3620. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3621. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3622. << _.VkErrorID(vuid)
  3623. << "According to the Vulkan spec BuiltIn "
  3624. << _.grammar().lookupOperandName(
  3625. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3626. << " variable needs to be a 3-component 32-bit int "
  3627. "vector. "
  3628. << message;
  3629. })) {
  3630. return error;
  3631. }
  3632. break;
  3633. case SpvBuiltInObjectToWorldKHR:
  3634. case SpvBuiltInWorldToObjectKHR:
  3635. // f32 mat4x3
  3636. if (spv_result_t error = ValidateF32Mat(
  3637. decoration, inst, 3, 4,
  3638. [this, &inst,
  3639. builtin](const std::string& message) -> spv_result_t {
  3640. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3641. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3642. << _.VkErrorID(vuid)
  3643. << "According to the Vulkan spec BuiltIn "
  3644. << _.grammar().lookupOperandName(
  3645. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3646. << " variable needs to be a matrix with"
  3647. << " 4 columns of 3-component vectors of 32-bit "
  3648. "floats. "
  3649. << message;
  3650. })) {
  3651. return error;
  3652. }
  3653. break;
  3654. default:
  3655. assert(0 && "Unexpected ray tracing builtin");
  3656. break;
  3657. }
  3658. }
  3659. // Seed at reference checks with this built-in.
  3660. return ValidateRayTracingBuiltinsAtReference(decoration, inst, inst, inst);
  3661. }
  3662. spv_result_t BuiltInsValidator::ValidateRayTracingBuiltinsAtReference(
  3663. const Decoration& decoration, const Instruction& built_in_inst,
  3664. const Instruction& referenced_inst,
  3665. const Instruction& referenced_from_inst) {
  3666. if (spvIsVulkanEnv(_.context()->target_env)) {
  3667. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3668. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3669. if (storage_class != SpvStorageClassMax &&
  3670. storage_class != SpvStorageClassInput) {
  3671. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3672. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3673. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  3674. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3675. decoration.params()[0])
  3676. << " to be only used for variables with Input storage class. "
  3677. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3678. referenced_from_inst)
  3679. << " " << GetStorageClassDesc(referenced_from_inst);
  3680. }
  3681. for (const SpvExecutionModel execution_model : execution_models_) {
  3682. if (!IsExecutionModelValidForRtBuiltIn(builtin, execution_model)) {
  3683. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3684. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3685. << _.VkErrorID(vuid) << "Vulkan spec does not allow BuiltIn "
  3686. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3687. decoration.params()[0])
  3688. << " to be used with the execution model "
  3689. << _.grammar().lookupOperandName(
  3690. SPV_OPERAND_TYPE_EXECUTION_MODEL, execution_model)
  3691. << ".\n"
  3692. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3693. referenced_from_inst, execution_model);
  3694. }
  3695. }
  3696. }
  3697. if (function_id_ == 0) {
  3698. // Propagate this rule to all dependant ids in the global scope.
  3699. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  3700. std::bind(&BuiltInsValidator::ValidateRayTracingBuiltinsAtReference,
  3701. this, decoration, built_in_inst, referenced_from_inst,
  3702. std::placeholders::_1));
  3703. }
  3704. return SPV_SUCCESS;
  3705. }
  3706. spv_result_t BuiltInsValidator::ValidateSingleBuiltInAtDefinition(
  3707. const Decoration& decoration, const Instruction& inst) {
  3708. const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]);
  3709. if (!spvIsVulkanEnv(_.context()->target_env)) {
  3710. // Early return. All currently implemented rules are based on Vulkan spec.
  3711. //
  3712. // TODO: If you are adding validation rules for environments other than
  3713. // Vulkan (or general rules which are not environment independent), then
  3714. // you need to modify or remove this condition. Consider also adding early
  3715. // returns into BuiltIn-specific rules, so that the system doesn't spawn new
  3716. // rules which don't do anything.
  3717. return SPV_SUCCESS;
  3718. }
  3719. // If you are adding a new BuiltIn enum, please register it here.
  3720. // If the newly added enum has validation rules associated with it
  3721. // consider leaving a TODO and/or creating an issue.
  3722. switch (label) {
  3723. case SpvBuiltInClipDistance:
  3724. case SpvBuiltInCullDistance: {
  3725. return ValidateClipOrCullDistanceAtDefinition(decoration, inst);
  3726. }
  3727. case SpvBuiltInFragCoord: {
  3728. return ValidateFragCoordAtDefinition(decoration, inst);
  3729. }
  3730. case SpvBuiltInFragDepth: {
  3731. return ValidateFragDepthAtDefinition(decoration, inst);
  3732. }
  3733. case SpvBuiltInFrontFacing: {
  3734. return ValidateFrontFacingAtDefinition(decoration, inst);
  3735. }
  3736. case SpvBuiltInGlobalInvocationId:
  3737. case SpvBuiltInLocalInvocationId:
  3738. case SpvBuiltInNumWorkgroups:
  3739. case SpvBuiltInWorkgroupId: {
  3740. return ValidateComputeShaderI32Vec3InputAtDefinition(decoration, inst);
  3741. }
  3742. case SpvBuiltInBaryCoordKHR:
  3743. case SpvBuiltInBaryCoordNoPerspKHR: {
  3744. return ValidateFragmentShaderF32Vec3InputAtDefinition(decoration, inst);
  3745. }
  3746. case SpvBuiltInHelperInvocation: {
  3747. return ValidateHelperInvocationAtDefinition(decoration, inst);
  3748. }
  3749. case SpvBuiltInInvocationId: {
  3750. return ValidateInvocationIdAtDefinition(decoration, inst);
  3751. }
  3752. case SpvBuiltInInstanceIndex: {
  3753. return ValidateInstanceIndexAtDefinition(decoration, inst);
  3754. }
  3755. case SpvBuiltInLayer:
  3756. case SpvBuiltInViewportIndex: {
  3757. return ValidateLayerOrViewportIndexAtDefinition(decoration, inst);
  3758. }
  3759. case SpvBuiltInPatchVertices: {
  3760. return ValidatePatchVerticesAtDefinition(decoration, inst);
  3761. }
  3762. case SpvBuiltInPointCoord: {
  3763. return ValidatePointCoordAtDefinition(decoration, inst);
  3764. }
  3765. case SpvBuiltInPointSize: {
  3766. return ValidatePointSizeAtDefinition(decoration, inst);
  3767. }
  3768. case SpvBuiltInPosition: {
  3769. return ValidatePositionAtDefinition(decoration, inst);
  3770. }
  3771. case SpvBuiltInPrimitiveId: {
  3772. return ValidatePrimitiveIdAtDefinition(decoration, inst);
  3773. }
  3774. case SpvBuiltInSampleId: {
  3775. return ValidateSampleIdAtDefinition(decoration, inst);
  3776. }
  3777. case SpvBuiltInSampleMask: {
  3778. return ValidateSampleMaskAtDefinition(decoration, inst);
  3779. }
  3780. case SpvBuiltInSamplePosition: {
  3781. return ValidateSamplePositionAtDefinition(decoration, inst);
  3782. }
  3783. case SpvBuiltInSubgroupId:
  3784. case SpvBuiltInNumSubgroups: {
  3785. return ValidateComputeI32InputAtDefinition(decoration, inst);
  3786. }
  3787. case SpvBuiltInSubgroupLocalInvocationId:
  3788. case SpvBuiltInSubgroupSize: {
  3789. return ValidateI32InputAtDefinition(decoration, inst);
  3790. }
  3791. case SpvBuiltInSubgroupEqMask:
  3792. case SpvBuiltInSubgroupGeMask:
  3793. case SpvBuiltInSubgroupGtMask:
  3794. case SpvBuiltInSubgroupLeMask:
  3795. case SpvBuiltInSubgroupLtMask: {
  3796. return ValidateI32Vec4InputAtDefinition(decoration, inst);
  3797. }
  3798. case SpvBuiltInTessCoord: {
  3799. return ValidateTessCoordAtDefinition(decoration, inst);
  3800. }
  3801. case SpvBuiltInTessLevelOuter: {
  3802. return ValidateTessLevelOuterAtDefinition(decoration, inst);
  3803. }
  3804. case SpvBuiltInTessLevelInner: {
  3805. return ValidateTessLevelInnerAtDefinition(decoration, inst);
  3806. }
  3807. case SpvBuiltInVertexIndex: {
  3808. return ValidateVertexIndexAtDefinition(decoration, inst);
  3809. }
  3810. case SpvBuiltInWorkgroupSize: {
  3811. return ValidateWorkgroupSizeAtDefinition(decoration, inst);
  3812. }
  3813. case SpvBuiltInVertexId: {
  3814. return ValidateVertexIdAtDefinition(decoration, inst);
  3815. }
  3816. case SpvBuiltInLocalInvocationIndex: {
  3817. return ValidateLocalInvocationIndexAtDefinition(decoration, inst);
  3818. }
  3819. case SpvBuiltInWarpsPerSMNV:
  3820. case SpvBuiltInSMCountNV:
  3821. case SpvBuiltInWarpIDNV:
  3822. case SpvBuiltInSMIDNV: {
  3823. return ValidateSMBuiltinsAtDefinition(decoration, inst);
  3824. }
  3825. case SpvBuiltInBaseInstance:
  3826. case SpvBuiltInBaseVertex: {
  3827. return ValidateBaseInstanceOrVertexAtDefinition(decoration, inst);
  3828. }
  3829. case SpvBuiltInDrawIndex: {
  3830. return ValidateDrawIndexAtDefinition(decoration, inst);
  3831. }
  3832. case SpvBuiltInViewIndex: {
  3833. return ValidateViewIndexAtDefinition(decoration, inst);
  3834. }
  3835. case SpvBuiltInDeviceIndex: {
  3836. return ValidateDeviceIndexAtDefinition(decoration, inst);
  3837. }
  3838. case SpvBuiltInFragInvocationCountEXT: {
  3839. // alias SpvBuiltInInvocationsPerPixelNV
  3840. return ValidateFragInvocationCountAtDefinition(decoration, inst);
  3841. }
  3842. case SpvBuiltInFragSizeEXT: {
  3843. // alias SpvBuiltInFragmentSizeNV
  3844. return ValidateFragSizeAtDefinition(decoration, inst);
  3845. }
  3846. case SpvBuiltInFragStencilRefEXT: {
  3847. return ValidateFragStencilRefAtDefinition(decoration, inst);
  3848. }
  3849. case SpvBuiltInFullyCoveredEXT:{
  3850. return ValidateFullyCoveredAtDefinition(decoration, inst);
  3851. }
  3852. // Ray tracing builtins
  3853. case SpvBuiltInHitKindKHR: // alias SpvBuiltInHitKindNV
  3854. case SpvBuiltInHitTNV: // NOT present in KHR
  3855. case SpvBuiltInInstanceId:
  3856. case SpvBuiltInLaunchIdKHR: // alias SpvBuiltInLaunchIdNV
  3857. case SpvBuiltInLaunchSizeKHR: // alias SpvBuiltInLaunchSizeNV
  3858. case SpvBuiltInWorldRayOriginKHR: // alias SpvBuiltInWorldRayOriginNV
  3859. case SpvBuiltInWorldRayDirectionKHR: // alias SpvBuiltInWorldRayDirectionNV
  3860. case SpvBuiltInObjectRayOriginKHR: // alias SpvBuiltInObjectRayOriginNV
  3861. case SpvBuiltInObjectRayDirectionKHR: // alias
  3862. // SpvBuiltInObjectRayDirectionNV
  3863. case SpvBuiltInRayTminKHR: // alias SpvBuiltInRayTminNV
  3864. case SpvBuiltInRayTmaxKHR: // alias SpvBuiltInRayTmaxNV
  3865. case SpvBuiltInInstanceCustomIndexKHR: // alias
  3866. // SpvBuiltInInstanceCustomIndexNV
  3867. case SpvBuiltInObjectToWorldKHR: // alias SpvBuiltInObjectToWorldNV
  3868. case SpvBuiltInWorldToObjectKHR: // alias SpvBuiltInWorldToObjectNV
  3869. case SpvBuiltInIncomingRayFlagsKHR: // alias SpvBuiltInIncomingRayFlagsNV
  3870. case SpvBuiltInRayGeometryIndexKHR: // NOT present in NV
  3871. case SpvBuiltInCullMaskKHR: {
  3872. return ValidateRayTracingBuiltinsAtDefinition(decoration, inst);
  3873. }
  3874. case SpvBuiltInWorkDim:
  3875. case SpvBuiltInGlobalSize:
  3876. case SpvBuiltInEnqueuedWorkgroupSize:
  3877. case SpvBuiltInGlobalOffset:
  3878. case SpvBuiltInGlobalLinearId:
  3879. case SpvBuiltInSubgroupMaxSize:
  3880. case SpvBuiltInNumEnqueuedSubgroups:
  3881. case SpvBuiltInBaryCoordNoPerspAMD:
  3882. case SpvBuiltInBaryCoordNoPerspCentroidAMD:
  3883. case SpvBuiltInBaryCoordNoPerspSampleAMD:
  3884. case SpvBuiltInBaryCoordSmoothAMD:
  3885. case SpvBuiltInBaryCoordSmoothCentroidAMD:
  3886. case SpvBuiltInBaryCoordSmoothSampleAMD:
  3887. case SpvBuiltInBaryCoordPullModelAMD:
  3888. case SpvBuiltInViewportMaskNV:
  3889. case SpvBuiltInSecondaryPositionNV:
  3890. case SpvBuiltInSecondaryViewportMaskNV:
  3891. case SpvBuiltInPositionPerViewNV:
  3892. case SpvBuiltInViewportMaskPerViewNV:
  3893. case SpvBuiltInMax:
  3894. case SpvBuiltInTaskCountNV:
  3895. case SpvBuiltInPrimitiveCountNV:
  3896. case SpvBuiltInPrimitiveIndicesNV:
  3897. case SpvBuiltInClipDistancePerViewNV:
  3898. case SpvBuiltInCullDistancePerViewNV:
  3899. case SpvBuiltInLayerPerViewNV:
  3900. case SpvBuiltInMeshViewCountNV:
  3901. case SpvBuiltInMeshViewIndicesNV:
  3902. case SpvBuiltInCurrentRayTimeNV:
  3903. // No validation rules (for the moment).
  3904. break;
  3905. case SpvBuiltInPrimitiveShadingRateKHR: {
  3906. return ValidatePrimitiveShadingRateAtDefinition(decoration, inst);
  3907. }
  3908. case SpvBuiltInShadingRateKHR: {
  3909. return ValidateShadingRateAtDefinition(decoration, inst);
  3910. }
  3911. }
  3912. return SPV_SUCCESS;
  3913. }
  3914. spv_result_t BuiltInsValidator::ValidateBuiltInsAtDefinition() {
  3915. for (const auto& kv : _.id_decorations()) {
  3916. const uint32_t id = kv.first;
  3917. const auto& decorations = kv.second;
  3918. if (decorations.empty()) {
  3919. continue;
  3920. }
  3921. const Instruction* inst = _.FindDef(id);
  3922. assert(inst);
  3923. for (const auto& decoration : kv.second) {
  3924. if (decoration.dec_type() != SpvDecorationBuiltIn) {
  3925. continue;
  3926. }
  3927. if (spv_result_t error =
  3928. ValidateSingleBuiltInAtDefinition(decoration, *inst)) {
  3929. return error;
  3930. }
  3931. }
  3932. }
  3933. return SPV_SUCCESS;
  3934. }
  3935. spv_result_t BuiltInsValidator::Run() {
  3936. // First pass: validate all built-ins at definition and seed
  3937. // id_to_at_reference_checks_ with built-ins.
  3938. if (auto error = ValidateBuiltInsAtDefinition()) {
  3939. return error;
  3940. }
  3941. if (id_to_at_reference_checks_.empty()) {
  3942. // No validation tasks were seeded. Nothing else to do.
  3943. return SPV_SUCCESS;
  3944. }
  3945. // Second pass: validate every id reference in the module using
  3946. // rules in id_to_at_reference_checks_.
  3947. for (const Instruction& inst : _.ordered_instructions()) {
  3948. Update(inst);
  3949. std::set<uint32_t> already_checked;
  3950. for (const auto& operand : inst.operands()) {
  3951. if (!spvIsIdType(operand.type)) {
  3952. // Not id.
  3953. continue;
  3954. }
  3955. const uint32_t id = inst.word(operand.offset);
  3956. if (id == inst.id()) {
  3957. // No need to check result id.
  3958. continue;
  3959. }
  3960. if (!already_checked.insert(id).second) {
  3961. // The instruction has already referenced this id.
  3962. continue;
  3963. }
  3964. // Instruction references the id. Run all checks associated with the id
  3965. // on the instruction. id_to_at_reference_checks_ can be modified in the
  3966. // process, iterators are safe because it's a tree-based map.
  3967. const auto it = id_to_at_reference_checks_.find(id);
  3968. if (it != id_to_at_reference_checks_.end()) {
  3969. for (const auto& check : it->second) {
  3970. if (spv_result_t error = check(inst)) {
  3971. return error;
  3972. }
  3973. }
  3974. }
  3975. }
  3976. }
  3977. return SPV_SUCCESS;
  3978. }
  3979. } // namespace
  3980. // Validates correctness of built-in variables.
  3981. spv_result_t ValidateBuiltIns(ValidationState_t& _) {
  3982. BuiltInsValidator validator(_);
  3983. return validator.Run();
  3984. }
  3985. } // namespace val
  3986. } // namespace spvtools