validate_builtins.cpp 199 KB

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