validate_builtins.cpp 219 KB

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