inst_bindless_check_test.cpp 225 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619
  1. // Copyright (c) 2017-2022 Valve Corporation
  2. // Copyright (c) 2017-2022 LunarG Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // Bindless Check Instrumentation Tests.
  16. #include <string>
  17. #include <vector>
  18. #include "test/opt/assembly_builder.h"
  19. #include "test/opt/pass_fixture.h"
  20. #include "test/opt/pass_utils.h"
  21. namespace spvtools {
  22. namespace opt {
  23. namespace {
  24. using InstBindlessTest = PassTest<::testing::Test>;
  25. static const std::string kOutputDecorations = R"(
  26. ; CHECK: OpDecorate [[output_buffer_type:%inst_bindless_OutputBuffer]] Block
  27. ; CHECK: OpMemberDecorate [[output_buffer_type]] 0 Offset 0
  28. ; CHECK: OpMemberDecorate [[output_buffer_type]] 1 Offset 4
  29. ; CHECK: OpDecorate [[output_buffer_var:%\w+]] DescriptorSet 7
  30. ; CHECK: OpDecorate [[output_buffer_var]] Binding 0
  31. )";
  32. static const std::string kOutputGlobals = R"(
  33. ; CHECK: [[output_buffer_type]] = OpTypeStruct %uint %uint %_runtimearr_uint
  34. ; CHECK: [[output_ptr_type:%\w+]] = OpTypePointer StorageBuffer [[output_buffer_type]]
  35. ; CHECK: [[output_buffer_var]] = OpVariable [[output_ptr_type]] StorageBuffer
  36. )";
  37. static const std::string kStreamWrite4Begin = R"(
  38. ; CHECK: %inst_bindless_stream_write_4 = OpFunction %void None {{%\w+}}
  39. ; CHECK: [[param_1:%\w+]] = OpFunctionParameter %uint
  40. ; CHECK: [[param_2:%\w+]] = OpFunctionParameter %uint
  41. ; CHECK: [[param_3:%\w+]] = OpFunctionParameter %uint
  42. ; CHECK: [[param_4:%\w+]] = OpFunctionParameter %uint
  43. ; CHECK: {{%\w+}} = OpLabel
  44. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_1
  45. ; CHECK: {{%\w+}} = OpAtomicIAdd %uint {{%\w+}} %uint_4 %uint_0 %uint_10
  46. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_10
  47. ; CHECK: {{%\w+}} = OpArrayLength %uint [[output_buffer_var]] 2
  48. ; CHECK: {{%\w+}} = OpULessThanEqual %bool {{%\w+}} {{%\w+}}
  49. ; CHECK: OpSelectionMerge {{%\w+}} None
  50. ; CHECK: OpBranchConditional {{%\w+}} {{%\w+}} {{%\w+}}
  51. ; CHECK: {{%\w+}} = OpLabel
  52. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_0
  53. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  54. ; CHECK: OpStore {{%\w+}} %uint_10
  55. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_1
  56. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  57. ; CHECK: OpStore {{%\w+}} %uint_23
  58. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_2
  59. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  60. ; CHECK: OpStore {{%\w+}} [[param_1]]
  61. )";
  62. static const std::string kStreamWrite4End = R"(
  63. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_7
  64. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  65. ; CHECK: OpStore {{%\w+}} [[param_2]]
  66. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_8
  67. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  68. ; CHECK: OpStore {{%\w+}} [[param_3]]
  69. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_9
  70. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  71. ; CHECK: OpStore {{%\w+}} [[param_4]]
  72. ; CHECK: OpBranch {{%\w+}}
  73. ; CHECK: {{%\w+}} = OpLabel
  74. ; CHECK: OpReturn
  75. ; CHECK: OpFunctionEnd
  76. )";
  77. // clang-format off
  78. static const std::string kStreamWrite4Frag = kStreamWrite4Begin + R"(
  79. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_3
  80. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  81. ; CHECK: OpStore {{%\w+}} %uint_4
  82. ; CHECK: {{%\w+}} = OpLoad %v4float %gl_FragCoord
  83. ; CHECK: {{%\w+}} = OpBitcast %v4uint {{%\w+}}
  84. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 0
  85. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_4
  86. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  87. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  88. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 1
  89. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_5
  90. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  91. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  92. )" + kStreamWrite4End;
  93. static const std::string kStreamWrite4Tese = kStreamWrite4Begin + R"(
  94. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_3
  95. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  96. ; CHECK: OpStore {{%\w+}} %uint_2
  97. ; CHECK: {{%\w+}} = OpLoad %uint %gl_PrimitiveID
  98. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_4
  99. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  100. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  101. ; CHECK: {{%\w+}} = OpLoad %v3float %gl_TessCoord
  102. ; CHECK: {{%\w+}} = OpBitcast %v3uint {{%\w+}}
  103. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 0
  104. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 1
  105. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_5
  106. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  107. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  108. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_6
  109. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  110. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  111. )" + kStreamWrite4End;
  112. static const std::string kStreamWrite4Vert = kStreamWrite4Begin + R"(
  113. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_3
  114. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  115. ; CHECK: OpStore {{%\w+}} %uint_0
  116. ; CHECK: {{%\w+}} = OpLoad %uint %gl_VertexIndex
  117. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_4
  118. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  119. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  120. ; CHECK: {{%\w+}} = OpLoad %uint %gl_InstanceIndex
  121. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_5
  122. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  123. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  124. )" + kStreamWrite4End;
  125. static const std::string kStreamWrite4Compute = kStreamWrite4Begin + R"(
  126. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_3
  127. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  128. ; CHECK: OpStore {{%\w+}} %uint_5
  129. ; CHECK: {{%\w+}} = OpLoad %v3uint %gl_GlobalInvocationID
  130. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 0
  131. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 1
  132. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 2
  133. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_4
  134. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  135. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  136. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_5
  137. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  138. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  139. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_6
  140. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  141. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  142. )" + kStreamWrite4End;
  143. static const std::string kStreamWrite4Ray = kStreamWrite4Begin + R"(
  144. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_3
  145. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  146. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  147. ; CHECK: {{%\w+}} = OpLoad %v3uint {{%\w+}}
  148. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 0
  149. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 1
  150. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 2
  151. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_4
  152. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  153. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  154. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_5
  155. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  156. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  157. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_6
  158. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  159. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  160. )" + kStreamWrite4End;
  161. // clang-format on
  162. static const std::string kStreamWrite5Begin = R"(
  163. ; CHECK: %inst_bindless_stream_write_5 = OpFunction %void None {{%\w+}}
  164. ; CHECK: [[param_1:%\w+]] = OpFunctionParameter %uint
  165. ; CHECK: [[param_2:%\w+]] = OpFunctionParameter %uint
  166. ; CHECK: [[param_3:%\w+]] = OpFunctionParameter %uint
  167. ; CHECK: [[param_4:%\w+]] = OpFunctionParameter %uint
  168. ; CHECK: [[param_5:%\w+]] = OpFunctionParameter %uint
  169. ; CHECK: {{%\w+}} = OpLabel
  170. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_1
  171. ; CHECK: {{%\w+}} = OpAtomicIAdd %uint {{%\w+}} %uint_4 %uint_0 %uint_11
  172. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_11
  173. ; CHECK: {{%\w+}} = OpArrayLength %uint [[output_buffer_var]] 2
  174. ; CHECK: {{%\w+}} = OpULessThanEqual %bool {{%\w+}} {{%\w+}}
  175. ; CHECK: OpSelectionMerge {{%\w+}} None
  176. ; CHECK: OpBranchConditional {{%\w+}} {{%\w+}} {{%\w+}}
  177. ; CHECK: {{%\w+}} = OpLabel
  178. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_0
  179. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  180. ; CHECK: OpStore {{%\w+}} %uint_11
  181. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_1
  182. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  183. ; CHECK: OpStore {{%\w+}} %uint_23
  184. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_2
  185. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  186. ; CHECK: OpStore {{%\w+}} [[param_1]]
  187. )";
  188. static const std::string kStreamWrite5End = R"(
  189. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_7
  190. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  191. ; CHECK: OpStore {{%\w+}} [[param_2]]
  192. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_8
  193. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  194. ; CHECK: OpStore {{%\w+}} [[param_3]]
  195. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_9
  196. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  197. ; CHECK: OpStore {{%\w+}} [[param_4]]
  198. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_10
  199. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  200. ; CHECK: OpStore {{%\w+}} [[param_5]]
  201. ; CHECK: OpBranch {{%\w+}}
  202. ; CHECK: {{%\w+}} = OpLabel
  203. ; CHECK: OpReturn
  204. ; CHECK: OpFunctionEnd
  205. )";
  206. // clang-format off
  207. static const std::string kStreamWrite5Frag = kStreamWrite5Begin + R"(
  208. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_3
  209. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  210. ; CHECK: OpStore {{%\w+}} %uint_4
  211. ; CHECK: {{%\w+}} = OpLoad %v4float %gl_FragCoord
  212. ; CHECK: {{%\w+}} = OpBitcast %v4uint {{%\w+}}
  213. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 0
  214. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_4
  215. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  216. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  217. ; CHECK: {{%\w+}} = OpCompositeExtract %uint {{%\w+}} 1
  218. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_5
  219. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  220. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  221. )" + kStreamWrite4End;
  222. static const std::string kStreamWrite5Vert = kStreamWrite5Begin + R"(
  223. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_3
  224. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  225. ; CHECK: OpStore {{%\w+}} %uint_0
  226. ; CHECK: {{%\w+}} = OpLoad %uint %gl_VertexIndex
  227. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_4
  228. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  229. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  230. ; CHECK: {{%\w+}} = OpLoad %uint %gl_InstanceIndex
  231. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} %uint_5
  232. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[output_buffer_var]] %uint_2 {{%\w+}}
  233. ; CHECK: OpStore {{%\w+}} {{%\w+}}
  234. )" + kStreamWrite5End;
  235. // clang-format on
  236. static const std::string kInputDecorations = R"(
  237. ; CHECK: OpDecorate [[input_buffer_type:%inst_bindless_InputBuffer]] Block
  238. ; CHECK: OpMemberDecorate [[input_buffer_type]] 0 Offset 0
  239. ; CHECK: OpDecorate [[input_buffer_var:%\w+]] DescriptorSet 7
  240. ; CHECK: OpDecorate [[input_buffer_var]] Binding 1
  241. )";
  242. static const std::string kInputGlobals = R"(
  243. ; CHECK: [[input_buffer_type]] = OpTypeStruct %_runtimearr_uint
  244. ; CHECK: [[input_ptr_type:%\w+]] = OpTypePointer StorageBuffer [[input_buffer_type]]
  245. ; CHECK: [[input_buffer_var]] = OpVariable [[input_ptr_type]] StorageBuffer
  246. )";
  247. static const std::string kDirectRead2 = R"(
  248. ; CHECK: %inst_bindless_direct_read_2 = OpFunction %uint None {{%\w+}}
  249. ; CHECK: [[param_1:%\w+]] = OpFunctionParameter %uint
  250. ; CHECK: [[param_2:%\w+]] = OpFunctionParameter %uint
  251. ; CHECK: {{%\w+}} = OpLabel
  252. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 [[param_1]]
  253. ; CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  254. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} [[param_2]]
  255. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 {{%\w+}}
  256. ; CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  257. ; CHECK: OpReturnValue {{%\w+}}
  258. ; CHECK: OpFunctionEnd
  259. )";
  260. static const std::string kDirectRead3 = R"(
  261. ;CHECK: %inst_bindless_direct_read_3 = OpFunction %uint None {{%\w+}}
  262. ; CHECK: [[param_1:%\w+]] = OpFunctionParameter %uint
  263. ; CHECK: [[param_2:%\w+]] = OpFunctionParameter %uint
  264. ; CHECK: [[param_3:%\w+]] = OpFunctionParameter %uint
  265. ;CHECK: {{%\w+}} = OpLabel
  266. ;CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 [[param_1]]
  267. ;CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  268. ;CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} [[param_2]]
  269. ;CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 {{%\w+}}
  270. ;CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  271. ;CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} [[param_3]]
  272. ;CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 {{%\w+}}
  273. ;CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  274. ;CHECK: OpReturnValue {{%\w+}}
  275. ;CHECK: OpFunctionEnd
  276. )";
  277. static const std::string kDirectRead4 = R"(
  278. ; CHECK: %inst_bindless_direct_read_4 = OpFunction %uint None {{%\w+}}
  279. ; CHECK: [[param_1:%\w+]] = OpFunctionParameter %uint
  280. ; CHECK: [[param_2:%\w+]] = OpFunctionParameter %uint
  281. ; CHECK: [[param_3:%\w+]] = OpFunctionParameter %uint
  282. ; CHECK: [[param_4:%\w+]] = OpFunctionParameter %uint
  283. ; CHECK: {{%\w+}} = OpLabel
  284. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 [[param_1]]
  285. ; CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  286. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} [[param_2]]
  287. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 {{%\w+}}
  288. ; CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  289. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} [[param_3]]
  290. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 {{%\w+}}
  291. ; CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  292. ; CHECK: {{%\w+}} = OpIAdd %uint {{%\w+}} [[param_4]]
  293. ; CHECK: {{%\w+}} = OpAccessChain %_ptr_StorageBuffer_uint [[input_buffer_var]] %uint_0 {{%\w+}}
  294. ; CHECK: {{%\w+}} = OpLoad %uint {{%\w+}}
  295. ; CHECK: OpReturnValue {{%\w+}}
  296. ; CHECK: OpFunctionEnd
  297. )";
  298. TEST_F(InstBindlessTest, NoInstrumentConstIndexInbounds) {
  299. // Texture2D g_tColor[128];
  300. //
  301. // SamplerState g_sAniso;
  302. //
  303. // struct PS_INPUT
  304. // {
  305. // float2 vTextureCoords : TEXCOORD2;
  306. // };
  307. //
  308. // struct PS_OUTPUT
  309. // {
  310. // float4 vColor : SV_Target0;
  311. // };
  312. //
  313. // PS_OUTPUT MainPs(PS_INPUT i)
  314. // {
  315. // PS_OUTPUT ps_output;
  316. //
  317. // ps_output.vColor = g_tColor[ 37 ].Sample(g_sAniso, i.vTextureCoords.xy);
  318. // return ps_output;
  319. // }
  320. const std::string before =
  321. R"(OpCapability Shader
  322. %1 = OpExtInstImport "GLSL.std.450"
  323. OpMemoryModel Logical GLSL450
  324. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  325. OpExecutionMode %MainPs OriginUpperLeft
  326. OpSource HLSL 500
  327. OpName %MainPs "MainPs"
  328. OpName %g_tColor "g_tColor"
  329. OpName %g_sAniso "g_sAniso"
  330. OpName %i_vTextureCoords "i.vTextureCoords"
  331. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  332. OpDecorate %g_tColor DescriptorSet 3
  333. OpDecorate %g_tColor Binding 0
  334. OpDecorate %g_sAniso DescriptorSet 0
  335. OpDecorate %i_vTextureCoords Location 0
  336. OpDecorate %_entryPointOutput_vColor Location 0
  337. %void = OpTypeVoid
  338. %8 = OpTypeFunction %void
  339. %float = OpTypeFloat 32
  340. %v2float = OpTypeVector %float 2
  341. %v4float = OpTypeVector %float 4
  342. %int = OpTypeInt 32 1
  343. %int_0 = OpConstant %int 0
  344. %int_37 = OpConstant %int 37
  345. %15 = OpTypeImage %float 2D 0 0 0 1 Unknown
  346. %uint = OpTypeInt 32 0
  347. %uint_128 = OpConstant %uint 128
  348. %_arr_15_uint_128 = OpTypeArray %15 %uint_128
  349. %_ptr_UniformConstant__arr_15_uint_128 = OpTypePointer UniformConstant %_arr_15_uint_128
  350. %g_tColor = OpVariable %_ptr_UniformConstant__arr_15_uint_128 UniformConstant
  351. %_ptr_UniformConstant_15 = OpTypePointer UniformConstant %15
  352. %21 = OpTypeSampler
  353. %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
  354. %g_sAniso = OpVariable %_ptr_UniformConstant_21 UniformConstant
  355. %23 = OpTypeSampledImage %15
  356. %_ptr_Input_v2float = OpTypePointer Input %v2float
  357. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  358. %_ptr_Output_v4float = OpTypePointer Output %v4float
  359. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  360. %MainPs = OpFunction %void None %8
  361. %26 = OpLabel
  362. %27 = OpLoad %v2float %i_vTextureCoords
  363. %28 = OpAccessChain %_ptr_UniformConstant_15 %g_tColor %int_37
  364. %29 = OpLoad %15 %28
  365. %30 = OpLoad %21 %g_sAniso
  366. %31 = OpSampledImage %23 %29 %30
  367. %32 = OpImageSampleImplicitLod %v4float %31 %27
  368. OpStore %_entryPointOutput_vColor %32
  369. OpReturn
  370. OpFunctionEnd
  371. )";
  372. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  373. SinglePassRunAndCheck<InstBindlessCheckPass>(
  374. before, before, true, true, 7u, 23u, false, false, false, false, false);
  375. }
  376. TEST_F(InstBindlessTest, NoInstrumentNonBindless) {
  377. // This test verifies that the pass will correctly not instrument vanilla
  378. // texture sample.
  379. //
  380. // Texture2D g_tColor;
  381. //
  382. // SamplerState g_sAniso;
  383. //
  384. // struct PS_INPUT
  385. // {
  386. // float2 vTextureCoords : TEXCOORD2;
  387. // };
  388. //
  389. // struct PS_OUTPUT
  390. // {
  391. // float4 vColor : SV_Target0;
  392. // };
  393. //
  394. // PS_OUTPUT MainPs(PS_INPUT i)
  395. // {
  396. // PS_OUTPUT ps_output;
  397. // ps_output.vColor =
  398. // g_tColor.Sample(g_sAniso, i.vTextureCoords.xy);
  399. // return ps_output;
  400. // }
  401. const std::string whole_file =
  402. R"(OpCapability Shader
  403. %1 = OpExtInstImport "GLSL.std.450"
  404. OpMemoryModel Logical GLSL450
  405. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  406. OpExecutionMode %MainPs OriginUpperLeft
  407. OpSource HLSL 500
  408. OpName %MainPs "MainPs"
  409. OpName %g_tColor "g_tColor"
  410. OpName %g_sAniso "g_sAniso"
  411. OpName %i_vTextureCoords "i.vTextureCoords"
  412. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  413. OpDecorate %g_tColor DescriptorSet 0
  414. OpDecorate %g_tColor Binding 0
  415. OpDecorate %g_sAniso DescriptorSet 0
  416. OpDecorate %g_sAniso Binding 0
  417. OpDecorate %i_vTextureCoords Location 0
  418. OpDecorate %_entryPointOutput_vColor Location 0
  419. %void = OpTypeVoid
  420. %8 = OpTypeFunction %void
  421. %float = OpTypeFloat 32
  422. %v2float = OpTypeVector %float 2
  423. %v4float = OpTypeVector %float 4
  424. %12 = OpTypeImage %float 2D 0 0 0 1 Unknown
  425. %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
  426. %g_tColor = OpVariable %_ptr_UniformConstant_12 UniformConstant
  427. %14 = OpTypeSampler
  428. %_ptr_UniformConstant_14 = OpTypePointer UniformConstant %14
  429. %g_sAniso = OpVariable %_ptr_UniformConstant_14 UniformConstant
  430. %16 = OpTypeSampledImage %12
  431. %_ptr_Input_v2float = OpTypePointer Input %v2float
  432. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  433. %_ptr_Output_v4float = OpTypePointer Output %v4float
  434. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  435. %MainPs = OpFunction %void None %8
  436. %19 = OpLabel
  437. %20 = OpLoad %v2float %i_vTextureCoords
  438. %21 = OpLoad %12 %g_tColor
  439. %22 = OpLoad %14 %g_sAniso
  440. %23 = OpSampledImage %16 %21 %22
  441. %24 = OpImageSampleImplicitLod %v4float %23 %20
  442. OpStore %_entryPointOutput_vColor %24
  443. OpReturn
  444. OpFunctionEnd
  445. )";
  446. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  447. SinglePassRunAndCheck<InstBindlessCheckPass>(whole_file, whole_file, true,
  448. true, 7u, 23u, false, false,
  449. false, false, false);
  450. }
  451. TEST_F(InstBindlessTest, Simple) {
  452. // Texture2D g_tColor[128];
  453. //
  454. // layout(push_constant) cbuffer PerViewConstantBuffer_t
  455. // {
  456. // uint g_nDataIdx;
  457. // };
  458. //
  459. // SamplerState g_sAniso;
  460. //
  461. // struct PS_INPUT
  462. // {
  463. // float2 vTextureCoords : TEXCOORD2;
  464. // };
  465. //
  466. // struct PS_OUTPUT
  467. // {
  468. // float4 vColor : SV_Target0;
  469. // };
  470. //
  471. // PS_OUTPUT MainPs(PS_INPUT i)
  472. // {
  473. // PS_OUTPUT ps_output;
  474. // ps_output.vColor =
  475. // g_tColor[ g_nDataIdx ].Sample(g_sAniso, i.vTextureCoords.xy);
  476. // return ps_output;
  477. // }
  478. const std::string entry = R"(
  479. OpCapability Shader
  480. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  481. %1 = OpExtInstImport "GLSL.std.450"
  482. OpMemoryModel Logical GLSL450
  483. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  484. ; CHECK: OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
  485. OpExecutionMode %MainPs OriginUpperLeft
  486. OpSource HLSL 500
  487. )";
  488. // clang-format off
  489. const std::string names_annots = R"(
  490. OpName %MainPs "MainPs"
  491. OpName %g_tColor "g_tColor"
  492. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  493. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  494. OpName %_ ""
  495. OpName %g_sAniso "g_sAniso"
  496. OpName %i_vTextureCoords "i.vTextureCoords"
  497. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  498. OpDecorate %g_tColor DescriptorSet 3
  499. OpDecorate %g_tColor Binding 0
  500. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  501. OpDecorate %PerViewConstantBuffer_t Block
  502. OpDecorate %g_sAniso DescriptorSet 0
  503. OpDecorate %i_vTextureCoords Location 0
  504. OpDecorate %_entryPointOutput_vColor Location 0
  505. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  506. )" + kOutputDecorations + R"(
  507. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  508. )";
  509. const std::string consts_types_vars = R"(
  510. %void = OpTypeVoid
  511. %10 = OpTypeFunction %void
  512. %float = OpTypeFloat 32
  513. %v2float = OpTypeVector %float 2
  514. %v4float = OpTypeVector %float 4
  515. %int = OpTypeInt 32 1
  516. %int_0 = OpConstant %int 0
  517. %16 = OpTypeImage %float 2D 0 0 0 1 Unknown
  518. %uint = OpTypeInt 32 0
  519. %uint_128 = OpConstant %uint 128
  520. %_arr_16_uint_128 = OpTypeArray %16 %uint_128
  521. %_ptr_UniformConstant__arr_16_uint_128 = OpTypePointer UniformConstant %_arr_16_uint_128
  522. %g_tColor = OpVariable %_ptr_UniformConstant__arr_16_uint_128 UniformConstant
  523. %PerViewConstantBuffer_t = OpTypeStruct %uint
  524. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  525. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  526. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  527. %_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
  528. %24 = OpTypeSampler
  529. %_ptr_UniformConstant_24 = OpTypePointer UniformConstant %24
  530. %g_sAniso = OpVariable %_ptr_UniformConstant_24 UniformConstant
  531. %26 = OpTypeSampledImage %16
  532. %_ptr_Input_v2float = OpTypePointer Input %v2float
  533. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  534. %_ptr_Output_v4float = OpTypePointer Output %v4float
  535. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  536. ; CHECK: %bool = OpTypeBool
  537. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  538. )" + kOutputGlobals + R"(
  539. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  540. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  541. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  542. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  543. )";
  544. // clang-format on
  545. const std::string main_func = R"(
  546. %MainPs = OpFunction %void None %10
  547. %29 = OpLabel
  548. %30 = OpLoad %v2float %i_vTextureCoords
  549. %31 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  550. %32 = OpLoad %uint %31
  551. %33 = OpAccessChain %_ptr_UniformConstant_16 %g_tColor %32
  552. %34 = OpLoad %16 %33
  553. %35 = OpLoad %24 %g_sAniso
  554. %36 = OpSampledImage %26 %34 %35
  555. %37 = OpImageSampleImplicitLod %v4float %36 %30
  556. OpStore %_entryPointOutput_vColor %37
  557. ; CHECK-NOT: %37 = OpImageSampleImplicitLod %v4float %36 %30
  558. ; CHECK-NOT: OpStore %_entryPointOutput_vColor %37
  559. ; CHECK: %40 = OpULessThan %bool %32 %uint_128
  560. ; CHECK: OpSelectionMerge %41 None
  561. ; CHECK: OpBranchConditional %40 %42 %43
  562. ; CHECK: %42 = OpLabel
  563. ; CHECK: %44 = OpLoad %16 %33
  564. ; CHECK: %45 = OpSampledImage %26 %44 %35
  565. ; CHECK: %46 = OpImageSampleImplicitLod %v4float %45 %30
  566. ; CHECK: OpBranch %41
  567. ; CHECK: %43 = OpLabel
  568. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_56 %uint_0 %32 %uint_128
  569. ; CHECK: OpBranch %41
  570. ; CHECK: %41 = OpLabel
  571. ; CHECK: %104 = OpPhi %v4float %46 %42 [[null_v4float]] %43
  572. ; CHECK: OpStore %_entryPointOutput_vColor %104
  573. OpReturn
  574. OpFunctionEnd
  575. )";
  576. const std::string output_func = kStreamWrite4Frag;
  577. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  578. SinglePassRunAndMatch<InstBindlessCheckPass, uint32_t, uint32_t, bool, bool>(
  579. entry + names_annots + consts_types_vars + main_func + output_func, true,
  580. 7u, 23u, false, false, false, false, false);
  581. }
  582. TEST_F(InstBindlessTest, InstrumentMultipleInstructions) {
  583. // Texture2D g_tColor[128];
  584. //
  585. // layout(push_constant) cbuffer PerViewConstantBuffer_t
  586. // {
  587. // uint g_nDataIdx;
  588. // uint g_nDataIdx2;
  589. // };
  590. //
  591. // SamplerState g_sAniso;
  592. //
  593. // struct PS_INPUT
  594. // {
  595. // float2 vTextureCoords : TEXCOORD2;
  596. // };
  597. //
  598. // struct PS_OUTPUT
  599. // {
  600. // float4 vColor : SV_Target0;
  601. // };
  602. //
  603. // PS_OUTPUT MainPs(PS_INPUT i)
  604. // {
  605. // PS_OUTPUT ps_output;
  606. //
  607. // float t = g_tColor[g_nDataIdx ].Sample(g_sAniso, i.vTextureCoords.xy);
  608. // float t2 = g_tColor[g_nDataIdx2].Sample(g_sAniso, i.vTextureCoords.xy);
  609. // ps_output.vColor = t + t2;
  610. // return ps_output;
  611. // }
  612. // clang-format off
  613. const std::string defs = R"(
  614. OpCapability Shader
  615. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  616. %1 = OpExtInstImport "GLSL.std.450"
  617. OpMemoryModel Logical GLSL450
  618. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  619. ; CHECK: OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
  620. OpExecutionMode %MainPs OriginUpperLeft
  621. OpSource HLSL 500
  622. OpName %MainPs "MainPs"
  623. OpName %g_tColor "g_tColor"
  624. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  625. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  626. OpName %_ ""
  627. OpName %g_sAniso "g_sAniso"
  628. OpName %i_vTextureCoords "i.vTextureCoords"
  629. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  630. OpDecorate %g_tColor DescriptorSet 3
  631. OpDecorate %g_tColor Binding 0
  632. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  633. OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 4
  634. OpDecorate %PerViewConstantBuffer_t Block
  635. OpDecorate %g_sAniso DescriptorSet 0
  636. OpDecorate %i_vTextureCoords Location 0
  637. OpDecorate %_entryPointOutput_vColor Location 0
  638. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  639. )" + kOutputDecorations + R"(
  640. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  641. %void = OpTypeVoid
  642. %10 = OpTypeFunction %void
  643. %float = OpTypeFloat 32
  644. %v2float = OpTypeVector %float 2
  645. %v4float = OpTypeVector %float 4
  646. %int = OpTypeInt 32 1
  647. %int_0 = OpConstant %int 0
  648. %int_1 = OpConstant %int 1
  649. %17 = OpTypeImage %float 2D 0 0 0 1 Unknown
  650. %uint = OpTypeInt 32 0
  651. %uint_128 = OpConstant %uint 128
  652. %_arr_17_uint_128 = OpTypeArray %17 %uint_128
  653. %_ptr_UniformConstant__arr_17_uint_128 = OpTypePointer UniformConstant %_arr_17_uint_128
  654. %g_tColor = OpVariable %_ptr_UniformConstant__arr_17_uint_128 UniformConstant
  655. %PerViewConstantBuffer_t = OpTypeStruct %uint %uint
  656. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  657. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  658. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  659. %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
  660. %25 = OpTypeSampler
  661. %_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
  662. %g_sAniso = OpVariable %_ptr_UniformConstant_25 UniformConstant
  663. %27 = OpTypeSampledImage %17
  664. %_ptr_Input_v2float = OpTypePointer Input %v2float
  665. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  666. %_ptr_Output_v4float = OpTypePointer Output %v4float
  667. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  668. ; CHECK: %bool = OpTypeBool
  669. ; CHECK: {{%\w+}} = OpTypeFunction %void %uint %uint %uint %uint
  670. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  671. )" + kOutputGlobals + R"(
  672. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  673. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  674. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  675. ; CHECK: %v4uint = OpTypeVector %uint 4
  676. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  677. )";
  678. // clang-format on
  679. const std::string main_func =
  680. R"(%MainPs = OpFunction %void None %10
  681. %30 = OpLabel
  682. %31 = OpLoad %v2float %i_vTextureCoords
  683. %32 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  684. %33 = OpLoad %uint %32
  685. %34 = OpAccessChain %_ptr_UniformConstant_17 %g_tColor %33
  686. %35 = OpLoad %17 %34
  687. %36 = OpLoad %25 %g_sAniso
  688. %37 = OpSampledImage %27 %35 %36
  689. %38 = OpImageSampleImplicitLod %v4float %37 %31
  690. ; CHECK-NOT: %38 = OpImageSampleImplicitLod %v4float %37 %31
  691. ; CHECK: %48 = OpULessThan %bool %33 %uint_128
  692. ; CHECK: OpSelectionMerge %49 None
  693. ; CHECK: OpBranchConditional %48 %50 %51
  694. ; CHECK: %50 = OpLabel
  695. ; CHECK: %52 = OpLoad %17 %34
  696. ; CHECK: %53 = OpSampledImage %27 %52 %36
  697. ; CHECK: %54 = OpImageSampleImplicitLod %v4float %53 %31
  698. ; CHECK: OpBranch %49
  699. ; CHECK: %51 = OpLabel
  700. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_58 %uint_0 %33 %uint_128
  701. ; CHECK: OpBranch %49
  702. ; CHECK: %49 = OpLabel
  703. ; CHECK: %112 = OpPhi %v4float %54 %50 [[null_v4float]] %51
  704. %39 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
  705. %40 = OpLoad %uint %39
  706. %41 = OpAccessChain %_ptr_UniformConstant_17 %g_tColor %40
  707. %42 = OpLoad %17 %41
  708. %43 = OpSampledImage %27 %42 %36
  709. %44 = OpImageSampleImplicitLod %v4float %43 %31
  710. %45 = OpFAdd %v4float %38 %44
  711. ; CHECK-NOT: %44 = OpImageSampleImplicitLod %v4float %43 %31
  712. ; CHECK-NOT: %45 = OpFAdd %v4float %38 %44
  713. ; CHECK: %113 = OpULessThan %bool %40 %uint_128
  714. ; CHECK: OpSelectionMerge %114 None
  715. ; CHECK: OpBranchConditional %113 %115 %116
  716. ; CHECK: %115 = OpLabel
  717. ; CHECK: %117 = OpLoad %17 %41
  718. ; CHECK: %118 = OpSampledImage %27 %117 %36
  719. ; CHECK: %119 = OpImageSampleImplicitLod %v4float %118 %31
  720. ; CHECK: OpBranch %114
  721. ; CHECK: %116 = OpLabel
  722. ; CHECK: %121 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_64 %uint_0 %40 %uint_128
  723. ; CHECK: OpBranch %114
  724. ; CHECK: %114 = OpLabel
  725. ; CHECK: %122 = OpPhi %v4float %119 %115 [[null_v4float]] %116
  726. ; CHECK: %45 = OpFAdd %v4float %112 %122
  727. OpStore %_entryPointOutput_vColor %45
  728. OpReturn
  729. OpFunctionEnd
  730. )";
  731. const std::string output_func = kStreamWrite4Frag;
  732. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  733. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + output_func,
  734. true, 7u, 23u, false, false,
  735. false, false, false);
  736. }
  737. TEST_F(InstBindlessTest, InstrumentOpImage) {
  738. // This test verifies that the pass will correctly instrument shader
  739. // using OpImage. This test was created by editing the SPIR-V
  740. // from the Simple test.
  741. // clang-format off
  742. const std::string defs = R"(
  743. OpCapability Shader
  744. OpCapability StorageImageReadWithoutFormat
  745. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  746. %1 = OpExtInstImport "GLSL.std.450"
  747. OpMemoryModel Logical GLSL450
  748. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  749. ; CHECK: OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
  750. OpExecutionMode %MainPs OriginUpperLeft
  751. OpSource HLSL 500
  752. OpName %MainPs "MainPs"
  753. OpName %g_tColor "g_tColor"
  754. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  755. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  756. OpName %_ ""
  757. OpName %i_vTextureCoords "i.vTextureCoords"
  758. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  759. OpDecorate %g_tColor DescriptorSet 3
  760. OpDecorate %g_tColor Binding 0
  761. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  762. OpDecorate %PerViewConstantBuffer_t Block
  763. OpDecorate %i_vTextureCoords Location 0
  764. OpDecorate %_entryPointOutput_vColor Location 0
  765. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  766. )" + kOutputDecorations + R"(
  767. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  768. %void = OpTypeVoid
  769. %3 = OpTypeFunction %void
  770. %float = OpTypeFloat 32
  771. %v4float = OpTypeVector %float 4
  772. %int = OpTypeInt 32 1
  773. %v2int = OpTypeVector %int 2
  774. %int_0 = OpConstant %int 0
  775. %20 = OpTypeImage %float 2D 0 0 0 0 Unknown
  776. %uint = OpTypeInt 32 0
  777. %uint_128 = OpConstant %uint 128
  778. %39 = OpTypeSampledImage %20
  779. %_arr_39_uint_128 = OpTypeArray %39 %uint_128
  780. %_ptr_UniformConstant__arr_39_uint_128 = OpTypePointer UniformConstant %_arr_39_uint_128
  781. %g_tColor = OpVariable %_ptr_UniformConstant__arr_39_uint_128 UniformConstant
  782. %PerViewConstantBuffer_t = OpTypeStruct %uint
  783. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  784. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  785. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  786. %_ptr_UniformConstant_39 = OpTypePointer UniformConstant %39
  787. %_ptr_Input_v2int = OpTypePointer Input %v2int
  788. %i_vTextureCoords = OpVariable %_ptr_Input_v2int Input
  789. %_ptr_Output_v4float = OpTypePointer Output %v4float
  790. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  791. ; CHECK: uint_0 = OpConstant %uint 0
  792. ; CHECK: bool = OpTypeBool
  793. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  794. )" + kOutputGlobals + R"(
  795. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  796. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  797. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  798. ; CHECK: %v4uint = OpTypeVector %uint 4
  799. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  800. )";
  801. // clang-format on
  802. const std::string main_func = R"(
  803. %MainPs = OpFunction %void None %3
  804. %5 = OpLabel
  805. %53 = OpLoad %v2int %i_vTextureCoords
  806. %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  807. %64 = OpLoad %uint %63
  808. %65 = OpAccessChain %_ptr_UniformConstant_39 %g_tColor %64
  809. %66 = OpLoad %39 %65
  810. %75 = OpImage %20 %66
  811. %71 = OpImageRead %v4float %75 %53
  812. OpStore %_entryPointOutput_vColor %71
  813. ; CHECK-NOT: %71 = OpImageRead %v4float %75 %53
  814. ; CHECK-NOT: OpStore %_entryPointOutput_vColor %71
  815. ; CHECK: %78 = OpULessThan %bool %64 %uint_128
  816. ; CHECK: OpSelectionMerge %79 None
  817. ; CHECK: OpBranchConditional %78 %80 %81
  818. ; CHECK: %80 = OpLabel
  819. ; CHECK: %82 = OpLoad %39 %65
  820. ; CHECK: %83 = OpImage %20 %82
  821. ; CHECK: %84 = OpImageRead %v4float %83 %53
  822. ; CHECK: OpBranch %79
  823. ; CHECK: %81 = OpLabel
  824. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_0 %64 %uint_128
  825. ; CHECK: OpBranch %79
  826. ; CHECK: %79 = OpLabel
  827. ; CHECK: %142 = OpPhi %v4float %84 %80 [[null_v4float]] %81
  828. ; CHECK: OpStore %_entryPointOutput_vColor %142
  829. OpReturn
  830. OpFunctionEnd
  831. )";
  832. const std::string output_func = kStreamWrite4Frag;
  833. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  834. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + output_func,
  835. true, 7u, 23u, false, false,
  836. false, false, false);
  837. }
  838. TEST_F(InstBindlessTest, InstrumentSampledImage) {
  839. // This test verifies that the pass will correctly instrument shader
  840. // using sampled image. This test was created by editing the SPIR-V
  841. // from the Simple test.
  842. // clang-format off
  843. const std::string defs = R"(
  844. OpCapability Shader
  845. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  846. %1 = OpExtInstImport "GLSL.std.450"
  847. OpMemoryModel Logical GLSL450
  848. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  849. ; CHECK: OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
  850. OpExecutionMode %MainPs OriginUpperLeft
  851. OpSource HLSL 500
  852. OpName %MainPs "MainPs"
  853. OpName %g_tColor "g_tColor"
  854. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  855. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  856. OpName %_ ""
  857. OpName %i_vTextureCoords "i.vTextureCoords"
  858. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  859. OpDecorate %g_tColor DescriptorSet 3
  860. OpDecorate %g_tColor Binding 0
  861. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  862. OpDecorate %PerViewConstantBuffer_t Block
  863. OpDecorate %i_vTextureCoords Location 0
  864. OpDecorate %_entryPointOutput_vColor Location 0
  865. )" + kOutputDecorations + R"(
  866. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  867. %void = OpTypeVoid
  868. %3 = OpTypeFunction %void
  869. %float = OpTypeFloat 32
  870. %v2float = OpTypeVector %float 2
  871. %v4float = OpTypeVector %float 4
  872. %int = OpTypeInt 32 1
  873. %int_0 = OpConstant %int 0
  874. %20 = OpTypeImage %float 2D 0 0 0 1 Unknown
  875. %uint = OpTypeInt 32 0
  876. %uint_128 = OpConstant %uint 128
  877. %39 = OpTypeSampledImage %20
  878. %_arr_39_uint_128 = OpTypeArray %39 %uint_128
  879. %_ptr_UniformConstant__arr_39_uint_128 = OpTypePointer UniformConstant %_arr_39_uint_128
  880. %g_tColor = OpVariable %_ptr_UniformConstant__arr_39_uint_128 UniformConstant
  881. %PerViewConstantBuffer_t = OpTypeStruct %uint
  882. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  883. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  884. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  885. %_ptr_UniformConstant_39 = OpTypePointer UniformConstant %39
  886. %_ptr_Input_v2float = OpTypePointer Input %v2float
  887. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  888. %_ptr_Output_v4float = OpTypePointer Output %v4float
  889. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  890. ; CHECK: uint_0 = OpConstant %uint 0
  891. ; CHECK: bool = OpTypeBool
  892. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  893. )" + kOutputGlobals + R"(
  894. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  895. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  896. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  897. ; CHECK: %v4uint = OpTypeVector %uint 4
  898. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  899. )";
  900. // clang-format on
  901. const std::string main_func = R"(
  902. %MainPs = OpFunction %void None %3
  903. %5 = OpLabel
  904. %53 = OpLoad %v2float %i_vTextureCoords
  905. %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  906. %64 = OpLoad %uint %63
  907. %65 = OpAccessChain %_ptr_UniformConstant_39 %g_tColor %64
  908. %66 = OpLoad %39 %65
  909. %71 = OpImageSampleImplicitLod %v4float %66 %53
  910. OpStore %_entryPointOutput_vColor %71
  911. ; CHECK-NOT: %71 = OpImageSampleImplicitLod %v4float %66 %53
  912. ; CHECK-NOT: OpStore %_entryPointOutput_vColor %71
  913. ; CHECK: %74 = OpULessThan %bool %64 %uint_128
  914. ; CHECK: OpSelectionMerge %75 None
  915. ; CHECK: OpBranchConditional %74 %76 %77
  916. ; CHECK: %76 = OpLabel
  917. ; CHECK: %78 = OpLoad %39 %65
  918. ; CHECK: %79 = OpImageSampleImplicitLod %v4float %78 %53
  919. ; CHECK: OpBranch %75
  920. ; CHECK: %77 = OpLabel
  921. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_49 %uint_0 %64 %uint_128
  922. ; CHECK: OpBranch %75
  923. ; CHECK: %75 = OpLabel
  924. ; CHECK: %137 = OpPhi %v4float %79 %76 [[null_v4float]] %77
  925. ; CHECK: OpStore %_entryPointOutput_vColor %137
  926. OpReturn
  927. OpFunctionEnd
  928. )";
  929. const std::string output_func = kStreamWrite4Frag;
  930. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  931. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + output_func,
  932. true, 7u, 23u, false, false,
  933. false, false, false);
  934. }
  935. TEST_F(InstBindlessTest, InstrumentImageWrite) {
  936. // This test verifies that the pass will correctly instrument shader
  937. // doing bindless image write. This test was created by editing the SPIR-V
  938. // from the Simple test.
  939. // clang-format off
  940. const std::string defs = R"(
  941. OpCapability Shader
  942. OpCapability StorageImageWriteWithoutFormat
  943. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  944. %1 = OpExtInstImport "GLSL.std.450"
  945. OpMemoryModel Logical GLSL450
  946. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  947. ; CHECK: OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
  948. OpExecutionMode %MainPs OriginUpperLeft
  949. OpSource HLSL 500
  950. OpName %MainPs "MainPs"
  951. OpName %g_tColor "g_tColor"
  952. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  953. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  954. OpName %_ ""
  955. OpName %i_vTextureCoords "i.vTextureCoords"
  956. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  957. OpDecorate %g_tColor DescriptorSet 3
  958. OpDecorate %g_tColor Binding 0
  959. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  960. OpDecorate %PerViewConstantBuffer_t Block
  961. OpDecorate %i_vTextureCoords Location 0
  962. OpDecorate %_entryPointOutput_vColor Location 0
  963. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  964. )" + kOutputDecorations + R"(
  965. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  966. %void = OpTypeVoid
  967. %3 = OpTypeFunction %void
  968. %float = OpTypeFloat 32
  969. %v2float = OpTypeVector %float 2
  970. %v4float = OpTypeVector %float 4
  971. %int = OpTypeInt 32 1
  972. %v2int = OpTypeVector %int 2
  973. %int_0 = OpConstant %int 0
  974. %20 = OpTypeImage %float 2D 0 0 0 0 Unknown
  975. %uint = OpTypeInt 32 0
  976. %uint_128 = OpConstant %uint 128
  977. %80 = OpConstantNull %v4float
  978. %_arr_20_uint_128 = OpTypeArray %20 %uint_128
  979. %_ptr_UniformConstant__arr_20_uint_128 = OpTypePointer UniformConstant %_arr_20_uint_128
  980. %g_tColor = OpVariable %_ptr_UniformConstant__arr_20_uint_128 UniformConstant
  981. %PerViewConstantBuffer_t = OpTypeStruct %uint
  982. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  983. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  984. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  985. %_ptr_UniformConstant_20 = OpTypePointer UniformConstant %20
  986. %_ptr_Input_v2int = OpTypePointer Input %v2int
  987. %i_vTextureCoords = OpVariable %_ptr_Input_v2int Input
  988. %_ptr_Output_v4float = OpTypePointer Output %v4float
  989. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  990. )" + kOutputGlobals + R"(
  991. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  992. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  993. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  994. ; CHECK: %v4uint = OpTypeVector %uint 4
  995. )";
  996. // clang-format on
  997. const std::string main_func = R"(
  998. %MainPs = OpFunction %void None %3
  999. %5 = OpLabel
  1000. %53 = OpLoad %v2int %i_vTextureCoords
  1001. %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  1002. %64 = OpLoad %uint %63
  1003. %65 = OpAccessChain %_ptr_UniformConstant_20 %g_tColor %64
  1004. %66 = OpLoad %20 %65
  1005. OpImageWrite %66 %53 %80
  1006. OpStore %_entryPointOutput_vColor %80
  1007. ; CHECK-NOT: OpImageWrite %66 %53 %80
  1008. ; CHECK-NOT: OpStore %_entryPointOutput_vColor %80
  1009. ; CHECK: %35 = OpULessThan %bool %30 %uint_128
  1010. ; CHECK: OpSelectionMerge %36 None
  1011. ; CHECK: OpBranchConditional %35 %37 %38
  1012. ; CHECK: %37 = OpLabel
  1013. ; CHECK: %39 = OpLoad %16 %31
  1014. ; CHECK: OpImageWrite %39 %28 %19
  1015. ; CHECK: OpBranch %36
  1016. ; CHECK: %38 = OpLabel
  1017. ; CHECK: %95 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_0 %30 %uint_128
  1018. ; CHECK: OpBranch %36
  1019. ; CHECK: %36 = OpLabel
  1020. ; CHECK: OpStore %_entryPointOutput_vColor %19
  1021. OpReturn
  1022. OpFunctionEnd
  1023. )";
  1024. const std::string output_func = kStreamWrite4Frag;
  1025. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1026. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + output_func,
  1027. true, 7u, 23u, false, false,
  1028. false, false, false);
  1029. }
  1030. TEST_F(InstBindlessTest, InstrumentVertexSimple) {
  1031. // This test verifies that the pass will correctly instrument shader
  1032. // doing bindless image write. This test was created by editing the SPIR-V
  1033. // from the Simple test.
  1034. // clang-format off
  1035. const std::string defs = R"(
  1036. OpCapability Shader
  1037. OpCapability Sampled1D
  1038. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  1039. %1 = OpExtInstImport "GLSL.std.450"
  1040. OpMemoryModel Logical GLSL450
  1041. OpEntryPoint Vertex %main "main" %_ %coords2D
  1042. OpSource GLSL 450
  1043. OpName %main "main"
  1044. OpName %lod "lod"
  1045. OpName %coords1D "coords1D"
  1046. OpName %gl_PerVertex "gl_PerVertex"
  1047. OpMemberName %gl_PerVertex 0 "gl_Position"
  1048. OpMemberName %gl_PerVertex 1 "gl_PointSize"
  1049. OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
  1050. OpMemberName %gl_PerVertex 3 "gl_CullDistance"
  1051. OpName %_ ""
  1052. OpName %texSampler1D "texSampler1D"
  1053. OpName %foo "foo"
  1054. OpMemberName %foo 0 "g_idx"
  1055. OpName %__0 ""
  1056. OpName %coords2D "coords2D"
  1057. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  1058. )" + kOutputDecorations + R"(
  1059. ; CHECK: OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  1060. ; CHECK: OpDecorate %gl_InstanceIndex BuiltIn InstanceIndex
  1061. OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
  1062. OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
  1063. OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
  1064. OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
  1065. OpDecorate %gl_PerVertex Block
  1066. OpDecorate %texSampler1D DescriptorSet 0
  1067. OpDecorate %texSampler1D Binding 3
  1068. OpMemberDecorate %foo 0 Offset 0
  1069. OpDecorate %foo Block
  1070. OpDecorate %__0 DescriptorSet 0
  1071. OpDecorate %__0 Binding 5
  1072. OpDecorate %coords2D Location 0
  1073. %void = OpTypeVoid
  1074. %3 = OpTypeFunction %void
  1075. %float = OpTypeFloat 32
  1076. %_ptr_Function_float = OpTypePointer Function %float
  1077. %float_3 = OpConstant %float 3
  1078. %float_1_78900003 = OpConstant %float 1.78900003
  1079. %v4float = OpTypeVector %float 4
  1080. %uint = OpTypeInt 32 0
  1081. %uint_1 = OpConstant %uint 1
  1082. %_arr_float_uint_1 = OpTypeArray %float %uint_1
  1083. %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
  1084. %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
  1085. %_ = OpVariable %_ptr_Output_gl_PerVertex Output
  1086. %int = OpTypeInt 32 1
  1087. %int_0 = OpConstant %int 0
  1088. %21 = OpTypeImage %float 1D 0 0 0 1 Unknown
  1089. %22 = OpTypeSampledImage %21
  1090. %uint_128 = OpConstant %uint 128
  1091. %_arr_22_uint_128 = OpTypeArray %22 %uint_128
  1092. %_ptr_UniformConstant__arr_22_uint_128 = OpTypePointer UniformConstant %_arr_22_uint_128
  1093. %texSampler1D = OpVariable %_ptr_UniformConstant__arr_22_uint_128 UniformConstant
  1094. %foo = OpTypeStruct %int
  1095. %_ptr_Uniform_foo = OpTypePointer Uniform %foo
  1096. %__0 = OpVariable %_ptr_Uniform_foo Uniform
  1097. %_ptr_Uniform_int = OpTypePointer Uniform %int
  1098. %_ptr_UniformConstant_22 = OpTypePointer UniformConstant %22
  1099. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1100. %v2float = OpTypeVector %float 2
  1101. %_ptr_Input_v2float = OpTypePointer Input %v2float
  1102. %coords2D = OpVariable %_ptr_Input_v2float Input
  1103. ; CHECK: %bool = OpTypeBool
  1104. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  1105. )" + kOutputGlobals + R"(
  1106. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  1107. ; CHECK: %_ptr_Input_uint = OpTypePointer Input %uint
  1108. ; CHECK: %gl_VertexIndex = OpVariable %_ptr_Input_uint Input
  1109. ; CHECK: %gl_InstanceIndex = OpVariable %_ptr_Input_uint Input
  1110. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  1111. )";
  1112. // clang-format on
  1113. const std::string main_func = R"(
  1114. %main = OpFunction %void None %3
  1115. %5 = OpLabel
  1116. %lod = OpVariable %_ptr_Function_float Function
  1117. %coords1D = OpVariable %_ptr_Function_float Function
  1118. OpStore %lod %float_3
  1119. OpStore %coords1D %float_1_78900003
  1120. %31 = OpAccessChain %_ptr_Uniform_int %__0 %int_0
  1121. %32 = OpLoad %int %31
  1122. %34 = OpAccessChain %_ptr_UniformConstant_22 %texSampler1D %32
  1123. %35 = OpLoad %22 %34
  1124. %36 = OpLoad %float %coords1D
  1125. %37 = OpLoad %float %lod
  1126. %38 = OpImageSampleExplicitLod %v4float %35 %36 Lod %37
  1127. %40 = OpAccessChain %_ptr_Output_v4float %_ %int_0
  1128. OpStore %40 %38
  1129. ; CHECK-NOT: %38 = OpImageSampleExplicitLod %v4float %35 %36 Lod %37
  1130. ; CHECK-NOT: %40 = OpAccessChain %_ptr_Output_v4float %_ %int_0
  1131. ; CHECK-NOT: OpStore %40 %38
  1132. ; CHECK: %46 = OpULessThan %bool %37 %uint_128
  1133. ; CHECK: OpSelectionMerge %47 None
  1134. ; CHECK: OpBranchConditional %46 %48 %49
  1135. ; CHECK: %48 = OpLabel
  1136. ; CHECK: %50 = OpLoad %25 %38
  1137. ; CHECK: %51 = OpImageSampleExplicitLod %v4float %50 %40 Lod %41
  1138. ; CHECK: OpBranch %47
  1139. ; CHECK: %49 = OpLabel
  1140. ; CHECK: [[bitcast_result:%\w+]] = OpBitcast %uint %37
  1141. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_74 %uint_0 [[bitcast_result]] %uint_128
  1142. ; CHECK: OpBranch %47
  1143. ; CHECK: %47 = OpLabel
  1144. ; CHECK: %107 = OpPhi %v4float %51 %48 [[null_v4float]] %49
  1145. ; CHECK: %43 = OpAccessChain %_ptr_Output_v4float %_ %int_0
  1146. ; CHECK: OpStore %43 %107
  1147. OpReturn
  1148. OpFunctionEnd
  1149. )";
  1150. const std::string output_func = kStreamWrite4Vert;
  1151. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1152. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + output_func,
  1153. true, 7u, 23u, false, false,
  1154. false, false, false);
  1155. }
  1156. TEST_F(InstBindlessTest, InstrumentTeseSimple) {
  1157. // This test verifies that the pass will correctly instrument tessellation
  1158. // evaluation shader doing bindless buffer load.
  1159. //
  1160. // clang-format off
  1161. //
  1162. // #version 450
  1163. // #extension GL_EXT_nonuniform_qualifier : enable
  1164. //
  1165. // layout(std140, set = 0, binding = 0) uniform ufoo { uint index; } uniform_index_buffer;
  1166. //
  1167. // layout(set = 0, binding = 1) buffer bfoo { vec4 val; } adds[11];
  1168. //
  1169. // layout(triangles, equal_spacing, cw) in;
  1170. //
  1171. // void main() {
  1172. // gl_Position = adds[uniform_index_buffer.index].val;
  1173. // }
  1174. //
  1175. const std::string defs = R"(
  1176. OpCapability Tessellation
  1177. %1 = OpExtInstImport "GLSL.std.450"
  1178. OpMemoryModel Logical GLSL450
  1179. OpEntryPoint TessellationEvaluation %main "main" %_
  1180. ; CHECK: OpEntryPoint TessellationEvaluation %main "main" %_ %gl_PrimitiveID %gl_TessCoord
  1181. OpExecutionMode %main Triangles
  1182. OpExecutionMode %main SpacingEqual
  1183. OpExecutionMode %main VertexOrderCw
  1184. OpSource GLSL 450
  1185. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  1186. OpName %main "main"
  1187. OpName %gl_PerVertex "gl_PerVertex"
  1188. OpMemberName %gl_PerVertex 0 "gl_Position"
  1189. OpMemberName %gl_PerVertex 1 "gl_PointSize"
  1190. OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
  1191. OpMemberName %gl_PerVertex 3 "gl_CullDistance"
  1192. OpName %_ ""
  1193. OpName %bfoo "bfoo"
  1194. OpMemberName %bfoo 0 "val"
  1195. OpName %adds "adds"
  1196. OpName %ufoo "ufoo"
  1197. OpMemberName %ufoo 0 "index"
  1198. OpName %uniform_index_buffer "uniform_index_buffer"
  1199. OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
  1200. OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
  1201. OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
  1202. OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
  1203. OpDecorate %gl_PerVertex Block
  1204. OpMemberDecorate %bfoo 0 Offset 0
  1205. OpDecorate %bfoo Block
  1206. OpDecorate %adds DescriptorSet 0
  1207. OpDecorate %adds Binding 1
  1208. OpMemberDecorate %ufoo 0 Offset 0
  1209. OpDecorate %ufoo Block
  1210. OpDecorate %uniform_index_buffer DescriptorSet 0
  1211. OpDecorate %uniform_index_buffer Binding 0
  1212. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  1213. )" + kOutputDecorations + R"(
  1214. ; CHECK: OpDecorate %gl_PrimitiveID BuiltIn PrimitiveId
  1215. ; CHECK: OpDecorate %gl_TessCoord BuiltIn TessCoord
  1216. %void = OpTypeVoid
  1217. %3 = OpTypeFunction %void
  1218. %float = OpTypeFloat 32
  1219. %v4float = OpTypeVector %float 4
  1220. %uint = OpTypeInt 32 0
  1221. %uint_1 = OpConstant %uint 1
  1222. %_arr_float_uint_1 = OpTypeArray %float %uint_1
  1223. %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
  1224. %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
  1225. %_ = OpVariable %_ptr_Output_gl_PerVertex Output
  1226. %int = OpTypeInt 32 1
  1227. %int_0 = OpConstant %int 0
  1228. %bfoo = OpTypeStruct %v4float
  1229. %uint_11 = OpConstant %uint 11
  1230. %_arr_bfoo_uint_11 = OpTypeArray %bfoo %uint_11
  1231. %_ptr_StorageBuffer__arr_bfoo_uint_11 = OpTypePointer StorageBuffer %_arr_bfoo_uint_11
  1232. %adds = OpVariable %_ptr_StorageBuffer__arr_bfoo_uint_11 StorageBuffer
  1233. %ufoo = OpTypeStruct %uint
  1234. %_ptr_Uniform_ufoo = OpTypePointer Uniform %ufoo
  1235. %uniform_index_buffer = OpVariable %_ptr_Uniform_ufoo Uniform
  1236. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  1237. %_ptr_StorageBuffer_v4float = OpTypePointer StorageBuffer %v4float
  1238. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1239. ; CHECK: %bool = OpTypeBool
  1240. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  1241. )" + kOutputGlobals + R"(
  1242. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  1243. ; CHECK: %_ptr_Input_uint = OpTypePointer Input %uint
  1244. ; CHECK: %gl_PrimitiveID = OpVariable %_ptr_Input_uint Input
  1245. ; CHECK: %v3float = OpTypeVector %float 3
  1246. ; CHECK: %_ptr_Input_v3float = OpTypePointer Input %v3float
  1247. ; CHECK: %gl_TessCoord = OpVariable %_ptr_Input_v3float Input
  1248. ; CHECK: %v3uint = OpTypeVector %uint 3
  1249. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  1250. )";
  1251. // clang-format on
  1252. const std::string main_func = R"(
  1253. %main = OpFunction %void None %3
  1254. %5 = OpLabel
  1255. %25 = OpAccessChain %_ptr_Uniform_uint %uniform_index_buffer %int_0
  1256. %26 = OpLoad %uint %25
  1257. %28 = OpAccessChain %_ptr_StorageBuffer_v4float %adds %26 %int_0
  1258. %29 = OpLoad %v4float %28
  1259. ; CHECK-NOT: %29 = OpLoad %v4float %28
  1260. ; CHECK: %34 = OpULessThan %bool %28 %uint_11
  1261. ; CHECK: OpSelectionMerge %35 None
  1262. ; CHECK: OpBranchConditional %34 %36 %37
  1263. ; CHECK: %36 = OpLabel
  1264. ; CHECK: %38 = OpLoad %v4float %29
  1265. ; CHECK: OpBranch %35
  1266. ; CHECK: %37 = OpLabel
  1267. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_63 %uint_0 %28 %uint_11
  1268. ; CHECK: OpBranch %35
  1269. ; CHECK: %35 = OpLabel
  1270. ; CHECK: %102 = OpPhi %v4float %38 %36 [[null_v4float]] %37
  1271. %31 = OpAccessChain %_ptr_Output_v4float %_ %int_0
  1272. OpStore %31 %29
  1273. ; CHECK-NOT: OpStore %31 %29
  1274. ; CHECK: OpStore %31 %102
  1275. OpReturn
  1276. OpFunctionEnd
  1277. )";
  1278. const std::string output_func = kStreamWrite4Tese;
  1279. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1280. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + output_func,
  1281. true, 7u, 23u, false, false,
  1282. false, false, false);
  1283. }
  1284. TEST_F(InstBindlessTest, MultipleDebugFunctions) {
  1285. // Same source as Simple, but compiled -g and not optimized, especially not
  1286. // inlined. The OpSource has had the source extracted for the sake of brevity.
  1287. // clang-format off
  1288. const std::string defs = R"(
  1289. OpCapability Shader
  1290. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  1291. %2 = OpExtInstImport "GLSL.std.450"
  1292. OpMemoryModel Logical GLSL450
  1293. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  1294. ; CHECK: OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
  1295. OpExecutionMode %MainPs OriginUpperLeft
  1296. %1 = OpString "foo5.frag"
  1297. OpSource HLSL 500 %1
  1298. OpName %MainPs "MainPs"
  1299. OpName %PS_INPUT "PS_INPUT"
  1300. OpMemberName %PS_INPUT 0 "vTextureCoords"
  1301. OpName %PS_OUTPUT "PS_OUTPUT"
  1302. OpMemberName %PS_OUTPUT 0 "vColor"
  1303. OpName %_MainPs_struct_PS_INPUT_vf21_ "@MainPs(struct-PS_INPUT-vf21;"
  1304. OpName %i "i"
  1305. OpName %ps_output "ps_output"
  1306. OpName %g_tColor "g_tColor"
  1307. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  1308. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  1309. OpName %_ ""
  1310. OpName %g_sAniso "g_sAniso"
  1311. OpName %i_0 "i"
  1312. OpName %i_vTextureCoords "i.vTextureCoords"
  1313. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  1314. OpName %param "param"
  1315. OpDecorate %g_tColor DescriptorSet 0
  1316. OpDecorate %g_tColor Binding 0
  1317. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  1318. OpDecorate %PerViewConstantBuffer_t Block
  1319. OpDecorate %g_sAniso DescriptorSet 0
  1320. OpDecorate %g_sAniso Binding 1
  1321. OpDecorate %i_vTextureCoords Location 0
  1322. OpDecorate %_entryPointOutput_vColor Location 0
  1323. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  1324. )" + kOutputDecorations + R"(
  1325. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  1326. %void = OpTypeVoid
  1327. %4 = OpTypeFunction %void
  1328. %float = OpTypeFloat 32
  1329. %v2float = OpTypeVector %float 2
  1330. %PS_INPUT = OpTypeStruct %v2float
  1331. %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
  1332. %v4float = OpTypeVector %float 4
  1333. %PS_OUTPUT = OpTypeStruct %v4float
  1334. %13 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
  1335. %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
  1336. %int = OpTypeInt 32 1
  1337. %int_0 = OpConstant %int 0
  1338. %21 = OpTypeImage %float 2D 0 0 0 1 Unknown
  1339. %uint = OpTypeInt 32 0
  1340. %uint_128 = OpConstant %uint 128
  1341. %_arr_21_uint_128 = OpTypeArray %21 %uint_128
  1342. %_ptr_UniformConstant__arr_21_uint_128 = OpTypePointer UniformConstant %_arr_21_uint_128
  1343. %g_tColor = OpVariable %_ptr_UniformConstant__arr_21_uint_128 UniformConstant
  1344. %PerViewConstantBuffer_t = OpTypeStruct %uint
  1345. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  1346. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  1347. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  1348. %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
  1349. %36 = OpTypeSampler
  1350. %_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
  1351. %g_sAniso = OpVariable %_ptr_UniformConstant_36 UniformConstant
  1352. %40 = OpTypeSampledImage %21
  1353. %_ptr_Function_v2float = OpTypePointer Function %v2float
  1354. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1355. %_ptr_Input_v2float = OpTypePointer Input %v2float
  1356. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  1357. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1358. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  1359. ; CHECK: %bool = OpTypeBool
  1360. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  1361. )" + kOutputGlobals + R"(
  1362. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  1363. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  1364. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  1365. ; CHECK: %v4uint = OpTypeVector %uint 4
  1366. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  1367. )";
  1368. // clang-format on
  1369. const std::string func1 = R"(
  1370. %MainPs = OpFunction %void None %4
  1371. %6 = OpLabel
  1372. %i_0 = OpVariable %_ptr_Function_PS_INPUT Function
  1373. %param = OpVariable %_ptr_Function_PS_INPUT Function
  1374. OpLine %1 21 0
  1375. %54 = OpLoad %v2float %i_vTextureCoords
  1376. %55 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
  1377. OpStore %55 %54
  1378. %59 = OpLoad %PS_INPUT %i_0
  1379. OpStore %param %59
  1380. %60 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
  1381. %61 = OpCompositeExtract %v4float %60 0
  1382. OpStore %_entryPointOutput_vColor %61
  1383. OpReturn
  1384. OpFunctionEnd
  1385. )";
  1386. const std::string func2 = R"(
  1387. %_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %13
  1388. %i = OpFunctionParameter %_ptr_Function_PS_INPUT
  1389. %16 = OpLabel
  1390. %ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
  1391. OpLine %1 24 0
  1392. %31 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  1393. %32 = OpLoad %uint %31
  1394. %34 = OpAccessChain %_ptr_UniformConstant_21 %g_tColor %32
  1395. %35 = OpLoad %21 %34
  1396. %39 = OpLoad %36 %g_sAniso
  1397. %41 = OpSampledImage %40 %35 %39
  1398. %43 = OpAccessChain %_ptr_Function_v2float %i %int_0
  1399. %44 = OpLoad %v2float %43
  1400. %45 = OpImageSampleImplicitLod %v4float %41 %44
  1401. ; CHECK-NOT: %45 = OpImageSampleImplicitLod %v4float %41 %44
  1402. ; CHECK: OpNoLine
  1403. ; CHECK: %62 = OpULessThan %bool %50 %uint_128
  1404. ; CHECK: OpSelectionMerge %63 None
  1405. ; CHECK: OpBranchConditional %62 %64 %65
  1406. ; CHECK: %64 = OpLabel
  1407. ; CHECK: %66 = OpLoad %27 %51
  1408. ; CHECK: %67 = OpSampledImage %37 %66 %53
  1409. ; CHECK: OpLine %5 24 0
  1410. ; CHECK: %68 = OpImageSampleImplicitLod %v4float %67 %56
  1411. ; CHECK: OpNoLine
  1412. ; CHECK: OpBranch %63
  1413. ; CHECK: %65 = OpLabel
  1414. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_109 %uint_0 %50 %uint_128
  1415. ; CHECK: OpBranch %63
  1416. ; CHECK: %63 = OpLabel
  1417. ; CHECK: [[phi_result:%\w+]] = OpPhi %v4float %68 %64 [[null_v4float]] %65
  1418. ; CHECK: OpLine %5 24 0
  1419. %47 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
  1420. OpStore %47 %45
  1421. ; CHECK-NOT: OpStore %47 %45
  1422. ; CHECK: [[store_loc:%\w+]] = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
  1423. ; CHECK: OpStore [[store_loc]] [[phi_result]]
  1424. OpLine %1 25 0
  1425. %48 = OpLoad %PS_OUTPUT %ps_output
  1426. OpReturnValue %48
  1427. OpFunctionEnd
  1428. )";
  1429. const std::string output_func = kStreamWrite4Frag;
  1430. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1431. SinglePassRunAndMatch<InstBindlessCheckPass>(
  1432. defs + func1 + func2 + output_func, true, 7u, 23u, false, false, false,
  1433. false, false);
  1434. }
  1435. TEST_F(InstBindlessTest, RuntimeArray) {
  1436. // This test verifies that the pass will correctly instrument shader
  1437. // with runtime descriptor array. This test was created by editing the
  1438. // SPIR-V from the Simple test.
  1439. // clang-format off
  1440. const std::string defs = R"(
  1441. OpCapability Shader
  1442. OpCapability RuntimeDescriptorArray
  1443. OpExtension "SPV_EXT_descriptor_indexing"
  1444. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  1445. %1 = OpExtInstImport "GLSL.std.450"
  1446. OpMemoryModel Logical GLSL450
  1447. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  1448. ; CHECK: OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
  1449. OpExecutionMode %MainPs OriginUpperLeft
  1450. OpSource HLSL 500
  1451. OpName %MainPs "MainPs"
  1452. OpName %g_tColor "g_tColor"
  1453. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  1454. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  1455. OpName %_ ""
  1456. OpName %g_sAniso "g_sAniso"
  1457. OpName %i_vTextureCoords "i.vTextureCoords"
  1458. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  1459. OpDecorate %g_tColor DescriptorSet 1
  1460. OpDecorate %g_tColor Binding 2
  1461. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  1462. OpDecorate %PerViewConstantBuffer_t Block
  1463. OpDecorate %g_sAniso DescriptorSet 1
  1464. OpDecorate %g_sAniso Binding 0
  1465. OpDecorate %i_vTextureCoords Location 0
  1466. OpDecorate %_entryPointOutput_vColor Location 0
  1467. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  1468. )" + kInputDecorations + kOutputDecorations + R"(
  1469. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  1470. %void = OpTypeVoid
  1471. %3 = OpTypeFunction %void
  1472. %float = OpTypeFloat 32
  1473. %v2float = OpTypeVector %float 2
  1474. %v4float = OpTypeVector %float 4
  1475. %int = OpTypeInt 32 1
  1476. %int_0 = OpConstant %int 0
  1477. %20 = OpTypeImage %float 2D 0 0 0 1 Unknown
  1478. %uint = OpTypeInt 32 0
  1479. %uint_1 = OpConstant %uint 1
  1480. %_rarr_20 = OpTypeRuntimeArray %20
  1481. %_ptr_UniformConstant__arr_20 = OpTypePointer UniformConstant %_rarr_20
  1482. %g_tColor = OpVariable %_ptr_UniformConstant__arr_20 UniformConstant
  1483. %PerViewConstantBuffer_t = OpTypeStruct %uint
  1484. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  1485. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  1486. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  1487. %_ptr_UniformConstant_20 = OpTypePointer UniformConstant %20
  1488. %35 = OpTypeSampler
  1489. %_ptr_UniformConstant_35 = OpTypePointer UniformConstant %35
  1490. %g_sAniso = OpVariable %_ptr_UniformConstant_35 UniformConstant
  1491. %39 = OpTypeSampledImage %20
  1492. %_ptr_Input_v2float = OpTypePointer Input %v2float
  1493. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  1494. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1495. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  1496. ; CHECK: %41 = OpTypeFunction %uint %uint %uint
  1497. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  1498. )" + kInputGlobals + R"(
  1499. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  1500. ; CHECK: %bool = OpTypeBool
  1501. )" + kOutputGlobals + R"(
  1502. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  1503. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  1504. ; CHECK: %v4uint = OpTypeVector %uint 4
  1505. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  1506. )";
  1507. // clang-format on
  1508. const std::string main_func = R"(
  1509. %MainPs = OpFunction %void None %3
  1510. %5 = OpLabel
  1511. %53 = OpLoad %v2float %i_vTextureCoords
  1512. %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  1513. %64 = OpLoad %uint %63
  1514. %65 = OpAccessChain %_ptr_UniformConstant_20 %g_tColor %64
  1515. %66 = OpLoad %20 %65
  1516. %67 = OpLoad %35 %g_sAniso
  1517. %68 = OpSampledImage %39 %66 %67
  1518. %71 = OpImageSampleImplicitLod %v4float %68 %53
  1519. OpStore %_entryPointOutput_vColor %71
  1520. ; CHECK-NOT: %71 = OpImageSampleImplicitLod %v4float %68 %53
  1521. ; CHECK-NOT: OpStore %_entryPointOutput_vColor %71
  1522. ; CHECK: %55 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_2 %uint_2
  1523. ; CHECK: %57 = OpULessThan %bool %32 %55
  1524. ; CHECK: OpSelectionMerge %58 None
  1525. ; CHECK: OpBranchConditional %57 %59 %60
  1526. ; CHECK: %59 = OpLabel
  1527. ; CHECK: %61 = OpLoad %16 %33
  1528. ; CHECK: %62 = OpSampledImage %26 %61 %35
  1529. ; CHECK: %136 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_1 %uint_2 %32
  1530. ; CHECK: %137 = OpULessThan %bool %uint_0 %136
  1531. ; CHECK: OpSelectionMerge %138 None
  1532. ; CHECK: OpBranchConditional %137 %139 %140
  1533. ; CHECK: %139 = OpLabel
  1534. ; CHECK: %141 = OpLoad %16 %33
  1535. ; CHECK: %142 = OpSampledImage %26 %141 %35
  1536. ; CHECK: %143 = OpImageSampleImplicitLod %v4float %142 %30
  1537. ; CHECK: OpBranch %138
  1538. ; CHECK: %140 = OpLabel
  1539. ; CHECK: %144 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_59 %uint_1 %32 %uint_0
  1540. ; CHECK: OpBranch %138
  1541. ; CHECK: %138 = OpLabel
  1542. ; CHECK: [[phi_result_1:%\w+]] = OpPhi %v4float %143 %139 [[null_v4float]] %140
  1543. ; CHECK: OpBranch %58
  1544. ; CHECK: %60 = OpLabel
  1545. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_59 %uint_0 %32 %55
  1546. ; CHECK: OpBranch %58
  1547. ; CHECK: %58 = OpLabel
  1548. ; CHECK: [[phi_result_2:%\w+]] = OpPhi %v4float [[phi_result_1]] %138 [[null_v4float]] %60
  1549. ; CHECK: OpStore %_entryPointOutput_vColor [[phi_result_2]]
  1550. OpReturn
  1551. OpFunctionEnd
  1552. )";
  1553. const std::string new_funcs = kDirectRead2 + kStreamWrite4Frag + kDirectRead4;
  1554. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1555. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  1556. true, 7u, 23u, true, true, false,
  1557. false, false);
  1558. }
  1559. TEST_F(InstBindlessTest, InstrumentInitCheckOnScalarDescriptor) {
  1560. // This test verifies that the pass will correctly instrument vanilla
  1561. // texture sample on a scalar descriptor with an initialization check if the
  1562. // input_init_enable argument is set to true. This can happen when the
  1563. // descriptor indexing extension is enabled in the API but the SPIR-V
  1564. // does not have the extension enabled because it does not contain a
  1565. // runtime array. This is the same shader as NoInstrumentNonBindless.
  1566. // clang-format off
  1567. const std::string defs = R"(
  1568. OpCapability Shader
  1569. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  1570. %1 = OpExtInstImport "GLSL.std.450"
  1571. OpMemoryModel Logical GLSL450
  1572. OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
  1573. ; CHECK: OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
  1574. OpExecutionMode %MainPs OriginUpperLeft
  1575. OpSource HLSL 500
  1576. OpName %MainPs "MainPs"
  1577. OpName %g_tColor "g_tColor"
  1578. OpName %g_sAniso "g_sAniso"
  1579. OpName %i_vTextureCoords "i.vTextureCoords"
  1580. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  1581. OpDecorate %g_tColor DescriptorSet 0
  1582. OpDecorate %g_tColor Binding 0
  1583. OpDecorate %g_sAniso DescriptorSet 0
  1584. OpDecorate %g_sAniso Binding 0
  1585. OpDecorate %i_vTextureCoords Location 0
  1586. OpDecorate %_entryPointOutput_vColor Location 0
  1587. ; check: OpDecorate %_runtimearr_uint ArrayStride 4
  1588. )" + kInputDecorations + kOutputDecorations + R"(
  1589. ; check: OpDecorate %gl_FragCoord BuiltIn FragCoord
  1590. %void = OpTypeVoid
  1591. %8 = OpTypeFunction %void
  1592. %float = OpTypeFloat 32
  1593. %v2float = OpTypeVector %float 2
  1594. %v4float = OpTypeVector %float 4
  1595. %12 = OpTypeImage %float 2D 0 0 0 1 Unknown
  1596. %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
  1597. %g_tColor = OpVariable %_ptr_UniformConstant_12 UniformConstant
  1598. %14 = OpTypeSampler
  1599. %_ptr_UniformConstant_14 = OpTypePointer UniformConstant %14
  1600. %g_sAniso = OpVariable %_ptr_UniformConstant_14 UniformConstant
  1601. %16 = OpTypeSampledImage %12
  1602. %_ptr_Input_v2float = OpTypePointer Input %v2float
  1603. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  1604. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1605. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  1606. ; CHECK: %uint = OpTypeInt 32 0
  1607. ; CHECK: %28 = OpTypeFunction %uint %uint %uint %uint %uint
  1608. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  1609. )" + kInputGlobals + R"(
  1610. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  1611. ; CHECK: %bool = OpTypeBool
  1612. )" + kOutputGlobals + R"(
  1613. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  1614. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  1615. ; CHECK: %v4uint = OpTypeVector %uint 4
  1616. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  1617. )";
  1618. // clang-format on
  1619. const std::string main_func = R"(
  1620. %MainPs = OpFunction %void None %8
  1621. %19 = OpLabel
  1622. %20 = OpLoad %v2float %i_vTextureCoords
  1623. %21 = OpLoad %12 %g_tColor
  1624. %22 = OpLoad %14 %g_sAniso
  1625. %23 = OpSampledImage %16 %21 %22
  1626. %24 = OpImageSampleImplicitLod %v4float %23 %20
  1627. OpStore %_entryPointOutput_vColor %24
  1628. ; CHECK-NOT: %24 = OpImageSampleImplicitLod %v4float %23 %20
  1629. ; CHECK-NOT: OpStore %_entryPointOutput_vColor %24
  1630. ; CHECK: %50 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  1631. ; CHECK: %52 = OpULessThan %bool %uint_0 %50
  1632. ; CHECK: OpSelectionMerge %54 None
  1633. ; CHECK: OpBranchConditional %52 %55 %56
  1634. ; CHECK: %55 = OpLabel
  1635. ; CHECK: %57 = OpLoad %12 %g_tColor
  1636. ; CHECK: %58 = OpSampledImage %16 %57 %22
  1637. ; CHECK: %59 = OpImageSampleImplicitLod %v4float %58 %20
  1638. ; CHECK: OpBranch %54
  1639. ; CHECK: %56 = OpLabel
  1640. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_39 %uint_1 %uint_0 %uint_0
  1641. ; CHECK: OpBranch %54
  1642. ; CHECK: %54 = OpLabel
  1643. ; CHECK: [[phi_result:%\w+]] = OpPhi %v4float %59 %55 [[null_v4float]] %56
  1644. ; CHECK: OpStore %_entryPointOutput_vColor [[phi_result]]
  1645. OpReturn
  1646. OpFunctionEnd
  1647. )";
  1648. const std::string new_funcs = kDirectRead4 + kStreamWrite4Frag;
  1649. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1650. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  1651. true, 7u, 23u, true, true, false,
  1652. false, false);
  1653. }
  1654. TEST_F(InstBindlessTest, SPV14AddToEntryPoint) {
  1655. const std::string text = R"(
  1656. ; CHECK: OpEntryPoint Fragment {{%\w+}} "foo" {{%\w+}} {{%\w+}} {{%\w+}} [[v1:%\w+]] [[v2:%\w+]]
  1657. ; CHECK: OpDecorate [[v1]] DescriptorSet 7
  1658. ; CHECK: OpDecorate [[v2]] DescriptorSet 7
  1659. ; CHECK: [[v1]] = OpVariable {{%\w+}} StorageBuffer
  1660. ; CHECK: [[v2]] = OpVariable {{%\w+}} StorageBuffer
  1661. OpCapability Shader
  1662. OpExtension "SPV_EXT_descriptor_indexing"
  1663. OpMemoryModel Logical GLSL450
  1664. OpEntryPoint Fragment %foo "foo" %gid %image_var %sampler_var
  1665. OpExecutionMode %foo OriginUpperLeft
  1666. OpDecorate %image_var DescriptorSet 0
  1667. OpDecorate %image_var Binding 0
  1668. OpDecorate %sampler_var DescriptorSet 0
  1669. OpDecorate %sampler_var Binding 1
  1670. OpDecorate %gid DescriptorSet 0
  1671. OpDecorate %gid Binding 2
  1672. OpDecorate %struct Block
  1673. OpMemberDecorate %struct 0 Offset 0
  1674. %void = OpTypeVoid
  1675. %int = OpTypeInt 32 0
  1676. %int_0 = OpConstant %int 0
  1677. %v3int = OpTypeVector %int 3
  1678. %float = OpTypeFloat 32
  1679. %v3float = OpTypeVector %float 3
  1680. %v4float = OpTypeVector %float 4
  1681. %struct = OpTypeStruct %v3int
  1682. %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
  1683. %ptr_ssbo_v3int = OpTypePointer StorageBuffer %v3int
  1684. %gid = OpVariable %ptr_ssbo_struct StorageBuffer
  1685. %image = OpTypeImage %float 3D 0 0 0 1 Unknown
  1686. %ptr_uc_image = OpTypePointer UniformConstant %image
  1687. %sampler = OpTypeSampler
  1688. %ptr_uc_sampler = OpTypePointer UniformConstant %sampler
  1689. %image_var = OpVariable %ptr_uc_image UniformConstant
  1690. %sampler_var = OpVariable %ptr_uc_sampler UniformConstant
  1691. %sampled = OpTypeSampledImage %image
  1692. %void_fn = OpTypeFunction %void
  1693. %foo = OpFunction %void None %void_fn
  1694. %entry = OpLabel
  1695. %ld_image = OpLoad %image %image_var
  1696. %ld_sampler = OpLoad %sampler %sampler_var
  1697. %gep = OpAccessChain %ptr_ssbo_v3int %gid %int_0
  1698. %ld_gid = OpLoad %v3int %gep
  1699. %convert = OpConvertUToF %v3float %ld_gid
  1700. %sampled_image = OpSampledImage %sampled %ld_image %ld_sampler
  1701. %sample = OpImageSampleImplicitLod %v4float %sampled_image %convert
  1702. OpReturn
  1703. OpFunctionEnd
  1704. )";
  1705. SetTargetEnv(SPV_ENV_VULKAN_1_1_SPIRV_1_4);
  1706. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, true, true,
  1707. false, false, false);
  1708. }
  1709. TEST_F(InstBindlessTest, SPV14AddToEntryPoints) {
  1710. const std::string text = R"(
  1711. ; CHECK: OpEntryPoint Fragment {{%\w+}} "foo" {{%\w+}} {{%\w+}} {{%\w+}} [[v1:%\w+]] [[v2:%\w+]]
  1712. ; CHECK: OpEntryPoint Fragment {{%\w+}} "bar" {{%\w+}} {{%\w+}} {{%\w+}} [[v1:%\w+]] [[v2:%\w+]]
  1713. ; CHECK: OpDecorate [[v1]] DescriptorSet 7
  1714. ; CHECK: OpDecorate [[v2]] DescriptorSet 7
  1715. ; CHECK: [[v1]] = OpVariable {{%\w+}} StorageBuffer
  1716. ; CHECK: [[v2]] = OpVariable {{%\w+}} StorageBuffer
  1717. OpCapability Shader
  1718. OpExtension "SPV_EXT_descriptor_indexing"
  1719. OpMemoryModel Logical GLSL450
  1720. OpEntryPoint Fragment %foo "foo" %gid %image_var %sampler_var
  1721. OpEntryPoint Fragment %foo "bar" %gid %image_var %sampler_var
  1722. OpExecutionMode %foo OriginUpperLeft
  1723. OpDecorate %image_var DescriptorSet 0
  1724. OpDecorate %image_var Binding 0
  1725. OpDecorate %sampler_var DescriptorSet 0
  1726. OpDecorate %sampler_var Binding 1
  1727. OpDecorate %gid DescriptorSet 0
  1728. OpDecorate %gid Binding 2
  1729. OpDecorate %struct Block
  1730. OpMemberDecorate %struct 0 Offset 0
  1731. %void = OpTypeVoid
  1732. %int = OpTypeInt 32 0
  1733. %int_0 = OpConstant %int 0
  1734. %v3int = OpTypeVector %int 3
  1735. %float = OpTypeFloat 32
  1736. %v3float = OpTypeVector %float 3
  1737. %v4float = OpTypeVector %float 4
  1738. %struct = OpTypeStruct %v3int
  1739. %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
  1740. %ptr_ssbo_v3int = OpTypePointer StorageBuffer %v3int
  1741. %gid = OpVariable %ptr_ssbo_struct StorageBuffer
  1742. %image = OpTypeImage %float 3D 0 0 0 1 Unknown
  1743. %ptr_uc_image = OpTypePointer UniformConstant %image
  1744. %sampler = OpTypeSampler
  1745. %ptr_uc_sampler = OpTypePointer UniformConstant %sampler
  1746. %image_var = OpVariable %ptr_uc_image UniformConstant
  1747. %sampler_var = OpVariable %ptr_uc_sampler UniformConstant
  1748. %sampled = OpTypeSampledImage %image
  1749. %void_fn = OpTypeFunction %void
  1750. %foo = OpFunction %void None %void_fn
  1751. %entry = OpLabel
  1752. %ld_image = OpLoad %image %image_var
  1753. %ld_sampler = OpLoad %sampler %sampler_var
  1754. %gep = OpAccessChain %ptr_ssbo_v3int %gid %int_0
  1755. %ld_gid = OpLoad %v3int %gep
  1756. %convert = OpConvertUToF %v3float %ld_gid
  1757. %sampled_image = OpSampledImage %sampled %ld_image %ld_sampler
  1758. %sample = OpImageSampleImplicitLod %v4float %sampled_image %convert
  1759. OpReturn
  1760. OpFunctionEnd
  1761. )";
  1762. SetTargetEnv(SPV_ENV_VULKAN_1_1_SPIRV_1_4);
  1763. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, true, true,
  1764. false, false, false);
  1765. }
  1766. TEST_F(InstBindlessTest, InstBoundsAndInitLoadUnsizedUBOArray) {
  1767. // #version 450
  1768. // #extension GL_EXT_nonuniform_qualifier : enable
  1769. //
  1770. // layout(location=0) in nonuniformEXT flat int nu_ii;
  1771. // layout(location=0) out float b;
  1772. //
  1773. // layout(binding=3) uniform uname { float a; } uniformBuffer[];
  1774. //
  1775. // void main()
  1776. // {
  1777. // b = uniformBuffer[nu_ii].a;
  1778. // }
  1779. // clang-format off
  1780. const std::string defs = R"(
  1781. OpCapability Shader
  1782. OpCapability ShaderNonUniform
  1783. OpCapability RuntimeDescriptorArray
  1784. OpCapability UniformBufferArrayNonUniformIndexing
  1785. OpExtension "SPV_EXT_descriptor_indexing"
  1786. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  1787. %1 = OpExtInstImport "GLSL.std.450"
  1788. OpMemoryModel Logical GLSL450
  1789. OpEntryPoint Fragment %main "main" %b %nu_ii
  1790. ; CHECK: OpEntryPoint Fragment %main "main" %b %nu_ii %gl_FragCoord
  1791. OpExecutionMode %main OriginUpperLeft
  1792. OpSource GLSL 450
  1793. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  1794. OpName %main "main"
  1795. OpName %b "b"
  1796. OpName %uname "uname"
  1797. OpMemberName %uname 0 "a"
  1798. OpName %uniformBuffer "uniformBuffer"
  1799. OpName %nu_ii "nu_ii"
  1800. OpDecorate %b Location 0
  1801. OpMemberDecorate %uname 0 Offset 0
  1802. OpDecorate %uname Block
  1803. OpDecorate %uniformBuffer DescriptorSet 0
  1804. OpDecorate %uniformBuffer Binding 3
  1805. OpDecorate %nu_ii Flat
  1806. OpDecorate %nu_ii Location 0
  1807. OpDecorate %nu_ii NonUniform
  1808. OpDecorate %16 NonUniform
  1809. OpDecorate %20 NonUniform
  1810. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  1811. )" + kInputDecorations + R"(
  1812. ; CHECK: OpDecorate %130 NonUniform
  1813. )" + kOutputDecorations + R"(
  1814. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  1815. ; CHECK: OpDecorate %127 NonUniform
  1816. %void = OpTypeVoid
  1817. %3 = OpTypeFunction %void
  1818. %float = OpTypeFloat 32
  1819. %_ptr_Output_float = OpTypePointer Output %float
  1820. %b = OpVariable %_ptr_Output_float Output
  1821. %uname = OpTypeStruct %float
  1822. %_runtimearr_uname = OpTypeRuntimeArray %uname
  1823. %_ptr_Uniform__runtimearr_uname = OpTypePointer Uniform %_runtimearr_uname
  1824. %uniformBuffer = OpVariable %_ptr_Uniform__runtimearr_uname Uniform
  1825. %int = OpTypeInt 32 1
  1826. %_ptr_Input_int = OpTypePointer Input %int
  1827. %nu_ii = OpVariable %_ptr_Input_int Input
  1828. %int_0 = OpConstant %int 0
  1829. %_ptr_Uniform_float = OpTypePointer Uniform %float
  1830. ; CHECK: %uint = OpTypeInt 32 0
  1831. ; CHECK: %26 = OpTypeFunction %uint %uint %uint
  1832. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  1833. )" + kInputGlobals + R"(
  1834. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  1835. ; CHECK: %bool = OpTypeBool
  1836. )" + kOutputGlobals + R"(
  1837. ; CHECK: %v4float = OpTypeVector %float 4
  1838. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  1839. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  1840. ; CHECK: %v4uint = OpTypeVector %uint 4
  1841. ; CHECK: [[null_float:%\w+]] = OpConstantNull %float
  1842. )";
  1843. // clang-format on
  1844. const std::string main_func = R"(
  1845. %main = OpFunction %void None %3
  1846. %5 = OpLabel
  1847. %16 = OpLoad %int %nu_ii
  1848. %19 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %16 %int_0
  1849. %20 = OpLoad %float %19
  1850. OpStore %b %20
  1851. ; CHECK-NOT: %20 = OpLoad %float %19
  1852. ; CHECK-NOT: OpStore %b %20
  1853. ; CHECK: %40 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_3
  1854. ; CHECK: %42 = OpULessThan %bool %7 %40
  1855. ; CHECK: OpSelectionMerge %43 None
  1856. ; CHECK: OpBranchConditional %42 %44 %45
  1857. ; CHECK: %44 = OpLabel
  1858. ; CHECK: %103 = OpBitcast %uint %7
  1859. ; CHECK: %122 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_3 %103
  1860. ; CHECK: %123 = OpULessThan %bool %uint_0 %122
  1861. ; CHECK: OpSelectionMerge %124 None
  1862. ; CHECK: OpBranchConditional %123 %125 %126
  1863. ; CHECK: %125 = OpLabel
  1864. ; CHECK: %127 = OpLoad %float %20
  1865. ; CHECK: OpBranch %124
  1866. ; CHECK: %126 = OpLabel
  1867. ; CHECK: %128 = OpBitcast %uint %7
  1868. ; CHECK: %129 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_45 %uint_1 %128 %uint_0
  1869. ; CHECK: OpBranch %124
  1870. ; CHECK: %124 = OpLabel
  1871. ; CHECK: [[phi_result_1:%\w+]] = OpPhi %float %127 %125 [[null_float]] %126
  1872. ; CHECK: OpBranch %43
  1873. ; CHECK: %45 = OpLabel
  1874. ; CHECK: [[bitcast_result:%\w+]] = OpBitcast %uint %7
  1875. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_45 %uint_0 [[bitcast_result]] %40
  1876. ; CHECK: OpBranch %43
  1877. ; CHECK: %43 = OpLabel
  1878. ; CHECK: [[phi_result_2:%\w+]] = OpPhi %float [[phi_result_1]] %124 [[null_float]] %45
  1879. ; CHECK: OpStore %b [[phi_result_2]]
  1880. OpReturn
  1881. OpFunctionEnd
  1882. )";
  1883. const std::string new_funcs = kDirectRead2 + kStreamWrite4Frag + kDirectRead4;
  1884. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1885. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  1886. true, 7u, 23u, true, true, false,
  1887. false, false);
  1888. }
  1889. TEST_F(InstBindlessTest, InstBoundsAndInitLoadUnsizedSSBOArrayDeprecated) {
  1890. // #version 450
  1891. // #extension GL_EXT_nonuniform_qualifier : enable
  1892. //
  1893. // layout(location=0) in nonuniformEXT flat int nu_ii;
  1894. // layout(location=0) out float b;
  1895. //
  1896. // layout(binding=3) buffer bname { float b; } storageBuffer[];
  1897. //
  1898. // void main()
  1899. // {
  1900. // b = storageBuffer[nu_ii].b;
  1901. // }
  1902. // clang-format off
  1903. const std::string defs = R"(
  1904. OpCapability Shader
  1905. OpCapability ShaderNonUniform
  1906. OpCapability RuntimeDescriptorArray
  1907. OpCapability StorageBufferArrayNonUniformIndexing
  1908. OpExtension "SPV_EXT_descriptor_indexing"
  1909. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  1910. %1 = OpExtInstImport "GLSL.std.450"
  1911. OpMemoryModel Logical GLSL450
  1912. OpEntryPoint Fragment %main "main" %b %nu_ii
  1913. ; CHECK: OpEntryPoint Fragment %main "main" %b %nu_ii %gl_FragCoord
  1914. OpExecutionMode %main OriginUpperLeft
  1915. OpSource GLSL 450
  1916. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  1917. OpName %main "main"
  1918. OpName %b "b"
  1919. OpName %bname "bname"
  1920. OpMemberName %bname 0 "a"
  1921. OpName %storageBuffer "storageBuffer"
  1922. OpName %nu_ii "nu_ii"
  1923. OpDecorate %b Location 0
  1924. OpMemberDecorate %bname 0 Offset 0
  1925. OpDecorate %bname Block
  1926. OpDecorate %storageBuffer DescriptorSet 0
  1927. OpDecorate %storageBuffer Binding 3
  1928. OpDecorate %nu_ii Flat
  1929. OpDecorate %nu_ii Location 0
  1930. OpDecorate %nu_ii NonUniform
  1931. OpDecorate %16 NonUniform
  1932. OpDecorate %20 NonUniform
  1933. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  1934. )" + kInputDecorations + R"(
  1935. ; CHECK: OpDecorate %130 NonUniform
  1936. )" + kOutputDecorations + R"(
  1937. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  1938. ; CHECK: OpDecorate %127 NonUniform
  1939. %void = OpTypeVoid
  1940. %3 = OpTypeFunction %void
  1941. %float = OpTypeFloat 32
  1942. %_ptr_Output_float = OpTypePointer Output %float
  1943. %b = OpVariable %_ptr_Output_float Output
  1944. %bname = OpTypeStruct %float
  1945. %_runtimearr_bname = OpTypeRuntimeArray %bname
  1946. %_ptr_StorageBuffer__runtimearr_bname = OpTypePointer StorageBuffer %_runtimearr_bname
  1947. %storageBuffer = OpVariable %_ptr_StorageBuffer__runtimearr_bname StorageBuffer
  1948. %int = OpTypeInt 32 1
  1949. %_ptr_Input_int = OpTypePointer Input %int
  1950. %nu_ii = OpVariable %_ptr_Input_int Input
  1951. %int_0 = OpConstant %int 0
  1952. %_ptr_StorageBuffer_float = OpTypePointer StorageBuffer %float
  1953. ; CHECK: %uint = OpTypeInt 32 0
  1954. ; CHECK: %26 = OpTypeFunction %uint %uint %uint
  1955. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  1956. )" + kInputGlobals + R"(
  1957. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  1958. ; CHECK: %bool = OpTypeBool
  1959. )" + kOutputGlobals + R"(
  1960. ; CHECK: %v4float = OpTypeVector %float 4
  1961. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  1962. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  1963. ; CHECK: %v4uint = OpTypeVector %uint 4
  1964. ; CHECK: [[null_float:%\w+]] = OpConstantNull %float
  1965. )";
  1966. // clang-format on
  1967. const std::string main_func = R"(
  1968. %main = OpFunction %void None %3
  1969. %5 = OpLabel
  1970. %16 = OpLoad %int %nu_ii
  1971. %19 = OpAccessChain %_ptr_StorageBuffer_float %storageBuffer %16 %int_0
  1972. %20 = OpLoad %float %19
  1973. OpStore %b %20
  1974. ; CHECK-NOT: %20 = OpLoad %float %19
  1975. ; CHECK-NOT: OpStore %b %20
  1976. ; CHECK: %40 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_3
  1977. ; CHECK: %42 = OpULessThan %bool %7 %40
  1978. ; CHECK: OpSelectionMerge %43 None
  1979. ; CHECK: OpBranchConditional %42 %44 %45
  1980. ; CHECK: %44 = OpLabel
  1981. ; CHECK: [[bitcast_result_1:%\w+]] = OpBitcast %uint %7
  1982. ; CHECK: %122 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_3 [[bitcast_result_1]]
  1983. ; CHECK: %123 = OpULessThan %bool %uint_0 %122
  1984. ; CHECK: OpSelectionMerge %124 None
  1985. ; CHECK: OpBranchConditional %123 %125 %126
  1986. ; CHECK: %125 = OpLabel
  1987. ; CHECK: %127 = OpLoad %float %20
  1988. ; CHECK: OpBranch %124
  1989. ; CHECK: %126 = OpLabel
  1990. ; CHECK: %128 = OpBitcast %uint %7
  1991. ; CHECK: %129 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_45 %uint_1 %128 %uint_0
  1992. ; CHECK: OpBranch %124
  1993. ; CHECK: %124 = OpLabel
  1994. ; CHECK: [[phi_result_1:%\w+]] = OpPhi %float %127 %125 [[null_float]] %126
  1995. ; CHECK: OpBranch %43
  1996. ; CHECK: %45 = OpLabel
  1997. ; CHECK: [[bitcast_result_2:%\w+]] = OpBitcast %uint %7
  1998. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_45 %uint_0 [[bitcast_result_2]] %40
  1999. ; CHECK: OpBranch %43
  2000. ; CHECK: %43 = OpLabel
  2001. ; CHECK: [[phi_result_2:%\w+]] = OpPhi %float [[phi_result_1]] %124 [[null_float]] %45
  2002. ; CHECK: OpStore %b [[phi_result_2]]
  2003. OpReturn
  2004. OpFunctionEnd
  2005. )";
  2006. const std::string new_funcs = kDirectRead2 + kStreamWrite4Frag + kDirectRead4;
  2007. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2008. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  2009. true, 7u, 23u, true, true, false,
  2010. false, false);
  2011. }
  2012. TEST_F(InstBindlessTest, InstBoundsAndInitLoadUnsizedSSBOArray) {
  2013. // Same as Deprecated but declaring as StorageBuffer Block
  2014. // clang-format off
  2015. const std::string defs = R"(
  2016. OpCapability Shader
  2017. OpCapability ShaderNonUniform
  2018. OpCapability RuntimeDescriptorArray
  2019. OpCapability StorageBufferArrayNonUniformIndexing
  2020. OpExtension "SPV_EXT_descriptor_indexing"
  2021. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  2022. %1 = OpExtInstImport "GLSL.std.450"
  2023. OpMemoryModel Logical GLSL450
  2024. OpEntryPoint Fragment %main "main" %b %nu_ii
  2025. ; CHECK: OpEntryPoint Fragment %main "main" %b %nu_ii %gl_FragCoord
  2026. OpExecutionMode %main OriginUpperLeft
  2027. OpSource GLSL 450
  2028. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  2029. OpName %main "main"
  2030. OpName %b "b"
  2031. OpName %bname "bname"
  2032. OpMemberName %bname 0 "a"
  2033. OpName %storageBuffer "storageBuffer"
  2034. OpName %nu_ii "nu_ii"
  2035. OpDecorate %b Location 0
  2036. OpMemberDecorate %bname 0 Offset 0
  2037. OpDecorate %bname Block
  2038. OpDecorate %storageBuffer DescriptorSet 0
  2039. OpDecorate %storageBuffer Binding 3
  2040. OpDecorate %nu_ii Flat
  2041. OpDecorate %nu_ii Location 0
  2042. OpDecorate %nu_ii NonUniform
  2043. OpDecorate %16 NonUniform
  2044. OpDecorate %20 NonUniform
  2045. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  2046. )" + kInputDecorations + R"(
  2047. ; CHECK: OpDecorate %130 NonUniform
  2048. )" + kOutputDecorations + R"(
  2049. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  2050. ; CHECK: OpDecorate %127 NonUniform
  2051. %void = OpTypeVoid
  2052. %3 = OpTypeFunction %void
  2053. %float = OpTypeFloat 32
  2054. %_ptr_Output_float = OpTypePointer Output %float
  2055. %b = OpVariable %_ptr_Output_float Output
  2056. %bname = OpTypeStruct %float
  2057. %_runtimearr_bname = OpTypeRuntimeArray %bname
  2058. %_ptr_StorageBuffer__runtimearr_bname = OpTypePointer StorageBuffer %_runtimearr_bname
  2059. %storageBuffer = OpVariable %_ptr_StorageBuffer__runtimearr_bname StorageBuffer
  2060. %int = OpTypeInt 32 1
  2061. %_ptr_Input_int = OpTypePointer Input %int
  2062. %nu_ii = OpVariable %_ptr_Input_int Input
  2063. %int_0 = OpConstant %int 0
  2064. %_ptr_StorageBuffer_float = OpTypePointer StorageBuffer %float
  2065. ; CHECK: %uint = OpTypeInt 32 0
  2066. ; CHECK: %26 = OpTypeFunction %uint %uint %uint
  2067. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  2068. )" + kInputGlobals + R"(
  2069. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  2070. ; CHECK: %bool = OpTypeBool
  2071. )" + kOutputGlobals + R"(
  2072. ; CHECK: %v4float = OpTypeVector %float 4
  2073. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  2074. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  2075. ; CHECK: %v4uint = OpTypeVector %uint 4
  2076. ; CHECK: [[null_float:%\w+]] = OpConstantNull %float
  2077. )";
  2078. // clang-format on
  2079. const std::string main_func = R"(
  2080. %main = OpFunction %void None %3
  2081. %5 = OpLabel
  2082. %16 = OpLoad %int %nu_ii
  2083. %19 = OpAccessChain %_ptr_StorageBuffer_float %storageBuffer %16 %int_0
  2084. %20 = OpLoad %float %19
  2085. OpStore %b %20
  2086. ; CHECK-NOT: %20 = OpLoad %float %19
  2087. ; CHECK-NOT: OpStore %b %20
  2088. ; CHECK: %40 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_3
  2089. ; CHECK: %42 = OpULessThan %bool %7 %40
  2090. ; CHECK: OpSelectionMerge %43 None
  2091. ; CHECK: OpBranchConditional %42 %44 %45
  2092. ; CHECK: %44 = OpLabel
  2093. ; CHECK: %103 = OpBitcast %uint %7
  2094. ; CHECK: %122 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_3 %103
  2095. ; CHECK: %123 = OpULessThan %bool %uint_0 %122
  2096. ; CHECK: OpSelectionMerge %124 None
  2097. ; CHECK: OpBranchConditional %123 %125 %126
  2098. ; CHECK: %125 = OpLabel
  2099. ; CHECK: %127 = OpLoad %float %20
  2100. ; CHECK: OpBranch %124
  2101. ; CHECK: %126 = OpLabel
  2102. ; CHECK: %128 = OpBitcast %uint %7
  2103. ; CHECK: %129 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_45 %uint_1 %128 %uint_0
  2104. ; CHECK: OpBranch %124
  2105. ; CHECK: %124 = OpLabel
  2106. ; CHECK: [[phi_result_1:%\w+]] = OpPhi %float %127 %125 [[null_float]] %126
  2107. ; CHECK: OpBranch %43
  2108. ; CHECK: %45 = OpLabel
  2109. ; CHECK: [[bitcast_result:%\w+]] = OpBitcast %uint %7
  2110. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_45 %uint_0 [[bitcast_result]] %40
  2111. ; CHECK: OpBranch %43
  2112. ; CHECK: %43 = OpLabel
  2113. ; CHECK: [[phi_result_2:%\w+]] = OpPhi %float [[phi_result_1]] %124 [[null_float]] %45
  2114. ; CHECK: OpStore %b [[phi_result_2]]
  2115. OpReturn
  2116. OpFunctionEnd
  2117. )";
  2118. const std::string new_funcs = kDirectRead2 + kStreamWrite4Frag + kDirectRead4;
  2119. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2120. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  2121. true, 7u, 23u, true, true, false,
  2122. false, false);
  2123. }
  2124. TEST_F(InstBindlessTest, InstInitLoadUBOScalar) {
  2125. // #version 450
  2126. // #extension GL_EXT_nonuniform_qualifier : enable
  2127. //
  2128. // layout(location=0) out float b;
  2129. // layout(binding=3) uniform uname { float a; } uniformBuffer;
  2130. //
  2131. // void main()
  2132. // {
  2133. // b = uniformBuffer.a;
  2134. // }
  2135. // clang-format off
  2136. const std::string defs = R"(
  2137. OpCapability Shader
  2138. OpExtension "SPV_EXT_descriptor_indexing"
  2139. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  2140. %1 = OpExtInstImport "GLSL.std.450"
  2141. OpMemoryModel Logical GLSL450
  2142. OpEntryPoint Fragment %main "main" %b
  2143. ; CHECK: OpEntryPoint Fragment %main "main" %b %gl_FragCoord
  2144. OpExecutionMode %main OriginUpperLeft
  2145. OpSource GLSL 450
  2146. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  2147. OpName %main "main"
  2148. OpName %b "b"
  2149. OpName %uname "uname"
  2150. OpMemberName %uname 0 "a"
  2151. OpName %uniformBuffer "uniformBuffer"
  2152. OpDecorate %b Location 0
  2153. OpMemberDecorate %uname 0 Offset 0
  2154. OpDecorate %uname Block
  2155. OpDecorate %uniformBuffer DescriptorSet 0
  2156. OpDecorate %uniformBuffer Binding 3
  2157. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  2158. )" + kInputDecorations + kOutputDecorations + R"(
  2159. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  2160. %void = OpTypeVoid
  2161. %3 = OpTypeFunction %void
  2162. %float = OpTypeFloat 32
  2163. %_ptr_Output_float = OpTypePointer Output %float
  2164. %b = OpVariable %_ptr_Output_float Output
  2165. %uname = OpTypeStruct %float
  2166. %_ptr_Uniform_uname = OpTypePointer Uniform %uname
  2167. %uniformBuffer = OpVariable %_ptr_Uniform_uname Uniform
  2168. %int = OpTypeInt 32 1
  2169. %int_0 = OpConstant %int 0
  2170. %_ptr_Uniform_float = OpTypePointer Uniform %float
  2171. ; CHECK: %int = OpTypeInt 32 1
  2172. ; CHECK: %_ptr_Uniform_float = OpTypePointer Uniform %float
  2173. ; CHECK: %uint = OpTypeInt 32 0
  2174. ; CHECK: %21 = OpTypeFunction %uint %uint %uint %uint %uint
  2175. ; CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  2176. )" + kInputGlobals + R"(
  2177. ; CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  2178. ; CHECK: %bool = OpTypeBool
  2179. )" + kOutputGlobals + R"(
  2180. ; CHECK: %v4float = OpTypeVector %float 4
  2181. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  2182. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  2183. ; CHECK: %v4uint = OpTypeVector %uint 4
  2184. ; CHECK: [[null_float:%\w+]] = OpConstantNull %float
  2185. )";
  2186. // clang-format on
  2187. const std::string main_func = R"(
  2188. %main = OpFunction %void None %3
  2189. %5 = OpLabel
  2190. %15 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %int_0
  2191. %16 = OpLoad %float %15
  2192. OpStore %b %16
  2193. ; CHECK-NOT: %16 = OpLoad %float %15
  2194. ; CHECK-NOT: OpStore %b %16
  2195. ; CHECK: %43 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_3 %uint_0
  2196. ; CHECK: %45 = OpULessThan %bool %uint_0 %43
  2197. ; CHECK: OpSelectionMerge %47 None
  2198. ; CHECK: OpBranchConditional %45 %48 %49
  2199. ; CHECK: %48 = OpLabel
  2200. ; CHECK: %50 = OpLoad %float %15
  2201. ; CHECK: OpBranch %47
  2202. ; CHECK: %49 = OpLabel
  2203. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_32 %uint_1 %uint_0 %uint_0
  2204. ; CHECK: OpBranch %47
  2205. ; CHECK: %47 = OpLabel
  2206. ; CHECK: [[phi_result:%\w+]] = OpPhi %float %50 %48 [[null_float]] %49
  2207. ; CHECK: OpStore %b [[phi_result]]
  2208. OpReturn
  2209. OpFunctionEnd
  2210. )";
  2211. const std::string new_funcs = kDirectRead4 + kStreamWrite4Frag;
  2212. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2213. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  2214. true, 7u, 23u, true, true, false,
  2215. false, false);
  2216. }
  2217. TEST_F(InstBindlessTest, InstBoundsInitStoreUnsizedSSBOArray) {
  2218. // #version 450
  2219. // #extension GL_EXT_nonuniform_qualifier : enable
  2220. //
  2221. // layout(location=0) in nonuniformEXT flat int nu_ii;
  2222. // layout(location=1) in float b;
  2223. //
  2224. // layout(binding=4) buffer bname { float b; } storageBuffer[];
  2225. //
  2226. // void main()
  2227. // {
  2228. // storageBuffer[nu_ii].b = b;
  2229. // }
  2230. // clang-format off
  2231. const std::string defs = R"(OpCapability Shader
  2232. OpCapability ShaderNonUniform
  2233. OpCapability RuntimeDescriptorArray
  2234. OpCapability StorageBufferArrayNonUniformIndexing
  2235. OpExtension "SPV_EXT_descriptor_indexing"
  2236. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  2237. %1 = OpExtInstImport "GLSL.std.450"
  2238. OpMemoryModel Logical GLSL450
  2239. OpEntryPoint Fragment %main "main" %nu_ii %b
  2240. ; CHECK: OpEntryPoint Fragment %main "main" %nu_ii %b %gl_FragCoord
  2241. OpExecutionMode %main OriginUpperLeft
  2242. OpSource GLSL 450
  2243. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  2244. OpName %main "main"
  2245. OpName %bname "bname"
  2246. OpMemberName %bname 0 "b"
  2247. OpName %storageBuffer "storageBuffer"
  2248. OpName %nu_ii "nu_ii"
  2249. OpName %b "b"
  2250. OpMemberDecorate %bname 0 Offset 0
  2251. OpDecorate %bname BufferBlock
  2252. OpDecorate %storageBuffer DescriptorSet 0
  2253. OpDecorate %storageBuffer Binding 4
  2254. OpDecorate %nu_ii Flat
  2255. OpDecorate %nu_ii Location 0
  2256. OpDecorate %nu_ii NonUniform
  2257. OpDecorate %14 NonUniform
  2258. OpDecorate %b Location 1
  2259. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  2260. )" + kInputDecorations + kOutputDecorations + R"(
  2261. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  2262. %void = OpTypeVoid
  2263. %3 = OpTypeFunction %void
  2264. %float = OpTypeFloat 32
  2265. %bname = OpTypeStruct %float
  2266. %_runtimearr_bname = OpTypeRuntimeArray %bname
  2267. %_ptr_Uniform__runtimearr_bname = OpTypePointer Uniform %_runtimearr_bname
  2268. %storageBuffer = OpVariable %_ptr_Uniform__runtimearr_bname Uniform
  2269. %int = OpTypeInt 32 1
  2270. %_ptr_Input_int = OpTypePointer Input %int
  2271. %nu_ii = OpVariable %_ptr_Input_int Input
  2272. %int_0 = OpConstant %int 0
  2273. %_ptr_Input_float = OpTypePointer Input %float
  2274. %b = OpVariable %_ptr_Input_float Input
  2275. %_ptr_Uniform_float = OpTypePointer Uniform %float
  2276. )" + kInputGlobals + kOutputGlobals + R"(
  2277. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  2278. ; CHECK: %v4uint = OpTypeVector %uint 4
  2279. ; CHECK: %102 = OpTypeFunction %uint %uint %uint %uint %uint
  2280. )";
  2281. // clang-format on
  2282. const std::string main_func = R"(
  2283. %main = OpFunction %void None %3
  2284. %5 = OpLabel
  2285. %14 = OpLoad %int %nu_ii
  2286. %18 = OpLoad %float %b
  2287. %20 = OpAccessChain %_ptr_Uniform_float %storageBuffer %14 %int_0
  2288. OpStore %20 %18
  2289. ; CHECK-NOT: OpStore %20 %18
  2290. ; CHECK: %40 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_4
  2291. ; CHECK: %42 = OpULessThan %bool %7 %40
  2292. ; CHECK: OpSelectionMerge %43 None
  2293. ; CHECK: OpBranchConditional %42 %44 %45
  2294. ; CHECK: %44 = OpLabel
  2295. ; CHECK: %100 = OpBitcast %uint %7
  2296. ; CHECK: %119 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_4 %100
  2297. ; CHECK: %120 = OpULessThan %bool %uint_0 %119
  2298. ; CHECK: OpSelectionMerge %121 None
  2299. ; CHECK: OpBranchConditional %120 %122 %123
  2300. ; CHECK: %122 = OpLabel
  2301. ; CHECK: OpStore %20 %19
  2302. ; CHECK: OpBranch %121
  2303. ; CHECK: %123 = OpLabel
  2304. ; CHECK: %124 = OpBitcast %uint %7
  2305. ; CHECK: %125 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_45 %uint_1 %124 %uint_0
  2306. ; CHECK: OpBranch %121
  2307. ; CHECK: %121 = OpLabel
  2308. ; CHECK: OpBranch %43
  2309. ; CHECK: %45 = OpLabel
  2310. ; CHECK: %46 = OpBitcast %uint %7
  2311. ; CHECK: %99 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_45 %uint_0 %46 %40
  2312. ; CHECK: OpBranch %43
  2313. ; CHECK: %43 = OpLabel
  2314. OpReturn
  2315. OpFunctionEnd
  2316. )";
  2317. const std::string new_funcs = kDirectRead2 + kStreamWrite4Frag + kDirectRead4;
  2318. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2319. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  2320. true, 7u, 23u, true, true, false,
  2321. false, false);
  2322. }
  2323. TEST_F(InstBindlessTest, InstBoundsInitLoadSizedUBOArray) {
  2324. // #version 450
  2325. // #extension GL_EXT_nonuniform_qualifier : enable
  2326. //
  2327. // layout(location=0) in nonuniformEXT flat int nu_ii;
  2328. // layout(location=0) out float b;
  2329. //
  2330. // layout(binding=3) uniform uname { float a; } uniformBuffer[128];
  2331. //
  2332. // void main()
  2333. // {
  2334. // b = uniformBuffer[nu_ii].a;
  2335. // }
  2336. // clang-format off
  2337. const std::string defs = R"(
  2338. OpCapability Shader
  2339. OpCapability ShaderNonUniform
  2340. OpCapability UniformBufferArrayNonUniformIndexing
  2341. OpExtension "SPV_EXT_descriptor_indexing"
  2342. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  2343. %1 = OpExtInstImport "GLSL.std.450"
  2344. OpMemoryModel Logical GLSL450
  2345. OpEntryPoint Fragment %main "main" %b %nu_ii
  2346. ; CHECK: OpEntryPoint Fragment %main "main" %b %nu_ii %gl_FragCoord
  2347. OpExecutionMode %main OriginUpperLeft
  2348. OpSource GLSL 450
  2349. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  2350. OpName %main "main"
  2351. OpName %b "b"
  2352. OpName %uname "uname"
  2353. OpMemberName %uname 0 "a"
  2354. OpName %uniformBuffer "uniformBuffer"
  2355. OpName %nu_ii "nu_ii"
  2356. OpDecorate %b Location 0
  2357. OpMemberDecorate %uname 0 Offset 0
  2358. OpDecorate %uname Block
  2359. OpDecorate %uniformBuffer DescriptorSet 0
  2360. OpDecorate %uniformBuffer Binding 3
  2361. OpDecorate %nu_ii Flat
  2362. OpDecorate %nu_ii Location 0
  2363. OpDecorate %nu_ii NonUniform
  2364. OpDecorate %18 NonUniform
  2365. OpDecorate %22 NonUniform
  2366. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  2367. )" + kOutputDecorations + R"(
  2368. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  2369. )" + kInputDecorations + R"(
  2370. ; CHECK: OpDecorate %117 NonUniform
  2371. %void = OpTypeVoid
  2372. %3 = OpTypeFunction %void
  2373. %float = OpTypeFloat 32
  2374. %_ptr_Output_float = OpTypePointer Output %float
  2375. %b = OpVariable %_ptr_Output_float Output
  2376. %uname = OpTypeStruct %float
  2377. %uint = OpTypeInt 32 0
  2378. %uint_128 = OpConstant %uint 128
  2379. %_arr_uname_uint_128 = OpTypeArray %uname %uint_128
  2380. %_ptr_Uniform__arr_uname_uint_128 = OpTypePointer Uniform %_arr_uname_uint_128
  2381. %uniformBuffer = OpVariable %_ptr_Uniform__arr_uname_uint_128 Uniform
  2382. %int = OpTypeInt 32 1
  2383. %_ptr_Input_int = OpTypePointer Input %int
  2384. %nu_ii = OpVariable %_ptr_Input_int Input
  2385. %int_0 = OpConstant %int 0
  2386. %_ptr_Uniform_float = OpTypePointer Uniform %float
  2387. )" + kOutputGlobals + R"(
  2388. ; CHECK: %v4float = OpTypeVector %float 4
  2389. ; CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  2390. ; CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  2391. ; CHECK: %v4uint = OpTypeVector %uint 4
  2392. ; CHECK: [[null_float:%\w+]] = OpConstantNull %float
  2393. )" + kInputGlobals;
  2394. // clang-format on
  2395. const std::string main_func = R"(
  2396. %main = OpFunction %void None %3
  2397. %5 = OpLabel
  2398. %18 = OpLoad %int %nu_ii
  2399. %21 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %18 %int_0
  2400. %22 = OpLoad %float %21
  2401. OpStore %b %22
  2402. ; CHECK-NOT: %22 = OpLoad %float %21
  2403. ; CHECK-NOT: OpStore %b %22
  2404. ; CHECK: %25 = OpULessThan %bool %7 %uint_128
  2405. ; CHECK: OpSelectionMerge %26 None
  2406. ; CHECK: OpBranchConditional %25 %27 %28
  2407. ; CHECK: %27 = OpLabel
  2408. ; CHECK: %90 = OpBitcast %uint %7
  2409. ; CHECK: %112 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_3 %90
  2410. ; CHECK: %113 = OpULessThan %bool %uint_0 %112
  2411. ; CHECK: OpSelectionMerge %114 None
  2412. ; CHECK: OpBranchConditional %113 %115 %116
  2413. ; CHECK: %115 = OpLabel
  2414. ; CHECK: %117 = OpLoad %float %22
  2415. ; CHECK: OpBranch %114
  2416. ; CHECK: %116 = OpLabel
  2417. ; CHECK: %118 = OpBitcast %uint %7
  2418. ; CHECK: %119 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_46 %uint_1 %118 %uint_0
  2419. ; CHECK: OpBranch %114
  2420. ; CHECK: %114 = OpLabel
  2421. ; CHECK: [[phi_result_1:%\w+]] = OpPhi %float %117 %115 [[null_float]] %116
  2422. ; CHECK: OpBranch %26
  2423. ; CHECK: %28 = OpLabel
  2424. ; CHECK: [[bitcast_result:%\w+]] = OpBitcast %uint %7
  2425. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_46 %uint_0 [[bitcast_result]] %uint_128
  2426. ; CHECK: OpBranch %26
  2427. ; CHECK: %26 = OpLabel
  2428. ; CHECK: [[phi_result_2:%\w+]] = OpPhi %float [[phi_result_1]] %114 [[null_float]] %28
  2429. ; CHECK: OpStore %b [[phi_result_2]]
  2430. OpReturn
  2431. OpFunctionEnd
  2432. )";
  2433. const std::string new_funcs = kStreamWrite4Frag + kDirectRead4;
  2434. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2435. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  2436. true, 7u, 23u, true, true, false,
  2437. false, false);
  2438. }
  2439. TEST_F(InstBindlessTest,
  2440. InstBoundsComputeShaderInitLoadVariableSizedSampledImagesArray) {
  2441. // #version 450
  2442. // #extension GL_EXT_nonuniform_qualifier : enable
  2443. //
  2444. // layout (local_size_x = 1, local_size_y = 1) in;
  2445. //
  2446. // layout(set = 0, binding = 0, std140) buffer Input {
  2447. // uint index;
  2448. // float red;
  2449. // } sbo;
  2450. //
  2451. // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
  2452. //
  2453. // void main()
  2454. // {
  2455. // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
  2456. // }
  2457. // clang-format off
  2458. const std::string defs = R"(
  2459. OpCapability Shader
  2460. OpCapability RuntimeDescriptorArray
  2461. OpExtension "SPV_EXT_descriptor_indexing"
  2462. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  2463. %1 = OpExtInstImport "GLSL.std.450"
  2464. OpMemoryModel Logical GLSL450
  2465. OpEntryPoint GLCompute %main "main"
  2466. ; CHECK: OpEntryPoint GLCompute %main "main" %gl_GlobalInvocationID
  2467. OpExecutionMode %main LocalSize 1 1 1
  2468. OpSource GLSL 450
  2469. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  2470. OpName %main "main"
  2471. OpName %Input "Input"
  2472. OpMemberName %Input 0 "index"
  2473. OpMemberName %Input 1 "red"
  2474. OpName %sbo "sbo"
  2475. OpName %images "images"
  2476. OpMemberDecorate %Input 0 Offset 0
  2477. OpMemberDecorate %Input 1 Offset 4
  2478. OpDecorate %Input BufferBlock
  2479. OpDecorate %sbo DescriptorSet 0
  2480. OpDecorate %sbo Binding 0
  2481. OpDecorate %images DescriptorSet 0
  2482. OpDecorate %images Binding 1
  2483. OpDecorate %images NonWritable
  2484. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  2485. )" + kInputDecorations + kOutputDecorations + R"(
  2486. ; CHECK: OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
  2487. %void = OpTypeVoid
  2488. %3 = OpTypeFunction %void
  2489. %uint = OpTypeInt 32 0
  2490. %float = OpTypeFloat 32
  2491. %Input = OpTypeStruct %uint %float
  2492. %_ptr_Uniform_Input = OpTypePointer Uniform %Input
  2493. %sbo = OpVariable %_ptr_Uniform_Input Uniform
  2494. %int = OpTypeInt 32 1
  2495. %int_1 = OpConstant %int 1
  2496. %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
  2497. %_runtimearr_13 = OpTypeRuntimeArray %13
  2498. %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
  2499. %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
  2500. %int_0 = OpConstant %int 0
  2501. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  2502. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  2503. %v2int = OpTypeVector %int 2
  2504. %25 = OpConstantComposite %v2int %int_0 %int_0
  2505. %v4float = OpTypeVector %float 4
  2506. %uint_0 = OpConstant %uint 0
  2507. %_ptr_Uniform_float = OpTypePointer Uniform %float
  2508. )" + kInputGlobals + kOutputGlobals + R"(
  2509. ; CHECK: %v3uint = OpTypeVector %uint 3
  2510. ; CHECK: %_ptr_Input_v3uint = OpTypePointer Input %v3uint
  2511. ; CHECK: %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
  2512. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  2513. ; CHECK: [[null_uint:%\w+]] = OpConstantNull %uint
  2514. )";
  2515. // clang-format on
  2516. const std::string main_func = R"(
  2517. %main = OpFunction %void None %3
  2518. %5 = OpLabel
  2519. %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
  2520. %20 = OpLoad %uint %19
  2521. %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
  2522. %23 = OpLoad %13 %22
  2523. %27 = OpImageRead %v4float %23 %25
  2524. %29 = OpCompositeExtract %float %27 0
  2525. %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  2526. OpStore %31 %29
  2527. ; CHECK-NOT: OpStore %31 %29
  2528. ; CHECK: %132 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  2529. ; CHECK: %133 = OpULessThan %bool %uint_0 %132
  2530. ; CHECK: OpSelectionMerge %134 None
  2531. ; CHECK: OpBranchConditional %133 %135 %136
  2532. ; CHECK: %135 = OpLabel
  2533. ; CHECK: %137 = OpLoad %uint %25
  2534. ; CHECK: OpBranch %134
  2535. ; CHECK: %136 = OpLabel
  2536. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_47 %uint_1 %uint_0 %uint_0
  2537. ; CHECK: OpBranch %134
  2538. ; CHECK: %134 = OpLabel
  2539. ; CHECK: [[phi_result_1:%\w+]] = OpPhi %uint %137 %135 [[null_uint]] %136
  2540. ; CHECK: %27 = OpAccessChain %_ptr_UniformConstant_13 %images %141
  2541. ; CHECK: %28 = OpLoad %13 %27
  2542. ; CHECK: %48 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_1
  2543. ; CHECK: %50 = OpULessThan %bool %141 %48
  2544. ; CHECK: OpSelectionMerge %51 None
  2545. ; CHECK: OpBranchConditional %50 %52 %53
  2546. ; CHECK: %52 = OpLabel
  2547. ; CHECK: %54 = OpLoad %13 %27
  2548. ; CHECK: {{%\w+}} = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_1 [[phi_result_1]]
  2549. ; CHECK: %143 = OpULessThan %bool %uint_0 %142
  2550. ; CHECK: OpSelectionMerge %144 None
  2551. ; CHECK: OpBranchConditional %143 %145 %146
  2552. ; CHECK: %145 = OpLabel
  2553. ; CHECK: %147 = OpLoad %13 %27
  2554. ; CHECK: %148 = OpImageRead %v4float %147 %20
  2555. ; CHECK: OpBranch %144
  2556. ; CHECK: %146 = OpLabel
  2557. ; CHECK: %149 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_50 %uint_1 %141 %uint_0
  2558. ; CHECK: OpBranch %144
  2559. ; CHECK: %144 = OpLabel
  2560. ; CHECK: [[phi_result_2:%\w+]] = OpPhi %v4float %148 %145 [[null_v4float]] %146
  2561. ; CHECK: OpBranch %51
  2562. ; CHECK: %53 = OpLabel
  2563. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_50 %uint_0 %141 %48
  2564. ; CHECK: OpBranch %51
  2565. ; CHECK: %51 = OpLabel
  2566. ; CHECK: %113 = OpPhi %v4float [[phi_result_2]] %144 [[null_v4float]] %53
  2567. ; CHECK: %30 = OpCompositeExtract %float %113 0
  2568. ; CHECK: %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  2569. ; CHECK: %151 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  2570. ; CHECK: %152 = OpULessThan %bool %uint_0 %151
  2571. ; CHECK: OpSelectionMerge %153 None
  2572. ; CHECK: OpBranchConditional %152 %154 %155
  2573. ; CHECK: %154 = OpLabel
  2574. ; CHECK: OpStore %31 %30
  2575. ; CHECK: OpBranch %153
  2576. ; CHECK: %155 = OpLabel
  2577. ; CHECK: %157 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_53 %uint_1 %uint_0 %uint_0
  2578. ; CHECK: OpBranch %153
  2579. ; CHECK: %153 = OpLabel
  2580. OpReturn
  2581. OpFunctionEnd
  2582. )";
  2583. const std::string new_funcs =
  2584. kDirectRead2 + kStreamWrite4Compute + kDirectRead4;
  2585. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2586. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  2587. true, 7u, 23u, true, true, false,
  2588. false, false);
  2589. }
  2590. TEST_F(InstBindlessTest,
  2591. InstBoundsRayGenerationInitLoadVariableSizedSampledImagesArray) {
  2592. // #version 460
  2593. // #extension GL_EXT_nonuniform_qualifier : require
  2594. // #extension GL_NV_ray_tracing : require
  2595. //
  2596. // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
  2597. // uint index;
  2598. // float red;
  2599. // } sbo;
  2600. //
  2601. // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
  2602. //
  2603. // void main()
  2604. // {
  2605. // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
  2606. // }
  2607. // clang-format off
  2608. const std::string defs = R"(
  2609. OpCapability RuntimeDescriptorArray
  2610. OpCapability RayTracingNV
  2611. OpExtension "SPV_EXT_descriptor_indexing"
  2612. OpExtension "SPV_NV_ray_tracing"
  2613. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  2614. %1 = OpExtInstImport "GLSL.std.450"
  2615. OpMemoryModel Logical GLSL450
  2616. OpEntryPoint RayGenerationNV %main "main"
  2617. ; CHECK: OpEntryPoint RayGenerationNV %main "main" [[launch_id:%\w+]]
  2618. OpSource GLSL 460
  2619. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  2620. OpSourceExtension "GL_NV_ray_tracing"
  2621. OpName %main "main"
  2622. OpName %StorageBuffer "StorageBuffer"
  2623. OpMemberName %StorageBuffer 0 "index"
  2624. OpMemberName %StorageBuffer 1 "red"
  2625. OpName %sbo "sbo"
  2626. OpName %images "images"
  2627. OpMemberDecorate %StorageBuffer 0 Offset 0
  2628. OpMemberDecorate %StorageBuffer 1 Offset 4
  2629. OpDecorate %StorageBuffer BufferBlock
  2630. OpDecorate %sbo DescriptorSet 0
  2631. OpDecorate %sbo Binding 0
  2632. OpDecorate %images DescriptorSet 0
  2633. OpDecorate %images Binding 1
  2634. OpDecorate %images NonWritable
  2635. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  2636. )" + kInputDecorations + kOutputDecorations + R"(
  2637. ; CHECK: OpDecorate [[launch_id]] BuiltIn LaunchIdNV
  2638. %void = OpTypeVoid
  2639. %3 = OpTypeFunction %void
  2640. %uint = OpTypeInt 32 0
  2641. %float = OpTypeFloat 32
  2642. %StorageBuffer = OpTypeStruct %uint %float
  2643. %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
  2644. %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
  2645. %int = OpTypeInt 32 1
  2646. %int_1 = OpConstant %int 1
  2647. %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
  2648. %_runtimearr_13 = OpTypeRuntimeArray %13
  2649. %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
  2650. %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
  2651. %int_0 = OpConstant %int 0
  2652. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  2653. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  2654. %v2int = OpTypeVector %int 2
  2655. %25 = OpConstantComposite %v2int %int_0 %int_0
  2656. %v4float = OpTypeVector %float 4
  2657. %uint_0 = OpConstant %uint 0
  2658. %_ptr_Uniform_float = OpTypePointer Uniform %float
  2659. )" + kInputGlobals + kOutputGlobals + R"(
  2660. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  2661. ; CHECK: [[null_uint:%\w+]] = OpConstantNull %uint
  2662. )";
  2663. // clang-format on
  2664. const std::string main_func = R"(
  2665. %main = OpFunction %void None %3
  2666. %5 = OpLabel
  2667. %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
  2668. %20 = OpLoad %uint %19
  2669. %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
  2670. %23 = OpLoad %13 %22
  2671. %27 = OpImageRead %v4float %23 %25
  2672. %29 = OpCompositeExtract %float %27 0
  2673. %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  2674. OpStore %31 %29
  2675. ; CHECK-NOT: OpStore %31 %29
  2676. ; CHECK: %133 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  2677. ; CHECK: %134 = OpULessThan %bool %uint_0 %133
  2678. ; CHECK: OpSelectionMerge %135 None
  2679. ; CHECK: OpBranchConditional %134 %136 %137
  2680. ; CHECK: %136 = OpLabel
  2681. ; CHECK: %138 = OpLoad %uint %25
  2682. ; CHECK: OpBranch %135
  2683. ; CHECK: %137 = OpLabel
  2684. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_48 %uint_1 %uint_0 %uint_0
  2685. ; CHECK: OpBranch %135
  2686. ; CHECK: %135 = OpLabel
  2687. ; CHECK: %142 = OpPhi %uint %138 %136 [[null_uint]] %137
  2688. ; CHECK: %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
  2689. ; CHECK: %28 = OpLoad %13 %27
  2690. ; CHECK: %48 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_1
  2691. ; CHECK: %50 = OpULessThan %bool %142 %48
  2692. ; CHECK: OpSelectionMerge %51 None
  2693. ; CHECK: OpBranchConditional %50 %52 %53
  2694. ; CHECK: %52 = OpLabel
  2695. ; CHECK: %54 = OpLoad %13 %27
  2696. ; CHECK: %143 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_1 %142
  2697. ; CHECK: %144 = OpULessThan %bool %uint_0 %143
  2698. ; CHECK: OpSelectionMerge %145 None
  2699. ; CHECK: OpBranchConditional %144 %146 %147
  2700. ; CHECK: %146 = OpLabel
  2701. ; CHECK: %148 = OpLoad %13 %27
  2702. ; CHECK: %149 = OpImageRead %v4float %148 %20
  2703. ; CHECK: OpBranch %145
  2704. ; CHECK: %147 = OpLabel
  2705. ; CHECK: %150 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_1 %142 %uint_0
  2706. ; CHECK: OpBranch %145
  2707. ; CHECK: %145 = OpLabel
  2708. ; CHECK: %151 = OpPhi %v4float %149 %146 [[null_v4float]] %147
  2709. ; CHECK: OpBranch %51
  2710. ; CHECK: %53 = OpLabel
  2711. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_0 %142 %48
  2712. ; CHECK: OpBranch %51
  2713. ; CHECK: %51 = OpLabel
  2714. ; CHECK: %114 = OpPhi %v4float %151 %145 [[null_v4float]] %53
  2715. ; CHECK: %30 = OpCompositeExtract %float %114 0
  2716. ; CHECK: %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  2717. ; CHECK: %152 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  2718. ; CHECK: %153 = OpULessThan %bool %uint_0 %152
  2719. ; CHECK: OpSelectionMerge %154 None
  2720. ; CHECK: OpBranchConditional %153 %155 %156
  2721. ; CHECK: %155 = OpLabel
  2722. ; CHECK: OpStore %31 %30
  2723. ; CHECK: OpBranch %154
  2724. ; CHECK: %156 = OpLabel
  2725. ; CHECK: %158 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_54 %uint_1 %uint_0 %uint_0
  2726. ; CHECK: OpBranch %154
  2727. ; CHECK: %154 = OpLabel
  2728. OpReturn
  2729. OpFunctionEnd
  2730. )";
  2731. const std::string new_funcs = kDirectRead2 + kStreamWrite4Ray + kDirectRead4;
  2732. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2733. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  2734. true, 7u, 23u, true, true, false,
  2735. false, false);
  2736. }
  2737. TEST_F(InstBindlessTest,
  2738. InstBoundsIntersectionInitLoadVariableSizedSampledImagesArray) {
  2739. // #version 460
  2740. // #extension GL_EXT_nonuniform_qualifier : require
  2741. // #extension GL_NV_ray_tracing : require
  2742. //
  2743. // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
  2744. // uint index;
  2745. // float red;
  2746. // } sbo;
  2747. //
  2748. // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
  2749. //
  2750. // void main()
  2751. // {
  2752. // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
  2753. // }
  2754. // clang-format off
  2755. const std::string defs = R"(
  2756. OpCapability RuntimeDescriptorArray
  2757. OpCapability RayTracingNV
  2758. OpExtension "SPV_EXT_descriptor_indexing"
  2759. OpExtension "SPV_NV_ray_tracing"
  2760. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  2761. %1 = OpExtInstImport "GLSL.std.450"
  2762. OpMemoryModel Logical GLSL450
  2763. OpEntryPoint IntersectionNV %main "main"
  2764. ; CHECK: OpEntryPoint IntersectionNV %main "main" [[launch_id:%\w+]]
  2765. OpSource GLSL 460
  2766. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  2767. OpSourceExtension "GL_NV_ray_tracing"
  2768. OpName %main "main"
  2769. OpName %StorageBuffer "StorageBuffer"
  2770. OpMemberName %StorageBuffer 0 "index"
  2771. OpMemberName %StorageBuffer 1 "red"
  2772. OpName %sbo "sbo"
  2773. OpName %images "images"
  2774. OpMemberDecorate %StorageBuffer 0 Offset 0
  2775. OpMemberDecorate %StorageBuffer 1 Offset 4
  2776. OpDecorate %StorageBuffer BufferBlock
  2777. OpDecorate %sbo DescriptorSet 0
  2778. OpDecorate %sbo Binding 0
  2779. OpDecorate %images DescriptorSet 0
  2780. OpDecorate %images Binding 1
  2781. OpDecorate %images NonWritable
  2782. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  2783. )" + kInputDecorations + kOutputDecorations + R"(
  2784. ; CHECK: OpDecorate [[launch_id]] BuiltIn LaunchIdNV
  2785. %void = OpTypeVoid
  2786. %3 = OpTypeFunction %void
  2787. %uint = OpTypeInt 32 0
  2788. %float = OpTypeFloat 32
  2789. %StorageBuffer = OpTypeStruct %uint %float
  2790. %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
  2791. %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
  2792. %int = OpTypeInt 32 1
  2793. %int_1 = OpConstant %int 1
  2794. %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
  2795. %_runtimearr_13 = OpTypeRuntimeArray %13
  2796. %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
  2797. %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
  2798. %int_0 = OpConstant %int 0
  2799. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  2800. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  2801. %v2int = OpTypeVector %int 2
  2802. %25 = OpConstantComposite %v2int %int_0 %int_0
  2803. %v4float = OpTypeVector %float 4
  2804. %uint_0 = OpConstant %uint 0
  2805. %_ptr_Uniform_float = OpTypePointer Uniform %float
  2806. )" + kInputGlobals + kOutputGlobals + R"(
  2807. ; CHECK: [[launch_id]] = OpVariable %_ptr_Input_v3uint Input
  2808. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  2809. ; CHECK: [[null_uint:%\w+]] = OpConstantNull %uint
  2810. )";
  2811. // clang-format on
  2812. const std::string main_func = R"(
  2813. %main = OpFunction %void None %3
  2814. %5 = OpLabel
  2815. %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
  2816. %20 = OpLoad %uint %19
  2817. %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
  2818. %23 = OpLoad %13 %22
  2819. %27 = OpImageRead %v4float %23 %25
  2820. %29 = OpCompositeExtract %float %27 0
  2821. %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  2822. OpStore %31 %29
  2823. ; CHECK-NOT: OpStore %31 %29
  2824. ; CHECK: %133 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  2825. ; CHECK: %134 = OpULessThan %bool %uint_0 %133
  2826. ; CHECK: OpSelectionMerge %135 None
  2827. ; CHECK: OpBranchConditional %134 %136 %137
  2828. ; CHECK: %136 = OpLabel
  2829. ; CHECK: %138 = OpLoad %uint %25
  2830. ; CHECK: OpBranch %135
  2831. ; CHECK: %137 = OpLabel
  2832. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_48 %uint_1 %uint_0 %uint_0
  2833. ; CHECK: OpBranch %135
  2834. ; CHECK: %135 = OpLabel
  2835. ; CHECK: %142 = OpPhi %uint %138 %136 [[null_uint]] %137
  2836. ; CHECK: %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
  2837. ; CHECK: %28 = OpLoad %13 %27
  2838. ; CHECK: %48 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_1
  2839. ; CHECK: %50 = OpULessThan %bool %142 %48
  2840. ; CHECK: OpSelectionMerge %51 None
  2841. ; CHECK: OpBranchConditional %50 %52 %53
  2842. ; CHECK: %52 = OpLabel
  2843. ; CHECK: %54 = OpLoad %13 %27
  2844. ; CHECK: %143 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_1 %142
  2845. ; CHECK: %144 = OpULessThan %bool %uint_0 %143
  2846. ; CHECK: OpSelectionMerge %145 None
  2847. ; CHECK: OpBranchConditional %144 %146 %147
  2848. ; CHECK: %146 = OpLabel
  2849. ; CHECK: %148 = OpLoad %13 %27
  2850. ; CHECK: %149 = OpImageRead %v4float %148 %20
  2851. ; CHECK: OpBranch %145
  2852. ; CHECK: %147 = OpLabel
  2853. ; CHECK: %150 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_1 %142 %uint_0
  2854. ; CHECK: OpBranch %145
  2855. ; CHECK: %145 = OpLabel
  2856. ; CHECK: %151 = OpPhi %v4float %149 %146 [[null_v4float]] %147
  2857. ; CHECK: OpBranch %51
  2858. ; CHECK: %53 = OpLabel
  2859. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_0 %142 %48
  2860. ; CHECK: OpBranch %51
  2861. ; CHECK: %51 = OpLabel
  2862. ; CHECK: %114 = OpPhi %v4float %151 %145 [[null_v4float]] %53
  2863. ; CHECK: %30 = OpCompositeExtract %float %114 0
  2864. ; CHECK: %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  2865. ; CHECK: %152 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  2866. ; CHECK: %153 = OpULessThan %bool %uint_0 %152
  2867. ; CHECK: OpSelectionMerge %154 None
  2868. ; CHECK: OpBranchConditional %153 %155 %156
  2869. ; CHECK: %155 = OpLabel
  2870. ; CHECK: OpStore %31 %30
  2871. ; CHECK: OpBranch %154
  2872. ; CHECK: %156 = OpLabel
  2873. ; CHECK: %158 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_54 %uint_1 %uint_0 %uint_0
  2874. ; CHECK: OpBranch %154
  2875. ; CHECK: %154 = OpLabel
  2876. OpReturn
  2877. OpFunctionEnd
  2878. )";
  2879. const std::string new_funcs = kDirectRead2 + kStreamWrite4Ray + kDirectRead4;
  2880. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2881. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  2882. true, 7u, 23u, true, true, false,
  2883. false, false);
  2884. }
  2885. TEST_F(InstBindlessTest,
  2886. InstBoundsAnyHitInitLoadVariableSizedSampledImagesArray) {
  2887. // #version 460
  2888. // #extension GL_EXT_nonuniform_qualifier : require
  2889. // #extension GL_NV_ray_tracing : require
  2890. //
  2891. // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
  2892. // uint index;
  2893. // float red;
  2894. // } sbo;
  2895. //
  2896. // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
  2897. //
  2898. // void main()
  2899. // {
  2900. // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
  2901. // }
  2902. // clang-format off
  2903. const std::string defs = R"(
  2904. OpCapability RuntimeDescriptorArray
  2905. OpCapability RayTracingNV
  2906. OpExtension "SPV_EXT_descriptor_indexing"
  2907. OpExtension "SPV_NV_ray_tracing"
  2908. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  2909. %1 = OpExtInstImport "GLSL.std.450"
  2910. OpMemoryModel Logical GLSL450
  2911. OpEntryPoint AnyHitNV %main "main"
  2912. ; CHECK: OpEntryPoint AnyHitNV %main "main" [[launch_id:%\w+]]
  2913. OpSource GLSL 460
  2914. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  2915. OpSourceExtension "GL_NV_ray_tracing"
  2916. OpName %main "main"
  2917. OpName %StorageBuffer "StorageBuffer"
  2918. OpMemberName %StorageBuffer 0 "index"
  2919. OpMemberName %StorageBuffer 1 "red"
  2920. OpName %sbo "sbo"
  2921. OpName %images "images"
  2922. OpMemberDecorate %StorageBuffer 0 Offset 0
  2923. OpMemberDecorate %StorageBuffer 1 Offset 4
  2924. OpDecorate %StorageBuffer BufferBlock
  2925. OpDecorate %sbo DescriptorSet 0
  2926. OpDecorate %sbo Binding 0
  2927. OpDecorate %images DescriptorSet 0
  2928. OpDecorate %images Binding 1
  2929. OpDecorate %images NonWritable
  2930. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  2931. )" + kInputDecorations + kOutputDecorations + R"(
  2932. ; CHECK: OpDecorate [[launch_id]] BuiltIn LaunchIdNV
  2933. %void = OpTypeVoid
  2934. %3 = OpTypeFunction %void
  2935. %uint = OpTypeInt 32 0
  2936. %float = OpTypeFloat 32
  2937. %StorageBuffer = OpTypeStruct %uint %float
  2938. %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
  2939. %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
  2940. %int = OpTypeInt 32 1
  2941. %int_1 = OpConstant %int 1
  2942. %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
  2943. %_runtimearr_13 = OpTypeRuntimeArray %13
  2944. %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
  2945. %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
  2946. %int_0 = OpConstant %int 0
  2947. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  2948. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  2949. %v2int = OpTypeVector %int 2
  2950. %25 = OpConstantComposite %v2int %int_0 %int_0
  2951. %v4float = OpTypeVector %float 4
  2952. %uint_0 = OpConstant %uint 0
  2953. %_ptr_Uniform_float = OpTypePointer Uniform %float
  2954. )" + kInputGlobals + kOutputGlobals + R"(
  2955. ; CHECK: [[launch_id]] = OpVariable %_ptr_Input_v3uint Input
  2956. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  2957. ; CHECK: [[null_uint:%\w+]] = OpConstantNull %uint
  2958. )";
  2959. // clang-format on
  2960. const std::string main_func = R"(
  2961. %main = OpFunction %void None %3
  2962. %5 = OpLabel
  2963. %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
  2964. %20 = OpLoad %uint %19
  2965. %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
  2966. %23 = OpLoad %13 %22
  2967. %27 = OpImageRead %v4float %23 %25
  2968. %29 = OpCompositeExtract %float %27 0
  2969. %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  2970. OpStore %31 %29
  2971. ; CHECK-NOT: OpStore %31 %29
  2972. ; CHECK: %133 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  2973. ; CHECK: %134 = OpULessThan %bool %uint_0 %133
  2974. ; CHECK: OpSelectionMerge %135 None
  2975. ; CHECK: OpBranchConditional %134 %136 %137
  2976. ; CHECK: %136 = OpLabel
  2977. ; CHECK: %138 = OpLoad %uint %25
  2978. ; CHECK: OpBranch %135
  2979. ; CHECK: %137 = OpLabel
  2980. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_48 %uint_1 %uint_0 %uint_0
  2981. ; CHECK: OpBranch %135
  2982. ; CHECK: %135 = OpLabel
  2983. ; CHECK: %142 = OpPhi %uint %138 %136 [[null_uint]] %137
  2984. ; CHECK: %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
  2985. ; CHECK: %28 = OpLoad %13 %27
  2986. ; CHECK: %48 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_1
  2987. ; CHECK: %50 = OpULessThan %bool %142 %48
  2988. ; CHECK: OpSelectionMerge %51 None
  2989. ; CHECK: OpBranchConditional %50 %52 %53
  2990. ; CHECK: %52 = OpLabel
  2991. ; CHECK: %54 = OpLoad %13 %27
  2992. ; CHECK: %143 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_1 %142
  2993. ; CHECK: %144 = OpULessThan %bool %uint_0 %143
  2994. ; CHECK: OpSelectionMerge %145 None
  2995. ; CHECK: OpBranchConditional %144 %146 %147
  2996. ; CHECK: %146 = OpLabel
  2997. ; CHECK: %148 = OpLoad %13 %27
  2998. ; CHECK: %149 = OpImageRead %v4float %148 %20
  2999. ; CHECK: OpBranch %145
  3000. ; CHECK: %147 = OpLabel
  3001. ; CHECK: %150 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_1 %142 %uint_0
  3002. ; CHECK: OpBranch %145
  3003. ; CHECK: %145 = OpLabel
  3004. ; CHECK: %151 = OpPhi %v4float %149 %146 [[null_v4float]] %147
  3005. ; CHECK: OpBranch %51
  3006. ; CHECK: %53 = OpLabel
  3007. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_0 %142 %48
  3008. ; CHECK: OpBranch %51
  3009. ; CHECK: %51 = OpLabel
  3010. ; CHECK: %114 = OpPhi %v4float %151 %145 [[null_v4float]] %53
  3011. ; CHECK: %30 = OpCompositeExtract %float %114 0
  3012. ; CHECK: %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  3013. ; CHECK: %152 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  3014. ; CHECK: %153 = OpULessThan %bool %uint_0 %152
  3015. ; CHECK: OpSelectionMerge %154 None
  3016. ; CHECK: OpBranchConditional %153 %155 %156
  3017. ; CHECK: %155 = OpLabel
  3018. ; CHECK: OpStore %31 %30
  3019. ; CHECK: OpBranch %154
  3020. ; CHECK: %156 = OpLabel
  3021. ; CHECK: %158 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_54 %uint_1 %uint_0 %uint_0
  3022. ; CHECK: OpBranch %154
  3023. ; CHECK: %154 = OpLabel
  3024. OpReturn
  3025. OpFunctionEnd
  3026. )";
  3027. const std::string new_funcs = kDirectRead2 + kStreamWrite4Ray + kDirectRead4;
  3028. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3029. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  3030. true, 7u, 23u, true, true, false,
  3031. false, false);
  3032. }
  3033. TEST_F(InstBindlessTest,
  3034. InstBoundsClosestHitInitLoadVariableSizedSampledImagesArray) {
  3035. // #version 460
  3036. // #extension GL_EXT_nonuniform_qualifier : require
  3037. // #extension GL_NV_ray_tracing : require
  3038. //
  3039. // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
  3040. // uint index;
  3041. // float red;
  3042. // } sbo;
  3043. //
  3044. // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
  3045. //
  3046. // void main()
  3047. // {
  3048. // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
  3049. // }
  3050. // clang-format off
  3051. const std::string defs = R"(
  3052. OpCapability RuntimeDescriptorArray
  3053. OpCapability RayTracingNV
  3054. OpExtension "SPV_EXT_descriptor_indexing"
  3055. OpExtension "SPV_NV_ray_tracing"
  3056. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  3057. %1 = OpExtInstImport "GLSL.std.450"
  3058. OpMemoryModel Logical GLSL450
  3059. OpEntryPoint ClosestHitNV %main "main"
  3060. ; CHECK: OpEntryPoint ClosestHitNV %main "main" [[launch_id:%\w+]]
  3061. OpSource GLSL 460
  3062. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  3063. OpSourceExtension "GL_NV_ray_tracing"
  3064. OpName %main "main"
  3065. OpName %StorageBuffer "StorageBuffer"
  3066. OpMemberName %StorageBuffer 0 "index"
  3067. OpMemberName %StorageBuffer 1 "red"
  3068. OpName %sbo "sbo"
  3069. OpName %images "images"
  3070. OpMemberDecorate %StorageBuffer 0 Offset 0
  3071. OpMemberDecorate %StorageBuffer 1 Offset 4
  3072. OpDecorate %StorageBuffer BufferBlock
  3073. OpDecorate %sbo DescriptorSet 0
  3074. OpDecorate %sbo Binding 0
  3075. OpDecorate %images DescriptorSet 0
  3076. OpDecorate %images Binding 1
  3077. OpDecorate %images NonWritable
  3078. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  3079. )" + kInputDecorations + kOutputDecorations + R"(
  3080. ; CHECK: OpDecorate [[launch_id]] BuiltIn LaunchIdNV
  3081. %void = OpTypeVoid
  3082. %3 = OpTypeFunction %void
  3083. %uint = OpTypeInt 32 0
  3084. %float = OpTypeFloat 32
  3085. %StorageBuffer = OpTypeStruct %uint %float
  3086. %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
  3087. %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
  3088. %int = OpTypeInt 32 1
  3089. %int_1 = OpConstant %int 1
  3090. %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
  3091. %_runtimearr_13 = OpTypeRuntimeArray %13
  3092. %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
  3093. %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
  3094. %int_0 = OpConstant %int 0
  3095. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  3096. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  3097. %v2int = OpTypeVector %int 2
  3098. %25 = OpConstantComposite %v2int %int_0 %int_0
  3099. %v4float = OpTypeVector %float 4
  3100. %uint_0 = OpConstant %uint 0
  3101. %_ptr_Uniform_float = OpTypePointer Uniform %float
  3102. )" + kInputGlobals + kOutputGlobals + R"(
  3103. ; CHECK: [[launch_id]] = OpVariable %_ptr_Input_v3uint Input
  3104. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  3105. ; CHECK: [[null_uint:%\w+]] = OpConstantNull %uint
  3106. )";
  3107. // clang-format on
  3108. const std::string main_func = R"(
  3109. %main = OpFunction %void None %3
  3110. %5 = OpLabel
  3111. %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
  3112. %20 = OpLoad %uint %19
  3113. %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
  3114. %23 = OpLoad %13 %22
  3115. %27 = OpImageRead %v4float %23 %25
  3116. %29 = OpCompositeExtract %float %27 0
  3117. %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  3118. OpStore %31 %29
  3119. ; CHECK-NOT: OpStore %31 %29
  3120. ; CHECK: %133 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  3121. ; CHECK: %134 = OpULessThan %bool %uint_0 %133
  3122. ; CHECK: OpSelectionMerge %135 None
  3123. ; CHECK: OpBranchConditional %134 %136 %137
  3124. ; CHECK: %136 = OpLabel
  3125. ; CHECK: %138 = OpLoad %uint %25
  3126. ; CHECK: OpBranch %135
  3127. ; CHECK: %137 = OpLabel
  3128. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_48 %uint_1 %uint_0 %uint_0
  3129. ; CHECK: OpBranch %135
  3130. ; CHECK: %135 = OpLabel
  3131. ; CHECK: %142 = OpPhi %uint %138 %136 [[null_uint]] %137
  3132. ; CHECK: %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
  3133. ; CHECK: %28 = OpLoad %13 %27
  3134. ; CHECK: %48 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_1
  3135. ; CHECK: %50 = OpULessThan %bool %142 %48
  3136. ; CHECK: OpSelectionMerge %51 None
  3137. ; CHECK: OpBranchConditional %50 %52 %53
  3138. ; CHECK: %52 = OpLabel
  3139. ; CHECK: %54 = OpLoad %13 %27
  3140. ; CHECK: %143 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_1 %142
  3141. ; CHECK: %144 = OpULessThan %bool %uint_0 %143
  3142. ; CHECK: OpSelectionMerge %145 None
  3143. ; CHECK: OpBranchConditional %144 %146 %147
  3144. ; CHECK: %146 = OpLabel
  3145. ; CHECK: %148 = OpLoad %13 %27
  3146. ; CHECK: %149 = OpImageRead %v4float %148 %20
  3147. ; CHECK: OpBranch %145
  3148. ; CHECK: %147 = OpLabel
  3149. ; CHECK: %150 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_1 %142 %uint_0
  3150. ; CHECK: OpBranch %145
  3151. ; CHECK: %145 = OpLabel
  3152. ; CHECK: %151 = OpPhi %v4float %149 %146 [[null_v4float]] %147
  3153. ; CHECK: OpBranch %51
  3154. ; CHECK: %53 = OpLabel
  3155. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_0 %142 %48
  3156. ; CHECK: OpBranch %51
  3157. ; CHECK: %51 = OpLabel
  3158. ; CHECK: %114 = OpPhi %v4float %151 %145 [[null_v4float]] %53
  3159. ; CHECK: %30 = OpCompositeExtract %float %114 0
  3160. ; CHECK: %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  3161. ; CHECK: %152 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  3162. ; CHECK: %153 = OpULessThan %bool %uint_0 %152
  3163. ; CHECK: OpSelectionMerge %154 None
  3164. ; CHECK: OpBranchConditional %153 %155 %156
  3165. ; CHECK: %155 = OpLabel
  3166. ; CHECK: OpStore %31 %30
  3167. ; CHECK: OpBranch %154
  3168. ; CHECK: %156 = OpLabel
  3169. ; CHECK: %158 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_54 %uint_1 %uint_0 %uint_0
  3170. ; CHECK: OpBranch %154
  3171. ; CHECK: %154 = OpLabel
  3172. OpReturn
  3173. OpFunctionEnd
  3174. )";
  3175. const std::string new_funcs = kDirectRead2 + kStreamWrite4Ray + kDirectRead4;
  3176. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3177. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  3178. true, 7u, 23u, true, true, false,
  3179. false, false);
  3180. }
  3181. TEST_F(InstBindlessTest,
  3182. InstBoundsMissInitLoadVariableSizedSampledImagesArray) {
  3183. // #version 460
  3184. // #extension GL_EXT_nonuniform_qualifier : require
  3185. // #extension GL_NV_ray_tracing : require
  3186. //
  3187. // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
  3188. // uint index;
  3189. // float red;
  3190. // } sbo;
  3191. //
  3192. // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
  3193. //
  3194. // void main()
  3195. // {
  3196. // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
  3197. // }
  3198. // clang-format off
  3199. const std::string defs = R"(
  3200. OpCapability RuntimeDescriptorArray
  3201. OpCapability RayTracingNV
  3202. OpExtension "SPV_EXT_descriptor_indexing"
  3203. OpExtension "SPV_NV_ray_tracing"
  3204. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  3205. %1 = OpExtInstImport "GLSL.std.450"
  3206. OpMemoryModel Logical GLSL450
  3207. OpEntryPoint MissNV %main "main"
  3208. ; CHECK: OpEntryPoint MissNV %main "main" [[launch_id:%\w+]]
  3209. OpSource GLSL 460
  3210. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  3211. OpSourceExtension "GL_NV_ray_tracing"
  3212. OpName %main "main"
  3213. OpName %StorageBuffer "StorageBuffer"
  3214. OpMemberName %StorageBuffer 0 "index"
  3215. OpMemberName %StorageBuffer 1 "red"
  3216. OpName %sbo "sbo"
  3217. OpName %images "images"
  3218. OpMemberDecorate %StorageBuffer 0 Offset 0
  3219. OpMemberDecorate %StorageBuffer 1 Offset 4
  3220. OpDecorate %StorageBuffer BufferBlock
  3221. OpDecorate %sbo DescriptorSet 0
  3222. OpDecorate %sbo Binding 0
  3223. OpDecorate %images DescriptorSet 0
  3224. OpDecorate %images Binding 1
  3225. OpDecorate %images NonWritable
  3226. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  3227. )" + kInputDecorations + kOutputDecorations + R"(
  3228. ; CHECK: OpDecorate [[launch_id]] BuiltIn LaunchIdNV
  3229. %void = OpTypeVoid
  3230. %3 = OpTypeFunction %void
  3231. %uint = OpTypeInt 32 0
  3232. %float = OpTypeFloat 32
  3233. %StorageBuffer = OpTypeStruct %uint %float
  3234. %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
  3235. %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
  3236. %int = OpTypeInt 32 1
  3237. %int_1 = OpConstant %int 1
  3238. %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
  3239. %_runtimearr_13 = OpTypeRuntimeArray %13
  3240. %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
  3241. %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
  3242. %int_0 = OpConstant %int 0
  3243. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  3244. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  3245. %v2int = OpTypeVector %int 2
  3246. %25 = OpConstantComposite %v2int %int_0 %int_0
  3247. %v4float = OpTypeVector %float 4
  3248. %uint_0 = OpConstant %uint 0
  3249. %_ptr_Uniform_float = OpTypePointer Uniform %float
  3250. )" + kInputGlobals + kOutputGlobals + R"(
  3251. ; CHECK: [[launch_id]] = OpVariable %_ptr_Input_v3uint Input
  3252. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  3253. ; CHECK: [[null_uint:%\w+]] = OpConstantNull %uint
  3254. )";
  3255. // clang-format on
  3256. const std::string main_func = R"(
  3257. %main = OpFunction %void None %3
  3258. %5 = OpLabel
  3259. %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
  3260. %20 = OpLoad %uint %19
  3261. %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
  3262. %23 = OpLoad %13 %22
  3263. %27 = OpImageRead %v4float %23 %25
  3264. %29 = OpCompositeExtract %float %27 0
  3265. %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  3266. OpStore %31 %29
  3267. ; CHECK-NOT OpStore %31 %29
  3268. ; CHECK: %133 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  3269. ; CHECK: %134 = OpULessThan %bool %uint_0 %133
  3270. ; CHECK: OpSelectionMerge %135 None
  3271. ; CHECK: OpBranchConditional %134 %136 %137
  3272. ; CHECK: %136 = OpLabel
  3273. ; CHECK: %138 = OpLoad %uint %25
  3274. ; CHECK: OpBranch %135
  3275. ; CHECK: %137 = OpLabel
  3276. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_48 %uint_1 %uint_0 %uint_0
  3277. ; CHECK: OpBranch %135
  3278. ; CHECK: %135 = OpLabel
  3279. ; CHECK: %142 = OpPhi %uint %138 %136 [[null_uint]] %137
  3280. ; CHECK: %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
  3281. ; CHECK: %28 = OpLoad %13 %27
  3282. ; CHECK: %48 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_1
  3283. ; CHECK: %50 = OpULessThan %bool %142 %48
  3284. ; CHECK: OpSelectionMerge %51 None
  3285. ; CHECK: OpBranchConditional %50 %52 %53
  3286. ; CHECK: %52 = OpLabel
  3287. ; CHECK: %54 = OpLoad %13 %27
  3288. ; CHECK: %143 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_1 %142
  3289. ; CHECK: %144 = OpULessThan %bool %uint_0 %143
  3290. ; CHECK: OpSelectionMerge %145 None
  3291. ; CHECK: OpBranchConditional %144 %146 %147
  3292. ; CHECK: %146 = OpLabel
  3293. ; CHECK: %148 = OpLoad %13 %27
  3294. ; CHECK: %149 = OpImageRead %v4float %148 %20
  3295. ; CHECK: OpBranch %145
  3296. ; CHECK: %147 = OpLabel
  3297. ; CHECK: %150 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_1 %142 %uint_0
  3298. ; CHECK: OpBranch %145
  3299. ; CHECK: %145 = OpLabel
  3300. ; CHECK: %151 = OpPhi %v4float %149 %146 [[null_v4float]] %147
  3301. ; CHECK: OpBranch %51
  3302. ; CHECK: %53 = OpLabel
  3303. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_0 %142 %48
  3304. ; CHECK: OpBranch %51
  3305. ; CHECK: %51 = OpLabel
  3306. ; CHECK: %114 = OpPhi %v4float %151 %145 [[null_v4float]] %53
  3307. ; CHECK: %30 = OpCompositeExtract %float %114 0
  3308. ; CHECK: %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  3309. ; CHECK: %152 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  3310. ; CHECK: %153 = OpULessThan %bool %uint_0 %152
  3311. ; CHECK: OpSelectionMerge %154 None
  3312. ; CHECK: OpBranchConditional %153 %155 %156
  3313. ; CHECK: %155 = OpLabel
  3314. ; CHECK: OpStore %31 %30
  3315. ; CHECK: OpBranch %154
  3316. ; CHECK: %156 = OpLabel
  3317. ; CHECK: %158 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_54 %uint_1 %uint_0 %uint_0
  3318. ; CHECK: OpBranch %154
  3319. ; CHECK: %154 = OpLabel
  3320. OpReturn
  3321. OpFunctionEnd
  3322. )";
  3323. const std::string new_funcs = kDirectRead2 + kStreamWrite4Ray + kDirectRead4;
  3324. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3325. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  3326. true, 7u, 23u, true, true, false,
  3327. false, false);
  3328. }
  3329. TEST_F(InstBindlessTest,
  3330. InstBoundsCallableInitLoadVariableSizedSampledImagesArray) {
  3331. // #version 460
  3332. // #extension GL_EXT_nonuniform_qualifier : require
  3333. // #extension GL_NV_ray_tracing : require
  3334. //
  3335. // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
  3336. // uint index;
  3337. // float red;
  3338. // } sbo;
  3339. //
  3340. // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
  3341. //
  3342. // void main()
  3343. // {
  3344. // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
  3345. // }
  3346. // clang-format off
  3347. const std::string defs = R"(
  3348. OpCapability RuntimeDescriptorArray
  3349. OpCapability RayTracingNV
  3350. OpExtension "SPV_EXT_descriptor_indexing"
  3351. OpExtension "SPV_NV_ray_tracing"
  3352. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  3353. %1 = OpExtInstImport "GLSL.std.450"
  3354. OpMemoryModel Logical GLSL450
  3355. OpEntryPoint CallableNV %main "main"
  3356. ; CHECK: OpEntryPoint CallableNV %main "main" [[launch_id:%\w+]]
  3357. OpSource GLSL 460
  3358. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  3359. OpSourceExtension "GL_NV_ray_tracing"
  3360. OpName %main "main"
  3361. OpName %StorageBuffer "StorageBuffer"
  3362. OpMemberName %StorageBuffer 0 "index"
  3363. OpMemberName %StorageBuffer 1 "red"
  3364. OpName %sbo "sbo"
  3365. OpName %images "images"
  3366. OpMemberDecorate %StorageBuffer 0 Offset 0
  3367. OpMemberDecorate %StorageBuffer 1 Offset 4
  3368. OpDecorate %StorageBuffer BufferBlock
  3369. OpDecorate %sbo DescriptorSet 0
  3370. OpDecorate %sbo Binding 0
  3371. OpDecorate %images DescriptorSet 0
  3372. OpDecorate %images Binding 1
  3373. OpDecorate %images NonWritable
  3374. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  3375. )" + kInputDecorations + kOutputDecorations + R"(
  3376. ; CHECK: OpDecorate [[launch_id]] BuiltIn LaunchIdNV
  3377. %void = OpTypeVoid
  3378. %3 = OpTypeFunction %void
  3379. %uint = OpTypeInt 32 0
  3380. %float = OpTypeFloat 32
  3381. %StorageBuffer = OpTypeStruct %uint %float
  3382. %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
  3383. %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
  3384. %int = OpTypeInt 32 1
  3385. %int_1 = OpConstant %int 1
  3386. %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
  3387. %_runtimearr_13 = OpTypeRuntimeArray %13
  3388. %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
  3389. %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
  3390. %int_0 = OpConstant %int 0
  3391. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  3392. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  3393. %v2int = OpTypeVector %int 2
  3394. %25 = OpConstantComposite %v2int %int_0 %int_0
  3395. %v4float = OpTypeVector %float 4
  3396. %uint_0 = OpConstant %uint 0
  3397. %_ptr_Uniform_float = OpTypePointer Uniform %float
  3398. )" + kInputGlobals + kOutputGlobals + R"(
  3399. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  3400. ; CHECK: [[null_uint:%\w+]] = OpConstantNull %uint
  3401. )";
  3402. // clang-format on
  3403. const std::string main_func = R"(
  3404. %main = OpFunction %void None %3
  3405. %5 = OpLabel
  3406. %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
  3407. %20 = OpLoad %uint %19
  3408. %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
  3409. %23 = OpLoad %13 %22
  3410. %27 = OpImageRead %v4float %23 %25
  3411. %29 = OpCompositeExtract %float %27 0
  3412. %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  3413. OpStore %31 %29
  3414. ; CHECK-NOT: OpStore %31 %29
  3415. ; CHECK: %133 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  3416. ; CHECK: %134 = OpULessThan %bool %uint_0 %133
  3417. ; CHECK: OpSelectionMerge %135 None
  3418. ; CHECK: OpBranchConditional %134 %136 %137
  3419. ; CHECK: %136 = OpLabel
  3420. ; CHECK: %138 = OpLoad %uint %25
  3421. ; CHECK: OpBranch %135
  3422. ; CHECK: %137 = OpLabel
  3423. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_48 %uint_1 %uint_0 %uint_0
  3424. ; CHECK: OpBranch %135
  3425. ; CHECK: %135 = OpLabel
  3426. ; CHECK: %142 = OpPhi %uint %138 %136 [[null_uint]] %137
  3427. ; CHECK: %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
  3428. ; CHECK: %28 = OpLoad %13 %27
  3429. ; CHECK: %48 = OpFunctionCall %uint %inst_bindless_direct_read_2 %uint_1 %uint_1
  3430. ; CHECK: %50 = OpULessThan %bool %142 %48
  3431. ; CHECK: OpSelectionMerge %51 None
  3432. ; CHECK: OpBranchConditional %50 %52 %53
  3433. ; CHECK: %52 = OpLabel
  3434. ; CHECK: %54 = OpLoad %13 %27
  3435. ; CHECK: %143 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_1 %142
  3436. ; CHECK: %144 = OpULessThan %bool %uint_0 %143
  3437. ; CHECK: OpSelectionMerge %145 None
  3438. ; CHECK: OpBranchConditional %144 %146 %147
  3439. ; CHECK: %146 = OpLabel
  3440. ; CHECK: %148 = OpLoad %13 %27
  3441. ; CHECK: %149 = OpImageRead %v4float %148 %20
  3442. ; CHECK: OpBranch %145
  3443. ; CHECK: %147 = OpLabel
  3444. ; CHECK: %150 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_1 %142 %uint_0
  3445. ; CHECK: OpBranch %145
  3446. ; CHECK: %145 = OpLabel
  3447. ; CHECK: %151 = OpPhi %v4float %149 %146 [[null_v4float]] %147
  3448. ; CHECK: OpBranch %51
  3449. ; CHECK: %53 = OpLabel
  3450. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_51 %uint_0 %142 %48
  3451. ; CHECK: OpBranch %51
  3452. ; CHECK: %51 = OpLabel
  3453. ; CHECK: %114 = OpPhi %v4float %151 %145 [[null_v4float]] %53
  3454. ; CHECK: %30 = OpCompositeExtract %float %114 0
  3455. ; CHECK: %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
  3456. ; CHECK: %152 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  3457. ; CHECK: %153 = OpULessThan %bool %uint_0 %152
  3458. ; CHECK: OpSelectionMerge %154 None
  3459. ; CHECK: OpBranchConditional %153 %155 %156
  3460. ; CHECK: %155 = OpLabel
  3461. ; CHECK: OpStore %31 %30
  3462. ; CHECK: OpBranch %154
  3463. ; CHECK: %156 = OpLabel
  3464. ; CHECK: %158 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_54 %uint_1 %uint_0 %uint_0
  3465. ; CHECK: OpBranch %154
  3466. ; CHECK: %154 = OpLabel
  3467. OpReturn
  3468. OpFunctionEnd
  3469. )";
  3470. const std::string new_funcs = kDirectRead2 + kStreamWrite4Ray + kDirectRead4;
  3471. // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3472. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  3473. true, 7u, 23u, true, true, false,
  3474. false, false);
  3475. }
  3476. TEST_F(InstBindlessTest, InstBoundsInitSameBlockOpReplication) {
  3477. // Test that same block ops like OpSampledImage are replicated properly
  3478. // where needed.
  3479. //
  3480. // clang-format off
  3481. //
  3482. // #version 450 core
  3483. // #extension GL_EXT_nonuniform_qualifier : enable
  3484. //
  3485. // layout(location = 0) in vec2 inTexcoord;
  3486. // layout(location = 0) out vec4 outColor;
  3487. //
  3488. // layout(set = 0, binding = 0) uniform Uniforms {
  3489. // vec2 var0;
  3490. // } uniforms;
  3491. //
  3492. // layout(set = 0, binding = 1) uniform sampler uniformSampler;
  3493. // layout(set = 0, binding = 2) uniform texture2D uniformTex;
  3494. // layout(set = 0, binding = 3) uniform texture2D uniformTexArr[8];
  3495. //
  3496. // void main() {
  3497. // int index = 0;
  3498. // float x = texture(sampler2D(uniformTexArr[nonuniformEXT(index)], uniformSampler), inTexcoord.xy).x;
  3499. // float y = texture(sampler2D(uniformTex, uniformSampler), inTexcoord.xy * uniforms.var0.xy).x;
  3500. // outColor = vec4(x, y, 0.0, 0.0);
  3501. // }
  3502. //
  3503. const std::string defs = R"(
  3504. OpCapability Shader
  3505. OpCapability ShaderNonUniformEXT
  3506. OpCapability SampledImageArrayNonUniformIndexingEXT
  3507. OpExtension "SPV_EXT_descriptor_indexing"
  3508. ; CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  3509. %1 = OpExtInstImport "GLSL.std.450"
  3510. OpMemoryModel Logical GLSL450
  3511. OpEntryPoint Fragment %main "main" %inTexcoord %outColor
  3512. ; CHECK: OpEntryPoint Fragment %main "main" %inTexcoord %outColor %gl_FragCoord
  3513. OpExecutionMode %main OriginUpperLeft
  3514. OpSource GLSL 450
  3515. OpSourceExtension "GL_EXT_nonuniform_qualifier"
  3516. OpName %main "main"
  3517. OpName %index "index"
  3518. OpName %x "x"
  3519. OpName %uniformTexArr "uniformTexArr"
  3520. OpName %uniformSampler "uniformSampler"
  3521. OpName %inTexcoord "inTexcoord"
  3522. OpName %y "y"
  3523. OpName %uniformTex "uniformTex"
  3524. OpName %Uniforms "Uniforms"
  3525. OpMemberName %Uniforms 0 "var0"
  3526. OpName %uniforms "uniforms"
  3527. OpName %outColor "outColor"
  3528. OpDecorate %uniformTexArr DescriptorSet 0
  3529. OpDecorate %uniformTexArr Binding 3
  3530. OpDecorate %19 NonUniformEXT
  3531. OpDecorate %22 NonUniformEXT
  3532. OpDecorate %uniformSampler DescriptorSet 0
  3533. OpDecorate %uniformSampler Binding 1
  3534. OpDecorate %inTexcoord Location 0
  3535. OpDecorate %uniformTex DescriptorSet 0
  3536. OpDecorate %uniformTex Binding 2
  3537. OpMemberDecorate %Uniforms 0 Offset 0
  3538. OpDecorate %Uniforms Block
  3539. OpDecorate %uniforms DescriptorSet 0
  3540. OpDecorate %uniforms Binding 0
  3541. OpDecorate %outColor Location 0
  3542. ; CHECK: OpDecorate %63 NonUniform
  3543. ; CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  3544. )" + kOutputDecorations + R"(
  3545. ; CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  3546. )" + kInputDecorations + R"(
  3547. ; CHECK: OpDecorate %151 NonUniform
  3548. %void = OpTypeVoid
  3549. %3 = OpTypeFunction %void
  3550. %int = OpTypeInt 32 1
  3551. %_ptr_Function_int = OpTypePointer Function %int
  3552. %int_0 = OpConstant %int 0
  3553. %float = OpTypeFloat 32
  3554. %_ptr_Function_float = OpTypePointer Function %float
  3555. %13 = OpTypeImage %float 2D 0 0 0 1 Unknown
  3556. %uint = OpTypeInt 32 0
  3557. %uint_8 = OpConstant %uint 8
  3558. %_arr_13_uint_8 = OpTypeArray %13 %uint_8
  3559. %_ptr_UniformConstant__arr_13_uint_8 = OpTypePointer UniformConstant %_arr_13_uint_8
  3560. %uniformTexArr = OpVariable %_ptr_UniformConstant__arr_13_uint_8 UniformConstant
  3561. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  3562. %23 = OpTypeSampler
  3563. %_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
  3564. %uniformSampler = OpVariable %_ptr_UniformConstant_23 UniformConstant
  3565. %27 = OpTypeSampledImage %13
  3566. %v2float = OpTypeVector %float 2
  3567. %_ptr_Input_v2float = OpTypePointer Input %v2float
  3568. %inTexcoord = OpVariable %_ptr_Input_v2float Input
  3569. %v4float = OpTypeVector %float 4
  3570. %uint_0 = OpConstant %uint 0
  3571. %uniformTex = OpVariable %_ptr_UniformConstant_13 UniformConstant
  3572. %Uniforms = OpTypeStruct %v2float
  3573. %_ptr_Uniform_Uniforms = OpTypePointer Uniform %Uniforms
  3574. %uniforms = OpVariable %_ptr_Uniform_Uniforms Uniform
  3575. %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
  3576. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3577. %outColor = OpVariable %_ptr_Output_v4float Output
  3578. %float_0 = OpConstant %float 0
  3579. )" + kOutputGlobals + R"(
  3580. ; CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  3581. )" + kInputGlobals + R"(
  3582. ; CHECK: [[null_v2float:%\w+]] = OpConstantNull %v2float
  3583. )";
  3584. // clang-format on
  3585. const std::string main_func = R"(
  3586. %main = OpFunction %void None %3
  3587. %5 = OpLabel
  3588. %index = OpVariable %_ptr_Function_int Function
  3589. %x = OpVariable %_ptr_Function_float Function
  3590. %y = OpVariable %_ptr_Function_float Function
  3591. OpStore %index %int_0
  3592. %19 = OpLoad %int %index
  3593. %21 = OpAccessChain %_ptr_UniformConstant_13 %uniformTexArr %19
  3594. %22 = OpLoad %13 %21
  3595. %26 = OpLoad %23 %uniformSampler
  3596. %28 = OpSampledImage %27 %22 %26
  3597. %32 = OpLoad %v2float %inTexcoord
  3598. %34 = OpImageSampleImplicitLod %v4float %28 %32
  3599. %36 = OpCompositeExtract %float %34 0
  3600. OpStore %x %36
  3601. %39 = OpLoad %13 %uniformTex
  3602. %40 = OpLoad %23 %uniformSampler
  3603. %41 = OpSampledImage %27 %39 %40
  3604. %42 = OpLoad %v2float %inTexcoord
  3605. %47 = OpAccessChain %_ptr_Uniform_v2float %uniforms %int_0
  3606. %48 = OpLoad %v2float %47
  3607. %49 = OpFMul %v2float %42 %48
  3608. %50 = OpImageSampleImplicitLod %v4float %41 %49
  3609. %51 = OpCompositeExtract %float %50 0
  3610. ; CHECK-NOT: %51 = OpCompositeExtract %float %50 0
  3611. ; CHECK: %157 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  3612. ; CHECK: %158 = OpULessThan %bool %uint_0 %157
  3613. ; CHECK: OpSelectionMerge %159 None
  3614. ; CHECK: OpBranchConditional %158 %160 %161
  3615. ; CHECK: %160 = OpLabel
  3616. ; CHECK: %162 = OpLoad %v2float %47
  3617. ; CHECK: OpBranch %159
  3618. ; CHECK: %161 = OpLabel
  3619. ; CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_87 %uint_1 %uint_0 %uint_0
  3620. ; CHECK: OpBranch %159
  3621. ; CHECK: %159 = OpLabel
  3622. ; CHECK: %166 = OpPhi %v2float %162 %160 [[null_v2float]] %161
  3623. ; CHECK: %49 = OpFMul %v2float %42 %166
  3624. ; CHECK: %167 = OpSampledImage %27 %39 %40
  3625. ; CHECK: %168 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_2 %uint_0
  3626. ; CHECK: %169 = OpULessThan %bool %uint_0 %168
  3627. ; CHECK: OpSelectionMerge %170 None
  3628. ; CHECK: OpBranchConditional %169 %171 %172
  3629. ; CHECK: %171 = OpLabel
  3630. ; CHECK: %173 = OpLoad %13 %uniformTex
  3631. ; CHECK: %174 = OpSampledImage %27 %173 %40
  3632. ; CHECK: %175 = OpImageSampleImplicitLod %v4float %174 %49
  3633. ; CHECK: OpBranch %170
  3634. ; CHECK: %172 = OpLabel
  3635. ; CHECK: %177 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_89 %uint_1 %uint_0 %uint_0
  3636. ; CHECK: OpBranch %170
  3637. ; CHECK: %170 = OpLabel
  3638. ; CHECK: %178 = OpPhi %v4float %175 %171 [[null_v4float]] %172
  3639. ; CHECK: %51 = OpCompositeExtract %float %178 0
  3640. OpStore %y %51
  3641. %54 = OpLoad %float %x
  3642. %55 = OpLoad %float %y
  3643. %57 = OpCompositeConstruct %v4float %54 %55 %float_0 %float_0
  3644. OpStore %outColor %57
  3645. OpReturn
  3646. OpFunctionEnd
  3647. )";
  3648. const std::string new_funcs = kStreamWrite4Frag + kDirectRead4;
  3649. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3650. SinglePassRunAndMatch<InstBindlessCheckPass>(defs + main_func + new_funcs,
  3651. true, 7u, 23u, true, true, false,
  3652. false, false);
  3653. }
  3654. TEST_F(InstBindlessTest, MultipleUniformNonAggregateRefsNoDescInit) {
  3655. // Check that uniform refs do not go out-of-bounds. All checks use same input
  3656. // buffer read function call result at top of function for uniform buffer
  3657. // length. Because descriptor indexing is not being checked, we can avoid one
  3658. // buffer load.
  3659. //
  3660. // Texture2D g_tColor;
  3661. // SamplerState g_sAniso;
  3662. //
  3663. // layout(push_constant) cbuffer PerViewPushConst_t { bool g_B; };
  3664. //
  3665. // cbuffer PerViewConstantBuffer_t {
  3666. // float2 g_TexOff0;
  3667. // float2 g_TexOff1;
  3668. // };
  3669. //
  3670. // struct PS_INPUT {
  3671. // float2 vTextureCoords : TEXCOORD2;
  3672. // };
  3673. //
  3674. // struct PS_OUTPUT {
  3675. // float4 vColor : SV_Target0;
  3676. // };
  3677. //
  3678. // PS_OUTPUT MainPs(PS_INPUT i) {
  3679. // PS_OUTPUT ps_output;
  3680. // float2 off;
  3681. // float2 vtc;
  3682. // if (g_B)
  3683. // off = g_TexOff0;
  3684. // else
  3685. // off = g_TexOff1;
  3686. // vtc = i.vTextureCoords.xy + off;
  3687. // ps_output.vColor = g_tColor.Sample(g_sAniso, vtc);
  3688. // return ps_output;
  3689. // }
  3690. // clang-format off
  3691. const std::string text = R"(
  3692. OpCapability Shader
  3693. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  3694. %1 = OpExtInstImport "GLSL.std.450"
  3695. OpMemoryModel Logical GLSL450
  3696. OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
  3697. ;CHECK: OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor %inst_bindless_input_buffer %inst_bindless_output_buffer %gl_FragCoord
  3698. OpExecutionMode %MainPs OriginUpperLeft
  3699. OpSource HLSL 500
  3700. OpName %MainPs "MainPs"
  3701. OpName %PerViewPushConst_t "PerViewPushConst_t"
  3702. OpMemberName %PerViewPushConst_t 0 "g_B"
  3703. OpName %_ ""
  3704. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  3705. OpMemberName %PerViewConstantBuffer_t 0 "g_TexOff0"
  3706. OpMemberName %PerViewConstantBuffer_t 1 "g_TexOff1"
  3707. OpName %__0 ""
  3708. OpName %g_tColor "g_tColor"
  3709. OpName %g_sAniso "g_sAniso"
  3710. OpName %i_vTextureCoords "i.vTextureCoords"
  3711. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  3712. OpMemberDecorate %PerViewPushConst_t 0 Offset 0
  3713. OpDecorate %PerViewPushConst_t Block
  3714. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  3715. OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 8
  3716. OpDecorate %PerViewConstantBuffer_t Block
  3717. OpDecorate %__0 DescriptorSet 0
  3718. OpDecorate %__0 Binding 1
  3719. OpDecorate %g_tColor DescriptorSet 0
  3720. OpDecorate %g_tColor Binding 0
  3721. OpDecorate %g_sAniso DescriptorSet 0
  3722. OpDecorate %g_sAniso Binding 2
  3723. OpDecorate %i_vTextureCoords Location 0
  3724. OpDecorate %_entryPointOutput_vColor Location 0
  3725. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  3726. )" + kInputDecorations + kOutputDecorations + R"(
  3727. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  3728. %void = OpTypeVoid
  3729. %3 = OpTypeFunction %void
  3730. %float = OpTypeFloat 32
  3731. %v2float = OpTypeVector %float 2
  3732. %v4float = OpTypeVector %float 4
  3733. %uint = OpTypeInt 32 0
  3734. %PerViewPushConst_t = OpTypeStruct %uint
  3735. %_ptr_PushConstant_PerViewPushConst_t = OpTypePointer PushConstant %PerViewPushConst_t
  3736. %_ = OpVariable %_ptr_PushConstant_PerViewPushConst_t PushConstant
  3737. %int = OpTypeInt 32 1
  3738. %int_0 = OpConstant %int 0
  3739. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  3740. %bool = OpTypeBool
  3741. %uint_0 = OpConstant %uint 0
  3742. %PerViewConstantBuffer_t = OpTypeStruct %v2float %v2float
  3743. %_ptr_Uniform_PerViewConstantBuffer_t = OpTypePointer Uniform %PerViewConstantBuffer_t
  3744. %__0 = OpVariable %_ptr_Uniform_PerViewConstantBuffer_t Uniform
  3745. %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
  3746. %int_1 = OpConstant %int 1
  3747. %49 = OpTypeImage %float 2D 0 0 0 1 Unknown
  3748. %_ptr_UniformConstant_49 = OpTypePointer UniformConstant %49
  3749. %g_tColor = OpVariable %_ptr_UniformConstant_49 UniformConstant
  3750. %53 = OpTypeSampler
  3751. %_ptr_UniformConstant_53 = OpTypePointer UniformConstant %53
  3752. %g_sAniso = OpVariable %_ptr_UniformConstant_53 UniformConstant
  3753. %57 = OpTypeSampledImage %49
  3754. %_ptr_Input_v2float = OpTypePointer Input %v2float
  3755. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  3756. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3757. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  3758. )" + kInputGlobals + kOutputGlobals + R"(
  3759. ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
  3760. ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  3761. ;CHECK: %v4uint = OpTypeVector %uint 4
  3762. ;CHECK: [[null_v2float:%\w+]] = OpConstantNull %v2float
  3763. %MainPs = OpFunction %void None %3
  3764. %5 = OpLabel
  3765. ;CHECK: %140 = OpFunctionCall %uint %inst_bindless_direct_read_3 %uint_1 %uint_1 %uint_0
  3766. ;CHECK: OpBranch %117
  3767. ;CHECK: %117 = OpLabel
  3768. ;CHECK: OpBranch %116
  3769. ;CHECK: %116 = OpLabel
  3770. %69 = OpLoad %v2float %i_vTextureCoords
  3771. %82 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
  3772. %83 = OpLoad %uint %82
  3773. %84 = OpINotEqual %bool %83 %uint_0
  3774. OpSelectionMerge %91 None
  3775. OpBranchConditional %84 %85 %88
  3776. %85 = OpLabel
  3777. %86 = OpAccessChain %_ptr_Uniform_v2float %__0 %int_0
  3778. %87 = OpLoad %v2float %86
  3779. ;CHECK-NOT: %87 = OpLoad %v2float %86
  3780. ;CHECK: %119 = OpIAdd %uint %uint_0 %uint_7
  3781. ;CHECK: %141 = OpULessThan %bool %119 %140
  3782. ;CHECK: OpSelectionMerge %143 None
  3783. ;CHECK: OpBranchConditional %141 %144 %145
  3784. ;CHECK: %144 = OpLabel
  3785. ;CHECK: %146 = OpLoad %v2float %86
  3786. ;CHECK: OpBranch %143
  3787. ;CHECK: %145 = OpLabel
  3788. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_71 %uint_4 %uint_0 %119 %140
  3789. ;CHECK: OpBranch %143
  3790. ;CHECK: %143 = OpLabel
  3791. ;CHECK: %203 = OpPhi %v2float %146 %144 [[null_v2float]] %145
  3792. OpBranch %91
  3793. %88 = OpLabel
  3794. %89 = OpAccessChain %_ptr_Uniform_v2float %__0 %int_1
  3795. %90 = OpLoad %v2float %89
  3796. ;CHECK-NOT: %90 = OpLoad %v2float %89
  3797. ;CHECK: %204 = OpIAdd %uint %uint_8 %uint_7
  3798. ;CHECK: %205 = OpULessThan %bool %204 %140
  3799. ;CHECK: OpSelectionMerge %206 None
  3800. ;CHECK: OpBranchConditional %205 %207 %208
  3801. ;CHECK: %207 = OpLabel
  3802. ;CHECK: %209 = OpLoad %v2float %89
  3803. ;CHECK: OpBranch %206
  3804. ;CHECK: %208 = OpLabel
  3805. ;CHECK: %211 = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_75 %uint_4 %uint_0 %204 %140
  3806. ;CHECK: OpBranch %206
  3807. ;CHECK: %206 = OpLabel
  3808. ;CHECK: %212 = OpPhi %v2float %209 %207 [[null_v2float]] %208
  3809. OpBranch %91
  3810. %91 = OpLabel
  3811. %115 = OpPhi %v2float %87 %85 %90 %88
  3812. ;CHECK-NOT: %115 = OpPhi %v2float %87 %85 %90 %88
  3813. ;CHECK: %115 = OpPhi %v2float %203 %143 %212 %206
  3814. %95 = OpFAdd %v2float %69 %115
  3815. %96 = OpLoad %49 %g_tColor
  3816. %97 = OpLoad %53 %g_sAniso
  3817. %98 = OpSampledImage %57 %96 %97
  3818. %100 = OpImageSampleImplicitLod %v4float %98 %95
  3819. OpStore %_entryPointOutput_vColor %100
  3820. OpReturn
  3821. OpFunctionEnd
  3822. )" + kDirectRead3 + kStreamWrite5Frag;
  3823. // clang-format on
  3824. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  3825. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3826. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  3827. false, true, false, true);
  3828. }
  3829. TEST_F(InstBindlessTest, UniformArrayRefNoDescInit) {
  3830. // Check that uniform array ref does not go out-of-bounds.
  3831. //
  3832. // Texture2D g_tColor;
  3833. // SamplerState g_sAniso;
  3834. //
  3835. // layout(push_constant) cbuffer PerViewPushConst_t { uint g_c; };
  3836. //
  3837. // struct PerBatchEnvMapConstantBuffer_t {
  3838. // float4x3 g_matEnvMapWorldToLocal;
  3839. // float4 g_vEnvironmentMapBoxMins;
  3840. // float2 g_TexOff;
  3841. // };
  3842. //
  3843. // cbuffer _BindlessFastEnvMapCB_PS_t {
  3844. // PerBatchEnvMapConstantBuffer_t g_envMapConstants[128];
  3845. // };
  3846. //
  3847. // struct PS_INPUT {
  3848. // float2 vTextureCoords : TEXCOORD2;
  3849. // };
  3850. //
  3851. // struct PS_OUTPUT {
  3852. // float4 vColor : SV_Target0;
  3853. // };
  3854. //
  3855. // PS_OUTPUT MainPs(PS_INPUT i) {
  3856. // PS_OUTPUT ps_output;
  3857. // float2 off;
  3858. // float2 vtc;
  3859. // off = g_envMapConstants[g_c].g_TexOff;
  3860. // vtc = i.vTextureCoords.xy + off;
  3861. // ps_output.vColor = g_tColor.Sample(g_sAniso, vtc);
  3862. // return ps_output;
  3863. // }
  3864. // clang-format off
  3865. const std::string text = R"(
  3866. OpCapability Shader
  3867. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  3868. %1 = OpExtInstImport "GLSL.std.450"
  3869. OpMemoryModel Logical GLSL450
  3870. OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
  3871. OpExecutionMode %MainPs OriginUpperLeft
  3872. OpSource HLSL 500
  3873. OpName %MainPs "MainPs"
  3874. OpName %PerBatchEnvMapConstantBuffer_t "PerBatchEnvMapConstantBuffer_t"
  3875. OpMemberName %PerBatchEnvMapConstantBuffer_t 0 "g_matEnvMapWorldToLocal"
  3876. OpMemberName %PerBatchEnvMapConstantBuffer_t 1 "g_vEnvironmentMapBoxMins"
  3877. OpMemberName %PerBatchEnvMapConstantBuffer_t 2 "g_TexOff"
  3878. OpName %_BindlessFastEnvMapCB_PS_t "_BindlessFastEnvMapCB_PS_t"
  3879. OpMemberName %_BindlessFastEnvMapCB_PS_t 0 "g_envMapConstants"
  3880. OpName %_ ""
  3881. OpName %PerViewPushConst_t "PerViewPushConst_t"
  3882. OpMemberName %PerViewPushConst_t 0 "g_c"
  3883. OpName %__0 ""
  3884. OpName %g_tColor "g_tColor"
  3885. OpName %g_sAniso "g_sAniso"
  3886. OpName %i_vTextureCoords "i.vTextureCoords"
  3887. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  3888. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 RowMajor
  3889. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 Offset 0
  3890. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 MatrixStride 16
  3891. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 1 Offset 48
  3892. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 2 Offset 64
  3893. OpDecorate %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 ArrayStride 80
  3894. OpMemberDecorate %_BindlessFastEnvMapCB_PS_t 0 Offset 0
  3895. OpDecorate %_BindlessFastEnvMapCB_PS_t Block
  3896. OpDecorate %_ DescriptorSet 0
  3897. OpDecorate %_ Binding 2
  3898. OpMemberDecorate %PerViewPushConst_t 0 Offset 0
  3899. OpDecorate %PerViewPushConst_t Block
  3900. OpDecorate %g_tColor DescriptorSet 0
  3901. OpDecorate %g_tColor Binding 0
  3902. OpDecorate %g_sAniso DescriptorSet 0
  3903. OpDecorate %g_sAniso Binding 1
  3904. OpDecorate %i_vTextureCoords Location 0
  3905. OpDecorate %_entryPointOutput_vColor Location 0
  3906. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  3907. )" + kInputDecorations + kOutputDecorations + R"(
  3908. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  3909. %void = OpTypeVoid
  3910. %3 = OpTypeFunction %void
  3911. %float = OpTypeFloat 32
  3912. %v2float = OpTypeVector %float 2
  3913. %v4float = OpTypeVector %float 4
  3914. %v3float = OpTypeVector %float 3
  3915. %mat4v3float = OpTypeMatrix %v3float 4
  3916. %PerBatchEnvMapConstantBuffer_t = OpTypeStruct %mat4v3float %v4float %v2float
  3917. %uint = OpTypeInt 32 0
  3918. %uint_128 = OpConstant %uint 128
  3919. %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 = OpTypeArray %PerBatchEnvMapConstantBuffer_t %uint_128
  3920. %_BindlessFastEnvMapCB_PS_t = OpTypeStruct %_arr_PerBatchEnvMapConstantBuffer_t_uint_128
  3921. %_ptr_Uniform__BindlessFastEnvMapCB_PS_t = OpTypePointer Uniform %_BindlessFastEnvMapCB_PS_t
  3922. %_ = OpVariable %_ptr_Uniform__BindlessFastEnvMapCB_PS_t Uniform
  3923. %int = OpTypeInt 32 1
  3924. %int_0 = OpConstant %int 0
  3925. %PerViewPushConst_t = OpTypeStruct %uint
  3926. %_ptr_PushConstant_PerViewPushConst_t = OpTypePointer PushConstant %PerViewPushConst_t
  3927. %__0 = OpVariable %_ptr_PushConstant_PerViewPushConst_t PushConstant
  3928. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  3929. %int_2 = OpConstant %int 2
  3930. %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
  3931. %46 = OpTypeImage %float 2D 0 0 0 1 Unknown
  3932. %_ptr_UniformConstant_46 = OpTypePointer UniformConstant %46
  3933. %g_tColor = OpVariable %_ptr_UniformConstant_46 UniformConstant
  3934. %50 = OpTypeSampler
  3935. %_ptr_UniformConstant_50 = OpTypePointer UniformConstant %50
  3936. %g_sAniso = OpVariable %_ptr_UniformConstant_50 UniformConstant
  3937. %54 = OpTypeSampledImage %46
  3938. %_ptr_Input_v2float = OpTypePointer Input %v2float
  3939. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  3940. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3941. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  3942. )" + kInputGlobals + kOutputGlobals + R"(
  3943. ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
  3944. ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  3945. ;CHECK: %v4uint = OpTypeVector %uint 4
  3946. ;CHECK: [[null_v2float:%\w+]] = OpConstantNull %v2float
  3947. %MainPs = OpFunction %void None %3
  3948. %5 = OpLabel
  3949. ;CHECK: %123 = OpFunctionCall %uint %inst_bindless_direct_read_3 %uint_1 %uint_2 %uint_0
  3950. ;CHECK: OpBranch %93
  3951. ;CHECK: %93 = OpLabel
  3952. ;CHECK: OpBranch %92
  3953. ;CHECK: %92 = OpLabel
  3954. %66 = OpLoad %v2float %i_vTextureCoords
  3955. %79 = OpAccessChain %_ptr_PushConstant_uint %__0 %int_0
  3956. %80 = OpLoad %uint %79
  3957. %81 = OpAccessChain %_ptr_Uniform_v2float %_ %int_0 %80 %int_2
  3958. %82 = OpLoad %v2float %81
  3959. ;CHECK-NOT: %82 = OpLoad %v2float %81
  3960. ;CHECK: %96 = OpIMul %uint %uint_80 %80
  3961. ;CHECK: %97 = OpIAdd %uint %uint_0 %96
  3962. ;CHECK: %99 = OpIAdd %uint %97 %uint_64
  3963. ;CHECK: %101 = OpIAdd %uint %99 %uint_7
  3964. ;CHECK: %125 = OpULessThan %bool %101 %123
  3965. ;CHECK: OpSelectionMerge %127 None
  3966. ;CHECK: OpBranchConditional %125 %128 %129
  3967. ;CHECK: %128 = OpLabel
  3968. ;CHECK: %130 = OpLoad %v2float %81
  3969. ;CHECK: OpBranch %127
  3970. ;CHECK: %129 = OpLabel
  3971. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_78 %uint_4 %uint_0 %101 %123
  3972. ;CHECK: OpBranch %127
  3973. ;CHECK: %127 = OpLabel
  3974. ;CHECK: %186 = OpPhi %v2float %130 %128 [[null_v2float]] %129
  3975. %86 = OpFAdd %v2float %66 %82
  3976. ;CHECK-NOT: %86 = OpFAdd %v2float %66 %82
  3977. ;CHECK: %86 = OpFAdd %v2float %66 %186
  3978. %87 = OpLoad %46 %g_tColor
  3979. %88 = OpLoad %50 %g_sAniso
  3980. %89 = OpSampledImage %54 %87 %88
  3981. %91 = OpImageSampleImplicitLod %v4float %89 %86
  3982. OpStore %_entryPointOutput_vColor %91
  3983. OpReturn
  3984. OpFunctionEnd
  3985. )" + kDirectRead3 + kStreamWrite5Frag;
  3986. // clang-format on
  3987. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  3988. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3989. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  3990. false, true, false, true);
  3991. }
  3992. TEST_F(InstBindlessTest, UniformArrayRefWithDescInit) {
  3993. // The buffer-oob and desc-init checks should use the same debug
  3994. // output buffer write function.
  3995. //
  3996. // Same source as UniformArrayRefNoDescInit
  3997. // clang-format off
  3998. const std::string text = R"(
  3999. OpCapability Shader
  4000. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4001. %1 = OpExtInstImport "GLSL.std.450"
  4002. OpMemoryModel Logical GLSL450
  4003. OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
  4004. ;CHECK: OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor %inst_bindless_input_buffer %inst_bindless_output_buffer %gl_FragCoord
  4005. OpExecutionMode %MainPs OriginUpperLeft
  4006. OpSource HLSL 500
  4007. OpName %MainPs "MainPs"
  4008. OpName %PerBatchEnvMapConstantBuffer_t "PerBatchEnvMapConstantBuffer_t"
  4009. OpMemberName %PerBatchEnvMapConstantBuffer_t 0 "g_matEnvMapWorldToLocal"
  4010. OpMemberName %PerBatchEnvMapConstantBuffer_t 1 "g_vEnvironmentMapBoxMins"
  4011. OpMemberName %PerBatchEnvMapConstantBuffer_t 2 "g_TexOff"
  4012. OpName %_BindlessFastEnvMapCB_PS_t "_BindlessFastEnvMapCB_PS_t"
  4013. OpMemberName %_BindlessFastEnvMapCB_PS_t 0 "g_envMapConstants"
  4014. OpName %_ ""
  4015. OpName %PerViewPushConst_t "PerViewPushConst_t"
  4016. OpMemberName %PerViewPushConst_t 0 "g_c"
  4017. OpName %__0 ""
  4018. OpName %g_tColor "g_tColor"
  4019. OpName %g_sAniso "g_sAniso"
  4020. OpName %i_vTextureCoords "i.vTextureCoords"
  4021. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  4022. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 RowMajor
  4023. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 Offset 0
  4024. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 MatrixStride 16
  4025. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 1 Offset 48
  4026. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 2 Offset 64
  4027. OpDecorate %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 ArrayStride 80
  4028. OpMemberDecorate %_BindlessFastEnvMapCB_PS_t 0 Offset 0
  4029. OpDecorate %_BindlessFastEnvMapCB_PS_t Block
  4030. OpDecorate %_ DescriptorSet 0
  4031. OpDecorate %_ Binding 2
  4032. OpMemberDecorate %PerViewPushConst_t 0 Offset 0
  4033. OpDecorate %PerViewPushConst_t Block
  4034. OpDecorate %g_tColor DescriptorSet 0
  4035. OpDecorate %g_tColor Binding 0
  4036. OpDecorate %g_sAniso DescriptorSet 0
  4037. OpDecorate %g_sAniso Binding 1
  4038. OpDecorate %i_vTextureCoords Location 0
  4039. OpDecorate %_entryPointOutput_vColor Location 0
  4040. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  4041. )" + kInputDecorations + kOutputDecorations + R"(
  4042. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  4043. %void = OpTypeVoid
  4044. %3 = OpTypeFunction %void
  4045. %float = OpTypeFloat 32
  4046. %v2float = OpTypeVector %float 2
  4047. %v4float = OpTypeVector %float 4
  4048. %v3float = OpTypeVector %float 3
  4049. %mat4v3float = OpTypeMatrix %v3float 4
  4050. %PerBatchEnvMapConstantBuffer_t = OpTypeStruct %mat4v3float %v4float %v2float
  4051. %uint = OpTypeInt 32 0
  4052. %uint_128 = OpConstant %uint 128
  4053. %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 = OpTypeArray %PerBatchEnvMapConstantBuffer_t %uint_128
  4054. %_BindlessFastEnvMapCB_PS_t = OpTypeStruct %_arr_PerBatchEnvMapConstantBuffer_t_uint_128
  4055. %_ptr_Uniform__BindlessFastEnvMapCB_PS_t = OpTypePointer Uniform %_BindlessFastEnvMapCB_PS_t
  4056. %_ = OpVariable %_ptr_Uniform__BindlessFastEnvMapCB_PS_t Uniform
  4057. %int = OpTypeInt 32 1
  4058. %int_0 = OpConstant %int 0
  4059. %PerViewPushConst_t = OpTypeStruct %uint
  4060. %_ptr_PushConstant_PerViewPushConst_t = OpTypePointer PushConstant %PerViewPushConst_t
  4061. %__0 = OpVariable %_ptr_PushConstant_PerViewPushConst_t PushConstant
  4062. %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
  4063. %int_2 = OpConstant %int 2
  4064. %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
  4065. %46 = OpTypeImage %float 2D 0 0 0 1 Unknown
  4066. %_ptr_UniformConstant_46 = OpTypePointer UniformConstant %46
  4067. %g_tColor = OpVariable %_ptr_UniformConstant_46 UniformConstant
  4068. %50 = OpTypeSampler
  4069. %_ptr_UniformConstant_50 = OpTypePointer UniformConstant %50
  4070. %g_sAniso = OpVariable %_ptr_UniformConstant_50 UniformConstant
  4071. %54 = OpTypeSampledImage %46
  4072. %_ptr_Input_v2float = OpTypePointer Input %v2float
  4073. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  4074. %_ptr_Output_v4float = OpTypePointer Output %v4float
  4075. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  4076. )" + kInputGlobals + kOutputGlobals + R"(
  4077. ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
  4078. ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  4079. ;CHECK: %v4uint = OpTypeVector %uint 4
  4080. ;CHECK: [[null_v2float:%\w+]] = OpConstantNull %v2float
  4081. ;CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  4082. %MainPs = OpFunction %void None %3
  4083. %5 = OpLabel
  4084. ;CHECK: %126 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_2 %uint_0
  4085. ;CHECK: %191 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %uint_0
  4086. ;CHECK: OpBranch %93
  4087. ;CHECK: %93 = OpLabel
  4088. ;CHECK: OpBranch %92
  4089. ;CHECK: %92 = OpLabel
  4090. %66 = OpLoad %v2float %i_vTextureCoords
  4091. %79 = OpAccessChain %_ptr_PushConstant_uint %__0 %int_0
  4092. %80 = OpLoad %uint %79
  4093. %81 = OpAccessChain %_ptr_Uniform_v2float %_ %int_0 %80 %int_2
  4094. %82 = OpLoad %v2float %81
  4095. %86 = OpFAdd %v2float %66 %82
  4096. ;CHECK-NOT: %82 = OpLoad %v2float %81
  4097. ;CHECK-NOT: %86 = OpFAdd %v2float %66 %82
  4098. ;CHECK: %96 = OpIMul %uint %uint_80 %80
  4099. ;CHECK: %97 = OpIAdd %uint %uint_0 %96
  4100. ;CHECK: %99 = OpIAdd %uint %97 %uint_64
  4101. ;CHECK: %101 = OpIAdd %uint %99 %uint_7
  4102. ;CHECK: %128 = OpULessThan %bool %101 %126
  4103. ;CHECK: OpSelectionMerge %130 None
  4104. ;CHECK: OpBranchConditional %128 %131 %132
  4105. ;CHECK: %131 = OpLabel
  4106. ;CHECK: %133 = OpLoad %v2float %81
  4107. ;CHECK: OpBranch %130
  4108. ;CHECK: %132 = OpLabel
  4109. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_78 %uint_4 %uint_0 %101 %126
  4110. ;CHECK: OpBranch %130
  4111. ;CHECK: %130 = OpLabel
  4112. ;CHECK: %190 = OpPhi %v2float %133 %131 [[null_v2float]] %132
  4113. ;CHECK: %86 = OpFAdd %v2float %66 %190
  4114. %87 = OpLoad %46 %g_tColor
  4115. %88 = OpLoad %50 %g_sAniso
  4116. %89 = OpSampledImage %54 %87 %88
  4117. %91 = OpImageSampleImplicitLod %v4float %89 %86
  4118. OpStore %_entryPointOutput_vColor %91
  4119. ;CHECK-NOT: %91 = OpImageSampleImplicitLod %v4float %89 %86
  4120. ;CHECK-NOT: OpStore %_entryPointOutput_vColor %91
  4121. ;CHECK: %192 = OpULessThan %bool %uint_0 %191
  4122. ;CHECK: OpSelectionMerge %193 None
  4123. ;CHECK: OpBranchConditional %192 %194 %195
  4124. ;CHECK: %194 = OpLabel
  4125. ;CHECK: %196 = OpLoad %46 %g_tColor
  4126. ;CHECK: %197 = OpSampledImage %54 %196 %88
  4127. ;CHECK: %198 = OpImageSampleImplicitLod %v4float %197 %86
  4128. ;CHECK: OpBranch %193
  4129. ;CHECK: %195 = OpLabel
  4130. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_83 %uint_1 %uint_0 %uint_0 %uint_0
  4131. ;CHECK: OpBranch %193
  4132. ;CHECK: %193 = OpLabel
  4133. ;CHECK: %202 = OpPhi %v4float %198 %194 [[null_v4float]] %195
  4134. ;CHECK: OpStore %_entryPointOutput_vColor %202
  4135. OpReturn
  4136. OpFunctionEnd
  4137. )" + kDirectRead4 + kStreamWrite5Frag;
  4138. // clang-format on
  4139. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4140. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4141. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, true, true,
  4142. true, false, true);
  4143. }
  4144. TEST_F(InstBindlessTest, Descriptor16BitIdxRef) {
  4145. // Check that descriptor indexed with 16bit index is inbounds and
  4146. // initialized
  4147. //
  4148. // Use Simple source with min16uint g_nDataIdx
  4149. // clang-format off
  4150. const std::string text = R"(
  4151. OpCapability Shader
  4152. OpCapability Int16
  4153. OpCapability StoragePushConstant16
  4154. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4155. %1 = OpExtInstImport "GLSL.std.450"
  4156. OpMemoryModel Logical GLSL450
  4157. OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %_ %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
  4158. ;CHECK: OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %_ %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor %inst_bindless_output_buffer %gl_FragCoord %inst_bindless_input_buffer
  4159. OpExecutionMode %MainPs OriginUpperLeft
  4160. OpSource HLSL 500
  4161. OpName %MainPs "MainPs"
  4162. OpName %g_tColor "g_tColor"
  4163. OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
  4164. OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
  4165. OpName %_ ""
  4166. OpName %g_sAniso "g_sAniso"
  4167. OpName %i_vTextureCoords "i.vTextureCoords"
  4168. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  4169. OpDecorate %g_tColor DescriptorSet 0
  4170. OpDecorate %g_tColor Binding 0
  4171. OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
  4172. OpDecorate %PerViewConstantBuffer_t Block
  4173. OpDecorate %g_sAniso DescriptorSet 0
  4174. OpDecorate %g_sAniso Binding 0
  4175. OpDecorate %i_vTextureCoords Location 0
  4176. OpDecorate %_entryPointOutput_vColor Location 0
  4177. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  4178. )" + kOutputDecorations + R"(
  4179. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  4180. )" + kInputDecorations + R"(
  4181. %void = OpTypeVoid
  4182. %10 = OpTypeFunction %void
  4183. %float = OpTypeFloat 32
  4184. %v2float = OpTypeVector %float 2
  4185. %v4float = OpTypeVector %float 4
  4186. %int = OpTypeInt 32 1
  4187. %int_0 = OpConstant %int 0
  4188. %16 = OpTypeImage %float 2D 0 0 0 1 Unknown
  4189. %uint = OpTypeInt 32 0
  4190. %uint_128 = OpConstant %uint 128
  4191. %_arr_16_uint_128 = OpTypeArray %16 %uint_128
  4192. %_ptr_UniformConstant__arr_16_uint_128 = OpTypePointer UniformConstant %_arr_16_uint_128
  4193. %g_tColor = OpVariable %_ptr_UniformConstant__arr_16_uint_128 UniformConstant
  4194. %ushort = OpTypeInt 16 0
  4195. %PerViewConstantBuffer_t = OpTypeStruct %ushort
  4196. %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
  4197. %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
  4198. %_ptr_PushConstant_ushort = OpTypePointer PushConstant %ushort
  4199. %_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
  4200. %25 = OpTypeSampler
  4201. %_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
  4202. %g_sAniso = OpVariable %_ptr_UniformConstant_25 UniformConstant
  4203. %27 = OpTypeSampledImage %16
  4204. %_ptr_Input_v2float = OpTypePointer Input %v2float
  4205. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  4206. %_ptr_Output_v4float = OpTypePointer Output %v4float
  4207. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  4208. )" + kOutputGlobals + R"(
  4209. ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
  4210. ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  4211. ;CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  4212. )" + kInputGlobals + R"(
  4213. %MainPs = OpFunction %void None %10
  4214. %30 = OpLabel
  4215. ;CHECK: OpBranch %108
  4216. ;CHECK: %108 = OpLabel
  4217. ;CHECK: OpBranch %39
  4218. ;CHECK: %39 = OpLabel
  4219. %31 = OpLoad %v2float %i_vTextureCoords
  4220. %32 = OpAccessChain %_ptr_PushConstant_ushort %_ %int_0
  4221. %33 = OpLoad %ushort %32
  4222. %34 = OpAccessChain %_ptr_UniformConstant_16 %g_tColor %33
  4223. %35 = OpLoad %16 %34
  4224. %36 = OpLoad %25 %g_sAniso
  4225. %37 = OpSampledImage %27 %35 %36
  4226. %38 = OpImageSampleImplicitLod %v4float %37 %31
  4227. OpStore %_entryPointOutput_vColor %38
  4228. ;CHECK-NOT: %38 = OpImageSampleImplicitLod %v4float %37 %31
  4229. ;CHECK-NOT: OpStore %_entryPointOutput_vColor %38
  4230. ;CHECK: %41 = OpUConvert %uint %33
  4231. ;CHECK: %43 = OpULessThan %bool %41 %uint_128
  4232. ;CHECK: OpSelectionMerge %44 None
  4233. ;CHECK: OpBranchConditional %43 %45 %46
  4234. ;CHECK: %45 = OpLabel
  4235. ;CHECK: %47 = OpLoad %16 %34
  4236. ;CHECK: %48 = OpSampledImage %27 %47 %36
  4237. ;CHECK: %109 = OpUConvert %uint %33
  4238. ;CHECK: %131 = OpFunctionCall %uint %inst_bindless_direct_read_4 %uint_0 %uint_0 %uint_0 %109
  4239. ;CHECK: %132 = OpULessThan %bool %uint_0 %131
  4240. ;CHECK: OpSelectionMerge %133 None
  4241. ;CHECK: OpBranchConditional %132 %134 %135
  4242. ;CHECK: %134 = OpLabel
  4243. ;CHECK: %136 = OpLoad %16 %34
  4244. ;CHECK: %137 = OpSampledImage %27 %136 %36
  4245. ;CHECK: %138 = OpImageSampleImplicitLod %v4float %137 %31
  4246. ;CHECK: OpBranch %133
  4247. ;CHECK: %135 = OpLabel
  4248. ;CHECK: %139 = OpUConvert %uint %33
  4249. ;CHECK: %140 = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_60 %uint_1 %139 %uint_0
  4250. ;CHECK: OpBranch %133
  4251. ;CHECK: %133 = OpLabel
  4252. ;CHECK: [[phi_result_1:%\w+]] = OpPhi %v4float %138 %134 [[null_v4float]] %135
  4253. ;CHECK: OpBranch %44
  4254. ;CHECK: %46 = OpLabel
  4255. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_4 %uint_60 %uint_0 %41 %uint_128
  4256. ;CHECK: OpBranch %44
  4257. ;CHECK: %44 = OpLabel
  4258. ;CHECK: [[phi_result_2:%\w+]] = OpPhi %v4float [[phi_result_1]] %133 [[null_v4float]] %46
  4259. ;CHECK: OpStore %_entryPointOutput_vColor [[phi_result_2]]
  4260. OpReturn
  4261. OpFunctionEnd
  4262. )" + kStreamWrite4Frag + kDirectRead4;
  4263. // clang-format on
  4264. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4265. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4266. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, true, true,
  4267. false, false, true);
  4268. }
  4269. TEST_F(InstBindlessTest, UniformArray16bitIdxRef) {
  4270. // Check that uniform array ref with 16bit index does not go out-of-bounds.
  4271. //
  4272. // Texture2D g_tColor;
  4273. // SamplerState g_sAniso;
  4274. //
  4275. // layout(push_constant) cbuffer PerViewPushConst_t { min16uint g_c; };
  4276. //
  4277. // struct PerBatchEnvMapConstantBuffer_t {
  4278. // float4x3 g_matEnvMapWorldToLocal;
  4279. // float4 g_vEnvironmentMapBoxMins;
  4280. // float2 g_TexOff;
  4281. // };
  4282. //
  4283. // cbuffer _BindlessFastEnvMapCB_PS_t {
  4284. // PerBatchEnvMapConstantBuffer_t g_envMapConstants[128];
  4285. // };
  4286. //
  4287. // struct PS_INPUT {
  4288. // float2 vTextureCoords : TEXCOORD2;
  4289. // };
  4290. //
  4291. // struct PS_OUTPUT {
  4292. // float4 vColor : SV_Target0;
  4293. // };
  4294. //
  4295. // PS_OUTPUT MainPs(PS_INPUT i) {
  4296. // PS_OUTPUT ps_output;
  4297. // float2 off;
  4298. // float2 vtc;
  4299. // off = g_envMapConstants[g_c].g_TexOff;
  4300. // vtc = i.vTextureCoords.xy + off;
  4301. // ps_output.vColor = g_tColor.Sample(g_sAniso, vtc);
  4302. // return ps_output;
  4303. // }
  4304. // clang-format off
  4305. const std::string text = R"(
  4306. OpCapability Shader
  4307. OpCapability Int16
  4308. OpCapability StoragePushConstant16
  4309. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4310. %1 = OpExtInstImport "GLSL.std.450"
  4311. OpMemoryModel Logical GLSL450
  4312. OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
  4313. ;CHECK: OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor %inst_bindless_input_buffer %inst_bindless_output_buffer %gl_FragCoord
  4314. OpExecutionMode %MainPs OriginUpperLeft
  4315. OpSource HLSL 500
  4316. OpName %MainPs "MainPs"
  4317. OpName %PerBatchEnvMapConstantBuffer_t "PerBatchEnvMapConstantBuffer_t"
  4318. OpMemberName %PerBatchEnvMapConstantBuffer_t 0 "g_matEnvMapWorldToLocal"
  4319. OpMemberName %PerBatchEnvMapConstantBuffer_t 1 "g_vEnvironmentMapBoxMins"
  4320. OpMemberName %PerBatchEnvMapConstantBuffer_t 2 "g_TexOff"
  4321. OpName %_BindlessFastEnvMapCB_PS_t "_BindlessFastEnvMapCB_PS_t"
  4322. OpMemberName %_BindlessFastEnvMapCB_PS_t 0 "g_envMapConstants"
  4323. OpName %_ ""
  4324. OpName %PerViewPushConst_t "PerViewPushConst_t"
  4325. OpMemberName %PerViewPushConst_t 0 "g_c"
  4326. OpName %__0 ""
  4327. OpName %g_tColor "g_tColor"
  4328. OpName %g_sAniso "g_sAniso"
  4329. OpName %i_vTextureCoords "i.vTextureCoords"
  4330. OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
  4331. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 RowMajor
  4332. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 Offset 0
  4333. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 MatrixStride 16
  4334. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 1 Offset 48
  4335. OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 2 Offset 64
  4336. OpDecorate %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 ArrayStride 80
  4337. OpMemberDecorate %_BindlessFastEnvMapCB_PS_t 0 Offset 0
  4338. OpDecorate %_BindlessFastEnvMapCB_PS_t Block
  4339. OpDecorate %_ DescriptorSet 0
  4340. OpDecorate %_ Binding 0
  4341. OpMemberDecorate %PerViewPushConst_t 0 Offset 0
  4342. OpDecorate %PerViewPushConst_t Block
  4343. OpDecorate %g_tColor DescriptorSet 0
  4344. OpDecorate %g_tColor Binding 0
  4345. OpDecorate %g_sAniso DescriptorSet 0
  4346. OpDecorate %g_sAniso Binding 0
  4347. OpDecorate %i_vTextureCoords Location 0
  4348. OpDecorate %_entryPointOutput_vColor Location 0
  4349. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  4350. )" + kInputDecorations + kOutputDecorations + R"(
  4351. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  4352. %void = OpTypeVoid
  4353. %14 = OpTypeFunction %void
  4354. %float = OpTypeFloat 32
  4355. %v2float = OpTypeVector %float 2
  4356. %v4float = OpTypeVector %float 4
  4357. %v3float = OpTypeVector %float 3
  4358. %mat4v3float = OpTypeMatrix %v3float 4
  4359. %PerBatchEnvMapConstantBuffer_t = OpTypeStruct %mat4v3float %v4float %v2float
  4360. %uint = OpTypeInt 32 0
  4361. %uint_128 = OpConstant %uint 128
  4362. %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 = OpTypeArray %PerBatchEnvMapConstantBuffer_t %uint_128
  4363. %_BindlessFastEnvMapCB_PS_t = OpTypeStruct %_arr_PerBatchEnvMapConstantBuffer_t_uint_128
  4364. %_ptr_Uniform__BindlessFastEnvMapCB_PS_t = OpTypePointer Uniform %_BindlessFastEnvMapCB_PS_t
  4365. %_ = OpVariable %_ptr_Uniform__BindlessFastEnvMapCB_PS_t Uniform
  4366. %int = OpTypeInt 32 1
  4367. %int_0 = OpConstant %int 0
  4368. %ushort = OpTypeInt 16 0
  4369. %PerViewPushConst_t = OpTypeStruct %ushort
  4370. %_ptr_PushConstant_PerViewPushConst_t = OpTypePointer PushConstant %PerViewPushConst_t
  4371. %__0 = OpVariable %_ptr_PushConstant_PerViewPushConst_t PushConstant
  4372. %_ptr_PushConstant_ushort = OpTypePointer PushConstant %ushort
  4373. %int_2 = OpConstant %int 2
  4374. %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
  4375. %30 = OpTypeImage %float 2D 0 0 0 1 Unknown
  4376. %_ptr_UniformConstant_30 = OpTypePointer UniformConstant %30
  4377. %g_tColor = OpVariable %_ptr_UniformConstant_30 UniformConstant
  4378. %32 = OpTypeSampler
  4379. %_ptr_UniformConstant_32 = OpTypePointer UniformConstant %32
  4380. %g_sAniso = OpVariable %_ptr_UniformConstant_32 UniformConstant
  4381. %34 = OpTypeSampledImage %30
  4382. %_ptr_Input_v2float = OpTypePointer Input %v2float
  4383. %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
  4384. %_ptr_Output_v4float = OpTypePointer Output %v4float
  4385. %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
  4386. )" + kInputGlobals + kOutputGlobals + R"(
  4387. ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
  4388. ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  4389. ;CHECK: [[null_v2float:%\w+]] = OpConstantNull %v2float
  4390. %MainPs = OpFunction %void None %14
  4391. %37 = OpLabel
  4392. ;CHECK: %79 = OpFunctionCall %uint %inst_bindless_direct_read_3 %uint_1 %uint_0 %uint_0
  4393. ;CHECK: OpBranch %49
  4394. ;CHECK: %49 = OpLabel
  4395. ;CHECK: OpBranch %48
  4396. ;CHECK: %48 = OpLabel
  4397. %38 = OpLoad %v2float %i_vTextureCoords
  4398. %39 = OpAccessChain %_ptr_PushConstant_ushort %__0 %int_0
  4399. %40 = OpLoad %ushort %39
  4400. %41 = OpAccessChain %_ptr_Uniform_v2float %_ %int_0 %40 %int_2
  4401. %42 = OpLoad %v2float %41
  4402. %43 = OpFAdd %v2float %38 %42
  4403. ;CHECK-NOT: %42 = OpLoad %v2float %41
  4404. ;CHECK-NOT: %43 = OpFAdd %v2float %38 %42
  4405. ;CHECK: %52 = OpUConvert %uint %40
  4406. ;CHECK: %53 = OpIMul %uint %uint_80 %52
  4407. ;CHECK: %54 = OpIAdd %uint %uint_0 %53
  4408. ;CHECK: %56 = OpIAdd %uint %54 %uint_64
  4409. ;CHECK: %58 = OpIAdd %uint %56 %uint_7
  4410. ;CHECK: %81 = OpULessThan %bool %58 %79
  4411. ;CHECK: OpSelectionMerge %83 None
  4412. ;CHECK: OpBranchConditional %81 %84 %85
  4413. ;CHECK: %84 = OpLabel
  4414. ;CHECK: %86 = OpLoad %v2float %41
  4415. ;CHECK: OpBranch %83
  4416. ;CHECK: %85 = OpLabel
  4417. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_81 %uint_4 %uint_0 %58 %79
  4418. ;CHECK: OpBranch %83
  4419. ;CHECK: %83 = OpLabel
  4420. ;CHECK: %143 = OpPhi %v2float %86 %84 [[null_v2float]] %85
  4421. ;CHECK: %43 = OpFAdd %v2float %38 %143
  4422. %44 = OpLoad %30 %g_tColor
  4423. %45 = OpLoad %32 %g_sAniso
  4424. %46 = OpSampledImage %34 %44 %45
  4425. %47 = OpImageSampleImplicitLod %v4float %46 %43
  4426. OpStore %_entryPointOutput_vColor %47
  4427. OpReturn
  4428. OpFunctionEnd
  4429. )" + kDirectRead3 + kStreamWrite5Frag;
  4430. // clang-format on
  4431. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4432. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4433. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  4434. false, true, false, true);
  4435. }
  4436. TEST_F(InstBindlessTest, UniformMatrixRefRowMajor) {
  4437. // The buffer-oob row major matrix check
  4438. //
  4439. // #version 450
  4440. // #extension GL_EXT_scalar_block_layout : enable
  4441. //
  4442. // layout(location = 0) in highp vec4 a_position;
  4443. // layout(location = 0) out mediump float v_vtxResult;
  4444. //
  4445. // layout(set = 0, binding = 0, std430, row_major) uniform Block
  4446. // {
  4447. // lowp mat4x2 var;
  4448. // };
  4449. //
  4450. // void main (void)
  4451. // {
  4452. // v_vtxResult = var[2][1];
  4453. // }
  4454. // clang-format off
  4455. std::string text = R"(
  4456. OpCapability Shader
  4457. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4458. %1 = OpExtInstImport "GLSL.std.450"
  4459. OpMemoryModel Logical GLSL450
  4460. OpEntryPoint Vertex %main "main" %v_vtxResult %_ %a_position
  4461. ;CHECK: OpEntryPoint Vertex %main "main" %v_vtxResult %_ %a_position %inst_bindless_input_buffer %inst_bindless_output_buffer %gl_VertexIndex %gl_InstanceIndex
  4462. OpSource GLSL 450
  4463. OpSourceExtension "GL_EXT_scalar_block_layout"
  4464. OpName %main "main"
  4465. OpName %v_vtxResult "v_vtxResult"
  4466. OpName %Block "Block"
  4467. OpMemberName %Block 0 "var"
  4468. OpName %_ ""
  4469. OpName %a_position "a_position"
  4470. OpDecorate %v_vtxResult RelaxedPrecision
  4471. OpDecorate %v_vtxResult Location 0
  4472. OpMemberDecorate %Block 0 RowMajor
  4473. OpMemberDecorate %Block 0 RelaxedPrecision
  4474. OpMemberDecorate %Block 0 Offset 0
  4475. OpMemberDecorate %Block 0 MatrixStride 16
  4476. OpDecorate %Block Block
  4477. OpDecorate %_ DescriptorSet 0
  4478. OpDecorate %_ Binding 0
  4479. OpDecorate %21 RelaxedPrecision
  4480. ;CHECK-NOT: OpDecorate %21 RelaxedPrecision
  4481. ;CHECK: OpDecorate %116 RelaxedPrecision
  4482. OpDecorate %a_position Location 0
  4483. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  4484. )" + kInputDecorations + R"(
  4485. ;CHECK: OpDecorate %61 RelaxedPrecision
  4486. )" + kOutputDecorations + R"(
  4487. ;CHECK: OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  4488. ;CHECK: OpDecorate %gl_InstanceIndex BuiltIn InstanceIndex
  4489. %void = OpTypeVoid
  4490. %3 = OpTypeFunction %void
  4491. %float = OpTypeFloat 32
  4492. %_ptr_Output_float = OpTypePointer Output %float
  4493. %v_vtxResult = OpVariable %_ptr_Output_float Output
  4494. %v2float = OpTypeVector %float 2
  4495. %mat4v2float = OpTypeMatrix %v2float 4
  4496. %Block = OpTypeStruct %mat4v2float
  4497. %_ptr_Uniform_Block = OpTypePointer Uniform %Block
  4498. %_ = OpVariable %_ptr_Uniform_Block Uniform
  4499. %int = OpTypeInt 32 1
  4500. %int_0 = OpConstant %int 0
  4501. %int_2 = OpConstant %int 2
  4502. %uint = OpTypeInt 32 0
  4503. %uint_1 = OpConstant %uint 1
  4504. %_ptr_Uniform_float = OpTypePointer Uniform %float
  4505. %v4float = OpTypeVector %float 4
  4506. %_ptr_Input_v4float = OpTypePointer Input %v4float
  4507. %a_position = OpVariable %_ptr_Input_v4float Input
  4508. )" + kInputGlobals + kOutputGlobals + R"(
  4509. ;CHECK:%_ptr_Input_uint = OpTypePointer Input %uint
  4510. ;CHECK:%gl_VertexIndex = OpVariable %_ptr_Input_uint Input
  4511. ;CHECK:%gl_InstanceIndex = OpVariable %_ptr_Input_uint Input
  4512. ;CHECK: [[null_float:%\w+]] = OpConstantNull %float
  4513. %main = OpFunction %void None %3
  4514. %5 = OpLabel
  4515. ;CHECK: %55 = OpFunctionCall %uint %inst_bindless_direct_read_3 %uint_1 %uint_0 %uint_0
  4516. ;CHECK: OpBranch %26
  4517. ;CHECK: %26 = OpLabel
  4518. ;CHECK: OpBranch %25
  4519. ;CHECK: %25 = OpLabel
  4520. %20 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %int_2 %uint_1
  4521. %21 = OpLoad %float %20
  4522. ;CHECK-NOT: %21 = OpLoad %float %20
  4523. ;CHECK: %30 = OpIMul %uint %uint_4 %int_2
  4524. ;CHECK: %31 = OpIAdd %uint %uint_0 %30
  4525. ;CHECK: %32 = OpIMul %uint %uint_16 %uint_1
  4526. ;CHECK: %33 = OpIAdd %uint %31 %32
  4527. ;CHECK: %35 = OpIAdd %uint %33 %uint_3
  4528. ;CHECK: %57 = OpULessThan %bool %35 %55
  4529. ;CHECK: OpSelectionMerge %58 None
  4530. ;CHECK: OpBranchConditional %57 %59 %60
  4531. ;CHECK: %59 = OpLabel
  4532. ;CHECK: %61 = OpLoad %float %20
  4533. ;CHECK: OpBranch %58
  4534. ;CHECK: %60 = OpLabel
  4535. ;CHECK: {{\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_45 %uint_4 %uint_0 %35 %55
  4536. ;CHECK: OpBranch %58
  4537. ;CHECK: %58 = OpLabel
  4538. ;CHECK: %116 = OpPhi %float %61 %59 [[null_float]] %60
  4539. OpStore %v_vtxResult %21
  4540. ;CHECK-NOT: OpStore %v_vtxResult %21
  4541. ;CHECK: OpStore %v_vtxResult %116
  4542. OpReturn
  4543. OpFunctionEnd
  4544. )" + kDirectRead3 + kStreamWrite5Vert;
  4545. // clang-format on
  4546. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4547. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4548. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  4549. false, true, false, true);
  4550. }
  4551. TEST_F(InstBindlessTest, UniformMatrixRefColumnMajor) {
  4552. // The buffer-oob column major matrix check
  4553. //
  4554. // #version 450
  4555. // #extension GL_EXT_scalar_block_layout : enable
  4556. //
  4557. // layout(location = 0) in highp vec4 a_position;
  4558. // layout(location = 0) out mediump float v_vtxResult;
  4559. //
  4560. // layout(set = 0, binding = 0, std430, column_major) uniform Block
  4561. // {
  4562. // lowp mat4x2 var;
  4563. // };
  4564. //
  4565. // void main (void)
  4566. // {
  4567. // v_vtxResult = var[2][1];
  4568. // }
  4569. // clang-format off
  4570. const std::string text = R"(
  4571. OpCapability Shader
  4572. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4573. %1 = OpExtInstImport "GLSL.std.450"
  4574. OpMemoryModel Logical GLSL450
  4575. OpEntryPoint Vertex %main "main" %v_vtxResult %_ %a_position
  4576. ;CHECK: OpEntryPoint Vertex %main "main" %v_vtxResult %_ %a_position %inst_bindless_input_buffer %inst_bindless_output_buffer %gl_VertexIndex %gl_InstanceIndex
  4577. OpSource GLSL 450
  4578. OpSourceExtension "GL_EXT_scalar_block_layout"
  4579. OpName %main "main"
  4580. OpName %v_vtxResult "v_vtxResult"
  4581. OpName %Block "Block"
  4582. OpMemberName %Block 0 "var"
  4583. OpName %_ ""
  4584. OpName %a_position "a_position"
  4585. OpDecorate %v_vtxResult RelaxedPrecision
  4586. OpDecorate %v_vtxResult Location 0
  4587. OpMemberDecorate %Block 0 ColMajor
  4588. OpMemberDecorate %Block 0 RelaxedPrecision
  4589. OpMemberDecorate %Block 0 Offset 0
  4590. OpMemberDecorate %Block 0 MatrixStride 8
  4591. OpDecorate %Block Block
  4592. OpDecorate %_ DescriptorSet 0
  4593. OpDecorate %_ Binding 0
  4594. OpDecorate %21 RelaxedPrecision
  4595. ;CHECK-NOT: OpDecorate %21 RelaxedPrecision
  4596. ;CHECK: OpDecorate %115 RelaxedPrecision
  4597. OpDecorate %a_position Location 0
  4598. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  4599. )" + kInputDecorations + R"(
  4600. ;CHECK: OpDecorate %61 RelaxedPrecision
  4601. )" + kOutputDecorations + R"(
  4602. ;CHECK: OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  4603. ;CHECK: OpDecorate %gl_InstanceIndex BuiltIn InstanceIndex
  4604. %void = OpTypeVoid
  4605. %3 = OpTypeFunction %void
  4606. %float = OpTypeFloat 32
  4607. %_ptr_Output_float = OpTypePointer Output %float
  4608. %v_vtxResult = OpVariable %_ptr_Output_float Output
  4609. %v2float = OpTypeVector %float 2
  4610. %mat4v2float = OpTypeMatrix %v2float 4
  4611. %Block = OpTypeStruct %mat4v2float
  4612. %_ptr_Uniform_Block = OpTypePointer Uniform %Block
  4613. %_ = OpVariable %_ptr_Uniform_Block Uniform
  4614. %int = OpTypeInt 32 1
  4615. %int_0 = OpConstant %int 0
  4616. %int_2 = OpConstant %int 2
  4617. %uint = OpTypeInt 32 0
  4618. %uint_1 = OpConstant %uint 1
  4619. %_ptr_Uniform_float = OpTypePointer Uniform %float
  4620. %v4float = OpTypeVector %float 4
  4621. %_ptr_Input_v4float = OpTypePointer Input %v4float
  4622. %a_position = OpVariable %_ptr_Input_v4float Input
  4623. )" + kInputGlobals + kOutputGlobals + R"(
  4624. ;CHECK:%_ptr_Input_uint = OpTypePointer Input %uint
  4625. ;CHECK:%gl_VertexIndex = OpVariable %_ptr_Input_uint Input
  4626. ;CHECK:%gl_InstanceIndex = OpVariable %_ptr_Input_uint Input
  4627. ;CHECK: [[null_float:%\w+]] = OpConstantNull %float
  4628. %main = OpFunction %void None %3
  4629. %5 = OpLabel
  4630. ;CHECK: %55 = OpFunctionCall %uint %inst_bindless_direct_read_3 %uint_1 %uint_0 %uint_0
  4631. ;CHECK: OpBranch %26
  4632. ;CHECK: %26 = OpLabel
  4633. ;CHECK: OpBranch %25
  4634. ;CHECK: %25 = OpLabel
  4635. %20 = OpAccessChain %_ptr_Uniform_float %_ %int_0 %int_2 %uint_1
  4636. %21 = OpLoad %float %20
  4637. ;CHECK-NOT: %21 = OpLoad %float %20
  4638. ;CHECK: %29 = OpIMul %uint %uint_8 %int_2
  4639. ;CHECK: %30 = OpIAdd %uint %uint_0 %29
  4640. ;CHECK: %32 = OpIMul %uint %uint_4 %uint_1
  4641. ;CHECK: %33 = OpIAdd %uint %30 %32
  4642. ;CHECK: %35 = OpIAdd %uint %33 %uint_3
  4643. ;CHECK: %57 = OpULessThan %bool %35 %55
  4644. ;CHECK: OpSelectionMerge %58 None
  4645. ;CHECK: OpBranchConditional %57 %59 %60
  4646. ;CHECK: %59 = OpLabel
  4647. ;CHECK: %61 = OpLoad %float %20
  4648. ;CHECK: OpBranch %58
  4649. ;CHECK: %60 = OpLabel
  4650. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_45 %uint_4 %uint_0 %35 %55
  4651. ;CHECK: OpBranch %58
  4652. ;CHECK: %58 = OpLabel
  4653. ;CHECK: %115 = OpPhi %float %61 %59 [[null_float]] %60
  4654. OpStore %v_vtxResult %21
  4655. ;CHECK-NOT: OpStore %v_vtxResult %21
  4656. ;CHECK: OpStore %v_vtxResult %115
  4657. OpReturn
  4658. OpFunctionEnd
  4659. )" + kDirectRead3 + kStreamWrite5Vert;
  4660. // clang-format on
  4661. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4662. ValidatorOptions()->uniform_buffer_standard_layout = true;
  4663. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4664. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  4665. false, true, false, true);
  4666. }
  4667. TEST_F(InstBindlessTest, UniformMatrixVecRefRowMajor) {
  4668. // The buffer-oob row major matrix vector ref check
  4669. //
  4670. // #version 450
  4671. // #extension GL_EXT_scalar_block_layout : enable
  4672. //
  4673. // layout(location = 0) in highp vec4 a_position;
  4674. // layout(location = 0) out highp vec2 v_vtxResult;
  4675. //
  4676. // layout(set = 0, binding = 0, std430, row_major) uniform Block
  4677. // {
  4678. // lowp mat2 var[3][4];
  4679. // };
  4680. //
  4681. // void main (void)
  4682. // {
  4683. // v_vtxResult = var[2][3][1];
  4684. // }
  4685. // clang-format off
  4686. const std::string text = R"(
  4687. OpCapability Shader
  4688. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4689. %1 = OpExtInstImport "GLSL.std.450"
  4690. OpMemoryModel Logical GLSL450
  4691. OpEntryPoint Vertex %main "main" %v_vtxResult %_ %a_position
  4692. ;CHECK: OpEntryPoint Vertex %main "main" %v_vtxResult %_ %a_position %inst_bindless_input_buffer %inst_bindless_output_buffer %gl_VertexIndex %gl_InstanceIndex
  4693. OpSource GLSL 450
  4694. OpSourceExtension "GL_EXT_scalar_block_layout"
  4695. OpName %main "main"
  4696. OpName %v_vtxResult "v_vtxResult"
  4697. OpName %Block "Block"
  4698. OpMemberName %Block 0 "var"
  4699. OpName %_ ""
  4700. OpName %a_position "a_position"
  4701. OpDecorate %v_vtxResult Location 0
  4702. OpDecorate %_arr_mat2v2float_uint_4 ArrayStride 32
  4703. OpDecorate %_arr__arr_mat2v2float_uint_4_uint_3 ArrayStride 128
  4704. OpMemberDecorate %Block 0 RowMajor
  4705. OpMemberDecorate %Block 0 RelaxedPrecision
  4706. OpMemberDecorate %Block 0 Offset 0
  4707. OpMemberDecorate %Block 0 MatrixStride 16
  4708. OpDecorate %Block Block
  4709. OpDecorate %_ DescriptorSet 0
  4710. OpDecorate %_ Binding 0
  4711. OpDecorate %26 RelaxedPrecision
  4712. ;CHECK-NOT: OpDecorate %26 RelaxedPrecision
  4713. ;CHECK: OpDecorate %125 RelaxedPrecision
  4714. OpDecorate %a_position Location 0
  4715. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  4716. )" + kInputDecorations + R"(
  4717. ;CHECK: OpDecorate %70 RelaxedPrecision
  4718. )" + kOutputDecorations + R"(
  4719. ;CHECK: OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  4720. ;CHECK: OpDecorate %gl_InstanceIndex BuiltIn InstanceIndex
  4721. %void = OpTypeVoid
  4722. %3 = OpTypeFunction %void
  4723. %float = OpTypeFloat 32
  4724. %v2float = OpTypeVector %float 2
  4725. %_ptr_Output_v2float = OpTypePointer Output %v2float
  4726. %v_vtxResult = OpVariable %_ptr_Output_v2float Output
  4727. %mat2v2float = OpTypeMatrix %v2float 2
  4728. %uint = OpTypeInt 32 0
  4729. %uint_4 = OpConstant %uint 4
  4730. %_arr_mat2v2float_uint_4 = OpTypeArray %mat2v2float %uint_4
  4731. %uint_3 = OpConstant %uint 3
  4732. %_arr__arr_mat2v2float_uint_4_uint_3 = OpTypeArray %_arr_mat2v2float_uint_4 %uint_3
  4733. %Block = OpTypeStruct %_arr__arr_mat2v2float_uint_4_uint_3
  4734. %_ptr_Uniform_Block = OpTypePointer Uniform %Block
  4735. %_ = OpVariable %_ptr_Uniform_Block Uniform
  4736. %int = OpTypeInt 32 1
  4737. %int_0 = OpConstant %int 0
  4738. %int_2 = OpConstant %int 2
  4739. %int_3 = OpConstant %int 3
  4740. %int_1 = OpConstant %int 1
  4741. %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
  4742. %v4float = OpTypeVector %float 4
  4743. %_ptr_Input_v4float = OpTypePointer Input %v4float
  4744. %a_position = OpVariable %_ptr_Input_v4float Input
  4745. )" + kInputGlobals + kOutputGlobals + R"(
  4746. ;CHECK:%_ptr_Input_uint = OpTypePointer Input %uint
  4747. ;CHECK:%gl_VertexIndex = OpVariable %_ptr_Input_uint Input
  4748. ;CHECK:%gl_InstanceIndex = OpVariable %_ptr_Input_uint Input
  4749. ;CHECK: [[null_v2float:%\w+]] = OpConstantNull %v2float
  4750. %main = OpFunction %void None %3
  4751. %5 = OpLabel
  4752. ;CHECK: %64 = OpFunctionCall %uint %inst_bindless_direct_read_3 %uint_1 %uint_0 %uint_0
  4753. ;CHECK: OpBranch %31
  4754. ;CHECK: %31 = OpLabel
  4755. ;CHECK: OpBranch %30
  4756. ;CHECK: %30 = OpLabel
  4757. %25 = OpAccessChain %_ptr_Uniform_v2float %_ %int_0 %int_2 %int_3 %int_1
  4758. %26 = OpLoad %v2float %25
  4759. OpStore %v_vtxResult %26
  4760. ;CHECK-NOT: %26 = OpLoad %v2float %25
  4761. ;CHECK-NOT: OpStore %v_vtxResult %26
  4762. ;CHECK: %34 = OpIMul %uint %uint_128 %int_2
  4763. ;CHECK: %35 = OpIAdd %uint %uint_0 %34
  4764. ;CHECK: %37 = OpIMul %uint %uint_32 %int_3
  4765. ;CHECK: %38 = OpIAdd %uint %35 %37
  4766. ;CHECK: %40 = OpIMul %uint %uint_4 %int_1
  4767. ;CHECK: %41 = OpIAdd %uint %38 %40
  4768. ;CHECK: %43 = OpIAdd %uint %41 %uint_19
  4769. ;CHECK: %66 = OpULessThan %bool %43 %64
  4770. ;CHECK: OpSelectionMerge %67 None
  4771. ;CHECK: OpBranchConditional %66 %68 %69
  4772. ;CHECK: %68 = OpLabel
  4773. ;CHECK: %70 = OpLoad %v2float %25
  4774. ;CHECK: OpBranch %67
  4775. ;CHECK: %69 = OpLabel
  4776. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_51 %uint_4 %uint_0 %43 %64
  4777. ;CHECK: OpBranch %67
  4778. ;CHECK: %67 = OpLabel
  4779. ;CHECK: %125 = OpPhi %v2float %70 %68 [[null_v2float]] %69
  4780. ;CHECK: OpStore %v_vtxResult %125
  4781. OpReturn
  4782. OpFunctionEnd
  4783. )" + kDirectRead3 + kStreamWrite5Vert;
  4784. // clang-format on
  4785. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4786. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4787. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  4788. false, true, false, true);
  4789. }
  4790. TEST_F(InstBindlessTest, ImageBufferOOBRead) {
  4791. // Texel buffer (imagebuffer) oob check for ImageRead
  4792. //
  4793. // #version 450
  4794. // layout(set=3, binding=7, r32f) uniform readonly imageBuffer s;
  4795. // layout(location=11) out vec4 x;
  4796. // layout(location=13) in flat int ii;
  4797. //
  4798. // void main(){
  4799. // x = imageLoad(s, ii);
  4800. // }
  4801. // clang-format off
  4802. const std::string text = R"(
  4803. OpCapability Shader
  4804. OpCapability ImageBuffer
  4805. ;CHECK: OpCapability ImageQuery
  4806. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4807. %1 = OpExtInstImport "GLSL.std.450"
  4808. OpMemoryModel Logical GLSL450
  4809. OpEntryPoint Fragment %main "main" %x %s %ii
  4810. OpExecutionMode %main OriginUpperLeft
  4811. OpSource GLSL 450
  4812. OpName %main "main"
  4813. OpName %x "x"
  4814. OpName %s "s"
  4815. OpName %ii "ii"
  4816. OpDecorate %x Location 11
  4817. OpDecorate %s DescriptorSet 3
  4818. OpDecorate %s Binding 7
  4819. OpDecorate %s NonWritable
  4820. OpDecorate %ii Flat
  4821. OpDecorate %ii Location 13
  4822. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  4823. )" + kOutputDecorations + R"(
  4824. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  4825. %void = OpTypeVoid
  4826. %3 = OpTypeFunction %void
  4827. %float = OpTypeFloat 32
  4828. %v4float = OpTypeVector %float 4
  4829. %_ptr_Output_v4float = OpTypePointer Output %v4float
  4830. %x = OpVariable %_ptr_Output_v4float Output
  4831. %10 = OpTypeImage %float Buffer 0 0 0 2 R32f
  4832. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  4833. %s = OpVariable %_ptr_UniformConstant_10 UniformConstant
  4834. %int = OpTypeInt 32 1
  4835. %_ptr_Input_int = OpTypePointer Input %int
  4836. %ii = OpVariable %_ptr_Input_int Input
  4837. ;CHECK: %uint = OpTypeInt 32 0
  4838. ;CHECK: %bool = OpTypeBool
  4839. ;CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  4840. )" + kOutputGlobals + R"(
  4841. ;CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  4842. ;CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  4843. ;CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  4844. ;CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  4845. %main = OpFunction %void None %3
  4846. %5 = OpLabel
  4847. ;CHECK: OpBranch %21
  4848. ;CHECK: %21 = OpLabel
  4849. ;CHECK: OpBranch %20
  4850. ;CHECK: %20 = OpLabel
  4851. ;CHECK: OpBranch %19
  4852. ;CHECK: %19 = OpLabel
  4853. %13 = OpLoad %10 %s
  4854. %17 = OpLoad %int %ii
  4855. %18 = OpImageRead %v4float %13 %17
  4856. OpStore %x %18
  4857. ;CHECK-NOT: %18 = OpImageRead %v4float %13 %17
  4858. ;CHECK-NOT: OpStore %x %18
  4859. ;CHECK: %23 = OpBitcast %uint %17
  4860. ;CHECK: %25 = OpImageQuerySize %uint %13
  4861. ;CHECK: %27 = OpULessThan %bool %23 %25
  4862. ;CHECK: OpSelectionMerge %29 None
  4863. ;CHECK: OpBranchConditional %27 %30 %31
  4864. ;CHECK: %30 = OpLabel
  4865. ;CHECK: %32 = OpLoad %10 %s
  4866. ;CHECK: %33 = OpImageRead %v4float %32 %17
  4867. ;CHECK: OpBranch %29
  4868. ;CHECK: %31 = OpLabel
  4869. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_33 %uint_7 %uint_0 %23 %25
  4870. ;CHECK: OpBranch %29
  4871. ;CHECK: %29 = OpLabel
  4872. ;CHECK: %94 = OpPhi %v4float %33 %30 [[null_v4float]] %31
  4873. ;CHECK: OpStore %x %94
  4874. OpReturn
  4875. OpFunctionEnd
  4876. )" + kStreamWrite5Frag;
  4877. // clang-format on
  4878. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4879. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4880. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  4881. false, true, true, true);
  4882. }
  4883. TEST_F(InstBindlessTest, ImageBufferOOBWrite) {
  4884. // Texel buffer (imagebuffer) oob check for ImageWrite
  4885. //
  4886. // #version 450
  4887. // layout(set=3, binding=7, r32f) uniform readonly imageBuffer s;
  4888. // layout(location=11) out vec4 x;
  4889. // layout(location=13) in flat int ii;
  4890. //
  4891. // void main(){
  4892. // imageStore(s, ii, x);
  4893. // }
  4894. // clang-format off
  4895. const std::string text = R"(
  4896. OpCapability Shader
  4897. OpCapability ImageBuffer
  4898. ;CHECK: OpCapability ImageQuery
  4899. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4900. %1 = OpExtInstImport "GLSL.std.450"
  4901. OpMemoryModel Logical GLSL450
  4902. OpEntryPoint Fragment %main "main" %s %ii %x
  4903. ;CHECK: OpEntryPoint Fragment %main "main" %s %ii %x %inst_bindless_output_buffer %gl_FragCoord
  4904. OpExecutionMode %main OriginUpperLeft
  4905. OpSource GLSL 450
  4906. OpName %main "main"
  4907. OpName %s "s"
  4908. OpName %ii "ii"
  4909. OpName %x "x"
  4910. OpDecorate %s DescriptorSet 3
  4911. OpDecorate %s Binding 7
  4912. OpDecorate %s NonReadable
  4913. OpDecorate %ii Flat
  4914. OpDecorate %ii Location 13
  4915. OpDecorate %x Location 11
  4916. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  4917. )" + kOutputDecorations + R"(
  4918. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  4919. %void = OpTypeVoid
  4920. %3 = OpTypeFunction %void
  4921. %float = OpTypeFloat 32
  4922. %7 = OpTypeImage %float Buffer 0 0 0 2 R32f
  4923. %_ptr_UniformConstant_7 = OpTypePointer UniformConstant %7
  4924. %s = OpVariable %_ptr_UniformConstant_7 UniformConstant
  4925. %int = OpTypeInt 32 1
  4926. %_ptr_Input_int = OpTypePointer Input %int
  4927. %ii = OpVariable %_ptr_Input_int Input
  4928. %v4float = OpTypeVector %float 4
  4929. %_ptr_Output_v4float = OpTypePointer Output %v4float
  4930. %x = OpVariable %_ptr_Output_v4float Output
  4931. ;CHECK: %uint = OpTypeInt 32 0
  4932. ;CHECK: %bool = OpTypeBool
  4933. ;CHECK: {{%\w+}} = OpTypeFunction %void %uint %uint %uint %uint %uint
  4934. ;CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  4935. )" + kOutputGlobals + R"(
  4936. ;CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  4937. ;CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  4938. ;CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  4939. ;CHECK: %v4uint = OpTypeVector %uint 4
  4940. %main = OpFunction %void None %3
  4941. %5 = OpLabel
  4942. ;CHECK: OpBranch %21
  4943. ;CHECK: %21 = OpLabel
  4944. ;CHECK: OpBranch %20
  4945. ;CHECK: %20 = OpLabel
  4946. ;CHECK: OpBranch %19
  4947. ;CHECK: %19 = OpLabel
  4948. %10 = OpLoad %7 %s
  4949. %14 = OpLoad %int %ii
  4950. %18 = OpLoad %v4float %x
  4951. OpImageWrite %10 %14 %18
  4952. ;CHECK-NOT: OpImageWrite %10 %14 %18
  4953. ;CHECK: %23 = OpBitcast %uint %14
  4954. ;CHECK: %25 = OpImageQuerySize %uint %10
  4955. ;CHECK: %27 = OpULessThan %bool %23 %25
  4956. ;CHECK: OpSelectionMerge %29 None
  4957. ;CHECK: OpBranchConditional %27 %30 %31
  4958. ;CHECK: %30 = OpLabel
  4959. ;CHECK: %32 = OpLoad %7 %s
  4960. ;CHECK: OpImageWrite %32 %14 %18
  4961. ;CHECK: OpBranch %29
  4962. ;CHECK: %31 = OpLabel
  4963. ;CHECK: %91 = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_34 %uint_7 %uint_0 %23 %25
  4964. ;CHECK: OpBranch %29
  4965. ;CHECK: %29 = OpLabel
  4966. OpReturn
  4967. OpFunctionEnd
  4968. )" + kStreamWrite5Frag;
  4969. // clang-format on
  4970. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4971. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4972. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  4973. false, true, true, true);
  4974. }
  4975. TEST_F(InstBindlessTest, TextureBufferOOBFetch) {
  4976. // Texel buffer (texturebuffer) oob check for ImageFetch
  4977. //
  4978. // #version 450
  4979. // layout(set=3, binding=7) uniform textureBuffer s;
  4980. // layout(location=11) out vec4 x;
  4981. // layout(location=13) in flat int ii;
  4982. //
  4983. // void main(){
  4984. // x = texelFetch(s, ii);
  4985. // }
  4986. // clang-format off
  4987. const std::string text = R"(
  4988. OpCapability Shader
  4989. OpCapability SampledBuffer
  4990. ;CHECK: OpCapability ImageQuery
  4991. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  4992. %1 = OpExtInstImport "GLSL.std.450"
  4993. OpMemoryModel Logical GLSL450
  4994. OpEntryPoint Fragment %main "main" %x %s %ii
  4995. ;CHECK: OpEntryPoint Fragment %main "main" %x %s %ii %inst_bindless_output_buffer %gl_FragCoord
  4996. OpExecutionMode %main OriginUpperLeft
  4997. OpSource GLSL 450
  4998. OpName %main "main"
  4999. OpName %x "x"
  5000. OpName %s "s"
  5001. OpName %ii "ii"
  5002. OpDecorate %x Location 11
  5003. OpDecorate %s DescriptorSet 3
  5004. OpDecorate %s Binding 7
  5005. OpDecorate %ii Flat
  5006. OpDecorate %ii Location 13
  5007. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  5008. )" + kOutputDecorations + R"(
  5009. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  5010. %void = OpTypeVoid
  5011. %3 = OpTypeFunction %void
  5012. %float = OpTypeFloat 32
  5013. %v4float = OpTypeVector %float 4
  5014. %_ptr_Output_v4float = OpTypePointer Output %v4float
  5015. %x = OpVariable %_ptr_Output_v4float Output
  5016. %10 = OpTypeImage %float Buffer 0 0 0 1 Unknown
  5017. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  5018. %s = OpVariable %_ptr_UniformConstant_10 UniformConstant
  5019. %int = OpTypeInt 32 1
  5020. %_ptr_Input_int = OpTypePointer Input %int
  5021. %ii = OpVariable %_ptr_Input_int Input
  5022. ;CHECK: %uint = OpTypeInt 32 0
  5023. ;CHECK: %bool = OpTypeBool
  5024. ;CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  5025. )" + kOutputGlobals + R"(
  5026. ;CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  5027. ;CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  5028. ;CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  5029. ;CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  5030. %main = OpFunction %void None %3
  5031. %5 = OpLabel
  5032. ;CHECK: OpBranch %21
  5033. ;CHECK: %21 = OpLabel
  5034. ;CHECK: OpBranch %20
  5035. ;CHECK: %20 = OpLabel
  5036. ;CHECK: OpBranch %19
  5037. ;CHECK: %19 = OpLabel
  5038. %13 = OpLoad %10 %s
  5039. %17 = OpLoad %int %ii
  5040. %18 = OpImageFetch %v4float %13 %17
  5041. OpStore %x %18
  5042. ;CHECK-NOT: %18 = OpImageFetch %v4float %13 %17
  5043. ;CHECK-NOT: OpStore %x %18
  5044. ;CHECK: %23 = OpBitcast %uint %17
  5045. ;CHECK: %25 = OpImageQuerySize %uint %13
  5046. ;CHECK: %27 = OpULessThan %bool %23 %25
  5047. ;CHECK: OpSelectionMerge %29 None
  5048. ;CHECK: OpBranchConditional %27 %30 %31
  5049. ;CHECK: %30 = OpLabel
  5050. ;CHECK: %32 = OpLoad %10 %s
  5051. ;CHECK: %33 = OpImageFetch %v4float %32 %17
  5052. ;CHECK: OpBranch %29
  5053. ;CHECK: %31 = OpLabel
  5054. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_32 %uint_6 %uint_0 %23 %25
  5055. ;CHECK: OpBranch %29
  5056. ;CHECK: %29 = OpLabel
  5057. ;CHECK: %95 = OpPhi %v4float %33 %30 [[null_v4float]] %31
  5058. ;CHECK: OpStore %x %95
  5059. OpReturn
  5060. OpFunctionEnd
  5061. )" + kStreamWrite5Frag;
  5062. // clang-format on
  5063. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  5064. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  5065. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  5066. false, true, true, true);
  5067. }
  5068. TEST_F(InstBindlessTest, SamplerBufferOOBFetch) {
  5069. // Texel buffer (samplerbuffer) oob check for ImageFetch
  5070. //
  5071. // #version 450
  5072. // layout(set=3, binding=7) uniform samplerBuffer s;
  5073. // layout(location=11) out vec4 x;
  5074. // layout(location=13) in flat int ii;
  5075. //
  5076. // void main(){
  5077. // x = texelFetch(s, ii);
  5078. // }
  5079. // clang-format off
  5080. const std::string text = R"(
  5081. OpCapability Shader
  5082. OpCapability SampledBuffer
  5083. ;CHECK: OpCapability ImageQuery
  5084. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  5085. %1 = OpExtInstImport "GLSL.std.450"
  5086. OpMemoryModel Logical GLSL450
  5087. OpEntryPoint Fragment %main "main" %x %s %ii
  5088. ;CHECK: OpEntryPoint Fragment %main "main" %x %s %ii %inst_bindless_output_buffer %gl_FragCoord
  5089. OpExecutionMode %main OriginUpperLeft
  5090. OpSource GLSL 450
  5091. OpName %main "main"
  5092. OpName %x "x"
  5093. OpName %s "s"
  5094. OpName %ii "ii"
  5095. OpDecorate %x Location 11
  5096. OpDecorate %s DescriptorSet 3
  5097. OpDecorate %s Binding 7
  5098. OpDecorate %ii Flat
  5099. OpDecorate %ii Location 13
  5100. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  5101. )" + kOutputDecorations + R"(
  5102. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  5103. %void = OpTypeVoid
  5104. %3 = OpTypeFunction %void
  5105. %float = OpTypeFloat 32
  5106. %v4float = OpTypeVector %float 4
  5107. %_ptr_Output_v4float = OpTypePointer Output %v4float
  5108. %x = OpVariable %_ptr_Output_v4float Output
  5109. %10 = OpTypeImage %float Buffer 0 0 0 1 Unknown
  5110. %11 = OpTypeSampledImage %10
  5111. %_ptr_UniformConstant_11 = OpTypePointer UniformConstant %11
  5112. %s = OpVariable %_ptr_UniformConstant_11 UniformConstant
  5113. %int = OpTypeInt 32 1
  5114. %_ptr_Input_int = OpTypePointer Input %int
  5115. %ii = OpVariable %_ptr_Input_int Input
  5116. ;CHECK: %uint = OpTypeInt 32 0
  5117. ;CHECK: %bool = OpTypeBool
  5118. ;CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  5119. )" + kOutputGlobals + R"(
  5120. ;CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  5121. ;CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  5122. ;CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  5123. ;CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  5124. %main = OpFunction %void None %3
  5125. %5 = OpLabel
  5126. ;CHECK: OpBranch %23
  5127. ;CHECK: %23 = OpLabel
  5128. ;CHECK: OpBranch %22
  5129. ;CHECK: %22 = OpLabel
  5130. ;CHECK: OpBranch %21
  5131. ;CHECK: %21 = OpLabel
  5132. %14 = OpLoad %11 %s
  5133. %18 = OpLoad %int %ii
  5134. %19 = OpImage %10 %14
  5135. %20 = OpImageFetch %v4float %19 %18
  5136. OpStore %x %20
  5137. ;CHECK-NOT: %20 = OpImageFetch %v4float %19 %18
  5138. ;CHECK-NOT: OpStore %x %20
  5139. ;CHECK: %25 = OpBitcast %uint %18
  5140. ;CHECK: %27 = OpImageQuerySize %uint %19
  5141. ;CHECK: %29 = OpULessThan %bool %25 %27
  5142. ;CHECK: OpSelectionMerge %31 None
  5143. ;CHECK: OpBranchConditional %29 %32 %33
  5144. ;CHECK: %32 = OpLabel
  5145. ;CHECK: %34 = OpLoad %11 %s
  5146. ;CHECK: %35 = OpImage %10 %34
  5147. ;CHECK: %36 = OpImageFetch %v4float %35 %18
  5148. ;CHECK: OpBranch %31
  5149. ;CHECK: %33 = OpLabel
  5150. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_34 %uint_6 %uint_0 %25 %27
  5151. ;CHECK: OpBranch %31
  5152. ;CHECK: %31 = OpLabel
  5153. ;CHECK: %98 = OpPhi %v4float %36 %32 [[null_v4float]] %33
  5154. ;CHECK: OpStore %x %98
  5155. OpReturn
  5156. OpFunctionEnd
  5157. )" + kStreamWrite5Frag;
  5158. // clang-format on
  5159. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  5160. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  5161. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  5162. false, true, true, true);
  5163. }
  5164. TEST_F(InstBindlessTest, SamplerBufferConstructorOOBFetch) {
  5165. // Texel buffer (samplerbuffer constructor) oob check for ImageFetch
  5166. //
  5167. // #version 450
  5168. // layout(set=3, binding=7) uniform textureBuffer tBuf;
  5169. // layout(set=3, binding=8) uniform sampler s;
  5170. // layout(location=11) out vec4 x;
  5171. // layout(location=13) in flat int ii;
  5172. //
  5173. // void main(){
  5174. // x = texelFetch(samplerBuffer(tBuf, s), ii);
  5175. // }
  5176. // clang-format off
  5177. const std::string text = R"(
  5178. OpCapability Shader
  5179. OpCapability SampledBuffer
  5180. ;CHECK: OpCapability ImageQuery
  5181. ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
  5182. %1 = OpExtInstImport "GLSL.std.450"
  5183. OpMemoryModel Logical GLSL450
  5184. OpEntryPoint Fragment %main "main" %x %tBuf %s %ii
  5185. ;CHECK: OpEntryPoint Fragment %main "main" %x %tBuf %s %ii %inst_bindless_output_buffer %gl_FragCoord
  5186. OpExecutionMode %main OriginUpperLeft
  5187. OpSource GLSL 450
  5188. OpName %main "main"
  5189. OpName %x "x"
  5190. OpName %tBuf "tBuf"
  5191. OpName %s "s"
  5192. OpName %ii "ii"
  5193. OpDecorate %x Location 11
  5194. OpDecorate %tBuf DescriptorSet 3
  5195. OpDecorate %tBuf Binding 7
  5196. OpDecorate %s DescriptorSet 3
  5197. OpDecorate %s Binding 8
  5198. OpDecorate %ii Flat
  5199. OpDecorate %ii Location 13
  5200. ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
  5201. )" + kOutputDecorations + R"(
  5202. ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
  5203. %void = OpTypeVoid
  5204. %3 = OpTypeFunction %void
  5205. %float = OpTypeFloat 32
  5206. %v4float = OpTypeVector %float 4
  5207. %_ptr_Output_v4float = OpTypePointer Output %v4float
  5208. %x = OpVariable %_ptr_Output_v4float Output
  5209. %10 = OpTypeImage %float Buffer 0 0 0 1 Unknown
  5210. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  5211. %tBuf = OpVariable %_ptr_UniformConstant_10 UniformConstant
  5212. %14 = OpTypeSampler
  5213. %_ptr_UniformConstant_14 = OpTypePointer UniformConstant %14
  5214. %s = OpVariable %_ptr_UniformConstant_14 UniformConstant
  5215. %18 = OpTypeSampledImage %10
  5216. %int = OpTypeInt 32 1
  5217. %_ptr_Input_int = OpTypePointer Input %int
  5218. %ii = OpVariable %_ptr_Input_int Input
  5219. ;CHECK: %uint = OpTypeInt 32 0
  5220. ;CHECK: %bool = OpTypeBool
  5221. ;CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
  5222. )" + kOutputGlobals + R"(
  5223. ;CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
  5224. ;CHECK: %_ptr_Input_v4float = OpTypePointer Input %v4float
  5225. ;CHECK: %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  5226. ;CHECK: [[null_v4float:%\w+]] = OpConstantNull %v4float
  5227. %main = OpFunction %void None %3
  5228. %5 = OpLabel
  5229. ;CHECK: OpBranch %28
  5230. ;CHECK: %28 = OpLabel
  5231. ;CHECK: OpBranch %27
  5232. ;CHECK: %27 = OpLabel
  5233. ;CHECK: OpBranch %26
  5234. ;CHECK: %26 = OpLabel
  5235. %13 = OpLoad %10 %tBuf
  5236. %17 = OpLoad %14 %s
  5237. %19 = OpSampledImage %18 %13 %17
  5238. %23 = OpLoad %int %ii
  5239. %24 = OpImage %10 %19
  5240. %25 = OpImageFetch %v4float %24 %23
  5241. OpStore %x %25
  5242. ;CHECK-NOT: %25 = OpImageFetch %v4float %24 %23
  5243. ;CHECK-NOT: OpStore %x %25
  5244. ;CHECK: %30 = OpBitcast %uint %23
  5245. ;CHECK: %32 = OpImageQuerySize %uint %24
  5246. ;CHECK: %34 = OpULessThan %bool %30 %32
  5247. ;CHECK: OpSelectionMerge %36 None
  5248. ;CHECK: OpBranchConditional %34 %37 %38
  5249. ;CHECK: %37 = OpLabel
  5250. ;CHECK: %39 = OpLoad %10 %tBuf
  5251. ;CHECK: %40 = OpSampledImage %18 %39 %17
  5252. ;CHECK: %41 = OpImage %10 %40
  5253. ;CHECK: %42 = OpImageFetch %v4float %41 %23
  5254. ;CHECK: OpBranch %36
  5255. ;CHECK: %38 = OpLabel
  5256. ;CHECK: {{%\w+}} = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_42 %uint_6 %uint_0 %30 %32
  5257. ;CHECK: OpBranch %36
  5258. ;CHECK: %36 = OpLabel
  5259. ;CHECK: %104 = OpPhi %v4float %42 %37 [[null_v4float]] %38
  5260. ;CHECK: OpStore %x %104
  5261. OpReturn
  5262. OpFunctionEnd
  5263. )" + kStreamWrite5Frag;
  5264. // clang-format on
  5265. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  5266. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  5267. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
  5268. false, true, true, true);
  5269. }
  5270. TEST_F(InstBindlessTest, DeviceBufferAddressOOB) {
  5271. // #version 450
  5272. // #extension GL_EXT_buffer_reference : enable
  5273. // layout(buffer_reference, buffer_reference_align = 16) buffer bufStruct;
  5274. // layout(set = 0, binding = 0) uniform ufoo {
  5275. // bufStruct data;
  5276. // int nWrites;
  5277. // } u_info;
  5278. // layout(buffer_reference, std140) buffer bufStruct {
  5279. // int a[4];
  5280. // };
  5281. // void main() {
  5282. // for (int i=0; i < u_info.nWrites; ++i) {
  5283. // u_info.data.a[i] = 0xdeadca71;
  5284. // }
  5285. // }
  5286. // clang-format off
  5287. const std::string text = R"(
  5288. OpCapability Shader
  5289. OpCapability PhysicalStorageBufferAddresses
  5290. ;CHECK: OpCapability Int64
  5291. %1 = OpExtInstImport "GLSL.std.450"
  5292. OpMemoryModel PhysicalStorageBuffer64 GLSL450
  5293. OpEntryPoint Vertex %main "main" %u_info
  5294. ;CHECK: OpEntryPoint Vertex %main "main" %u_info %inst_bindless_input_buffer %inst_bindless_output_buffer %gl_VertexIndex %gl_InstanceIndex
  5295. OpSource GLSL 450
  5296. OpSourceExtension "GL_EXT_buffer_reference"
  5297. OpName %main "main"
  5298. OpName %i "i"
  5299. OpName %ufoo "ufoo"
  5300. OpMemberName %ufoo 0 "data"
  5301. OpMemberName %ufoo 1 "nWrites"
  5302. OpName %bufStruct "bufStruct"
  5303. OpMemberName %bufStruct 0 "a"
  5304. OpName %u_info "u_info"
  5305. OpMemberDecorate %ufoo 0 Offset 0
  5306. OpMemberDecorate %ufoo 1 Offset 8
  5307. OpDecorate %ufoo Block
  5308. OpDecorate %_arr_int_uint_4 ArrayStride 16
  5309. OpMemberDecorate %bufStruct 0 Offset 0
  5310. OpDecorate %bufStruct Block
  5311. OpDecorate %u_info DescriptorSet 0
  5312. OpDecorate %u_info Binding 0)"
  5313. + kInputDecorations + kOutputDecorations +
  5314. R"(%void = OpTypeVoid
  5315. %3 = OpTypeFunction %void
  5316. %int = OpTypeInt 32 1
  5317. %_ptr_Function_int = OpTypePointer Function %int
  5318. %int_0 = OpConstant %int 0
  5319. OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_bufStruct PhysicalStorageBuffer
  5320. %ufoo = OpTypeStruct %_ptr_PhysicalStorageBuffer_bufStruct %int
  5321. %uint = OpTypeInt 32 0
  5322. %uint_4 = OpConstant %uint 4
  5323. %_arr_int_uint_4 = OpTypeArray %int %uint_4
  5324. %bufStruct = OpTypeStruct %_arr_int_uint_4
  5325. %_ptr_PhysicalStorageBuffer_bufStruct = OpTypePointer PhysicalStorageBuffer %bufStruct
  5326. %_ptr_Uniform_ufoo = OpTypePointer Uniform %ufoo
  5327. %u_info = OpVariable %_ptr_Uniform_ufoo Uniform
  5328. %int_1 = OpConstant %int 1
  5329. %_ptr_Uniform_int = OpTypePointer Uniform %int
  5330. %bool = OpTypeBool
  5331. %_ptr_Uniform__ptr_PhysicalStorageBuffer_bufStruct = OpTypePointer Uniform %_ptr_PhysicalStorageBuffer_bufStruct
  5332. %int_n559035791 = OpConstant %int -559035791
  5333. %_ptr_PhysicalStorageBuffer_int = OpTypePointer PhysicalStorageBuffer %int)"
  5334. + kInputGlobals + kOutputGlobals +
  5335. R"(%main = OpFunction %void None %3
  5336. %5 = OpLabel
  5337. %i = OpVariable %_ptr_Function_int Function
  5338. ;CHECK: OpBranch %137
  5339. ;CHECK: %137 = OpLabel
  5340. ;CHECK: 65 = OpFunctionCall %uint %inst_bindless_direct_read_3 %uint_1 %uint_0 %uint_0
  5341. ;CHECK: OpBranch %40
  5342. ;CHECK: 40 = OpLabel
  5343. ;CHECK: OpBranch %39
  5344. ;CHECK: 39 = OpLabel
  5345. OpStore %i %int_0
  5346. OpBranch %10
  5347. %10 = OpLabel
  5348. OpLoopMerge %12 %13 None
  5349. OpBranch %14
  5350. %14 = OpLabel
  5351. %15 = OpLoad %int %i
  5352. %26 = OpAccessChain %_ptr_Uniform_int %u_info %int_1
  5353. %27 = OpLoad %int %26
  5354. %29 = OpSLessThan %bool %15 %27
  5355. ;CHECK-NOT: %27 = OpLoad %int %26
  5356. ;CHECK-NOT: %29 = OpSLessThan %bool %15 %27
  5357. ;CHECK: 43 = OpIAdd %uint %uint_8 %uint_3
  5358. ;CHECK: 66 = OpULessThan %bool %43 %65
  5359. ;CHECK: OpSelectionMerge %67 None
  5360. ;CHECK: OpBranchConditional %66 %68 %69
  5361. ;CHECK: 68 = OpLabel
  5362. ;CHECK: 70 = OpLoad %int %26
  5363. ;CHECK: OpBranch %67
  5364. ;CHECK: 69 = OpLabel
  5365. ;CHECK: 122 = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_54 %uint_4 %uint_0 %43 %65
  5366. ;CHECK: OpBranch %67
  5367. ;CHECK: 67 = OpLabel
  5368. ;CHECK: 124 = OpPhi %int %70 %68 %123 %69
  5369. ;CHECK: 29 = OpSLessThan %bool %15 %124
  5370. OpBranchConditional %29 %11 %12
  5371. %11 = OpLabel
  5372. %31 = OpAccessChain %_ptr_Uniform__ptr_PhysicalStorageBuffer_bufStruct %u_info %int_0
  5373. %32 = OpLoad %_ptr_PhysicalStorageBuffer_bufStruct %31
  5374. ;CHECK-NOT: %32 = OpLoad %_ptr_PhysicalStorageBuffer_bufStruct %31
  5375. ;CHECK: 125 = OpIAdd %uint %uint_0 %uint_7
  5376. ;CHECK: 126 = OpULessThan %bool %125 %65
  5377. ;CHECK: OpSelectionMerge %127 None
  5378. ;CHECK: OpBranchConditional %126 %128 %129
  5379. ;CHECK: 128 = OpLabel
  5380. ;CHECK: 130 = OpLoad %_ptr_PhysicalStorageBuffer_bufStruct %31
  5381. ;CHECK: OpBranch %127
  5382. ;CHECK: 129 = OpLabel
  5383. ;CHECK: 132 = OpFunctionCall %void %inst_bindless_stream_write_5 %uint_59 %uint_4 %uint_0 %125 %65
  5384. ;CHECK: 135 = OpConvertUToPtr %_ptr_PhysicalStorageBuffer_bufStruct %134
  5385. ;CHECK: OpBranch %127
  5386. ;CHECK: 127 = OpLabel
  5387. ;CHECK: 136 = OpPhi %_ptr_PhysicalStorageBuffer_bufStruct %130 %128 %135 %129
  5388. %33 = OpLoad %int %i
  5389. %36 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %32 %int_0 %33
  5390. ;CHECK-NOT: %36 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %32 %int_0 %33
  5391. ;CHECK: %36 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %136 %int_0 %33
  5392. OpStore %36 %int_n559035791 Aligned 16
  5393. OpBranch %13
  5394. %13 = OpLabel
  5395. %37 = OpLoad %int %i
  5396. %38 = OpIAdd %int %37 %int_1
  5397. OpStore %i %38
  5398. OpBranch %10
  5399. %12 = OpLabel
  5400. OpReturn
  5401. OpFunctionEnd)"
  5402. + kDirectRead3 + kStreamWrite5Vert;
  5403. // clang-format on
  5404. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  5405. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  5406. SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false, true,
  5407. true, true, true);
  5408. }
  5409. // TODO(greg-lunarg): Add tests to verify handling of these cases:
  5410. //
  5411. // Compute shader
  5412. // Geometry shader
  5413. // Tessellation control shader
  5414. // Tessellation eval shader
  5415. // OpImage
  5416. // SampledImage variable
  5417. } // namespace
  5418. } // namespace opt
  5419. } // namespace spvtools