validate_builtins.cpp 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389
  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() != spv::Op::OpTypeStruct) {
  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() == spv::Op::OpTypeStruct) {
  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. spv::StorageClass storage_class;
  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 spv::StorageClass::Max if not.
  89. spv::StorageClass GetStorageClass(const Instruction& inst) {
  90. switch (inst.opcode()) {
  91. case spv::Op::OpTypePointer:
  92. case spv::Op::OpTypeForwardPointer: {
  93. return spv::StorageClass(inst.word(2));
  94. }
  95. case spv::Op::OpVariable: {
  96. return spv::StorageClass(inst.word(3));
  97. }
  98. case spv::Op::OpGenericCastToPtrExplicit: {
  99. return spv::StorageClass(inst.word(4));
  100. }
  101. default: { break; }
  102. }
  103. return spv::StorageClass::Max;
  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. spv::BuiltIn 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. {spv::BuiltIn::SubgroupEqMask, {0, 4370, 4371}},
  119. {spv::BuiltIn::SubgroupGeMask, {0, 4372, 4373}},
  120. {spv::BuiltIn::SubgroupGtMask, {0, 4374, 4375}},
  121. {spv::BuiltIn::SubgroupLeMask, {0, 4376, 4377}},
  122. {spv::BuiltIn::SubgroupLtMask, {0, 4378, 4379}},
  123. {spv::BuiltIn::SubgroupLocalInvocationId, {0, 4380, 4381}},
  124. {spv::BuiltIn::SubgroupSize, {0, 4382, 4383}},
  125. {spv::BuiltIn::GlobalInvocationId, {4236, 4237, 4238}},
  126. {spv::BuiltIn::LocalInvocationId, {4281, 4282, 4283}},
  127. {spv::BuiltIn::NumWorkgroups, {4296, 4297, 4298}},
  128. {spv::BuiltIn::NumSubgroups, {4293, 4294, 4295}},
  129. {spv::BuiltIn::SubgroupId, {4367, 4368, 4369}},
  130. {spv::BuiltIn::WorkgroupId, {4422, 4423, 4424}},
  131. {spv::BuiltIn::HitKindKHR, {4242, 4243, 4244}},
  132. {spv::BuiltIn::HitTNV, {4245, 4246, 4247}},
  133. {spv::BuiltIn::InstanceCustomIndexKHR, {4251, 4252, 4253}},
  134. {spv::BuiltIn::InstanceId, {4254, 4255, 4256}},
  135. {spv::BuiltIn::RayGeometryIndexKHR, {4345, 4346, 4347}},
  136. {spv::BuiltIn::ObjectRayDirectionKHR, {4299, 4300, 4301}},
  137. {spv::BuiltIn::ObjectRayOriginKHR, {4302, 4303, 4304}},
  138. {spv::BuiltIn::ObjectToWorldKHR, {4305, 4306, 4307}},
  139. {spv::BuiltIn::WorldToObjectKHR, {4434, 4435, 4436}},
  140. {spv::BuiltIn::IncomingRayFlagsKHR, {4248, 4249, 4250}},
  141. {spv::BuiltIn::RayTminKHR, {4351, 4352, 4353}},
  142. {spv::BuiltIn::RayTmaxKHR, {4348, 4349, 4350}},
  143. {spv::BuiltIn::WorldRayDirectionKHR, {4428, 4429, 4430}},
  144. {spv::BuiltIn::WorldRayOriginKHR, {4431, 4432, 4433}},
  145. {spv::BuiltIn::LaunchIdKHR, {4266, 4267, 4268}},
  146. {spv::BuiltIn::LaunchSizeKHR, {4269, 4270, 4271}},
  147. {spv::BuiltIn::FragInvocationCountEXT, {4217, 4218, 4219}},
  148. {spv::BuiltIn::FragSizeEXT, {4220, 4221, 4222}},
  149. {spv::BuiltIn::FragStencilRefEXT, {4223, 4224, 4225}},
  150. {spv::BuiltIn::FullyCoveredEXT, {4232, 4233, 4234}},
  151. {spv::BuiltIn::CullMaskKHR, {6735, 6736, 6737}},
  152. {spv::BuiltIn::BaryCoordKHR, {4154, 4155, 4156}},
  153. {spv::BuiltIn::BaryCoordNoPerspKHR, {4160, 4161, 4162}},
  154. // clang-format off
  155. } };
  156. uint32_t GetVUIDForBuiltin(spv::BuiltIn 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(spv::BuiltIn builtin,
  168. spv::ExecutionModel stage) {
  169. switch (builtin) {
  170. case spv::BuiltIn::HitKindKHR:
  171. case spv::BuiltIn::HitTNV:
  172. if (stage == spv::ExecutionModel::AnyHitKHR ||
  173. stage == spv::ExecutionModel::ClosestHitKHR) {
  174. return true;
  175. }
  176. break;
  177. case spv::BuiltIn::InstanceCustomIndexKHR:
  178. case spv::BuiltIn::InstanceId:
  179. case spv::BuiltIn::RayGeometryIndexKHR:
  180. case spv::BuiltIn::ObjectRayDirectionKHR:
  181. case spv::BuiltIn::ObjectRayOriginKHR:
  182. case spv::BuiltIn::ObjectToWorldKHR:
  183. case spv::BuiltIn::WorldToObjectKHR:
  184. switch (stage) {
  185. case spv::ExecutionModel::IntersectionKHR:
  186. case spv::ExecutionModel::AnyHitKHR:
  187. case spv::ExecutionModel::ClosestHitKHR:
  188. return true;
  189. default:
  190. return false;
  191. }
  192. break;
  193. case spv::BuiltIn::IncomingRayFlagsKHR:
  194. case spv::BuiltIn::RayTminKHR:
  195. case spv::BuiltIn::RayTmaxKHR:
  196. case spv::BuiltIn::WorldRayDirectionKHR:
  197. case spv::BuiltIn::WorldRayOriginKHR:
  198. case spv::BuiltIn::CullMaskKHR:
  199. switch (stage) {
  200. case spv::ExecutionModel::IntersectionKHR:
  201. case spv::ExecutionModel::AnyHitKHR:
  202. case spv::ExecutionModel::ClosestHitKHR:
  203. case spv::ExecutionModel::MissKHR:
  204. return true;
  205. default:
  206. return false;
  207. }
  208. break;
  209. case spv::BuiltIn::LaunchIdKHR:
  210. case spv::BuiltIn::LaunchSizeKHR:
  211. switch (stage) {
  212. case spv::ExecutionModel::RayGenerationKHR:
  213. case spv::ExecutionModel::IntersectionKHR:
  214. case spv::ExecutionModel::AnyHitKHR:
  215. case spv::ExecutionModel::ClosestHitKHR:
  216. case spv::ExecutionModel::MissKHR:
  217. case spv::ExecutionModel::CallableKHR:
  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 ValidateNVSMOrARMCoreBuiltinsAtDefinition(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 ValidateNVSMOrARMCoreBuiltinsAtReference(
  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, spv::ExecutionModel 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. spv::ExecutionModel execution_model = spv::ExecutionModel::Max) 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<spv::ExecutionModel> execution_models_;
  604. };
  605. void BuiltInsValidator::Update(const Instruction& inst) {
  606. const spv::Op opcode = inst.opcode();
  607. if (opcode == spv::Op::OpFunction) {
  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 == spv::Op::OpFunctionEnd) {
  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() == spv::Op::OpTypeStruct);
  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. spv::ExecutionModel 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 != spv::ExecutionModel::Max) {
  657. ss << " called with execution model ";
  658. ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_EXECUTION_MODEL,
  659. uint32_t(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. uint32_t(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) == spv::Op::OpTypeArray) {
  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) == spv::Op::OpTypeArray) {
  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) == spv::Op::OpTypeArray) {
  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() != spv::Op::OpTypeArray) {
  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) == spv::Op::OpTypeArray) {
  911. uint32_t subtype = _.FindDef(underlying_type)->word(2u);
  912. if (_.GetIdOpcode(subtype) == spv::Op::OpTypeArray) {
  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() != spv::Op::OpTypeArray) {
  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, spv::ExecutionModel 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, uint32_t(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 spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1020. if (storage_class != spv::StorageClass::Max &&
  1021. storage_class != spv::StorageClass::Input &&
  1022. storage_class != spv::StorageClass::Output) {
  1023. uint32_t vuid = (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::ClipDistance) ? 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 == spv::StorageClass::Input) {
  1035. assert(function_id_ == 0);
  1036. uint32_t vuid = (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::ClipDistance) ? 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. spv::ExecutionModel::Vertex, 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. "MeshNV.",
  1049. spv::ExecutionModel::MeshNV, decoration, built_in_inst,
  1050. referenced_from_inst, std::placeholders::_1));
  1051. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1052. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  1053. "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
  1054. "used for variables with Input storage class if execution model is "
  1055. "MeshEXT.",
  1056. spv::ExecutionModel::MeshEXT, decoration, built_in_inst,
  1057. referenced_from_inst, std::placeholders::_1));
  1058. }
  1059. if (storage_class == spv::StorageClass::Output) {
  1060. assert(function_id_ == 0);
  1061. uint32_t vuid = (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::ClipDistance) ? 4189 : 4198;
  1062. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1063. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  1064. "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
  1065. "used for variables with Output storage class if execution model is "
  1066. "Fragment.",
  1067. spv::ExecutionModel::Fragment, decoration, built_in_inst,
  1068. referenced_from_inst, std::placeholders::_1));
  1069. }
  1070. for (const spv::ExecutionModel execution_model : execution_models_) {
  1071. switch (execution_model) {
  1072. case spv::ExecutionModel::Fragment:
  1073. case spv::ExecutionModel::Vertex: {
  1074. if (spv_result_t error = ValidateF32Arr(
  1075. decoration, built_in_inst, /* Any number of components */ 0,
  1076. [this, &decoration, &referenced_from_inst](
  1077. const std::string& message) -> spv_result_t {
  1078. uint32_t vuid =
  1079. (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::ClipDistance)
  1080. ? 4191
  1081. : 4200;
  1082. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1083. << _.VkErrorID(vuid)
  1084. << "According to the Vulkan spec BuiltIn "
  1085. << _.grammar().lookupOperandName(
  1086. SPV_OPERAND_TYPE_BUILT_IN,
  1087. decoration.params()[0])
  1088. << " variable needs to be a 32-bit float array. "
  1089. << message;
  1090. })) {
  1091. return error;
  1092. }
  1093. break;
  1094. }
  1095. case spv::ExecutionModel::TessellationControl:
  1096. case spv::ExecutionModel::TessellationEvaluation:
  1097. case spv::ExecutionModel::Geometry:
  1098. case spv::ExecutionModel::MeshNV:
  1099. case spv::ExecutionModel::MeshEXT: {
  1100. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1101. // The outer level of array is applied on the variable.
  1102. if (spv_result_t error = ValidateF32Arr(
  1103. decoration, built_in_inst, /* Any number of components */ 0,
  1104. [this, &decoration, &referenced_from_inst](
  1105. const std::string& message) -> spv_result_t {
  1106. uint32_t vuid =
  1107. (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::ClipDistance)
  1108. ? 4191
  1109. : 4200;
  1110. return _.diag(SPV_ERROR_INVALID_DATA,
  1111. &referenced_from_inst)
  1112. << _.VkErrorID(vuid)
  1113. << "According to the Vulkan spec BuiltIn "
  1114. << _.grammar().lookupOperandName(
  1115. SPV_OPERAND_TYPE_BUILT_IN,
  1116. decoration.params()[0])
  1117. << " variable needs to be a 32-bit float array. "
  1118. << message;
  1119. })) {
  1120. return error;
  1121. }
  1122. } else {
  1123. if (spv_result_t error = ValidateOptionalArrayedF32Arr(
  1124. decoration, built_in_inst, /* Any number of components */ 0,
  1125. [this, &decoration, &referenced_from_inst](
  1126. const std::string& message) -> spv_result_t {
  1127. uint32_t vuid =
  1128. (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::ClipDistance)
  1129. ? 4191
  1130. : 4200;
  1131. return _.diag(SPV_ERROR_INVALID_DATA,
  1132. &referenced_from_inst)
  1133. << _.VkErrorID(vuid)
  1134. << "According to the Vulkan spec BuiltIn "
  1135. << _.grammar().lookupOperandName(
  1136. SPV_OPERAND_TYPE_BUILT_IN,
  1137. decoration.params()[0])
  1138. << " variable needs to be a 32-bit float array. "
  1139. << message;
  1140. })) {
  1141. return error;
  1142. }
  1143. }
  1144. break;
  1145. }
  1146. default: {
  1147. uint32_t vuid =
  1148. (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::ClipDistance) ? 4187 : 4196;
  1149. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1150. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  1151. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  1152. operand)
  1153. << " to be used only with Fragment, Vertex, "
  1154. "TessellationControl, TessellationEvaluation or Geometry "
  1155. "execution models. "
  1156. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1157. referenced_from_inst, execution_model);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. if (function_id_ == 0) {
  1163. // Propagate this rule to all dependant ids in the global scope.
  1164. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  1165. std::bind(&BuiltInsValidator::ValidateClipOrCullDistanceAtReference,
  1166. this, decoration, built_in_inst, referenced_from_inst,
  1167. std::placeholders::_1));
  1168. }
  1169. return SPV_SUCCESS;
  1170. }
  1171. spv_result_t BuiltInsValidator::ValidateFragCoordAtDefinition(
  1172. const Decoration& decoration, const Instruction& inst) {
  1173. if (spvIsVulkanEnv(_.context()->target_env)) {
  1174. if (spv_result_t error = ValidateF32Vec(
  1175. decoration, inst, 4,
  1176. [this, &inst](const std::string& message) -> spv_result_t {
  1177. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1178. << _.VkErrorID(4212) << "According to the "
  1179. << spvLogStringForEnv(_.context()->target_env)
  1180. << " spec BuiltIn FragCoord "
  1181. "variable needs to be a 4-component 32-bit float "
  1182. "vector. "
  1183. << message;
  1184. })) {
  1185. return error;
  1186. }
  1187. }
  1188. // Seed at reference checks with this built-in.
  1189. return ValidateFragCoordAtReference(decoration, inst, inst, inst);
  1190. }
  1191. spv_result_t BuiltInsValidator::ValidateFragCoordAtReference(
  1192. const Decoration& decoration, const Instruction& built_in_inst,
  1193. const Instruction& referenced_inst,
  1194. const Instruction& referenced_from_inst) {
  1195. if (spvIsVulkanEnv(_.context()->target_env)) {
  1196. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1197. if (storage_class != spv::StorageClass::Max &&
  1198. storage_class != spv::StorageClass::Input) {
  1199. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1200. << _.VkErrorID(4211) << spvLogStringForEnv(_.context()->target_env)
  1201. << " spec allows BuiltIn FragCoord to be only used for "
  1202. "variables with Input storage class. "
  1203. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1204. referenced_from_inst)
  1205. << " " << GetStorageClassDesc(referenced_from_inst);
  1206. }
  1207. for (const spv::ExecutionModel execution_model : execution_models_) {
  1208. if (execution_model != spv::ExecutionModel::Fragment) {
  1209. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1210. << _.VkErrorID(4210)
  1211. << spvLogStringForEnv(_.context()->target_env)
  1212. << " spec allows BuiltIn FragCoord to be used only with "
  1213. "Fragment execution model. "
  1214. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1215. referenced_from_inst, execution_model);
  1216. }
  1217. }
  1218. }
  1219. if (function_id_ == 0) {
  1220. // Propagate this rule to all dependant ids in the global scope.
  1221. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1222. &BuiltInsValidator::ValidateFragCoordAtReference, this, decoration,
  1223. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1224. }
  1225. return SPV_SUCCESS;
  1226. }
  1227. spv_result_t BuiltInsValidator::ValidateFragDepthAtDefinition(
  1228. const Decoration& decoration, const Instruction& inst) {
  1229. if (spvIsVulkanEnv(_.context()->target_env)) {
  1230. if (spv_result_t error = ValidateF32(
  1231. decoration, inst,
  1232. [this, &inst](const std::string& message) -> spv_result_t {
  1233. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1234. << _.VkErrorID(4215) << "According to the "
  1235. << spvLogStringForEnv(_.context()->target_env)
  1236. << " spec BuiltIn FragDepth "
  1237. "variable needs to be a 32-bit float scalar. "
  1238. << message;
  1239. })) {
  1240. return error;
  1241. }
  1242. }
  1243. // Seed at reference checks with this built-in.
  1244. return ValidateFragDepthAtReference(decoration, inst, inst, inst);
  1245. }
  1246. spv_result_t BuiltInsValidator::ValidateFragDepthAtReference(
  1247. const Decoration& decoration, const Instruction& built_in_inst,
  1248. const Instruction& referenced_inst,
  1249. const Instruction& referenced_from_inst) {
  1250. if (spvIsVulkanEnv(_.context()->target_env)) {
  1251. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1252. if (storage_class != spv::StorageClass::Max &&
  1253. storage_class != spv::StorageClass::Output) {
  1254. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1255. << _.VkErrorID(4214) << spvLogStringForEnv(_.context()->target_env)
  1256. << " spec allows BuiltIn FragDepth to be only used for "
  1257. "variables with Output storage class. "
  1258. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1259. referenced_from_inst)
  1260. << " " << GetStorageClassDesc(referenced_from_inst);
  1261. }
  1262. for (const spv::ExecutionModel execution_model : execution_models_) {
  1263. if (execution_model != spv::ExecutionModel::Fragment) {
  1264. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1265. << _.VkErrorID(4213)
  1266. << spvLogStringForEnv(_.context()->target_env)
  1267. << " spec allows BuiltIn FragDepth to be used only with "
  1268. "Fragment execution model. "
  1269. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1270. referenced_from_inst, execution_model);
  1271. }
  1272. }
  1273. for (const uint32_t entry_point : *entry_points_) {
  1274. // Every entry point from which this function is called needs to have
  1275. // Execution Mode DepthReplacing.
  1276. const auto* modes = _.GetExecutionModes(entry_point);
  1277. if (!modes || !modes->count(spv::ExecutionMode::DepthReplacing)) {
  1278. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1279. << _.VkErrorID(4216)
  1280. << spvLogStringForEnv(_.context()->target_env)
  1281. << " spec requires DepthReplacing execution mode to be "
  1282. "declared when using BuiltIn FragDepth. "
  1283. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1284. referenced_from_inst);
  1285. }
  1286. }
  1287. }
  1288. if (function_id_ == 0) {
  1289. // Propagate this rule to all dependant ids in the global scope.
  1290. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1291. &BuiltInsValidator::ValidateFragDepthAtReference, this, decoration,
  1292. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1293. }
  1294. return SPV_SUCCESS;
  1295. }
  1296. spv_result_t BuiltInsValidator::ValidateFrontFacingAtDefinition(
  1297. const Decoration& decoration, const Instruction& inst) {
  1298. if (spvIsVulkanEnv(_.context()->target_env)) {
  1299. if (spv_result_t error = ValidateBool(
  1300. decoration, inst,
  1301. [this, &inst](const std::string& message) -> spv_result_t {
  1302. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1303. << _.VkErrorID(4231) << "According to the "
  1304. << spvLogStringForEnv(_.context()->target_env)
  1305. << " spec BuiltIn FrontFacing "
  1306. "variable needs to be a bool scalar. "
  1307. << message;
  1308. })) {
  1309. return error;
  1310. }
  1311. }
  1312. // Seed at reference checks with this built-in.
  1313. return ValidateFrontFacingAtReference(decoration, inst, inst, inst);
  1314. }
  1315. spv_result_t BuiltInsValidator::ValidateFrontFacingAtReference(
  1316. const Decoration& decoration, const Instruction& built_in_inst,
  1317. const Instruction& referenced_inst,
  1318. const Instruction& referenced_from_inst) {
  1319. if (spvIsVulkanEnv(_.context()->target_env)) {
  1320. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1321. if (storage_class != spv::StorageClass::Max &&
  1322. storage_class != spv::StorageClass::Input) {
  1323. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1324. << _.VkErrorID(4230) << spvLogStringForEnv(_.context()->target_env)
  1325. << " spec allows BuiltIn FrontFacing to be only used for "
  1326. "variables with Input storage class. "
  1327. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1328. referenced_from_inst)
  1329. << " " << GetStorageClassDesc(referenced_from_inst);
  1330. }
  1331. for (const spv::ExecutionModel execution_model : execution_models_) {
  1332. if (execution_model != spv::ExecutionModel::Fragment) {
  1333. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1334. << _.VkErrorID(4229)
  1335. << spvLogStringForEnv(_.context()->target_env)
  1336. << " spec allows BuiltIn FrontFacing to be used only with "
  1337. "Fragment execution model. "
  1338. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1339. referenced_from_inst, execution_model);
  1340. }
  1341. }
  1342. }
  1343. if (function_id_ == 0) {
  1344. // Propagate this rule to all dependant ids in the global scope.
  1345. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1346. &BuiltInsValidator::ValidateFrontFacingAtReference, this, decoration,
  1347. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1348. }
  1349. return SPV_SUCCESS;
  1350. }
  1351. spv_result_t BuiltInsValidator::ValidateHelperInvocationAtDefinition(
  1352. const Decoration& decoration, const Instruction& inst) {
  1353. if (spvIsVulkanEnv(_.context()->target_env)) {
  1354. if (spv_result_t error = ValidateBool(
  1355. decoration, inst,
  1356. [this, &inst](const std::string& message) -> spv_result_t {
  1357. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1358. << _.VkErrorID(4241)
  1359. << "According to the Vulkan spec BuiltIn HelperInvocation "
  1360. "variable needs to be a bool scalar. "
  1361. << message;
  1362. })) {
  1363. return error;
  1364. }
  1365. }
  1366. // Seed at reference checks with this built-in.
  1367. return ValidateHelperInvocationAtReference(decoration, inst, inst, inst);
  1368. }
  1369. spv_result_t BuiltInsValidator::ValidateHelperInvocationAtReference(
  1370. const Decoration& decoration, const Instruction& built_in_inst,
  1371. const Instruction& referenced_inst,
  1372. const Instruction& referenced_from_inst) {
  1373. if (spvIsVulkanEnv(_.context()->target_env)) {
  1374. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1375. if (storage_class != spv::StorageClass::Max &&
  1376. storage_class != spv::StorageClass::Input) {
  1377. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1378. << _.VkErrorID(4240)
  1379. << "Vulkan spec allows BuiltIn HelperInvocation to be only used "
  1380. "for variables with Input storage class. "
  1381. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1382. referenced_from_inst)
  1383. << " " << GetStorageClassDesc(referenced_from_inst);
  1384. }
  1385. for (const spv::ExecutionModel execution_model : execution_models_) {
  1386. if (execution_model != spv::ExecutionModel::Fragment) {
  1387. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1388. << _.VkErrorID(4239)
  1389. << "Vulkan spec allows BuiltIn HelperInvocation to be used only "
  1390. "with Fragment execution model. "
  1391. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1392. referenced_from_inst, execution_model);
  1393. }
  1394. }
  1395. }
  1396. if (function_id_ == 0) {
  1397. // Propagate this rule to all dependant ids in the global scope.
  1398. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  1399. std::bind(&BuiltInsValidator::ValidateHelperInvocationAtReference, this,
  1400. decoration, built_in_inst, referenced_from_inst,
  1401. std::placeholders::_1));
  1402. }
  1403. return SPV_SUCCESS;
  1404. }
  1405. spv_result_t BuiltInsValidator::ValidateInvocationIdAtDefinition(
  1406. const Decoration& decoration, const Instruction& inst) {
  1407. if (spvIsVulkanEnv(_.context()->target_env)) {
  1408. if (spv_result_t error = ValidateI32(
  1409. decoration, inst,
  1410. [this, &inst](const std::string& message) -> spv_result_t {
  1411. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1412. << _.VkErrorID(4259)
  1413. << "According to the Vulkan spec BuiltIn InvocationId "
  1414. "variable needs to be a 32-bit int scalar. "
  1415. << message;
  1416. })) {
  1417. return error;
  1418. }
  1419. }
  1420. // Seed at reference checks with this built-in.
  1421. return ValidateInvocationIdAtReference(decoration, inst, inst, inst);
  1422. }
  1423. spv_result_t BuiltInsValidator::ValidateInvocationIdAtReference(
  1424. const Decoration& decoration, const Instruction& built_in_inst,
  1425. const Instruction& referenced_inst,
  1426. const Instruction& referenced_from_inst) {
  1427. if (spvIsVulkanEnv(_.context()->target_env)) {
  1428. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1429. if (storage_class != spv::StorageClass::Max &&
  1430. storage_class != spv::StorageClass::Input) {
  1431. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1432. << _.VkErrorID(4258)
  1433. << "Vulkan spec allows BuiltIn InvocationId to be only used for "
  1434. "variables with Input storage class. "
  1435. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1436. referenced_from_inst)
  1437. << " " << GetStorageClassDesc(referenced_from_inst);
  1438. }
  1439. for (const spv::ExecutionModel execution_model : execution_models_) {
  1440. if (execution_model != spv::ExecutionModel::TessellationControl &&
  1441. execution_model != spv::ExecutionModel::Geometry) {
  1442. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1443. << _.VkErrorID(4257)
  1444. << "Vulkan spec allows BuiltIn InvocationId to be used only "
  1445. "with TessellationControl or Geometry execution models. "
  1446. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1447. referenced_from_inst, execution_model);
  1448. }
  1449. }
  1450. }
  1451. if (function_id_ == 0) {
  1452. // Propagate this rule to all dependant ids in the global scope.
  1453. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1454. &BuiltInsValidator::ValidateInvocationIdAtReference, this, decoration,
  1455. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1456. }
  1457. return SPV_SUCCESS;
  1458. }
  1459. spv_result_t BuiltInsValidator::ValidateInstanceIndexAtDefinition(
  1460. const Decoration& decoration, const Instruction& inst) {
  1461. if (spvIsVulkanEnv(_.context()->target_env)) {
  1462. if (spv_result_t error = ValidateI32(
  1463. decoration, inst,
  1464. [this, &inst](const std::string& message) -> spv_result_t {
  1465. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1466. << _.VkErrorID(4265) << "According to the "
  1467. << spvLogStringForEnv(_.context()->target_env)
  1468. << " spec BuiltIn InstanceIndex "
  1469. "variable needs to be a 32-bit int scalar. "
  1470. << message;
  1471. })) {
  1472. return error;
  1473. }
  1474. }
  1475. // Seed at reference checks with this built-in.
  1476. return ValidateInstanceIndexAtReference(decoration, inst, inst, inst);
  1477. }
  1478. spv_result_t BuiltInsValidator::ValidateInstanceIndexAtReference(
  1479. const Decoration& decoration, const Instruction& built_in_inst,
  1480. const Instruction& referenced_inst,
  1481. const Instruction& referenced_from_inst) {
  1482. if (spvIsVulkanEnv(_.context()->target_env)) {
  1483. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1484. if (storage_class != spv::StorageClass::Max &&
  1485. storage_class != spv::StorageClass::Input) {
  1486. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1487. << _.VkErrorID(4264) << spvLogStringForEnv(_.context()->target_env)
  1488. << " spec allows BuiltIn InstanceIndex to be only used for "
  1489. "variables with Input storage class. "
  1490. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1491. referenced_from_inst)
  1492. << " " << GetStorageClassDesc(referenced_from_inst);
  1493. }
  1494. for (const spv::ExecutionModel execution_model : execution_models_) {
  1495. if (execution_model != spv::ExecutionModel::Vertex) {
  1496. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1497. << _.VkErrorID(4263)
  1498. << spvLogStringForEnv(_.context()->target_env)
  1499. << " spec allows BuiltIn InstanceIndex to be used only "
  1500. "with Vertex execution model. "
  1501. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1502. referenced_from_inst, execution_model);
  1503. }
  1504. }
  1505. }
  1506. if (function_id_ == 0) {
  1507. // Propagate this rule to all dependant ids in the global scope.
  1508. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1509. &BuiltInsValidator::ValidateInstanceIndexAtReference, this, decoration,
  1510. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1511. }
  1512. return SPV_SUCCESS;
  1513. }
  1514. spv_result_t BuiltInsValidator::ValidatePatchVerticesAtDefinition(
  1515. const Decoration& decoration, const Instruction& inst) {
  1516. if (spvIsVulkanEnv(_.context()->target_env)) {
  1517. if (spv_result_t error = ValidateI32(
  1518. decoration, inst,
  1519. [this, &inst](const std::string& message) -> spv_result_t {
  1520. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1521. << _.VkErrorID(4310)
  1522. << "According to the Vulkan spec BuiltIn PatchVertices "
  1523. "variable needs to be a 32-bit int scalar. "
  1524. << message;
  1525. })) {
  1526. return error;
  1527. }
  1528. }
  1529. // Seed at reference checks with this built-in.
  1530. return ValidatePatchVerticesAtReference(decoration, inst, inst, inst);
  1531. }
  1532. spv_result_t BuiltInsValidator::ValidatePatchVerticesAtReference(
  1533. const Decoration& decoration, const Instruction& built_in_inst,
  1534. const Instruction& referenced_inst,
  1535. const Instruction& referenced_from_inst) {
  1536. if (spvIsVulkanEnv(_.context()->target_env)) {
  1537. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1538. if (storage_class != spv::StorageClass::Max &&
  1539. storage_class != spv::StorageClass::Input) {
  1540. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1541. << _.VkErrorID(4309)
  1542. << "Vulkan spec allows BuiltIn PatchVertices to be only used for "
  1543. "variables with Input storage class. "
  1544. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1545. referenced_from_inst)
  1546. << " " << GetStorageClassDesc(referenced_from_inst);
  1547. }
  1548. for (const spv::ExecutionModel execution_model : execution_models_) {
  1549. if (execution_model != spv::ExecutionModel::TessellationControl &&
  1550. execution_model != spv::ExecutionModel::TessellationEvaluation) {
  1551. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1552. << _.VkErrorID(4308)
  1553. << "Vulkan spec allows BuiltIn PatchVertices to be used only "
  1554. "with TessellationControl or TessellationEvaluation "
  1555. "execution models. "
  1556. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1557. referenced_from_inst, execution_model);
  1558. }
  1559. }
  1560. }
  1561. if (function_id_ == 0) {
  1562. // Propagate this rule to all dependant ids in the global scope.
  1563. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1564. &BuiltInsValidator::ValidatePatchVerticesAtReference, this, decoration,
  1565. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1566. }
  1567. return SPV_SUCCESS;
  1568. }
  1569. spv_result_t BuiltInsValidator::ValidatePointCoordAtDefinition(
  1570. const Decoration& decoration, const Instruction& inst) {
  1571. if (spvIsVulkanEnv(_.context()->target_env)) {
  1572. if (spv_result_t error = ValidateF32Vec(
  1573. decoration, inst, 2,
  1574. [this, &inst](const std::string& message) -> spv_result_t {
  1575. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1576. << _.VkErrorID(4313)
  1577. << "According to the Vulkan spec BuiltIn PointCoord "
  1578. "variable needs to be a 2-component 32-bit float "
  1579. "vector. "
  1580. << message;
  1581. })) {
  1582. return error;
  1583. }
  1584. }
  1585. // Seed at reference checks with this built-in.
  1586. return ValidatePointCoordAtReference(decoration, inst, inst, inst);
  1587. }
  1588. spv_result_t BuiltInsValidator::ValidatePointCoordAtReference(
  1589. const Decoration& decoration, const Instruction& built_in_inst,
  1590. const Instruction& referenced_inst,
  1591. const Instruction& referenced_from_inst) {
  1592. if (spvIsVulkanEnv(_.context()->target_env)) {
  1593. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1594. if (storage_class != spv::StorageClass::Max &&
  1595. storage_class != spv::StorageClass::Input) {
  1596. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1597. << _.VkErrorID(4312)
  1598. << "Vulkan spec allows BuiltIn PointCoord to be only used for "
  1599. "variables with Input storage class. "
  1600. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1601. referenced_from_inst)
  1602. << " " << GetStorageClassDesc(referenced_from_inst);
  1603. }
  1604. for (const spv::ExecutionModel execution_model : execution_models_) {
  1605. if (execution_model != spv::ExecutionModel::Fragment) {
  1606. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1607. << _.VkErrorID(4311)
  1608. << "Vulkan spec allows BuiltIn PointCoord to be used only with "
  1609. "Fragment execution model. "
  1610. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1611. referenced_from_inst, execution_model);
  1612. }
  1613. }
  1614. }
  1615. if (function_id_ == 0) {
  1616. // Propagate this rule to all dependant ids in the global scope.
  1617. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1618. &BuiltInsValidator::ValidatePointCoordAtReference, this, decoration,
  1619. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1620. }
  1621. return SPV_SUCCESS;
  1622. }
  1623. spv_result_t BuiltInsValidator::ValidatePointSizeAtDefinition(
  1624. const Decoration& decoration, const Instruction& inst) {
  1625. // Seed at reference checks with this built-in.
  1626. return ValidatePointSizeAtReference(decoration, inst, inst, inst);
  1627. }
  1628. spv_result_t BuiltInsValidator::ValidatePointSizeAtReference(
  1629. const Decoration& decoration, const Instruction& built_in_inst,
  1630. const Instruction& referenced_inst,
  1631. const Instruction& referenced_from_inst) {
  1632. if (spvIsVulkanEnv(_.context()->target_env)) {
  1633. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1634. if (storage_class != spv::StorageClass::Max &&
  1635. storage_class != spv::StorageClass::Input &&
  1636. storage_class != spv::StorageClass::Output) {
  1637. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1638. << _.VkErrorID(4316)
  1639. << "Vulkan spec allows BuiltIn PointSize to be only used for "
  1640. "variables with Input or Output storage class. "
  1641. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1642. referenced_from_inst)
  1643. << " " << GetStorageClassDesc(referenced_from_inst);
  1644. }
  1645. if (storage_class == spv::StorageClass::Input) {
  1646. assert(function_id_ == 0);
  1647. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1648. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4315,
  1649. "Vulkan spec doesn't allow BuiltIn PointSize to be used for "
  1650. "variables with Input storage class if execution model is "
  1651. "Vertex.",
  1652. spv::ExecutionModel::Vertex, decoration, built_in_inst,
  1653. referenced_from_inst, std::placeholders::_1));
  1654. }
  1655. for (const spv::ExecutionModel execution_model : execution_models_) {
  1656. switch (execution_model) {
  1657. case spv::ExecutionModel::Vertex: {
  1658. if (spv_result_t error = ValidateF32(
  1659. decoration, built_in_inst,
  1660. [this, &referenced_from_inst](
  1661. const std::string& message) -> spv_result_t {
  1662. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1663. << _.VkErrorID(4317)
  1664. << "According to the Vulkan spec BuiltIn PointSize "
  1665. "variable needs to be a 32-bit float scalar. "
  1666. << message;
  1667. })) {
  1668. return error;
  1669. }
  1670. break;
  1671. }
  1672. case spv::ExecutionModel::TessellationControl:
  1673. case spv::ExecutionModel::TessellationEvaluation:
  1674. case spv::ExecutionModel::Geometry:
  1675. case spv::ExecutionModel::MeshNV:
  1676. case spv::ExecutionModel::MeshEXT: {
  1677. // PointSize can be a per-vertex variable for tessellation control,
  1678. // tessellation evaluation and geometry shader stages. In such cases
  1679. // variables will have an array of 32-bit floats.
  1680. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1681. // The array is on the variable, so this must be a 32-bit float.
  1682. if (spv_result_t error = ValidateF32(
  1683. decoration, built_in_inst,
  1684. [this, &referenced_from_inst](
  1685. const std::string& message) -> spv_result_t {
  1686. return _.diag(SPV_ERROR_INVALID_DATA,
  1687. &referenced_from_inst)
  1688. << _.VkErrorID(4317)
  1689. << "According to the Vulkan spec BuiltIn "
  1690. "PointSize variable needs to be a 32-bit "
  1691. "float scalar. "
  1692. << message;
  1693. })) {
  1694. return error;
  1695. }
  1696. } else {
  1697. if (spv_result_t error = ValidateOptionalArrayedF32(
  1698. decoration, built_in_inst,
  1699. [this, &referenced_from_inst](
  1700. const std::string& message) -> spv_result_t {
  1701. return _.diag(SPV_ERROR_INVALID_DATA,
  1702. &referenced_from_inst)
  1703. << _.VkErrorID(4317)
  1704. << "According to the Vulkan spec BuiltIn "
  1705. "PointSize variable needs to be a 32-bit "
  1706. "float scalar. "
  1707. << message;
  1708. })) {
  1709. return error;
  1710. }
  1711. }
  1712. break;
  1713. }
  1714. default: {
  1715. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1716. << _.VkErrorID(4314)
  1717. << "Vulkan spec allows BuiltIn PointSize to be used only with "
  1718. "Vertex, TessellationControl, TessellationEvaluation or "
  1719. "Geometry execution models. "
  1720. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1721. referenced_from_inst, execution_model);
  1722. }
  1723. }
  1724. }
  1725. }
  1726. if (function_id_ == 0) {
  1727. // Propagate this rule to all dependant ids in the global scope.
  1728. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1729. &BuiltInsValidator::ValidatePointSizeAtReference, this, decoration,
  1730. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1731. }
  1732. return SPV_SUCCESS;
  1733. }
  1734. spv_result_t BuiltInsValidator::ValidatePositionAtDefinition(
  1735. const Decoration& decoration, const Instruction& inst) {
  1736. // Seed at reference checks with this built-in.
  1737. return ValidatePositionAtReference(decoration, inst, inst, inst);
  1738. }
  1739. spv_result_t BuiltInsValidator::ValidatePositionAtReference(
  1740. const Decoration& decoration, const Instruction& built_in_inst,
  1741. const Instruction& referenced_inst,
  1742. const Instruction& referenced_from_inst) {
  1743. if (spvIsVulkanEnv(_.context()->target_env)) {
  1744. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1745. if (storage_class != spv::StorageClass::Max &&
  1746. storage_class != spv::StorageClass::Input &&
  1747. storage_class != spv::StorageClass::Output) {
  1748. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1749. << _.VkErrorID(4320) << "Vulkan spec allows BuiltIn Position to be only used for "
  1750. "variables with Input or Output storage class. "
  1751. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1752. referenced_from_inst)
  1753. << " " << GetStorageClassDesc(referenced_from_inst);
  1754. }
  1755. if (storage_class == spv::StorageClass::Input) {
  1756. assert(function_id_ == 0);
  1757. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1758. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4319,
  1759. "Vulkan spec doesn't allow BuiltIn Position to be used "
  1760. "for variables "
  1761. "with Input storage class if execution model is Vertex.",
  1762. spv::ExecutionModel::Vertex, decoration, built_in_inst,
  1763. referenced_from_inst, std::placeholders::_1));
  1764. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1765. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4319,
  1766. "Vulkan spec doesn't allow BuiltIn Position to be used "
  1767. "for variables "
  1768. "with Input storage class if execution model is MeshNV.",
  1769. spv::ExecutionModel::MeshNV, decoration, built_in_inst,
  1770. referenced_from_inst, std::placeholders::_1));
  1771. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1772. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4319,
  1773. "Vulkan spec doesn't allow BuiltIn Position to be used "
  1774. "for variables "
  1775. "with Input storage class if execution model is MeshEXT.",
  1776. spv::ExecutionModel::MeshEXT, decoration, built_in_inst,
  1777. referenced_from_inst, std::placeholders::_1));
  1778. }
  1779. for (const spv::ExecutionModel execution_model : execution_models_) {
  1780. switch (execution_model) {
  1781. case spv::ExecutionModel::Vertex: {
  1782. if (spv_result_t error = ValidateF32Vec(
  1783. decoration, built_in_inst, 4,
  1784. [this, &referenced_from_inst](
  1785. const std::string& message) -> spv_result_t {
  1786. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1787. << _.VkErrorID(4321)
  1788. << "According to the Vulkan spec BuiltIn Position "
  1789. "variable needs to be a 4-component 32-bit float "
  1790. "vector. "
  1791. << message;
  1792. })) {
  1793. return error;
  1794. }
  1795. break;
  1796. }
  1797. case spv::ExecutionModel::Geometry:
  1798. case spv::ExecutionModel::TessellationControl:
  1799. case spv::ExecutionModel::TessellationEvaluation:
  1800. case spv::ExecutionModel::MeshNV:
  1801. case spv::ExecutionModel::MeshEXT: {
  1802. // Position can be a per-vertex variable for tessellation control,
  1803. // tessellation evaluation, geometry and mesh shader stages. In such
  1804. // cases variables will have an array of 4-component 32-bit float
  1805. // vectors.
  1806. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1807. // The array is on the variable, so this must be a 4-component
  1808. // 32-bit float vector.
  1809. if (spv_result_t error = ValidateF32Vec(
  1810. decoration, built_in_inst, 4,
  1811. [this, &referenced_from_inst](
  1812. const std::string& message) -> spv_result_t {
  1813. return _.diag(SPV_ERROR_INVALID_DATA,
  1814. &referenced_from_inst)
  1815. << _.VkErrorID(4321)
  1816. << "According to the Vulkan spec BuiltIn Position "
  1817. "variable needs to be a 4-component 32-bit "
  1818. "float vector. "
  1819. << message;
  1820. })) {
  1821. return error;
  1822. }
  1823. } else {
  1824. if (spv_result_t error = ValidateOptionalArrayedF32Vec(
  1825. decoration, built_in_inst, 4,
  1826. [this, &referenced_from_inst](
  1827. const std::string& message) -> spv_result_t {
  1828. return _.diag(SPV_ERROR_INVALID_DATA,
  1829. &referenced_from_inst)
  1830. << _.VkErrorID(4321)
  1831. << "According to the Vulkan spec BuiltIn Position "
  1832. "variable needs to be a 4-component 32-bit "
  1833. "float vector. "
  1834. << message;
  1835. })) {
  1836. return error;
  1837. }
  1838. }
  1839. break;
  1840. }
  1841. default: {
  1842. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1843. << _.VkErrorID(4318)
  1844. << "Vulkan spec allows BuiltIn Position to be used only "
  1845. "with Vertex, TessellationControl, TessellationEvaluation"
  1846. " or Geometry execution models. "
  1847. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1848. referenced_from_inst, execution_model);
  1849. }
  1850. }
  1851. }
  1852. }
  1853. if (function_id_ == 0) {
  1854. // Propagate this rule to all dependant ids in the global scope.
  1855. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1856. &BuiltInsValidator::ValidatePositionAtReference, this, decoration,
  1857. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1858. }
  1859. return SPV_SUCCESS;
  1860. }
  1861. spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtDefinition(
  1862. const Decoration& decoration, const Instruction& inst) {
  1863. if (spvIsVulkanEnv(_.context()->target_env)) {
  1864. // PrimitiveId can be a per-primitive variable for mesh shader stage.
  1865. // In such cases variable will have an array of 32-bit integers.
  1866. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1867. // This must be a 32-bit int scalar.
  1868. if (spv_result_t error = ValidateI32(
  1869. decoration, inst,
  1870. [this, &inst](const std::string& message) -> spv_result_t {
  1871. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1872. << _.VkErrorID(4337)
  1873. << "According to the Vulkan spec BuiltIn PrimitiveId "
  1874. "variable needs to be a 32-bit int scalar. "
  1875. << message;
  1876. })) {
  1877. return error;
  1878. }
  1879. } else {
  1880. if (spv_result_t error = ValidateOptionalArrayedI32(
  1881. decoration, inst,
  1882. [this, &inst](const std::string& message) -> spv_result_t {
  1883. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1884. << _.VkErrorID(4337)
  1885. << "According to the Vulkan spec BuiltIn PrimitiveId "
  1886. "variable needs to be a 32-bit int scalar. "
  1887. << message;
  1888. })) {
  1889. return error;
  1890. }
  1891. }
  1892. }
  1893. // Seed at reference checks with this built-in.
  1894. return ValidatePrimitiveIdAtReference(decoration, inst, inst, inst);
  1895. }
  1896. spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtReference(
  1897. const Decoration& decoration, const Instruction& built_in_inst,
  1898. const Instruction& referenced_inst,
  1899. const Instruction& referenced_from_inst) {
  1900. if (spvIsVulkanEnv(_.context()->target_env)) {
  1901. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  1902. if (storage_class != spv::StorageClass::Max &&
  1903. storage_class != spv::StorageClass::Input &&
  1904. storage_class != spv::StorageClass::Output) {
  1905. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1906. << "Vulkan spec allows BuiltIn PrimitiveId to be only used for "
  1907. "variables with Input or Output storage class. "
  1908. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1909. referenced_from_inst)
  1910. << " " << GetStorageClassDesc(referenced_from_inst);
  1911. }
  1912. if (storage_class == spv::StorageClass::Output) {
  1913. assert(function_id_ == 0);
  1914. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1915. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1916. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1917. "variables with Output storage class if execution model is "
  1918. "TessellationControl.",
  1919. spv::ExecutionModel::TessellationControl, decoration, built_in_inst,
  1920. referenced_from_inst, std::placeholders::_1));
  1921. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1922. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1923. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1924. "variables with Output storage class if execution model is "
  1925. "TessellationEvaluation.",
  1926. spv::ExecutionModel::TessellationEvaluation, decoration, built_in_inst,
  1927. referenced_from_inst, std::placeholders::_1));
  1928. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1929. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1930. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1931. "variables with Output storage class if execution model is "
  1932. "Fragment.",
  1933. spv::ExecutionModel::Fragment, decoration, built_in_inst,
  1934. referenced_from_inst, std::placeholders::_1));
  1935. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1936. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1937. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1938. "variables with Output storage class if execution model is "
  1939. "IntersectionKHR.",
  1940. spv::ExecutionModel::IntersectionKHR, decoration, built_in_inst,
  1941. referenced_from_inst, std::placeholders::_1));
  1942. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1943. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1944. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1945. "variables with Output storage class if execution model is "
  1946. "AnyHitKHR.",
  1947. spv::ExecutionModel::AnyHitKHR, decoration, built_in_inst,
  1948. referenced_from_inst, std::placeholders::_1));
  1949. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1950. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, 4334,
  1951. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1952. "variables with Output storage class if execution model is "
  1953. "ClosestHitKHR.",
  1954. spv::ExecutionModel::ClosestHitKHR, decoration, built_in_inst,
  1955. referenced_from_inst, std::placeholders::_1));
  1956. }
  1957. for (const spv::ExecutionModel execution_model : execution_models_) {
  1958. switch (execution_model) {
  1959. case spv::ExecutionModel::Fragment:
  1960. case spv::ExecutionModel::TessellationControl:
  1961. case spv::ExecutionModel::TessellationEvaluation:
  1962. case spv::ExecutionModel::Geometry:
  1963. case spv::ExecutionModel::MeshNV:
  1964. case spv::ExecutionModel::MeshEXT:
  1965. case spv::ExecutionModel::IntersectionKHR:
  1966. case spv::ExecutionModel::AnyHitKHR:
  1967. case spv::ExecutionModel::ClosestHitKHR: {
  1968. // Ok.
  1969. break;
  1970. }
  1971. default: {
  1972. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1973. << _.VkErrorID(4330)
  1974. << "Vulkan spec allows BuiltIn PrimitiveId to be used only "
  1975. "with Fragment, TessellationControl, "
  1976. "TessellationEvaluation, Geometry, MeshNV, MeshEXT, "
  1977. "IntersectionKHR, AnyHitKHR, and ClosestHitKHR execution models. "
  1978. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1979. referenced_from_inst, execution_model);
  1980. }
  1981. }
  1982. }
  1983. }
  1984. if (function_id_ == 0) {
  1985. // Propagate this rule to all dependant ids in the global scope.
  1986. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1987. &BuiltInsValidator::ValidatePrimitiveIdAtReference, this, decoration,
  1988. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1989. }
  1990. return SPV_SUCCESS;
  1991. }
  1992. spv_result_t BuiltInsValidator::ValidateSampleIdAtDefinition(
  1993. const Decoration& decoration, const Instruction& inst) {
  1994. if (spvIsVulkanEnv(_.context()->target_env)) {
  1995. if (spv_result_t error = ValidateI32(
  1996. decoration, inst,
  1997. [this, &inst](const std::string& message) -> spv_result_t {
  1998. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1999. << _.VkErrorID(4356)
  2000. << "According to the Vulkan spec BuiltIn SampleId "
  2001. "variable needs to be a 32-bit int scalar. "
  2002. << message;
  2003. })) {
  2004. return error;
  2005. }
  2006. }
  2007. // Seed at reference checks with this built-in.
  2008. return ValidateSampleIdAtReference(decoration, inst, inst, inst);
  2009. }
  2010. spv_result_t BuiltInsValidator::ValidateSampleIdAtReference(
  2011. const Decoration& decoration, const Instruction& built_in_inst,
  2012. const Instruction& referenced_inst,
  2013. const Instruction& referenced_from_inst) {
  2014. if (spvIsVulkanEnv(_.context()->target_env)) {
  2015. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2016. if (storage_class != spv::StorageClass::Max &&
  2017. storage_class != spv::StorageClass::Input) {
  2018. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2019. << _.VkErrorID(4355)
  2020. << "Vulkan spec allows BuiltIn SampleId to be only used for "
  2021. "variables with Input storage class. "
  2022. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2023. referenced_from_inst)
  2024. << " " << GetStorageClassDesc(referenced_from_inst);
  2025. }
  2026. for (const spv::ExecutionModel execution_model : execution_models_) {
  2027. if (execution_model != spv::ExecutionModel::Fragment) {
  2028. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2029. << _.VkErrorID(4354)
  2030. << "Vulkan spec allows BuiltIn SampleId to be used only with "
  2031. "Fragment execution model. "
  2032. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2033. referenced_from_inst, execution_model);
  2034. }
  2035. }
  2036. }
  2037. if (function_id_ == 0) {
  2038. // Propagate this rule to all dependant ids in the global scope.
  2039. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2040. &BuiltInsValidator::ValidateSampleIdAtReference, this, decoration,
  2041. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2042. }
  2043. return SPV_SUCCESS;
  2044. }
  2045. spv_result_t BuiltInsValidator::ValidateSampleMaskAtDefinition(
  2046. const Decoration& decoration, const Instruction& inst) {
  2047. if (spvIsVulkanEnv(_.context()->target_env)) {
  2048. if (spv_result_t error = ValidateI32Arr(
  2049. decoration, inst,
  2050. [this, &inst](const std::string& message) -> spv_result_t {
  2051. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2052. << _.VkErrorID(4359)
  2053. << "According to the Vulkan spec BuiltIn SampleMask "
  2054. "variable needs to be a 32-bit int array. "
  2055. << message;
  2056. })) {
  2057. return error;
  2058. }
  2059. }
  2060. // Seed at reference checks with this built-in.
  2061. return ValidateSampleMaskAtReference(decoration, inst, inst, inst);
  2062. }
  2063. spv_result_t BuiltInsValidator::ValidateSampleMaskAtReference(
  2064. const Decoration& decoration, const Instruction& built_in_inst,
  2065. const Instruction& referenced_inst,
  2066. const Instruction& referenced_from_inst) {
  2067. if (spvIsVulkanEnv(_.context()->target_env)) {
  2068. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2069. if (storage_class != spv::StorageClass::Max &&
  2070. storage_class != spv::StorageClass::Input &&
  2071. storage_class != spv::StorageClass::Output) {
  2072. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2073. << _.VkErrorID(4358)
  2074. << "Vulkan spec allows BuiltIn SampleMask to be only used for "
  2075. "variables with Input or Output storage class. "
  2076. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2077. referenced_from_inst)
  2078. << " " << GetStorageClassDesc(referenced_from_inst);
  2079. }
  2080. for (const spv::ExecutionModel execution_model : execution_models_) {
  2081. if (execution_model != spv::ExecutionModel::Fragment) {
  2082. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2083. << _.VkErrorID(4357)
  2084. << "Vulkan spec allows BuiltIn SampleMask to be used only "
  2085. "with "
  2086. "Fragment execution model. "
  2087. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2088. referenced_from_inst, execution_model);
  2089. }
  2090. }
  2091. }
  2092. if (function_id_ == 0) {
  2093. // Propagate this rule to all dependant ids in the global scope.
  2094. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2095. &BuiltInsValidator::ValidateSampleMaskAtReference, this, decoration,
  2096. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2097. }
  2098. return SPV_SUCCESS;
  2099. }
  2100. spv_result_t BuiltInsValidator::ValidateSamplePositionAtDefinition(
  2101. const Decoration& decoration, const Instruction& inst) {
  2102. if (spvIsVulkanEnv(_.context()->target_env)) {
  2103. if (spv_result_t error = ValidateF32Vec(
  2104. decoration, inst, 2,
  2105. [this, &inst](const std::string& message) -> spv_result_t {
  2106. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2107. << _.VkErrorID(4362)
  2108. << "According to the Vulkan spec BuiltIn SamplePosition "
  2109. "variable needs to be a 2-component 32-bit float "
  2110. "vector. "
  2111. << message;
  2112. })) {
  2113. return error;
  2114. }
  2115. }
  2116. // Seed at reference checks with this built-in.
  2117. return ValidateSamplePositionAtReference(decoration, inst, inst, inst);
  2118. }
  2119. spv_result_t BuiltInsValidator::ValidateSamplePositionAtReference(
  2120. const Decoration& decoration, const Instruction& built_in_inst,
  2121. const Instruction& referenced_inst,
  2122. const Instruction& referenced_from_inst) {
  2123. if (spvIsVulkanEnv(_.context()->target_env)) {
  2124. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2125. if (storage_class != spv::StorageClass::Max &&
  2126. storage_class != spv::StorageClass::Input) {
  2127. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2128. << _.VkErrorID(4361)
  2129. << "Vulkan spec allows BuiltIn SamplePosition to be only used "
  2130. "for "
  2131. "variables with Input storage class. "
  2132. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2133. referenced_from_inst)
  2134. << " " << GetStorageClassDesc(referenced_from_inst);
  2135. }
  2136. for (const spv::ExecutionModel execution_model : execution_models_) {
  2137. if (execution_model != spv::ExecutionModel::Fragment) {
  2138. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2139. << _.VkErrorID(4360)
  2140. << "Vulkan spec allows BuiltIn SamplePosition to be used only "
  2141. "with "
  2142. "Fragment execution model. "
  2143. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2144. referenced_from_inst, execution_model);
  2145. }
  2146. }
  2147. }
  2148. if (function_id_ == 0) {
  2149. // Propagate this rule to all dependant ids in the global scope.
  2150. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2151. &BuiltInsValidator::ValidateSamplePositionAtReference, this, decoration,
  2152. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2153. }
  2154. return SPV_SUCCESS;
  2155. }
  2156. spv_result_t BuiltInsValidator::ValidateTessCoordAtDefinition(
  2157. const Decoration& decoration, const Instruction& inst) {
  2158. if (spvIsVulkanEnv(_.context()->target_env)) {
  2159. if (spv_result_t error = ValidateF32Vec(
  2160. decoration, inst, 3,
  2161. [this, &inst](const std::string& message) -> spv_result_t {
  2162. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2163. << _.VkErrorID(4389)
  2164. << "According to the Vulkan spec BuiltIn TessCoord "
  2165. "variable needs to be a 3-component 32-bit float "
  2166. "vector. "
  2167. << message;
  2168. })) {
  2169. return error;
  2170. }
  2171. }
  2172. // Seed at reference checks with this built-in.
  2173. return ValidateTessCoordAtReference(decoration, inst, inst, inst);
  2174. }
  2175. spv_result_t BuiltInsValidator::ValidateTessCoordAtReference(
  2176. const Decoration& decoration, const Instruction& built_in_inst,
  2177. const Instruction& referenced_inst,
  2178. const Instruction& referenced_from_inst) {
  2179. if (spvIsVulkanEnv(_.context()->target_env)) {
  2180. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2181. if (storage_class != spv::StorageClass::Max &&
  2182. storage_class != spv::StorageClass::Input) {
  2183. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2184. << _.VkErrorID(4388)
  2185. << "Vulkan spec allows BuiltIn TessCoord to be only used for "
  2186. "variables with Input storage class. "
  2187. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2188. referenced_from_inst)
  2189. << " " << GetStorageClassDesc(referenced_from_inst);
  2190. }
  2191. for (const spv::ExecutionModel execution_model : execution_models_) {
  2192. if (execution_model != spv::ExecutionModel::TessellationEvaluation) {
  2193. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2194. << _.VkErrorID(4387)
  2195. << "Vulkan spec allows BuiltIn TessCoord to be used only with "
  2196. "TessellationEvaluation execution model. "
  2197. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2198. referenced_from_inst, execution_model);
  2199. }
  2200. }
  2201. }
  2202. if (function_id_ == 0) {
  2203. // Propagate this rule to all dependant ids in the global scope.
  2204. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2205. &BuiltInsValidator::ValidateTessCoordAtReference, this, decoration,
  2206. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2207. }
  2208. return SPV_SUCCESS;
  2209. }
  2210. spv_result_t BuiltInsValidator::ValidateTessLevelOuterAtDefinition(
  2211. const Decoration& decoration, const Instruction& inst) {
  2212. if (spvIsVulkanEnv(_.context()->target_env)) {
  2213. if (spv_result_t error = ValidateF32Arr(
  2214. decoration, inst, 4,
  2215. [this, &inst](const std::string& message) -> spv_result_t {
  2216. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2217. << _.VkErrorID(4393)
  2218. << "According to the Vulkan spec BuiltIn TessLevelOuter "
  2219. "variable needs to be a 4-component 32-bit float "
  2220. "array. "
  2221. << message;
  2222. })) {
  2223. return error;
  2224. }
  2225. }
  2226. // Seed at reference checks with this built-in.
  2227. return ValidateTessLevelAtReference(decoration, inst, inst, inst);
  2228. }
  2229. spv_result_t BuiltInsValidator::ValidateTessLevelInnerAtDefinition(
  2230. const Decoration& decoration, const Instruction& inst) {
  2231. if (spvIsVulkanEnv(_.context()->target_env)) {
  2232. if (spv_result_t error = ValidateF32Arr(
  2233. decoration, inst, 2,
  2234. [this, &inst](const std::string& message) -> spv_result_t {
  2235. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2236. << _.VkErrorID(4397)
  2237. << "According to the Vulkan spec BuiltIn TessLevelOuter "
  2238. "variable needs to be a 2-component 32-bit float "
  2239. "array. "
  2240. << message;
  2241. })) {
  2242. return error;
  2243. }
  2244. }
  2245. // Seed at reference checks with this built-in.
  2246. return ValidateTessLevelAtReference(decoration, inst, inst, inst);
  2247. }
  2248. spv_result_t BuiltInsValidator::ValidateTessLevelAtReference(
  2249. const Decoration& decoration, const Instruction& built_in_inst,
  2250. const Instruction& referenced_inst,
  2251. const Instruction& referenced_from_inst) {
  2252. uint32_t operand = decoration.params()[0];
  2253. if (spvIsVulkanEnv(_.context()->target_env)) {
  2254. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2255. if (storage_class != spv::StorageClass::Max &&
  2256. storage_class != spv::StorageClass::Input &&
  2257. storage_class != spv::StorageClass::Output) {
  2258. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2259. << "Vulkan spec allows BuiltIn "
  2260. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2261. operand)
  2262. << " to be only used for variables with Input or Output storage "
  2263. "class. "
  2264. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2265. referenced_from_inst)
  2266. << " " << GetStorageClassDesc(referenced_from_inst);
  2267. }
  2268. if (storage_class == spv::StorageClass::Input) {
  2269. assert(function_id_ == 0);
  2270. uint32_t vuid = (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::TessLevelOuter) ? 4391 : 4395;
  2271. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2272. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  2273. "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
  2274. "used "
  2275. "for variables with Input storage class if execution model is "
  2276. "TessellationControl.",
  2277. spv::ExecutionModel::TessellationControl, decoration, built_in_inst,
  2278. referenced_from_inst, std::placeholders::_1));
  2279. }
  2280. if (storage_class == spv::StorageClass::Output) {
  2281. assert(function_id_ == 0);
  2282. uint32_t vuid = (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::TessLevelOuter) ? 4392 : 4396;
  2283. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2284. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this, vuid,
  2285. "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
  2286. "used "
  2287. "for variables with Output storage class if execution model is "
  2288. "TessellationEvaluation.",
  2289. spv::ExecutionModel::TessellationEvaluation, decoration, built_in_inst,
  2290. referenced_from_inst, std::placeholders::_1));
  2291. }
  2292. for (const spv::ExecutionModel execution_model : execution_models_) {
  2293. switch (execution_model) {
  2294. case spv::ExecutionModel::TessellationControl:
  2295. case spv::ExecutionModel::TessellationEvaluation: {
  2296. // Ok.
  2297. break;
  2298. }
  2299. default: {
  2300. uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::TessLevelOuter) ? 4390 : 4394;
  2301. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2302. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2303. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2304. operand)
  2305. << " to be used only with TessellationControl or "
  2306. "TessellationEvaluation execution models. "
  2307. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2308. referenced_from_inst, execution_model);
  2309. }
  2310. }
  2311. }
  2312. }
  2313. if (function_id_ == 0) {
  2314. // Propagate this rule to all dependant ids in the global scope.
  2315. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2316. &BuiltInsValidator::ValidateTessLevelAtReference, this, decoration,
  2317. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2318. }
  2319. return SPV_SUCCESS;
  2320. }
  2321. spv_result_t BuiltInsValidator::ValidateVertexIndexAtDefinition(
  2322. const Decoration& decoration, const Instruction& inst) {
  2323. if (spvIsVulkanEnv(_.context()->target_env)) {
  2324. if (spv_result_t error = ValidateI32(
  2325. decoration, inst,
  2326. [this, &inst](const std::string& message) -> spv_result_t {
  2327. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2328. << _.VkErrorID(4400) << "According to the "
  2329. << spvLogStringForEnv(_.context()->target_env)
  2330. << " spec BuiltIn VertexIndex variable needs to be a "
  2331. "32-bit int scalar. "
  2332. << message;
  2333. })) {
  2334. return error;
  2335. }
  2336. }
  2337. // Seed at reference checks with this built-in.
  2338. return ValidateVertexIndexAtReference(decoration, inst, inst, inst);
  2339. }
  2340. spv_result_t BuiltInsValidator::ValidateVertexIdAtDefinition(
  2341. const Decoration& decoration, const Instruction& inst) {
  2342. (void)decoration;
  2343. if (spvIsVulkanEnv(_.context()->target_env)) {
  2344. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2345. << "Vulkan spec doesn't allow BuiltIn VertexId "
  2346. "to be used.";
  2347. }
  2348. return SPV_SUCCESS;
  2349. }
  2350. spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtDefinition(
  2351. const Decoration& decoration, const Instruction& inst) {
  2352. // Seed at reference checks with this built-in.
  2353. return ValidateLocalInvocationIndexAtReference(decoration, inst, inst, inst);
  2354. }
  2355. spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtReference(
  2356. const Decoration& decoration, const Instruction& built_in_inst,
  2357. const Instruction&,
  2358. const Instruction& referenced_from_inst) {
  2359. if (function_id_ == 0) {
  2360. // Propagate this rule to all dependant ids in the global scope.
  2361. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2362. std::bind(&BuiltInsValidator::ValidateLocalInvocationIndexAtReference,
  2363. this, decoration, built_in_inst, referenced_from_inst,
  2364. std::placeholders::_1));
  2365. }
  2366. return SPV_SUCCESS;
  2367. }
  2368. spv_result_t BuiltInsValidator::ValidateVertexIndexAtReference(
  2369. const Decoration& decoration, const Instruction& built_in_inst,
  2370. const Instruction& referenced_inst,
  2371. const Instruction& referenced_from_inst) {
  2372. if (spvIsVulkanEnv(_.context()->target_env)) {
  2373. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2374. if (storage_class != spv::StorageClass::Max &&
  2375. storage_class != spv::StorageClass::Input) {
  2376. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2377. << _.VkErrorID(4399) << spvLogStringForEnv(_.context()->target_env)
  2378. << " spec allows BuiltIn VertexIndex to be only used for "
  2379. "variables with Input storage class. "
  2380. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2381. referenced_from_inst)
  2382. << " " << GetStorageClassDesc(referenced_from_inst);
  2383. }
  2384. for (const spv::ExecutionModel execution_model : execution_models_) {
  2385. if (execution_model != spv::ExecutionModel::Vertex) {
  2386. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2387. << _.VkErrorID(4398)
  2388. << spvLogStringForEnv(_.context()->target_env)
  2389. << " spec allows BuiltIn VertexIndex to be used only with "
  2390. "Vertex execution model. "
  2391. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2392. referenced_from_inst, execution_model);
  2393. }
  2394. }
  2395. }
  2396. if (function_id_ == 0) {
  2397. // Propagate this rule to all dependant ids in the global scope.
  2398. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2399. &BuiltInsValidator::ValidateVertexIndexAtReference, this, decoration,
  2400. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2401. }
  2402. return SPV_SUCCESS;
  2403. }
  2404. spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtDefinition(
  2405. const Decoration& decoration, const Instruction& inst) {
  2406. if (spvIsVulkanEnv(_.context()->target_env)) {
  2407. // This can be a per-primitive variable for mesh shader stage.
  2408. // In such cases variable will have an array of 32-bit integers.
  2409. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2410. // This must be a 32-bit int scalar.
  2411. if (spv_result_t error = ValidateI32(
  2412. decoration, inst,
  2413. [this, &decoration,
  2414. &inst](const std::string& message) -> spv_result_t {
  2415. uint32_t vuid =
  2416. (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::Layer) ? 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. } else {
  2428. if (spv_result_t error = ValidateOptionalArrayedI32(
  2429. decoration, inst,
  2430. [this, &decoration,
  2431. &inst](const std::string& message) -> spv_result_t {
  2432. uint32_t vuid =
  2433. (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::Layer) ? 4276 : 4408;
  2434. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2435. << _.VkErrorID(vuid)
  2436. << "According to the Vulkan spec BuiltIn "
  2437. << _.grammar().lookupOperandName(
  2438. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
  2439. << "variable needs to be a 32-bit int scalar. "
  2440. << message;
  2441. })) {
  2442. return error;
  2443. }
  2444. }
  2445. }
  2446. // Seed at reference checks with this built-in.
  2447. return ValidateLayerOrViewportIndexAtReference(decoration, inst, inst, inst);
  2448. }
  2449. spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtReference(
  2450. const Decoration& decoration, const Instruction& built_in_inst,
  2451. const Instruction& referenced_inst,
  2452. const Instruction& referenced_from_inst) {
  2453. uint32_t operand = decoration.params()[0];
  2454. if (spvIsVulkanEnv(_.context()->target_env)) {
  2455. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2456. if (storage_class != spv::StorageClass::Max &&
  2457. storage_class != spv::StorageClass::Input &&
  2458. storage_class != spv::StorageClass::Output) {
  2459. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2460. << "Vulkan spec allows BuiltIn "
  2461. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2462. operand)
  2463. << " to be only used for variables with Input or Output storage "
  2464. "class. "
  2465. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2466. referenced_from_inst)
  2467. << " " << GetStorageClassDesc(referenced_from_inst);
  2468. }
  2469. if (storage_class == spv::StorageClass::Input) {
  2470. assert(function_id_ == 0);
  2471. for (const auto em :
  2472. {spv::ExecutionModel::Vertex, spv::ExecutionModel::TessellationEvaluation,
  2473. spv::ExecutionModel::Geometry, spv::ExecutionModel::MeshNV,
  2474. spv::ExecutionModel::MeshEXT}) {
  2475. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2476. std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel,
  2477. this, ((spv::BuiltIn(operand) == spv::BuiltIn::Layer) ? 4274 : 4406),
  2478. "Vulkan spec doesn't allow BuiltIn Layer and "
  2479. "ViewportIndex to be "
  2480. "used for variables with Input storage class if "
  2481. "execution model is Vertex, TessellationEvaluation, "
  2482. "Geometry, MeshNV or MeshEXT.",
  2483. em, decoration, built_in_inst, referenced_from_inst,
  2484. std::placeholders::_1));
  2485. }
  2486. }
  2487. if (storage_class == spv::StorageClass::Output) {
  2488. assert(function_id_ == 0);
  2489. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2490. std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel,
  2491. this, ((spv::BuiltIn(operand) == spv::BuiltIn::Layer) ? 4275 : 4407),
  2492. "Vulkan spec doesn't allow BuiltIn Layer and "
  2493. "ViewportIndex to be "
  2494. "used for variables with Output storage class if "
  2495. "execution model is "
  2496. "Fragment.",
  2497. spv::ExecutionModel::Fragment, decoration, built_in_inst,
  2498. referenced_from_inst, std::placeholders::_1));
  2499. }
  2500. for (const spv::ExecutionModel execution_model : execution_models_) {
  2501. switch (execution_model) {
  2502. case spv::ExecutionModel::Geometry:
  2503. case spv::ExecutionModel::Fragment:
  2504. case spv::ExecutionModel::MeshNV:
  2505. case spv::ExecutionModel::MeshEXT:
  2506. // Ok.
  2507. break;
  2508. case spv::ExecutionModel::Vertex:
  2509. case spv::ExecutionModel::TessellationEvaluation: {
  2510. if (!_.HasCapability(spv::Capability::ShaderViewportIndexLayerEXT)) {
  2511. if (spv::BuiltIn(operand) == spv::BuiltIn::ViewportIndex &&
  2512. _.HasCapability(spv::Capability::ShaderViewportIndex))
  2513. break; // Ok
  2514. if (spv::BuiltIn(operand) == spv::BuiltIn::Layer &&
  2515. _.HasCapability(spv::Capability::ShaderLayer))
  2516. break; // Ok
  2517. const char* capability = "ShaderViewportIndexLayerEXT";
  2518. if (spv::BuiltIn(operand) == spv::BuiltIn::ViewportIndex)
  2519. capability = "ShaderViewportIndexLayerEXT or ShaderViewportIndex";
  2520. if (spv::BuiltIn(operand) == spv::BuiltIn::Layer)
  2521. capability = "ShaderViewportIndexLayerEXT or ShaderLayer";
  2522. uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::Layer) ? 4273 : 4405;
  2523. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2524. << _.VkErrorID(vuid) << "Using BuiltIn "
  2525. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2526. operand)
  2527. << " in Vertex or Tessellation execution model requires the "
  2528. << capability << " capability.";
  2529. }
  2530. break;
  2531. }
  2532. default: {
  2533. uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::Layer) ? 4272 : 4404;
  2534. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2535. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2536. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2537. operand)
  2538. << " to be used only with Vertex, TessellationEvaluation, "
  2539. "Geometry, or Fragment execution models. "
  2540. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2541. referenced_from_inst, execution_model);
  2542. }
  2543. }
  2544. }
  2545. }
  2546. if (function_id_ == 0) {
  2547. // Propagate this rule to all dependant ids in the global scope.
  2548. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2549. std::bind(&BuiltInsValidator::ValidateLayerOrViewportIndexAtReference,
  2550. this, decoration, built_in_inst, referenced_from_inst,
  2551. std::placeholders::_1));
  2552. }
  2553. return SPV_SUCCESS;
  2554. }
  2555. spv_result_t BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtDefinition(
  2556. const Decoration& decoration, const Instruction& inst) {
  2557. if (spvIsVulkanEnv(_.context()->target_env)) {
  2558. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  2559. if (spv_result_t error = ValidateF32Vec(
  2560. decoration, inst, 3,
  2561. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2562. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2563. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2564. << _.VkErrorID(vuid) << "According to the "
  2565. << spvLogStringForEnv(_.context()->target_env)
  2566. << " spec BuiltIn "
  2567. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2568. uint32_t(builtin))
  2569. << " variable needs to be a 3-component 32-bit float "
  2570. "vector. "
  2571. << message;
  2572. })) {
  2573. return error;
  2574. }
  2575. }
  2576. // Seed at reference checks with this built-in.
  2577. return ValidateFragmentShaderF32Vec3InputAtReference(decoration, inst, inst,
  2578. inst);
  2579. }
  2580. spv_result_t BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtReference(
  2581. const Decoration& decoration, const Instruction& built_in_inst,
  2582. const Instruction& referenced_inst,
  2583. const Instruction& referenced_from_inst) {
  2584. if (spvIsVulkanEnv(_.context()->target_env)) {
  2585. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  2586. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2587. if (storage_class != spv::StorageClass::Max &&
  2588. storage_class != spv::StorageClass::Input) {
  2589. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2590. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2591. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  2592. << " spec allows BuiltIn "
  2593. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2594. << " to be only used for variables with Input storage class. "
  2595. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2596. referenced_from_inst)
  2597. << " " << GetStorageClassDesc(referenced_from_inst);
  2598. }
  2599. for (const spv::ExecutionModel execution_model : execution_models_) {
  2600. if (execution_model != spv::ExecutionModel::Fragment) {
  2601. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  2602. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2603. << _.VkErrorID(vuid)
  2604. << spvLogStringForEnv(_.context()->target_env)
  2605. << " spec allows BuiltIn "
  2606. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2607. << " to be used only with Fragment execution model. "
  2608. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2609. referenced_from_inst, execution_model);
  2610. }
  2611. }
  2612. }
  2613. if (function_id_ == 0) {
  2614. // Propagate this rule to all dependant ids in the global scope.
  2615. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2616. &BuiltInsValidator::ValidateFragmentShaderF32Vec3InputAtReference, this,
  2617. decoration, built_in_inst, referenced_from_inst,
  2618. std::placeholders::_1));
  2619. }
  2620. return SPV_SUCCESS;
  2621. }
  2622. spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtDefinition(
  2623. const Decoration& decoration, const Instruction& inst) {
  2624. if (spvIsVulkanEnv(_.context()->target_env)) {
  2625. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  2626. if (spv_result_t error = ValidateI32Vec(
  2627. decoration, inst, 3,
  2628. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2629. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2630. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2631. << _.VkErrorID(vuid) << "According to the "
  2632. << spvLogStringForEnv(_.context()->target_env)
  2633. << " spec BuiltIn "
  2634. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2635. uint32_t(builtin))
  2636. << " variable needs to be a 3-component 32-bit int "
  2637. "vector. "
  2638. << message;
  2639. })) {
  2640. return error;
  2641. }
  2642. }
  2643. // Seed at reference checks with this built-in.
  2644. return ValidateComputeShaderI32Vec3InputAtReference(decoration, inst, inst,
  2645. inst);
  2646. }
  2647. spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference(
  2648. const Decoration& decoration, const Instruction& built_in_inst,
  2649. const Instruction& referenced_inst,
  2650. const Instruction& referenced_from_inst) {
  2651. if (spvIsVulkanEnv(_.context()->target_env)) {
  2652. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  2653. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2654. if (storage_class != spv::StorageClass::Max &&
  2655. storage_class != spv::StorageClass::Input) {
  2656. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2657. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2658. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  2659. << " spec allows BuiltIn "
  2660. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2661. << " to be only used for variables with Input storage class. "
  2662. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2663. referenced_from_inst)
  2664. << " " << GetStorageClassDesc(referenced_from_inst);
  2665. }
  2666. for (const spv::ExecutionModel execution_model : execution_models_) {
  2667. bool has_vulkan_model = execution_model == spv::ExecutionModel::GLCompute ||
  2668. execution_model == spv::ExecutionModel::TaskNV ||
  2669. execution_model == spv::ExecutionModel::MeshNV ||
  2670. execution_model == spv::ExecutionModel::TaskEXT ||
  2671. execution_model == spv::ExecutionModel::MeshEXT;
  2672. if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
  2673. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  2674. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2675. << _.VkErrorID(vuid)
  2676. << spvLogStringForEnv(_.context()->target_env)
  2677. << " spec allows BuiltIn "
  2678. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2679. << " to be used only with GLCompute, MeshNV, TaskNV, MeshEXT or"
  2680. << " TaskEXT execution model. "
  2681. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2682. referenced_from_inst, execution_model);
  2683. }
  2684. }
  2685. }
  2686. if (function_id_ == 0) {
  2687. // Propagate this rule to all dependant ids in the global scope.
  2688. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2689. &BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference, this,
  2690. decoration, built_in_inst, referenced_from_inst,
  2691. std::placeholders::_1));
  2692. }
  2693. return SPV_SUCCESS;
  2694. }
  2695. spv_result_t BuiltInsValidator::ValidateComputeI32InputAtDefinition(
  2696. const Decoration& decoration, const Instruction& inst) {
  2697. if (spvIsVulkanEnv(_.context()->target_env)) {
  2698. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  2699. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2700. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2701. << "BuiltIn "
  2702. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2703. << " cannot be used as a member decoration ";
  2704. }
  2705. if (spv_result_t error = ValidateI32(
  2706. decoration, inst,
  2707. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2708. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2709. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2710. << _.VkErrorID(vuid)
  2711. << "According to the "
  2712. << spvLogStringForEnv(_.context()->target_env)
  2713. << " spec BuiltIn "
  2714. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2715. << " variable needs to be a 32-bit int "
  2716. "vector. "
  2717. << message;
  2718. })) {
  2719. return error;
  2720. }
  2721. }
  2722. // Seed at reference checks with this built-in.
  2723. return ValidateComputeI32InputAtReference(decoration, inst, inst, inst);
  2724. }
  2725. spv_result_t BuiltInsValidator::ValidateComputeI32InputAtReference(
  2726. const Decoration& decoration, const Instruction& built_in_inst,
  2727. const Instruction& referenced_inst,
  2728. const Instruction& referenced_from_inst) {
  2729. if (spvIsVulkanEnv(_.context()->target_env)) {
  2730. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  2731. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2732. if (storage_class != spv::StorageClass::Max &&
  2733. storage_class != spv::StorageClass::Input) {
  2734. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2735. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2736. << _.VkErrorID(vuid)
  2737. << spvLogStringForEnv(_.context()->target_env)
  2738. << " spec allows BuiltIn "
  2739. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2740. << " to be only used for variables with Input storage class. "
  2741. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2742. referenced_from_inst)
  2743. << " " << GetStorageClassDesc(referenced_from_inst);
  2744. }
  2745. for (const spv::ExecutionModel execution_model : execution_models_) {
  2746. bool has_vulkan_model = execution_model == spv::ExecutionModel::GLCompute ||
  2747. execution_model == spv::ExecutionModel::TaskNV ||
  2748. execution_model == spv::ExecutionModel::MeshNV ||
  2749. execution_model == spv::ExecutionModel::TaskEXT ||
  2750. execution_model == spv::ExecutionModel::MeshEXT;
  2751. if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
  2752. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  2753. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2754. << _.VkErrorID(vuid)
  2755. << spvLogStringForEnv(_.context()->target_env)
  2756. << " spec allows BuiltIn "
  2757. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2758. << " to be used only with GLCompute, MeshNV, TaskNV, MeshEXT or "
  2759. << "TaskEXT execution model. "
  2760. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2761. referenced_from_inst, execution_model);
  2762. }
  2763. }
  2764. }
  2765. if (function_id_ == 0) {
  2766. // Propagate this rule to all dependant ids in the global scope.
  2767. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2768. std::bind(&BuiltInsValidator::ValidateComputeI32InputAtReference, this,
  2769. decoration, built_in_inst, referenced_from_inst,
  2770. std::placeholders::_1));
  2771. }
  2772. return SPV_SUCCESS;
  2773. }
  2774. spv_result_t BuiltInsValidator::ValidateI32InputAtDefinition(
  2775. const Decoration& decoration, const Instruction& inst) {
  2776. if (spvIsVulkanEnv(_.context()->target_env)) {
  2777. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  2778. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2779. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2780. << "BuiltIn "
  2781. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2782. << " cannot be used as a member decoration ";
  2783. }
  2784. if (spv_result_t error = ValidateI32(
  2785. decoration, inst,
  2786. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2787. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2788. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2789. << _.VkErrorID(vuid)
  2790. << "According to the "
  2791. << spvLogStringForEnv(_.context()->target_env)
  2792. << " spec BuiltIn "
  2793. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2794. << " variable needs to be a 32-bit int. " << message;
  2795. })) {
  2796. return error;
  2797. }
  2798. const spv::StorageClass storage_class = GetStorageClass(inst);
  2799. if (storage_class != spv::StorageClass::Max &&
  2800. storage_class != spv::StorageClass::Input) {
  2801. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2802. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2803. << _.VkErrorID(vuid)
  2804. << spvLogStringForEnv(_.context()->target_env)
  2805. << " spec allows BuiltIn "
  2806. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2807. << " to be only used for variables with Input storage class. "
  2808. << GetReferenceDesc(decoration, inst, inst, inst) << " "
  2809. << GetStorageClassDesc(inst);
  2810. }
  2811. }
  2812. return SPV_SUCCESS;
  2813. }
  2814. spv_result_t BuiltInsValidator::ValidateI32Vec4InputAtDefinition(
  2815. const Decoration& decoration, const Instruction& inst) {
  2816. if (spvIsVulkanEnv(_.context()->target_env)) {
  2817. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  2818. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2819. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2820. << "BuiltIn "
  2821. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2822. << " cannot be used as a member decoration ";
  2823. }
  2824. if (spv_result_t error = ValidateI32Vec(
  2825. decoration, inst, 4,
  2826. [this, &inst, builtin](const std::string& message) -> spv_result_t {
  2827. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  2828. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2829. << _.VkErrorID(vuid)
  2830. << "According to the "
  2831. << spvLogStringForEnv(_.context()->target_env)
  2832. << " spec BuiltIn "
  2833. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2834. << " variable needs to be a 4-component 32-bit int "
  2835. "vector. "
  2836. << message;
  2837. })) {
  2838. return error;
  2839. }
  2840. const spv::StorageClass storage_class = GetStorageClass(inst);
  2841. if (storage_class != spv::StorageClass::Max &&
  2842. storage_class != spv::StorageClass::Input) {
  2843. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  2844. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2845. << _.VkErrorID(vuid)
  2846. << spvLogStringForEnv(_.context()->target_env)
  2847. << " spec allows BuiltIn "
  2848. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  2849. << " to be only used for variables with Input storage class. "
  2850. << GetReferenceDesc(decoration, inst, inst, inst) << " "
  2851. << GetStorageClassDesc(inst);
  2852. }
  2853. }
  2854. return SPV_SUCCESS;
  2855. }
  2856. spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtDefinition(
  2857. const Decoration& decoration, const Instruction& inst) {
  2858. if (spvIsVulkanEnv(_.context()->target_env)) {
  2859. if (spvIsVulkanEnv(_.context()->target_env) &&
  2860. !spvOpcodeIsConstant(inst.opcode())) {
  2861. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2862. << _.VkErrorID(4426)
  2863. << "Vulkan spec requires BuiltIn WorkgroupSize to be a "
  2864. "constant. "
  2865. << GetIdDesc(inst) << " is not a constant.";
  2866. }
  2867. if (spv_result_t error = ValidateI32Vec(
  2868. decoration, inst, 3,
  2869. [this, &inst](const std::string& message) -> spv_result_t {
  2870. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2871. << _.VkErrorID(4427) << "According to the "
  2872. << spvLogStringForEnv(_.context()->target_env)
  2873. << " spec BuiltIn WorkgroupSize variable needs to be a "
  2874. "3-component 32-bit int vector. "
  2875. << message;
  2876. })) {
  2877. return error;
  2878. }
  2879. }
  2880. // Seed at reference checks with this built-in.
  2881. return ValidateWorkgroupSizeAtReference(decoration, inst, inst, inst);
  2882. }
  2883. spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtReference(
  2884. const Decoration& decoration, const Instruction& built_in_inst,
  2885. const Instruction& referenced_inst,
  2886. const Instruction& referenced_from_inst) {
  2887. if (spvIsVulkanEnv(_.context()->target_env)) {
  2888. for (const spv::ExecutionModel execution_model : execution_models_) {
  2889. if (execution_model != spv::ExecutionModel::GLCompute &&
  2890. execution_model != spv::ExecutionModel::TaskNV &&
  2891. execution_model != spv::ExecutionModel::MeshNV &&
  2892. execution_model != spv::ExecutionModel::TaskEXT &&
  2893. execution_model != spv::ExecutionModel::MeshEXT) {
  2894. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2895. << _.VkErrorID(4425)
  2896. << spvLogStringForEnv(_.context()->target_env)
  2897. << " spec allows BuiltIn "
  2898. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2899. decoration.params()[0])
  2900. << " to be used only with GLCompute, MeshNV, TaskNV, MeshEXT or "
  2901. << "TaskEXT execution model. "
  2902. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2903. referenced_from_inst, execution_model);
  2904. }
  2905. }
  2906. }
  2907. if (function_id_ == 0) {
  2908. // Propagate this rule to all dependant ids in the global scope.
  2909. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2910. &BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, decoration,
  2911. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2912. }
  2913. return SPV_SUCCESS;
  2914. }
  2915. spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtDefinition(
  2916. const Decoration& decoration, const Instruction& inst) {
  2917. if (spvIsVulkanEnv(_.context()->target_env)) {
  2918. if (spv_result_t error = ValidateI32(
  2919. decoration, inst,
  2920. [this, &inst,
  2921. &decoration](const std::string& message) -> spv_result_t {
  2922. uint32_t vuid = (spv::BuiltIn(decoration.params()[0]) == spv::BuiltIn::BaseInstance)
  2923. ? 4183
  2924. : 4186;
  2925. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2926. << _.VkErrorID(vuid)
  2927. << "According to the Vulkan spec BuiltIn "
  2928. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2929. decoration.params()[0])
  2930. << " variable needs to be a 32-bit int scalar. "
  2931. << message;
  2932. })) {
  2933. return error;
  2934. }
  2935. }
  2936. return ValidateBaseInstanceOrVertexAtReference(decoration, inst, inst, inst);
  2937. }
  2938. spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference(
  2939. const Decoration& decoration, const Instruction& built_in_inst,
  2940. const Instruction& referenced_inst,
  2941. const Instruction& referenced_from_inst) {
  2942. uint32_t operand = decoration.params()[0];
  2943. if (spvIsVulkanEnv(_.context()->target_env)) {
  2944. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  2945. if (storage_class != spv::StorageClass::Max &&
  2946. storage_class != spv::StorageClass::Input) {
  2947. uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::BaseInstance) ? 4182 : 4185;
  2948. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2949. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2950. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2951. operand)
  2952. << " to be only used for variables with Input storage class. "
  2953. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2954. referenced_from_inst)
  2955. << " " << GetStorageClassDesc(referenced_from_inst);
  2956. }
  2957. for (const spv::ExecutionModel execution_model : execution_models_) {
  2958. if (execution_model != spv::ExecutionModel::Vertex) {
  2959. uint32_t vuid = (spv::BuiltIn(operand) == spv::BuiltIn::BaseInstance) ? 4181 : 4184;
  2960. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2961. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2962. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2963. operand)
  2964. << " to be used only with Vertex execution model. "
  2965. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2966. referenced_from_inst, execution_model);
  2967. }
  2968. }
  2969. }
  2970. if (function_id_ == 0) {
  2971. // Propagate this rule to all dependant ids in the global scope.
  2972. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2973. std::bind(&BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference,
  2974. this, decoration, built_in_inst, referenced_from_inst,
  2975. std::placeholders::_1));
  2976. }
  2977. return SPV_SUCCESS;
  2978. }
  2979. spv_result_t BuiltInsValidator::ValidateDrawIndexAtDefinition(
  2980. const Decoration& decoration, const Instruction& inst) {
  2981. if (spvIsVulkanEnv(_.context()->target_env)) {
  2982. if (spv_result_t error = ValidateI32(
  2983. decoration, inst,
  2984. [this, &inst,
  2985. &decoration](const std::string& message) -> spv_result_t {
  2986. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2987. << _.VkErrorID(4209)
  2988. << "According to the Vulkan spec BuiltIn "
  2989. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2990. decoration.params()[0])
  2991. << " variable needs to be a 32-bit int scalar. "
  2992. << message;
  2993. })) {
  2994. return error;
  2995. }
  2996. }
  2997. return ValidateDrawIndexAtReference(decoration, inst, inst, inst);
  2998. }
  2999. spv_result_t BuiltInsValidator::ValidateDrawIndexAtReference(
  3000. const Decoration& decoration, const Instruction& built_in_inst,
  3001. const Instruction& referenced_inst,
  3002. const Instruction& referenced_from_inst) {
  3003. uint32_t operand = decoration.params()[0];
  3004. if (spvIsVulkanEnv(_.context()->target_env)) {
  3005. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3006. if (storage_class != spv::StorageClass::Max &&
  3007. storage_class != spv::StorageClass::Input) {
  3008. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3009. << _.VkErrorID(4208) << "Vulkan spec allows BuiltIn "
  3010. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3011. operand)
  3012. << " to be only used for variables with Input storage class. "
  3013. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3014. referenced_from_inst)
  3015. << " " << GetStorageClassDesc(referenced_from_inst);
  3016. }
  3017. for (const spv::ExecutionModel execution_model : execution_models_) {
  3018. if (execution_model != spv::ExecutionModel::Vertex &&
  3019. execution_model != spv::ExecutionModel::MeshNV &&
  3020. execution_model != spv::ExecutionModel::TaskNV &&
  3021. execution_model != spv::ExecutionModel::MeshEXT &&
  3022. execution_model != spv::ExecutionModel::TaskEXT) {
  3023. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3024. << _.VkErrorID(4207) << "Vulkan spec allows BuiltIn "
  3025. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3026. operand)
  3027. << " to be used only with Vertex, MeshNV, TaskNV , MeshEXT or"
  3028. << " TaskEXT execution "
  3029. "model. "
  3030. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3031. referenced_from_inst, execution_model);
  3032. }
  3033. }
  3034. }
  3035. if (function_id_ == 0) {
  3036. // Propagate this rule to all dependant ids in the global scope.
  3037. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3038. &BuiltInsValidator::ValidateDrawIndexAtReference, this, decoration,
  3039. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3040. }
  3041. return SPV_SUCCESS;
  3042. }
  3043. spv_result_t BuiltInsValidator::ValidateViewIndexAtDefinition(
  3044. const Decoration& decoration, const Instruction& inst) {
  3045. if (spvIsVulkanEnv(_.context()->target_env)) {
  3046. if (spv_result_t error = ValidateI32(
  3047. decoration, inst,
  3048. [this, &inst,
  3049. &decoration](const std::string& message) -> spv_result_t {
  3050. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3051. << _.VkErrorID(4403)
  3052. << "According to the Vulkan spec BuiltIn "
  3053. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3054. decoration.params()[0])
  3055. << " variable needs to be a 32-bit int scalar. "
  3056. << message;
  3057. })) {
  3058. return error;
  3059. }
  3060. }
  3061. return ValidateViewIndexAtReference(decoration, inst, inst, inst);
  3062. }
  3063. spv_result_t BuiltInsValidator::ValidateViewIndexAtReference(
  3064. const Decoration& decoration, const Instruction& built_in_inst,
  3065. const Instruction& referenced_inst,
  3066. const Instruction& referenced_from_inst) {
  3067. uint32_t operand = decoration.params()[0];
  3068. if (spvIsVulkanEnv(_.context()->target_env)) {
  3069. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3070. if (storage_class != spv::StorageClass::Max &&
  3071. storage_class != spv::StorageClass::Input) {
  3072. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3073. << _.VkErrorID(4402) << "Vulkan spec allows BuiltIn "
  3074. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3075. operand)
  3076. << " to be only used for variables with Input storage class. "
  3077. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3078. referenced_from_inst)
  3079. << " " << GetStorageClassDesc(referenced_from_inst);
  3080. }
  3081. for (const spv::ExecutionModel execution_model : execution_models_) {
  3082. if (execution_model == spv::ExecutionModel::GLCompute) {
  3083. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3084. << _.VkErrorID(4401) << "Vulkan spec allows BuiltIn "
  3085. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3086. operand)
  3087. << " to be not be used with GLCompute execution model. "
  3088. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3089. referenced_from_inst, execution_model);
  3090. }
  3091. }
  3092. }
  3093. if (function_id_ == 0) {
  3094. // Propagate this rule to all dependant ids in the global scope.
  3095. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3096. &BuiltInsValidator::ValidateViewIndexAtReference, this, decoration,
  3097. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3098. }
  3099. return SPV_SUCCESS;
  3100. }
  3101. spv_result_t BuiltInsValidator::ValidateDeviceIndexAtDefinition(
  3102. const Decoration& decoration, const Instruction& inst) {
  3103. if (spvIsVulkanEnv(_.context()->target_env)) {
  3104. if (spv_result_t error = ValidateI32(
  3105. decoration, inst,
  3106. [this, &inst,
  3107. &decoration](const std::string& message) -> spv_result_t {
  3108. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3109. << _.VkErrorID(4206)
  3110. << "According to the Vulkan spec BuiltIn "
  3111. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3112. decoration.params()[0])
  3113. << " variable needs to be a 32-bit int scalar. "
  3114. << message;
  3115. })) {
  3116. return error;
  3117. }
  3118. }
  3119. return ValidateDeviceIndexAtReference(decoration, inst, inst, inst);
  3120. }
  3121. spv_result_t BuiltInsValidator::ValidateDeviceIndexAtReference(
  3122. const Decoration& decoration, const Instruction& built_in_inst,
  3123. const Instruction& referenced_inst,
  3124. const Instruction& referenced_from_inst) {
  3125. uint32_t operand = decoration.params()[0];
  3126. if (spvIsVulkanEnv(_.context()->target_env)) {
  3127. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3128. if (storage_class != spv::StorageClass::Max &&
  3129. storage_class != spv::StorageClass::Input) {
  3130. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3131. << _.VkErrorID(4205) << "Vulkan spec allows BuiltIn "
  3132. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3133. operand)
  3134. << " to be only used for variables with Input storage class. "
  3135. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3136. referenced_from_inst)
  3137. << " " << GetStorageClassDesc(referenced_from_inst);
  3138. }
  3139. }
  3140. if (function_id_ == 0) {
  3141. // Propagate this rule to all dependant ids in the global scope.
  3142. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3143. &BuiltInsValidator::ValidateDeviceIndexAtReference, this, decoration,
  3144. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3145. }
  3146. return SPV_SUCCESS;
  3147. }
  3148. spv_result_t BuiltInsValidator::ValidateFragInvocationCountAtDefinition(const Decoration& decoration,
  3149. const Instruction& inst) {
  3150. if (spvIsVulkanEnv(_.context()->target_env)) {
  3151. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3152. if (spv_result_t error = ValidateI32(
  3153. decoration, inst,
  3154. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3155. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3156. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3157. << _.VkErrorID(vuid) << "According to the "
  3158. << spvLogStringForEnv(_.context()->target_env)
  3159. << " spec BuiltIn "
  3160. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3161. uint32_t(builtin))
  3162. << " variable needs to be a 32-bit int scalar. "
  3163. << message;
  3164. })) {
  3165. return error;
  3166. }
  3167. }
  3168. return ValidateFragInvocationCountAtReference(decoration, inst, inst, inst);
  3169. }
  3170. spv_result_t BuiltInsValidator::ValidateFragInvocationCountAtReference(
  3171. const Decoration& decoration, const Instruction& built_in_inst,
  3172. const Instruction& referenced_inst,
  3173. const Instruction& referenced_from_inst) {
  3174. if (spvIsVulkanEnv(_.context()->target_env)) {
  3175. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3176. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3177. if (storage_class != spv::StorageClass::Max &&
  3178. storage_class != spv::StorageClass::Input) {
  3179. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3180. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3181. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3182. << " spec allows BuiltIn "
  3183. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3184. << " to be only used for variables with Input storage class. "
  3185. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3186. referenced_from_inst)
  3187. << " " << GetStorageClassDesc(referenced_from_inst);
  3188. }
  3189. for (const spv::ExecutionModel execution_model : execution_models_) {
  3190. if (execution_model != spv::ExecutionModel::Fragment) {
  3191. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3192. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3193. << _.VkErrorID(vuid)
  3194. << spvLogStringForEnv(_.context()->target_env)
  3195. << " spec allows BuiltIn "
  3196. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3197. << " to be used only with Fragment execution model. "
  3198. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3199. referenced_from_inst, execution_model);
  3200. }
  3201. }
  3202. }
  3203. if (function_id_ == 0) {
  3204. // Propagate this rule to all dependant ids in the global scope.
  3205. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3206. &BuiltInsValidator::ValidateFragInvocationCountAtReference, this, decoration,
  3207. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3208. }
  3209. return SPV_SUCCESS;
  3210. }
  3211. spv_result_t BuiltInsValidator::ValidateFragSizeAtDefinition(const Decoration& decoration,
  3212. const Instruction& inst) {
  3213. if (spvIsVulkanEnv(_.context()->target_env)) {
  3214. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3215. if (spv_result_t error = ValidateI32Vec(
  3216. decoration, inst, 2,
  3217. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3218. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3219. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3220. << _.VkErrorID(vuid) << "According to the "
  3221. << spvLogStringForEnv(_.context()->target_env)
  3222. << " spec BuiltIn "
  3223. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3224. uint32_t(builtin))
  3225. << " variable needs to be a 2-component 32-bit int vector. "
  3226. << message;
  3227. })) {
  3228. return error;
  3229. }
  3230. }
  3231. return ValidateFragSizeAtReference(decoration, inst, inst, inst);
  3232. }
  3233. spv_result_t BuiltInsValidator::ValidateFragSizeAtReference(
  3234. const Decoration& decoration, const Instruction& built_in_inst,
  3235. const Instruction& referenced_inst,
  3236. const Instruction& referenced_from_inst) {
  3237. if (spvIsVulkanEnv(_.context()->target_env)) {
  3238. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3239. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3240. if (storage_class != spv::StorageClass::Max &&
  3241. storage_class != spv::StorageClass::Input) {
  3242. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3243. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3244. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3245. << " spec allows BuiltIn "
  3246. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3247. << " to be only used for variables with Input storage class. "
  3248. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3249. referenced_from_inst)
  3250. << " " << GetStorageClassDesc(referenced_from_inst);
  3251. }
  3252. for (const spv::ExecutionModel execution_model : execution_models_) {
  3253. if (execution_model != spv::ExecutionModel::Fragment) {
  3254. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3255. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3256. << _.VkErrorID(vuid)
  3257. << spvLogStringForEnv(_.context()->target_env)
  3258. << " spec allows BuiltIn "
  3259. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3260. << " to be used only with Fragment execution model. "
  3261. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3262. referenced_from_inst, execution_model);
  3263. }
  3264. }
  3265. }
  3266. if (function_id_ == 0) {
  3267. // Propagate this rule to all dependant ids in the global scope.
  3268. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3269. &BuiltInsValidator::ValidateFragSizeAtReference, this, decoration,
  3270. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3271. }
  3272. return SPV_SUCCESS;
  3273. }
  3274. spv_result_t BuiltInsValidator::ValidateFragStencilRefAtDefinition(const Decoration& decoration,
  3275. const Instruction& inst) {
  3276. if (spvIsVulkanEnv(_.context()->target_env)) {
  3277. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3278. if (spv_result_t error = ValidateI(
  3279. decoration, inst,
  3280. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3281. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3282. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3283. << _.VkErrorID(vuid) << "According to the "
  3284. << spvLogStringForEnv(_.context()->target_env)
  3285. << " spec BuiltIn "
  3286. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3287. uint32_t(builtin))
  3288. << " variable needs to be a int scalar. "
  3289. << message;
  3290. })) {
  3291. return error;
  3292. }
  3293. }
  3294. return ValidateFragStencilRefAtReference(decoration, inst, inst, inst);
  3295. }
  3296. spv_result_t BuiltInsValidator::ValidateFragStencilRefAtReference(
  3297. const Decoration& decoration, const Instruction& built_in_inst,
  3298. const Instruction& referenced_inst,
  3299. const Instruction& referenced_from_inst) {
  3300. if (spvIsVulkanEnv(_.context()->target_env)) {
  3301. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3302. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3303. if (storage_class != spv::StorageClass::Max &&
  3304. storage_class != spv::StorageClass::Output) {
  3305. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3306. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3307. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3308. << " spec allows BuiltIn "
  3309. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3310. << " to be only used for variables with Output storage class. "
  3311. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3312. referenced_from_inst)
  3313. << " " << GetStorageClassDesc(referenced_from_inst);
  3314. }
  3315. for (const spv::ExecutionModel execution_model : execution_models_) {
  3316. if (execution_model != spv::ExecutionModel::Fragment) {
  3317. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3318. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3319. << _.VkErrorID(vuid)
  3320. << spvLogStringForEnv(_.context()->target_env)
  3321. << " spec allows BuiltIn "
  3322. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3323. << " to be used only with Fragment execution model. "
  3324. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3325. referenced_from_inst, execution_model);
  3326. }
  3327. }
  3328. }
  3329. if (function_id_ == 0) {
  3330. // Propagate this rule to all dependant ids in the global scope.
  3331. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3332. &BuiltInsValidator::ValidateFragStencilRefAtReference, this, decoration,
  3333. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3334. }
  3335. return SPV_SUCCESS;
  3336. }
  3337. spv_result_t BuiltInsValidator::ValidateFullyCoveredAtDefinition(const Decoration& decoration,
  3338. const Instruction& inst) {
  3339. if (spvIsVulkanEnv(_.context()->target_env)) {
  3340. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3341. if (spv_result_t error = ValidateBool(
  3342. decoration, inst,
  3343. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3344. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3345. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3346. << _.VkErrorID(vuid) << "According to the "
  3347. << spvLogStringForEnv(_.context()->target_env)
  3348. << " spec BuiltIn "
  3349. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3350. uint32_t(builtin))
  3351. << " variable needs to be a bool scalar. "
  3352. << message;
  3353. })) {
  3354. return error;
  3355. }
  3356. }
  3357. return ValidateFullyCoveredAtReference(decoration, inst, inst, inst);
  3358. }
  3359. spv_result_t BuiltInsValidator::ValidateFullyCoveredAtReference(
  3360. const Decoration& decoration, const Instruction& built_in_inst,
  3361. const Instruction& referenced_inst,
  3362. const Instruction& referenced_from_inst) {
  3363. if (spvIsVulkanEnv(_.context()->target_env)) {
  3364. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3365. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3366. if (storage_class != spv::StorageClass::Max &&
  3367. storage_class != spv::StorageClass::Input) {
  3368. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3369. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3370. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3371. << " spec allows BuiltIn "
  3372. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3373. << " to be only used for variables with Input storage class. "
  3374. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3375. referenced_from_inst)
  3376. << " " << GetStorageClassDesc(referenced_from_inst);
  3377. }
  3378. for (const spv::ExecutionModel execution_model : execution_models_) {
  3379. if (execution_model != spv::ExecutionModel::Fragment) {
  3380. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3381. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3382. << _.VkErrorID(vuid)
  3383. << spvLogStringForEnv(_.context()->target_env)
  3384. << " spec allows BuiltIn "
  3385. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3386. << " to be used only with Fragment execution model. "
  3387. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3388. referenced_from_inst, execution_model);
  3389. }
  3390. }
  3391. }
  3392. if (function_id_ == 0) {
  3393. // Propagate this rule to all dependant ids in the global scope.
  3394. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3395. &BuiltInsValidator::ValidateFullyCoveredAtReference, this, decoration,
  3396. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3397. }
  3398. return SPV_SUCCESS;
  3399. }
  3400. spv_result_t BuiltInsValidator::ValidateNVSMOrARMCoreBuiltinsAtDefinition(
  3401. const Decoration& decoration, const Instruction& inst) {
  3402. if (spvIsVulkanEnv(_.context()->target_env)) {
  3403. if (spv_result_t error = ValidateI32(
  3404. decoration, inst,
  3405. [this, &inst,
  3406. &decoration](const std::string& message) -> spv_result_t {
  3407. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3408. << "According to the "
  3409. << spvLogStringForEnv(_.context()->target_env)
  3410. << " spec BuiltIn "
  3411. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3412. decoration.params()[0])
  3413. << " variable needs to be a 32-bit int scalar. "
  3414. << message;
  3415. })) {
  3416. return error;
  3417. }
  3418. }
  3419. // Seed at reference checks with this built-in.
  3420. return ValidateNVSMOrARMCoreBuiltinsAtReference(decoration, inst, inst, inst);
  3421. }
  3422. spv_result_t BuiltInsValidator::ValidateNVSMOrARMCoreBuiltinsAtReference(
  3423. const Decoration& decoration, const Instruction& built_in_inst,
  3424. const Instruction& referenced_inst,
  3425. const Instruction& referenced_from_inst) {
  3426. if (spvIsVulkanEnv(_.context()->target_env)) {
  3427. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3428. if (storage_class != spv::StorageClass::Max &&
  3429. storage_class != spv::StorageClass::Input) {
  3430. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3431. << spvLogStringForEnv(_.context()->target_env)
  3432. << " spec allows BuiltIn "
  3433. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3434. decoration.params()[0])
  3435. << " to be only used for "
  3436. "variables with Input storage class. "
  3437. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3438. referenced_from_inst)
  3439. << " " << GetStorageClassDesc(referenced_from_inst);
  3440. }
  3441. }
  3442. if (function_id_ == 0) {
  3443. // Propagate this rule to all dependant ids in the global scope.
  3444. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3445. &BuiltInsValidator::ValidateNVSMOrARMCoreBuiltinsAtReference, this, decoration,
  3446. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3447. }
  3448. return SPV_SUCCESS;
  3449. }
  3450. spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtDefinition(
  3451. const Decoration& decoration, const Instruction& inst) {
  3452. if (spvIsVulkanEnv(_.context()->target_env)) {
  3453. if (spv_result_t error = ValidateI32(
  3454. decoration, inst,
  3455. [this, &inst,
  3456. &decoration](const std::string& message) -> spv_result_t {
  3457. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3458. << _.VkErrorID(4486)
  3459. << "According to the Vulkan spec BuiltIn "
  3460. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3461. decoration.params()[0])
  3462. << " variable needs to be a 32-bit int scalar. "
  3463. << message;
  3464. })) {
  3465. return error;
  3466. }
  3467. }
  3468. // Seed at reference checks with this built-in.
  3469. return ValidatePrimitiveShadingRateAtReference(decoration, inst, inst, inst);
  3470. }
  3471. spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtReference(
  3472. const Decoration& decoration, const Instruction& built_in_inst,
  3473. const Instruction& referenced_inst,
  3474. const Instruction& referenced_from_inst) {
  3475. if (spvIsVulkanEnv(_.context()->target_env)) {
  3476. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3477. if (storage_class != spv::StorageClass::Max &&
  3478. storage_class != spv::StorageClass::Output) {
  3479. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3480. << _.VkErrorID(4485) << "Vulkan spec allows BuiltIn "
  3481. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3482. decoration.params()[0])
  3483. << " to be only used for variables with Output storage class. "
  3484. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3485. referenced_from_inst)
  3486. << " " << GetStorageClassDesc(referenced_from_inst);
  3487. }
  3488. for (const spv::ExecutionModel execution_model : execution_models_) {
  3489. switch (execution_model) {
  3490. case spv::ExecutionModel::Vertex:
  3491. case spv::ExecutionModel::Geometry:
  3492. case spv::ExecutionModel::MeshNV:
  3493. case spv::ExecutionModel::MeshEXT:
  3494. break;
  3495. default: {
  3496. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3497. << _.VkErrorID(4484) << "Vulkan spec allows BuiltIn "
  3498. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3499. decoration.params()[0])
  3500. << " to be used only with Vertex, Geometry, or MeshNV "
  3501. "execution models. "
  3502. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3503. referenced_from_inst, execution_model);
  3504. }
  3505. }
  3506. }
  3507. }
  3508. if (function_id_ == 0) {
  3509. // Propagate this rule to all dependant ids in the global scope.
  3510. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  3511. std::bind(&BuiltInsValidator::ValidatePrimitiveShadingRateAtReference,
  3512. this, decoration, built_in_inst, referenced_from_inst,
  3513. std::placeholders::_1));
  3514. }
  3515. return SPV_SUCCESS;
  3516. }
  3517. spv_result_t BuiltInsValidator::ValidateShadingRateAtDefinition(
  3518. const Decoration& decoration, const Instruction& inst) {
  3519. if (spvIsVulkanEnv(_.context()->target_env)) {
  3520. if (spv_result_t error = ValidateI32(
  3521. decoration, inst,
  3522. [this, &inst,
  3523. &decoration](const std::string& message) -> spv_result_t {
  3524. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3525. << _.VkErrorID(4492)
  3526. << "According to the Vulkan spec BuiltIn "
  3527. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3528. decoration.params()[0])
  3529. << " variable needs to be a 32-bit int scalar. "
  3530. << message;
  3531. })) {
  3532. return error;
  3533. }
  3534. }
  3535. // Seed at reference checks with this built-in.
  3536. return ValidateShadingRateAtReference(decoration, inst, inst, inst);
  3537. }
  3538. spv_result_t BuiltInsValidator::ValidateShadingRateAtReference(
  3539. const Decoration& decoration, const Instruction& built_in_inst,
  3540. const Instruction& referenced_inst,
  3541. const Instruction& referenced_from_inst) {
  3542. if (spvIsVulkanEnv(_.context()->target_env)) {
  3543. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3544. if (storage_class != spv::StorageClass::Max &&
  3545. storage_class != spv::StorageClass::Input) {
  3546. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3547. << _.VkErrorID(4491) << "Vulkan spec allows BuiltIn "
  3548. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3549. decoration.params()[0])
  3550. << " to be only used for variables with Input storage class. "
  3551. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3552. referenced_from_inst)
  3553. << " " << GetStorageClassDesc(referenced_from_inst);
  3554. }
  3555. for (const spv::ExecutionModel execution_model : execution_models_) {
  3556. if (execution_model != spv::ExecutionModel::Fragment) {
  3557. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3558. << _.VkErrorID(4490) << "Vulkan spec allows BuiltIn "
  3559. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3560. decoration.params()[0])
  3561. << " to be used only with the Fragment execution model. "
  3562. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3563. referenced_from_inst, execution_model);
  3564. }
  3565. }
  3566. }
  3567. if (function_id_ == 0) {
  3568. // Propagate this rule to all dependant ids in the global scope.
  3569. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3570. &BuiltInsValidator::ValidateShadingRateAtReference, this, decoration,
  3571. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3572. }
  3573. return SPV_SUCCESS;
  3574. }
  3575. spv_result_t BuiltInsValidator::ValidateRayTracingBuiltinsAtDefinition(
  3576. const Decoration& decoration, const Instruction& inst) {
  3577. if (spvIsVulkanEnv(_.context()->target_env)) {
  3578. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3579. switch (builtin) {
  3580. case spv::BuiltIn::HitTNV:
  3581. case spv::BuiltIn::RayTminKHR:
  3582. case spv::BuiltIn::RayTmaxKHR:
  3583. // f32 scalar
  3584. if (spv_result_t error = ValidateF32(
  3585. decoration, inst,
  3586. [this, &inst,
  3587. builtin](const std::string& message) -> spv_result_t {
  3588. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3589. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3590. << _.VkErrorID(vuid)
  3591. << "According to the Vulkan spec BuiltIn "
  3592. << _.grammar().lookupOperandName(
  3593. SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3594. << " variable needs to be a 32-bit float scalar. "
  3595. << message;
  3596. })) {
  3597. return error;
  3598. }
  3599. break;
  3600. case spv::BuiltIn::HitKindKHR:
  3601. case spv::BuiltIn::InstanceCustomIndexKHR:
  3602. case spv::BuiltIn::InstanceId:
  3603. case spv::BuiltIn::RayGeometryIndexKHR:
  3604. case spv::BuiltIn::IncomingRayFlagsKHR:
  3605. case spv::BuiltIn::CullMaskKHR:
  3606. // i32 scalar
  3607. if (spv_result_t error = ValidateI32(
  3608. decoration, inst,
  3609. [this, &inst,
  3610. builtin](const std::string& message) -> spv_result_t {
  3611. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3612. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3613. << _.VkErrorID(vuid)
  3614. << "According to the Vulkan spec BuiltIn "
  3615. << _.grammar().lookupOperandName(
  3616. SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3617. << " variable needs to be a 32-bit int scalar. "
  3618. << message;
  3619. })) {
  3620. return error;
  3621. }
  3622. break;
  3623. case spv::BuiltIn::ObjectRayDirectionKHR:
  3624. case spv::BuiltIn::ObjectRayOriginKHR:
  3625. case spv::BuiltIn::WorldRayDirectionKHR:
  3626. case spv::BuiltIn::WorldRayOriginKHR:
  3627. // f32 vec3
  3628. if (spv_result_t error = ValidateF32Vec(
  3629. decoration, inst, 3,
  3630. [this, &inst,
  3631. builtin](const std::string& message) -> spv_result_t {
  3632. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3633. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3634. << _.VkErrorID(vuid)
  3635. << "According to the Vulkan spec BuiltIn "
  3636. << _.grammar().lookupOperandName(
  3637. SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3638. << " variable needs to be a 3-component 32-bit float "
  3639. "vector. "
  3640. << message;
  3641. })) {
  3642. return error;
  3643. }
  3644. break;
  3645. case spv::BuiltIn::LaunchIdKHR:
  3646. case spv::BuiltIn::LaunchSizeKHR:
  3647. // i32 vec3
  3648. if (spv_result_t error = ValidateI32Vec(
  3649. decoration, inst, 3,
  3650. [this, &inst,
  3651. builtin](const std::string& message) -> spv_result_t {
  3652. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3653. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3654. << _.VkErrorID(vuid)
  3655. << "According to the Vulkan spec BuiltIn "
  3656. << _.grammar().lookupOperandName(
  3657. SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3658. << " variable needs to be a 3-component 32-bit int "
  3659. "vector. "
  3660. << message;
  3661. })) {
  3662. return error;
  3663. }
  3664. break;
  3665. case spv::BuiltIn::ObjectToWorldKHR:
  3666. case spv::BuiltIn::WorldToObjectKHR:
  3667. // f32 mat4x3
  3668. if (spv_result_t error = ValidateF32Mat(
  3669. decoration, inst, 3, 4,
  3670. [this, &inst,
  3671. builtin](const std::string& message) -> spv_result_t {
  3672. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3673. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3674. << _.VkErrorID(vuid)
  3675. << "According to the Vulkan spec BuiltIn "
  3676. << _.grammar().lookupOperandName(
  3677. SPV_OPERAND_TYPE_BUILT_IN, uint32_t(builtin))
  3678. << " variable needs to be a matrix with"
  3679. << " 4 columns of 3-component vectors of 32-bit "
  3680. "floats. "
  3681. << message;
  3682. })) {
  3683. return error;
  3684. }
  3685. break;
  3686. default:
  3687. assert(0 && "Unexpected ray tracing builtin");
  3688. break;
  3689. }
  3690. }
  3691. // Seed at reference checks with this built-in.
  3692. return ValidateRayTracingBuiltinsAtReference(decoration, inst, inst, inst);
  3693. }
  3694. spv_result_t BuiltInsValidator::ValidateRayTracingBuiltinsAtReference(
  3695. const Decoration& decoration, const Instruction& built_in_inst,
  3696. const Instruction& referenced_inst,
  3697. const Instruction& referenced_from_inst) {
  3698. if (spvIsVulkanEnv(_.context()->target_env)) {
  3699. const spv::BuiltIn builtin = spv::BuiltIn(decoration.params()[0]);
  3700. const spv::StorageClass storage_class = GetStorageClass(referenced_from_inst);
  3701. if (storage_class != spv::StorageClass::Max &&
  3702. storage_class != spv::StorageClass::Input) {
  3703. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3704. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3705. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  3706. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3707. decoration.params()[0])
  3708. << " to be only used for variables with Input storage class. "
  3709. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3710. referenced_from_inst)
  3711. << " " << GetStorageClassDesc(referenced_from_inst);
  3712. }
  3713. for (const spv::ExecutionModel execution_model : execution_models_) {
  3714. if (!IsExecutionModelValidForRtBuiltIn(builtin, execution_model)) {
  3715. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3716. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3717. << _.VkErrorID(vuid) << "Vulkan spec does not allow BuiltIn "
  3718. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3719. decoration.params()[0])
  3720. << " to be used with the execution model "
  3721. << _.grammar().lookupOperandName(
  3722. SPV_OPERAND_TYPE_EXECUTION_MODEL, uint32_t(execution_model))
  3723. << ".\n"
  3724. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3725. referenced_from_inst, execution_model);
  3726. }
  3727. }
  3728. }
  3729. if (function_id_ == 0) {
  3730. // Propagate this rule to all dependant ids in the global scope.
  3731. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  3732. std::bind(&BuiltInsValidator::ValidateRayTracingBuiltinsAtReference,
  3733. this, decoration, built_in_inst, referenced_from_inst,
  3734. std::placeholders::_1));
  3735. }
  3736. return SPV_SUCCESS;
  3737. }
  3738. spv_result_t BuiltInsValidator::ValidateSingleBuiltInAtDefinition(
  3739. const Decoration& decoration, const Instruction& inst) {
  3740. const spv::BuiltIn label = spv::BuiltIn(decoration.params()[0]);
  3741. if (!spvIsVulkanEnv(_.context()->target_env)) {
  3742. // Early return. All currently implemented rules are based on Vulkan spec.
  3743. //
  3744. // TODO: If you are adding validation rules for environments other than
  3745. // Vulkan (or general rules which are not environment independent), then
  3746. // you need to modify or remove this condition. Consider also adding early
  3747. // returns into BuiltIn-specific rules, so that the system doesn't spawn new
  3748. // rules which don't do anything.
  3749. return SPV_SUCCESS;
  3750. }
  3751. // If you are adding a new BuiltIn enum, please register it here.
  3752. // If the newly added enum has validation rules associated with it
  3753. // consider leaving a TODO and/or creating an issue.
  3754. switch (label) {
  3755. case spv::BuiltIn::ClipDistance:
  3756. case spv::BuiltIn::CullDistance: {
  3757. return ValidateClipOrCullDistanceAtDefinition(decoration, inst);
  3758. }
  3759. case spv::BuiltIn::FragCoord: {
  3760. return ValidateFragCoordAtDefinition(decoration, inst);
  3761. }
  3762. case spv::BuiltIn::FragDepth: {
  3763. return ValidateFragDepthAtDefinition(decoration, inst);
  3764. }
  3765. case spv::BuiltIn::FrontFacing: {
  3766. return ValidateFrontFacingAtDefinition(decoration, inst);
  3767. }
  3768. case spv::BuiltIn::GlobalInvocationId:
  3769. case spv::BuiltIn::LocalInvocationId:
  3770. case spv::BuiltIn::NumWorkgroups:
  3771. case spv::BuiltIn::WorkgroupId: {
  3772. return ValidateComputeShaderI32Vec3InputAtDefinition(decoration, inst);
  3773. }
  3774. case spv::BuiltIn::BaryCoordKHR:
  3775. case spv::BuiltIn::BaryCoordNoPerspKHR: {
  3776. return ValidateFragmentShaderF32Vec3InputAtDefinition(decoration, inst);
  3777. }
  3778. case spv::BuiltIn::HelperInvocation: {
  3779. return ValidateHelperInvocationAtDefinition(decoration, inst);
  3780. }
  3781. case spv::BuiltIn::InvocationId: {
  3782. return ValidateInvocationIdAtDefinition(decoration, inst);
  3783. }
  3784. case spv::BuiltIn::InstanceIndex: {
  3785. return ValidateInstanceIndexAtDefinition(decoration, inst);
  3786. }
  3787. case spv::BuiltIn::Layer:
  3788. case spv::BuiltIn::ViewportIndex: {
  3789. return ValidateLayerOrViewportIndexAtDefinition(decoration, inst);
  3790. }
  3791. case spv::BuiltIn::PatchVertices: {
  3792. return ValidatePatchVerticesAtDefinition(decoration, inst);
  3793. }
  3794. case spv::BuiltIn::PointCoord: {
  3795. return ValidatePointCoordAtDefinition(decoration, inst);
  3796. }
  3797. case spv::BuiltIn::PointSize: {
  3798. return ValidatePointSizeAtDefinition(decoration, inst);
  3799. }
  3800. case spv::BuiltIn::Position: {
  3801. return ValidatePositionAtDefinition(decoration, inst);
  3802. }
  3803. case spv::BuiltIn::PrimitiveId: {
  3804. return ValidatePrimitiveIdAtDefinition(decoration, inst);
  3805. }
  3806. case spv::BuiltIn::SampleId: {
  3807. return ValidateSampleIdAtDefinition(decoration, inst);
  3808. }
  3809. case spv::BuiltIn::SampleMask: {
  3810. return ValidateSampleMaskAtDefinition(decoration, inst);
  3811. }
  3812. case spv::BuiltIn::SamplePosition: {
  3813. return ValidateSamplePositionAtDefinition(decoration, inst);
  3814. }
  3815. case spv::BuiltIn::SubgroupId:
  3816. case spv::BuiltIn::NumSubgroups: {
  3817. return ValidateComputeI32InputAtDefinition(decoration, inst);
  3818. }
  3819. case spv::BuiltIn::SubgroupLocalInvocationId:
  3820. case spv::BuiltIn::SubgroupSize: {
  3821. return ValidateI32InputAtDefinition(decoration, inst);
  3822. }
  3823. case spv::BuiltIn::SubgroupEqMask:
  3824. case spv::BuiltIn::SubgroupGeMask:
  3825. case spv::BuiltIn::SubgroupGtMask:
  3826. case spv::BuiltIn::SubgroupLeMask:
  3827. case spv::BuiltIn::SubgroupLtMask: {
  3828. return ValidateI32Vec4InputAtDefinition(decoration, inst);
  3829. }
  3830. case spv::BuiltIn::TessCoord: {
  3831. return ValidateTessCoordAtDefinition(decoration, inst);
  3832. }
  3833. case spv::BuiltIn::TessLevelOuter: {
  3834. return ValidateTessLevelOuterAtDefinition(decoration, inst);
  3835. }
  3836. case spv::BuiltIn::TessLevelInner: {
  3837. return ValidateTessLevelInnerAtDefinition(decoration, inst);
  3838. }
  3839. case spv::BuiltIn::VertexIndex: {
  3840. return ValidateVertexIndexAtDefinition(decoration, inst);
  3841. }
  3842. case spv::BuiltIn::WorkgroupSize: {
  3843. return ValidateWorkgroupSizeAtDefinition(decoration, inst);
  3844. }
  3845. case spv::BuiltIn::VertexId: {
  3846. return ValidateVertexIdAtDefinition(decoration, inst);
  3847. }
  3848. case spv::BuiltIn::LocalInvocationIndex: {
  3849. return ValidateLocalInvocationIndexAtDefinition(decoration, inst);
  3850. }
  3851. case spv::BuiltIn::CoreIDARM:
  3852. case spv::BuiltIn::CoreCountARM:
  3853. case spv::BuiltIn::CoreMaxIDARM:
  3854. case spv::BuiltIn::WarpIDARM:
  3855. case spv::BuiltIn::WarpMaxIDARM:
  3856. case spv::BuiltIn::WarpsPerSMNV:
  3857. case spv::BuiltIn::SMCountNV:
  3858. case spv::BuiltIn::WarpIDNV:
  3859. case spv::BuiltIn::SMIDNV: {
  3860. return ValidateNVSMOrARMCoreBuiltinsAtDefinition(decoration, inst);
  3861. }
  3862. case spv::BuiltIn::BaseInstance:
  3863. case spv::BuiltIn::BaseVertex: {
  3864. return ValidateBaseInstanceOrVertexAtDefinition(decoration, inst);
  3865. }
  3866. case spv::BuiltIn::DrawIndex: {
  3867. return ValidateDrawIndexAtDefinition(decoration, inst);
  3868. }
  3869. case spv::BuiltIn::ViewIndex: {
  3870. return ValidateViewIndexAtDefinition(decoration, inst);
  3871. }
  3872. case spv::BuiltIn::DeviceIndex: {
  3873. return ValidateDeviceIndexAtDefinition(decoration, inst);
  3874. }
  3875. case spv::BuiltIn::FragInvocationCountEXT: {
  3876. // alias spv::BuiltIn::InvocationsPerPixelNV
  3877. return ValidateFragInvocationCountAtDefinition(decoration, inst);
  3878. }
  3879. case spv::BuiltIn::FragSizeEXT: {
  3880. // alias spv::BuiltIn::FragmentSizeNV
  3881. return ValidateFragSizeAtDefinition(decoration, inst);
  3882. }
  3883. case spv::BuiltIn::FragStencilRefEXT: {
  3884. return ValidateFragStencilRefAtDefinition(decoration, inst);
  3885. }
  3886. case spv::BuiltIn::FullyCoveredEXT:{
  3887. return ValidateFullyCoveredAtDefinition(decoration, inst);
  3888. }
  3889. // Ray tracing builtins
  3890. case spv::BuiltIn::HitKindKHR: // alias spv::BuiltIn::HitKindNV
  3891. case spv::BuiltIn::HitTNV: // NOT present in KHR
  3892. case spv::BuiltIn::InstanceId:
  3893. case spv::BuiltIn::LaunchIdKHR: // alias spv::BuiltIn::LaunchIdNV
  3894. case spv::BuiltIn::LaunchSizeKHR: // alias spv::BuiltIn::LaunchSizeNV
  3895. case spv::BuiltIn::WorldRayOriginKHR: // alias spv::BuiltIn::WorldRayOriginNV
  3896. case spv::BuiltIn::WorldRayDirectionKHR: // alias spv::BuiltIn::WorldRayDirectionNV
  3897. case spv::BuiltIn::ObjectRayOriginKHR: // alias spv::BuiltIn::ObjectRayOriginNV
  3898. case spv::BuiltIn::ObjectRayDirectionKHR: // alias
  3899. // spv::BuiltIn::ObjectRayDirectionNV
  3900. case spv::BuiltIn::RayTminKHR: // alias spv::BuiltIn::RayTminNV
  3901. case spv::BuiltIn::RayTmaxKHR: // alias spv::BuiltIn::RayTmaxNV
  3902. case spv::BuiltIn::InstanceCustomIndexKHR: // alias
  3903. // spv::BuiltIn::InstanceCustomIndexNV
  3904. case spv::BuiltIn::ObjectToWorldKHR: // alias spv::BuiltIn::ObjectToWorldNV
  3905. case spv::BuiltIn::WorldToObjectKHR: // alias spv::BuiltIn::WorldToObjectNV
  3906. case spv::BuiltIn::IncomingRayFlagsKHR: // alias spv::BuiltIn::IncomingRayFlagsNV
  3907. case spv::BuiltIn::RayGeometryIndexKHR: // NOT present in NV
  3908. case spv::BuiltIn::CullMaskKHR: {
  3909. return ValidateRayTracingBuiltinsAtDefinition(decoration, inst);
  3910. }
  3911. case spv::BuiltIn::PrimitiveShadingRateKHR: {
  3912. return ValidatePrimitiveShadingRateAtDefinition(decoration, inst);
  3913. }
  3914. case spv::BuiltIn::ShadingRateKHR: {
  3915. return ValidateShadingRateAtDefinition(decoration, inst);
  3916. }
  3917. default:
  3918. // No validation rules (for the moment).
  3919. break;
  3920. }
  3921. return SPV_SUCCESS;
  3922. }
  3923. spv_result_t BuiltInsValidator::ValidateBuiltInsAtDefinition() {
  3924. for (const auto& kv : _.id_decorations()) {
  3925. const uint32_t id = kv.first;
  3926. const auto& decorations = kv.second;
  3927. if (decorations.empty()) {
  3928. continue;
  3929. }
  3930. const Instruction* inst = _.FindDef(id);
  3931. assert(inst);
  3932. for (const auto& decoration : kv.second) {
  3933. if (decoration.dec_type() != spv::Decoration::BuiltIn) {
  3934. continue;
  3935. }
  3936. if (spv_result_t error =
  3937. ValidateSingleBuiltInAtDefinition(decoration, *inst)) {
  3938. return error;
  3939. }
  3940. }
  3941. }
  3942. return SPV_SUCCESS;
  3943. }
  3944. spv_result_t BuiltInsValidator::Run() {
  3945. // First pass: validate all built-ins at definition and seed
  3946. // id_to_at_reference_checks_ with built-ins.
  3947. if (auto error = ValidateBuiltInsAtDefinition()) {
  3948. return error;
  3949. }
  3950. if (id_to_at_reference_checks_.empty()) {
  3951. // No validation tasks were seeded. Nothing else to do.
  3952. return SPV_SUCCESS;
  3953. }
  3954. // Second pass: validate every id reference in the module using
  3955. // rules in id_to_at_reference_checks_.
  3956. for (const Instruction& inst : _.ordered_instructions()) {
  3957. Update(inst);
  3958. std::set<uint32_t> already_checked;
  3959. for (const auto& operand : inst.operands()) {
  3960. if (!spvIsIdType(operand.type)) {
  3961. // Not id.
  3962. continue;
  3963. }
  3964. const uint32_t id = inst.word(operand.offset);
  3965. if (id == inst.id()) {
  3966. // No need to check result id.
  3967. continue;
  3968. }
  3969. if (!already_checked.insert(id).second) {
  3970. // The instruction has already referenced this id.
  3971. continue;
  3972. }
  3973. // Instruction references the id. Run all checks associated with the id
  3974. // on the instruction. id_to_at_reference_checks_ can be modified in the
  3975. // process, iterators are safe because it's a tree-based map.
  3976. const auto it = id_to_at_reference_checks_.find(id);
  3977. if (it != id_to_at_reference_checks_.end()) {
  3978. for (const auto& check : it->second) {
  3979. if (spv_result_t error = check(inst)) {
  3980. return error;
  3981. }
  3982. }
  3983. }
  3984. }
  3985. }
  3986. return SPV_SUCCESS;
  3987. }
  3988. } // namespace
  3989. // Validates correctness of built-in variables.
  3990. spv_result_t ValidateBuiltIns(ValidationState_t& _) {
  3991. BuiltInsValidator validator(_);
  3992. return validator.Run();
  3993. }
  3994. } // namespace val
  3995. } // namespace spvtools