Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iris_state.c 191KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259
  1. /*
  2. * Copyright © 2017 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /**
  23. * @file iris_state.c
  24. *
  25. * ============================= GENXML CODE =============================
  26. * [This file is compiled once per generation.]
  27. * =======================================================================
  28. *
  29. * This is the main state upload code.
  30. *
  31. * Gallium uses Constant State Objects, or CSOs, for most state. Large,
  32. * complex, or highly reusable state can be created once, and bound and
  33. * rebound multiple times. This is modeled with the pipe->create_*_state()
  34. * and pipe->bind_*_state() hooks. Highly dynamic or inexpensive state is
  35. * streamed out on the fly, via pipe->set_*_state() hooks.
  36. *
  37. * OpenGL involves frequently mutating context state, which is mirrored in
  38. * core Mesa by highly mutable data structures. However, most applications
  39. * typically draw the same things over and over - from frame to frame, most
  40. * of the same objects are still visible and need to be redrawn. So, rather
  41. * than inventing new state all the time, applications usually mutate to swap
  42. * between known states that we've seen before.
  43. *
  44. * Gallium isolates us from this mutation by tracking API state, and
  45. * distilling it into a set of Constant State Objects, or CSOs. Large,
  46. * complex, or typically reusable state can be created once, then reused
  47. * multiple times. Drivers can create and store their own associated data.
  48. * This create/bind model corresponds to the pipe->create_*_state() and
  49. * pipe->bind_*_state() driver hooks.
  50. *
  51. * Some state is cheap to create, or expected to be highly dynamic. Rather
  52. * than creating and caching piles of CSOs for these, Gallium simply streams
  53. * them out, via the pipe->set_*_state() driver hooks.
  54. *
  55. * To reduce draw time overhead, we try to compute as much state at create
  56. * time as possible. Wherever possible, we translate the Gallium pipe state
  57. * to 3DSTATE commands, and store those commands in the CSO. At draw time,
  58. * we can simply memcpy them into a batch buffer.
  59. *
  60. * No hardware matches the abstraction perfectly, so some commands require
  61. * information from multiple CSOs. In this case, we can store two copies
  62. * of the packet (one in each CSO), and simply | together their DWords at
  63. * draw time. Sometimes the second set is trivial (one or two fields), so
  64. * we simply pack it at draw time.
  65. *
  66. * There are two main components in the file below. First, the CSO hooks
  67. * create/bind/track state. The second are the draw-time upload functions,
  68. * iris_upload_render_state() and iris_upload_compute_state(), which read
  69. * the context state and emit the commands into the actual batch.
  70. */
  71. #include <stdio.h>
  72. #include <errno.h>
  73. #if HAVE_VALGRIND
  74. #include <valgrind.h>
  75. #include <memcheck.h>
  76. #define VG(x) x
  77. #ifndef NDEBUG
  78. #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
  79. #endif
  80. #else
  81. #define VG(x)
  82. #endif
  83. #include "pipe/p_defines.h"
  84. #include "pipe/p_state.h"
  85. #include "pipe/p_context.h"
  86. #include "pipe/p_screen.h"
  87. #include "util/u_inlines.h"
  88. #include "util/u_format.h"
  89. #include "util/u_framebuffer.h"
  90. #include "util/u_transfer.h"
  91. #include "util/u_upload_mgr.h"
  92. #include "util/u_viewport.h"
  93. #include "i915_drm.h"
  94. #include "nir.h"
  95. #include "intel/compiler/brw_compiler.h"
  96. #include "intel/common/gen_l3_config.h"
  97. #include "intel/common/gen_sample_positions.h"
  98. #include "iris_batch.h"
  99. #include "iris_context.h"
  100. #include "iris_pipe.h"
  101. #include "iris_resource.h"
  102. #define __gen_address_type struct iris_address
  103. #define __gen_user_data struct iris_batch
  104. #define ARRAY_BYTES(x) (sizeof(uint32_t) * ARRAY_SIZE(x))
  105. static uint64_t
  106. __gen_combine_address(struct iris_batch *batch, void *location,
  107. struct iris_address addr, uint32_t delta)
  108. {
  109. uint64_t result = addr.offset + delta;
  110. if (addr.bo) {
  111. iris_use_pinned_bo(batch, addr.bo, addr.write);
  112. /* Assume this is a general address, not relative to a base. */
  113. result += addr.bo->gtt_offset;
  114. }
  115. return result;
  116. }
  117. #define __genxml_cmd_length(cmd) cmd ## _length
  118. #define __genxml_cmd_length_bias(cmd) cmd ## _length_bias
  119. #define __genxml_cmd_header(cmd) cmd ## _header
  120. #define __genxml_cmd_pack(cmd) cmd ## _pack
  121. #define _iris_pack_command(batch, cmd, dst, name) \
  122. for (struct cmd name = { __genxml_cmd_header(cmd) }, \
  123. *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
  124. ({ __genxml_cmd_pack(cmd)(batch, (void *)_dst, &name); \
  125. _dst = NULL; \
  126. }))
  127. #define iris_pack_command(cmd, dst, name) \
  128. _iris_pack_command(NULL, cmd, dst, name)
  129. #define iris_pack_state(cmd, dst, name) \
  130. for (struct cmd name = {}, \
  131. *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
  132. __genxml_cmd_pack(cmd)(NULL, (void *)_dst, &name), \
  133. _dst = NULL)
  134. #define iris_emit_cmd(batch, cmd, name) \
  135. _iris_pack_command(batch, cmd, iris_get_command_space(batch, 4 * __genxml_cmd_length(cmd)), name)
  136. #define iris_emit_merge(batch, dwords0, dwords1, num_dwords) \
  137. do { \
  138. uint32_t *dw = iris_get_command_space(batch, 4 * num_dwords); \
  139. for (uint32_t i = 0; i < num_dwords; i++) \
  140. dw[i] = (dwords0)[i] | (dwords1)[i]; \
  141. VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, num_dwords)); \
  142. } while (0)
  143. #include "genxml/genX_pack.h"
  144. #include "genxml/gen_macros.h"
  145. #include "genxml/genX_bits.h"
  146. #define MOCS_WB (2 << 1)
  147. /**
  148. * Statically assert that PIPE_* enums match the hardware packets.
  149. * (As long as they match, we don't need to translate them.)
  150. */
  151. UNUSED static void pipe_asserts()
  152. {
  153. #define PIPE_ASSERT(x) STATIC_ASSERT((int)x)
  154. /* pipe_logicop happens to match the hardware. */
  155. PIPE_ASSERT(PIPE_LOGICOP_CLEAR == LOGICOP_CLEAR);
  156. PIPE_ASSERT(PIPE_LOGICOP_NOR == LOGICOP_NOR);
  157. PIPE_ASSERT(PIPE_LOGICOP_AND_INVERTED == LOGICOP_AND_INVERTED);
  158. PIPE_ASSERT(PIPE_LOGICOP_COPY_INVERTED == LOGICOP_COPY_INVERTED);
  159. PIPE_ASSERT(PIPE_LOGICOP_AND_REVERSE == LOGICOP_AND_REVERSE);
  160. PIPE_ASSERT(PIPE_LOGICOP_INVERT == LOGICOP_INVERT);
  161. PIPE_ASSERT(PIPE_LOGICOP_XOR == LOGICOP_XOR);
  162. PIPE_ASSERT(PIPE_LOGICOP_NAND == LOGICOP_NAND);
  163. PIPE_ASSERT(PIPE_LOGICOP_AND == LOGICOP_AND);
  164. PIPE_ASSERT(PIPE_LOGICOP_EQUIV == LOGICOP_EQUIV);
  165. PIPE_ASSERT(PIPE_LOGICOP_NOOP == LOGICOP_NOOP);
  166. PIPE_ASSERT(PIPE_LOGICOP_OR_INVERTED == LOGICOP_OR_INVERTED);
  167. PIPE_ASSERT(PIPE_LOGICOP_COPY == LOGICOP_COPY);
  168. PIPE_ASSERT(PIPE_LOGICOP_OR_REVERSE == LOGICOP_OR_REVERSE);
  169. PIPE_ASSERT(PIPE_LOGICOP_OR == LOGICOP_OR);
  170. PIPE_ASSERT(PIPE_LOGICOP_SET == LOGICOP_SET);
  171. /* pipe_blend_func happens to match the hardware. */
  172. PIPE_ASSERT(PIPE_BLENDFACTOR_ONE == BLENDFACTOR_ONE);
  173. PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_COLOR == BLENDFACTOR_SRC_COLOR);
  174. PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA == BLENDFACTOR_SRC_ALPHA);
  175. PIPE_ASSERT(PIPE_BLENDFACTOR_DST_ALPHA == BLENDFACTOR_DST_ALPHA);
  176. PIPE_ASSERT(PIPE_BLENDFACTOR_DST_COLOR == BLENDFACTOR_DST_COLOR);
  177. PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE == BLENDFACTOR_SRC_ALPHA_SATURATE);
  178. PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_COLOR == BLENDFACTOR_CONST_COLOR);
  179. PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_ALPHA == BLENDFACTOR_CONST_ALPHA);
  180. PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_COLOR == BLENDFACTOR_SRC1_COLOR);
  181. PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_ALPHA == BLENDFACTOR_SRC1_ALPHA);
  182. PIPE_ASSERT(PIPE_BLENDFACTOR_ZERO == BLENDFACTOR_ZERO);
  183. PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_COLOR == BLENDFACTOR_INV_SRC_COLOR);
  184. PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_ALPHA == BLENDFACTOR_INV_SRC_ALPHA);
  185. PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_ALPHA == BLENDFACTOR_INV_DST_ALPHA);
  186. PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_COLOR == BLENDFACTOR_INV_DST_COLOR);
  187. PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_COLOR == BLENDFACTOR_INV_CONST_COLOR);
  188. PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_ALPHA == BLENDFACTOR_INV_CONST_ALPHA);
  189. PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_COLOR == BLENDFACTOR_INV_SRC1_COLOR);
  190. PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_ALPHA == BLENDFACTOR_INV_SRC1_ALPHA);
  191. /* pipe_blend_func happens to match the hardware. */
  192. PIPE_ASSERT(PIPE_BLEND_ADD == BLENDFUNCTION_ADD);
  193. PIPE_ASSERT(PIPE_BLEND_SUBTRACT == BLENDFUNCTION_SUBTRACT);
  194. PIPE_ASSERT(PIPE_BLEND_REVERSE_SUBTRACT == BLENDFUNCTION_REVERSE_SUBTRACT);
  195. PIPE_ASSERT(PIPE_BLEND_MIN == BLENDFUNCTION_MIN);
  196. PIPE_ASSERT(PIPE_BLEND_MAX == BLENDFUNCTION_MAX);
  197. /* pipe_stencil_op happens to match the hardware. */
  198. PIPE_ASSERT(PIPE_STENCIL_OP_KEEP == STENCILOP_KEEP);
  199. PIPE_ASSERT(PIPE_STENCIL_OP_ZERO == STENCILOP_ZERO);
  200. PIPE_ASSERT(PIPE_STENCIL_OP_REPLACE == STENCILOP_REPLACE);
  201. PIPE_ASSERT(PIPE_STENCIL_OP_INCR == STENCILOP_INCRSAT);
  202. PIPE_ASSERT(PIPE_STENCIL_OP_DECR == STENCILOP_DECRSAT);
  203. PIPE_ASSERT(PIPE_STENCIL_OP_INCR_WRAP == STENCILOP_INCR);
  204. PIPE_ASSERT(PIPE_STENCIL_OP_DECR_WRAP == STENCILOP_DECR);
  205. PIPE_ASSERT(PIPE_STENCIL_OP_INVERT == STENCILOP_INVERT);
  206. /* pipe_sprite_coord_mode happens to match 3DSTATE_SBE */
  207. PIPE_ASSERT(PIPE_SPRITE_COORD_UPPER_LEFT == UPPERLEFT);
  208. PIPE_ASSERT(PIPE_SPRITE_COORD_LOWER_LEFT == LOWERLEFT);
  209. #undef PIPE_ASSERT
  210. }
  211. static unsigned
  212. translate_prim_type(enum pipe_prim_type prim, uint8_t verts_per_patch)
  213. {
  214. static const unsigned map[] = {
  215. [PIPE_PRIM_POINTS] = _3DPRIM_POINTLIST,
  216. [PIPE_PRIM_LINES] = _3DPRIM_LINELIST,
  217. [PIPE_PRIM_LINE_LOOP] = _3DPRIM_LINELOOP,
  218. [PIPE_PRIM_LINE_STRIP] = _3DPRIM_LINESTRIP,
  219. [PIPE_PRIM_TRIANGLES] = _3DPRIM_TRILIST,
  220. [PIPE_PRIM_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
  221. [PIPE_PRIM_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
  222. [PIPE_PRIM_QUADS] = _3DPRIM_QUADLIST,
  223. [PIPE_PRIM_QUAD_STRIP] = _3DPRIM_QUADSTRIP,
  224. [PIPE_PRIM_POLYGON] = _3DPRIM_POLYGON,
  225. [PIPE_PRIM_LINES_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
  226. [PIPE_PRIM_LINE_STRIP_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
  227. [PIPE_PRIM_TRIANGLES_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
  228. [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
  229. [PIPE_PRIM_PATCHES] = _3DPRIM_PATCHLIST_1 - 1,
  230. };
  231. return map[prim] + (prim == PIPE_PRIM_PATCHES ? verts_per_patch : 0);
  232. }
  233. static unsigned
  234. translate_compare_func(enum pipe_compare_func pipe_func)
  235. {
  236. static const unsigned map[] = {
  237. [PIPE_FUNC_NEVER] = COMPAREFUNCTION_NEVER,
  238. [PIPE_FUNC_LESS] = COMPAREFUNCTION_LESS,
  239. [PIPE_FUNC_EQUAL] = COMPAREFUNCTION_EQUAL,
  240. [PIPE_FUNC_LEQUAL] = COMPAREFUNCTION_LEQUAL,
  241. [PIPE_FUNC_GREATER] = COMPAREFUNCTION_GREATER,
  242. [PIPE_FUNC_NOTEQUAL] = COMPAREFUNCTION_NOTEQUAL,
  243. [PIPE_FUNC_GEQUAL] = COMPAREFUNCTION_GEQUAL,
  244. [PIPE_FUNC_ALWAYS] = COMPAREFUNCTION_ALWAYS,
  245. };
  246. return map[pipe_func];
  247. }
  248. static unsigned
  249. translate_shadow_func(enum pipe_compare_func pipe_func)
  250. {
  251. /* Gallium specifies the result of shadow comparisons as:
  252. *
  253. * 1 if ref <op> texel,
  254. * 0 otherwise.
  255. *
  256. * The hardware does:
  257. *
  258. * 0 if texel <op> ref,
  259. * 1 otherwise.
  260. *
  261. * So we need to flip the operator and also negate.
  262. */
  263. static const unsigned map[] = {
  264. [PIPE_FUNC_NEVER] = PREFILTEROPALWAYS,
  265. [PIPE_FUNC_LESS] = PREFILTEROPLEQUAL,
  266. [PIPE_FUNC_EQUAL] = PREFILTEROPNOTEQUAL,
  267. [PIPE_FUNC_LEQUAL] = PREFILTEROPLESS,
  268. [PIPE_FUNC_GREATER] = PREFILTEROPGEQUAL,
  269. [PIPE_FUNC_NOTEQUAL] = PREFILTEROPEQUAL,
  270. [PIPE_FUNC_GEQUAL] = PREFILTEROPGREATER,
  271. [PIPE_FUNC_ALWAYS] = PREFILTEROPNEVER,
  272. };
  273. return map[pipe_func];
  274. }
  275. static unsigned
  276. translate_cull_mode(unsigned pipe_face)
  277. {
  278. static const unsigned map[4] = {
  279. [PIPE_FACE_NONE] = CULLMODE_NONE,
  280. [PIPE_FACE_FRONT] = CULLMODE_FRONT,
  281. [PIPE_FACE_BACK] = CULLMODE_BACK,
  282. [PIPE_FACE_FRONT_AND_BACK] = CULLMODE_BOTH,
  283. };
  284. return map[pipe_face];
  285. }
  286. static unsigned
  287. translate_fill_mode(unsigned pipe_polymode)
  288. {
  289. static const unsigned map[4] = {
  290. [PIPE_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
  291. [PIPE_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
  292. [PIPE_POLYGON_MODE_POINT] = FILL_MODE_POINT,
  293. [PIPE_POLYGON_MODE_FILL_RECTANGLE] = FILL_MODE_SOLID,
  294. };
  295. return map[pipe_polymode];
  296. }
  297. static unsigned
  298. translate_mip_filter(enum pipe_tex_mipfilter pipe_mip)
  299. {
  300. static const unsigned map[] = {
  301. [PIPE_TEX_MIPFILTER_NEAREST] = MIPFILTER_NEAREST,
  302. [PIPE_TEX_MIPFILTER_LINEAR] = MIPFILTER_LINEAR,
  303. [PIPE_TEX_MIPFILTER_NONE] = MIPFILTER_NONE,
  304. };
  305. return map[pipe_mip];
  306. }
  307. static uint32_t
  308. translate_wrap(unsigned pipe_wrap)
  309. {
  310. static const unsigned map[] = {
  311. [PIPE_TEX_WRAP_REPEAT] = TCM_WRAP,
  312. [PIPE_TEX_WRAP_CLAMP] = TCM_HALF_BORDER,
  313. [PIPE_TEX_WRAP_CLAMP_TO_EDGE] = TCM_CLAMP,
  314. [PIPE_TEX_WRAP_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
  315. [PIPE_TEX_WRAP_MIRROR_REPEAT] = TCM_MIRROR,
  316. [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
  317. /* These are unsupported. */
  318. [PIPE_TEX_WRAP_MIRROR_CLAMP] = -1,
  319. [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER] = -1,
  320. };
  321. return map[pipe_wrap];
  322. }
  323. static struct iris_address
  324. ro_bo(struct iris_bo *bo, uint64_t offset)
  325. {
  326. /* CSOs must pass NULL for bo! Otherwise it will add the BO to the
  327. * validation list at CSO creation time, instead of draw time.
  328. */
  329. return (struct iris_address) { .bo = bo, .offset = offset };
  330. }
  331. static struct iris_address
  332. rw_bo(struct iris_bo *bo, uint64_t offset)
  333. {
  334. /* CSOs must pass NULL for bo! Otherwise it will add the BO to the
  335. * validation list at CSO creation time, instead of draw time.
  336. */
  337. return (struct iris_address) { .bo = bo, .offset = offset, .write = true };
  338. }
  339. /**
  340. * Allocate space for some indirect state.
  341. *
  342. * Return a pointer to the map (to fill it out) and a state ref (for
  343. * referring to the state in GPU commands).
  344. */
  345. static void *
  346. upload_state(struct u_upload_mgr *uploader,
  347. struct iris_state_ref *ref,
  348. unsigned size,
  349. unsigned alignment)
  350. {
  351. void *p = NULL;
  352. u_upload_alloc(uploader, 0, size, alignment, &ref->offset, &ref->res, &p);
  353. return p;
  354. }
  355. /**
  356. * Stream out temporary/short-lived state.
  357. *
  358. * This allocates space, pins the BO, and includes the BO address in the
  359. * returned offset (which works because all state lives in 32-bit memory
  360. * zones).
  361. */
  362. static uint32_t *
  363. stream_state(struct iris_batch *batch,
  364. struct u_upload_mgr *uploader,
  365. struct pipe_resource **out_res,
  366. unsigned size,
  367. unsigned alignment,
  368. uint32_t *out_offset)
  369. {
  370. void *ptr = NULL;
  371. u_upload_alloc(uploader, 0, size, alignment, out_offset, out_res, &ptr);
  372. struct iris_bo *bo = iris_resource_bo(*out_res);
  373. iris_use_pinned_bo(batch, bo, false);
  374. *out_offset += iris_bo_offset_from_base_address(bo);
  375. return ptr;
  376. }
  377. /**
  378. * stream_state() + memcpy.
  379. */
  380. static uint32_t
  381. emit_state(struct iris_batch *batch,
  382. struct u_upload_mgr *uploader,
  383. struct pipe_resource **out_res,
  384. const void *data,
  385. unsigned size,
  386. unsigned alignment)
  387. {
  388. unsigned offset = 0;
  389. uint32_t *map =
  390. stream_state(batch, uploader, out_res, size, alignment, &offset);
  391. if (map)
  392. memcpy(map, data, size);
  393. return offset;
  394. }
  395. /**
  396. * Did field 'x' change between 'old_cso' and 'new_cso'?
  397. *
  398. * (If so, we may want to set some dirty flags.)
  399. */
  400. #define cso_changed(x) (!old_cso || (old_cso->x != new_cso->x))
  401. #define cso_changed_memcmp(x) \
  402. (!old_cso || memcmp(old_cso->x, new_cso->x, sizeof(old_cso->x)) != 0)
  403. static void
  404. flush_for_state_base_change(struct iris_batch *batch)
  405. {
  406. /* Flush before emitting STATE_BASE_ADDRESS.
  407. *
  408. * This isn't documented anywhere in the PRM. However, it seems to be
  409. * necessary prior to changing the surface state base adress. We've
  410. * seen issues in Vulkan where we get GPU hangs when using multi-level
  411. * command buffers which clear depth, reset state base address, and then
  412. * go render stuff.
  413. *
  414. * Normally, in GL, we would trust the kernel to do sufficient stalls
  415. * and flushes prior to executing our batch. However, it doesn't seem
  416. * as if the kernel's flushing is always sufficient and we don't want to
  417. * rely on it.
  418. *
  419. * We make this an end-of-pipe sync instead of a normal flush because we
  420. * do not know the current status of the GPU. On Haswell at least,
  421. * having a fast-clear operation in flight at the same time as a normal
  422. * rendering operation can cause hangs. Since the kernel's flushing is
  423. * insufficient, we need to ensure that any rendering operations from
  424. * other processes are definitely complete before we try to do our own
  425. * rendering. It's a bit of a big hammer but it appears to work.
  426. */
  427. iris_emit_end_of_pipe_sync(batch,
  428. PIPE_CONTROL_RENDER_TARGET_FLUSH |
  429. PIPE_CONTROL_DEPTH_CACHE_FLUSH |
  430. PIPE_CONTROL_DATA_CACHE_FLUSH);
  431. }
  432. static void
  433. _iris_emit_lri(struct iris_batch *batch, uint32_t reg, uint32_t val)
  434. {
  435. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
  436. lri.RegisterOffset = reg;
  437. lri.DataDWord = val;
  438. }
  439. }
  440. #define iris_emit_lri(b, r, v) _iris_emit_lri(b, GENX(r##_num), v)
  441. static void
  442. emit_pipeline_select(struct iris_batch *batch, uint32_t pipeline)
  443. {
  444. #if GEN_GEN >= 8 && GEN_GEN < 10
  445. /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
  446. *
  447. * Software must clear the COLOR_CALC_STATE Valid field in
  448. * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
  449. * with Pipeline Select set to GPGPU.
  450. *
  451. * The internal hardware docs recommend the same workaround for Gen9
  452. * hardware too.
  453. */
  454. if (pipeline == GPGPU)
  455. iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
  456. #endif
  457. /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
  458. * PIPELINE_SELECT [DevBWR+]":
  459. *
  460. * "Project: DEVSNB+
  461. *
  462. * Software must ensure all the write caches are flushed through a
  463. * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
  464. * command to invalidate read only caches prior to programming
  465. * MI_PIPELINE_SELECT command to change the Pipeline Select Mode."
  466. */
  467. iris_emit_pipe_control_flush(batch,
  468. PIPE_CONTROL_RENDER_TARGET_FLUSH |
  469. PIPE_CONTROL_DEPTH_CACHE_FLUSH |
  470. PIPE_CONTROL_DATA_CACHE_FLUSH |
  471. PIPE_CONTROL_CS_STALL);
  472. iris_emit_pipe_control_flush(batch,
  473. PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
  474. PIPE_CONTROL_CONST_CACHE_INVALIDATE |
  475. PIPE_CONTROL_STATE_CACHE_INVALIDATE |
  476. PIPE_CONTROL_INSTRUCTION_INVALIDATE);
  477. iris_emit_cmd(batch, GENX(PIPELINE_SELECT), sel) {
  478. #if GEN_GEN >= 9
  479. sel.MaskBits = 3;
  480. #endif
  481. sel.PipelineSelection = pipeline;
  482. }
  483. }
  484. UNUSED static void
  485. init_glk_barrier_mode(struct iris_batch *batch, uint32_t value)
  486. {
  487. #if GEN_GEN == 9
  488. /* Project: DevGLK
  489. *
  490. * "This chicken bit works around a hardware issue with barrier
  491. * logic encountered when switching between GPGPU and 3D pipelines.
  492. * To workaround the issue, this mode bit should be set after a
  493. * pipeline is selected."
  494. */
  495. uint32_t reg_val;
  496. iris_pack_state(GENX(SLICE_COMMON_ECO_CHICKEN1), &reg_val, reg) {
  497. reg.GLKBarrierMode = value;
  498. reg.GLKBarrierModeMask = 1;
  499. }
  500. iris_emit_lri(batch, SLICE_COMMON_ECO_CHICKEN1, reg_val);
  501. #endif
  502. }
  503. static void
  504. init_state_base_address(struct iris_batch *batch)
  505. {
  506. flush_for_state_base_change(batch);
  507. /* We program most base addresses once at context initialization time.
  508. * Each base address points at a 4GB memory zone, and never needs to
  509. * change. See iris_bufmgr.h for a description of the memory zones.
  510. *
  511. * The one exception is Surface State Base Address, which needs to be
  512. * updated occasionally. See iris_binder.c for the details there.
  513. */
  514. iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
  515. #if 0
  516. // XXX: MOCS is stupid for this.
  517. sba.GeneralStateMemoryObjectControlState = MOCS_WB;
  518. sba.StatelessDataPortAccessMemoryObjectControlState = MOCS_WB;
  519. sba.DynamicStateMemoryObjectControlState = MOCS_WB;
  520. sba.IndirectObjectMemoryObjectControlState = MOCS_WB;
  521. sba.InstructionMemoryObjectControlState = MOCS_WB;
  522. sba.BindlessSurfaceStateMemoryObjectControlState = MOCS_WB;
  523. #endif
  524. sba.GeneralStateBaseAddressModifyEnable = true;
  525. sba.DynamicStateBaseAddressModifyEnable = true;
  526. sba.IndirectObjectBaseAddressModifyEnable = true;
  527. sba.InstructionBaseAddressModifyEnable = true;
  528. sba.GeneralStateBufferSizeModifyEnable = true;
  529. sba.DynamicStateBufferSizeModifyEnable = true;
  530. sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
  531. sba.IndirectObjectBufferSizeModifyEnable = true;
  532. sba.InstructionBuffersizeModifyEnable = true;
  533. sba.InstructionBaseAddress = ro_bo(NULL, IRIS_MEMZONE_SHADER_START);
  534. sba.DynamicStateBaseAddress = ro_bo(NULL, IRIS_MEMZONE_DYNAMIC_START);
  535. sba.GeneralStateBufferSize = 0xfffff;
  536. sba.IndirectObjectBufferSize = 0xfffff;
  537. sba.InstructionBufferSize = 0xfffff;
  538. sba.DynamicStateBufferSize = 0xfffff;
  539. }
  540. }
  541. /**
  542. * Upload the initial GPU state for a render context.
  543. *
  544. * This sets some invariant state that needs to be programmed a particular
  545. * way, but we never actually change.
  546. */
  547. static void
  548. iris_init_render_context(struct iris_screen *screen,
  549. struct iris_batch *batch,
  550. struct iris_vtable *vtbl,
  551. struct pipe_debug_callback *dbg)
  552. {
  553. UNUSED const struct gen_device_info *devinfo = &screen->devinfo;
  554. uint32_t reg_val;
  555. emit_pipeline_select(batch, _3D);
  556. init_state_base_address(batch);
  557. // XXX: INSTPM on Gen8
  558. iris_pack_state(GENX(CS_DEBUG_MODE2), &reg_val, reg) {
  559. reg.CONSTANT_BUFFERAddressOffsetDisable = true;
  560. reg.CONSTANT_BUFFERAddressOffsetDisableMask = true;
  561. }
  562. iris_emit_lri(batch, CS_DEBUG_MODE2, reg_val);
  563. #if GEN_GEN == 9
  564. iris_pack_state(GENX(CACHE_MODE_1), &reg_val, reg) {
  565. reg.FloatBlendOptimizationEnable = true;
  566. reg.FloatBlendOptimizationEnableMask = true;
  567. reg.PartialResolveDisableInVC = true;
  568. reg.PartialResolveDisableInVCMask = true;
  569. }
  570. iris_emit_lri(batch, CACHE_MODE_1, reg_val);
  571. if (devinfo->is_geminilake)
  572. init_glk_barrier_mode(batch, GLK_BARRIER_MODE_3D_HULL);
  573. #endif
  574. #if GEN_GEN == 11
  575. iris_pack_state(GENX(SAMPLER_MODE), &reg_val, reg) {
  576. reg.HeaderlessMessageforPreemptableContexts = 1;
  577. reg.HeaderlessMessageforPreemptableContextsMask = 1;
  578. }
  579. iris_emit_lri(batch, SAMPLER_MODE, reg_val);
  580. // XXX: 3D_MODE?
  581. #endif
  582. /* 3DSTATE_DRAWING_RECTANGLE is non-pipelined, so we want to avoid
  583. * changing it dynamically. We set it to the maximum size here, and
  584. * instead include the render target dimensions in the viewport, so
  585. * viewport extents clipping takes care of pruning stray geometry.
  586. */
  587. iris_emit_cmd(batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
  588. rect.ClippedDrawingRectangleXMax = UINT16_MAX;
  589. rect.ClippedDrawingRectangleYMax = UINT16_MAX;
  590. }
  591. /* Set the initial MSAA sample positions. */
  592. iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_PATTERN), pat) {
  593. GEN_SAMPLE_POS_1X(pat._1xSample);
  594. GEN_SAMPLE_POS_2X(pat._2xSample);
  595. GEN_SAMPLE_POS_4X(pat._4xSample);
  596. GEN_SAMPLE_POS_8X(pat._8xSample);
  597. GEN_SAMPLE_POS_16X(pat._16xSample);
  598. }
  599. /* Use the legacy AA line coverage computation. */
  600. iris_emit_cmd(batch, GENX(3DSTATE_AA_LINE_PARAMETERS), foo);
  601. /* Disable chromakeying (it's for media) */
  602. iris_emit_cmd(batch, GENX(3DSTATE_WM_CHROMAKEY), foo);
  603. /* We want regular rendering, not special HiZ operations. */
  604. iris_emit_cmd(batch, GENX(3DSTATE_WM_HZ_OP), foo);
  605. /* No polygon stippling offsets are necessary. */
  606. // XXX: may need to set an offset for origin-UL framebuffers
  607. iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_OFFSET), foo);
  608. /* Set a static partitioning of the push constant area. */
  609. // XXX: this may be a bad idea...could starve the push ringbuffers...
  610. for (int i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
  611. iris_emit_cmd(batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
  612. alloc._3DCommandSubOpcode = 18 + i;
  613. alloc.ConstantBufferOffset = 6 * i;
  614. alloc.ConstantBufferSize = i == MESA_SHADER_FRAGMENT ? 8 : 6;
  615. }
  616. }
  617. }
  618. static void
  619. iris_init_compute_context(struct iris_screen *screen,
  620. struct iris_batch *batch,
  621. struct iris_vtable *vtbl,
  622. struct pipe_debug_callback *dbg)
  623. {
  624. UNUSED const struct gen_device_info *devinfo = &screen->devinfo;
  625. emit_pipeline_select(batch, GPGPU);
  626. init_state_base_address(batch);
  627. #if GEN_GEN == 9
  628. if (devinfo->is_geminilake)
  629. init_glk_barrier_mode(batch, GLK_BARRIER_MODE_GPGPU);
  630. #endif
  631. }
  632. struct iris_vertex_buffer_state {
  633. /** The 3DSTATE_VERTEX_BUFFERS hardware packet. */
  634. uint32_t vertex_buffers[1 + 33 * GENX(VERTEX_BUFFER_STATE_length)];
  635. /** The resource to source vertex data from. */
  636. struct pipe_resource *resources[33];
  637. /** The number of bound vertex buffers. */
  638. unsigned num_buffers;
  639. };
  640. struct iris_depth_buffer_state {
  641. /* Depth/HiZ/Stencil related hardware packets. */
  642. uint32_t packets[GENX(3DSTATE_DEPTH_BUFFER_length) +
  643. GENX(3DSTATE_STENCIL_BUFFER_length) +
  644. GENX(3DSTATE_HIER_DEPTH_BUFFER_length) +
  645. GENX(3DSTATE_CLEAR_PARAMS_length)];
  646. };
  647. /**
  648. * Generation-specific context state (ice->state.genx->...).
  649. *
  650. * Most state can go in iris_context directly, but these encode hardware
  651. * packets which vary by generation.
  652. */
  653. struct iris_genx_state {
  654. /** SF_CLIP_VIEWPORT */
  655. uint32_t sf_cl_vp[GENX(SF_CLIP_VIEWPORT_length) * IRIS_MAX_VIEWPORTS];
  656. struct iris_vertex_buffer_state vertex_buffers;
  657. struct iris_depth_buffer_state depth_buffer;
  658. uint32_t so_buffers[4 * GENX(3DSTATE_SO_BUFFER_length)];
  659. uint32_t streamout[4 * GENX(3DSTATE_STREAMOUT_length)];
  660. };
  661. /**
  662. * The pipe->set_blend_color() driver hook.
  663. *
  664. * This corresponds to our COLOR_CALC_STATE.
  665. */
  666. static void
  667. iris_set_blend_color(struct pipe_context *ctx,
  668. const struct pipe_blend_color *state)
  669. {
  670. struct iris_context *ice = (struct iris_context *) ctx;
  671. /* Our COLOR_CALC_STATE is exactly pipe_blend_color, so just memcpy */
  672. memcpy(&ice->state.blend_color, state, sizeof(struct pipe_blend_color));
  673. ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
  674. }
  675. /**
  676. * Gallium CSO for blend state (see pipe_blend_state).
  677. */
  678. struct iris_blend_state {
  679. /** Partial 3DSTATE_PS_BLEND */
  680. uint32_t ps_blend[GENX(3DSTATE_PS_BLEND_length)];
  681. /** Partial BLEND_STATE */
  682. uint32_t blend_state[GENX(BLEND_STATE_length) +
  683. BRW_MAX_DRAW_BUFFERS * GENX(BLEND_STATE_ENTRY_length)];
  684. bool alpha_to_coverage; /* for shader key */
  685. };
  686. /**
  687. * The pipe->create_blend_state() driver hook.
  688. *
  689. * Translates a pipe_blend_state into iris_blend_state.
  690. */
  691. static void *
  692. iris_create_blend_state(struct pipe_context *ctx,
  693. const struct pipe_blend_state *state)
  694. {
  695. struct iris_blend_state *cso = malloc(sizeof(struct iris_blend_state));
  696. uint32_t *blend_state = cso->blend_state;
  697. cso->alpha_to_coverage = state->alpha_to_coverage;
  698. iris_pack_command(GENX(3DSTATE_PS_BLEND), cso->ps_blend, pb) {
  699. /* pb.HasWriteableRT is filled in at draw time. */
  700. /* pb.AlphaTestEnable is filled in at draw time. */
  701. pb.AlphaToCoverageEnable = state->alpha_to_coverage;
  702. pb.IndependentAlphaBlendEnable = state->independent_blend_enable;
  703. pb.ColorBufferBlendEnable = state->rt[0].blend_enable;
  704. pb.SourceBlendFactor = state->rt[0].rgb_src_factor;
  705. pb.SourceAlphaBlendFactor = state->rt[0].alpha_func;
  706. pb.DestinationBlendFactor = state->rt[0].rgb_dst_factor;
  707. pb.DestinationAlphaBlendFactor = state->rt[0].alpha_dst_factor;
  708. }
  709. iris_pack_state(GENX(BLEND_STATE), blend_state, bs) {
  710. bs.AlphaToCoverageEnable = state->alpha_to_coverage;
  711. bs.IndependentAlphaBlendEnable = state->independent_blend_enable;
  712. bs.AlphaToOneEnable = state->alpha_to_one;
  713. bs.AlphaToCoverageDitherEnable = state->alpha_to_coverage;
  714. bs.ColorDitherEnable = state->dither;
  715. /* bl.AlphaTestEnable and bs.AlphaTestFunction are filled in later. */
  716. }
  717. blend_state += GENX(BLEND_STATE_length);
  718. for (int i = 0; i < BRW_MAX_DRAW_BUFFERS; i++) {
  719. const struct pipe_rt_blend_state *rt =
  720. &state->rt[state->independent_blend_enable ? i : 0];
  721. iris_pack_state(GENX(BLEND_STATE_ENTRY), blend_state, be) {
  722. be.LogicOpEnable = state->logicop_enable;
  723. be.LogicOpFunction = state->logicop_func;
  724. be.PreBlendSourceOnlyClampEnable = false;
  725. be.ColorClampRange = COLORCLAMP_RTFORMAT;
  726. be.PreBlendColorClampEnable = true;
  727. be.PostBlendColorClampEnable = true;
  728. be.ColorBufferBlendEnable = rt->blend_enable;
  729. be.ColorBlendFunction = rt->rgb_func;
  730. be.AlphaBlendFunction = rt->alpha_func;
  731. be.SourceBlendFactor = rt->rgb_src_factor;
  732. be.SourceAlphaBlendFactor = rt->alpha_func;
  733. be.DestinationBlendFactor = rt->rgb_dst_factor;
  734. be.DestinationAlphaBlendFactor = rt->alpha_dst_factor;
  735. be.WriteDisableRed = !(rt->colormask & PIPE_MASK_R);
  736. be.WriteDisableGreen = !(rt->colormask & PIPE_MASK_G);
  737. be.WriteDisableBlue = !(rt->colormask & PIPE_MASK_B);
  738. be.WriteDisableAlpha = !(rt->colormask & PIPE_MASK_A);
  739. }
  740. blend_state += GENX(BLEND_STATE_ENTRY_length);
  741. }
  742. return cso;
  743. }
  744. /**
  745. * The pipe->bind_blend_state() driver hook.
  746. *
  747. * Bind a blending CSO and flag related dirty bits.
  748. */
  749. static void
  750. iris_bind_blend_state(struct pipe_context *ctx, void *state)
  751. {
  752. struct iris_context *ice = (struct iris_context *) ctx;
  753. ice->state.cso_blend = state;
  754. ice->state.dirty |= IRIS_DIRTY_PS_BLEND;
  755. ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
  756. ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_BLEND];
  757. }
  758. /**
  759. * Gallium CSO for depth, stencil, and alpha testing state.
  760. */
  761. struct iris_depth_stencil_alpha_state {
  762. /** Partial 3DSTATE_WM_DEPTH_STENCIL. */
  763. uint32_t wmds[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
  764. /** Outbound to BLEND_STATE, 3DSTATE_PS_BLEND, COLOR_CALC_STATE. */
  765. struct pipe_alpha_state alpha;
  766. /** Outbound to resolve and cache set tracking. */
  767. bool depth_writes_enabled;
  768. bool stencil_writes_enabled;
  769. };
  770. /**
  771. * The pipe->create_depth_stencil_alpha_state() driver hook.
  772. *
  773. * We encode most of 3DSTATE_WM_DEPTH_STENCIL, and just save off the alpha
  774. * testing state since we need pieces of it in a variety of places.
  775. */
  776. static void *
  777. iris_create_zsa_state(struct pipe_context *ctx,
  778. const struct pipe_depth_stencil_alpha_state *state)
  779. {
  780. struct iris_depth_stencil_alpha_state *cso =
  781. malloc(sizeof(struct iris_depth_stencil_alpha_state));
  782. bool two_sided_stencil = state->stencil[1].enabled;
  783. cso->alpha = state->alpha;
  784. cso->depth_writes_enabled = state->depth.writemask;
  785. cso->stencil_writes_enabled =
  786. state->stencil[0].writemask != 0 ||
  787. (two_sided_stencil && state->stencil[1].writemask != 1);
  788. /* The state tracker needs to optimize away EQUAL writes for us. */
  789. assert(!(state->depth.func == PIPE_FUNC_EQUAL && state->depth.writemask));
  790. iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), cso->wmds, wmds) {
  791. wmds.StencilFailOp = state->stencil[0].fail_op;
  792. wmds.StencilPassDepthFailOp = state->stencil[0].zfail_op;
  793. wmds.StencilPassDepthPassOp = state->stencil[0].zpass_op;
  794. wmds.StencilTestFunction =
  795. translate_compare_func(state->stencil[0].func);
  796. wmds.BackfaceStencilFailOp = state->stencil[1].fail_op;
  797. wmds.BackfaceStencilPassDepthFailOp = state->stencil[1].zfail_op;
  798. wmds.BackfaceStencilPassDepthPassOp = state->stencil[1].zpass_op;
  799. wmds.BackfaceStencilTestFunction =
  800. translate_compare_func(state->stencil[1].func);
  801. wmds.DepthTestFunction = translate_compare_func(state->depth.func);
  802. wmds.DoubleSidedStencilEnable = two_sided_stencil;
  803. wmds.StencilTestEnable = state->stencil[0].enabled;
  804. wmds.StencilBufferWriteEnable =
  805. state->stencil[0].writemask != 0 ||
  806. (two_sided_stencil && state->stencil[1].writemask != 0);
  807. wmds.DepthTestEnable = state->depth.enabled;
  808. wmds.DepthBufferWriteEnable = state->depth.writemask;
  809. wmds.StencilTestMask = state->stencil[0].valuemask;
  810. wmds.StencilWriteMask = state->stencil[0].writemask;
  811. wmds.BackfaceStencilTestMask = state->stencil[1].valuemask;
  812. wmds.BackfaceStencilWriteMask = state->stencil[1].writemask;
  813. /* wmds.[Backface]StencilReferenceValue are merged later */
  814. }
  815. return cso;
  816. }
  817. /**
  818. * The pipe->bind_depth_stencil_alpha_state() driver hook.
  819. *
  820. * Bind a depth/stencil/alpha CSO and flag related dirty bits.
  821. */
  822. static void
  823. iris_bind_zsa_state(struct pipe_context *ctx, void *state)
  824. {
  825. struct iris_context *ice = (struct iris_context *) ctx;
  826. struct iris_depth_stencil_alpha_state *old_cso = ice->state.cso_zsa;
  827. struct iris_depth_stencil_alpha_state *new_cso = state;
  828. if (new_cso) {
  829. if (cso_changed(alpha.ref_value))
  830. ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
  831. if (cso_changed(alpha.enabled))
  832. ice->state.dirty |= IRIS_DIRTY_PS_BLEND | IRIS_DIRTY_BLEND_STATE;
  833. if (cso_changed(alpha.func))
  834. ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
  835. ice->state.depth_writes_enabled = new_cso->depth_writes_enabled;
  836. ice->state.stencil_writes_enabled = new_cso->stencil_writes_enabled;
  837. }
  838. ice->state.cso_zsa = new_cso;
  839. ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
  840. ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
  841. ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_DEPTH_STENCIL_ALPHA];
  842. }
  843. /**
  844. * Gallium CSO for rasterizer state.
  845. */
  846. struct iris_rasterizer_state {
  847. uint32_t sf[GENX(3DSTATE_SF_length)];
  848. uint32_t clip[GENX(3DSTATE_CLIP_length)];
  849. uint32_t raster[GENX(3DSTATE_RASTER_length)];
  850. uint32_t wm[GENX(3DSTATE_WM_length)];
  851. uint32_t line_stipple[GENX(3DSTATE_LINE_STIPPLE_length)];
  852. bool clip_halfz; /* for CC_VIEWPORT */
  853. bool depth_clip_near; /* for CC_VIEWPORT */
  854. bool depth_clip_far; /* for CC_VIEWPORT */
  855. bool flatshade; /* for shader state */
  856. bool flatshade_first; /* for stream output */
  857. bool clamp_fragment_color; /* for shader state */
  858. bool light_twoside; /* for shader state */
  859. bool rasterizer_discard; /* for 3DSTATE_STREAMOUT */
  860. bool half_pixel_center; /* for 3DSTATE_MULTISAMPLE */
  861. bool line_stipple_enable;
  862. bool poly_stipple_enable;
  863. bool multisample;
  864. bool force_persample_interp;
  865. enum pipe_sprite_coord_mode sprite_coord_mode; /* PIPE_SPRITE_* */
  866. uint16_t sprite_coord_enable;
  867. };
  868. static float
  869. get_line_width(const struct pipe_rasterizer_state *state)
  870. {
  871. float line_width = state->line_width;
  872. /* From the OpenGL 4.4 spec:
  873. *
  874. * "The actual width of non-antialiased lines is determined by rounding
  875. * the supplied width to the nearest integer, then clamping it to the
  876. * implementation-dependent maximum non-antialiased line width."
  877. */
  878. if (!state->multisample && !state->line_smooth)
  879. line_width = roundf(state->line_width);
  880. if (!state->multisample && state->line_smooth && line_width < 1.5f) {
  881. /* For 1 pixel line thickness or less, the general anti-aliasing
  882. * algorithm gives up, and a garbage line is generated. Setting a
  883. * Line Width of 0.0 specifies the rasterization of the "thinnest"
  884. * (one-pixel-wide), non-antialiased lines.
  885. *
  886. * Lines rendered with zero Line Width are rasterized using the
  887. * "Grid Intersection Quantization" rules as specified by the
  888. * "Zero-Width (Cosmetic) Line Rasterization" section of the docs.
  889. */
  890. line_width = 0.0f;
  891. }
  892. return line_width;
  893. }
  894. /**
  895. * The pipe->create_rasterizer_state() driver hook.
  896. */
  897. static void *
  898. iris_create_rasterizer_state(struct pipe_context *ctx,
  899. const struct pipe_rasterizer_state *state)
  900. {
  901. struct iris_rasterizer_state *cso =
  902. malloc(sizeof(struct iris_rasterizer_state));
  903. #if 0
  904. point_quad_rasterization -> SBE?
  905. not necessary?
  906. {
  907. poly_smooth
  908. force_persample_interp - ?
  909. bottom_edge_rule
  910. offset_units_unscaled - cap not exposed
  911. }
  912. #endif
  913. // XXX: it may make more sense just to store the pipe_rasterizer_state,
  914. // we're copying a lot of booleans here. But we don't need all of them...
  915. cso->multisample = state->multisample;
  916. cso->force_persample_interp = state->force_persample_interp;
  917. cso->clip_halfz = state->clip_halfz;
  918. cso->depth_clip_near = state->depth_clip_near;
  919. cso->depth_clip_far = state->depth_clip_far;
  920. cso->flatshade = state->flatshade;
  921. cso->flatshade_first = state->flatshade_first;
  922. cso->clamp_fragment_color = state->clamp_fragment_color;
  923. cso->light_twoside = state->light_twoside;
  924. cso->rasterizer_discard = state->rasterizer_discard;
  925. cso->half_pixel_center = state->half_pixel_center;
  926. cso->sprite_coord_mode = state->sprite_coord_mode;
  927. cso->sprite_coord_enable = state->sprite_coord_enable;
  928. cso->line_stipple_enable = state->line_stipple_enable;
  929. cso->poly_stipple_enable = state->poly_stipple_enable;
  930. float line_width = get_line_width(state);
  931. iris_pack_command(GENX(3DSTATE_SF), cso->sf, sf) {
  932. sf.StatisticsEnable = true;
  933. sf.ViewportTransformEnable = true;
  934. sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
  935. sf.LineEndCapAntialiasingRegionWidth =
  936. state->line_smooth ? _10pixels : _05pixels;
  937. sf.LastPixelEnable = state->line_last_pixel;
  938. sf.LineWidth = line_width;
  939. sf.SmoothPointEnable = state->point_smooth || state->multisample;
  940. sf.PointWidthSource = state->point_size_per_vertex ? Vertex : State;
  941. sf.PointWidth = state->point_size;
  942. if (state->flatshade_first) {
  943. sf.TriangleFanProvokingVertexSelect = 1;
  944. } else {
  945. sf.TriangleStripListProvokingVertexSelect = 2;
  946. sf.TriangleFanProvokingVertexSelect = 2;
  947. sf.LineStripListProvokingVertexSelect = 1;
  948. }
  949. }
  950. iris_pack_command(GENX(3DSTATE_RASTER), cso->raster, rr) {
  951. rr.FrontWinding = state->front_ccw ? CounterClockwise : Clockwise;
  952. rr.CullMode = translate_cull_mode(state->cull_face);
  953. rr.FrontFaceFillMode = translate_fill_mode(state->fill_front);
  954. rr.BackFaceFillMode = translate_fill_mode(state->fill_back);
  955. rr.DXMultisampleRasterizationEnable = state->multisample;
  956. rr.GlobalDepthOffsetEnableSolid = state->offset_tri;
  957. rr.GlobalDepthOffsetEnableWireframe = state->offset_line;
  958. rr.GlobalDepthOffsetEnablePoint = state->offset_point;
  959. rr.GlobalDepthOffsetConstant = state->offset_units * 2;
  960. rr.GlobalDepthOffsetScale = state->offset_scale;
  961. rr.GlobalDepthOffsetClamp = state->offset_clamp;
  962. rr.SmoothPointEnable = state->point_smooth || state->multisample;
  963. rr.AntialiasingEnable = state->line_smooth;
  964. rr.ScissorRectangleEnable = state->scissor;
  965. rr.ViewportZNearClipTestEnable = state->depth_clip_near;
  966. rr.ViewportZFarClipTestEnable = state->depth_clip_far;
  967. //rr.ConservativeRasterizationEnable = not yet supported by Gallium...
  968. }
  969. iris_pack_command(GENX(3DSTATE_CLIP), cso->clip, cl) {
  970. /* cl.NonPerspectiveBarycentricEnable is filled in at draw time from
  971. * the FS program; cl.ForceZeroRTAIndexEnable is filled in from the FB.
  972. */
  973. cl.StatisticsEnable = true;
  974. cl.EarlyCullEnable = true;
  975. cl.UserClipDistanceClipTestEnableBitmask = state->clip_plane_enable;
  976. cl.ForceUserClipDistanceClipTestEnableBitmask = true;
  977. cl.APIMode = state->clip_halfz ? APIMODE_D3D : APIMODE_OGL;
  978. cl.GuardbandClipTestEnable = true;
  979. cl.ClipMode = CLIPMODE_NORMAL;
  980. cl.ClipEnable = true;
  981. cl.ViewportXYClipTestEnable = state->point_tri_clip;
  982. cl.MinimumPointWidth = 0.125;
  983. cl.MaximumPointWidth = 255.875;
  984. if (state->flatshade_first) {
  985. cl.TriangleFanProvokingVertexSelect = 1;
  986. } else {
  987. cl.TriangleStripListProvokingVertexSelect = 2;
  988. cl.TriangleFanProvokingVertexSelect = 2;
  989. cl.LineStripListProvokingVertexSelect = 1;
  990. }
  991. }
  992. iris_pack_command(GENX(3DSTATE_WM), cso->wm, wm) {
  993. /* wm.BarycentricInterpolationMode and wm.EarlyDepthStencilControl are
  994. * filled in at draw time from the FS program.
  995. */
  996. wm.LineAntialiasingRegionWidth = _10pixels;
  997. wm.LineEndCapAntialiasingRegionWidth = _05pixels;
  998. wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
  999. wm.LineStippleEnable = state->line_stipple_enable;
  1000. wm.PolygonStippleEnable = state->poly_stipple_enable;
  1001. }
  1002. /* Remap from 0..255 back to 1..256 */
  1003. const unsigned line_stipple_factor = state->line_stipple_factor + 1;
  1004. iris_pack_command(GENX(3DSTATE_LINE_STIPPLE), cso->line_stipple, line) {
  1005. line.LineStipplePattern = state->line_stipple_pattern;
  1006. line.LineStippleInverseRepeatCount = 1.0f / line_stipple_factor;
  1007. line.LineStippleRepeatCount = line_stipple_factor;
  1008. }
  1009. return cso;
  1010. }
  1011. /**
  1012. * The pipe->bind_rasterizer_state() driver hook.
  1013. *
  1014. * Bind a rasterizer CSO and flag related dirty bits.
  1015. */
  1016. static void
  1017. iris_bind_rasterizer_state(struct pipe_context *ctx, void *state)
  1018. {
  1019. struct iris_context *ice = (struct iris_context *) ctx;
  1020. struct iris_rasterizer_state *old_cso = ice->state.cso_rast;
  1021. struct iris_rasterizer_state *new_cso = state;
  1022. if (new_cso) {
  1023. /* Try to avoid re-emitting 3DSTATE_LINE_STIPPLE, it's non-pipelined */
  1024. if (cso_changed_memcmp(line_stipple))
  1025. ice->state.dirty |= IRIS_DIRTY_LINE_STIPPLE;
  1026. if (cso_changed(half_pixel_center))
  1027. ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
  1028. if (cso_changed(line_stipple_enable) || cso_changed(poly_stipple_enable))
  1029. ice->state.dirty |= IRIS_DIRTY_WM;
  1030. if (cso_changed(rasterizer_discard) || cso_changed(flatshade_first))
  1031. ice->state.dirty |= IRIS_DIRTY_STREAMOUT;
  1032. if (cso_changed(depth_clip_near) || cso_changed(depth_clip_far) ||
  1033. cso_changed(clip_halfz))
  1034. ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
  1035. if (cso_changed(sprite_coord_enable) || cso_changed(light_twoside))
  1036. ice->state.dirty |= IRIS_DIRTY_SBE;
  1037. }
  1038. ice->state.cso_rast = new_cso;
  1039. ice->state.dirty |= IRIS_DIRTY_RASTER;
  1040. ice->state.dirty |= IRIS_DIRTY_CLIP;
  1041. ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_RASTERIZER];
  1042. }
  1043. /**
  1044. * Return true if the given wrap mode requires the border color to exist.
  1045. *
  1046. * (We can skip uploading it if the sampler isn't going to use it.)
  1047. */
  1048. static bool
  1049. wrap_mode_needs_border_color(unsigned wrap_mode)
  1050. {
  1051. return wrap_mode == TCM_CLAMP_BORDER || wrap_mode == TCM_HALF_BORDER;
  1052. }
  1053. /**
  1054. * Gallium CSO for sampler state.
  1055. */
  1056. struct iris_sampler_state {
  1057. union pipe_color_union border_color;
  1058. bool needs_border_color;
  1059. uint32_t sampler_state[GENX(SAMPLER_STATE_length)];
  1060. };
  1061. /**
  1062. * The pipe->create_sampler_state() driver hook.
  1063. *
  1064. * We fill out SAMPLER_STATE (except for the border color pointer), and
  1065. * store that on the CPU. It doesn't make sense to upload it to a GPU
  1066. * buffer object yet, because 3DSTATE_SAMPLER_STATE_POINTERS requires
  1067. * all bound sampler states to be in contiguous memor.
  1068. */
  1069. static void *
  1070. iris_create_sampler_state(struct pipe_context *ctx,
  1071. const struct pipe_sampler_state *state)
  1072. {
  1073. struct iris_sampler_state *cso = CALLOC_STRUCT(iris_sampler_state);
  1074. if (!cso)
  1075. return NULL;
  1076. STATIC_ASSERT(PIPE_TEX_FILTER_NEAREST == MAPFILTER_NEAREST);
  1077. STATIC_ASSERT(PIPE_TEX_FILTER_LINEAR == MAPFILTER_LINEAR);
  1078. unsigned wrap_s = translate_wrap(state->wrap_s);
  1079. unsigned wrap_t = translate_wrap(state->wrap_t);
  1080. unsigned wrap_r = translate_wrap(state->wrap_r);
  1081. memcpy(&cso->border_color, &state->border_color, sizeof(cso->border_color));
  1082. cso->needs_border_color = wrap_mode_needs_border_color(wrap_s) ||
  1083. wrap_mode_needs_border_color(wrap_t) ||
  1084. wrap_mode_needs_border_color(wrap_r);
  1085. float min_lod = state->min_lod;
  1086. unsigned mag_img_filter = state->mag_img_filter;
  1087. // XXX: explain this code ported from ilo...I don't get it at all...
  1088. if (state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE &&
  1089. state->min_lod > 0.0f) {
  1090. min_lod = 0.0f;
  1091. mag_img_filter = state->min_img_filter;
  1092. }
  1093. iris_pack_state(GENX(SAMPLER_STATE), cso->sampler_state, samp) {
  1094. samp.TCXAddressControlMode = wrap_s;
  1095. samp.TCYAddressControlMode = wrap_t;
  1096. samp.TCZAddressControlMode = wrap_r;
  1097. samp.CubeSurfaceControlMode = state->seamless_cube_map;
  1098. samp.NonnormalizedCoordinateEnable = !state->normalized_coords;
  1099. samp.MinModeFilter = state->min_img_filter;
  1100. samp.MagModeFilter = mag_img_filter;
  1101. samp.MipModeFilter = translate_mip_filter(state->min_mip_filter);
  1102. samp.MaximumAnisotropy = RATIO21;
  1103. if (state->max_anisotropy >= 2) {
  1104. if (state->min_img_filter == PIPE_TEX_FILTER_LINEAR) {
  1105. samp.MinModeFilter = MAPFILTER_ANISOTROPIC;
  1106. samp.AnisotropicAlgorithm = EWAApproximation;
  1107. }
  1108. if (state->mag_img_filter == PIPE_TEX_FILTER_LINEAR)
  1109. samp.MagModeFilter = MAPFILTER_ANISOTROPIC;
  1110. samp.MaximumAnisotropy =
  1111. MIN2((state->max_anisotropy - 2) / 2, RATIO161);
  1112. }
  1113. /* Set address rounding bits if not using nearest filtering. */
  1114. if (state->min_img_filter != PIPE_TEX_FILTER_NEAREST) {
  1115. samp.UAddressMinFilterRoundingEnable = true;
  1116. samp.VAddressMinFilterRoundingEnable = true;
  1117. samp.RAddressMinFilterRoundingEnable = true;
  1118. }
  1119. if (state->mag_img_filter != PIPE_TEX_FILTER_NEAREST) {
  1120. samp.UAddressMagFilterRoundingEnable = true;
  1121. samp.VAddressMagFilterRoundingEnable = true;
  1122. samp.RAddressMagFilterRoundingEnable = true;
  1123. }
  1124. if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
  1125. samp.ShadowFunction = translate_shadow_func(state->compare_func);
  1126. const float hw_max_lod = GEN_GEN >= 7 ? 14 : 13;
  1127. samp.LODPreClampMode = CLAMP_MODE_OGL;
  1128. samp.MinLOD = CLAMP(min_lod, 0, hw_max_lod);
  1129. samp.MaxLOD = CLAMP(state->max_lod, 0, hw_max_lod);
  1130. samp.TextureLODBias = CLAMP(state->lod_bias, -16, 15);
  1131. /* .BorderColorPointer is filled in by iris_bind_sampler_states. */
  1132. }
  1133. return cso;
  1134. }
  1135. /**
  1136. * The pipe->bind_sampler_states() driver hook.
  1137. *
  1138. * Now that we know all the sampler states, we upload them all into a
  1139. * contiguous area of GPU memory, for 3DSTATE_SAMPLER_STATE_POINTERS_*.
  1140. * We also fill out the border color state pointers at this point.
  1141. *
  1142. * We could defer this work to draw time, but we assume that binding
  1143. * will be less frequent than drawing.
  1144. */
  1145. // XXX: this may be a bad idea, need to make sure that st/mesa calls us
  1146. // XXX: with the complete set of shaders. If it makes multiple calls to
  1147. // XXX: things one at a time, we could waste a lot of time assembling things.
  1148. // XXX: it doesn't even BUY us anything to do it here, because we only flag
  1149. // XXX: IRIS_DIRTY_SAMPLER_STATE when this is called...
  1150. static void
  1151. iris_bind_sampler_states(struct pipe_context *ctx,
  1152. enum pipe_shader_type p_stage,
  1153. unsigned start, unsigned count,
  1154. void **states)
  1155. {
  1156. struct iris_context *ice = (struct iris_context *) ctx;
  1157. gl_shader_stage stage = stage_from_pipe(p_stage);
  1158. struct iris_shader_state *shs = &ice->state.shaders[stage];
  1159. assert(start + count <= IRIS_MAX_TEXTURE_SAMPLERS);
  1160. shs->num_samplers = MAX2(shs->num_samplers, start + count);
  1161. for (int i = 0; i < count; i++) {
  1162. shs->samplers[start + i] = states[i];
  1163. }
  1164. /* Assemble the SAMPLER_STATEs into a contiguous table that lives
  1165. * in the dynamic state memory zone, so we can point to it via the
  1166. * 3DSTATE_SAMPLER_STATE_POINTERS_* commands.
  1167. */
  1168. uint32_t *map =
  1169. upload_state(ice->state.dynamic_uploader, &shs->sampler_table,
  1170. count * 4 * GENX(SAMPLER_STATE_length), 32);
  1171. if (unlikely(!map))
  1172. return;
  1173. struct pipe_resource *res = shs->sampler_table.res;
  1174. shs->sampler_table.offset +=
  1175. iris_bo_offset_from_base_address(iris_resource_bo(res));
  1176. /* Make sure all land in the same BO */
  1177. iris_border_color_pool_reserve(ice, IRIS_MAX_TEXTURE_SAMPLERS);
  1178. for (int i = 0; i < count; i++) {
  1179. struct iris_sampler_state *state = shs->samplers[i];
  1180. if (!state) {
  1181. memset(map, 0, 4 * GENX(SAMPLER_STATE_length));
  1182. } else if (!state->needs_border_color) {
  1183. memcpy(map, state->sampler_state, 4 * GENX(SAMPLER_STATE_length));
  1184. } else {
  1185. ice->state.need_border_colors = true;
  1186. /* Stream out the border color and merge the pointer. */
  1187. uint32_t offset =
  1188. iris_upload_border_color(ice, &state->border_color);
  1189. uint32_t dynamic[GENX(SAMPLER_STATE_length)];
  1190. iris_pack_state(GENX(SAMPLER_STATE), dynamic, dyns) {
  1191. dyns.BorderColorPointer = offset;
  1192. }
  1193. for (uint32_t j = 0; j < GENX(SAMPLER_STATE_length); j++)
  1194. map[j] = state->sampler_state[j] | dynamic[j];
  1195. }
  1196. map += GENX(SAMPLER_STATE_length);
  1197. }
  1198. ice->state.dirty |= IRIS_DIRTY_SAMPLER_STATES_VS << stage;
  1199. }
  1200. static enum isl_channel_select
  1201. fmt_swizzle(const struct iris_format_info *fmt, enum pipe_swizzle swz)
  1202. {
  1203. switch (swz) {
  1204. case PIPE_SWIZZLE_X: return fmt->swizzle.r;
  1205. case PIPE_SWIZZLE_Y: return fmt->swizzle.g;
  1206. case PIPE_SWIZZLE_Z: return fmt->swizzle.b;
  1207. case PIPE_SWIZZLE_W: return fmt->swizzle.a;
  1208. case PIPE_SWIZZLE_1: return SCS_ONE;
  1209. case PIPE_SWIZZLE_0: return SCS_ZERO;
  1210. default: unreachable("invalid swizzle");
  1211. }
  1212. }
  1213. static void
  1214. fill_buffer_surface_state(struct isl_device *isl_dev,
  1215. struct iris_bo *bo,
  1216. void *map,
  1217. enum isl_format format,
  1218. unsigned offset,
  1219. unsigned size)
  1220. {
  1221. const struct isl_format_layout *fmtl = isl_format_get_layout(format);
  1222. const unsigned cpp = fmtl->bpb / 8;
  1223. /* The ARB_texture_buffer_specification says:
  1224. *
  1225. * "The number of texels in the buffer texture's texel array is given by
  1226. *
  1227. * floor(<buffer_size> / (<components> * sizeof(<base_type>)),
  1228. *
  1229. * where <buffer_size> is the size of the buffer object, in basic
  1230. * machine units and <components> and <base_type> are the element count
  1231. * and base data type for elements, as specified in Table X.1. The
  1232. * number of texels in the texel array is then clamped to the
  1233. * implementation-dependent limit MAX_TEXTURE_BUFFER_SIZE_ARB."
  1234. *
  1235. * We need to clamp the size in bytes to MAX_TEXTURE_BUFFER_SIZE * stride,
  1236. * so that when ISL divides by stride to obtain the number of texels, that
  1237. * texel count is clamped to MAX_TEXTURE_BUFFER_SIZE.
  1238. */
  1239. unsigned final_size =
  1240. MIN3(size, bo->size - offset, IRIS_MAX_TEXTURE_BUFFER_SIZE * cpp);
  1241. isl_buffer_fill_state(isl_dev, map,
  1242. .address = bo->gtt_offset + offset,
  1243. .size_B = final_size,
  1244. .format = format,
  1245. .stride_B = cpp,
  1246. .mocs = MOCS_WB);
  1247. }
  1248. /**
  1249. * The pipe->create_sampler_view() driver hook.
  1250. */
  1251. static struct pipe_sampler_view *
  1252. iris_create_sampler_view(struct pipe_context *ctx,
  1253. struct pipe_resource *tex,
  1254. const struct pipe_sampler_view *tmpl)
  1255. {
  1256. struct iris_context *ice = (struct iris_context *) ctx;
  1257. struct iris_screen *screen = (struct iris_screen *)ctx->screen;
  1258. const struct gen_device_info *devinfo = &screen->devinfo;
  1259. struct iris_sampler_view *isv = calloc(1, sizeof(struct iris_sampler_view));
  1260. if (!isv)
  1261. return NULL;
  1262. /* initialize base object */
  1263. isv->base = *tmpl;
  1264. isv->base.context = ctx;
  1265. isv->base.texture = NULL;
  1266. pipe_reference_init(&isv->base.reference, 1);
  1267. pipe_resource_reference(&isv->base.texture, tex);
  1268. void *map = upload_state(ice->state.surface_uploader, &isv->surface_state,
  1269. 4 * GENX(RENDER_SURFACE_STATE_length), 64);
  1270. if (!unlikely(map))
  1271. return NULL;
  1272. struct iris_bo *state_bo = iris_resource_bo(isv->surface_state.res);
  1273. isv->surface_state.offset += iris_bo_offset_from_base_address(state_bo);
  1274. if (util_format_is_depth_or_stencil(tmpl->format)) {
  1275. struct iris_resource *zres, *sres;
  1276. const struct util_format_description *desc =
  1277. util_format_description(tmpl->format);
  1278. iris_get_depth_stencil_resources(tex, &zres, &sres);
  1279. tex = util_format_has_depth(desc) ? &zres->base : &sres->base;
  1280. }
  1281. isv->res = (struct iris_resource *) tex;
  1282. isl_surf_usage_flags_t usage =
  1283. ISL_SURF_USAGE_TEXTURE_BIT |
  1284. (isv->res->surf.usage & ISL_SURF_USAGE_CUBE_BIT);
  1285. const struct iris_format_info fmt =
  1286. iris_format_for_usage(devinfo, tmpl->format, usage);
  1287. isv->view = (struct isl_view) {
  1288. .format = fmt.fmt,
  1289. .swizzle = (struct isl_swizzle) {
  1290. .r = fmt_swizzle(&fmt, tmpl->swizzle_r),
  1291. .g = fmt_swizzle(&fmt, tmpl->swizzle_g),
  1292. .b = fmt_swizzle(&fmt, tmpl->swizzle_b),
  1293. .a = fmt_swizzle(&fmt, tmpl->swizzle_a),
  1294. },
  1295. .usage = usage,
  1296. };
  1297. /* Fill out SURFACE_STATE for this view. */
  1298. if (tmpl->target != PIPE_BUFFER) {
  1299. isv->view.base_level = tmpl->u.tex.first_level;
  1300. isv->view.levels = tmpl->u.tex.last_level - tmpl->u.tex.first_level + 1;
  1301. isv->view.base_array_layer = tmpl->u.tex.first_layer;
  1302. isv->view.array_len =
  1303. tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1;
  1304. isl_surf_fill_state(&screen->isl_dev, map,
  1305. .surf = &isv->res->surf, .view = &isv->view,
  1306. .mocs = MOCS_WB,
  1307. .address = isv->res->bo->gtt_offset);
  1308. // .aux_surf =
  1309. // .clear_color = clear_color,
  1310. } else {
  1311. fill_buffer_surface_state(&screen->isl_dev, isv->res->bo, map,
  1312. isv->view.format, tmpl->u.buf.offset,
  1313. tmpl->u.buf.size);
  1314. }
  1315. return &isv->base;
  1316. }
  1317. static void
  1318. iris_sampler_view_destroy(struct pipe_context *ctx,
  1319. struct pipe_sampler_view *state)
  1320. {
  1321. struct iris_sampler_view *isv = (void *) state;
  1322. pipe_resource_reference(&state->texture, NULL);
  1323. pipe_resource_reference(&isv->surface_state.res, NULL);
  1324. free(isv);
  1325. }
  1326. /**
  1327. * The pipe->create_surface() driver hook.
  1328. *
  1329. * In Gallium nomenclature, "surfaces" are a view of a resource that
  1330. * can be bound as a render target or depth/stencil buffer.
  1331. */
  1332. static struct pipe_surface *
  1333. iris_create_surface(struct pipe_context *ctx,
  1334. struct pipe_resource *tex,
  1335. const struct pipe_surface *tmpl)
  1336. {
  1337. struct iris_context *ice = (struct iris_context *) ctx;
  1338. struct iris_screen *screen = (struct iris_screen *)ctx->screen;
  1339. const struct gen_device_info *devinfo = &screen->devinfo;
  1340. struct iris_surface *surf = calloc(1, sizeof(struct iris_surface));
  1341. struct pipe_surface *psurf = &surf->base;
  1342. struct iris_resource *res = (struct iris_resource *) tex;
  1343. if (!surf)
  1344. return NULL;
  1345. pipe_reference_init(&psurf->reference, 1);
  1346. pipe_resource_reference(&psurf->texture, tex);
  1347. psurf->context = ctx;
  1348. psurf->format = tmpl->format;
  1349. psurf->width = tex->width0;
  1350. psurf->height = tex->height0;
  1351. psurf->texture = tex;
  1352. psurf->u.tex.first_layer = tmpl->u.tex.first_layer;
  1353. psurf->u.tex.last_layer = tmpl->u.tex.last_layer;
  1354. psurf->u.tex.level = tmpl->u.tex.level;
  1355. isl_surf_usage_flags_t usage = 0;
  1356. if (tmpl->writable)
  1357. usage = ISL_SURF_USAGE_STORAGE_BIT;
  1358. else if (util_format_is_depth_or_stencil(tmpl->format))
  1359. usage = ISL_SURF_USAGE_DEPTH_BIT;
  1360. else
  1361. usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
  1362. const struct iris_format_info fmt =
  1363. iris_format_for_usage(devinfo, psurf->format, usage);
  1364. if ((usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) &&
  1365. !isl_format_supports_rendering(devinfo, fmt.fmt)) {
  1366. /* Framebuffer validation will reject this invalid case, but it
  1367. * hasn't had the opportunity yet. In the meantime, we need to
  1368. * avoid hitting ISL asserts about unsupported formats below.
  1369. */
  1370. free(surf);
  1371. return NULL;
  1372. }
  1373. surf->view = (struct isl_view) {
  1374. .format = fmt.fmt,
  1375. .base_level = tmpl->u.tex.level,
  1376. .levels = 1,
  1377. .base_array_layer = tmpl->u.tex.first_layer,
  1378. .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
  1379. .swizzle = ISL_SWIZZLE_IDENTITY,
  1380. .usage = usage,
  1381. };
  1382. /* Bail early for depth/stencil - we don't want SURFACE_STATE for them. */
  1383. if (res->surf.usage & (ISL_SURF_USAGE_DEPTH_BIT |
  1384. ISL_SURF_USAGE_STENCIL_BIT))
  1385. return psurf;
  1386. void *map = upload_state(ice->state.surface_uploader, &surf->surface_state,
  1387. 4 * GENX(RENDER_SURFACE_STATE_length), 64);
  1388. if (!unlikely(map))
  1389. return NULL;
  1390. struct iris_bo *state_bo = iris_resource_bo(surf->surface_state.res);
  1391. surf->surface_state.offset += iris_bo_offset_from_base_address(state_bo);
  1392. isl_surf_fill_state(&screen->isl_dev, map,
  1393. .surf = &res->surf, .view = &surf->view,
  1394. .mocs = MOCS_WB,
  1395. .address = res->bo->gtt_offset);
  1396. // .aux_surf =
  1397. // .clear_color = clear_color,
  1398. return psurf;
  1399. }
  1400. /**
  1401. * The pipe->set_shader_images() driver hook.
  1402. */
  1403. static void
  1404. iris_set_shader_images(struct pipe_context *ctx,
  1405. enum pipe_shader_type p_stage,
  1406. unsigned start_slot, unsigned count,
  1407. const struct pipe_image_view *p_images)
  1408. {
  1409. struct iris_context *ice = (struct iris_context *) ctx;
  1410. struct iris_screen *screen = (struct iris_screen *)ctx->screen;
  1411. const struct gen_device_info *devinfo = &screen->devinfo;
  1412. gl_shader_stage stage = stage_from_pipe(p_stage);
  1413. struct iris_shader_state *shs = &ice->state.shaders[stage];
  1414. for (unsigned i = 0; i < count; i++) {
  1415. if (p_images && p_images[i].resource) {
  1416. const struct pipe_image_view *img = &p_images[i];
  1417. struct iris_resource *res = (void *) img->resource;
  1418. pipe_resource_reference(&shs->image[start_slot + i].res, &res->base);
  1419. // XXX: these are not retained forever, use a separate uploader?
  1420. void *map =
  1421. upload_state(ice->state.surface_uploader,
  1422. &shs->image[start_slot + i].surface_state,
  1423. 4 * GENX(RENDER_SURFACE_STATE_length), 64);
  1424. if (!unlikely(map)) {
  1425. pipe_resource_reference(&shs->image[start_slot + i].res, NULL);
  1426. return;
  1427. }
  1428. struct iris_bo *surf_state_bo =
  1429. iris_resource_bo(shs->image[start_slot + i].surface_state.res);
  1430. shs->image[start_slot + i].surface_state.offset +=
  1431. iris_bo_offset_from_base_address(surf_state_bo);
  1432. isl_surf_usage_flags_t usage = ISL_SURF_USAGE_STORAGE_BIT;
  1433. enum isl_format isl_format =
  1434. iris_format_for_usage(devinfo, img->format, usage).fmt;
  1435. if (img->shader_access & PIPE_IMAGE_ACCESS_READ)
  1436. isl_format = isl_lower_storage_image_format(devinfo, isl_format);
  1437. shs->image[start_slot + i].access = img->shader_access;
  1438. if (res->base.target != PIPE_BUFFER) {
  1439. struct isl_view view = {
  1440. .format = isl_format,
  1441. .base_level = img->u.tex.level,
  1442. .levels = 1,
  1443. .base_array_layer = img->u.tex.first_layer,
  1444. .array_len = img->u.tex.last_layer - img->u.tex.first_layer + 1,
  1445. .swizzle = ISL_SWIZZLE_IDENTITY,
  1446. .usage = usage,
  1447. };
  1448. isl_surf_fill_state(&screen->isl_dev, map,
  1449. .surf = &res->surf, .view = &view,
  1450. .mocs = MOCS_WB,
  1451. .address = res->bo->gtt_offset);
  1452. // .aux_surf =
  1453. // .clear_color = clear_color,
  1454. } else {
  1455. fill_buffer_surface_state(&screen->isl_dev, res->bo, map,
  1456. isl_format, img->u.buf.offset,
  1457. img->u.buf.size);
  1458. }
  1459. } else {
  1460. pipe_resource_reference(&shs->image[start_slot + i].res, NULL);
  1461. pipe_resource_reference(&shs->image[start_slot + i].surface_state.res,
  1462. NULL);
  1463. }
  1464. }
  1465. ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
  1466. }
  1467. /**
  1468. * The pipe->set_sampler_views() driver hook.
  1469. */
  1470. static void
  1471. iris_set_sampler_views(struct pipe_context *ctx,
  1472. enum pipe_shader_type p_stage,
  1473. unsigned start, unsigned count,
  1474. struct pipe_sampler_view **views)
  1475. {
  1476. struct iris_context *ice = (struct iris_context *) ctx;
  1477. gl_shader_stage stage = stage_from_pipe(p_stage);
  1478. struct iris_shader_state *shs = &ice->state.shaders[stage];
  1479. unsigned i;
  1480. for (i = 0; i < count; i++) {
  1481. pipe_sampler_view_reference((struct pipe_sampler_view **)
  1482. &shs->textures[i], views[i]);
  1483. }
  1484. for (; i < shs->num_textures; i++) {
  1485. pipe_sampler_view_reference((struct pipe_sampler_view **)
  1486. &shs->textures[i], NULL);
  1487. }
  1488. shs->num_textures = count;
  1489. ice->state.dirty |= (IRIS_DIRTY_BINDINGS_VS << stage);
  1490. }
  1491. /**
  1492. * The pipe->set_tess_state() driver hook.
  1493. */
  1494. static void
  1495. iris_set_tess_state(struct pipe_context *ctx,
  1496. const float default_outer_level[4],
  1497. const float default_inner_level[2])
  1498. {
  1499. struct iris_context *ice = (struct iris_context *) ctx;
  1500. memcpy(&ice->state.default_outer_level[0], &default_outer_level[0], 4 * sizeof(float));
  1501. memcpy(&ice->state.default_inner_level[0], &default_inner_level[0], 2 * sizeof(float));
  1502. ice->state.dirty |= IRIS_DIRTY_CONSTANTS_TCS;
  1503. }
  1504. static void
  1505. iris_surface_destroy(struct pipe_context *ctx, struct pipe_surface *p_surf)
  1506. {
  1507. struct iris_surface *surf = (void *) p_surf;
  1508. pipe_resource_reference(&p_surf->texture, NULL);
  1509. pipe_resource_reference(&surf->surface_state.res, NULL);
  1510. free(surf);
  1511. }
  1512. // XXX: actually implement user clip planes
  1513. static void
  1514. iris_set_clip_state(struct pipe_context *ctx,
  1515. const struct pipe_clip_state *state)
  1516. {
  1517. }
  1518. /**
  1519. * The pipe->set_polygon_stipple() driver hook.
  1520. */
  1521. static void
  1522. iris_set_polygon_stipple(struct pipe_context *ctx,
  1523. const struct pipe_poly_stipple *state)
  1524. {
  1525. struct iris_context *ice = (struct iris_context *) ctx;
  1526. memcpy(&ice->state.poly_stipple, state, sizeof(*state));
  1527. ice->state.dirty |= IRIS_DIRTY_POLYGON_STIPPLE;
  1528. }
  1529. /**
  1530. * The pipe->set_sample_mask() driver hook.
  1531. */
  1532. static void
  1533. iris_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
  1534. {
  1535. struct iris_context *ice = (struct iris_context *) ctx;
  1536. /* We only support 16x MSAA, so we have 16 bits of sample maks.
  1537. * st/mesa may pass us 0xffffffff though, meaning "enable all samples".
  1538. */
  1539. ice->state.sample_mask = sample_mask & 0xffff;
  1540. ice->state.dirty |= IRIS_DIRTY_SAMPLE_MASK;
  1541. }
  1542. /**
  1543. * The pipe->set_scissor_states() driver hook.
  1544. *
  1545. * This corresponds to our SCISSOR_RECT state structures. It's an
  1546. * exact match, so we just store them, and memcpy them out later.
  1547. */
  1548. static void
  1549. iris_set_scissor_states(struct pipe_context *ctx,
  1550. unsigned start_slot,
  1551. unsigned num_scissors,
  1552. const struct pipe_scissor_state *rects)
  1553. {
  1554. struct iris_context *ice = (struct iris_context *) ctx;
  1555. for (unsigned i = 0; i < num_scissors; i++) {
  1556. if (rects[i].minx == rects[i].maxx || rects[i].miny == rects[i].maxy) {
  1557. /* If the scissor was out of bounds and got clamped to 0 width/height
  1558. * at the bounds, the subtraction of 1 from maximums could produce a
  1559. * negative number and thus not clip anything. Instead, just provide
  1560. * a min > max scissor inside the bounds, which produces the expected
  1561. * no rendering.
  1562. */
  1563. ice->state.scissors[start_slot + i] = (struct pipe_scissor_state) {
  1564. .minx = 1, .maxx = 0, .miny = 1, .maxy = 0,
  1565. };
  1566. } else {
  1567. ice->state.scissors[start_slot + i] = (struct pipe_scissor_state) {
  1568. .minx = rects[i].minx, .miny = rects[i].miny,
  1569. .maxx = rects[i].maxx - 1, .maxy = rects[i].maxy - 1,
  1570. };
  1571. }
  1572. }
  1573. ice->state.dirty |= IRIS_DIRTY_SCISSOR_RECT;
  1574. }
  1575. /**
  1576. * The pipe->set_stencil_ref() driver hook.
  1577. *
  1578. * This is added to 3DSTATE_WM_DEPTH_STENCIL dynamically at draw time.
  1579. */
  1580. static void
  1581. iris_set_stencil_ref(struct pipe_context *ctx,
  1582. const struct pipe_stencil_ref *state)
  1583. {
  1584. struct iris_context *ice = (struct iris_context *) ctx;
  1585. memcpy(&ice->state.stencil_ref, state, sizeof(*state));
  1586. ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
  1587. }
  1588. static float
  1589. viewport_extent(const struct pipe_viewport_state *state, int axis, float sign)
  1590. {
  1591. return copysignf(state->scale[axis], sign) + state->translate[axis];
  1592. }
  1593. #if 0
  1594. static void
  1595. calculate_guardband_size(uint32_t fb_width, uint32_t fb_height,
  1596. float m00, float m11, float m30, float m31,
  1597. float *xmin, float *xmax,
  1598. float *ymin, float *ymax)
  1599. {
  1600. /* According to the "Vertex X,Y Clamping and Quantization" section of the
  1601. * Strips and Fans documentation:
  1602. *
  1603. * "The vertex X and Y screen-space coordinates are also /clamped/ to the
  1604. * fixed-point "guardband" range supported by the rasterization hardware"
  1605. *
  1606. * and
  1607. *
  1608. * "In almost all circumstances, if an object’s vertices are actually
  1609. * modified by this clamping (i.e., had X or Y coordinates outside of
  1610. * the guardband extent the rendered object will not match the intended
  1611. * result. Therefore software should take steps to ensure that this does
  1612. * not happen - e.g., by clipping objects such that they do not exceed
  1613. * these limits after the Drawing Rectangle is applied."
  1614. *
  1615. * I believe the fundamental restriction is that the rasterizer (in
  1616. * the SF/WM stages) have a limit on the number of pixels that can be
  1617. * rasterized. We need to ensure any coordinates beyond the rasterizer
  1618. * limit are handled by the clipper. So effectively that limit becomes
  1619. * the clipper's guardband size.
  1620. *
  1621. * It goes on to say:
  1622. *
  1623. * "In addition, in order to be correctly rendered, objects must have a
  1624. * screenspace bounding box not exceeding 8K in the X or Y direction.
  1625. * This additional restriction must also be comprehended by software,
  1626. * i.e., enforced by use of clipping."
  1627. *
  1628. * This makes no sense. Gen7+ hardware supports 16K render targets,
  1629. * and you definitely need to be able to draw polygons that fill the
  1630. * surface. Our assumption is that the rasterizer was limited to 8K
  1631. * on Sandybridge, which only supports 8K surfaces, and it was actually
  1632. * increased to 16K on Ivybridge and later.
  1633. *
  1634. * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
  1635. */
  1636. const float gb_size = GEN_GEN >= 7 ? 16384.0f : 8192.0f;
  1637. if (m00 != 0 && m11 != 0) {
  1638. /* First, we compute the screen-space render area */
  1639. const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
  1640. const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
  1641. const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
  1642. const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
  1643. /* We want the guardband to be centered on that */
  1644. const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
  1645. const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
  1646. const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
  1647. const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
  1648. /* Now we need it in native device coordinates */
  1649. const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
  1650. const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
  1651. const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
  1652. const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
  1653. /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
  1654. * flipped upside-down. X should be fine though.
  1655. */
  1656. assert(ndc_gb_xmin <= ndc_gb_xmax);
  1657. *xmin = ndc_gb_xmin;
  1658. *xmax = ndc_gb_xmax;
  1659. *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
  1660. *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
  1661. } else {
  1662. /* The viewport scales to 0, so nothing will be rendered. */
  1663. *xmin = 0.0f;
  1664. *xmax = 0.0f;
  1665. *ymin = 0.0f;
  1666. *ymax = 0.0f;
  1667. }
  1668. }
  1669. #endif
  1670. /**
  1671. * The pipe->set_viewport_states() driver hook.
  1672. *
  1673. * This corresponds to our SF_CLIP_VIEWPORT states. We can't calculate
  1674. * the guardband yet, as we need the framebuffer dimensions, but we can
  1675. * at least fill out the rest.
  1676. */
  1677. static void
  1678. iris_set_viewport_states(struct pipe_context *ctx,
  1679. unsigned start_slot,
  1680. unsigned count,
  1681. const struct pipe_viewport_state *states)
  1682. {
  1683. struct iris_context *ice = (struct iris_context *) ctx;
  1684. struct iris_genx_state *genx = ice->state.genx;
  1685. uint32_t *vp_map =
  1686. &genx->sf_cl_vp[start_slot * GENX(SF_CLIP_VIEWPORT_length)];
  1687. for (unsigned i = 0; i < count; i++) {
  1688. const struct pipe_viewport_state *state = &states[i];
  1689. memcpy(&ice->state.viewports[start_slot + i], state, sizeof(*state));
  1690. iris_pack_state(GENX(SF_CLIP_VIEWPORT), vp_map, vp) {
  1691. vp.ViewportMatrixElementm00 = state->scale[0];
  1692. vp.ViewportMatrixElementm11 = state->scale[1];
  1693. vp.ViewportMatrixElementm22 = state->scale[2];
  1694. vp.ViewportMatrixElementm30 = state->translate[0];
  1695. vp.ViewportMatrixElementm31 = state->translate[1];
  1696. vp.ViewportMatrixElementm32 = state->translate[2];
  1697. /* XXX: in i965 this is computed based on the drawbuffer size,
  1698. * but we don't have that here...
  1699. */
  1700. vp.XMinClipGuardband = -1.0;
  1701. vp.XMaxClipGuardband = 1.0;
  1702. vp.YMinClipGuardband = -1.0;
  1703. vp.YMaxClipGuardband = 1.0;
  1704. vp.XMinViewPort = viewport_extent(state, 0, -1.0f);
  1705. vp.XMaxViewPort = viewport_extent(state, 0, 1.0f) - 1;
  1706. vp.YMinViewPort = viewport_extent(state, 1, -1.0f);
  1707. vp.YMaxViewPort = viewport_extent(state, 1, 1.0f) - 1;
  1708. }
  1709. vp_map += GENX(SF_CLIP_VIEWPORT_length);
  1710. }
  1711. ice->state.dirty |= IRIS_DIRTY_SF_CL_VIEWPORT;
  1712. if (ice->state.cso_rast && (!ice->state.cso_rast->depth_clip_near ||
  1713. !ice->state.cso_rast->depth_clip_far))
  1714. ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
  1715. }
  1716. /**
  1717. * The pipe->set_framebuffer_state() driver hook.
  1718. *
  1719. * Sets the current draw FBO, including color render targets, depth,
  1720. * and stencil buffers.
  1721. */
  1722. static void
  1723. iris_set_framebuffer_state(struct pipe_context *ctx,
  1724. const struct pipe_framebuffer_state *state)
  1725. {
  1726. struct iris_context *ice = (struct iris_context *) ctx;
  1727. struct iris_screen *screen = (struct iris_screen *)ctx->screen;
  1728. struct isl_device *isl_dev = &screen->isl_dev;
  1729. struct pipe_framebuffer_state *cso = &ice->state.framebuffer;
  1730. struct iris_resource *zres;
  1731. struct iris_resource *stencil_res;
  1732. unsigned samples = util_framebuffer_get_num_samples(state);
  1733. if (cso->samples != samples) {
  1734. ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
  1735. }
  1736. if (cso->nr_cbufs != state->nr_cbufs) {
  1737. ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
  1738. }
  1739. if ((cso->layers == 0) != (state->layers == 0)) {
  1740. ice->state.dirty |= IRIS_DIRTY_CLIP;
  1741. }
  1742. util_copy_framebuffer_state(cso, state);
  1743. cso->samples = samples;
  1744. struct iris_depth_buffer_state *cso_z = &ice->state.genx->depth_buffer;
  1745. struct isl_view view = {
  1746. .base_level = 0,
  1747. .levels = 1,
  1748. .base_array_layer = 0,
  1749. .array_len = 1,
  1750. .swizzle = ISL_SWIZZLE_IDENTITY,
  1751. };
  1752. struct isl_depth_stencil_hiz_emit_info info = {
  1753. .view = &view,
  1754. .mocs = MOCS_WB,
  1755. };
  1756. if (cso->zsbuf) {
  1757. iris_get_depth_stencil_resources(cso->zsbuf->texture, &zres,
  1758. &stencil_res);
  1759. view.base_level = cso->zsbuf->u.tex.level;
  1760. view.base_array_layer = cso->zsbuf->u.tex.first_layer;
  1761. view.array_len =
  1762. cso->zsbuf->u.tex.last_layer - cso->zsbuf->u.tex.first_layer + 1;
  1763. if (zres) {
  1764. view.usage |= ISL_SURF_USAGE_DEPTH_BIT;
  1765. info.depth_surf = &zres->surf;
  1766. info.depth_address = zres->bo->gtt_offset;
  1767. info.hiz_usage = ISL_AUX_USAGE_NONE;
  1768. view.format = zres->surf.format;
  1769. }
  1770. if (stencil_res) {
  1771. view.usage |= ISL_SURF_USAGE_STENCIL_BIT;
  1772. info.stencil_surf = &stencil_res->surf;
  1773. info.stencil_address = stencil_res->bo->gtt_offset;
  1774. if (!zres)
  1775. view.format = stencil_res->surf.format;
  1776. }
  1777. }
  1778. isl_emit_depth_stencil_hiz_s(isl_dev, cso_z->packets, &info);
  1779. /* Make a null surface for unbound buffers */
  1780. void *null_surf_map =
  1781. upload_state(ice->state.surface_uploader, &ice->state.null_fb,
  1782. 4 * GENX(RENDER_SURFACE_STATE_length), 64);
  1783. isl_null_fill_state(&screen->isl_dev, null_surf_map,
  1784. isl_extent3d(MAX2(cso->width, 1),
  1785. MAX2(cso->height, 1),
  1786. cso->layers ? cso->layers : 1));
  1787. ice->state.null_fb.offset +=
  1788. iris_bo_offset_from_base_address(iris_resource_bo(ice->state.null_fb.res));
  1789. ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
  1790. /* Render target change */
  1791. ice->state.dirty |= IRIS_DIRTY_BINDINGS_FS;
  1792. ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_FRAMEBUFFER];
  1793. #if GEN_GEN == 11
  1794. // XXX: we may want to flag IRIS_DIRTY_MULTISAMPLE (or SAMPLE_MASK?)
  1795. // XXX: see commit 979fc1bc9bcc64027ff2cfafd285676f31b930a6
  1796. /* The PIPE_CONTROL command description says:
  1797. *
  1798. * "Whenever a Binding Table Index (BTI) used by a Render Target Message
  1799. * points to a different RENDER_SURFACE_STATE, SW must issue a Render
  1800. * Target Cache Flush by enabling this bit. When render target flush
  1801. * is set due to new association of BTI, PS Scoreboard Stall bit must
  1802. * be set in this packet."
  1803. */
  1804. // XXX: does this need to happen at 3DSTATE_BTP_PS time?
  1805. iris_emit_pipe_control_flush(&ice->render_batch,
  1806. PIPE_CONTROL_RENDER_TARGET_FLUSH |
  1807. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  1808. #endif
  1809. }
  1810. static void
  1811. upload_ubo_surf_state(struct iris_context *ice,
  1812. struct iris_const_buffer *cbuf,
  1813. unsigned buffer_size)
  1814. {
  1815. struct pipe_context *ctx = &ice->ctx;
  1816. struct iris_screen *screen = (struct iris_screen *) ctx->screen;
  1817. // XXX: these are not retained forever, use a separate uploader?
  1818. void *map =
  1819. upload_state(ice->state.surface_uploader, &cbuf->surface_state,
  1820. 4 * GENX(RENDER_SURFACE_STATE_length), 64);
  1821. if (!unlikely(map)) {
  1822. pipe_resource_reference(&cbuf->data.res, NULL);
  1823. return;
  1824. }
  1825. struct iris_resource *res = (void *) cbuf->data.res;
  1826. struct iris_bo *surf_bo = iris_resource_bo(cbuf->surface_state.res);
  1827. cbuf->surface_state.offset += iris_bo_offset_from_base_address(surf_bo);
  1828. isl_buffer_fill_state(&screen->isl_dev, map,
  1829. .address = res->bo->gtt_offset + cbuf->data.offset,
  1830. .size_B = MIN2(buffer_size,
  1831. res->bo->size - cbuf->data.offset),
  1832. .format = ISL_FORMAT_R32G32B32A32_FLOAT,
  1833. .stride_B = 1,
  1834. .mocs = MOCS_WB)
  1835. }
  1836. /**
  1837. * The pipe->set_constant_buffer() driver hook.
  1838. *
  1839. * This uploads any constant data in user buffers, and references
  1840. * any UBO resources containing constant data.
  1841. */
  1842. static void
  1843. iris_set_constant_buffer(struct pipe_context *ctx,
  1844. enum pipe_shader_type p_stage, unsigned index,
  1845. const struct pipe_constant_buffer *input)
  1846. {
  1847. struct iris_context *ice = (struct iris_context *) ctx;
  1848. gl_shader_stage stage = stage_from_pipe(p_stage);
  1849. struct iris_shader_state *shs = &ice->state.shaders[stage];
  1850. struct iris_const_buffer *cbuf = &shs->constbuf[index];
  1851. if (input && input->buffer) {
  1852. assert(index > 0);
  1853. pipe_resource_reference(&cbuf->data.res, input->buffer);
  1854. cbuf->data.offset = input->buffer_offset;
  1855. upload_ubo_surf_state(ice, cbuf, input->buffer_size);
  1856. } else {
  1857. pipe_resource_reference(&cbuf->data.res, NULL);
  1858. pipe_resource_reference(&cbuf->surface_state.res, NULL);
  1859. }
  1860. if (index == 0) {
  1861. if (input)
  1862. memcpy(&shs->cbuf0, input, sizeof(shs->cbuf0));
  1863. else
  1864. memset(&shs->cbuf0, 0, sizeof(shs->cbuf0));
  1865. shs->cbuf0_needs_upload = true;
  1866. }
  1867. ice->state.dirty |= IRIS_DIRTY_CONSTANTS_VS << stage;
  1868. // XXX: maybe not necessary all the time...?
  1869. // XXX: we need 3DS_BTP to commit these changes, and if we fell back to
  1870. // XXX: pull model we may need actual new bindings...
  1871. ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
  1872. }
  1873. static void
  1874. upload_uniforms(struct iris_context *ice,
  1875. gl_shader_stage stage)
  1876. {
  1877. struct iris_shader_state *shs = &ice->state.shaders[stage];
  1878. struct iris_const_buffer *cbuf = &shs->constbuf[0];
  1879. struct iris_compiled_shader *shader = ice->shaders.prog[stage];
  1880. struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
  1881. unsigned upload_size = prog_data->nr_params * sizeof(uint32_t) +
  1882. shs->cbuf0.buffer_size;
  1883. if (upload_size == 0)
  1884. return;
  1885. uint32_t *map =
  1886. upload_state(ice->ctx.const_uploader, &cbuf->data, upload_size, 64);
  1887. for (int i = 0; i < prog_data->nr_params; i++) {
  1888. uint32_t param = prog_data->param[i];
  1889. uint32_t value = 0;
  1890. printf("got a param to upload - %u\n", param);
  1891. *map++ = value;
  1892. }
  1893. if (shs->cbuf0.user_buffer) {
  1894. memcpy(map, shs->cbuf0.user_buffer, shs->cbuf0.buffer_size);
  1895. }
  1896. upload_ubo_surf_state(ice, cbuf, upload_size);
  1897. }
  1898. /**
  1899. * The pipe->set_shader_buffers() driver hook.
  1900. *
  1901. * This binds SSBOs and ABOs. Unfortunately, we need to stream out
  1902. * SURFACE_STATE here, as the buffer offset may change each time.
  1903. */
  1904. static void
  1905. iris_set_shader_buffers(struct pipe_context *ctx,
  1906. enum pipe_shader_type p_stage,
  1907. unsigned start_slot, unsigned count,
  1908. const struct pipe_shader_buffer *buffers)
  1909. {
  1910. struct iris_context *ice = (struct iris_context *) ctx;
  1911. struct iris_screen *screen = (struct iris_screen *)ctx->screen;
  1912. gl_shader_stage stage = stage_from_pipe(p_stage);
  1913. struct iris_shader_state *shs = &ice->state.shaders[stage];
  1914. for (unsigned i = 0; i < count; i++) {
  1915. if (buffers && buffers[i].buffer) {
  1916. const struct pipe_shader_buffer *buffer = &buffers[i];
  1917. struct iris_resource *res = (void *) buffer->buffer;
  1918. pipe_resource_reference(&shs->ssbo[start_slot + i], &res->base);
  1919. // XXX: these are not retained forever, use a separate uploader?
  1920. void *map =
  1921. upload_state(ice->state.surface_uploader,
  1922. &shs->ssbo_surface_state[start_slot + i],
  1923. 4 * GENX(RENDER_SURFACE_STATE_length), 64);
  1924. if (!unlikely(map)) {
  1925. pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
  1926. return;
  1927. }
  1928. struct iris_bo *surf_state_bo =
  1929. iris_resource_bo(shs->ssbo_surface_state[start_slot + i].res);
  1930. shs->ssbo_surface_state[start_slot + i].offset +=
  1931. iris_bo_offset_from_base_address(surf_state_bo);
  1932. isl_buffer_fill_state(&screen->isl_dev, map,
  1933. .address =
  1934. res->bo->gtt_offset + buffer->buffer_offset,
  1935. .size_B =
  1936. MIN2(buffer->buffer_size,
  1937. res->bo->size - buffer->buffer_offset),
  1938. .format = ISL_FORMAT_RAW,
  1939. .stride_B = 1,
  1940. .mocs = MOCS_WB);
  1941. } else {
  1942. pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
  1943. pipe_resource_reference(&shs->ssbo_surface_state[start_slot + i].res,
  1944. NULL);
  1945. }
  1946. }
  1947. ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
  1948. }
  1949. static void
  1950. iris_delete_state(struct pipe_context *ctx, void *state)
  1951. {
  1952. free(state);
  1953. }
  1954. static void
  1955. iris_free_vertex_buffers(struct iris_vertex_buffer_state *cso)
  1956. {
  1957. for (unsigned i = 0; i < cso->num_buffers; i++)
  1958. pipe_resource_reference(&cso->resources[i], NULL);
  1959. }
  1960. /**
  1961. * The pipe->set_vertex_buffers() driver hook.
  1962. *
  1963. * This translates pipe_vertex_buffer to our 3DSTATE_VERTEX_BUFFERS packet.
  1964. */
  1965. static void
  1966. iris_set_vertex_buffers(struct pipe_context *ctx,
  1967. unsigned start_slot, unsigned count,
  1968. const struct pipe_vertex_buffer *buffers)
  1969. {
  1970. struct iris_context *ice = (struct iris_context *) ctx;
  1971. struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
  1972. iris_free_vertex_buffers(&ice->state.genx->vertex_buffers);
  1973. if (!buffers)
  1974. count = 0;
  1975. cso->num_buffers = count;
  1976. iris_pack_command(GENX(3DSTATE_VERTEX_BUFFERS), cso->vertex_buffers, vb) {
  1977. vb.DWordLength = 4 * MAX2(cso->num_buffers, 1) - 1;
  1978. }
  1979. uint32_t *vb_pack_dest = &cso->vertex_buffers[1];
  1980. if (count == 0) {
  1981. iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
  1982. vb.VertexBufferIndex = start_slot;
  1983. vb.NullVertexBuffer = true;
  1984. vb.AddressModifyEnable = true;
  1985. }
  1986. }
  1987. for (unsigned i = 0; i < count; i++) {
  1988. assert(!buffers[i].is_user_buffer);
  1989. pipe_resource_reference(&cso->resources[i], buffers[i].buffer.resource);
  1990. struct iris_resource *res = (void *) cso->resources[i];
  1991. iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
  1992. vb.VertexBufferIndex = start_slot + i;
  1993. vb.MOCS = MOCS_WB;
  1994. vb.AddressModifyEnable = true;
  1995. vb.BufferPitch = buffers[i].stride;
  1996. if (res) {
  1997. vb.BufferSize = res->bo->size;
  1998. vb.BufferStartingAddress =
  1999. ro_bo(NULL, res->bo->gtt_offset + buffers[i].buffer_offset);
  2000. } else {
  2001. vb.NullVertexBuffer = true;
  2002. }
  2003. }
  2004. vb_pack_dest += GENX(VERTEX_BUFFER_STATE_length);
  2005. }
  2006. ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS;
  2007. }
  2008. /**
  2009. * Gallium CSO for vertex elements.
  2010. */
  2011. struct iris_vertex_element_state {
  2012. uint32_t vertex_elements[1 + 33 * GENX(VERTEX_ELEMENT_STATE_length)];
  2013. uint32_t vf_instancing[33 * GENX(3DSTATE_VF_INSTANCING_length)];
  2014. unsigned count;
  2015. };
  2016. /**
  2017. * The pipe->create_vertex_elements() driver hook.
  2018. *
  2019. * This translates pipe_vertex_element to our 3DSTATE_VERTEX_ELEMENTS
  2020. * and 3DSTATE_VF_INSTANCING commands. SGVs are handled at draw time.
  2021. */
  2022. static void *
  2023. iris_create_vertex_elements(struct pipe_context *ctx,
  2024. unsigned count,
  2025. const struct pipe_vertex_element *state)
  2026. {
  2027. struct iris_screen *screen = (struct iris_screen *)ctx->screen;
  2028. const struct gen_device_info *devinfo = &screen->devinfo;
  2029. struct iris_vertex_element_state *cso =
  2030. malloc(sizeof(struct iris_vertex_element_state));
  2031. cso->count = count;
  2032. /* TODO:
  2033. * - create edge flag one
  2034. * - create SGV ones
  2035. * - if those are necessary, use count + 1/2/3... OR in the length
  2036. */
  2037. iris_pack_command(GENX(3DSTATE_VERTEX_ELEMENTS), cso->vertex_elements, ve) {
  2038. ve.DWordLength =
  2039. 1 + GENX(VERTEX_ELEMENT_STATE_length) * MAX2(count, 1) - 2;
  2040. }
  2041. uint32_t *ve_pack_dest = &cso->vertex_elements[1];
  2042. uint32_t *vfi_pack_dest = cso->vf_instancing;
  2043. if (count == 0) {
  2044. iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
  2045. ve.Valid = true;
  2046. ve.SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT;
  2047. ve.Component0Control = VFCOMP_STORE_0;
  2048. ve.Component1Control = VFCOMP_STORE_0;
  2049. ve.Component2Control = VFCOMP_STORE_0;
  2050. ve.Component3Control = VFCOMP_STORE_1_FP;
  2051. }
  2052. iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
  2053. }
  2054. }
  2055. for (int i = 0; i < count; i++) {
  2056. const struct iris_format_info fmt =
  2057. iris_format_for_usage(devinfo, state[i].src_format, 0);
  2058. unsigned comp[4] = { VFCOMP_STORE_SRC, VFCOMP_STORE_SRC,
  2059. VFCOMP_STORE_SRC, VFCOMP_STORE_SRC };
  2060. switch (isl_format_get_num_channels(fmt.fmt)) {
  2061. case 0: comp[0] = VFCOMP_STORE_0;
  2062. case 1: comp[1] = VFCOMP_STORE_0;
  2063. case 2: comp[2] = VFCOMP_STORE_0;
  2064. case 3:
  2065. comp[3] = isl_format_has_int_channel(fmt.fmt) ? VFCOMP_STORE_1_INT
  2066. : VFCOMP_STORE_1_FP;
  2067. break;
  2068. }
  2069. iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
  2070. ve.VertexBufferIndex = state[i].vertex_buffer_index;
  2071. ve.Valid = true;
  2072. ve.SourceElementOffset = state[i].src_offset;
  2073. ve.SourceElementFormat = fmt.fmt;
  2074. ve.Component0Control = comp[0];
  2075. ve.Component1Control = comp[1];
  2076. ve.Component2Control = comp[2];
  2077. ve.Component3Control = comp[3];
  2078. }
  2079. iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
  2080. vi.VertexElementIndex = i;
  2081. vi.InstancingEnable = state[i].instance_divisor > 0;
  2082. vi.InstanceDataStepRate = state[i].instance_divisor;
  2083. }
  2084. ve_pack_dest += GENX(VERTEX_ELEMENT_STATE_length);
  2085. vfi_pack_dest += GENX(3DSTATE_VF_INSTANCING_length);
  2086. }
  2087. return cso;
  2088. }
  2089. /**
  2090. * The pipe->bind_vertex_elements_state() driver hook.
  2091. */
  2092. static void
  2093. iris_bind_vertex_elements_state(struct pipe_context *ctx, void *state)
  2094. {
  2095. struct iris_context *ice = (struct iris_context *) ctx;
  2096. struct iris_vertex_element_state *old_cso = ice->state.cso_vertex_elements;
  2097. struct iris_vertex_element_state *new_cso = state;
  2098. /* 3DSTATE_VF_SGVs overrides the last VE, so if the count is changing,
  2099. * we need to re-emit it to ensure we're overriding the right one.
  2100. */
  2101. if (new_cso && cso_changed(count))
  2102. ice->state.dirty |= IRIS_DIRTY_VF_SGVS;
  2103. ice->state.cso_vertex_elements = state;
  2104. ice->state.dirty |= IRIS_DIRTY_VERTEX_ELEMENTS;
  2105. }
  2106. /**
  2107. * Gallium CSO for stream output (transform feedback) targets.
  2108. */
  2109. struct iris_stream_output_target {
  2110. struct pipe_stream_output_target base;
  2111. uint32_t so_buffer[GENX(3DSTATE_SO_BUFFER_length)];
  2112. /** Storage holding the offset where we're writing in the buffer */
  2113. struct iris_state_ref offset;
  2114. };
  2115. /**
  2116. * The pipe->create_stream_output_target() driver hook.
  2117. *
  2118. * "Target" here refers to a destination buffer. We translate this into
  2119. * a 3DSTATE_SO_BUFFER packet. We can handle most fields, but don't yet
  2120. * know which buffer this represents, or whether we ought to zero the
  2121. * write-offsets, or append. Those are handled in the set() hook.
  2122. */
  2123. static struct pipe_stream_output_target *
  2124. iris_create_stream_output_target(struct pipe_context *ctx,
  2125. struct pipe_resource *res,
  2126. unsigned buffer_offset,
  2127. unsigned buffer_size)
  2128. {
  2129. struct iris_stream_output_target *cso = calloc(1, sizeof(*cso));
  2130. if (!cso)
  2131. return NULL;
  2132. pipe_reference_init(&cso->base.reference, 1);
  2133. pipe_resource_reference(&cso->base.buffer, res);
  2134. cso->base.buffer_offset = buffer_offset;
  2135. cso->base.buffer_size = buffer_size;
  2136. cso->base.context = ctx;
  2137. upload_state(ctx->stream_uploader, &cso->offset, 4 * sizeof(uint32_t), 4);
  2138. iris_pack_command(GENX(3DSTATE_SO_BUFFER), cso->so_buffer, sob) {
  2139. sob.SurfaceBaseAddress =
  2140. rw_bo(NULL, iris_resource_bo(res)->gtt_offset + buffer_offset);
  2141. sob.SOBufferEnable = true;
  2142. sob.StreamOffsetWriteEnable = true;
  2143. sob.StreamOutputBufferOffsetAddressEnable = true;
  2144. sob.MOCS = MOCS_WB; // XXX: MOCS
  2145. sob.SurfaceSize = MAX2(buffer_size / 4, 1) - 1;
  2146. /* .SOBufferIndex, .StreamOffset, and .StreamOutputBufferOffsetAddress
  2147. * are filled in later when we have stream IDs.
  2148. */
  2149. }
  2150. return &cso->base;
  2151. }
  2152. static void
  2153. iris_stream_output_target_destroy(struct pipe_context *ctx,
  2154. struct pipe_stream_output_target *state)
  2155. {
  2156. struct iris_stream_output_target *cso = (void *) state;
  2157. pipe_resource_reference(&cso->base.buffer, NULL);
  2158. pipe_resource_reference(&cso->offset.res, NULL);
  2159. free(cso);
  2160. }
  2161. /**
  2162. * The pipe->set_stream_output_targets() driver hook.
  2163. *
  2164. * At this point, we know which targets are bound to a particular index,
  2165. * and also whether we want to append or start over. We can finish the
  2166. * 3DSTATE_SO_BUFFER packets we started earlier.
  2167. */
  2168. static void
  2169. iris_set_stream_output_targets(struct pipe_context *ctx,
  2170. unsigned num_targets,
  2171. struct pipe_stream_output_target **targets,
  2172. const unsigned *offsets)
  2173. {
  2174. struct iris_context *ice = (struct iris_context *) ctx;
  2175. struct iris_genx_state *genx = ice->state.genx;
  2176. uint32_t *so_buffers = genx->so_buffers;
  2177. const bool active = num_targets > 0;
  2178. if (ice->state.streamout_active != active) {
  2179. ice->state.streamout_active = active;
  2180. ice->state.dirty |= IRIS_DIRTY_STREAMOUT;
  2181. /* We only emit 3DSTATE_SO_DECL_LIST when streamout is active, because
  2182. * it's a non-pipelined command. If we're switching streamout on, we
  2183. * may have missed emitting it earlier, so do so now. (We're already
  2184. * taking a stall to update 3DSTATE_SO_BUFFERS anyway...)
  2185. */
  2186. if (active)
  2187. ice->state.dirty |= IRIS_DIRTY_SO_DECL_LIST;
  2188. }
  2189. for (int i = 0; i < 4; i++) {
  2190. pipe_so_target_reference(&ice->state.so_target[i],
  2191. i < num_targets ? targets[i] : NULL);
  2192. }
  2193. /* No need to update 3DSTATE_SO_BUFFER unless SOL is active. */
  2194. if (!active)
  2195. return;
  2196. for (unsigned i = 0; i < 4; i++,
  2197. so_buffers += GENX(3DSTATE_SO_BUFFER_length)) {
  2198. if (i >= num_targets || !targets[i]) {
  2199. iris_pack_command(GENX(3DSTATE_SO_BUFFER), so_buffers, sob)
  2200. sob.SOBufferIndex = i;
  2201. continue;
  2202. }
  2203. struct iris_stream_output_target *tgt = (void *) targets[i];
  2204. /* Note that offsets[i] will either be 0, causing us to zero
  2205. * the value in the buffer, or 0xFFFFFFFF, which happens to mean
  2206. * "continue appending at the existing offset."
  2207. */
  2208. assert(offsets[i] == 0 || offsets[i] == 0xFFFFFFFF);
  2209. uint32_t dynamic[GENX(3DSTATE_SO_BUFFER_length)];
  2210. iris_pack_state(GENX(3DSTATE_SO_BUFFER), dynamic, dyns) {
  2211. dyns.SOBufferIndex = i;
  2212. dyns.StreamOffset = offsets[i];
  2213. dyns.StreamOutputBufferOffsetAddress =
  2214. rw_bo(NULL, iris_resource_bo(tgt->offset.res)->gtt_offset + tgt->offset.offset + i * sizeof(uint32_t));
  2215. }
  2216. for (uint32_t j = 0; j < GENX(3DSTATE_SO_BUFFER_length); j++) {
  2217. so_buffers[j] = tgt->so_buffer[j] | dynamic[j];
  2218. }
  2219. }
  2220. ice->state.dirty |= IRIS_DIRTY_SO_BUFFERS;
  2221. }
  2222. /**
  2223. * An iris-vtable helper for encoding the 3DSTATE_SO_DECL_LIST and
  2224. * 3DSTATE_STREAMOUT packets.
  2225. *
  2226. * 3DSTATE_SO_DECL_LIST is a list of shader outputs we want the streamout
  2227. * hardware to record. We can create it entirely based on the shader, with
  2228. * no dynamic state dependencies.
  2229. *
  2230. * 3DSTATE_STREAMOUT is an annoying mix of shader-based information and
  2231. * state-based settings. We capture the shader-related ones here, and merge
  2232. * the rest in at draw time.
  2233. */
  2234. static uint32_t *
  2235. iris_create_so_decl_list(const struct pipe_stream_output_info *info,
  2236. const struct brw_vue_map *vue_map)
  2237. {
  2238. struct GENX(SO_DECL) so_decl[MAX_VERTEX_STREAMS][128];
  2239. int buffer_mask[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
  2240. int next_offset[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
  2241. int decls[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
  2242. int max_decls = 0;
  2243. STATIC_ASSERT(ARRAY_SIZE(so_decl[0]) >= MAX_PROGRAM_OUTPUTS);
  2244. memset(so_decl, 0, sizeof(so_decl));
  2245. /* Construct the list of SO_DECLs to be emitted. The formatting of the
  2246. * command feels strange -- each dword pair contains a SO_DECL per stream.
  2247. */
  2248. for (unsigned i = 0; i < info->num_outputs; i++) {
  2249. const struct pipe_stream_output *output = &info->output[i];
  2250. const int buffer = output->output_buffer;
  2251. const int varying = output->register_index;
  2252. const unsigned stream_id = output->stream;
  2253. assert(stream_id < MAX_VERTEX_STREAMS);
  2254. buffer_mask[stream_id] |= 1 << buffer;
  2255. assert(vue_map->varying_to_slot[varying] >= 0);
  2256. /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
  2257. * array. Instead, it simply increments DstOffset for the following
  2258. * input by the number of components that should be skipped.
  2259. *
  2260. * Our hardware is unusual in that it requires us to program SO_DECLs
  2261. * for fake "hole" components, rather than simply taking the offset
  2262. * for each real varying. Each hole can have size 1, 2, 3, or 4; we
  2263. * program as many size = 4 holes as we can, then a final hole to
  2264. * accommodate the final 1, 2, or 3 remaining.
  2265. */
  2266. int skip_components = output->dst_offset - next_offset[buffer];
  2267. while (skip_components > 0) {
  2268. so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
  2269. .HoleFlag = 1,
  2270. .OutputBufferSlot = output->output_buffer,
  2271. .ComponentMask = (1 << MIN2(skip_components, 4)) - 1,
  2272. };
  2273. skip_components -= 4;
  2274. }
  2275. next_offset[buffer] = output->dst_offset + output->num_components;
  2276. so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
  2277. .OutputBufferSlot = output->output_buffer,
  2278. .RegisterIndex = vue_map->varying_to_slot[varying],
  2279. .ComponentMask =
  2280. ((1 << output->num_components) - 1) << output->start_component,
  2281. };
  2282. if (decls[stream_id] > max_decls)
  2283. max_decls = decls[stream_id];
  2284. }
  2285. unsigned dwords = GENX(3DSTATE_STREAMOUT_length) + (3 + 2 * max_decls);
  2286. uint32_t *map = ralloc_size(NULL, sizeof(uint32_t) * dwords);
  2287. uint32_t *so_decl_map = map + GENX(3DSTATE_STREAMOUT_length);
  2288. iris_pack_command(GENX(3DSTATE_STREAMOUT), map, sol) {
  2289. int urb_entry_read_offset = 0;
  2290. int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
  2291. urb_entry_read_offset;
  2292. /* We always read the whole vertex. This could be reduced at some
  2293. * point by reading less and offsetting the register index in the
  2294. * SO_DECLs.
  2295. */
  2296. sol.Stream0VertexReadOffset = urb_entry_read_offset;
  2297. sol.Stream0VertexReadLength = urb_entry_read_length - 1;
  2298. sol.Stream1VertexReadOffset = urb_entry_read_offset;
  2299. sol.Stream1VertexReadLength = urb_entry_read_length - 1;
  2300. sol.Stream2VertexReadOffset = urb_entry_read_offset;
  2301. sol.Stream2VertexReadLength = urb_entry_read_length - 1;
  2302. sol.Stream3VertexReadOffset = urb_entry_read_offset;
  2303. sol.Stream3VertexReadLength = urb_entry_read_length - 1;
  2304. /* Set buffer pitches; 0 means unbound. */
  2305. sol.Buffer0SurfacePitch = 4 * info->stride[0];
  2306. sol.Buffer1SurfacePitch = 4 * info->stride[1];
  2307. sol.Buffer2SurfacePitch = 4 * info->stride[2];
  2308. sol.Buffer3SurfacePitch = 4 * info->stride[3];
  2309. }
  2310. iris_pack_command(GENX(3DSTATE_SO_DECL_LIST), so_decl_map, list) {
  2311. list.DWordLength = 3 + 2 * max_decls - 2;
  2312. list.StreamtoBufferSelects0 = buffer_mask[0];
  2313. list.StreamtoBufferSelects1 = buffer_mask[1];
  2314. list.StreamtoBufferSelects2 = buffer_mask[2];
  2315. list.StreamtoBufferSelects3 = buffer_mask[3];
  2316. list.NumEntries0 = decls[0];
  2317. list.NumEntries1 = decls[1];
  2318. list.NumEntries2 = decls[2];
  2319. list.NumEntries3 = decls[3];
  2320. }
  2321. for (int i = 0; i < max_decls; i++) {
  2322. iris_pack_state(GENX(SO_DECL_ENTRY), so_decl_map + 3 + i * 2, entry) {
  2323. entry.Stream0Decl = so_decl[0][i];
  2324. entry.Stream1Decl = so_decl[1][i];
  2325. entry.Stream2Decl = so_decl[2][i];
  2326. entry.Stream3Decl = so_decl[3][i];
  2327. }
  2328. }
  2329. return map;
  2330. }
  2331. static void
  2332. iris_compute_sbe_urb_read_interval(uint64_t fs_input_slots,
  2333. const struct brw_vue_map *last_vue_map,
  2334. bool two_sided_color,
  2335. unsigned *out_offset,
  2336. unsigned *out_length)
  2337. {
  2338. /* The compiler computes the first URB slot without considering COL/BFC
  2339. * swizzling (because it doesn't know whether it's enabled), so we need
  2340. * to do that here too. This may result in a smaller offset, which
  2341. * should be safe.
  2342. */
  2343. const unsigned first_slot =
  2344. brw_compute_first_urb_slot_required(fs_input_slots, last_vue_map);
  2345. /* This becomes the URB read offset (counted in pairs of slots). */
  2346. assert(first_slot % 2 == 0);
  2347. *out_offset = first_slot / 2;
  2348. /* We need to adjust the inputs read to account for front/back color
  2349. * swizzling, as it can make the URB length longer.
  2350. */
  2351. for (int c = 0; c <= 1; c++) {
  2352. if (fs_input_slots & (VARYING_BIT_COL0 << c)) {
  2353. /* If two sided color is enabled, the fragment shader's gl_Color
  2354. * (COL0) input comes from either the gl_FrontColor (COL0) or
  2355. * gl_BackColor (BFC0) input varyings. Mark BFC as used, too.
  2356. */
  2357. if (two_sided_color)
  2358. fs_input_slots |= (VARYING_BIT_BFC0 << c);
  2359. /* If front color isn't written, we opt to give them back color
  2360. * instead of an undefined value. Switch from COL to BFC.
  2361. */
  2362. if (last_vue_map->varying_to_slot[VARYING_SLOT_COL0 + c] == -1) {
  2363. fs_input_slots &= ~(VARYING_BIT_COL0 << c);
  2364. fs_input_slots |= (VARYING_BIT_BFC0 << c);
  2365. }
  2366. }
  2367. }
  2368. /* Compute the minimum URB Read Length necessary for the FS inputs.
  2369. *
  2370. * From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
  2371. * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
  2372. *
  2373. * "This field should be set to the minimum length required to read the
  2374. * maximum source attribute. The maximum source attribute is indicated
  2375. * by the maximum value of the enabled Attribute # Source Attribute if
  2376. * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
  2377. * enable is not set.
  2378. * read_length = ceiling((max_source_attr + 1) / 2)
  2379. *
  2380. * [errata] Corruption/Hang possible if length programmed larger than
  2381. * recommended"
  2382. *
  2383. * Similar text exists for Ivy Bridge.
  2384. *
  2385. * We find the last URB slot that's actually read by the FS.
  2386. */
  2387. unsigned last_read_slot = last_vue_map->num_slots - 1;
  2388. while (last_read_slot > first_slot && !(fs_input_slots &
  2389. (1ull << last_vue_map->slot_to_varying[last_read_slot])))
  2390. --last_read_slot;
  2391. /* The URB read length is the difference of the two, counted in pairs. */
  2392. *out_length = DIV_ROUND_UP(last_read_slot - first_slot + 1, 2);
  2393. }
  2394. static void
  2395. iris_emit_sbe_swiz(struct iris_batch *batch,
  2396. const struct iris_context *ice,
  2397. unsigned urb_read_offset,
  2398. unsigned sprite_coord_enables)
  2399. {
  2400. struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attr_overrides[16] = {};
  2401. const struct brw_wm_prog_data *wm_prog_data = (void *)
  2402. ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
  2403. const struct brw_vue_map *vue_map = ice->shaders.last_vue_map;
  2404. const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
  2405. /* XXX: this should be generated when putting programs in place */
  2406. // XXX: raster->sprite_coord_enable
  2407. for (int fs_attr = 0; fs_attr < VARYING_SLOT_MAX; fs_attr++) {
  2408. const int input_index = wm_prog_data->urb_setup[fs_attr];
  2409. if (input_index < 0 || input_index >= 16)
  2410. continue;
  2411. struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr =
  2412. &attr_overrides[input_index];
  2413. int slot = vue_map->varying_to_slot[fs_attr];
  2414. /* Viewport and Layer are stored in the VUE header. We need to override
  2415. * them to zero if earlier stages didn't write them, as GL requires that
  2416. * they read back as zero when not explicitly set.
  2417. */
  2418. switch (fs_attr) {
  2419. case VARYING_SLOT_VIEWPORT:
  2420. case VARYING_SLOT_LAYER:
  2421. attr->ComponentOverrideX = true;
  2422. attr->ComponentOverrideW = true;
  2423. attr->ConstantSource = CONST_0000;
  2424. if (!(vue_map->slots_valid & VARYING_BIT_LAYER))
  2425. attr->ComponentOverrideY = true;
  2426. if (!(vue_map->slots_valid & VARYING_BIT_VIEWPORT))
  2427. attr->ComponentOverrideZ = true;
  2428. continue;
  2429. case VARYING_SLOT_PRIMITIVE_ID:
  2430. /* Override if the previous shader stage didn't write gl_PrimitiveID. */
  2431. if (slot == -1) {
  2432. attr->ComponentOverrideX = true;
  2433. attr->ComponentOverrideY = true;
  2434. attr->ComponentOverrideZ = true;
  2435. attr->ComponentOverrideW = true;
  2436. attr->ConstantSource = PRIM_ID;
  2437. continue;
  2438. }
  2439. default:
  2440. break;
  2441. }
  2442. if (sprite_coord_enables & (1 << input_index))
  2443. continue;
  2444. /* If there was only a back color written but not front, use back
  2445. * as the color instead of undefined.
  2446. */
  2447. if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
  2448. slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
  2449. if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
  2450. slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
  2451. /* Not written by the previous stage - undefined. */
  2452. if (slot == -1) {
  2453. attr->ComponentOverrideX = true;
  2454. attr->ComponentOverrideY = true;
  2455. attr->ComponentOverrideZ = true;
  2456. attr->ComponentOverrideW = true;
  2457. attr->ConstantSource = CONST_0001_FLOAT;
  2458. continue;
  2459. }
  2460. /* Compute the location of the attribute relative to the read offset,
  2461. * which is counted in 256-bit increments (two 128-bit VUE slots).
  2462. */
  2463. const int source_attr = slot - 2 * urb_read_offset;
  2464. assert(source_attr >= 0 && source_attr <= 32);
  2465. attr->SourceAttribute = source_attr;
  2466. /* If we are doing two-sided color, and the VUE slot following this one
  2467. * represents a back-facing color, then we need to instruct the SF unit
  2468. * to do back-facing swizzling.
  2469. */
  2470. if (cso_rast->light_twoside &&
  2471. ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
  2472. vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
  2473. (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
  2474. vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1)))
  2475. attr->SwizzleSelect = INPUTATTR_FACING;
  2476. }
  2477. iris_emit_cmd(batch, GENX(3DSTATE_SBE_SWIZ), sbes) {
  2478. for (int i = 0; i < 16; i++)
  2479. sbes.Attribute[i] = attr_overrides[i];
  2480. }
  2481. }
  2482. static unsigned
  2483. iris_calculate_point_sprite_overrides(const struct brw_wm_prog_data *prog_data,
  2484. const struct iris_rasterizer_state *cso)
  2485. {
  2486. unsigned overrides = 0;
  2487. if (prog_data->urb_setup[VARYING_SLOT_PNTC] != -1)
  2488. overrides |= 1 << prog_data->urb_setup[VARYING_SLOT_PNTC];
  2489. for (int i = 0; i < 8; i++) {
  2490. if ((cso->sprite_coord_enable & (1 << i)) &&
  2491. prog_data->urb_setup[VARYING_SLOT_TEX0 + i] != -1)
  2492. overrides |= 1 << prog_data->urb_setup[VARYING_SLOT_TEX0 + i];
  2493. }
  2494. return overrides;
  2495. }
  2496. static void
  2497. iris_emit_sbe(struct iris_batch *batch, const struct iris_context *ice)
  2498. {
  2499. const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
  2500. const struct brw_wm_prog_data *wm_prog_data = (void *)
  2501. ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
  2502. const struct shader_info *fs_info =
  2503. iris_get_shader_info(ice, MESA_SHADER_FRAGMENT);
  2504. unsigned urb_read_offset, urb_read_length;
  2505. iris_compute_sbe_urb_read_interval(fs_info->inputs_read,
  2506. ice->shaders.last_vue_map,
  2507. cso_rast->light_twoside,
  2508. &urb_read_offset, &urb_read_length);
  2509. unsigned sprite_coord_overrides =
  2510. iris_calculate_point_sprite_overrides(wm_prog_data, cso_rast);
  2511. iris_emit_cmd(batch, GENX(3DSTATE_SBE), sbe) {
  2512. sbe.AttributeSwizzleEnable = true;
  2513. sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
  2514. sbe.PointSpriteTextureCoordinateOrigin = cso_rast->sprite_coord_mode;
  2515. sbe.VertexURBEntryReadOffset = urb_read_offset;
  2516. sbe.VertexURBEntryReadLength = urb_read_length;
  2517. sbe.ForceVertexURBEntryReadOffset = true;
  2518. sbe.ForceVertexURBEntryReadLength = true;
  2519. sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
  2520. sbe.PointSpriteTextureCoordinateEnable = sprite_coord_overrides;
  2521. for (int i = 0; i < 32; i++) {
  2522. sbe.AttributeActiveComponentFormat[i] = ACTIVE_COMPONENT_XYZW;
  2523. }
  2524. }
  2525. iris_emit_sbe_swiz(batch, ice, urb_read_offset, sprite_coord_overrides);
  2526. }
  2527. /* ------------------------------------------------------------------- */
  2528. /**
  2529. * Set sampler-related program key fields based on the current state.
  2530. */
  2531. static void
  2532. iris_populate_sampler_key(const struct iris_context *ice,
  2533. struct brw_sampler_prog_key_data *key)
  2534. {
  2535. for (int i = 0; i < MAX_SAMPLERS; i++) {
  2536. key->swizzles[i] = 0x688; /* XYZW */
  2537. }
  2538. }
  2539. /**
  2540. * Populate VS program key fields based on the current state.
  2541. */
  2542. static void
  2543. iris_populate_vs_key(const struct iris_context *ice,
  2544. struct brw_vs_prog_key *key)
  2545. {
  2546. iris_populate_sampler_key(ice, &key->tex);
  2547. }
  2548. /**
  2549. * Populate TCS program key fields based on the current state.
  2550. */
  2551. static void
  2552. iris_populate_tcs_key(const struct iris_context *ice,
  2553. struct brw_tcs_prog_key *key)
  2554. {
  2555. iris_populate_sampler_key(ice, &key->tex);
  2556. }
  2557. /**
  2558. * Populate TES program key fields based on the current state.
  2559. */
  2560. static void
  2561. iris_populate_tes_key(const struct iris_context *ice,
  2562. struct brw_tes_prog_key *key)
  2563. {
  2564. iris_populate_sampler_key(ice, &key->tex);
  2565. }
  2566. /**
  2567. * Populate GS program key fields based on the current state.
  2568. */
  2569. static void
  2570. iris_populate_gs_key(const struct iris_context *ice,
  2571. struct brw_gs_prog_key *key)
  2572. {
  2573. iris_populate_sampler_key(ice, &key->tex);
  2574. }
  2575. /**
  2576. * Populate FS program key fields based on the current state.
  2577. */
  2578. static void
  2579. iris_populate_fs_key(const struct iris_context *ice,
  2580. struct brw_wm_prog_key *key)
  2581. {
  2582. iris_populate_sampler_key(ice, &key->tex);
  2583. /* XXX: dirty flags? */
  2584. const struct pipe_framebuffer_state *fb = &ice->state.framebuffer;
  2585. const struct iris_depth_stencil_alpha_state *zsa = ice->state.cso_zsa;
  2586. const struct iris_rasterizer_state *rast = ice->state.cso_rast;
  2587. const struct iris_blend_state *blend = ice->state.cso_blend;
  2588. key->nr_color_regions = fb->nr_cbufs;
  2589. key->clamp_fragment_color = rast->clamp_fragment_color;
  2590. key->replicate_alpha = fb->nr_cbufs > 1 &&
  2591. (zsa->alpha.enabled || blend->alpha_to_coverage);
  2592. /* XXX: only bother if COL0/1 are read */
  2593. key->flat_shade = rast->flatshade;
  2594. key->persample_interp = rast->force_persample_interp;
  2595. key->multisample_fbo = rast->multisample && fb->samples > 1;
  2596. key->coherent_fb_fetch = true;
  2597. // XXX: uint64_t input_slots_valid; - for >16 inputs
  2598. // XXX: key->force_dual_color_blend for unigine
  2599. // XXX: respect hint for high_quality_derivatives:1;
  2600. }
  2601. static void
  2602. iris_populate_cs_key(const struct iris_context *ice,
  2603. struct brw_cs_prog_key *key)
  2604. {
  2605. iris_populate_sampler_key(ice, &key->tex);
  2606. }
  2607. #if 0
  2608. // XXX: these need to go in INIT_THREAD_DISPATCH_FIELDS
  2609. pkt.SamplerCount = \
  2610. DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
  2611. #endif
  2612. static uint64_t
  2613. KSP(const struct iris_compiled_shader *shader)
  2614. {
  2615. struct iris_resource *res = (void *) shader->assembly.res;
  2616. return iris_bo_offset_from_base_address(res->bo) + shader->assembly.offset;
  2617. }
  2618. // Gen11 workaround table #2056 WABTPPrefetchDisable suggests to disable
  2619. // prefetching of binding tables in A0 and B0 steppings. XXX: Revisit
  2620. // this WA on C0 stepping.
  2621. #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix, stage) \
  2622. pkt.KernelStartPointer = KSP(shader); \
  2623. pkt.BindingTableEntryCount = GEN_GEN == 11 ? 0 : \
  2624. prog_data->binding_table.size_bytes / 4; \
  2625. pkt.FloatingPointMode = prog_data->use_alt_mode; \
  2626. \
  2627. pkt.DispatchGRFStartRegisterForURBData = \
  2628. prog_data->dispatch_grf_start_reg; \
  2629. pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
  2630. pkt.prefix##URBEntryReadOffset = 0; \
  2631. \
  2632. pkt.StatisticsEnable = true; \
  2633. pkt.Enable = true; \
  2634. \
  2635. if (prog_data->total_scratch) { \
  2636. uint32_t scratch_addr = \
  2637. iris_get_scratch_space(ice, prog_data->total_scratch, stage); \
  2638. pkt.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11; \
  2639. pkt.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr); \
  2640. }
  2641. /**
  2642. * Encode most of 3DSTATE_VS based on the compiled shader.
  2643. */
  2644. static void
  2645. iris_store_vs_state(struct iris_context *ice,
  2646. const struct gen_device_info *devinfo,
  2647. struct iris_compiled_shader *shader)
  2648. {
  2649. struct brw_stage_prog_data *prog_data = shader->prog_data;
  2650. struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
  2651. iris_pack_command(GENX(3DSTATE_VS), shader->derived_data, vs) {
  2652. INIT_THREAD_DISPATCH_FIELDS(vs, Vertex, MESA_SHADER_VERTEX);
  2653. vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
  2654. vs.SIMD8DispatchEnable = true;
  2655. vs.UserClipDistanceCullTestEnableBitmask =
  2656. vue_prog_data->cull_distance_mask;
  2657. }
  2658. }
  2659. /**
  2660. * Encode most of 3DSTATE_HS based on the compiled shader.
  2661. */
  2662. static void
  2663. iris_store_tcs_state(struct iris_context *ice,
  2664. const struct gen_device_info *devinfo,
  2665. struct iris_compiled_shader *shader)
  2666. {
  2667. struct brw_stage_prog_data *prog_data = shader->prog_data;
  2668. struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
  2669. struct brw_tcs_prog_data *tcs_prog_data = (void *) prog_data;
  2670. iris_pack_command(GENX(3DSTATE_HS), shader->derived_data, hs) {
  2671. INIT_THREAD_DISPATCH_FIELDS(hs, Vertex, MESA_SHADER_TESS_CTRL);
  2672. hs.InstanceCount = tcs_prog_data->instances - 1;
  2673. hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
  2674. hs.IncludeVertexHandles = true;
  2675. }
  2676. }
  2677. /**
  2678. * Encode 3DSTATE_TE and most of 3DSTATE_DS based on the compiled shader.
  2679. */
  2680. static void
  2681. iris_store_tes_state(struct iris_context *ice,
  2682. const struct gen_device_info *devinfo,
  2683. struct iris_compiled_shader *shader)
  2684. {
  2685. struct brw_stage_prog_data *prog_data = shader->prog_data;
  2686. struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
  2687. struct brw_tes_prog_data *tes_prog_data = (void *) prog_data;
  2688. uint32_t *te_state = (void *) shader->derived_data;
  2689. uint32_t *ds_state = te_state + GENX(3DSTATE_TE_length);
  2690. iris_pack_command(GENX(3DSTATE_TE), te_state, te) {
  2691. te.Partitioning = tes_prog_data->partitioning;
  2692. te.OutputTopology = tes_prog_data->output_topology;
  2693. te.TEDomain = tes_prog_data->domain;
  2694. te.TEEnable = true;
  2695. te.MaximumTessellationFactorOdd = 63.0;
  2696. te.MaximumTessellationFactorNotOdd = 64.0;
  2697. }
  2698. iris_pack_command(GENX(3DSTATE_DS), ds_state, ds) {
  2699. INIT_THREAD_DISPATCH_FIELDS(ds, Patch, MESA_SHADER_TESS_EVAL);
  2700. ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
  2701. ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
  2702. ds.ComputeWCoordinateEnable =
  2703. tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
  2704. ds.UserClipDistanceCullTestEnableBitmask =
  2705. vue_prog_data->cull_distance_mask;
  2706. }
  2707. }
  2708. /**
  2709. * Encode most of 3DSTATE_GS based on the compiled shader.
  2710. */
  2711. static void
  2712. iris_store_gs_state(struct iris_context *ice,
  2713. const struct gen_device_info *devinfo,
  2714. struct iris_compiled_shader *shader)
  2715. {
  2716. struct brw_stage_prog_data *prog_data = shader->prog_data;
  2717. struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
  2718. struct brw_gs_prog_data *gs_prog_data = (void *) prog_data;
  2719. iris_pack_command(GENX(3DSTATE_GS), shader->derived_data, gs) {
  2720. INIT_THREAD_DISPATCH_FIELDS(gs, Vertex, MESA_SHADER_GEOMETRY);
  2721. gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
  2722. gs.OutputTopology = gs_prog_data->output_topology;
  2723. gs.ControlDataHeaderSize =
  2724. gs_prog_data->control_data_header_size_hwords;
  2725. gs.InstanceControl = gs_prog_data->invocations - 1;
  2726. gs.DispatchMode = DISPATCH_MODE_SIMD8;
  2727. gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
  2728. gs.ControlDataFormat = gs_prog_data->control_data_format;
  2729. gs.ReorderMode = TRAILING;
  2730. gs.ExpectedVertexCount = gs_prog_data->vertices_in;
  2731. gs.MaximumNumberofThreads =
  2732. GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
  2733. : (devinfo->max_gs_threads - 1);
  2734. if (gs_prog_data->static_vertex_count != -1) {
  2735. gs.StaticOutput = true;
  2736. gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
  2737. }
  2738. gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
  2739. gs.UserClipDistanceCullTestEnableBitmask =
  2740. vue_prog_data->cull_distance_mask;
  2741. const int urb_entry_write_offset = 1;
  2742. const uint32_t urb_entry_output_length =
  2743. DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
  2744. urb_entry_write_offset;
  2745. gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
  2746. gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
  2747. }
  2748. }
  2749. /**
  2750. * Encode most of 3DSTATE_PS and 3DSTATE_PS_EXTRA based on the shader.
  2751. */
  2752. static void
  2753. iris_store_fs_state(struct iris_context *ice,
  2754. const struct gen_device_info *devinfo,
  2755. struct iris_compiled_shader *shader)
  2756. {
  2757. struct brw_stage_prog_data *prog_data = shader->prog_data;
  2758. struct brw_wm_prog_data *wm_prog_data = (void *) shader->prog_data;
  2759. uint32_t *ps_state = (void *) shader->derived_data;
  2760. uint32_t *psx_state = ps_state + GENX(3DSTATE_PS_length);
  2761. iris_pack_command(GENX(3DSTATE_PS), ps_state, ps) {
  2762. ps.VectorMaskEnable = true;
  2763. //ps.SamplerCount = ...
  2764. // XXX: WABTPPrefetchDisable, see above, drop at C0
  2765. ps.BindingTableEntryCount = GEN_GEN == 11 ? 0 :
  2766. prog_data->binding_table.size_bytes / 4;
  2767. ps.FloatingPointMode = prog_data->use_alt_mode;
  2768. ps.MaximumNumberofThreadsPerPSD = 64 - (GEN_GEN == 8 ? 2 : 1);
  2769. ps.PushConstantEnable = prog_data->nr_params > 0 ||
  2770. prog_data->ubo_ranges[0].length > 0;
  2771. /* From the documentation for this packet:
  2772. * "If the PS kernel does not need the Position XY Offsets to
  2773. * compute a Position Value, then this field should be programmed
  2774. * to POSOFFSET_NONE."
  2775. *
  2776. * "SW Recommendation: If the PS kernel needs the Position Offsets
  2777. * to compute a Position XY value, this field should match Position
  2778. * ZW Interpolation Mode to ensure a consistent position.xyzw
  2779. * computation."
  2780. *
  2781. * We only require XY sample offsets. So, this recommendation doesn't
  2782. * look useful at the moment. We might need this in future.
  2783. */
  2784. ps.PositionXYOffsetSelect =
  2785. wm_prog_data->uses_pos_offset ? POSOFFSET_SAMPLE : POSOFFSET_NONE;
  2786. ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
  2787. ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
  2788. ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
  2789. // XXX: Disable SIMD32 with 16x MSAA
  2790. ps.DispatchGRFStartRegisterForConstantSetupData0 =
  2791. brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
  2792. ps.DispatchGRFStartRegisterForConstantSetupData1 =
  2793. brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
  2794. ps.DispatchGRFStartRegisterForConstantSetupData2 =
  2795. brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
  2796. ps.KernelStartPointer0 =
  2797. KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
  2798. ps.KernelStartPointer1 =
  2799. KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
  2800. ps.KernelStartPointer2 =
  2801. KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
  2802. if (prog_data->total_scratch) {
  2803. uint32_t scratch_addr =
  2804. iris_get_scratch_space(ice, prog_data->total_scratch,
  2805. MESA_SHADER_FRAGMENT);
  2806. ps.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;
  2807. ps.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);
  2808. }
  2809. }
  2810. iris_pack_command(GENX(3DSTATE_PS_EXTRA), psx_state, psx) {
  2811. psx.PixelShaderValid = true;
  2812. psx.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
  2813. psx.PixelShaderKillsPixel = wm_prog_data->uses_kill;
  2814. psx.AttributeEnable = wm_prog_data->num_varying_inputs != 0;
  2815. psx.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
  2816. psx.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
  2817. psx.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
  2818. if (wm_prog_data->uses_sample_mask) {
  2819. /* TODO: conservative rasterization */
  2820. if (wm_prog_data->post_depth_coverage)
  2821. psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
  2822. else
  2823. psx.InputCoverageMaskState = ICMS_NORMAL;
  2824. }
  2825. psx.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
  2826. psx.PixelShaderPullsBary = wm_prog_data->pulls_bary;
  2827. psx.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
  2828. // XXX: UAV bit
  2829. }
  2830. }
  2831. /**
  2832. * Compute the size of the derived data (shader command packets).
  2833. *
  2834. * This must match the data written by the iris_store_xs_state() functions.
  2835. */
  2836. static void
  2837. iris_store_cs_state(struct iris_context *ice,
  2838. const struct gen_device_info *devinfo,
  2839. struct iris_compiled_shader *shader)
  2840. {
  2841. struct brw_stage_prog_data *prog_data = shader->prog_data;
  2842. struct brw_cs_prog_data *cs_prog_data = (void *) shader->prog_data;
  2843. void *map = shader->derived_data;
  2844. iris_pack_state(GENX(INTERFACE_DESCRIPTOR_DATA), map, desc) {
  2845. desc.KernelStartPointer = KSP(shader);
  2846. desc.ConstantURBEntryReadLength = cs_prog_data->push.per_thread.regs;
  2847. desc.NumberofThreadsinGPGPUThreadGroup = cs_prog_data->threads;
  2848. desc.SharedLocalMemorySize =
  2849. encode_slm_size(GEN_GEN, prog_data->total_shared);
  2850. desc.BarrierEnable = cs_prog_data->uses_barrier;
  2851. desc.CrossThreadConstantDataReadLength =
  2852. cs_prog_data->push.cross_thread.regs;
  2853. }
  2854. }
  2855. static unsigned
  2856. iris_derived_program_state_size(enum iris_program_cache_id cache_id)
  2857. {
  2858. assert(cache_id <= IRIS_CACHE_BLORP);
  2859. static const unsigned dwords[] = {
  2860. [IRIS_CACHE_VS] = GENX(3DSTATE_VS_length),
  2861. [IRIS_CACHE_TCS] = GENX(3DSTATE_HS_length),
  2862. [IRIS_CACHE_TES] = GENX(3DSTATE_TE_length) + GENX(3DSTATE_DS_length),
  2863. [IRIS_CACHE_GS] = GENX(3DSTATE_GS_length),
  2864. [IRIS_CACHE_FS] =
  2865. GENX(3DSTATE_PS_length) + GENX(3DSTATE_PS_EXTRA_length),
  2866. [IRIS_CACHE_CS] = GENX(INTERFACE_DESCRIPTOR_DATA_length),
  2867. [IRIS_CACHE_BLORP] = 0,
  2868. };
  2869. return sizeof(uint32_t) * dwords[cache_id];
  2870. }
  2871. /**
  2872. * Create any state packets corresponding to the given shader stage
  2873. * (i.e. 3DSTATE_VS) and save them as "derived data" in the shader variant.
  2874. * This means that we can look up a program in the in-memory cache and
  2875. * get most of the state packet without having to reconstruct it.
  2876. */
  2877. static void
  2878. iris_store_derived_program_state(struct iris_context *ice,
  2879. enum iris_program_cache_id cache_id,
  2880. struct iris_compiled_shader *shader)
  2881. {
  2882. struct iris_screen *screen = (void *) ice->ctx.screen;
  2883. const struct gen_device_info *devinfo = &screen->devinfo;
  2884. switch (cache_id) {
  2885. case IRIS_CACHE_VS:
  2886. iris_store_vs_state(ice, devinfo, shader);
  2887. break;
  2888. case IRIS_CACHE_TCS:
  2889. iris_store_tcs_state(ice, devinfo, shader);
  2890. break;
  2891. case IRIS_CACHE_TES:
  2892. iris_store_tes_state(ice, devinfo, shader);
  2893. break;
  2894. case IRIS_CACHE_GS:
  2895. iris_store_gs_state(ice, devinfo, shader);
  2896. break;
  2897. case IRIS_CACHE_FS:
  2898. iris_store_fs_state(ice, devinfo, shader);
  2899. break;
  2900. case IRIS_CACHE_CS:
  2901. iris_store_cs_state(ice, devinfo, shader);
  2902. case IRIS_CACHE_BLORP:
  2903. break;
  2904. default:
  2905. break;
  2906. }
  2907. }
  2908. /* ------------------------------------------------------------------- */
  2909. /**
  2910. * Configure the URB.
  2911. *
  2912. * XXX: write a real comment.
  2913. */
  2914. static void
  2915. iris_upload_urb_config(struct iris_context *ice, struct iris_batch *batch)
  2916. {
  2917. const struct gen_device_info *devinfo = &batch->screen->devinfo;
  2918. const unsigned push_size_kB = 32;
  2919. unsigned entries[4];
  2920. unsigned start[4];
  2921. unsigned size[4];
  2922. for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
  2923. if (!ice->shaders.prog[i]) {
  2924. size[i] = 1;
  2925. } else {
  2926. struct brw_vue_prog_data *vue_prog_data =
  2927. (void *) ice->shaders.prog[i]->prog_data;
  2928. size[i] = vue_prog_data->urb_entry_size;
  2929. }
  2930. assert(size[i] != 0);
  2931. }
  2932. gen_get_urb_config(devinfo, 1024 * push_size_kB,
  2933. 1024 * ice->shaders.urb_size,
  2934. ice->shaders.prog[MESA_SHADER_TESS_EVAL] != NULL,
  2935. ice->shaders.prog[MESA_SHADER_GEOMETRY] != NULL,
  2936. size, entries, start);
  2937. for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
  2938. iris_emit_cmd(batch, GENX(3DSTATE_URB_VS), urb) {
  2939. urb._3DCommandSubOpcode += i;
  2940. urb.VSURBStartingAddress = start[i];
  2941. urb.VSURBEntryAllocationSize = size[i] - 1;
  2942. urb.VSNumberofURBEntries = entries[i];
  2943. }
  2944. }
  2945. }
  2946. static const uint32_t push_constant_opcodes[] = {
  2947. [MESA_SHADER_VERTEX] = 21,
  2948. [MESA_SHADER_TESS_CTRL] = 25, /* HS */
  2949. [MESA_SHADER_TESS_EVAL] = 26, /* DS */
  2950. [MESA_SHADER_GEOMETRY] = 22,
  2951. [MESA_SHADER_FRAGMENT] = 23,
  2952. [MESA_SHADER_COMPUTE] = 0,
  2953. };
  2954. static uint32_t
  2955. use_null_surface(struct iris_batch *batch, struct iris_context *ice)
  2956. {
  2957. struct iris_bo *state_bo = iris_resource_bo(ice->state.unbound_tex.res);
  2958. iris_use_pinned_bo(batch, state_bo, false);
  2959. return ice->state.unbound_tex.offset;
  2960. }
  2961. static uint32_t
  2962. use_null_fb_surface(struct iris_batch *batch, struct iris_context *ice)
  2963. {
  2964. /* If set_framebuffer_state() was never called, fall back to 1x1x1 */
  2965. if (!ice->state.null_fb.res)
  2966. return use_null_surface(batch, ice);
  2967. struct iris_bo *state_bo = iris_resource_bo(ice->state.null_fb.res);
  2968. iris_use_pinned_bo(batch, state_bo, false);
  2969. return ice->state.null_fb.offset;
  2970. }
  2971. /**
  2972. * Add a surface to the validation list, as well as the buffer containing
  2973. * the corresponding SURFACE_STATE.
  2974. *
  2975. * Returns the binding table entry (offset to SURFACE_STATE).
  2976. */
  2977. static uint32_t
  2978. use_surface(struct iris_batch *batch,
  2979. struct pipe_surface *p_surf,
  2980. bool writeable)
  2981. {
  2982. struct iris_surface *surf = (void *) p_surf;
  2983. iris_use_pinned_bo(batch, iris_resource_bo(p_surf->texture), writeable);
  2984. iris_use_pinned_bo(batch, iris_resource_bo(surf->surface_state.res), false);
  2985. return surf->surface_state.offset;
  2986. }
  2987. static uint32_t
  2988. use_sampler_view(struct iris_batch *batch, struct iris_sampler_view *isv)
  2989. {
  2990. iris_use_pinned_bo(batch, isv->res->bo, false);
  2991. iris_use_pinned_bo(batch, iris_resource_bo(isv->surface_state.res), false);
  2992. return isv->surface_state.offset;
  2993. }
  2994. static uint32_t
  2995. use_const_buffer(struct iris_batch *batch,
  2996. struct iris_context *ice,
  2997. struct iris_const_buffer *cbuf)
  2998. {
  2999. if (!cbuf->surface_state.res)
  3000. return use_null_surface(batch, ice);
  3001. iris_use_pinned_bo(batch, iris_resource_bo(cbuf->data.res), false);
  3002. iris_use_pinned_bo(batch, iris_resource_bo(cbuf->surface_state.res), false);
  3003. return cbuf->surface_state.offset;
  3004. }
  3005. static uint32_t
  3006. use_ssbo(struct iris_batch *batch, struct iris_context *ice,
  3007. struct iris_shader_state *shs, int i)
  3008. {
  3009. if (!shs->ssbo[i])
  3010. return use_null_surface(batch, ice);
  3011. struct iris_state_ref *surf_state = &shs->ssbo_surface_state[i];
  3012. iris_use_pinned_bo(batch, iris_resource_bo(shs->ssbo[i]), true);
  3013. iris_use_pinned_bo(batch, iris_resource_bo(surf_state->res), false);
  3014. return surf_state->offset;
  3015. }
  3016. static uint32_t
  3017. use_image(struct iris_batch *batch, struct iris_context *ice,
  3018. struct iris_shader_state *shs, int i)
  3019. {
  3020. if (!shs->image[i].res)
  3021. return use_null_surface(batch, ice);
  3022. struct iris_state_ref *surf_state = &shs->image[i].surface_state;
  3023. iris_use_pinned_bo(batch, iris_resource_bo(shs->image[i].res),
  3024. shs->image[i].access & PIPE_IMAGE_ACCESS_WRITE);
  3025. iris_use_pinned_bo(batch, iris_resource_bo(surf_state->res), false);
  3026. return surf_state->offset;
  3027. }
  3028. #define push_bt_entry(addr) \
  3029. assert(addr >= binder_addr); \
  3030. if (!pin_only) bt_map[s++] = (addr) - binder_addr;
  3031. /**
  3032. * Populate the binding table for a given shader stage.
  3033. *
  3034. * This fills out the table of pointers to surfaces required by the shader,
  3035. * and also adds those buffers to the validation list so the kernel can make
  3036. * resident before running our batch.
  3037. */
  3038. static void
  3039. iris_populate_binding_table(struct iris_context *ice,
  3040. struct iris_batch *batch,
  3041. gl_shader_stage stage,
  3042. bool pin_only)
  3043. {
  3044. const struct iris_binder *binder = &ice->state.binder;
  3045. struct iris_compiled_shader *shader = ice->shaders.prog[stage];
  3046. if (!shader)
  3047. return;
  3048. struct iris_shader_state *shs = &ice->state.shaders[stage];
  3049. uint32_t binder_addr = binder->bo->gtt_offset;
  3050. //struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
  3051. uint32_t *bt_map = binder->map + binder->bt_offset[stage];
  3052. int s = 0;
  3053. const struct shader_info *info = iris_get_shader_info(ice, stage);
  3054. if (!info) {
  3055. /* TCS passthrough doesn't need a binding table. */
  3056. assert(stage == MESA_SHADER_TESS_CTRL);
  3057. return;
  3058. }
  3059. if (stage == MESA_SHADER_COMPUTE) {
  3060. /* surface for gl_NumWorkGroups */
  3061. struct iris_state_ref *grid_data = &ice->state.grid_size;
  3062. struct iris_state_ref *grid_state = &ice->state.grid_surf_state;
  3063. iris_use_pinned_bo(batch, iris_resource_bo(grid_data->res), false);
  3064. iris_use_pinned_bo(batch, iris_resource_bo(grid_state->res), false);
  3065. push_bt_entry(grid_state->offset);
  3066. }
  3067. if (stage == MESA_SHADER_FRAGMENT) {
  3068. struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
  3069. /* Note that cso_fb->nr_cbufs == fs_key->nr_color_regions. */
  3070. if (cso_fb->nr_cbufs) {
  3071. for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
  3072. uint32_t addr =
  3073. cso_fb->cbufs[i] ? use_surface(batch, cso_fb->cbufs[i], true)
  3074. : use_null_fb_surface(batch, ice);
  3075. push_bt_entry(addr);
  3076. }
  3077. } else {
  3078. uint32_t addr = use_null_fb_surface(batch, ice);
  3079. push_bt_entry(addr);
  3080. }
  3081. }
  3082. //assert(prog_data->binding_table.texture_start ==
  3083. //(ice->state.num_textures[stage] ? s : 0xd0d0d0d0));
  3084. for (int i = 0; i < shs->num_textures; i++) {
  3085. struct iris_sampler_view *view = shs->textures[i];
  3086. uint32_t addr = view ? use_sampler_view(batch, view)
  3087. : use_null_surface(batch, ice);
  3088. push_bt_entry(addr);
  3089. }
  3090. for (int i = 0; i < info->num_images; i++) {
  3091. uint32_t addr = use_image(batch, ice, shs, i);
  3092. push_bt_entry(addr);
  3093. }
  3094. const int num_ubos = iris_get_shader_num_ubos(ice, stage);
  3095. for (int i = 0; i < num_ubos; i++) {
  3096. uint32_t addr = use_const_buffer(batch, ice, &shs->constbuf[i]);
  3097. push_bt_entry(addr);
  3098. }
  3099. /* XXX: st is wasting 16 binding table slots for ABOs. Should add a cap
  3100. * for changing nir_lower_atomics_to_ssbos setting and buffer_base offset
  3101. * in st_atom_storagebuf.c so it'll compact them into one range, with
  3102. * SSBOs starting at info->num_abos. Ideally it'd reset num_abos to 0 too
  3103. */
  3104. if (info->num_abos + info->num_ssbos > 0) {
  3105. for (int i = 0; i < IRIS_MAX_ABOS + info->num_ssbos; i++) {
  3106. uint32_t addr = use_ssbo(batch, ice, shs, i);
  3107. push_bt_entry(addr);
  3108. }
  3109. }
  3110. #if 0
  3111. // XXX: not implemented yet
  3112. assert(prog_data->binding_table.plane_start[1] == 0xd0d0d0d0);
  3113. assert(prog_data->binding_table.plane_start[2] == 0xd0d0d0d0);
  3114. #endif
  3115. }
  3116. static void
  3117. iris_use_optional_res(struct iris_batch *batch,
  3118. struct pipe_resource *res,
  3119. bool writeable)
  3120. {
  3121. if (res) {
  3122. struct iris_bo *bo = iris_resource_bo(res);
  3123. iris_use_pinned_bo(batch, bo, writeable);
  3124. }
  3125. }
  3126. /* ------------------------------------------------------------------- */
  3127. /**
  3128. * Pin any BOs which were installed by a previous batch, and restored
  3129. * via the hardware logical context mechanism.
  3130. *
  3131. * We don't need to re-emit all state every batch - the hardware context
  3132. * mechanism will save and restore it for us. This includes pointers to
  3133. * various BOs...which won't exist unless we ask the kernel to pin them
  3134. * by adding them to the validation list.
  3135. *
  3136. * We can skip buffers if we've re-emitted those packets, as we're
  3137. * overwriting those stale pointers with new ones, and don't actually
  3138. * refer to the old BOs.
  3139. */
  3140. static void
  3141. iris_restore_render_saved_bos(struct iris_context *ice,
  3142. struct iris_batch *batch,
  3143. const struct pipe_draw_info *draw)
  3144. {
  3145. // XXX: whack IRIS_SHADER_DIRTY_BINDING_TABLE on new batch
  3146. const uint64_t clean = ~ice->state.dirty;
  3147. if (clean & IRIS_DIRTY_CC_VIEWPORT) {
  3148. iris_use_optional_res(batch, ice->state.last_res.cc_vp, false);
  3149. }
  3150. if (clean & IRIS_DIRTY_SF_CL_VIEWPORT) {
  3151. iris_use_optional_res(batch, ice->state.last_res.sf_cl_vp, false);
  3152. }
  3153. if (clean & IRIS_DIRTY_BLEND_STATE) {
  3154. iris_use_optional_res(batch, ice->state.last_res.blend, false);
  3155. }
  3156. if (clean & IRIS_DIRTY_COLOR_CALC_STATE) {
  3157. iris_use_optional_res(batch, ice->state.last_res.color_calc, false);
  3158. }
  3159. if (clean & IRIS_DIRTY_SCISSOR_RECT) {
  3160. iris_use_optional_res(batch, ice->state.last_res.scissor, false);
  3161. }
  3162. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3163. if (!(clean & (IRIS_DIRTY_CONSTANTS_VS << stage)))
  3164. continue;
  3165. struct iris_shader_state *shs = &ice->state.shaders[stage];
  3166. struct iris_compiled_shader *shader = ice->shaders.prog[stage];
  3167. if (!shader)
  3168. continue;
  3169. struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
  3170. for (int i = 0; i < 4; i++) {
  3171. const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
  3172. if (range->length == 0)
  3173. continue;
  3174. struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
  3175. struct iris_resource *res = (void *) cbuf->data.res;
  3176. if (res)
  3177. iris_use_pinned_bo(batch, res->bo, false);
  3178. else
  3179. iris_use_pinned_bo(batch, batch->screen->workaround_bo, false);
  3180. }
  3181. }
  3182. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3183. if (clean & (IRIS_DIRTY_BINDINGS_VS << stage)) {
  3184. /* Re-pin any buffers referred to by the binding table. */
  3185. iris_populate_binding_table(ice, batch, stage, true);
  3186. }
  3187. }
  3188. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3189. struct iris_shader_state *shs = &ice->state.shaders[stage];
  3190. struct pipe_resource *res = shs->sampler_table.res;
  3191. if (res)
  3192. iris_use_pinned_bo(batch, iris_resource_bo(res), false);
  3193. }
  3194. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3195. if (clean & (IRIS_DIRTY_VS << stage)) {
  3196. struct iris_compiled_shader *shader = ice->shaders.prog[stage];
  3197. if (shader) {
  3198. struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
  3199. iris_use_pinned_bo(batch, bo, false);
  3200. }
  3201. // XXX: scratch buffer
  3202. }
  3203. }
  3204. if (clean & IRIS_DIRTY_DEPTH_BUFFER) {
  3205. struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
  3206. if (cso_fb->zsbuf) {
  3207. struct iris_resource *zres, *sres;
  3208. iris_get_depth_stencil_resources(cso_fb->zsbuf->texture,
  3209. &zres, &sres);
  3210. // XXX: might not be writable...
  3211. if (zres)
  3212. iris_use_pinned_bo(batch, zres->bo, true);
  3213. if (sres)
  3214. iris_use_pinned_bo(batch, sres->bo, true);
  3215. }
  3216. }
  3217. if (draw->index_size == 0 && ice->state.last_res.index_buffer) {
  3218. /* This draw didn't emit a new index buffer, so we are inheriting the
  3219. * older index buffer. This draw didn't need it, but future ones may.
  3220. */
  3221. struct iris_bo *bo = iris_resource_bo(ice->state.last_res.index_buffer);
  3222. iris_use_pinned_bo(batch, bo, false);
  3223. }
  3224. if (clean & IRIS_DIRTY_VERTEX_BUFFERS) {
  3225. struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
  3226. for (unsigned i = 0; i < cso->num_buffers; i++) {
  3227. struct iris_resource *res = (void *) cso->resources[i];
  3228. iris_use_pinned_bo(batch, res->bo, false);
  3229. }
  3230. }
  3231. }
  3232. static void
  3233. iris_restore_compute_saved_bos(struct iris_context *ice,
  3234. struct iris_batch *batch,
  3235. const struct pipe_grid_info *grid)
  3236. {
  3237. const uint64_t clean = ~ice->state.dirty;
  3238. const int stage = MESA_SHADER_COMPUTE;
  3239. struct iris_shader_state *shs = &ice->state.shaders[stage];
  3240. if (clean & IRIS_DIRTY_CONSTANTS_CS) {
  3241. struct iris_compiled_shader *shader = ice->shaders.prog[stage];
  3242. if (shader) {
  3243. struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
  3244. const struct brw_ubo_range *range = &prog_data->ubo_ranges[0];
  3245. if (range->length > 0) {
  3246. struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
  3247. struct iris_resource *res = (void *) cbuf->data.res;
  3248. if (res)
  3249. iris_use_pinned_bo(batch, res->bo, false);
  3250. else
  3251. iris_use_pinned_bo(batch, batch->screen->workaround_bo, false);
  3252. }
  3253. }
  3254. }
  3255. if (clean & IRIS_DIRTY_BINDINGS_CS) {
  3256. /* Re-pin any buffers referred to by the binding table. */
  3257. iris_populate_binding_table(ice, batch, stage, true);
  3258. }
  3259. struct pipe_resource *sampler_res = shs->sampler_table.res;
  3260. if (sampler_res)
  3261. iris_use_pinned_bo(batch, iris_resource_bo(sampler_res), false);
  3262. if (clean & IRIS_DIRTY_CS) {
  3263. struct iris_compiled_shader *shader = ice->shaders.prog[stage];
  3264. if (shader) {
  3265. struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
  3266. iris_use_pinned_bo(batch, bo, false);
  3267. }
  3268. // XXX: scratch buffer
  3269. }
  3270. }
  3271. /**
  3272. * Possibly emit STATE_BASE_ADDRESS to update Surface State Base Address.
  3273. */
  3274. static void
  3275. iris_update_surface_base_address(struct iris_batch *batch,
  3276. struct iris_binder *binder)
  3277. {
  3278. if (batch->last_surface_base_address == binder->bo->gtt_offset)
  3279. return;
  3280. flush_for_state_base_change(batch);
  3281. iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
  3282. // XXX: sba.SurfaceStateMemoryObjectControlState = MOCS_WB;
  3283. sba.SurfaceStateBaseAddressModifyEnable = true;
  3284. sba.SurfaceStateBaseAddress = ro_bo(binder->bo, 0);
  3285. }
  3286. batch->last_surface_base_address = binder->bo->gtt_offset;
  3287. }
  3288. static void
  3289. iris_upload_dirty_render_state(struct iris_context *ice,
  3290. struct iris_batch *batch,
  3291. const struct pipe_draw_info *draw)
  3292. {
  3293. const uint64_t dirty = ice->state.dirty;
  3294. if (!(dirty & IRIS_ALL_DIRTY_FOR_RENDER))
  3295. return;
  3296. struct iris_genx_state *genx = ice->state.genx;
  3297. struct iris_binder *binder = &ice->state.binder;
  3298. struct brw_wm_prog_data *wm_prog_data = (void *)
  3299. ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
  3300. if (dirty & IRIS_DIRTY_CC_VIEWPORT) {
  3301. const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
  3302. uint32_t cc_vp_address;
  3303. /* XXX: could avoid streaming for depth_clip [0,1] case. */
  3304. uint32_t *cc_vp_map =
  3305. stream_state(batch, ice->state.dynamic_uploader,
  3306. &ice->state.last_res.cc_vp,
  3307. 4 * ice->state.num_viewports *
  3308. GENX(CC_VIEWPORT_length), 32, &cc_vp_address);
  3309. for (int i = 0; i < ice->state.num_viewports; i++) {
  3310. float zmin, zmax;
  3311. util_viewport_zmin_zmax(&ice->state.viewports[i],
  3312. cso_rast->clip_halfz, &zmin, &zmax);
  3313. if (cso_rast->depth_clip_near)
  3314. zmin = 0.0;
  3315. if (cso_rast->depth_clip_far)
  3316. zmax = 1.0;
  3317. iris_pack_state(GENX(CC_VIEWPORT), cc_vp_map, ccv) {
  3318. ccv.MinimumDepth = zmin;
  3319. ccv.MaximumDepth = zmax;
  3320. }
  3321. cc_vp_map += GENX(CC_VIEWPORT_length);
  3322. }
  3323. iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
  3324. ptr.CCViewportPointer = cc_vp_address;
  3325. }
  3326. }
  3327. if (dirty & IRIS_DIRTY_SF_CL_VIEWPORT) {
  3328. iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
  3329. ptr.SFClipViewportPointer =
  3330. emit_state(batch, ice->state.dynamic_uploader,
  3331. &ice->state.last_res.sf_cl_vp,
  3332. genx->sf_cl_vp, 4 * GENX(SF_CLIP_VIEWPORT_length) *
  3333. ice->state.num_viewports, 64);
  3334. }
  3335. }
  3336. /* XXX: L3 State */
  3337. // XXX: this is only flagged at setup, we assume a static configuration
  3338. if (dirty & IRIS_DIRTY_URB) {
  3339. iris_upload_urb_config(ice, batch);
  3340. }
  3341. if (dirty & IRIS_DIRTY_BLEND_STATE) {
  3342. struct iris_blend_state *cso_blend = ice->state.cso_blend;
  3343. struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
  3344. struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
  3345. const int header_dwords = GENX(BLEND_STATE_length);
  3346. const int rt_dwords = cso_fb->nr_cbufs * GENX(BLEND_STATE_ENTRY_length);
  3347. uint32_t blend_offset;
  3348. uint32_t *blend_map =
  3349. stream_state(batch, ice->state.dynamic_uploader,
  3350. &ice->state.last_res.blend,
  3351. 4 * (header_dwords + rt_dwords), 64, &blend_offset);
  3352. uint32_t blend_state_header;
  3353. iris_pack_state(GENX(BLEND_STATE), &blend_state_header, bs) {
  3354. bs.AlphaTestEnable = cso_zsa->alpha.enabled;
  3355. bs.AlphaTestFunction = translate_compare_func(cso_zsa->alpha.func);
  3356. }
  3357. blend_map[0] = blend_state_header | cso_blend->blend_state[0];
  3358. memcpy(&blend_map[1], &cso_blend->blend_state[1], 4 * rt_dwords);
  3359. iris_emit_cmd(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
  3360. ptr.BlendStatePointer = blend_offset;
  3361. ptr.BlendStatePointerValid = true;
  3362. }
  3363. }
  3364. if (dirty & IRIS_DIRTY_COLOR_CALC_STATE) {
  3365. struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
  3366. uint32_t cc_offset;
  3367. void *cc_map =
  3368. stream_state(batch, ice->state.dynamic_uploader,
  3369. &ice->state.last_res.color_calc,
  3370. sizeof(uint32_t) * GENX(COLOR_CALC_STATE_length),
  3371. 64, &cc_offset);
  3372. iris_pack_state(GENX(COLOR_CALC_STATE), cc_map, cc) {
  3373. cc.AlphaTestFormat = ALPHATEST_FLOAT32;
  3374. cc.AlphaReferenceValueAsFLOAT32 = cso->alpha.ref_value;
  3375. cc.BlendConstantColorRed = ice->state.blend_color.color[0];
  3376. cc.BlendConstantColorGreen = ice->state.blend_color.color[1];
  3377. cc.BlendConstantColorBlue = ice->state.blend_color.color[2];
  3378. cc.BlendConstantColorAlpha = ice->state.blend_color.color[3];
  3379. }
  3380. iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
  3381. ptr.ColorCalcStatePointer = cc_offset;
  3382. ptr.ColorCalcStatePointerValid = true;
  3383. }
  3384. }
  3385. /* Upload constants for TCS passthrough. */
  3386. if ((dirty & IRIS_DIRTY_CONSTANTS_TCS) &&
  3387. ice->shaders.prog[MESA_SHADER_TESS_CTRL] &&
  3388. !ice->shaders.uncompiled[MESA_SHADER_TESS_CTRL]) {
  3389. struct iris_compiled_shader *tes_shader = ice->shaders.prog[MESA_SHADER_TESS_EVAL];
  3390. assert(tes_shader);
  3391. /* Passthrough always copies 2 vec4s, so when uploading data we ensure
  3392. * it is in the right layout for TES.
  3393. */
  3394. float hdr[8] = {};
  3395. struct brw_tes_prog_data *tes_prog_data = (void *) tes_shader->prog_data;
  3396. switch (tes_prog_data->domain) {
  3397. case BRW_TESS_DOMAIN_QUAD:
  3398. for (int i = 0; i < 4; i++)
  3399. hdr[7 - i] = ice->state.default_outer_level[i];
  3400. hdr[3] = ice->state.default_inner_level[0];
  3401. hdr[2] = ice->state.default_inner_level[1];
  3402. break;
  3403. case BRW_TESS_DOMAIN_TRI:
  3404. for (int i = 0; i < 3; i++)
  3405. hdr[7 - i] = ice->state.default_outer_level[i];
  3406. hdr[4] = ice->state.default_inner_level[0];
  3407. break;
  3408. case BRW_TESS_DOMAIN_ISOLINE:
  3409. hdr[7] = ice->state.default_outer_level[1];
  3410. hdr[6] = ice->state.default_outer_level[0];
  3411. break;
  3412. }
  3413. struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_TESS_CTRL];
  3414. struct iris_const_buffer *cbuf = &shs->constbuf[0];
  3415. u_upload_data(ice->ctx.const_uploader, 0, sizeof(hdr), 32,
  3416. &hdr[0], &cbuf->data.offset,
  3417. &cbuf->data.res);
  3418. }
  3419. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3420. if (!(dirty & (IRIS_DIRTY_CONSTANTS_VS << stage)))
  3421. continue;
  3422. struct iris_shader_state *shs = &ice->state.shaders[stage];
  3423. struct iris_compiled_shader *shader = ice->shaders.prog[stage];
  3424. if (!shader)
  3425. continue;
  3426. if (shs->cbuf0_needs_upload)
  3427. upload_uniforms(ice, stage);
  3428. struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
  3429. iris_emit_cmd(batch, GENX(3DSTATE_CONSTANT_VS), pkt) {
  3430. pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
  3431. if (prog_data) {
  3432. /* The Skylake PRM contains the following restriction:
  3433. *
  3434. * "The driver must ensure The following case does not occur
  3435. * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
  3436. * buffer 3 read length equal to zero committed followed by a
  3437. * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
  3438. * zero committed."
  3439. *
  3440. * To avoid this, we program the buffers in the highest slots.
  3441. * This way, slot 0 is only used if slot 3 is also used.
  3442. */
  3443. int n = 3;
  3444. for (int i = 3; i >= 0; i--) {
  3445. const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
  3446. if (range->length == 0)
  3447. continue;
  3448. struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
  3449. struct iris_resource *res = (void *) cbuf->data.res;
  3450. assert(cbuf->data.offset % 32 == 0);
  3451. pkt.ConstantBody.ReadLength[n] = range->length;
  3452. pkt.ConstantBody.Buffer[n] =
  3453. res ? ro_bo(res->bo, range->start * 32 + cbuf->data.offset)
  3454. : ro_bo(batch->screen->workaround_bo, 0);
  3455. n--;
  3456. }
  3457. }
  3458. }
  3459. }
  3460. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3461. if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
  3462. iris_emit_cmd(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), ptr) {
  3463. ptr._3DCommandSubOpcode = 38 + stage;
  3464. ptr.PointertoVSBindingTable = binder->bt_offset[stage];
  3465. }
  3466. }
  3467. }
  3468. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3469. if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
  3470. iris_populate_binding_table(ice, batch, stage, false);
  3471. }
  3472. }
  3473. if (ice->state.need_border_colors)
  3474. iris_use_pinned_bo(batch, ice->state.border_color_pool.bo, false);
  3475. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3476. if (!(dirty & (IRIS_DIRTY_SAMPLER_STATES_VS << stage)) ||
  3477. !ice->shaders.prog[stage])
  3478. continue;
  3479. struct iris_shader_state *shs = &ice->state.shaders[stage];
  3480. struct pipe_resource *res = shs->sampler_table.res;
  3481. if (res)
  3482. iris_use_pinned_bo(batch, iris_resource_bo(res), false);
  3483. iris_emit_cmd(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
  3484. ptr._3DCommandSubOpcode = 43 + stage;
  3485. ptr.PointertoVSSamplerState = shs->sampler_table.offset;
  3486. }
  3487. }
  3488. if (dirty & IRIS_DIRTY_MULTISAMPLE) {
  3489. iris_emit_cmd(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
  3490. ms.PixelLocation =
  3491. ice->state.cso_rast->half_pixel_center ? CENTER : UL_CORNER;
  3492. if (ice->state.framebuffer.samples > 0)
  3493. ms.NumberofMultisamples = ffs(ice->state.framebuffer.samples) - 1;
  3494. }
  3495. }
  3496. if (dirty & IRIS_DIRTY_SAMPLE_MASK) {
  3497. iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_MASK), ms) {
  3498. ms.SampleMask = MAX2(ice->state.sample_mask, 1);
  3499. }
  3500. }
  3501. for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
  3502. if (!(dirty & (IRIS_DIRTY_VS << stage)))
  3503. continue;
  3504. struct iris_compiled_shader *shader = ice->shaders.prog[stage];
  3505. if (shader) {
  3506. struct iris_resource *cache = (void *) shader->assembly.res;
  3507. iris_use_pinned_bo(batch, cache->bo, false);
  3508. iris_batch_emit(batch, shader->derived_data,
  3509. iris_derived_program_state_size(stage));
  3510. } else {
  3511. if (stage == MESA_SHADER_TESS_EVAL) {
  3512. iris_emit_cmd(batch, GENX(3DSTATE_HS), hs);
  3513. iris_emit_cmd(batch, GENX(3DSTATE_TE), te);
  3514. iris_emit_cmd(batch, GENX(3DSTATE_DS), ds);
  3515. } else if (stage == MESA_SHADER_GEOMETRY) {
  3516. iris_emit_cmd(batch, GENX(3DSTATE_GS), gs);
  3517. }
  3518. }
  3519. }
  3520. if (ice->state.streamout_active) {
  3521. if (dirty & IRIS_DIRTY_SO_BUFFERS) {
  3522. iris_batch_emit(batch, genx->so_buffers,
  3523. 4 * 4 * GENX(3DSTATE_SO_BUFFER_length));
  3524. for (int i = 0; i < 4; i++) {
  3525. struct iris_stream_output_target *tgt =
  3526. (void *) ice->state.so_target[i];
  3527. if (tgt) {
  3528. iris_use_pinned_bo(batch, iris_resource_bo(tgt->base.buffer),
  3529. true);
  3530. iris_use_pinned_bo(batch, iris_resource_bo(tgt->offset.res),
  3531. true);
  3532. }
  3533. }
  3534. }
  3535. if ((dirty & IRIS_DIRTY_SO_DECL_LIST) && ice->state.streamout) {
  3536. uint32_t *decl_list =
  3537. ice->state.streamout + GENX(3DSTATE_STREAMOUT_length);
  3538. iris_batch_emit(batch, decl_list, 4 * ((decl_list[0] & 0xff) + 2));
  3539. }
  3540. if (dirty & IRIS_DIRTY_STREAMOUT) {
  3541. const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
  3542. uint32_t dynamic_sol[GENX(3DSTATE_STREAMOUT_length)];
  3543. iris_pack_command(GENX(3DSTATE_STREAMOUT), dynamic_sol, sol) {
  3544. sol.SOFunctionEnable = true;
  3545. sol.SOStatisticsEnable = true;
  3546. sol.RenderingDisable = cso_rast->rasterizer_discard &&
  3547. !ice->state.prims_generated_query_active;
  3548. sol.ReorderMode = cso_rast->flatshade_first ? LEADING : TRAILING;
  3549. }
  3550. assert(ice->state.streamout);
  3551. iris_emit_merge(batch, ice->state.streamout, dynamic_sol,
  3552. GENX(3DSTATE_STREAMOUT_length));
  3553. }
  3554. } else {
  3555. if (dirty & IRIS_DIRTY_STREAMOUT) {
  3556. iris_emit_cmd(batch, GENX(3DSTATE_STREAMOUT), sol);
  3557. }
  3558. }
  3559. if (dirty & IRIS_DIRTY_CLIP) {
  3560. struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
  3561. struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
  3562. uint32_t dynamic_clip[GENX(3DSTATE_CLIP_length)];
  3563. iris_pack_command(GENX(3DSTATE_CLIP), &dynamic_clip, cl) {
  3564. if (wm_prog_data->barycentric_interp_modes &
  3565. BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
  3566. cl.NonPerspectiveBarycentricEnable = true;
  3567. cl.ForceZeroRTAIndexEnable = cso_fb->layers == 0;
  3568. cl.MaximumVPIndex = ice->state.num_viewports - 1;
  3569. }
  3570. iris_emit_merge(batch, cso_rast->clip, dynamic_clip,
  3571. ARRAY_SIZE(cso_rast->clip));
  3572. }
  3573. if (dirty & IRIS_DIRTY_RASTER) {
  3574. struct iris_rasterizer_state *cso = ice->state.cso_rast;
  3575. iris_batch_emit(batch, cso->raster, sizeof(cso->raster));
  3576. iris_batch_emit(batch, cso->sf, sizeof(cso->sf));
  3577. }
  3578. /* XXX: FS program updates needs to flag IRIS_DIRTY_WM */
  3579. if (dirty & IRIS_DIRTY_WM) {
  3580. struct iris_rasterizer_state *cso = ice->state.cso_rast;
  3581. uint32_t dynamic_wm[GENX(3DSTATE_WM_length)];
  3582. iris_pack_command(GENX(3DSTATE_WM), &dynamic_wm, wm) {
  3583. wm.StatisticsEnable = ice->state.statistics_counters_enabled;
  3584. wm.BarycentricInterpolationMode =
  3585. wm_prog_data->barycentric_interp_modes;
  3586. if (wm_prog_data->early_fragment_tests)
  3587. wm.EarlyDepthStencilControl = EDSC_PREPS;
  3588. else if (wm_prog_data->has_side_effects)
  3589. wm.EarlyDepthStencilControl = EDSC_PSEXEC;
  3590. }
  3591. iris_emit_merge(batch, cso->wm, dynamic_wm, ARRAY_SIZE(cso->wm));
  3592. }
  3593. if (dirty & IRIS_DIRTY_SBE) {
  3594. iris_emit_sbe(batch, ice);
  3595. }
  3596. if (dirty & IRIS_DIRTY_PS_BLEND) {
  3597. struct iris_blend_state *cso_blend = ice->state.cso_blend;
  3598. struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
  3599. uint32_t dynamic_pb[GENX(3DSTATE_PS_BLEND_length)];
  3600. iris_pack_command(GENX(3DSTATE_PS_BLEND), &dynamic_pb, pb) {
  3601. pb.HasWriteableRT = true; // XXX: comes from somewhere :(
  3602. pb.AlphaTestEnable = cso_zsa->alpha.enabled;
  3603. }
  3604. iris_emit_merge(batch, cso_blend->ps_blend, dynamic_pb,
  3605. ARRAY_SIZE(cso_blend->ps_blend));
  3606. }
  3607. if (dirty & IRIS_DIRTY_WM_DEPTH_STENCIL) {
  3608. struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
  3609. struct pipe_stencil_ref *p_stencil_refs = &ice->state.stencil_ref;
  3610. uint32_t stencil_refs[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
  3611. iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), &stencil_refs, wmds) {
  3612. wmds.StencilReferenceValue = p_stencil_refs->ref_value[0];
  3613. wmds.BackfaceStencilReferenceValue = p_stencil_refs->ref_value[1];
  3614. }
  3615. iris_emit_merge(batch, cso->wmds, stencil_refs, ARRAY_SIZE(cso->wmds));
  3616. }
  3617. if (dirty & IRIS_DIRTY_SCISSOR_RECT) {
  3618. uint32_t scissor_offset =
  3619. emit_state(batch, ice->state.dynamic_uploader,
  3620. &ice->state.last_res.scissor,
  3621. ice->state.scissors,
  3622. sizeof(struct pipe_scissor_state) *
  3623. ice->state.num_viewports, 32);
  3624. iris_emit_cmd(batch, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
  3625. ptr.ScissorRectPointer = scissor_offset;
  3626. }
  3627. }
  3628. if (dirty & IRIS_DIRTY_DEPTH_BUFFER) {
  3629. struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
  3630. struct iris_depth_buffer_state *cso_z = &ice->state.genx->depth_buffer;
  3631. iris_batch_emit(batch, cso_z->packets, sizeof(cso_z->packets));
  3632. if (cso_fb->zsbuf) {
  3633. struct iris_resource *zres = (void *) cso_fb->zsbuf->texture;
  3634. // XXX: depth might not be writable...
  3635. iris_use_pinned_bo(batch, zres->bo, true);
  3636. }
  3637. }
  3638. if (dirty & IRIS_DIRTY_POLYGON_STIPPLE) {
  3639. iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
  3640. for (int i = 0; i < 32; i++) {
  3641. poly.PatternRow[i] = ice->state.poly_stipple.stipple[i];
  3642. }
  3643. }
  3644. }
  3645. if (dirty & IRIS_DIRTY_LINE_STIPPLE) {
  3646. struct iris_rasterizer_state *cso = ice->state.cso_rast;
  3647. iris_batch_emit(batch, cso->line_stipple, sizeof(cso->line_stipple));
  3648. }
  3649. if (dirty & IRIS_DIRTY_VF_TOPOLOGY) {
  3650. iris_emit_cmd(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
  3651. topo.PrimitiveTopologyType =
  3652. translate_prim_type(draw->mode, draw->vertices_per_patch);
  3653. }
  3654. }
  3655. if (dirty & IRIS_DIRTY_VERTEX_BUFFERS) {
  3656. struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
  3657. const unsigned vb_dwords = GENX(VERTEX_BUFFER_STATE_length);
  3658. if (cso->num_buffers > 0) {
  3659. iris_batch_emit(batch, cso->vertex_buffers, sizeof(uint32_t) *
  3660. (1 + vb_dwords * cso->num_buffers));
  3661. for (unsigned i = 0; i < cso->num_buffers; i++) {
  3662. struct iris_resource *res = (void *) cso->resources[i];
  3663. if (res)
  3664. iris_use_pinned_bo(batch, res->bo, false);
  3665. }
  3666. }
  3667. }
  3668. if (dirty & IRIS_DIRTY_VERTEX_ELEMENTS) {
  3669. struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
  3670. const unsigned entries = MAX2(cso->count, 1);
  3671. iris_batch_emit(batch, cso->vertex_elements, sizeof(uint32_t) *
  3672. (1 + entries * GENX(VERTEX_ELEMENT_STATE_length)));
  3673. iris_batch_emit(batch, cso->vf_instancing, sizeof(uint32_t) *
  3674. entries * GENX(3DSTATE_VF_INSTANCING_length));
  3675. }
  3676. if (dirty & IRIS_DIRTY_VF_SGVS) {
  3677. const struct brw_vs_prog_data *vs_prog_data = (void *)
  3678. ice->shaders.prog[MESA_SHADER_VERTEX]->prog_data;
  3679. struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
  3680. iris_emit_cmd(batch, GENX(3DSTATE_VF_SGVS), sgv) {
  3681. if (vs_prog_data->uses_vertexid) {
  3682. sgv.VertexIDEnable = true;
  3683. sgv.VertexIDComponentNumber = 2;
  3684. sgv.VertexIDElementOffset = cso->count;
  3685. }
  3686. if (vs_prog_data->uses_instanceid) {
  3687. sgv.InstanceIDEnable = true;
  3688. sgv.InstanceIDComponentNumber = 3;
  3689. sgv.InstanceIDElementOffset = cso->count;
  3690. }
  3691. }
  3692. }
  3693. if (dirty & IRIS_DIRTY_VF) {
  3694. iris_emit_cmd(batch, GENX(3DSTATE_VF), vf) {
  3695. if (draw->primitive_restart) {
  3696. vf.IndexedDrawCutIndexEnable = true;
  3697. vf.CutIndex = draw->restart_index;
  3698. }
  3699. }
  3700. }
  3701. // XXX: Gen8 - PMA fix
  3702. }
  3703. static void
  3704. iris_upload_render_state(struct iris_context *ice,
  3705. struct iris_batch *batch,
  3706. const struct pipe_draw_info *draw)
  3707. {
  3708. /* Always pin the binder. If we're emitting new binding table pointers,
  3709. * we need it. If not, we're probably inheriting old tables via the
  3710. * context, and need it anyway. Since true zero-bindings cases are
  3711. * practically non-existent, just pin it and avoid last_res tracking.
  3712. */
  3713. iris_use_pinned_bo(batch, ice->state.binder.bo, false);
  3714. iris_upload_dirty_render_state(ice, batch, draw);
  3715. if (draw->index_size > 0) {
  3716. unsigned offset;
  3717. if (draw->has_user_indices) {
  3718. u_upload_data(ice->ctx.stream_uploader, 0,
  3719. draw->count * draw->index_size, 4, draw->index.user,
  3720. &offset, &ice->state.last_res.index_buffer);
  3721. } else {
  3722. pipe_resource_reference(&ice->state.last_res.index_buffer,
  3723. draw->index.resource);
  3724. offset = 0;
  3725. }
  3726. struct iris_bo *bo = iris_resource_bo(ice->state.last_res.index_buffer);
  3727. iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
  3728. ib.IndexFormat = draw->index_size >> 1;
  3729. ib.MOCS = MOCS_WB;
  3730. ib.BufferSize = bo->size;
  3731. ib.BufferStartingAddress = ro_bo(bo, offset);
  3732. }
  3733. }
  3734. #define _3DPRIM_END_OFFSET 0x2420
  3735. #define _3DPRIM_START_VERTEX 0x2430
  3736. #define _3DPRIM_VERTEX_COUNT 0x2434
  3737. #define _3DPRIM_INSTANCE_COUNT 0x2438
  3738. #define _3DPRIM_START_INSTANCE 0x243C
  3739. #define _3DPRIM_BASE_VERTEX 0x2440
  3740. if (draw->indirect) {
  3741. /* We don't support this MultidrawIndirect. */
  3742. assert(!draw->indirect->indirect_draw_count);
  3743. struct iris_bo *bo = iris_resource_bo(draw->indirect->buffer);
  3744. assert(bo);
  3745. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3746. lrm.RegisterAddress = _3DPRIM_VERTEX_COUNT;
  3747. lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 0);
  3748. }
  3749. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3750. lrm.RegisterAddress = _3DPRIM_INSTANCE_COUNT;
  3751. lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 4);
  3752. }
  3753. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3754. lrm.RegisterAddress = _3DPRIM_START_VERTEX;
  3755. lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 8);
  3756. }
  3757. if (draw->index_size) {
  3758. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3759. lrm.RegisterAddress = _3DPRIM_BASE_VERTEX;
  3760. lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 12);
  3761. }
  3762. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3763. lrm.RegisterAddress = _3DPRIM_START_INSTANCE;
  3764. lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 16);
  3765. }
  3766. } else {
  3767. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3768. lrm.RegisterAddress = _3DPRIM_START_INSTANCE;
  3769. lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 12);
  3770. }
  3771. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
  3772. lri.RegisterOffset = _3DPRIM_BASE_VERTEX;
  3773. lri.DataDWord = 0;
  3774. }
  3775. }
  3776. }
  3777. iris_emit_cmd(batch, GENX(3DPRIMITIVE), prim) {
  3778. prim.StartInstanceLocation = draw->start_instance;
  3779. prim.InstanceCount = draw->instance_count;
  3780. prim.VertexCountPerInstance = draw->count;
  3781. prim.VertexAccessType = draw->index_size > 0 ? RANDOM : SEQUENTIAL;
  3782. // XXX: this is probably bonkers.
  3783. prim.StartVertexLocation = draw->start;
  3784. prim.IndirectParameterEnable = draw->indirect != NULL;
  3785. if (draw->index_size) {
  3786. prim.BaseVertexLocation += draw->index_bias;
  3787. } else {
  3788. prim.StartVertexLocation += draw->index_bias;
  3789. }
  3790. //prim.BaseVertexLocation = ...;
  3791. }
  3792. if (!batch->contains_draw) {
  3793. iris_restore_render_saved_bos(ice, batch, draw);
  3794. batch->contains_draw = true;
  3795. }
  3796. }
  3797. static void
  3798. iris_upload_compute_state(struct iris_context *ice,
  3799. struct iris_batch *batch,
  3800. const struct pipe_grid_info *grid)
  3801. {
  3802. const uint64_t dirty = ice->state.dirty;
  3803. struct iris_screen *screen = batch->screen;
  3804. const struct gen_device_info *devinfo = &screen->devinfo;
  3805. struct iris_binder *binder = &ice->state.binder;
  3806. struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_COMPUTE];
  3807. struct iris_compiled_shader *shader =
  3808. ice->shaders.prog[MESA_SHADER_COMPUTE];
  3809. struct brw_stage_prog_data *prog_data = shader->prog_data;
  3810. struct brw_cs_prog_data *cs_prog_data = (void *) prog_data;
  3811. // XXX: L3 configuration not set up for SLM
  3812. assert(prog_data->total_shared == 0);
  3813. if (dirty & IRIS_DIRTY_BINDINGS_CS)
  3814. iris_populate_binding_table(ice, batch, MESA_SHADER_COMPUTE, false);
  3815. iris_use_optional_res(batch, shs->sampler_table.res, false);
  3816. iris_use_pinned_bo(batch, iris_resource_bo(shader->assembly.res), false);
  3817. if (ice->state.need_border_colors)
  3818. iris_use_pinned_bo(batch, ice->state.border_color_pool.bo, false);
  3819. if (dirty & IRIS_DIRTY_CS) {
  3820. /* The MEDIA_VFE_STATE documentation for Gen8+ says:
  3821. *
  3822. * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
  3823. * the only bits that are changed are scoreboard related: Scoreboard
  3824. * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard Delta. For
  3825. * these scoreboard related states, a MEDIA_STATE_FLUSH is
  3826. * sufficient."
  3827. */
  3828. iris_emit_pipe_control_flush(batch, PIPE_CONTROL_CS_STALL);
  3829. iris_emit_cmd(batch, GENX(MEDIA_VFE_STATE), vfe) {
  3830. if (prog_data->total_scratch) {
  3831. uint32_t scratch_addr =
  3832. iris_get_scratch_space(ice, prog_data->total_scratch,
  3833. MESA_SHADER_COMPUTE);
  3834. vfe.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;
  3835. vfe.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);
  3836. }
  3837. vfe.MaximumNumberofThreads =
  3838. devinfo->max_cs_threads * screen->subslice_total - 1;
  3839. #if GEN_GEN < 11
  3840. vfe.ResetGatewayTimer =
  3841. Resettingrelativetimerandlatchingtheglobaltimestamp;
  3842. #endif
  3843. vfe.NumberofURBEntries = 2;
  3844. vfe.URBEntryAllocationSize = 2;
  3845. // XXX: Use Indirect Payload Storage?
  3846. vfe.CURBEAllocationSize =
  3847. ALIGN(cs_prog_data->push.per_thread.regs * cs_prog_data->threads +
  3848. cs_prog_data->push.cross_thread.regs, 2);
  3849. }
  3850. }
  3851. // XXX: hack iris_set_constant_buffers to upload these thread counts
  3852. // XXX: along with regular uniforms for compute shaders, somehow.
  3853. uint32_t curbe_data_offset = 0;
  3854. // TODO: Move subgroup-id into uniforms ubo so we can push uniforms
  3855. assert(cs_prog_data->push.cross_thread.dwords == 0 &&
  3856. cs_prog_data->push.per_thread.dwords == 1 &&
  3857. cs_prog_data->base.param[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID);
  3858. struct pipe_resource *curbe_data_res = NULL;
  3859. uint32_t *curbe_data_map =
  3860. stream_state(batch, ice->state.dynamic_uploader, &curbe_data_res,
  3861. ALIGN(cs_prog_data->push.total.size, 64), 64,
  3862. &curbe_data_offset);
  3863. assert(curbe_data_map);
  3864. memset(curbe_data_map, 0x5a, ALIGN(cs_prog_data->push.total.size, 64));
  3865. iris_fill_cs_push_const_buffer(cs_prog_data, curbe_data_map);
  3866. if (dirty & IRIS_DIRTY_CONSTANTS_CS) {
  3867. iris_emit_cmd(batch, GENX(MEDIA_CURBE_LOAD), curbe) {
  3868. curbe.CURBETotalDataLength =
  3869. ALIGN(cs_prog_data->push.total.size, 64);
  3870. curbe.CURBEDataStartAddress = curbe_data_offset;
  3871. }
  3872. }
  3873. if (dirty & (IRIS_DIRTY_SAMPLER_STATES_CS |
  3874. IRIS_DIRTY_BINDINGS_CS |
  3875. IRIS_DIRTY_CONSTANTS_CS |
  3876. IRIS_DIRTY_CS)) {
  3877. struct pipe_resource *desc_res = NULL;
  3878. uint32_t desc[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
  3879. iris_pack_state(GENX(INTERFACE_DESCRIPTOR_DATA), desc, idd) {
  3880. idd.SamplerStatePointer = shs->sampler_table.offset;
  3881. idd.BindingTablePointer = binder->bt_offset[MESA_SHADER_COMPUTE];
  3882. idd.ConstantURBEntryReadLength = cs_prog_data->push.per_thread.regs;
  3883. idd.CrossThreadConstantDataReadLength =
  3884. cs_prog_data->push.cross_thread.regs;
  3885. }
  3886. for (int i = 0; i < GENX(INTERFACE_DESCRIPTOR_DATA_length); i++)
  3887. desc[i] |= ((uint32_t *) shader->derived_data)[i];
  3888. iris_emit_cmd(batch, GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), load) {
  3889. load.InterfaceDescriptorTotalLength =
  3890. GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
  3891. load.InterfaceDescriptorDataStartAddress =
  3892. emit_state(batch, ice->state.dynamic_uploader,
  3893. &desc_res, desc, sizeof(desc), 32);
  3894. }
  3895. pipe_resource_reference(&desc_res, NULL);
  3896. }
  3897. uint32_t group_size = grid->block[0] * grid->block[1] * grid->block[2];
  3898. uint32_t remainder = group_size & (cs_prog_data->simd_size - 1);
  3899. uint32_t right_mask;
  3900. if (remainder > 0)
  3901. right_mask = ~0u >> (32 - remainder);
  3902. else
  3903. right_mask = ~0u >> (32 - cs_prog_data->simd_size);
  3904. #define GPGPU_DISPATCHDIMX 0x2500
  3905. #define GPGPU_DISPATCHDIMY 0x2504
  3906. #define GPGPU_DISPATCHDIMZ 0x2508
  3907. if (grid->indirect) {
  3908. struct iris_state_ref *grid_size = &ice->state.grid_size;
  3909. struct iris_bo *bo = iris_resource_bo(grid_size->res);
  3910. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3911. lrm.RegisterAddress = GPGPU_DISPATCHDIMX;
  3912. lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 0);
  3913. }
  3914. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3915. lrm.RegisterAddress = GPGPU_DISPATCHDIMY;
  3916. lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 4);
  3917. }
  3918. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3919. lrm.RegisterAddress = GPGPU_DISPATCHDIMZ;
  3920. lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 8);
  3921. }
  3922. }
  3923. iris_emit_cmd(batch, GENX(GPGPU_WALKER), ggw) {
  3924. ggw.IndirectParameterEnable = grid->indirect != NULL;
  3925. ggw.SIMDSize = cs_prog_data->simd_size / 16;
  3926. ggw.ThreadDepthCounterMaximum = 0;
  3927. ggw.ThreadHeightCounterMaximum = 0;
  3928. ggw.ThreadWidthCounterMaximum = cs_prog_data->threads - 1;
  3929. ggw.ThreadGroupIDXDimension = grid->grid[0];
  3930. ggw.ThreadGroupIDYDimension = grid->grid[1];
  3931. ggw.ThreadGroupIDZDimension = grid->grid[2];
  3932. ggw.RightExecutionMask = right_mask;
  3933. ggw.BottomExecutionMask = 0xffffffff;
  3934. }
  3935. iris_emit_cmd(batch, GENX(MEDIA_STATE_FLUSH), msf);
  3936. if (!batch->contains_draw) {
  3937. iris_restore_compute_saved_bos(ice, batch, grid);
  3938. batch->contains_draw = true;
  3939. }
  3940. }
  3941. /**
  3942. * State module teardown.
  3943. */
  3944. static void
  3945. iris_destroy_state(struct iris_context *ice)
  3946. {
  3947. iris_free_vertex_buffers(&ice->state.genx->vertex_buffers);
  3948. // XXX: unreference resources/surfaces.
  3949. for (unsigned i = 0; i < ice->state.framebuffer.nr_cbufs; i++) {
  3950. pipe_surface_reference(&ice->state.framebuffer.cbufs[i], NULL);
  3951. }
  3952. pipe_surface_reference(&ice->state.framebuffer.zsbuf, NULL);
  3953. for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
  3954. struct iris_shader_state *shs = &ice->state.shaders[stage];
  3955. pipe_resource_reference(&shs->sampler_table.res, NULL);
  3956. }
  3957. free(ice->state.genx);
  3958. pipe_resource_reference(&ice->state.last_res.cc_vp, NULL);
  3959. pipe_resource_reference(&ice->state.last_res.sf_cl_vp, NULL);
  3960. pipe_resource_reference(&ice->state.last_res.color_calc, NULL);
  3961. pipe_resource_reference(&ice->state.last_res.scissor, NULL);
  3962. pipe_resource_reference(&ice->state.last_res.blend, NULL);
  3963. pipe_resource_reference(&ice->state.last_res.index_buffer, NULL);
  3964. }
  3965. /* ------------------------------------------------------------------- */
  3966. static void
  3967. iris_load_register_imm32(struct iris_batch *batch, uint32_t reg,
  3968. uint32_t val)
  3969. {
  3970. _iris_emit_lri(batch, reg, val);
  3971. }
  3972. static void
  3973. iris_load_register_imm64(struct iris_batch *batch, uint32_t reg,
  3974. uint64_t val)
  3975. {
  3976. _iris_emit_lri(batch, reg + 0, val & 0xffffffff);
  3977. _iris_emit_lri(batch, reg + 4, val >> 32);
  3978. }
  3979. /**
  3980. * Emit MI_LOAD_REGISTER_MEM to load a 32-bit MMIO register from a buffer.
  3981. */
  3982. static void
  3983. iris_load_register_mem32(struct iris_batch *batch, uint32_t reg,
  3984. struct iris_bo *bo, uint32_t offset)
  3985. {
  3986. iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
  3987. lrm.RegisterAddress = reg;
  3988. lrm.MemoryAddress = ro_bo(bo, offset);
  3989. }
  3990. }
  3991. /**
  3992. * Load a 64-bit value from a buffer into a MMIO register via
  3993. * two MI_LOAD_REGISTER_MEM commands.
  3994. */
  3995. static void
  3996. iris_load_register_mem64(struct iris_batch *batch, uint32_t reg,
  3997. struct iris_bo *bo, uint32_t offset)
  3998. {
  3999. iris_load_register_mem32(batch, reg + 0, bo, offset + 0);
  4000. iris_load_register_mem32(batch, reg + 4, bo, offset + 4);
  4001. }
  4002. static void
  4003. iris_store_register_mem32(struct iris_batch *batch, uint32_t reg,
  4004. struct iris_bo *bo, uint32_t offset,
  4005. bool predicated)
  4006. {
  4007. iris_emit_cmd(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
  4008. srm.RegisterAddress = reg;
  4009. srm.MemoryAddress = rw_bo(bo, offset);
  4010. srm.PredicateEnable = predicated;
  4011. }
  4012. }
  4013. static void
  4014. iris_store_register_mem64(struct iris_batch *batch, uint32_t reg,
  4015. struct iris_bo *bo, uint32_t offset,
  4016. bool predicated)
  4017. {
  4018. iris_store_register_mem32(batch, reg + 0, bo, offset + 0, predicated);
  4019. iris_store_register_mem32(batch, reg + 4, bo, offset + 4, predicated);
  4020. }
  4021. static void
  4022. iris_store_data_imm32(struct iris_batch *batch,
  4023. struct iris_bo *bo, uint32_t offset,
  4024. uint32_t imm)
  4025. {
  4026. iris_emit_cmd(batch, GENX(MI_STORE_DATA_IMM), sdi) {
  4027. sdi.Address = rw_bo(bo, offset);
  4028. sdi.ImmediateData = imm;
  4029. }
  4030. }
  4031. static void
  4032. iris_store_data_imm64(struct iris_batch *batch,
  4033. struct iris_bo *bo, uint32_t offset,
  4034. uint64_t imm)
  4035. {
  4036. /* Can't use iris_emit_cmd because MI_STORE_DATA_IMM has a length of
  4037. * 2 in genxml but it's actually variable length and we need 5 DWords.
  4038. */
  4039. void *map = iris_get_command_space(batch, 4 * 5);
  4040. _iris_pack_command(batch, GENX(MI_STORE_DATA_IMM), map, sdi) {
  4041. sdi.DWordLength = 5 - 2;
  4042. sdi.Address = rw_bo(bo, offset);
  4043. sdi.ImmediateData = imm;
  4044. }
  4045. }
  4046. static void
  4047. iris_copy_mem_mem(struct iris_batch *batch,
  4048. struct iris_bo *dst_bo, uint32_t dst_offset,
  4049. struct iris_bo *src_bo, uint32_t src_offset,
  4050. unsigned bytes)
  4051. {
  4052. /* MI_COPY_MEM_MEM operates on DWords. */
  4053. assert(bytes % 4 == 0);
  4054. assert(dst_offset % 4 == 0);
  4055. assert(src_offset % 4 == 0);
  4056. for (unsigned i = 0; i < bytes; i += 4) {
  4057. iris_emit_cmd(batch, GENX(MI_COPY_MEM_MEM), cp) {
  4058. cp.DestinationMemoryAddress = rw_bo(dst_bo, dst_offset + i);
  4059. cp.SourceMemoryAddress = ro_bo(src_bo, src_offset + i);
  4060. }
  4061. }
  4062. }
  4063. /* ------------------------------------------------------------------- */
  4064. static unsigned
  4065. flags_to_post_sync_op(uint32_t flags)
  4066. {
  4067. if (flags & PIPE_CONTROL_WRITE_IMMEDIATE)
  4068. return WriteImmediateData;
  4069. if (flags & PIPE_CONTROL_WRITE_DEPTH_COUNT)
  4070. return WritePSDepthCount;
  4071. if (flags & PIPE_CONTROL_WRITE_TIMESTAMP)
  4072. return WriteTimestamp;
  4073. return 0;
  4074. }
  4075. /**
  4076. * Do the given flags have a Post Sync or LRI Post Sync operation?
  4077. */
  4078. static enum pipe_control_flags
  4079. get_post_sync_flags(enum pipe_control_flags flags)
  4080. {
  4081. flags &= PIPE_CONTROL_WRITE_IMMEDIATE |
  4082. PIPE_CONTROL_WRITE_DEPTH_COUNT |
  4083. PIPE_CONTROL_WRITE_TIMESTAMP |
  4084. PIPE_CONTROL_LRI_POST_SYNC_OP;
  4085. /* Only one "Post Sync Op" is allowed, and it's mutually exclusive with
  4086. * "LRI Post Sync Operation". So more than one bit set would be illegal.
  4087. */
  4088. assert(util_bitcount(flags) <= 1);
  4089. return flags;
  4090. }
  4091. // XXX: compute support
  4092. #define IS_COMPUTE_PIPELINE(batch) (batch->engine != I915_EXEC_RENDER)
  4093. /**
  4094. * Emit a series of PIPE_CONTROL commands, taking into account any
  4095. * workarounds necessary to actually accomplish the caller's request.
  4096. *
  4097. * Unless otherwise noted, spec quotations in this function come from:
  4098. *
  4099. * Synchronization of the 3D Pipeline > PIPE_CONTROL Command > Programming
  4100. * Restrictions for PIPE_CONTROL.
  4101. *
  4102. * You should not use this function directly. Use the helpers in
  4103. * iris_pipe_control.c instead, which may split the pipe control further.
  4104. */
  4105. static void
  4106. iris_emit_raw_pipe_control(struct iris_batch *batch, uint32_t flags,
  4107. struct iris_bo *bo, uint32_t offset, uint64_t imm)
  4108. {
  4109. UNUSED const struct gen_device_info *devinfo = &batch->screen->devinfo;
  4110. enum pipe_control_flags post_sync_flags = get_post_sync_flags(flags);
  4111. enum pipe_control_flags non_lri_post_sync_flags =
  4112. post_sync_flags & ~PIPE_CONTROL_LRI_POST_SYNC_OP;
  4113. /* Recursive PIPE_CONTROL workarounds --------------------------------
  4114. * (http://knowyourmeme.com/memes/xzibit-yo-dawg)
  4115. *
  4116. * We do these first because we want to look at the original operation,
  4117. * rather than any workarounds we set.
  4118. */
  4119. if (GEN_GEN == 9 && (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE)) {
  4120. /* The PIPE_CONTROL "VF Cache Invalidation Enable" bit description
  4121. * lists several workarounds:
  4122. *
  4123. * "Project: SKL, KBL, BXT
  4124. *
  4125. * If the VF Cache Invalidation Enable is set to a 1 in a
  4126. * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields
  4127. * sets to 0, with the VF Cache Invalidation Enable set to 0
  4128. * needs to be sent prior to the PIPE_CONTROL with VF Cache
  4129. * Invalidation Enable set to a 1."
  4130. */
  4131. iris_emit_raw_pipe_control(batch, 0, NULL, 0, 0);
  4132. }
  4133. if (GEN_GEN == 9 && IS_COMPUTE_PIPELINE(batch) && post_sync_flags) {
  4134. /* Project: SKL / Argument: LRI Post Sync Operation [23]
  4135. *
  4136. * "PIPECONTROL command with “Command Streamer Stall Enable” must be
  4137. * programmed prior to programming a PIPECONTROL command with "LRI
  4138. * Post Sync Operation" in GPGPU mode of operation (i.e when
  4139. * PIPELINE_SELECT command is set to GPGPU mode of operation)."
  4140. *
  4141. * The same text exists a few rows below for Post Sync Op.
  4142. */
  4143. iris_emit_raw_pipe_control(batch, PIPE_CONTROL_CS_STALL, bo, offset, imm);
  4144. }
  4145. if (GEN_GEN == 10 && (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH)) {
  4146. /* Cannonlake:
  4147. * "Before sending a PIPE_CONTROL command with bit 12 set, SW must issue
  4148. * another PIPE_CONTROL with Render Target Cache Flush Enable (bit 12)
  4149. * = 0 and Pipe Control Flush Enable (bit 7) = 1"
  4150. */
  4151. iris_emit_raw_pipe_control(batch, PIPE_CONTROL_FLUSH_ENABLE, bo,
  4152. offset, imm);
  4153. }
  4154. /* "Flush Types" workarounds ---------------------------------------------
  4155. * We do these now because they may add post-sync operations or CS stalls.
  4156. */
  4157. if (GEN_GEN < 11 && flags & PIPE_CONTROL_VF_CACHE_INVALIDATE) {
  4158. /* Project: BDW, SKL+ (stopping at CNL) / Argument: VF Invalidate
  4159. *
  4160. * "'Post Sync Operation' must be enabled to 'Write Immediate Data' or
  4161. * 'Write PS Depth Count' or 'Write Timestamp'."
  4162. */
  4163. if (!bo) {
  4164. flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
  4165. post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
  4166. non_lri_post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
  4167. bo = batch->screen->workaround_bo;
  4168. }
  4169. }
  4170. /* #1130 from Gen10 workarounds page:
  4171. *
  4172. * "Enable Depth Stall on every Post Sync Op if Render target Cache
  4173. * Flush is not enabled in same PIPE CONTROL and Enable Pixel score
  4174. * board stall if Render target cache flush is enabled."
  4175. *
  4176. * Applicable to CNL B0 and C0 steppings only.
  4177. *
  4178. * The wording here is unclear, and this workaround doesn't look anything
  4179. * like the internal bug report recommendations, but leave it be for now...
  4180. */
  4181. if (GEN_GEN == 10) {
  4182. if (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH) {
  4183. flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
  4184. } else if (flags & non_lri_post_sync_flags) {
  4185. flags |= PIPE_CONTROL_DEPTH_STALL;
  4186. }
  4187. }
  4188. if (flags & PIPE_CONTROL_DEPTH_STALL) {
  4189. /* From the PIPE_CONTROL instruction table, bit 13 (Depth Stall Enable):
  4190. *
  4191. * "This bit must be DISABLED for operations other than writing
  4192. * PS_DEPTH_COUNT."
  4193. *
  4194. * This seems like nonsense. An Ivybridge workaround requires us to
  4195. * emit a PIPE_CONTROL with a depth stall and write immediate post-sync
  4196. * operation. Gen8+ requires us to emit depth stalls and depth cache
  4197. * flushes together. So, it's hard to imagine this means anything other
  4198. * than "we originally intended this to be used for PS_DEPTH_COUNT".
  4199. *
  4200. * We ignore the supposed restriction and do nothing.
  4201. */
  4202. }
  4203. if (flags & (PIPE_CONTROL_RENDER_TARGET_FLUSH |
  4204. PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
  4205. /* From the PIPE_CONTROL instruction table, bit 12 and bit 1:
  4206. *
  4207. * "This bit must be DISABLED for End-of-pipe (Read) fences,
  4208. * PS_DEPTH_COUNT or TIMESTAMP queries."
  4209. *
  4210. * TODO: Implement end-of-pipe checking.
  4211. */
  4212. assert(!(post_sync_flags & (PIPE_CONTROL_WRITE_DEPTH_COUNT |
  4213. PIPE_CONTROL_WRITE_TIMESTAMP)));
  4214. }
  4215. if (GEN_GEN < 11 && (flags & PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
  4216. /* From the PIPE_CONTROL instruction table, bit 1:
  4217. *
  4218. * "This bit is ignored if Depth Stall Enable is set.
  4219. * Further, the render cache is not flushed even if Write Cache
  4220. * Flush Enable bit is set."
  4221. *
  4222. * We assert that the caller doesn't do this combination, to try and
  4223. * prevent mistakes. It shouldn't hurt the GPU, though.
  4224. *
  4225. * We skip this check on Gen11+ as the "Stall at Pixel Scoreboard"
  4226. * and "Render Target Flush" combo is explicitly required for BTI
  4227. * update workarounds.
  4228. */
  4229. assert(!(flags & (PIPE_CONTROL_DEPTH_STALL |
  4230. PIPE_CONTROL_RENDER_TARGET_FLUSH)));
  4231. }
  4232. /* PIPE_CONTROL page workarounds ------------------------------------- */
  4233. if (GEN_GEN <= 8 && (flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE)) {
  4234. /* From the PIPE_CONTROL page itself:
  4235. *
  4236. * "IVB, HSW, BDW
  4237. * Restriction: Pipe_control with CS-stall bit set must be issued
  4238. * before a pipe-control command that has the State Cache
  4239. * Invalidate bit set."
  4240. */
  4241. flags |= PIPE_CONTROL_CS_STALL;
  4242. }
  4243. if (flags & PIPE_CONTROL_FLUSH_LLC) {
  4244. /* From the PIPE_CONTROL instruction table, bit 26 (Flush LLC):
  4245. *
  4246. * "Project: ALL
  4247. * SW must always program Post-Sync Operation to "Write Immediate
  4248. * Data" when Flush LLC is set."
  4249. *
  4250. * For now, we just require the caller to do it.
  4251. */
  4252. assert(flags & PIPE_CONTROL_WRITE_IMMEDIATE);
  4253. }
  4254. /* "Post-Sync Operation" workarounds -------------------------------- */
  4255. /* Project: All / Argument: Global Snapshot Count Reset [19]
  4256. *
  4257. * "This bit must not be exercised on any product.
  4258. * Requires stall bit ([20] of DW1) set."
  4259. *
  4260. * We don't use this, so we just assert that it isn't used. The
  4261. * PIPE_CONTROL instruction page indicates that they intended this
  4262. * as a debug feature and don't think it is useful in production,
  4263. * but it may actually be usable, should we ever want to.
  4264. */
  4265. assert((flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET) == 0);
  4266. if (flags & (PIPE_CONTROL_MEDIA_STATE_CLEAR |
  4267. PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE)) {
  4268. /* Project: All / Arguments:
  4269. *
  4270. * - Generic Media State Clear [16]
  4271. * - Indirect State Pointers Disable [16]
  4272. *
  4273. * "Requires stall bit ([20] of DW1) set."
  4274. *
  4275. * Also, the PIPE_CONTROL instruction table, bit 16 (Generic Media
  4276. * State Clear) says:
  4277. *
  4278. * "PIPECONTROL command with “Command Streamer Stall Enable” must be
  4279. * programmed prior to programming a PIPECONTROL command with "Media
  4280. * State Clear" set in GPGPU mode of operation"
  4281. *
  4282. * This is a subset of the earlier rule, so there's nothing to do.
  4283. */
  4284. flags |= PIPE_CONTROL_CS_STALL;
  4285. }
  4286. if (flags & PIPE_CONTROL_STORE_DATA_INDEX) {
  4287. /* Project: All / Argument: Store Data Index
  4288. *
  4289. * "Post-Sync Operation ([15:14] of DW1) must be set to something other
  4290. * than '0'."
  4291. *
  4292. * For now, we just assert that the caller does this. We might want to
  4293. * automatically add a write to the workaround BO...
  4294. */
  4295. assert(non_lri_post_sync_flags != 0);
  4296. }
  4297. if (flags & PIPE_CONTROL_SYNC_GFDT) {
  4298. /* Project: All / Argument: Sync GFDT
  4299. *
  4300. * "Post-Sync Operation ([15:14] of DW1) must be set to something other
  4301. * than '0' or 0x2520[13] must be set."
  4302. *
  4303. * For now, we just assert that the caller does this.
  4304. */
  4305. assert(non_lri_post_sync_flags != 0);
  4306. }
  4307. if (flags & PIPE_CONTROL_TLB_INVALIDATE) {
  4308. /* Project: IVB+ / Argument: TLB inv
  4309. *
  4310. * "Requires stall bit ([20] of DW1) set."
  4311. *
  4312. * Also, from the PIPE_CONTROL instruction table:
  4313. *
  4314. * "Project: SKL+
  4315. * Post Sync Operation or CS stall must be set to ensure a TLB
  4316. * invalidation occurs. Otherwise no cycle will occur to the TLB
  4317. * cache to invalidate."
  4318. *
  4319. * This is not a subset of the earlier rule, so there's nothing to do.
  4320. */
  4321. flags |= PIPE_CONTROL_CS_STALL;
  4322. }
  4323. if (GEN_GEN == 9 && devinfo->gt == 4) {
  4324. /* TODO: The big Skylake GT4 post sync op workaround */
  4325. }
  4326. /* "GPGPU specific workarounds" (both post-sync and flush) ------------ */
  4327. if (IS_COMPUTE_PIPELINE(batch)) {
  4328. if (GEN_GEN >= 9 && (flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE)) {
  4329. /* Project: SKL+ / Argument: Tex Invalidate
  4330. * "Requires stall bit ([20] of DW) set for all GPGPU Workloads."
  4331. */
  4332. flags |= PIPE_CONTROL_CS_STALL;
  4333. }
  4334. if (GEN_GEN == 8 && (post_sync_flags ||
  4335. (flags & (PIPE_CONTROL_NOTIFY_ENABLE |
  4336. PIPE_CONTROL_DEPTH_STALL |
  4337. PIPE_CONTROL_RENDER_TARGET_FLUSH |
  4338. PIPE_CONTROL_DEPTH_CACHE_FLUSH |
  4339. PIPE_CONTROL_DATA_CACHE_FLUSH)))) {
  4340. /* Project: BDW / Arguments:
  4341. *
  4342. * - LRI Post Sync Operation [23]
  4343. * - Post Sync Op [15:14]
  4344. * - Notify En [8]
  4345. * - Depth Stall [13]
  4346. * - Render Target Cache Flush [12]
  4347. * - Depth Cache Flush [0]
  4348. * - DC Flush Enable [5]
  4349. *
  4350. * "Requires stall bit ([20] of DW) set for all GPGPU and Media
  4351. * Workloads."
  4352. */
  4353. flags |= PIPE_CONTROL_CS_STALL;
  4354. /* Also, from the PIPE_CONTROL instruction table, bit 20:
  4355. *
  4356. * "Project: BDW
  4357. * This bit must be always set when PIPE_CONTROL command is
  4358. * programmed by GPGPU and MEDIA workloads, except for the cases
  4359. * when only Read Only Cache Invalidation bits are set (State
  4360. * Cache Invalidation Enable, Instruction cache Invalidation
  4361. * Enable, Texture Cache Invalidation Enable, Constant Cache
  4362. * Invalidation Enable). This is to WA FFDOP CG issue, this WA
  4363. * need not implemented when FF_DOP_CG is disable via "Fixed
  4364. * Function DOP Clock Gate Disable" bit in RC_PSMI_CTRL register."
  4365. *
  4366. * It sounds like we could avoid CS stalls in some cases, but we
  4367. * don't currently bother. This list isn't exactly the list above,
  4368. * either...
  4369. */
  4370. }
  4371. }
  4372. /* "Stall" workarounds ----------------------------------------------
  4373. * These have to come after the earlier ones because we may have added
  4374. * some additional CS stalls above.
  4375. */
  4376. if (GEN_GEN < 9 && (flags & PIPE_CONTROL_CS_STALL)) {
  4377. /* Project: PRE-SKL, VLV, CHV
  4378. *
  4379. * "[All Stepping][All SKUs]:
  4380. *
  4381. * One of the following must also be set:
  4382. *
  4383. * - Render Target Cache Flush Enable ([12] of DW1)
  4384. * - Depth Cache Flush Enable ([0] of DW1)
  4385. * - Stall at Pixel Scoreboard ([1] of DW1)
  4386. * - Depth Stall ([13] of DW1)
  4387. * - Post-Sync Operation ([13] of DW1)
  4388. * - DC Flush Enable ([5] of DW1)"
  4389. *
  4390. * If we don't already have one of those bits set, we choose to add
  4391. * "Stall at Pixel Scoreboard". Some of the other bits require a
  4392. * CS stall as a workaround (see above), which would send us into
  4393. * an infinite recursion of PIPE_CONTROLs. "Stall at Pixel Scoreboard"
  4394. * appears to be safe, so we choose that.
  4395. */
  4396. const uint32_t wa_bits = PIPE_CONTROL_RENDER_TARGET_FLUSH |
  4397. PIPE_CONTROL_DEPTH_CACHE_FLUSH |
  4398. PIPE_CONTROL_WRITE_IMMEDIATE |
  4399. PIPE_CONTROL_WRITE_DEPTH_COUNT |
  4400. PIPE_CONTROL_WRITE_TIMESTAMP |
  4401. PIPE_CONTROL_STALL_AT_SCOREBOARD |
  4402. PIPE_CONTROL_DEPTH_STALL |
  4403. PIPE_CONTROL_DATA_CACHE_FLUSH;
  4404. if (!(flags & wa_bits))
  4405. flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
  4406. }
  4407. /* Emit --------------------------------------------------------------- */
  4408. iris_emit_cmd(batch, GENX(PIPE_CONTROL), pc) {
  4409. pc.LRIPostSyncOperation = NoLRIOperation;
  4410. pc.PipeControlFlushEnable = flags & PIPE_CONTROL_FLUSH_ENABLE;
  4411. pc.DCFlushEnable = flags & PIPE_CONTROL_DATA_CACHE_FLUSH;
  4412. pc.StoreDataIndex = 0;
  4413. pc.CommandStreamerStallEnable = flags & PIPE_CONTROL_CS_STALL;
  4414. pc.GlobalSnapshotCountReset =
  4415. flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET;
  4416. pc.TLBInvalidate = flags & PIPE_CONTROL_TLB_INVALIDATE;
  4417. pc.GenericMediaStateClear = flags & PIPE_CONTROL_MEDIA_STATE_CLEAR;
  4418. pc.StallAtPixelScoreboard = flags & PIPE_CONTROL_STALL_AT_SCOREBOARD;
  4419. pc.RenderTargetCacheFlushEnable =
  4420. flags & PIPE_CONTROL_RENDER_TARGET_FLUSH;
  4421. pc.DepthCacheFlushEnable = flags & PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  4422. pc.StateCacheInvalidationEnable =
  4423. flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  4424. pc.VFCacheInvalidationEnable = flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
  4425. pc.ConstantCacheInvalidationEnable =
  4426. flags & PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  4427. pc.PostSyncOperation = flags_to_post_sync_op(flags);
  4428. pc.DepthStallEnable = flags & PIPE_CONTROL_DEPTH_STALL;
  4429. pc.InstructionCacheInvalidateEnable =
  4430. flags & PIPE_CONTROL_INSTRUCTION_INVALIDATE;
  4431. pc.NotifyEnable = flags & PIPE_CONTROL_NOTIFY_ENABLE;
  4432. pc.IndirectStatePointersDisable =
  4433. flags & PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE;
  4434. pc.TextureCacheInvalidationEnable =
  4435. flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  4436. pc.Address = rw_bo(bo, offset);
  4437. pc.ImmediateData = imm;
  4438. }
  4439. }
  4440. void
  4441. genX(init_state)(struct iris_context *ice)
  4442. {
  4443. struct pipe_context *ctx = &ice->ctx;
  4444. struct iris_screen *screen = (struct iris_screen *)ctx->screen;
  4445. ctx->create_blend_state = iris_create_blend_state;
  4446. ctx->create_depth_stencil_alpha_state = iris_create_zsa_state;
  4447. ctx->create_rasterizer_state = iris_create_rasterizer_state;
  4448. ctx->create_sampler_state = iris_create_sampler_state;
  4449. ctx->create_sampler_view = iris_create_sampler_view;
  4450. ctx->create_surface = iris_create_surface;
  4451. ctx->create_vertex_elements_state = iris_create_vertex_elements;
  4452. ctx->bind_blend_state = iris_bind_blend_state;
  4453. ctx->bind_depth_stencil_alpha_state = iris_bind_zsa_state;
  4454. ctx->bind_sampler_states = iris_bind_sampler_states;
  4455. ctx->bind_rasterizer_state = iris_bind_rasterizer_state;
  4456. ctx->bind_vertex_elements_state = iris_bind_vertex_elements_state;
  4457. ctx->delete_blend_state = iris_delete_state;
  4458. ctx->delete_depth_stencil_alpha_state = iris_delete_state;
  4459. ctx->delete_fs_state = iris_delete_state;
  4460. ctx->delete_rasterizer_state = iris_delete_state;
  4461. ctx->delete_sampler_state = iris_delete_state;
  4462. ctx->delete_vertex_elements_state = iris_delete_state;
  4463. ctx->delete_tcs_state = iris_delete_state;
  4464. ctx->delete_tes_state = iris_delete_state;
  4465. ctx->delete_gs_state = iris_delete_state;
  4466. ctx->delete_vs_state = iris_delete_state;
  4467. ctx->set_blend_color = iris_set_blend_color;
  4468. ctx->set_clip_state = iris_set_clip_state;
  4469. ctx->set_constant_buffer = iris_set_constant_buffer;
  4470. ctx->set_shader_buffers = iris_set_shader_buffers;
  4471. ctx->set_shader_images = iris_set_shader_images;
  4472. ctx->set_sampler_views = iris_set_sampler_views;
  4473. ctx->set_tess_state = iris_set_tess_state;
  4474. ctx->set_framebuffer_state = iris_set_framebuffer_state;
  4475. ctx->set_polygon_stipple = iris_set_polygon_stipple;
  4476. ctx->set_sample_mask = iris_set_sample_mask;
  4477. ctx->set_scissor_states = iris_set_scissor_states;
  4478. ctx->set_stencil_ref = iris_set_stencil_ref;
  4479. ctx->set_vertex_buffers = iris_set_vertex_buffers;
  4480. ctx->set_viewport_states = iris_set_viewport_states;
  4481. ctx->sampler_view_destroy = iris_sampler_view_destroy;
  4482. ctx->surface_destroy = iris_surface_destroy;
  4483. ctx->draw_vbo = iris_draw_vbo;
  4484. ctx->launch_grid = iris_launch_grid;
  4485. ctx->create_stream_output_target = iris_create_stream_output_target;
  4486. ctx->stream_output_target_destroy = iris_stream_output_target_destroy;
  4487. ctx->set_stream_output_targets = iris_set_stream_output_targets;
  4488. ice->vtbl.destroy_state = iris_destroy_state;
  4489. ice->vtbl.init_render_context = iris_init_render_context;
  4490. ice->vtbl.init_compute_context = iris_init_compute_context;
  4491. ice->vtbl.upload_render_state = iris_upload_render_state;
  4492. ice->vtbl.update_surface_base_address = iris_update_surface_base_address;
  4493. ice->vtbl.upload_compute_state = iris_upload_compute_state;
  4494. ice->vtbl.emit_raw_pipe_control = iris_emit_raw_pipe_control;
  4495. ice->vtbl.load_register_imm32 = iris_load_register_imm32;
  4496. ice->vtbl.load_register_imm64 = iris_load_register_imm64;
  4497. ice->vtbl.load_register_mem32 = iris_load_register_mem32;
  4498. ice->vtbl.load_register_mem64 = iris_load_register_mem64;
  4499. ice->vtbl.store_register_mem32 = iris_store_register_mem32;
  4500. ice->vtbl.store_register_mem64 = iris_store_register_mem64;
  4501. ice->vtbl.store_data_imm32 = iris_store_data_imm32;
  4502. ice->vtbl.store_data_imm64 = iris_store_data_imm64;
  4503. ice->vtbl.copy_mem_mem = iris_copy_mem_mem;
  4504. ice->vtbl.derived_program_state_size = iris_derived_program_state_size;
  4505. ice->vtbl.store_derived_program_state = iris_store_derived_program_state;
  4506. ice->vtbl.create_so_decl_list = iris_create_so_decl_list;
  4507. ice->vtbl.populate_vs_key = iris_populate_vs_key;
  4508. ice->vtbl.populate_tcs_key = iris_populate_tcs_key;
  4509. ice->vtbl.populate_tes_key = iris_populate_tes_key;
  4510. ice->vtbl.populate_gs_key = iris_populate_gs_key;
  4511. ice->vtbl.populate_fs_key = iris_populate_fs_key;
  4512. ice->vtbl.populate_cs_key = iris_populate_cs_key;
  4513. ice->state.dirty = ~0ull;
  4514. ice->state.statistics_counters_enabled = true;
  4515. ice->state.sample_mask = 0xffff;
  4516. ice->state.num_viewports = 1;
  4517. ice->state.genx = calloc(1, sizeof(struct iris_genx_state));
  4518. /* Make a 1x1x1 null surface for unbound textures */
  4519. void *null_surf_map =
  4520. upload_state(ice->state.surface_uploader, &ice->state.unbound_tex,
  4521. 4 * GENX(RENDER_SURFACE_STATE_length), 64);
  4522. isl_null_fill_state(&screen->isl_dev, null_surf_map, isl_extent3d(1, 1, 1));
  4523. ice->state.unbound_tex.offset +=
  4524. iris_bo_offset_from_base_address(iris_resource_bo(ice->state.unbound_tex.res));
  4525. /* Default all scissor rectangles to be empty regions. */
  4526. for (int i = 0; i < IRIS_MAX_VIEWPORTS; i++) {
  4527. ice->state.scissors[i] = (struct pipe_scissor_state) {
  4528. .minx = 1, .maxx = 0, .miny = 1, .maxy = 0,
  4529. };
  4530. }
  4531. }