validate_builtins.cpp 183 KB

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