validate_builtins.cpp 183 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289
  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, MeshNV, or TaskNV 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, MeshNV, or TaskNV 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. execution_model != SpvExecutionModelTaskNV &&
  2787. execution_model != SpvExecutionModelMeshNV) {
  2788. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2789. << _.VkErrorID(4425)
  2790. << spvLogStringForEnv(_.context()->target_env)
  2791. << " spec allows BuiltIn "
  2792. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2793. decoration.params()[0])
  2794. << " to be used only with GLCompute, MeshNV, or TaskNV execution model. "
  2795. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2796. referenced_from_inst, execution_model);
  2797. }
  2798. }
  2799. }
  2800. if (function_id_ == 0) {
  2801. // Propagate this rule to all dependant ids in the global scope.
  2802. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2803. &BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, decoration,
  2804. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2805. }
  2806. return SPV_SUCCESS;
  2807. }
  2808. spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtDefinition(
  2809. const Decoration& decoration, const Instruction& inst) {
  2810. if (spvIsVulkanEnv(_.context()->target_env)) {
  2811. if (spv_result_t error = ValidateI32(
  2812. decoration, inst,
  2813. [this, &inst,
  2814. &decoration](const std::string& message) -> spv_result_t {
  2815. uint32_t vuid = (decoration.params()[0] == SpvBuiltInBaseInstance)
  2816. ? 4183
  2817. : 4186;
  2818. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2819. << _.VkErrorID(vuid)
  2820. << "According to the Vulkan spec BuiltIn "
  2821. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2822. decoration.params()[0])
  2823. << " variable needs to be a 32-bit int scalar. "
  2824. << message;
  2825. })) {
  2826. return error;
  2827. }
  2828. }
  2829. return ValidateBaseInstanceOrVertexAtReference(decoration, inst, inst, inst);
  2830. }
  2831. spv_result_t BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference(
  2832. const Decoration& decoration, const Instruction& built_in_inst,
  2833. const Instruction& referenced_inst,
  2834. const Instruction& referenced_from_inst) {
  2835. uint32_t operand = decoration.params()[0];
  2836. if (spvIsVulkanEnv(_.context()->target_env)) {
  2837. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2838. if (storage_class != SpvStorageClassMax &&
  2839. storage_class != SpvStorageClassInput) {
  2840. uint32_t vuid = (operand == SpvBuiltInBaseInstance) ? 4182 : 4185;
  2841. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2842. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2843. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2844. operand)
  2845. << " to be only used for variables with Input storage class. "
  2846. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2847. referenced_from_inst)
  2848. << " " << GetStorageClassDesc(referenced_from_inst);
  2849. }
  2850. for (const SpvExecutionModel execution_model : execution_models_) {
  2851. if (execution_model != SpvExecutionModelVertex) {
  2852. uint32_t vuid = (operand == SpvBuiltInBaseInstance) ? 4181 : 4184;
  2853. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2854. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  2855. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2856. operand)
  2857. << " to be used only with Vertex execution model. "
  2858. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2859. referenced_from_inst, execution_model);
  2860. }
  2861. }
  2862. }
  2863. if (function_id_ == 0) {
  2864. // Propagate this rule to all dependant ids in the global scope.
  2865. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2866. std::bind(&BuiltInsValidator::ValidateBaseInstanceOrVertexAtReference,
  2867. this, decoration, built_in_inst, referenced_from_inst,
  2868. std::placeholders::_1));
  2869. }
  2870. return SPV_SUCCESS;
  2871. }
  2872. spv_result_t BuiltInsValidator::ValidateDrawIndexAtDefinition(
  2873. const Decoration& decoration, const Instruction& inst) {
  2874. if (spvIsVulkanEnv(_.context()->target_env)) {
  2875. if (spv_result_t error = ValidateI32(
  2876. decoration, inst,
  2877. [this, &inst,
  2878. &decoration](const std::string& message) -> spv_result_t {
  2879. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2880. << _.VkErrorID(4209)
  2881. << "According to the Vulkan spec BuiltIn "
  2882. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2883. decoration.params()[0])
  2884. << " variable needs to be a 32-bit int scalar. "
  2885. << message;
  2886. })) {
  2887. return error;
  2888. }
  2889. }
  2890. return ValidateDrawIndexAtReference(decoration, inst, inst, inst);
  2891. }
  2892. spv_result_t BuiltInsValidator::ValidateDrawIndexAtReference(
  2893. const Decoration& decoration, const Instruction& built_in_inst,
  2894. const Instruction& referenced_inst,
  2895. const Instruction& referenced_from_inst) {
  2896. uint32_t operand = decoration.params()[0];
  2897. if (spvIsVulkanEnv(_.context()->target_env)) {
  2898. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2899. if (storage_class != SpvStorageClassMax &&
  2900. storage_class != SpvStorageClassInput) {
  2901. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2902. << _.VkErrorID(4208) << "Vulkan spec allows BuiltIn "
  2903. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2904. operand)
  2905. << " to be only used for variables with Input storage class. "
  2906. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2907. referenced_from_inst)
  2908. << " " << GetStorageClassDesc(referenced_from_inst);
  2909. }
  2910. for (const SpvExecutionModel execution_model : execution_models_) {
  2911. if (execution_model != SpvExecutionModelVertex &&
  2912. execution_model != SpvExecutionModelMeshNV &&
  2913. execution_model != SpvExecutionModelTaskNV) {
  2914. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2915. << _.VkErrorID(4207) << "Vulkan spec allows BuiltIn "
  2916. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2917. operand)
  2918. << " to be used only with Vertex, MeshNV, or TaskNV execution "
  2919. "model. "
  2920. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2921. referenced_from_inst, execution_model);
  2922. }
  2923. }
  2924. }
  2925. if (function_id_ == 0) {
  2926. // Propagate this rule to all dependant ids in the global scope.
  2927. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2928. &BuiltInsValidator::ValidateDrawIndexAtReference, this, decoration,
  2929. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2930. }
  2931. return SPV_SUCCESS;
  2932. }
  2933. spv_result_t BuiltInsValidator::ValidateViewIndexAtDefinition(
  2934. const Decoration& decoration, const Instruction& inst) {
  2935. if (spvIsVulkanEnv(_.context()->target_env)) {
  2936. if (spv_result_t error = ValidateI32(
  2937. decoration, inst,
  2938. [this, &inst,
  2939. &decoration](const std::string& message) -> spv_result_t {
  2940. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2941. << _.VkErrorID(4403)
  2942. << "According to the Vulkan spec BuiltIn "
  2943. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2944. decoration.params()[0])
  2945. << " variable needs to be a 32-bit int scalar. "
  2946. << message;
  2947. })) {
  2948. return error;
  2949. }
  2950. }
  2951. return ValidateViewIndexAtReference(decoration, inst, inst, inst);
  2952. }
  2953. spv_result_t BuiltInsValidator::ValidateViewIndexAtReference(
  2954. const Decoration& decoration, const Instruction& built_in_inst,
  2955. const Instruction& referenced_inst,
  2956. const Instruction& referenced_from_inst) {
  2957. uint32_t operand = decoration.params()[0];
  2958. if (spvIsVulkanEnv(_.context()->target_env)) {
  2959. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2960. if (storage_class != SpvStorageClassMax &&
  2961. storage_class != SpvStorageClassInput) {
  2962. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2963. << _.VkErrorID(4402) << "Vulkan spec allows BuiltIn "
  2964. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2965. operand)
  2966. << " to be only used for variables with Input storage class. "
  2967. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2968. referenced_from_inst)
  2969. << " " << GetStorageClassDesc(referenced_from_inst);
  2970. }
  2971. for (const SpvExecutionModel execution_model : execution_models_) {
  2972. if (execution_model == SpvExecutionModelGLCompute) {
  2973. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2974. << _.VkErrorID(4401) << "Vulkan spec allows BuiltIn "
  2975. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2976. operand)
  2977. << " to be not be used with GLCompute execution model. "
  2978. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2979. referenced_from_inst, execution_model);
  2980. }
  2981. }
  2982. }
  2983. if (function_id_ == 0) {
  2984. // Propagate this rule to all dependant ids in the global scope.
  2985. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2986. &BuiltInsValidator::ValidateViewIndexAtReference, this, decoration,
  2987. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2988. }
  2989. return SPV_SUCCESS;
  2990. }
  2991. spv_result_t BuiltInsValidator::ValidateDeviceIndexAtDefinition(
  2992. const Decoration& decoration, const Instruction& inst) {
  2993. if (spvIsVulkanEnv(_.context()->target_env)) {
  2994. if (spv_result_t error = ValidateI32(
  2995. decoration, inst,
  2996. [this, &inst,
  2997. &decoration](const std::string& message) -> spv_result_t {
  2998. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2999. << _.VkErrorID(4206)
  3000. << "According to the Vulkan spec BuiltIn "
  3001. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3002. decoration.params()[0])
  3003. << " variable needs to be a 32-bit int scalar. "
  3004. << message;
  3005. })) {
  3006. return error;
  3007. }
  3008. }
  3009. return ValidateDeviceIndexAtReference(decoration, inst, inst, inst);
  3010. }
  3011. spv_result_t BuiltInsValidator::ValidateDeviceIndexAtReference(
  3012. const Decoration& decoration, const Instruction& built_in_inst,
  3013. const Instruction& referenced_inst,
  3014. const Instruction& referenced_from_inst) {
  3015. uint32_t operand = decoration.params()[0];
  3016. if (spvIsVulkanEnv(_.context()->target_env)) {
  3017. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3018. if (storage_class != SpvStorageClassMax &&
  3019. storage_class != SpvStorageClassInput) {
  3020. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3021. << _.VkErrorID(4205) << "Vulkan spec allows BuiltIn "
  3022. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3023. operand)
  3024. << " to be only used for variables with Input storage class. "
  3025. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3026. referenced_from_inst)
  3027. << " " << GetStorageClassDesc(referenced_from_inst);
  3028. }
  3029. }
  3030. if (function_id_ == 0) {
  3031. // Propagate this rule to all dependant ids in the global scope.
  3032. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3033. &BuiltInsValidator::ValidateDeviceIndexAtReference, this, decoration,
  3034. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3035. }
  3036. return SPV_SUCCESS;
  3037. }
  3038. spv_result_t BuiltInsValidator::ValidateFragInvocationCountAtDefinition(const Decoration& decoration,
  3039. const Instruction& inst) {
  3040. if (spvIsVulkanEnv(_.context()->target_env)) {
  3041. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3042. if (spv_result_t error = ValidateI32(
  3043. decoration, inst,
  3044. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3045. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3046. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3047. << _.VkErrorID(vuid) << "According to the "
  3048. << spvLogStringForEnv(_.context()->target_env)
  3049. << " spec BuiltIn "
  3050. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3051. builtin)
  3052. << " variable needs to be a 32-bit int scalar. "
  3053. << message;
  3054. })) {
  3055. return error;
  3056. }
  3057. }
  3058. return ValidateFragInvocationCountAtReference(decoration, inst, inst, inst);
  3059. }
  3060. spv_result_t BuiltInsValidator::ValidateFragInvocationCountAtReference(
  3061. const Decoration& decoration, const Instruction& built_in_inst,
  3062. const Instruction& referenced_inst,
  3063. const Instruction& referenced_from_inst) {
  3064. if (spvIsVulkanEnv(_.context()->target_env)) {
  3065. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3066. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3067. if (storage_class != SpvStorageClassMax &&
  3068. storage_class != SpvStorageClassInput) {
  3069. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3070. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3071. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3072. << " spec allows BuiltIn "
  3073. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3074. << " to be only used for variables with Input storage class. "
  3075. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3076. referenced_from_inst)
  3077. << " " << GetStorageClassDesc(referenced_from_inst);
  3078. }
  3079. for (const SpvExecutionModel execution_model : execution_models_) {
  3080. if (execution_model != SpvExecutionModelFragment) {
  3081. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3082. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3083. << _.VkErrorID(vuid)
  3084. << spvLogStringForEnv(_.context()->target_env)
  3085. << " spec allows BuiltIn "
  3086. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3087. << " to be used only with Fragment execution model. "
  3088. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3089. referenced_from_inst, execution_model);
  3090. }
  3091. }
  3092. }
  3093. if (function_id_ == 0) {
  3094. // Propagate this rule to all dependant ids in the global scope.
  3095. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3096. &BuiltInsValidator::ValidateFragInvocationCountAtReference, this, decoration,
  3097. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3098. }
  3099. return SPV_SUCCESS;
  3100. }
  3101. spv_result_t BuiltInsValidator::ValidateFragSizeAtDefinition(const Decoration& decoration,
  3102. const Instruction& inst) {
  3103. if (spvIsVulkanEnv(_.context()->target_env)) {
  3104. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3105. if (spv_result_t error = ValidateI32Vec(
  3106. decoration, inst, 2,
  3107. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3108. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3109. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3110. << _.VkErrorID(vuid) << "According to the "
  3111. << spvLogStringForEnv(_.context()->target_env)
  3112. << " spec BuiltIn "
  3113. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3114. builtin)
  3115. << " variable needs to be a 2-component 32-bit int vector. "
  3116. << message;
  3117. })) {
  3118. return error;
  3119. }
  3120. }
  3121. return ValidateFragSizeAtReference(decoration, inst, inst, inst);
  3122. }
  3123. spv_result_t BuiltInsValidator::ValidateFragSizeAtReference(
  3124. const Decoration& decoration, const Instruction& built_in_inst,
  3125. const Instruction& referenced_inst,
  3126. const Instruction& referenced_from_inst) {
  3127. if (spvIsVulkanEnv(_.context()->target_env)) {
  3128. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3129. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3130. if (storage_class != SpvStorageClassMax &&
  3131. storage_class != SpvStorageClassInput) {
  3132. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3133. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3134. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3135. << " spec allows BuiltIn "
  3136. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3137. << " to be only used for variables with Input storage class. "
  3138. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3139. referenced_from_inst)
  3140. << " " << GetStorageClassDesc(referenced_from_inst);
  3141. }
  3142. for (const SpvExecutionModel execution_model : execution_models_) {
  3143. if (execution_model != SpvExecutionModelFragment) {
  3144. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3145. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3146. << _.VkErrorID(vuid)
  3147. << spvLogStringForEnv(_.context()->target_env)
  3148. << " spec allows BuiltIn "
  3149. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3150. << " to be used only with Fragment execution model. "
  3151. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3152. referenced_from_inst, execution_model);
  3153. }
  3154. }
  3155. }
  3156. if (function_id_ == 0) {
  3157. // Propagate this rule to all dependant ids in the global scope.
  3158. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3159. &BuiltInsValidator::ValidateFragSizeAtReference, this, decoration,
  3160. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3161. }
  3162. return SPV_SUCCESS;
  3163. }
  3164. spv_result_t BuiltInsValidator::ValidateFragStencilRefAtDefinition(const Decoration& decoration,
  3165. const Instruction& inst) {
  3166. if (spvIsVulkanEnv(_.context()->target_env)) {
  3167. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3168. if (spv_result_t error = ValidateI(
  3169. decoration, inst,
  3170. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3171. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3172. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3173. << _.VkErrorID(vuid) << "According to the "
  3174. << spvLogStringForEnv(_.context()->target_env)
  3175. << " spec BuiltIn "
  3176. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3177. builtin)
  3178. << " variable needs to be a int scalar. "
  3179. << message;
  3180. })) {
  3181. return error;
  3182. }
  3183. }
  3184. return ValidateFragStencilRefAtReference(decoration, inst, inst, inst);
  3185. }
  3186. spv_result_t BuiltInsValidator::ValidateFragStencilRefAtReference(
  3187. const Decoration& decoration, const Instruction& built_in_inst,
  3188. const Instruction& referenced_inst,
  3189. const Instruction& referenced_from_inst) {
  3190. if (spvIsVulkanEnv(_.context()->target_env)) {
  3191. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3192. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3193. if (storage_class != SpvStorageClassMax &&
  3194. storage_class != SpvStorageClassOutput) {
  3195. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3196. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3197. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3198. << " spec allows BuiltIn "
  3199. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3200. << " to be only used for variables with Output storage class. "
  3201. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3202. referenced_from_inst)
  3203. << " " << GetStorageClassDesc(referenced_from_inst);
  3204. }
  3205. for (const SpvExecutionModel execution_model : execution_models_) {
  3206. if (execution_model != SpvExecutionModelFragment) {
  3207. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3208. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3209. << _.VkErrorID(vuid)
  3210. << spvLogStringForEnv(_.context()->target_env)
  3211. << " spec allows BuiltIn "
  3212. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3213. << " to be used only with Fragment execution model. "
  3214. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3215. referenced_from_inst, execution_model);
  3216. }
  3217. }
  3218. }
  3219. if (function_id_ == 0) {
  3220. // Propagate this rule to all dependant ids in the global scope.
  3221. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3222. &BuiltInsValidator::ValidateFragStencilRefAtReference, this, decoration,
  3223. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3224. }
  3225. return SPV_SUCCESS;
  3226. }
  3227. spv_result_t BuiltInsValidator::ValidateFullyCoveredAtDefinition(const Decoration& decoration,
  3228. const Instruction& inst) {
  3229. if (spvIsVulkanEnv(_.context()->target_env)) {
  3230. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3231. if (spv_result_t error = ValidateBool(
  3232. decoration, inst,
  3233. [this, &inst, &builtin](const std::string& message) -> spv_result_t {
  3234. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3235. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3236. << _.VkErrorID(vuid) << "According to the "
  3237. << spvLogStringForEnv(_.context()->target_env)
  3238. << " spec BuiltIn "
  3239. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3240. builtin)
  3241. << " variable needs to be a bool scalar. "
  3242. << message;
  3243. })) {
  3244. return error;
  3245. }
  3246. }
  3247. return ValidateFullyCoveredAtReference(decoration, inst, inst, inst);
  3248. }
  3249. spv_result_t BuiltInsValidator::ValidateFullyCoveredAtReference(
  3250. const Decoration& decoration, const Instruction& built_in_inst,
  3251. const Instruction& referenced_inst,
  3252. const Instruction& referenced_from_inst) {
  3253. if (spvIsVulkanEnv(_.context()->target_env)) {
  3254. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3255. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3256. if (storage_class != SpvStorageClassMax &&
  3257. storage_class != SpvStorageClassInput) {
  3258. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3259. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3260. << _.VkErrorID(vuid) << spvLogStringForEnv(_.context()->target_env)
  3261. << " spec allows BuiltIn "
  3262. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3263. << " to be only used for variables with Input storage class. "
  3264. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3265. referenced_from_inst)
  3266. << " " << GetStorageClassDesc(referenced_from_inst);
  3267. }
  3268. for (const SpvExecutionModel execution_model : execution_models_) {
  3269. if (execution_model != SpvExecutionModelFragment) {
  3270. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3271. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3272. << _.VkErrorID(vuid)
  3273. << spvLogStringForEnv(_.context()->target_env)
  3274. << " spec allows BuiltIn "
  3275. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3276. << " to be used only with Fragment execution model. "
  3277. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3278. referenced_from_inst, execution_model);
  3279. }
  3280. }
  3281. }
  3282. if (function_id_ == 0) {
  3283. // Propagate this rule to all dependant ids in the global scope.
  3284. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3285. &BuiltInsValidator::ValidateFullyCoveredAtReference, this, decoration,
  3286. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3287. }
  3288. return SPV_SUCCESS;
  3289. }
  3290. spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtDefinition(
  3291. const Decoration& decoration, const Instruction& inst) {
  3292. if (spvIsVulkanEnv(_.context()->target_env)) {
  3293. if (spv_result_t error = ValidateI32(
  3294. decoration, inst,
  3295. [this, &inst,
  3296. &decoration](const std::string& message) -> spv_result_t {
  3297. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3298. << "According to the "
  3299. << spvLogStringForEnv(_.context()->target_env)
  3300. << " spec BuiltIn "
  3301. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3302. decoration.params()[0])
  3303. << " variable needs to be a 32-bit int scalar. "
  3304. << message;
  3305. })) {
  3306. return error;
  3307. }
  3308. }
  3309. // Seed at reference checks with this built-in.
  3310. return ValidateSMBuiltinsAtReference(decoration, inst, inst, inst);
  3311. }
  3312. spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtReference(
  3313. const Decoration& decoration, const Instruction& built_in_inst,
  3314. const Instruction& referenced_inst,
  3315. const Instruction& referenced_from_inst) {
  3316. if (spvIsVulkanEnv(_.context()->target_env)) {
  3317. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3318. if (storage_class != SpvStorageClassMax &&
  3319. storage_class != SpvStorageClassInput) {
  3320. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3321. << spvLogStringForEnv(_.context()->target_env)
  3322. << " spec allows BuiltIn "
  3323. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3324. decoration.params()[0])
  3325. << " to be only used for "
  3326. "variables with Input storage class. "
  3327. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3328. referenced_from_inst)
  3329. << " " << GetStorageClassDesc(referenced_from_inst);
  3330. }
  3331. }
  3332. if (function_id_ == 0) {
  3333. // Propagate this rule to all dependant ids in the global scope.
  3334. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3335. &BuiltInsValidator::ValidateSMBuiltinsAtReference, this, decoration,
  3336. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3337. }
  3338. return SPV_SUCCESS;
  3339. }
  3340. spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtDefinition(
  3341. const Decoration& decoration, const Instruction& inst) {
  3342. if (spvIsVulkanEnv(_.context()->target_env)) {
  3343. if (spv_result_t error = ValidateI32(
  3344. decoration, inst,
  3345. [this, &inst,
  3346. &decoration](const std::string& message) -> spv_result_t {
  3347. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3348. << _.VkErrorID(4486)
  3349. << "According to the Vulkan spec BuiltIn "
  3350. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3351. decoration.params()[0])
  3352. << " variable needs to be a 32-bit int scalar. "
  3353. << message;
  3354. })) {
  3355. return error;
  3356. }
  3357. }
  3358. // Seed at reference checks with this built-in.
  3359. return ValidatePrimitiveShadingRateAtReference(decoration, inst, inst, inst);
  3360. }
  3361. spv_result_t BuiltInsValidator::ValidatePrimitiveShadingRateAtReference(
  3362. const Decoration& decoration, const Instruction& built_in_inst,
  3363. const Instruction& referenced_inst,
  3364. const Instruction& referenced_from_inst) {
  3365. if (spvIsVulkanEnv(_.context()->target_env)) {
  3366. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3367. if (storage_class != SpvStorageClassMax &&
  3368. storage_class != SpvStorageClassOutput) {
  3369. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3370. << _.VkErrorID(4485) << "Vulkan spec allows BuiltIn "
  3371. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3372. decoration.params()[0])
  3373. << " to be only used for variables with Output storage class. "
  3374. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3375. referenced_from_inst)
  3376. << " " << GetStorageClassDesc(referenced_from_inst);
  3377. }
  3378. for (const SpvExecutionModel execution_model : execution_models_) {
  3379. switch (execution_model) {
  3380. case SpvExecutionModelVertex:
  3381. case SpvExecutionModelGeometry:
  3382. case SpvExecutionModelMeshNV:
  3383. break;
  3384. default: {
  3385. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3386. << _.VkErrorID(4484) << "Vulkan spec allows BuiltIn "
  3387. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3388. decoration.params()[0])
  3389. << " to be used only with Vertex, Geometry, or MeshNV "
  3390. "execution models. "
  3391. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3392. referenced_from_inst, execution_model);
  3393. }
  3394. }
  3395. }
  3396. }
  3397. if (function_id_ == 0) {
  3398. // Propagate this rule to all dependant ids in the global scope.
  3399. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  3400. std::bind(&BuiltInsValidator::ValidatePrimitiveShadingRateAtReference,
  3401. this, decoration, built_in_inst, referenced_from_inst,
  3402. std::placeholders::_1));
  3403. }
  3404. return SPV_SUCCESS;
  3405. }
  3406. spv_result_t BuiltInsValidator::ValidateShadingRateAtDefinition(
  3407. const Decoration& decoration, const Instruction& inst) {
  3408. if (spvIsVulkanEnv(_.context()->target_env)) {
  3409. if (spv_result_t error = ValidateI32(
  3410. decoration, inst,
  3411. [this, &inst,
  3412. &decoration](const std::string& message) -> spv_result_t {
  3413. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3414. << _.VkErrorID(4492)
  3415. << "According to the Vulkan spec BuiltIn "
  3416. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3417. decoration.params()[0])
  3418. << " variable needs to be a 32-bit int scalar. "
  3419. << message;
  3420. })) {
  3421. return error;
  3422. }
  3423. }
  3424. // Seed at reference checks with this built-in.
  3425. return ValidateShadingRateAtReference(decoration, inst, inst, inst);
  3426. }
  3427. spv_result_t BuiltInsValidator::ValidateShadingRateAtReference(
  3428. const Decoration& decoration, const Instruction& built_in_inst,
  3429. const Instruction& referenced_inst,
  3430. const Instruction& referenced_from_inst) {
  3431. if (spvIsVulkanEnv(_.context()->target_env)) {
  3432. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3433. if (storage_class != SpvStorageClassMax &&
  3434. storage_class != SpvStorageClassInput) {
  3435. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3436. << _.VkErrorID(4491) << "Vulkan spec allows BuiltIn "
  3437. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3438. decoration.params()[0])
  3439. << " to be only used for variables with Input storage class. "
  3440. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3441. referenced_from_inst)
  3442. << " " << GetStorageClassDesc(referenced_from_inst);
  3443. }
  3444. for (const SpvExecutionModel execution_model : execution_models_) {
  3445. if (execution_model != SpvExecutionModelFragment) {
  3446. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3447. << _.VkErrorID(4490) << "Vulkan spec allows BuiltIn "
  3448. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3449. decoration.params()[0])
  3450. << " to be used only with the Fragment execution model. "
  3451. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3452. referenced_from_inst, execution_model);
  3453. }
  3454. }
  3455. }
  3456. if (function_id_ == 0) {
  3457. // Propagate this rule to all dependant ids in the global scope.
  3458. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  3459. &BuiltInsValidator::ValidateShadingRateAtReference, this, decoration,
  3460. built_in_inst, referenced_from_inst, std::placeholders::_1));
  3461. }
  3462. return SPV_SUCCESS;
  3463. }
  3464. spv_result_t BuiltInsValidator::ValidateRayTracingBuiltinsAtDefinition(
  3465. const Decoration& decoration, const Instruction& inst) {
  3466. if (spvIsVulkanEnv(_.context()->target_env)) {
  3467. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3468. switch (builtin) {
  3469. case SpvBuiltInHitTNV:
  3470. case SpvBuiltInRayTminKHR:
  3471. case SpvBuiltInRayTmaxKHR:
  3472. // f32 scalar
  3473. if (spv_result_t error = ValidateF32(
  3474. decoration, inst,
  3475. [this, &inst,
  3476. builtin](const std::string& message) -> spv_result_t {
  3477. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3478. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3479. << _.VkErrorID(vuid)
  3480. << "According to the Vulkan spec BuiltIn "
  3481. << _.grammar().lookupOperandName(
  3482. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3483. << " variable needs to be a 32-bit float scalar. "
  3484. << message;
  3485. })) {
  3486. return error;
  3487. }
  3488. break;
  3489. case SpvBuiltInHitKindKHR:
  3490. case SpvBuiltInInstanceCustomIndexKHR:
  3491. case SpvBuiltInInstanceId:
  3492. case SpvBuiltInRayGeometryIndexKHR:
  3493. case SpvBuiltInIncomingRayFlagsKHR:
  3494. // i32 scalar
  3495. if (spv_result_t error = ValidateI32(
  3496. decoration, inst,
  3497. [this, &inst,
  3498. builtin](const std::string& message) -> spv_result_t {
  3499. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3500. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3501. << _.VkErrorID(vuid)
  3502. << "According to the Vulkan spec BuiltIn "
  3503. << _.grammar().lookupOperandName(
  3504. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3505. << " variable needs to be a 32-bit int scalar. "
  3506. << message;
  3507. })) {
  3508. return error;
  3509. }
  3510. break;
  3511. case SpvBuiltInObjectRayDirectionKHR:
  3512. case SpvBuiltInObjectRayOriginKHR:
  3513. case SpvBuiltInWorldRayDirectionKHR:
  3514. case SpvBuiltInWorldRayOriginKHR:
  3515. // f32 vec3
  3516. if (spv_result_t error = ValidateF32Vec(
  3517. decoration, inst, 3,
  3518. [this, &inst,
  3519. builtin](const std::string& message) -> spv_result_t {
  3520. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3521. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3522. << _.VkErrorID(vuid)
  3523. << "According to the Vulkan spec BuiltIn "
  3524. << _.grammar().lookupOperandName(
  3525. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3526. << " variable needs to be a 3-component 32-bit float "
  3527. "vector. "
  3528. << message;
  3529. })) {
  3530. return error;
  3531. }
  3532. break;
  3533. case SpvBuiltInLaunchIdKHR:
  3534. case SpvBuiltInLaunchSizeKHR:
  3535. // i32 vec3
  3536. if (spv_result_t error = ValidateI32Vec(
  3537. decoration, inst, 3,
  3538. [this, &inst,
  3539. builtin](const std::string& message) -> spv_result_t {
  3540. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3541. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3542. << _.VkErrorID(vuid)
  3543. << "According to the Vulkan spec BuiltIn "
  3544. << _.grammar().lookupOperandName(
  3545. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3546. << " variable needs to be a 3-component 32-bit int "
  3547. "vector. "
  3548. << message;
  3549. })) {
  3550. return error;
  3551. }
  3552. break;
  3553. case SpvBuiltInObjectToWorldKHR:
  3554. case SpvBuiltInWorldToObjectKHR:
  3555. // f32 mat4x3
  3556. if (spv_result_t error = ValidateF32Mat(
  3557. decoration, inst, 3, 4,
  3558. [this, &inst,
  3559. builtin](const std::string& message) -> spv_result_t {
  3560. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorType);
  3561. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  3562. << _.VkErrorID(vuid)
  3563. << "According to the Vulkan spec BuiltIn "
  3564. << _.grammar().lookupOperandName(
  3565. SPV_OPERAND_TYPE_BUILT_IN, builtin)
  3566. << " variable needs to be a matrix with"
  3567. << " 4 columns of 3-component vectors of 32-bit "
  3568. "floats. "
  3569. << message;
  3570. })) {
  3571. return error;
  3572. }
  3573. break;
  3574. default:
  3575. assert(0 && "Unexpected ray tracing builtin");
  3576. break;
  3577. }
  3578. }
  3579. // Seed at reference checks with this built-in.
  3580. return ValidateRayTracingBuiltinsAtReference(decoration, inst, inst, inst);
  3581. }
  3582. spv_result_t BuiltInsValidator::ValidateRayTracingBuiltinsAtReference(
  3583. const Decoration& decoration, const Instruction& built_in_inst,
  3584. const Instruction& referenced_inst,
  3585. const Instruction& referenced_from_inst) {
  3586. if (spvIsVulkanEnv(_.context()->target_env)) {
  3587. const SpvBuiltIn builtin = SpvBuiltIn(decoration.params()[0]);
  3588. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  3589. if (storage_class != SpvStorageClassMax &&
  3590. storage_class != SpvStorageClassInput) {
  3591. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorStorageClass);
  3592. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3593. << _.VkErrorID(vuid) << "Vulkan spec allows BuiltIn "
  3594. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3595. decoration.params()[0])
  3596. << " to be only used for variables with Input storage class. "
  3597. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3598. referenced_from_inst)
  3599. << " " << GetStorageClassDesc(referenced_from_inst);
  3600. }
  3601. for (const SpvExecutionModel execution_model : execution_models_) {
  3602. if (!IsExecutionModelValidForRtBuiltIn(builtin, execution_model)) {
  3603. uint32_t vuid = GetVUIDForBuiltin(builtin, VUIDErrorExecutionModel);
  3604. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  3605. << _.VkErrorID(vuid) << "Vulkan spec does not allow BuiltIn "
  3606. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  3607. decoration.params()[0])
  3608. << " to be used with the execution model "
  3609. << _.grammar().lookupOperandName(
  3610. SPV_OPERAND_TYPE_EXECUTION_MODEL, execution_model)
  3611. << ".\n"
  3612. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  3613. referenced_from_inst, execution_model);
  3614. }
  3615. }
  3616. }
  3617. if (function_id_ == 0) {
  3618. // Propagate this rule to all dependant ids in the global scope.
  3619. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  3620. std::bind(&BuiltInsValidator::ValidateRayTracingBuiltinsAtReference,
  3621. this, decoration, built_in_inst, referenced_from_inst,
  3622. std::placeholders::_1));
  3623. }
  3624. return SPV_SUCCESS;
  3625. }
  3626. spv_result_t BuiltInsValidator::ValidateSingleBuiltInAtDefinition(
  3627. const Decoration& decoration, const Instruction& inst) {
  3628. const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]);
  3629. if (!spvIsVulkanEnv(_.context()->target_env)) {
  3630. // Early return. All currently implemented rules are based on Vulkan spec.
  3631. //
  3632. // TODO: If you are adding validation rules for environments other than
  3633. // Vulkan (or general rules which are not environment independent), then
  3634. // you need to modify or remove this condition. Consider also adding early
  3635. // returns into BuiltIn-specific rules, so that the system doesn't spawn new
  3636. // rules which don't do anything.
  3637. return SPV_SUCCESS;
  3638. }
  3639. // If you are adding a new BuiltIn enum, please register it here.
  3640. // If the newly added enum has validation rules associated with it
  3641. // consider leaving a TODO and/or creating an issue.
  3642. switch (label) {
  3643. case SpvBuiltInClipDistance:
  3644. case SpvBuiltInCullDistance: {
  3645. return ValidateClipOrCullDistanceAtDefinition(decoration, inst);
  3646. }
  3647. case SpvBuiltInFragCoord: {
  3648. return ValidateFragCoordAtDefinition(decoration, inst);
  3649. }
  3650. case SpvBuiltInFragDepth: {
  3651. return ValidateFragDepthAtDefinition(decoration, inst);
  3652. }
  3653. case SpvBuiltInFrontFacing: {
  3654. return ValidateFrontFacingAtDefinition(decoration, inst);
  3655. }
  3656. case SpvBuiltInGlobalInvocationId:
  3657. case SpvBuiltInLocalInvocationId:
  3658. case SpvBuiltInNumWorkgroups:
  3659. case SpvBuiltInWorkgroupId: {
  3660. return ValidateComputeShaderI32Vec3InputAtDefinition(decoration, inst);
  3661. }
  3662. case SpvBuiltInHelperInvocation: {
  3663. return ValidateHelperInvocationAtDefinition(decoration, inst);
  3664. }
  3665. case SpvBuiltInInvocationId: {
  3666. return ValidateInvocationIdAtDefinition(decoration, inst);
  3667. }
  3668. case SpvBuiltInInstanceIndex: {
  3669. return ValidateInstanceIndexAtDefinition(decoration, inst);
  3670. }
  3671. case SpvBuiltInLayer:
  3672. case SpvBuiltInViewportIndex: {
  3673. return ValidateLayerOrViewportIndexAtDefinition(decoration, inst);
  3674. }
  3675. case SpvBuiltInPatchVertices: {
  3676. return ValidatePatchVerticesAtDefinition(decoration, inst);
  3677. }
  3678. case SpvBuiltInPointCoord: {
  3679. return ValidatePointCoordAtDefinition(decoration, inst);
  3680. }
  3681. case SpvBuiltInPointSize: {
  3682. return ValidatePointSizeAtDefinition(decoration, inst);
  3683. }
  3684. case SpvBuiltInPosition: {
  3685. return ValidatePositionAtDefinition(decoration, inst);
  3686. }
  3687. case SpvBuiltInPrimitiveId: {
  3688. return ValidatePrimitiveIdAtDefinition(decoration, inst);
  3689. }
  3690. case SpvBuiltInSampleId: {
  3691. return ValidateSampleIdAtDefinition(decoration, inst);
  3692. }
  3693. case SpvBuiltInSampleMask: {
  3694. return ValidateSampleMaskAtDefinition(decoration, inst);
  3695. }
  3696. case SpvBuiltInSamplePosition: {
  3697. return ValidateSamplePositionAtDefinition(decoration, inst);
  3698. }
  3699. case SpvBuiltInSubgroupId:
  3700. case SpvBuiltInNumSubgroups: {
  3701. return ValidateComputeI32InputAtDefinition(decoration, inst);
  3702. }
  3703. case SpvBuiltInSubgroupLocalInvocationId:
  3704. case SpvBuiltInSubgroupSize: {
  3705. return ValidateI32InputAtDefinition(decoration, inst);
  3706. }
  3707. case SpvBuiltInSubgroupEqMask:
  3708. case SpvBuiltInSubgroupGeMask:
  3709. case SpvBuiltInSubgroupGtMask:
  3710. case SpvBuiltInSubgroupLeMask:
  3711. case SpvBuiltInSubgroupLtMask: {
  3712. return ValidateI32Vec4InputAtDefinition(decoration, inst);
  3713. }
  3714. case SpvBuiltInTessCoord: {
  3715. return ValidateTessCoordAtDefinition(decoration, inst);
  3716. }
  3717. case SpvBuiltInTessLevelOuter: {
  3718. return ValidateTessLevelOuterAtDefinition(decoration, inst);
  3719. }
  3720. case SpvBuiltInTessLevelInner: {
  3721. return ValidateTessLevelInnerAtDefinition(decoration, inst);
  3722. }
  3723. case SpvBuiltInVertexIndex: {
  3724. return ValidateVertexIndexAtDefinition(decoration, inst);
  3725. }
  3726. case SpvBuiltInWorkgroupSize: {
  3727. return ValidateWorkgroupSizeAtDefinition(decoration, inst);
  3728. }
  3729. case SpvBuiltInVertexId: {
  3730. return ValidateVertexIdAtDefinition(decoration, inst);
  3731. }
  3732. case SpvBuiltInLocalInvocationIndex: {
  3733. return ValidateLocalInvocationIndexAtDefinition(decoration, inst);
  3734. }
  3735. case SpvBuiltInWarpsPerSMNV:
  3736. case SpvBuiltInSMCountNV:
  3737. case SpvBuiltInWarpIDNV:
  3738. case SpvBuiltInSMIDNV: {
  3739. return ValidateSMBuiltinsAtDefinition(decoration, inst);
  3740. }
  3741. case SpvBuiltInBaseInstance:
  3742. case SpvBuiltInBaseVertex: {
  3743. return ValidateBaseInstanceOrVertexAtDefinition(decoration, inst);
  3744. }
  3745. case SpvBuiltInDrawIndex: {
  3746. return ValidateDrawIndexAtDefinition(decoration, inst);
  3747. }
  3748. case SpvBuiltInViewIndex: {
  3749. return ValidateViewIndexAtDefinition(decoration, inst);
  3750. }
  3751. case SpvBuiltInDeviceIndex: {
  3752. return ValidateDeviceIndexAtDefinition(decoration, inst);
  3753. }
  3754. case SpvBuiltInFragInvocationCountEXT: {
  3755. // alias SpvBuiltInInvocationsPerPixelNV
  3756. return ValidateFragInvocationCountAtDefinition(decoration, inst);
  3757. }
  3758. case SpvBuiltInFragSizeEXT: {
  3759. // alias SpvBuiltInFragmentSizeNV
  3760. return ValidateFragSizeAtDefinition(decoration, inst);
  3761. }
  3762. case SpvBuiltInFragStencilRefEXT: {
  3763. return ValidateFragStencilRefAtDefinition(decoration, inst);
  3764. }
  3765. case SpvBuiltInFullyCoveredEXT:{
  3766. return ValidateFullyCoveredAtDefinition(decoration, inst);
  3767. }
  3768. // Ray tracing builtins
  3769. case SpvBuiltInHitKindKHR: // alias SpvBuiltInHitKindNV
  3770. case SpvBuiltInHitTNV: // NOT present in KHR
  3771. case SpvBuiltInInstanceId:
  3772. case SpvBuiltInLaunchIdKHR: // alias SpvBuiltInLaunchIdNV
  3773. case SpvBuiltInLaunchSizeKHR: // alias SpvBuiltInLaunchSizeNV
  3774. case SpvBuiltInWorldRayOriginKHR: // alias SpvBuiltInWorldRayOriginNV
  3775. case SpvBuiltInWorldRayDirectionKHR: // alias SpvBuiltInWorldRayDirectionNV
  3776. case SpvBuiltInObjectRayOriginKHR: // alias SpvBuiltInObjectRayOriginNV
  3777. case SpvBuiltInObjectRayDirectionKHR: // alias
  3778. // SpvBuiltInObjectRayDirectionNV
  3779. case SpvBuiltInRayTminKHR: // alias SpvBuiltInRayTminNV
  3780. case SpvBuiltInRayTmaxKHR: // alias SpvBuiltInRayTmaxNV
  3781. case SpvBuiltInInstanceCustomIndexKHR: // alias
  3782. // SpvBuiltInInstanceCustomIndexNV
  3783. case SpvBuiltInObjectToWorldKHR: // alias SpvBuiltInObjectToWorldNV
  3784. case SpvBuiltInWorldToObjectKHR: // alias SpvBuiltInWorldToObjectNV
  3785. case SpvBuiltInIncomingRayFlagsKHR: // alias SpvBuiltInIncomingRayFlagsNV
  3786. case SpvBuiltInRayGeometryIndexKHR: { // NOT present in NV
  3787. return ValidateRayTracingBuiltinsAtDefinition(decoration, inst);
  3788. }
  3789. case SpvBuiltInWorkDim:
  3790. case SpvBuiltInGlobalSize:
  3791. case SpvBuiltInEnqueuedWorkgroupSize:
  3792. case SpvBuiltInGlobalOffset:
  3793. case SpvBuiltInGlobalLinearId:
  3794. case SpvBuiltInSubgroupMaxSize:
  3795. case SpvBuiltInNumEnqueuedSubgroups:
  3796. case SpvBuiltInBaryCoordNoPerspAMD:
  3797. case SpvBuiltInBaryCoordNoPerspCentroidAMD:
  3798. case SpvBuiltInBaryCoordNoPerspSampleAMD:
  3799. case SpvBuiltInBaryCoordSmoothAMD:
  3800. case SpvBuiltInBaryCoordSmoothCentroidAMD:
  3801. case SpvBuiltInBaryCoordSmoothSampleAMD:
  3802. case SpvBuiltInBaryCoordPullModelAMD:
  3803. case SpvBuiltInViewportMaskNV:
  3804. case SpvBuiltInSecondaryPositionNV:
  3805. case SpvBuiltInSecondaryViewportMaskNV:
  3806. case SpvBuiltInPositionPerViewNV:
  3807. case SpvBuiltInViewportMaskPerViewNV:
  3808. case SpvBuiltInMax:
  3809. case SpvBuiltInTaskCountNV:
  3810. case SpvBuiltInPrimitiveCountNV:
  3811. case SpvBuiltInPrimitiveIndicesNV:
  3812. case SpvBuiltInClipDistancePerViewNV:
  3813. case SpvBuiltInCullDistancePerViewNV:
  3814. case SpvBuiltInLayerPerViewNV:
  3815. case SpvBuiltInMeshViewCountNV:
  3816. case SpvBuiltInMeshViewIndicesNV:
  3817. case SpvBuiltInBaryCoordNV:
  3818. case SpvBuiltInBaryCoordNoPerspNV:
  3819. case SpvBuiltInCurrentRayTimeNV:
  3820. // No validation rules (for the moment).
  3821. break;
  3822. case SpvBuiltInPrimitiveShadingRateKHR: {
  3823. return ValidatePrimitiveShadingRateAtDefinition(decoration, inst);
  3824. }
  3825. case SpvBuiltInShadingRateKHR: {
  3826. return ValidateShadingRateAtDefinition(decoration, inst);
  3827. }
  3828. }
  3829. return SPV_SUCCESS;
  3830. }
  3831. spv_result_t BuiltInsValidator::ValidateBuiltInsAtDefinition() {
  3832. for (const auto& kv : _.id_decorations()) {
  3833. const uint32_t id = kv.first;
  3834. const auto& decorations = kv.second;
  3835. if (decorations.empty()) {
  3836. continue;
  3837. }
  3838. const Instruction* inst = _.FindDef(id);
  3839. assert(inst);
  3840. for (const auto& decoration : kv.second) {
  3841. if (decoration.dec_type() != SpvDecorationBuiltIn) {
  3842. continue;
  3843. }
  3844. if (spv_result_t error =
  3845. ValidateSingleBuiltInAtDefinition(decoration, *inst)) {
  3846. return error;
  3847. }
  3848. }
  3849. }
  3850. return SPV_SUCCESS;
  3851. }
  3852. spv_result_t BuiltInsValidator::Run() {
  3853. // First pass: validate all built-ins at definition and seed
  3854. // id_to_at_reference_checks_ with built-ins.
  3855. if (auto error = ValidateBuiltInsAtDefinition()) {
  3856. return error;
  3857. }
  3858. if (id_to_at_reference_checks_.empty()) {
  3859. // No validation tasks were seeded. Nothing else to do.
  3860. return SPV_SUCCESS;
  3861. }
  3862. // Second pass: validate every id reference in the module using
  3863. // rules in id_to_at_reference_checks_.
  3864. for (const Instruction& inst : _.ordered_instructions()) {
  3865. Update(inst);
  3866. std::set<uint32_t> already_checked;
  3867. for (const auto& operand : inst.operands()) {
  3868. if (!spvIsIdType(operand.type)) {
  3869. // Not id.
  3870. continue;
  3871. }
  3872. const uint32_t id = inst.word(operand.offset);
  3873. if (id == inst.id()) {
  3874. // No need to check result id.
  3875. continue;
  3876. }
  3877. if (!already_checked.insert(id).second) {
  3878. // The instruction has already referenced this id.
  3879. continue;
  3880. }
  3881. // Instruction references the id. Run all checks associated with the id
  3882. // on the instruction. id_to_at_reference_checks_ can be modified in the
  3883. // process, iterators are safe because it's a tree-based map.
  3884. const auto it = id_to_at_reference_checks_.find(id);
  3885. if (it != id_to_at_reference_checks_.end()) {
  3886. for (const auto& check : it->second) {
  3887. if (spv_result_t error = check(inst)) {
  3888. return error;
  3889. }
  3890. }
  3891. }
  3892. }
  3893. }
  3894. return SPV_SUCCESS;
  3895. }
  3896. } // namespace
  3897. // Validates correctness of built-in variables.
  3898. spv_result_t ValidateBuiltIns(ValidationState_t& _) {
  3899. BuiltInsValidator validator(_);
  3900. return validator.Run();
  3901. }
  3902. } // namespace val
  3903. } // namespace spvtools