Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

si_state.c 165KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006
  1. /*
  2. * Copyright 2012 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include "si_build_pm4.h"
  25. #include "gfx9d.h"
  26. #include "si_query.h"
  27. #include "util/u_dual_blend.h"
  28. #include "util/u_format.h"
  29. #include "util/u_format_s3tc.h"
  30. #include "util/u_memory.h"
  31. #include "util/u_resource.h"
  32. #include "util/u_upload_mgr.h"
  33. static unsigned si_map_swizzle(unsigned swizzle)
  34. {
  35. switch (swizzle) {
  36. case PIPE_SWIZZLE_Y:
  37. return V_008F0C_SQ_SEL_Y;
  38. case PIPE_SWIZZLE_Z:
  39. return V_008F0C_SQ_SEL_Z;
  40. case PIPE_SWIZZLE_W:
  41. return V_008F0C_SQ_SEL_W;
  42. case PIPE_SWIZZLE_0:
  43. return V_008F0C_SQ_SEL_0;
  44. case PIPE_SWIZZLE_1:
  45. return V_008F0C_SQ_SEL_1;
  46. default: /* PIPE_SWIZZLE_X */
  47. return V_008F0C_SQ_SEL_X;
  48. }
  49. }
  50. /* 12.4 fixed-point */
  51. static unsigned si_pack_float_12p4(float x)
  52. {
  53. return x <= 0 ? 0 :
  54. x >= 4096 ? 0xffff : x * 16;
  55. }
  56. /*
  57. * Inferred framebuffer and blender state.
  58. *
  59. * CB_TARGET_MASK is emitted here to avoid a hang with dual source blending
  60. * if there is not enough PS outputs.
  61. */
  62. static void si_emit_cb_render_state(struct si_context *sctx)
  63. {
  64. struct radeon_winsys_cs *cs = sctx->gfx_cs;
  65. struct si_state_blend *blend = sctx->queued.named.blend;
  66. /* CB_COLORn_INFO.FORMAT=INVALID should disable unbound colorbuffers,
  67. * but you never know. */
  68. uint32_t cb_target_mask = sctx->framebuffer.colorbuf_enabled_4bit;
  69. unsigned i;
  70. if (blend)
  71. cb_target_mask &= blend->cb_target_mask;
  72. /* Avoid a hang that happens when dual source blending is enabled
  73. * but there is not enough color outputs. This is undefined behavior,
  74. * so disable color writes completely.
  75. *
  76. * Reproducible with Unigine Heaven 4.0 and drirc missing.
  77. */
  78. if (blend && blend->dual_src_blend &&
  79. sctx->ps_shader.cso &&
  80. (sctx->ps_shader.cso->info.colors_written & 0x3) != 0x3)
  81. cb_target_mask = 0;
  82. radeon_opt_set_context_reg(sctx, R_028238_CB_TARGET_MASK,
  83. SI_TRACKED_CB_TARGET_MASK, cb_target_mask);
  84. /* GFX9: Flush DFSM when CB_TARGET_MASK changes.
  85. * I think we don't have to do anything between IBs.
  86. */
  87. if (sctx->screen->dfsm_allowed &&
  88. sctx->last_cb_target_mask != cb_target_mask) {
  89. sctx->last_cb_target_mask = cb_target_mask;
  90. radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
  91. radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
  92. }
  93. if (sctx->chip_class >= VI) {
  94. /* DCC MSAA workaround for blending.
  95. * Alternatively, we can set CB_COLORi_DCC_CONTROL.OVERWRITE_-
  96. * COMBINER_DISABLE, but that would be more complicated.
  97. */
  98. bool oc_disable = (sctx->chip_class == VI ||
  99. sctx->chip_class == GFX9) &&
  100. blend &&
  101. blend->blend_enable_4bit & cb_target_mask &&
  102. sctx->framebuffer.nr_samples >= 2;
  103. radeon_opt_set_context_reg(
  104. sctx, R_028424_CB_DCC_CONTROL,
  105. SI_TRACKED_CB_DCC_CONTROL,
  106. S_028424_OVERWRITE_COMBINER_MRT_SHARING_DISABLE(1) |
  107. S_028424_OVERWRITE_COMBINER_WATERMARK(4) |
  108. S_028424_OVERWRITE_COMBINER_DISABLE(oc_disable));
  109. }
  110. /* RB+ register settings. */
  111. if (sctx->screen->rbplus_allowed) {
  112. unsigned spi_shader_col_format =
  113. sctx->ps_shader.cso ?
  114. sctx->ps_shader.current->key.part.ps.epilog.spi_shader_col_format : 0;
  115. unsigned sx_ps_downconvert = 0;
  116. unsigned sx_blend_opt_epsilon = 0;
  117. unsigned sx_blend_opt_control = 0;
  118. for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
  119. struct r600_surface *surf =
  120. (struct r600_surface*)sctx->framebuffer.state.cbufs[i];
  121. unsigned format, swap, spi_format, colormask;
  122. bool has_alpha, has_rgb;
  123. if (!surf)
  124. continue;
  125. format = G_028C70_FORMAT(surf->cb_color_info);
  126. swap = G_028C70_COMP_SWAP(surf->cb_color_info);
  127. spi_format = (spi_shader_col_format >> (i * 4)) & 0xf;
  128. colormask = (cb_target_mask >> (i * 4)) & 0xf;
  129. /* Set if RGB and A are present. */
  130. has_alpha = !G_028C74_FORCE_DST_ALPHA_1(surf->cb_color_attrib);
  131. if (format == V_028C70_COLOR_8 ||
  132. format == V_028C70_COLOR_16 ||
  133. format == V_028C70_COLOR_32)
  134. has_rgb = !has_alpha;
  135. else
  136. has_rgb = true;
  137. /* Check the colormask and export format. */
  138. if (!(colormask & (PIPE_MASK_RGBA & ~PIPE_MASK_A)))
  139. has_rgb = false;
  140. if (!(colormask & PIPE_MASK_A))
  141. has_alpha = false;
  142. if (spi_format == V_028714_SPI_SHADER_ZERO) {
  143. has_rgb = false;
  144. has_alpha = false;
  145. }
  146. /* Disable value checking for disabled channels. */
  147. if (!has_rgb)
  148. sx_blend_opt_control |= S_02875C_MRT0_COLOR_OPT_DISABLE(1) << (i * 4);
  149. if (!has_alpha)
  150. sx_blend_opt_control |= S_02875C_MRT0_ALPHA_OPT_DISABLE(1) << (i * 4);
  151. /* Enable down-conversion for 32bpp and smaller formats. */
  152. switch (format) {
  153. case V_028C70_COLOR_8:
  154. case V_028C70_COLOR_8_8:
  155. case V_028C70_COLOR_8_8_8_8:
  156. /* For 1 and 2-channel formats, use the superset thereof. */
  157. if (spi_format == V_028714_SPI_SHADER_FP16_ABGR ||
  158. spi_format == V_028714_SPI_SHADER_UINT16_ABGR ||
  159. spi_format == V_028714_SPI_SHADER_SINT16_ABGR) {
  160. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_8_8_8_8 << (i * 4);
  161. sx_blend_opt_epsilon |= V_028758_8BIT_FORMAT << (i * 4);
  162. }
  163. break;
  164. case V_028C70_COLOR_5_6_5:
  165. if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
  166. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_5_6_5 << (i * 4);
  167. sx_blend_opt_epsilon |= V_028758_6BIT_FORMAT << (i * 4);
  168. }
  169. break;
  170. case V_028C70_COLOR_1_5_5_5:
  171. if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
  172. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_1_5_5_5 << (i * 4);
  173. sx_blend_opt_epsilon |= V_028758_5BIT_FORMAT << (i * 4);
  174. }
  175. break;
  176. case V_028C70_COLOR_4_4_4_4:
  177. if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
  178. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_4_4_4_4 << (i * 4);
  179. sx_blend_opt_epsilon |= V_028758_4BIT_FORMAT << (i * 4);
  180. }
  181. break;
  182. case V_028C70_COLOR_32:
  183. if (swap == V_028C70_SWAP_STD &&
  184. spi_format == V_028714_SPI_SHADER_32_R)
  185. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_R << (i * 4);
  186. else if (swap == V_028C70_SWAP_ALT_REV &&
  187. spi_format == V_028714_SPI_SHADER_32_AR)
  188. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_A << (i * 4);
  189. break;
  190. case V_028C70_COLOR_16:
  191. case V_028C70_COLOR_16_16:
  192. /* For 1-channel formats, use the superset thereof. */
  193. if (spi_format == V_028714_SPI_SHADER_UNORM16_ABGR ||
  194. spi_format == V_028714_SPI_SHADER_SNORM16_ABGR ||
  195. spi_format == V_028714_SPI_SHADER_UINT16_ABGR ||
  196. spi_format == V_028714_SPI_SHADER_SINT16_ABGR) {
  197. if (swap == V_028C70_SWAP_STD ||
  198. swap == V_028C70_SWAP_STD_REV)
  199. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_16_16_GR << (i * 4);
  200. else
  201. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_16_16_AR << (i * 4);
  202. }
  203. break;
  204. case V_028C70_COLOR_10_11_11:
  205. if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
  206. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_10_11_11 << (i * 4);
  207. sx_blend_opt_epsilon |= V_028758_11BIT_FORMAT << (i * 4);
  208. }
  209. break;
  210. case V_028C70_COLOR_2_10_10_10:
  211. if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
  212. sx_ps_downconvert |= V_028754_SX_RT_EXPORT_2_10_10_10 << (i * 4);
  213. sx_blend_opt_epsilon |= V_028758_10BIT_FORMAT << (i * 4);
  214. }
  215. break;
  216. }
  217. }
  218. /* SX_PS_DOWNCONVERT, SX_BLEND_OPT_EPSILON, SX_BLEND_OPT_CONTROL */
  219. radeon_opt_set_context_reg3(sctx, R_028754_SX_PS_DOWNCONVERT,
  220. SI_TRACKED_SX_PS_DOWNCONVERT,
  221. sx_ps_downconvert, sx_blend_opt_epsilon,
  222. sx_blend_opt_control);
  223. }
  224. }
  225. /*
  226. * Blender functions
  227. */
  228. static uint32_t si_translate_blend_function(int blend_func)
  229. {
  230. switch (blend_func) {
  231. case PIPE_BLEND_ADD:
  232. return V_028780_COMB_DST_PLUS_SRC;
  233. case PIPE_BLEND_SUBTRACT:
  234. return V_028780_COMB_SRC_MINUS_DST;
  235. case PIPE_BLEND_REVERSE_SUBTRACT:
  236. return V_028780_COMB_DST_MINUS_SRC;
  237. case PIPE_BLEND_MIN:
  238. return V_028780_COMB_MIN_DST_SRC;
  239. case PIPE_BLEND_MAX:
  240. return V_028780_COMB_MAX_DST_SRC;
  241. default:
  242. PRINT_ERR("Unknown blend function %d\n", blend_func);
  243. assert(0);
  244. break;
  245. }
  246. return 0;
  247. }
  248. static uint32_t si_translate_blend_factor(int blend_fact)
  249. {
  250. switch (blend_fact) {
  251. case PIPE_BLENDFACTOR_ONE:
  252. return V_028780_BLEND_ONE;
  253. case PIPE_BLENDFACTOR_SRC_COLOR:
  254. return V_028780_BLEND_SRC_COLOR;
  255. case PIPE_BLENDFACTOR_SRC_ALPHA:
  256. return V_028780_BLEND_SRC_ALPHA;
  257. case PIPE_BLENDFACTOR_DST_ALPHA:
  258. return V_028780_BLEND_DST_ALPHA;
  259. case PIPE_BLENDFACTOR_DST_COLOR:
  260. return V_028780_BLEND_DST_COLOR;
  261. case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
  262. return V_028780_BLEND_SRC_ALPHA_SATURATE;
  263. case PIPE_BLENDFACTOR_CONST_COLOR:
  264. return V_028780_BLEND_CONSTANT_COLOR;
  265. case PIPE_BLENDFACTOR_CONST_ALPHA:
  266. return V_028780_BLEND_CONSTANT_ALPHA;
  267. case PIPE_BLENDFACTOR_ZERO:
  268. return V_028780_BLEND_ZERO;
  269. case PIPE_BLENDFACTOR_INV_SRC_COLOR:
  270. return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
  271. case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
  272. return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
  273. case PIPE_BLENDFACTOR_INV_DST_ALPHA:
  274. return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
  275. case PIPE_BLENDFACTOR_INV_DST_COLOR:
  276. return V_028780_BLEND_ONE_MINUS_DST_COLOR;
  277. case PIPE_BLENDFACTOR_INV_CONST_COLOR:
  278. return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
  279. case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
  280. return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
  281. case PIPE_BLENDFACTOR_SRC1_COLOR:
  282. return V_028780_BLEND_SRC1_COLOR;
  283. case PIPE_BLENDFACTOR_SRC1_ALPHA:
  284. return V_028780_BLEND_SRC1_ALPHA;
  285. case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
  286. return V_028780_BLEND_INV_SRC1_COLOR;
  287. case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
  288. return V_028780_BLEND_INV_SRC1_ALPHA;
  289. default:
  290. PRINT_ERR("Bad blend factor %d not supported!\n", blend_fact);
  291. assert(0);
  292. break;
  293. }
  294. return 0;
  295. }
  296. static uint32_t si_translate_blend_opt_function(int blend_func)
  297. {
  298. switch (blend_func) {
  299. case PIPE_BLEND_ADD:
  300. return V_028760_OPT_COMB_ADD;
  301. case PIPE_BLEND_SUBTRACT:
  302. return V_028760_OPT_COMB_SUBTRACT;
  303. case PIPE_BLEND_REVERSE_SUBTRACT:
  304. return V_028760_OPT_COMB_REVSUBTRACT;
  305. case PIPE_BLEND_MIN:
  306. return V_028760_OPT_COMB_MIN;
  307. case PIPE_BLEND_MAX:
  308. return V_028760_OPT_COMB_MAX;
  309. default:
  310. return V_028760_OPT_COMB_BLEND_DISABLED;
  311. }
  312. }
  313. static uint32_t si_translate_blend_opt_factor(int blend_fact, bool is_alpha)
  314. {
  315. switch (blend_fact) {
  316. case PIPE_BLENDFACTOR_ZERO:
  317. return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;
  318. case PIPE_BLENDFACTOR_ONE:
  319. return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;
  320. case PIPE_BLENDFACTOR_SRC_COLOR:
  321. return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
  322. : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;
  323. case PIPE_BLENDFACTOR_INV_SRC_COLOR:
  324. return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
  325. : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;
  326. case PIPE_BLENDFACTOR_SRC_ALPHA:
  327. return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;
  328. case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
  329. return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;
  330. case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
  331. return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
  332. : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
  333. default:
  334. return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
  335. }
  336. }
  337. static void si_blend_check_commutativity(struct si_screen *sscreen,
  338. struct si_state_blend *blend,
  339. enum pipe_blend_func func,
  340. enum pipe_blendfactor src,
  341. enum pipe_blendfactor dst,
  342. unsigned chanmask)
  343. {
  344. /* Src factor is allowed when it does not depend on Dst */
  345. static const uint32_t src_allowed =
  346. (1u << PIPE_BLENDFACTOR_ONE) |
  347. (1u << PIPE_BLENDFACTOR_SRC_COLOR) |
  348. (1u << PIPE_BLENDFACTOR_SRC_ALPHA) |
  349. (1u << PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) |
  350. (1u << PIPE_BLENDFACTOR_CONST_COLOR) |
  351. (1u << PIPE_BLENDFACTOR_CONST_ALPHA) |
  352. (1u << PIPE_BLENDFACTOR_SRC1_COLOR) |
  353. (1u << PIPE_BLENDFACTOR_SRC1_ALPHA) |
  354. (1u << PIPE_BLENDFACTOR_ZERO) |
  355. (1u << PIPE_BLENDFACTOR_INV_SRC_COLOR) |
  356. (1u << PIPE_BLENDFACTOR_INV_SRC_ALPHA) |
  357. (1u << PIPE_BLENDFACTOR_INV_CONST_COLOR) |
  358. (1u << PIPE_BLENDFACTOR_INV_CONST_ALPHA) |
  359. (1u << PIPE_BLENDFACTOR_INV_SRC1_COLOR) |
  360. (1u << PIPE_BLENDFACTOR_INV_SRC1_ALPHA);
  361. if (dst == PIPE_BLENDFACTOR_ONE &&
  362. (src_allowed & (1u << src))) {
  363. /* Addition is commutative, but floating point addition isn't
  364. * associative: subtle changes can be introduced via different
  365. * rounding.
  366. *
  367. * Out-of-order is also non-deterministic, which means that
  368. * this breaks OpenGL invariance requirements. So only enable
  369. * out-of-order additive blending if explicitly allowed by a
  370. * setting.
  371. */
  372. if (func == PIPE_BLEND_MAX || func == PIPE_BLEND_MIN ||
  373. (func == PIPE_BLEND_ADD && sscreen->commutative_blend_add))
  374. blend->commutative_4bit |= chanmask;
  375. }
  376. }
  377. /**
  378. * Get rid of DST in the blend factors by commuting the operands:
  379. * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
  380. */
  381. static void si_blend_remove_dst(unsigned *func, unsigned *src_factor,
  382. unsigned *dst_factor, unsigned expected_dst,
  383. unsigned replacement_src)
  384. {
  385. if (*src_factor == expected_dst &&
  386. *dst_factor == PIPE_BLENDFACTOR_ZERO) {
  387. *src_factor = PIPE_BLENDFACTOR_ZERO;
  388. *dst_factor = replacement_src;
  389. /* Commuting the operands requires reversing subtractions. */
  390. if (*func == PIPE_BLEND_SUBTRACT)
  391. *func = PIPE_BLEND_REVERSE_SUBTRACT;
  392. else if (*func == PIPE_BLEND_REVERSE_SUBTRACT)
  393. *func = PIPE_BLEND_SUBTRACT;
  394. }
  395. }
  396. static bool si_blend_factor_uses_dst(unsigned factor)
  397. {
  398. return factor == PIPE_BLENDFACTOR_DST_COLOR ||
  399. factor == PIPE_BLENDFACTOR_DST_ALPHA ||
  400. factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
  401. factor == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
  402. factor == PIPE_BLENDFACTOR_INV_DST_COLOR;
  403. }
  404. static void *si_create_blend_state_mode(struct pipe_context *ctx,
  405. const struct pipe_blend_state *state,
  406. unsigned mode)
  407. {
  408. struct si_context *sctx = (struct si_context*)ctx;
  409. struct si_state_blend *blend = CALLOC_STRUCT(si_state_blend);
  410. struct si_pm4_state *pm4 = &blend->pm4;
  411. uint32_t sx_mrt_blend_opt[8] = {0};
  412. uint32_t color_control = 0;
  413. if (!blend)
  414. return NULL;
  415. blend->alpha_to_coverage = state->alpha_to_coverage;
  416. blend->alpha_to_one = state->alpha_to_one;
  417. blend->dual_src_blend = util_blend_state_is_dual(state, 0);
  418. blend->logicop_enable = state->logicop_enable;
  419. if (state->logicop_enable) {
  420. color_control |= S_028808_ROP3(state->logicop_func | (state->logicop_func << 4));
  421. } else {
  422. color_control |= S_028808_ROP3(0xcc);
  423. }
  424. si_pm4_set_reg(pm4, R_028B70_DB_ALPHA_TO_MASK,
  425. S_028B70_ALPHA_TO_MASK_ENABLE(state->alpha_to_coverage) |
  426. S_028B70_ALPHA_TO_MASK_OFFSET0(2) |
  427. S_028B70_ALPHA_TO_MASK_OFFSET1(2) |
  428. S_028B70_ALPHA_TO_MASK_OFFSET2(2) |
  429. S_028B70_ALPHA_TO_MASK_OFFSET3(2));
  430. if (state->alpha_to_coverage)
  431. blend->need_src_alpha_4bit |= 0xf;
  432. blend->cb_target_mask = 0;
  433. blend->cb_target_enabled_4bit = 0;
  434. for (int i = 0; i < 8; i++) {
  435. /* state->rt entries > 0 only written if independent blending */
  436. const int j = state->independent_blend_enable ? i : 0;
  437. unsigned eqRGB = state->rt[j].rgb_func;
  438. unsigned srcRGB = state->rt[j].rgb_src_factor;
  439. unsigned dstRGB = state->rt[j].rgb_dst_factor;
  440. unsigned eqA = state->rt[j].alpha_func;
  441. unsigned srcA = state->rt[j].alpha_src_factor;
  442. unsigned dstA = state->rt[j].alpha_dst_factor;
  443. unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;
  444. unsigned blend_cntl = 0;
  445. sx_mrt_blend_opt[i] =
  446. S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED) |
  447. S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED);
  448. /* Only set dual source blending for MRT0 to avoid a hang. */
  449. if (i >= 1 && blend->dual_src_blend) {
  450. /* Vulkan does this for dual source blending. */
  451. if (i == 1)
  452. blend_cntl |= S_028780_ENABLE(1);
  453. si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
  454. continue;
  455. }
  456. /* Only addition and subtraction equations are supported with
  457. * dual source blending.
  458. */
  459. if (blend->dual_src_blend &&
  460. (eqRGB == PIPE_BLEND_MIN || eqRGB == PIPE_BLEND_MAX ||
  461. eqA == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MAX)) {
  462. assert(!"Unsupported equation for dual source blending");
  463. si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
  464. continue;
  465. }
  466. /* cb_render_state will disable unused ones */
  467. blend->cb_target_mask |= (unsigned)state->rt[j].colormask << (4 * i);
  468. if (state->rt[j].colormask)
  469. blend->cb_target_enabled_4bit |= 0xf << (4 * i);
  470. if (!state->rt[j].colormask || !state->rt[j].blend_enable) {
  471. si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
  472. continue;
  473. }
  474. si_blend_check_commutativity(sctx->screen, blend,
  475. eqRGB, srcRGB, dstRGB, 0x7 << (4 * i));
  476. si_blend_check_commutativity(sctx->screen, blend,
  477. eqA, srcA, dstA, 0x8 << (4 * i));
  478. /* Blending optimizations for RB+.
  479. * These transformations don't change the behavior.
  480. *
  481. * First, get rid of DST in the blend factors:
  482. * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
  483. */
  484. si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB,
  485. PIPE_BLENDFACTOR_DST_COLOR,
  486. PIPE_BLENDFACTOR_SRC_COLOR);
  487. si_blend_remove_dst(&eqA, &srcA, &dstA,
  488. PIPE_BLENDFACTOR_DST_COLOR,
  489. PIPE_BLENDFACTOR_SRC_COLOR);
  490. si_blend_remove_dst(&eqA, &srcA, &dstA,
  491. PIPE_BLENDFACTOR_DST_ALPHA,
  492. PIPE_BLENDFACTOR_SRC_ALPHA);
  493. /* Look up the ideal settings from tables. */
  494. srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);
  495. dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);
  496. srcA_opt = si_translate_blend_opt_factor(srcA, true);
  497. dstA_opt = si_translate_blend_opt_factor(dstA, true);
  498. /* Handle interdependencies. */
  499. if (si_blend_factor_uses_dst(srcRGB))
  500. dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
  501. if (si_blend_factor_uses_dst(srcA))
  502. dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
  503. if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE &&
  504. (dstRGB == PIPE_BLENDFACTOR_ZERO ||
  505. dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
  506. dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE))
  507. dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
  508. /* Set the final value. */
  509. sx_mrt_blend_opt[i] =
  510. S_028760_COLOR_SRC_OPT(srcRGB_opt) |
  511. S_028760_COLOR_DST_OPT(dstRGB_opt) |
  512. S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |
  513. S_028760_ALPHA_SRC_OPT(srcA_opt) |
  514. S_028760_ALPHA_DST_OPT(dstA_opt) |
  515. S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));
  516. /* Set blend state. */
  517. blend_cntl |= S_028780_ENABLE(1);
  518. blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
  519. blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
  520. blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
  521. if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
  522. blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
  523. blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
  524. blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
  525. blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
  526. }
  527. si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
  528. blend->blend_enable_4bit |= 0xfu << (i * 4);
  529. /* This is only important for formats without alpha. */
  530. if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
  531. dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
  532. srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
  533. dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
  534. srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  535. dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA)
  536. blend->need_src_alpha_4bit |= 0xfu << (i * 4);
  537. }
  538. if (blend->cb_target_mask) {
  539. color_control |= S_028808_MODE(mode);
  540. } else {
  541. color_control |= S_028808_MODE(V_028808_CB_DISABLE);
  542. }
  543. if (sctx->screen->rbplus_allowed) {
  544. /* Disable RB+ blend optimizations for dual source blending.
  545. * Vulkan does this.
  546. */
  547. if (blend->dual_src_blend) {
  548. for (int i = 0; i < 8; i++) {
  549. sx_mrt_blend_opt[i] =
  550. S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE) |
  551. S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE);
  552. }
  553. }
  554. for (int i = 0; i < 8; i++)
  555. si_pm4_set_reg(pm4, R_028760_SX_MRT0_BLEND_OPT + i * 4,
  556. sx_mrt_blend_opt[i]);
  557. /* RB+ doesn't work with dual source blending, logic op, and RESOLVE. */
  558. if (blend->dual_src_blend || state->logicop_enable ||
  559. mode == V_028808_CB_RESOLVE)
  560. color_control |= S_028808_DISABLE_DUAL_QUAD(1);
  561. }
  562. si_pm4_set_reg(pm4, R_028808_CB_COLOR_CONTROL, color_control);
  563. return blend;
  564. }
  565. static void *si_create_blend_state(struct pipe_context *ctx,
  566. const struct pipe_blend_state *state)
  567. {
  568. return si_create_blend_state_mode(ctx, state, V_028808_CB_NORMAL);
  569. }
  570. static void si_bind_blend_state(struct pipe_context *ctx, void *state)
  571. {
  572. struct si_context *sctx = (struct si_context *)ctx;
  573. struct si_state_blend *old_blend = sctx->queued.named.blend;
  574. struct si_state_blend *blend = (struct si_state_blend *)state;
  575. if (!state)
  576. return;
  577. si_pm4_bind_state(sctx, blend, state);
  578. if (!old_blend ||
  579. old_blend->cb_target_mask != blend->cb_target_mask ||
  580. old_blend->dual_src_blend != blend->dual_src_blend ||
  581. (old_blend->blend_enable_4bit != blend->blend_enable_4bit &&
  582. sctx->framebuffer.nr_samples >= 2 &&
  583. sctx->screen->dcc_msaa_allowed))
  584. si_mark_atom_dirty(sctx, &sctx->atoms.s.cb_render_state);
  585. if (!old_blend ||
  586. old_blend->cb_target_mask != blend->cb_target_mask ||
  587. old_blend->alpha_to_coverage != blend->alpha_to_coverage ||
  588. old_blend->alpha_to_one != blend->alpha_to_one ||
  589. old_blend->dual_src_blend != blend->dual_src_blend ||
  590. old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
  591. old_blend->need_src_alpha_4bit != blend->need_src_alpha_4bit)
  592. sctx->do_update_shaders = true;
  593. if (sctx->screen->dpbb_allowed &&
  594. (!old_blend ||
  595. old_blend->alpha_to_coverage != blend->alpha_to_coverage ||
  596. old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
  597. old_blend->cb_target_enabled_4bit != blend->cb_target_enabled_4bit))
  598. si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
  599. if (sctx->screen->has_out_of_order_rast &&
  600. (!old_blend ||
  601. (old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
  602. old_blend->cb_target_enabled_4bit != blend->cb_target_enabled_4bit ||
  603. old_blend->commutative_4bit != blend->commutative_4bit ||
  604. old_blend->logicop_enable != blend->logicop_enable)))
  605. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
  606. }
  607. static void si_delete_blend_state(struct pipe_context *ctx, void *state)
  608. {
  609. struct si_context *sctx = (struct si_context *)ctx;
  610. si_pm4_delete_state(sctx, blend, (struct si_state_blend *)state);
  611. }
  612. static void si_set_blend_color(struct pipe_context *ctx,
  613. const struct pipe_blend_color *state)
  614. {
  615. struct si_context *sctx = (struct si_context *)ctx;
  616. static const struct pipe_blend_color zeros;
  617. sctx->blend_color.state = *state;
  618. sctx->blend_color.any_nonzeros = memcmp(state, &zeros, sizeof(*state)) != 0;
  619. si_mark_atom_dirty(sctx, &sctx->atoms.s.blend_color);
  620. }
  621. static void si_emit_blend_color(struct si_context *sctx)
  622. {
  623. struct radeon_winsys_cs *cs = sctx->gfx_cs;
  624. radeon_set_context_reg_seq(cs, R_028414_CB_BLEND_RED, 4);
  625. radeon_emit_array(cs, (uint32_t*)sctx->blend_color.state.color, 4);
  626. }
  627. /*
  628. * Clipping
  629. */
  630. static void si_set_clip_state(struct pipe_context *ctx,
  631. const struct pipe_clip_state *state)
  632. {
  633. struct si_context *sctx = (struct si_context *)ctx;
  634. struct pipe_constant_buffer cb;
  635. static const struct pipe_clip_state zeros;
  636. if (memcmp(&sctx->clip_state.state, state, sizeof(*state)) == 0)
  637. return;
  638. sctx->clip_state.state = *state;
  639. sctx->clip_state.any_nonzeros = memcmp(state, &zeros, sizeof(*state)) != 0;
  640. si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_state);
  641. cb.buffer = NULL;
  642. cb.user_buffer = state->ucp;
  643. cb.buffer_offset = 0;
  644. cb.buffer_size = 4*4*8;
  645. si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES, &cb);
  646. pipe_resource_reference(&cb.buffer, NULL);
  647. }
  648. static void si_emit_clip_state(struct si_context *sctx)
  649. {
  650. struct radeon_winsys_cs *cs = sctx->gfx_cs;
  651. radeon_set_context_reg_seq(cs, R_0285BC_PA_CL_UCP_0_X, 6*4);
  652. radeon_emit_array(cs, (uint32_t*)sctx->clip_state.state.ucp, 6*4);
  653. }
  654. static void si_emit_clip_regs(struct si_context *sctx)
  655. {
  656. struct si_shader *vs = si_get_vs_state(sctx);
  657. struct si_shader_selector *vs_sel = vs->selector;
  658. struct tgsi_shader_info *info = &vs_sel->info;
  659. struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
  660. unsigned window_space =
  661. info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
  662. unsigned clipdist_mask = vs_sel->clipdist_mask;
  663. unsigned ucp_mask = clipdist_mask ? 0 : rs->clip_plane_enable & SIX_BITS;
  664. unsigned culldist_mask = vs_sel->culldist_mask;
  665. unsigned total_mask;
  666. if (vs->key.opt.clip_disable) {
  667. assert(!info->culldist_writemask);
  668. clipdist_mask = 0;
  669. culldist_mask = 0;
  670. }
  671. total_mask = clipdist_mask | culldist_mask;
  672. /* Clip distances on points have no effect, so need to be implemented
  673. * as cull distances. This applies for the clipvertex case as well.
  674. *
  675. * Setting this for primitives other than points should have no adverse
  676. * effects.
  677. */
  678. clipdist_mask &= rs->clip_plane_enable;
  679. culldist_mask |= clipdist_mask;
  680. radeon_opt_set_context_reg(sctx, R_02881C_PA_CL_VS_OUT_CNTL,
  681. SI_TRACKED_PA_CL_VS_OUT_CNTL,
  682. vs_sel->pa_cl_vs_out_cntl |
  683. S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0F) != 0) |
  684. S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xF0) != 0) |
  685. clipdist_mask | (culldist_mask << 8));
  686. radeon_opt_set_context_reg(sctx, R_028810_PA_CL_CLIP_CNTL,
  687. SI_TRACKED_PA_CL_CLIP_CNTL,
  688. rs->pa_cl_clip_cntl |
  689. ucp_mask |
  690. S_028810_CLIP_DISABLE(window_space));
  691. }
  692. /*
  693. * inferred state between framebuffer and rasterizer
  694. */
  695. static void si_update_poly_offset_state(struct si_context *sctx)
  696. {
  697. struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
  698. if (!rs || !rs->uses_poly_offset || !sctx->framebuffer.state.zsbuf) {
  699. si_pm4_bind_state(sctx, poly_offset, NULL);
  700. return;
  701. }
  702. /* Use the user format, not db_render_format, so that the polygon
  703. * offset behaves as expected by applications.
  704. */
  705. switch (sctx->framebuffer.state.zsbuf->texture->format) {
  706. case PIPE_FORMAT_Z16_UNORM:
  707. si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[0]);
  708. break;
  709. default: /* 24-bit */
  710. si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[1]);
  711. break;
  712. case PIPE_FORMAT_Z32_FLOAT:
  713. case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
  714. si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[2]);
  715. break;
  716. }
  717. }
  718. /*
  719. * Rasterizer
  720. */
  721. static uint32_t si_translate_fill(uint32_t func)
  722. {
  723. switch(func) {
  724. case PIPE_POLYGON_MODE_FILL:
  725. return V_028814_X_DRAW_TRIANGLES;
  726. case PIPE_POLYGON_MODE_LINE:
  727. return V_028814_X_DRAW_LINES;
  728. case PIPE_POLYGON_MODE_POINT:
  729. return V_028814_X_DRAW_POINTS;
  730. default:
  731. assert(0);
  732. return V_028814_X_DRAW_POINTS;
  733. }
  734. }
  735. static void *si_create_rs_state(struct pipe_context *ctx,
  736. const struct pipe_rasterizer_state *state)
  737. {
  738. struct si_screen *sscreen = ((struct si_context *)ctx)->screen;
  739. struct si_state_rasterizer *rs = CALLOC_STRUCT(si_state_rasterizer);
  740. struct si_pm4_state *pm4 = &rs->pm4;
  741. unsigned tmp, i;
  742. float psize_min, psize_max;
  743. if (!rs) {
  744. return NULL;
  745. }
  746. rs->scissor_enable = state->scissor;
  747. rs->clip_halfz = state->clip_halfz;
  748. rs->two_side = state->light_twoside;
  749. rs->multisample_enable = state->multisample;
  750. rs->force_persample_interp = state->force_persample_interp;
  751. rs->clip_plane_enable = state->clip_plane_enable;
  752. rs->line_stipple_enable = state->line_stipple_enable;
  753. rs->poly_stipple_enable = state->poly_stipple_enable;
  754. rs->line_smooth = state->line_smooth;
  755. rs->line_width = state->line_width;
  756. rs->poly_smooth = state->poly_smooth;
  757. rs->uses_poly_offset = state->offset_point || state->offset_line ||
  758. state->offset_tri;
  759. rs->clamp_fragment_color = state->clamp_fragment_color;
  760. rs->clamp_vertex_color = state->clamp_vertex_color;
  761. rs->flatshade = state->flatshade;
  762. rs->sprite_coord_enable = state->sprite_coord_enable;
  763. rs->rasterizer_discard = state->rasterizer_discard;
  764. rs->pa_sc_line_stipple = state->line_stipple_enable ?
  765. S_028A0C_LINE_PATTERN(state->line_stipple_pattern) |
  766. S_028A0C_REPEAT_COUNT(state->line_stipple_factor) : 0;
  767. rs->pa_cl_clip_cntl =
  768. S_028810_DX_CLIP_SPACE_DEF(state->clip_halfz) |
  769. S_028810_ZCLIP_NEAR_DISABLE(!state->depth_clip) |
  770. S_028810_ZCLIP_FAR_DISABLE(!state->depth_clip) |
  771. S_028810_DX_RASTERIZATION_KILL(state->rasterizer_discard) |
  772. S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
  773. si_pm4_set_reg(pm4, R_0286D4_SPI_INTERP_CONTROL_0,
  774. S_0286D4_FLAT_SHADE_ENA(1) |
  775. S_0286D4_PNT_SPRITE_ENA(state->point_quad_rasterization) |
  776. S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
  777. S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
  778. S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
  779. S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
  780. S_0286D4_PNT_SPRITE_TOP_1(state->sprite_coord_mode != PIPE_SPRITE_COORD_UPPER_LEFT));
  781. /* point size 12.4 fixed point */
  782. tmp = (unsigned)(state->point_size * 8.0);
  783. si_pm4_set_reg(pm4, R_028A00_PA_SU_POINT_SIZE, S_028A00_HEIGHT(tmp) | S_028A00_WIDTH(tmp));
  784. if (state->point_size_per_vertex) {
  785. psize_min = util_get_min_point_size(state);
  786. psize_max = 8192;
  787. } else {
  788. /* Force the point size to be as if the vertex output was disabled. */
  789. psize_min = state->point_size;
  790. psize_max = state->point_size;
  791. }
  792. rs->max_point_size = psize_max;
  793. /* Divide by two, because 0.5 = 1 pixel. */
  794. si_pm4_set_reg(pm4, R_028A04_PA_SU_POINT_MINMAX,
  795. S_028A04_MIN_SIZE(si_pack_float_12p4(psize_min/2)) |
  796. S_028A04_MAX_SIZE(si_pack_float_12p4(psize_max/2)));
  797. si_pm4_set_reg(pm4, R_028A08_PA_SU_LINE_CNTL,
  798. S_028A08_WIDTH(si_pack_float_12p4(state->line_width/2)));
  799. si_pm4_set_reg(pm4, R_028A48_PA_SC_MODE_CNTL_0,
  800. S_028A48_LINE_STIPPLE_ENABLE(state->line_stipple_enable) |
  801. S_028A48_MSAA_ENABLE(state->multisample ||
  802. state->poly_smooth ||
  803. state->line_smooth) |
  804. S_028A48_VPORT_SCISSOR_ENABLE(1) |
  805. S_028A48_ALTERNATE_RBS_PER_TILE(sscreen->info.chip_class >= GFX9));
  806. si_pm4_set_reg(pm4, R_028BE4_PA_SU_VTX_CNTL,
  807. S_028BE4_PIX_CENTER(state->half_pixel_center) |
  808. S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH));
  809. si_pm4_set_reg(pm4, R_028B7C_PA_SU_POLY_OFFSET_CLAMP, fui(state->offset_clamp));
  810. si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL,
  811. S_028814_PROVOKING_VTX_LAST(!state->flatshade_first) |
  812. S_028814_CULL_FRONT((state->cull_face & PIPE_FACE_FRONT) ? 1 : 0) |
  813. S_028814_CULL_BACK((state->cull_face & PIPE_FACE_BACK) ? 1 : 0) |
  814. S_028814_FACE(!state->front_ccw) |
  815. S_028814_POLY_OFFSET_FRONT_ENABLE(util_get_offset(state, state->fill_front)) |
  816. S_028814_POLY_OFFSET_BACK_ENABLE(util_get_offset(state, state->fill_back)) |
  817. S_028814_POLY_OFFSET_PARA_ENABLE(state->offset_point || state->offset_line) |
  818. S_028814_POLY_MODE(state->fill_front != PIPE_POLYGON_MODE_FILL ||
  819. state->fill_back != PIPE_POLYGON_MODE_FILL) |
  820. S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(state->fill_front)) |
  821. S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(state->fill_back)));
  822. if (!rs->uses_poly_offset)
  823. return rs;
  824. rs->pm4_poly_offset = CALLOC(3, sizeof(struct si_pm4_state));
  825. if (!rs->pm4_poly_offset) {
  826. FREE(rs);
  827. return NULL;
  828. }
  829. /* Precalculate polygon offset states for 16-bit, 24-bit, and 32-bit zbuffers. */
  830. for (i = 0; i < 3; i++) {
  831. struct si_pm4_state *pm4 = &rs->pm4_poly_offset[i];
  832. float offset_units = state->offset_units;
  833. float offset_scale = state->offset_scale * 16.0f;
  834. uint32_t pa_su_poly_offset_db_fmt_cntl = 0;
  835. if (!state->offset_units_unscaled) {
  836. switch (i) {
  837. case 0: /* 16-bit zbuffer */
  838. offset_units *= 4.0f;
  839. pa_su_poly_offset_db_fmt_cntl =
  840. S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
  841. break;
  842. case 1: /* 24-bit zbuffer */
  843. offset_units *= 2.0f;
  844. pa_su_poly_offset_db_fmt_cntl =
  845. S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
  846. break;
  847. case 2: /* 32-bit zbuffer */
  848. offset_units *= 1.0f;
  849. pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
  850. S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
  851. break;
  852. }
  853. }
  854. si_pm4_set_reg(pm4, R_028B80_PA_SU_POLY_OFFSET_FRONT_SCALE,
  855. fui(offset_scale));
  856. si_pm4_set_reg(pm4, R_028B84_PA_SU_POLY_OFFSET_FRONT_OFFSET,
  857. fui(offset_units));
  858. si_pm4_set_reg(pm4, R_028B88_PA_SU_POLY_OFFSET_BACK_SCALE,
  859. fui(offset_scale));
  860. si_pm4_set_reg(pm4, R_028B8C_PA_SU_POLY_OFFSET_BACK_OFFSET,
  861. fui(offset_units));
  862. si_pm4_set_reg(pm4, R_028B78_PA_SU_POLY_OFFSET_DB_FMT_CNTL,
  863. pa_su_poly_offset_db_fmt_cntl);
  864. }
  865. return rs;
  866. }
  867. static void si_bind_rs_state(struct pipe_context *ctx, void *state)
  868. {
  869. struct si_context *sctx = (struct si_context *)ctx;
  870. struct si_state_rasterizer *old_rs =
  871. (struct si_state_rasterizer*)sctx->queued.named.rasterizer;
  872. struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
  873. if (!state)
  874. return;
  875. if (!old_rs || old_rs->multisample_enable != rs->multisample_enable) {
  876. si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
  877. /* Update the small primitive filter workaround if necessary. */
  878. if (sctx->screen->has_msaa_sample_loc_bug &&
  879. sctx->framebuffer.nr_samples > 1)
  880. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_sample_locs);
  881. }
  882. sctx->current_vs_state &= C_VS_STATE_CLAMP_VERTEX_COLOR;
  883. sctx->current_vs_state |= S_VS_STATE_CLAMP_VERTEX_COLOR(rs->clamp_vertex_color);
  884. si_pm4_bind_state(sctx, rasterizer, rs);
  885. si_update_poly_offset_state(sctx);
  886. if (!old_rs ||
  887. (old_rs->scissor_enable != rs->scissor_enable ||
  888. old_rs->line_width != rs->line_width ||
  889. old_rs->max_point_size != rs->max_point_size)) {
  890. sctx->scissors.dirty_mask = (1 << SI_MAX_VIEWPORTS) - 1;
  891. si_mark_atom_dirty(sctx, &sctx->atoms.s.scissors);
  892. }
  893. if (!old_rs ||
  894. old_rs->clip_halfz != rs->clip_halfz) {
  895. sctx->viewports.depth_range_dirty_mask = (1 << SI_MAX_VIEWPORTS) - 1;
  896. si_mark_atom_dirty(sctx, &sctx->atoms.s.viewports);
  897. }
  898. if (!old_rs ||
  899. old_rs->clip_plane_enable != rs->clip_plane_enable ||
  900. old_rs->pa_cl_clip_cntl != rs->pa_cl_clip_cntl)
  901. si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_regs);
  902. sctx->ia_multi_vgt_param_key.u.line_stipple_enabled =
  903. rs->line_stipple_enable;
  904. if (!old_rs ||
  905. old_rs->clip_plane_enable != rs->clip_plane_enable ||
  906. old_rs->rasterizer_discard != rs->rasterizer_discard ||
  907. old_rs->sprite_coord_enable != rs->sprite_coord_enable ||
  908. old_rs->flatshade != rs->flatshade ||
  909. old_rs->two_side != rs->two_side ||
  910. old_rs->multisample_enable != rs->multisample_enable ||
  911. old_rs->poly_stipple_enable != rs->poly_stipple_enable ||
  912. old_rs->poly_smooth != rs->poly_smooth ||
  913. old_rs->line_smooth != rs->line_smooth ||
  914. old_rs->clamp_fragment_color != rs->clamp_fragment_color ||
  915. old_rs->force_persample_interp != rs->force_persample_interp)
  916. sctx->do_update_shaders = true;
  917. }
  918. static void si_delete_rs_state(struct pipe_context *ctx, void *state)
  919. {
  920. struct si_context *sctx = (struct si_context *)ctx;
  921. struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
  922. if (sctx->queued.named.rasterizer == state)
  923. si_pm4_bind_state(sctx, poly_offset, NULL);
  924. FREE(rs->pm4_poly_offset);
  925. si_pm4_delete_state(sctx, rasterizer, rs);
  926. }
  927. /*
  928. * infeered state between dsa and stencil ref
  929. */
  930. static void si_emit_stencil_ref(struct si_context *sctx)
  931. {
  932. struct radeon_winsys_cs *cs = sctx->gfx_cs;
  933. struct pipe_stencil_ref *ref = &sctx->stencil_ref.state;
  934. struct si_dsa_stencil_ref_part *dsa = &sctx->stencil_ref.dsa_part;
  935. radeon_set_context_reg_seq(cs, R_028430_DB_STENCILREFMASK, 2);
  936. radeon_emit(cs, S_028430_STENCILTESTVAL(ref->ref_value[0]) |
  937. S_028430_STENCILMASK(dsa->valuemask[0]) |
  938. S_028430_STENCILWRITEMASK(dsa->writemask[0]) |
  939. S_028430_STENCILOPVAL(1));
  940. radeon_emit(cs, S_028434_STENCILTESTVAL_BF(ref->ref_value[1]) |
  941. S_028434_STENCILMASK_BF(dsa->valuemask[1]) |
  942. S_028434_STENCILWRITEMASK_BF(dsa->writemask[1]) |
  943. S_028434_STENCILOPVAL_BF(1));
  944. }
  945. static void si_set_stencil_ref(struct pipe_context *ctx,
  946. const struct pipe_stencil_ref *state)
  947. {
  948. struct si_context *sctx = (struct si_context *)ctx;
  949. if (memcmp(&sctx->stencil_ref.state, state, sizeof(*state)) == 0)
  950. return;
  951. sctx->stencil_ref.state = *state;
  952. si_mark_atom_dirty(sctx, &sctx->atoms.s.stencil_ref);
  953. }
  954. /*
  955. * DSA
  956. */
  957. static uint32_t si_translate_stencil_op(int s_op)
  958. {
  959. switch (s_op) {
  960. case PIPE_STENCIL_OP_KEEP:
  961. return V_02842C_STENCIL_KEEP;
  962. case PIPE_STENCIL_OP_ZERO:
  963. return V_02842C_STENCIL_ZERO;
  964. case PIPE_STENCIL_OP_REPLACE:
  965. return V_02842C_STENCIL_REPLACE_TEST;
  966. case PIPE_STENCIL_OP_INCR:
  967. return V_02842C_STENCIL_ADD_CLAMP;
  968. case PIPE_STENCIL_OP_DECR:
  969. return V_02842C_STENCIL_SUB_CLAMP;
  970. case PIPE_STENCIL_OP_INCR_WRAP:
  971. return V_02842C_STENCIL_ADD_WRAP;
  972. case PIPE_STENCIL_OP_DECR_WRAP:
  973. return V_02842C_STENCIL_SUB_WRAP;
  974. case PIPE_STENCIL_OP_INVERT:
  975. return V_02842C_STENCIL_INVERT;
  976. default:
  977. PRINT_ERR("Unknown stencil op %d", s_op);
  978. assert(0);
  979. break;
  980. }
  981. return 0;
  982. }
  983. static bool si_dsa_writes_stencil(const struct pipe_stencil_state *s)
  984. {
  985. return s->enabled && s->writemask &&
  986. (s->fail_op != PIPE_STENCIL_OP_KEEP ||
  987. s->zfail_op != PIPE_STENCIL_OP_KEEP ||
  988. s->zpass_op != PIPE_STENCIL_OP_KEEP);
  989. }
  990. static bool si_order_invariant_stencil_op(enum pipe_stencil_op op)
  991. {
  992. /* REPLACE is normally order invariant, except when the stencil
  993. * reference value is written by the fragment shader. Tracking this
  994. * interaction does not seem worth the effort, so be conservative. */
  995. return op != PIPE_STENCIL_OP_INCR &&
  996. op != PIPE_STENCIL_OP_DECR &&
  997. op != PIPE_STENCIL_OP_REPLACE;
  998. }
  999. /* Compute whether, assuming Z writes are disabled, this stencil state is order
  1000. * invariant in the sense that the set of passing fragments as well as the
  1001. * final stencil buffer result does not depend on the order of fragments. */
  1002. static bool si_order_invariant_stencil_state(const struct pipe_stencil_state *state)
  1003. {
  1004. return !state->enabled || !state->writemask ||
  1005. /* The following assumes that Z writes are disabled. */
  1006. (state->func == PIPE_FUNC_ALWAYS &&
  1007. si_order_invariant_stencil_op(state->zpass_op) &&
  1008. si_order_invariant_stencil_op(state->zfail_op)) ||
  1009. (state->func == PIPE_FUNC_NEVER &&
  1010. si_order_invariant_stencil_op(state->fail_op));
  1011. }
  1012. static void *si_create_dsa_state(struct pipe_context *ctx,
  1013. const struct pipe_depth_stencil_alpha_state *state)
  1014. {
  1015. struct si_context *sctx = (struct si_context *)ctx;
  1016. struct si_state_dsa *dsa = CALLOC_STRUCT(si_state_dsa);
  1017. struct si_pm4_state *pm4 = &dsa->pm4;
  1018. unsigned db_depth_control;
  1019. uint32_t db_stencil_control = 0;
  1020. if (!dsa) {
  1021. return NULL;
  1022. }
  1023. dsa->stencil_ref.valuemask[0] = state->stencil[0].valuemask;
  1024. dsa->stencil_ref.valuemask[1] = state->stencil[1].valuemask;
  1025. dsa->stencil_ref.writemask[0] = state->stencil[0].writemask;
  1026. dsa->stencil_ref.writemask[1] = state->stencil[1].writemask;
  1027. db_depth_control = S_028800_Z_ENABLE(state->depth.enabled) |
  1028. S_028800_Z_WRITE_ENABLE(state->depth.writemask) |
  1029. S_028800_ZFUNC(state->depth.func) |
  1030. S_028800_DEPTH_BOUNDS_ENABLE(state->depth.bounds_test);
  1031. /* stencil */
  1032. if (state->stencil[0].enabled) {
  1033. db_depth_control |= S_028800_STENCIL_ENABLE(1);
  1034. db_depth_control |= S_028800_STENCILFUNC(state->stencil[0].func);
  1035. db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(state->stencil[0].fail_op));
  1036. db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(state->stencil[0].zpass_op));
  1037. db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(state->stencil[0].zfail_op));
  1038. if (state->stencil[1].enabled) {
  1039. db_depth_control |= S_028800_BACKFACE_ENABLE(1);
  1040. db_depth_control |= S_028800_STENCILFUNC_BF(state->stencil[1].func);
  1041. db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(state->stencil[1].fail_op));
  1042. db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(state->stencil[1].zpass_op));
  1043. db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(state->stencil[1].zfail_op));
  1044. }
  1045. }
  1046. /* alpha */
  1047. if (state->alpha.enabled) {
  1048. dsa->alpha_func = state->alpha.func;
  1049. si_pm4_set_reg(pm4, R_00B030_SPI_SHADER_USER_DATA_PS_0 +
  1050. SI_SGPR_ALPHA_REF * 4, fui(state->alpha.ref_value));
  1051. } else {
  1052. dsa->alpha_func = PIPE_FUNC_ALWAYS;
  1053. }
  1054. si_pm4_set_reg(pm4, R_028800_DB_DEPTH_CONTROL, db_depth_control);
  1055. if (state->stencil[0].enabled)
  1056. si_pm4_set_reg(pm4, R_02842C_DB_STENCIL_CONTROL, db_stencil_control);
  1057. if (state->depth.bounds_test) {
  1058. si_pm4_set_reg(pm4, R_028020_DB_DEPTH_BOUNDS_MIN, fui(state->depth.bounds_min));
  1059. si_pm4_set_reg(pm4, R_028024_DB_DEPTH_BOUNDS_MAX, fui(state->depth.bounds_max));
  1060. }
  1061. dsa->depth_enabled = state->depth.enabled;
  1062. dsa->depth_write_enabled = state->depth.enabled &&
  1063. state->depth.writemask;
  1064. dsa->stencil_enabled = state->stencil[0].enabled;
  1065. dsa->stencil_write_enabled = state->stencil[0].enabled &&
  1066. (si_dsa_writes_stencil(&state->stencil[0]) ||
  1067. si_dsa_writes_stencil(&state->stencil[1]));
  1068. dsa->db_can_write = dsa->depth_write_enabled ||
  1069. dsa->stencil_write_enabled;
  1070. bool zfunc_is_ordered =
  1071. state->depth.func == PIPE_FUNC_NEVER ||
  1072. state->depth.func == PIPE_FUNC_LESS ||
  1073. state->depth.func == PIPE_FUNC_LEQUAL ||
  1074. state->depth.func == PIPE_FUNC_GREATER ||
  1075. state->depth.func == PIPE_FUNC_GEQUAL;
  1076. bool nozwrite_and_order_invariant_stencil =
  1077. !dsa->db_can_write ||
  1078. (!dsa->depth_write_enabled &&
  1079. si_order_invariant_stencil_state(&state->stencil[0]) &&
  1080. si_order_invariant_stencil_state(&state->stencil[1]));
  1081. dsa->order_invariance[1].zs =
  1082. nozwrite_and_order_invariant_stencil ||
  1083. (!dsa->stencil_write_enabled && zfunc_is_ordered);
  1084. dsa->order_invariance[0].zs = !dsa->depth_write_enabled || zfunc_is_ordered;
  1085. dsa->order_invariance[1].pass_set =
  1086. nozwrite_and_order_invariant_stencil ||
  1087. (!dsa->stencil_write_enabled &&
  1088. (state->depth.func == PIPE_FUNC_ALWAYS ||
  1089. state->depth.func == PIPE_FUNC_NEVER));
  1090. dsa->order_invariance[0].pass_set =
  1091. !dsa->depth_write_enabled ||
  1092. (state->depth.func == PIPE_FUNC_ALWAYS ||
  1093. state->depth.func == PIPE_FUNC_NEVER);
  1094. dsa->order_invariance[1].pass_last =
  1095. sctx->screen->assume_no_z_fights &&
  1096. !dsa->stencil_write_enabled &&
  1097. dsa->depth_write_enabled && zfunc_is_ordered;
  1098. dsa->order_invariance[0].pass_last =
  1099. sctx->screen->assume_no_z_fights &&
  1100. dsa->depth_write_enabled && zfunc_is_ordered;
  1101. return dsa;
  1102. }
  1103. static void si_bind_dsa_state(struct pipe_context *ctx, void *state)
  1104. {
  1105. struct si_context *sctx = (struct si_context *)ctx;
  1106. struct si_state_dsa *old_dsa = sctx->queued.named.dsa;
  1107. struct si_state_dsa *dsa = state;
  1108. if (!state)
  1109. return;
  1110. si_pm4_bind_state(sctx, dsa, dsa);
  1111. if (memcmp(&dsa->stencil_ref, &sctx->stencil_ref.dsa_part,
  1112. sizeof(struct si_dsa_stencil_ref_part)) != 0) {
  1113. sctx->stencil_ref.dsa_part = dsa->stencil_ref;
  1114. si_mark_atom_dirty(sctx, &sctx->atoms.s.stencil_ref);
  1115. }
  1116. if (!old_dsa || old_dsa->alpha_func != dsa->alpha_func)
  1117. sctx->do_update_shaders = true;
  1118. if (sctx->screen->dpbb_allowed &&
  1119. (!old_dsa ||
  1120. (old_dsa->depth_enabled != dsa->depth_enabled ||
  1121. old_dsa->stencil_enabled != dsa->stencil_enabled ||
  1122. old_dsa->db_can_write != dsa->db_can_write)))
  1123. si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
  1124. if (sctx->screen->has_out_of_order_rast &&
  1125. (!old_dsa ||
  1126. memcmp(old_dsa->order_invariance, dsa->order_invariance,
  1127. sizeof(old_dsa->order_invariance))))
  1128. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
  1129. }
  1130. static void si_delete_dsa_state(struct pipe_context *ctx, void *state)
  1131. {
  1132. struct si_context *sctx = (struct si_context *)ctx;
  1133. si_pm4_delete_state(sctx, dsa, (struct si_state_dsa *)state);
  1134. }
  1135. static void *si_create_db_flush_dsa(struct si_context *sctx)
  1136. {
  1137. struct pipe_depth_stencil_alpha_state dsa = {};
  1138. return sctx->b.create_depth_stencil_alpha_state(&sctx->b, &dsa);
  1139. }
  1140. /* DB RENDER STATE */
  1141. static void si_set_active_query_state(struct pipe_context *ctx, boolean enable)
  1142. {
  1143. struct si_context *sctx = (struct si_context*)ctx;
  1144. /* Pipeline stat & streamout queries. */
  1145. if (enable) {
  1146. sctx->flags &= ~SI_CONTEXT_STOP_PIPELINE_STATS;
  1147. sctx->flags |= SI_CONTEXT_START_PIPELINE_STATS;
  1148. } else {
  1149. sctx->flags &= ~SI_CONTEXT_START_PIPELINE_STATS;
  1150. sctx->flags |= SI_CONTEXT_STOP_PIPELINE_STATS;
  1151. }
  1152. /* Occlusion queries. */
  1153. if (sctx->occlusion_queries_disabled != !enable) {
  1154. sctx->occlusion_queries_disabled = !enable;
  1155. si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
  1156. }
  1157. }
  1158. void si_set_occlusion_query_state(struct si_context *sctx,
  1159. bool old_perfect_enable)
  1160. {
  1161. si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
  1162. bool perfect_enable = sctx->num_perfect_occlusion_queries != 0;
  1163. if (perfect_enable != old_perfect_enable)
  1164. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
  1165. }
  1166. void si_save_qbo_state(struct si_context *sctx, struct si_qbo_state *st)
  1167. {
  1168. st->saved_compute = sctx->cs_shader_state.program;
  1169. si_get_pipe_constant_buffer(sctx, PIPE_SHADER_COMPUTE, 0, &st->saved_const0);
  1170. si_get_shader_buffers(sctx, PIPE_SHADER_COMPUTE, 0, 3, st->saved_ssbo);
  1171. }
  1172. static void si_emit_db_render_state(struct si_context *sctx)
  1173. {
  1174. struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
  1175. unsigned db_shader_control, db_render_control, db_count_control;
  1176. /* DB_RENDER_CONTROL */
  1177. if (sctx->dbcb_depth_copy_enabled ||
  1178. sctx->dbcb_stencil_copy_enabled) {
  1179. db_render_control =
  1180. S_028000_DEPTH_COPY(sctx->dbcb_depth_copy_enabled) |
  1181. S_028000_STENCIL_COPY(sctx->dbcb_stencil_copy_enabled) |
  1182. S_028000_COPY_CENTROID(1) |
  1183. S_028000_COPY_SAMPLE(sctx->dbcb_copy_sample);
  1184. } else if (sctx->db_flush_depth_inplace || sctx->db_flush_stencil_inplace) {
  1185. db_render_control =
  1186. S_028000_DEPTH_COMPRESS_DISABLE(sctx->db_flush_depth_inplace) |
  1187. S_028000_STENCIL_COMPRESS_DISABLE(sctx->db_flush_stencil_inplace);
  1188. } else {
  1189. db_render_control =
  1190. S_028000_DEPTH_CLEAR_ENABLE(sctx->db_depth_clear) |
  1191. S_028000_STENCIL_CLEAR_ENABLE(sctx->db_stencil_clear);
  1192. }
  1193. /* DB_COUNT_CONTROL (occlusion queries) */
  1194. if (sctx->num_occlusion_queries > 0 &&
  1195. !sctx->occlusion_queries_disabled) {
  1196. bool perfect = sctx->num_perfect_occlusion_queries > 0;
  1197. if (sctx->chip_class >= CIK) {
  1198. db_count_control =
  1199. S_028004_PERFECT_ZPASS_COUNTS(perfect) |
  1200. S_028004_SAMPLE_RATE(sctx->framebuffer.log_samples) |
  1201. S_028004_ZPASS_ENABLE(1) |
  1202. S_028004_SLICE_EVEN_ENABLE(1) |
  1203. S_028004_SLICE_ODD_ENABLE(1);
  1204. } else {
  1205. db_count_control =
  1206. S_028004_PERFECT_ZPASS_COUNTS(perfect) |
  1207. S_028004_SAMPLE_RATE(sctx->framebuffer.log_samples);
  1208. }
  1209. } else {
  1210. /* Disable occlusion queries. */
  1211. if (sctx->chip_class >= CIK) {
  1212. db_count_control = 0;
  1213. } else {
  1214. db_count_control = S_028004_ZPASS_INCREMENT_DISABLE(1);
  1215. }
  1216. }
  1217. radeon_opt_set_context_reg2(sctx, R_028000_DB_RENDER_CONTROL,
  1218. SI_TRACKED_DB_RENDER_CONTROL, db_render_control,
  1219. db_count_control);
  1220. /* DB_RENDER_OVERRIDE2 */
  1221. radeon_opt_set_context_reg(sctx, R_028010_DB_RENDER_OVERRIDE2,
  1222. SI_TRACKED_DB_RENDER_OVERRIDE2,
  1223. S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(sctx->db_depth_disable_expclear) |
  1224. S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(sctx->db_stencil_disable_expclear) |
  1225. S_028010_DECOMPRESS_Z_ON_FLUSH(sctx->framebuffer.nr_samples >= 4));
  1226. db_shader_control = sctx->ps_db_shader_control;
  1227. /* Bug workaround for smoothing (overrasterization) on SI. */
  1228. if (sctx->chip_class == SI && sctx->smoothing_enabled) {
  1229. db_shader_control &= C_02880C_Z_ORDER;
  1230. db_shader_control |= S_02880C_Z_ORDER(V_02880C_LATE_Z);
  1231. }
  1232. /* Disable the gl_SampleMask fragment shader output if MSAA is disabled. */
  1233. if (!rs || !rs->multisample_enable)
  1234. db_shader_control &= C_02880C_MASK_EXPORT_ENABLE;
  1235. if (sctx->screen->has_rbplus &&
  1236. !sctx->screen->rbplus_allowed)
  1237. db_shader_control |= S_02880C_DUAL_QUAD_DISABLE(1);
  1238. radeon_opt_set_context_reg(sctx, R_02880C_DB_SHADER_CONTROL,
  1239. SI_TRACKED_DB_SHADER_CONTROL, db_shader_control);
  1240. }
  1241. /*
  1242. * format translation
  1243. */
  1244. static uint32_t si_translate_colorformat(enum pipe_format format)
  1245. {
  1246. const struct util_format_description *desc = util_format_description(format);
  1247. if (!desc)
  1248. return V_028C70_COLOR_INVALID;
  1249. #define HAS_SIZE(x,y,z,w) \
  1250. (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
  1251. desc->channel[2].size == (z) && desc->channel[3].size == (w))
  1252. if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
  1253. return V_028C70_COLOR_10_11_11;
  1254. if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
  1255. return V_028C70_COLOR_INVALID;
  1256. /* hw cannot support mixed formats (except depth/stencil, since
  1257. * stencil is not written to). */
  1258. if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
  1259. return V_028C70_COLOR_INVALID;
  1260. switch (desc->nr_channels) {
  1261. case 1:
  1262. switch (desc->channel[0].size) {
  1263. case 8:
  1264. return V_028C70_COLOR_8;
  1265. case 16:
  1266. return V_028C70_COLOR_16;
  1267. case 32:
  1268. return V_028C70_COLOR_32;
  1269. }
  1270. break;
  1271. case 2:
  1272. if (desc->channel[0].size == desc->channel[1].size) {
  1273. switch (desc->channel[0].size) {
  1274. case 8:
  1275. return V_028C70_COLOR_8_8;
  1276. case 16:
  1277. return V_028C70_COLOR_16_16;
  1278. case 32:
  1279. return V_028C70_COLOR_32_32;
  1280. }
  1281. } else if (HAS_SIZE(8,24,0,0)) {
  1282. return V_028C70_COLOR_24_8;
  1283. } else if (HAS_SIZE(24,8,0,0)) {
  1284. return V_028C70_COLOR_8_24;
  1285. }
  1286. break;
  1287. case 3:
  1288. if (HAS_SIZE(5,6,5,0)) {
  1289. return V_028C70_COLOR_5_6_5;
  1290. } else if (HAS_SIZE(32,8,24,0)) {
  1291. return V_028C70_COLOR_X24_8_32_FLOAT;
  1292. }
  1293. break;
  1294. case 4:
  1295. if (desc->channel[0].size == desc->channel[1].size &&
  1296. desc->channel[0].size == desc->channel[2].size &&
  1297. desc->channel[0].size == desc->channel[3].size) {
  1298. switch (desc->channel[0].size) {
  1299. case 4:
  1300. return V_028C70_COLOR_4_4_4_4;
  1301. case 8:
  1302. return V_028C70_COLOR_8_8_8_8;
  1303. case 16:
  1304. return V_028C70_COLOR_16_16_16_16;
  1305. case 32:
  1306. return V_028C70_COLOR_32_32_32_32;
  1307. }
  1308. } else if (HAS_SIZE(5,5,5,1)) {
  1309. return V_028C70_COLOR_1_5_5_5;
  1310. } else if (HAS_SIZE(1,5,5,5)) {
  1311. return V_028C70_COLOR_5_5_5_1;
  1312. } else if (HAS_SIZE(10,10,10,2)) {
  1313. return V_028C70_COLOR_2_10_10_10;
  1314. }
  1315. break;
  1316. }
  1317. return V_028C70_COLOR_INVALID;
  1318. }
  1319. static uint32_t si_colorformat_endian_swap(uint32_t colorformat)
  1320. {
  1321. if (SI_BIG_ENDIAN) {
  1322. switch(colorformat) {
  1323. /* 8-bit buffers. */
  1324. case V_028C70_COLOR_8:
  1325. return V_028C70_ENDIAN_NONE;
  1326. /* 16-bit buffers. */
  1327. case V_028C70_COLOR_5_6_5:
  1328. case V_028C70_COLOR_1_5_5_5:
  1329. case V_028C70_COLOR_4_4_4_4:
  1330. case V_028C70_COLOR_16:
  1331. case V_028C70_COLOR_8_8:
  1332. return V_028C70_ENDIAN_8IN16;
  1333. /* 32-bit buffers. */
  1334. case V_028C70_COLOR_8_8_8_8:
  1335. case V_028C70_COLOR_2_10_10_10:
  1336. case V_028C70_COLOR_8_24:
  1337. case V_028C70_COLOR_24_8:
  1338. case V_028C70_COLOR_16_16:
  1339. return V_028C70_ENDIAN_8IN32;
  1340. /* 64-bit buffers. */
  1341. case V_028C70_COLOR_16_16_16_16:
  1342. return V_028C70_ENDIAN_8IN16;
  1343. case V_028C70_COLOR_32_32:
  1344. return V_028C70_ENDIAN_8IN32;
  1345. /* 128-bit buffers. */
  1346. case V_028C70_COLOR_32_32_32_32:
  1347. return V_028C70_ENDIAN_8IN32;
  1348. default:
  1349. return V_028C70_ENDIAN_NONE; /* Unsupported. */
  1350. }
  1351. } else {
  1352. return V_028C70_ENDIAN_NONE;
  1353. }
  1354. }
  1355. static uint32_t si_translate_dbformat(enum pipe_format format)
  1356. {
  1357. switch (format) {
  1358. case PIPE_FORMAT_Z16_UNORM:
  1359. return V_028040_Z_16;
  1360. case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  1361. case PIPE_FORMAT_X8Z24_UNORM:
  1362. case PIPE_FORMAT_Z24X8_UNORM:
  1363. case PIPE_FORMAT_Z24_UNORM_S8_UINT:
  1364. return V_028040_Z_24; /* deprecated on SI */
  1365. case PIPE_FORMAT_Z32_FLOAT:
  1366. case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
  1367. return V_028040_Z_32_FLOAT;
  1368. default:
  1369. return V_028040_Z_INVALID;
  1370. }
  1371. }
  1372. /*
  1373. * Texture translation
  1374. */
  1375. static uint32_t si_translate_texformat(struct pipe_screen *screen,
  1376. enum pipe_format format,
  1377. const struct util_format_description *desc,
  1378. int first_non_void)
  1379. {
  1380. struct si_screen *sscreen = (struct si_screen*)screen;
  1381. bool uniform = true;
  1382. int i;
  1383. /* Colorspace (return non-RGB formats directly). */
  1384. switch (desc->colorspace) {
  1385. /* Depth stencil formats */
  1386. case UTIL_FORMAT_COLORSPACE_ZS:
  1387. switch (format) {
  1388. case PIPE_FORMAT_Z16_UNORM:
  1389. return V_008F14_IMG_DATA_FORMAT_16;
  1390. case PIPE_FORMAT_X24S8_UINT:
  1391. case PIPE_FORMAT_S8X24_UINT:
  1392. /*
  1393. * Implemented as an 8_8_8_8 data format to fix texture
  1394. * gathers in stencil sampling. This affects at least
  1395. * GL45-CTS.texture_cube_map_array.sampling on VI.
  1396. */
  1397. if (sscreen->info.chip_class <= VI)
  1398. return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
  1399. if (format == PIPE_FORMAT_X24S8_UINT)
  1400. return V_008F14_IMG_DATA_FORMAT_8_24;
  1401. else
  1402. return V_008F14_IMG_DATA_FORMAT_24_8;
  1403. case PIPE_FORMAT_Z24X8_UNORM:
  1404. case PIPE_FORMAT_Z24_UNORM_S8_UINT:
  1405. return V_008F14_IMG_DATA_FORMAT_8_24;
  1406. case PIPE_FORMAT_X8Z24_UNORM:
  1407. case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  1408. return V_008F14_IMG_DATA_FORMAT_24_8;
  1409. case PIPE_FORMAT_S8_UINT:
  1410. return V_008F14_IMG_DATA_FORMAT_8;
  1411. case PIPE_FORMAT_Z32_FLOAT:
  1412. return V_008F14_IMG_DATA_FORMAT_32;
  1413. case PIPE_FORMAT_X32_S8X24_UINT:
  1414. case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
  1415. return V_008F14_IMG_DATA_FORMAT_X24_8_32;
  1416. default:
  1417. goto out_unknown;
  1418. }
  1419. case UTIL_FORMAT_COLORSPACE_YUV:
  1420. goto out_unknown; /* TODO */
  1421. case UTIL_FORMAT_COLORSPACE_SRGB:
  1422. if (desc->nr_channels != 4 && desc->nr_channels != 1)
  1423. goto out_unknown;
  1424. break;
  1425. default:
  1426. break;
  1427. }
  1428. if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
  1429. if (!sscreen->info.has_format_bc1_through_bc7)
  1430. goto out_unknown;
  1431. switch (format) {
  1432. case PIPE_FORMAT_RGTC1_SNORM:
  1433. case PIPE_FORMAT_LATC1_SNORM:
  1434. case PIPE_FORMAT_RGTC1_UNORM:
  1435. case PIPE_FORMAT_LATC1_UNORM:
  1436. return V_008F14_IMG_DATA_FORMAT_BC4;
  1437. case PIPE_FORMAT_RGTC2_SNORM:
  1438. case PIPE_FORMAT_LATC2_SNORM:
  1439. case PIPE_FORMAT_RGTC2_UNORM:
  1440. case PIPE_FORMAT_LATC2_UNORM:
  1441. return V_008F14_IMG_DATA_FORMAT_BC5;
  1442. default:
  1443. goto out_unknown;
  1444. }
  1445. }
  1446. if (desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
  1447. (sscreen->info.family == CHIP_STONEY ||
  1448. sscreen->info.family == CHIP_VEGA10 ||
  1449. sscreen->info.family == CHIP_RAVEN)) {
  1450. switch (format) {
  1451. case PIPE_FORMAT_ETC1_RGB8:
  1452. case PIPE_FORMAT_ETC2_RGB8:
  1453. case PIPE_FORMAT_ETC2_SRGB8:
  1454. return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
  1455. case PIPE_FORMAT_ETC2_RGB8A1:
  1456. case PIPE_FORMAT_ETC2_SRGB8A1:
  1457. return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
  1458. case PIPE_FORMAT_ETC2_RGBA8:
  1459. case PIPE_FORMAT_ETC2_SRGBA8:
  1460. return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
  1461. case PIPE_FORMAT_ETC2_R11_UNORM:
  1462. case PIPE_FORMAT_ETC2_R11_SNORM:
  1463. return V_008F14_IMG_DATA_FORMAT_ETC2_R;
  1464. case PIPE_FORMAT_ETC2_RG11_UNORM:
  1465. case PIPE_FORMAT_ETC2_RG11_SNORM:
  1466. return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
  1467. default:
  1468. goto out_unknown;
  1469. }
  1470. }
  1471. if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
  1472. if (!sscreen->info.has_format_bc1_through_bc7)
  1473. goto out_unknown;
  1474. switch (format) {
  1475. case PIPE_FORMAT_BPTC_RGBA_UNORM:
  1476. case PIPE_FORMAT_BPTC_SRGBA:
  1477. return V_008F14_IMG_DATA_FORMAT_BC7;
  1478. case PIPE_FORMAT_BPTC_RGB_FLOAT:
  1479. case PIPE_FORMAT_BPTC_RGB_UFLOAT:
  1480. return V_008F14_IMG_DATA_FORMAT_BC6;
  1481. default:
  1482. goto out_unknown;
  1483. }
  1484. }
  1485. if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
  1486. switch (format) {
  1487. case PIPE_FORMAT_R8G8_B8G8_UNORM:
  1488. case PIPE_FORMAT_G8R8_B8R8_UNORM:
  1489. return V_008F14_IMG_DATA_FORMAT_GB_GR;
  1490. case PIPE_FORMAT_G8R8_G8B8_UNORM:
  1491. case PIPE_FORMAT_R8G8_R8B8_UNORM:
  1492. return V_008F14_IMG_DATA_FORMAT_BG_RG;
  1493. default:
  1494. goto out_unknown;
  1495. }
  1496. }
  1497. if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
  1498. if (!sscreen->info.has_format_bc1_through_bc7)
  1499. goto out_unknown;
  1500. switch (format) {
  1501. case PIPE_FORMAT_DXT1_RGB:
  1502. case PIPE_FORMAT_DXT1_RGBA:
  1503. case PIPE_FORMAT_DXT1_SRGB:
  1504. case PIPE_FORMAT_DXT1_SRGBA:
  1505. return V_008F14_IMG_DATA_FORMAT_BC1;
  1506. case PIPE_FORMAT_DXT3_RGBA:
  1507. case PIPE_FORMAT_DXT3_SRGBA:
  1508. return V_008F14_IMG_DATA_FORMAT_BC2;
  1509. case PIPE_FORMAT_DXT5_RGBA:
  1510. case PIPE_FORMAT_DXT5_SRGBA:
  1511. return V_008F14_IMG_DATA_FORMAT_BC3;
  1512. default:
  1513. goto out_unknown;
  1514. }
  1515. }
  1516. if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
  1517. return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
  1518. } else if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
  1519. return V_008F14_IMG_DATA_FORMAT_10_11_11;
  1520. }
  1521. /* R8G8Bx_SNORM - TODO CxV8U8 */
  1522. /* hw cannot support mixed formats (except depth/stencil, since only
  1523. * depth is read).*/
  1524. if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
  1525. goto out_unknown;
  1526. /* See whether the components are of the same size. */
  1527. for (i = 1; i < desc->nr_channels; i++) {
  1528. uniform = uniform && desc->channel[0].size == desc->channel[i].size;
  1529. }
  1530. /* Non-uniform formats. */
  1531. if (!uniform) {
  1532. switch(desc->nr_channels) {
  1533. case 3:
  1534. if (desc->channel[0].size == 5 &&
  1535. desc->channel[1].size == 6 &&
  1536. desc->channel[2].size == 5) {
  1537. return V_008F14_IMG_DATA_FORMAT_5_6_5;
  1538. }
  1539. goto out_unknown;
  1540. case 4:
  1541. if (desc->channel[0].size == 5 &&
  1542. desc->channel[1].size == 5 &&
  1543. desc->channel[2].size == 5 &&
  1544. desc->channel[3].size == 1) {
  1545. return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
  1546. }
  1547. if (desc->channel[0].size == 1 &&
  1548. desc->channel[1].size == 5 &&
  1549. desc->channel[2].size == 5 &&
  1550. desc->channel[3].size == 5) {
  1551. return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
  1552. }
  1553. if (desc->channel[0].size == 10 &&
  1554. desc->channel[1].size == 10 &&
  1555. desc->channel[2].size == 10 &&
  1556. desc->channel[3].size == 2) {
  1557. return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
  1558. }
  1559. goto out_unknown;
  1560. }
  1561. goto out_unknown;
  1562. }
  1563. if (first_non_void < 0 || first_non_void > 3)
  1564. goto out_unknown;
  1565. /* uniform formats */
  1566. switch (desc->channel[first_non_void].size) {
  1567. case 4:
  1568. switch (desc->nr_channels) {
  1569. #if 0 /* Not supported for render targets */
  1570. case 2:
  1571. return V_008F14_IMG_DATA_FORMAT_4_4;
  1572. #endif
  1573. case 4:
  1574. return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
  1575. }
  1576. break;
  1577. case 8:
  1578. switch (desc->nr_channels) {
  1579. case 1:
  1580. return V_008F14_IMG_DATA_FORMAT_8;
  1581. case 2:
  1582. return V_008F14_IMG_DATA_FORMAT_8_8;
  1583. case 4:
  1584. return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
  1585. }
  1586. break;
  1587. case 16:
  1588. switch (desc->nr_channels) {
  1589. case 1:
  1590. return V_008F14_IMG_DATA_FORMAT_16;
  1591. case 2:
  1592. return V_008F14_IMG_DATA_FORMAT_16_16;
  1593. case 4:
  1594. return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
  1595. }
  1596. break;
  1597. case 32:
  1598. switch (desc->nr_channels) {
  1599. case 1:
  1600. return V_008F14_IMG_DATA_FORMAT_32;
  1601. case 2:
  1602. return V_008F14_IMG_DATA_FORMAT_32_32;
  1603. #if 0 /* Not supported for render targets */
  1604. case 3:
  1605. return V_008F14_IMG_DATA_FORMAT_32_32_32;
  1606. #endif
  1607. case 4:
  1608. return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
  1609. }
  1610. }
  1611. out_unknown:
  1612. return ~0;
  1613. }
  1614. static unsigned si_tex_wrap(unsigned wrap)
  1615. {
  1616. switch (wrap) {
  1617. default:
  1618. case PIPE_TEX_WRAP_REPEAT:
  1619. return V_008F30_SQ_TEX_WRAP;
  1620. case PIPE_TEX_WRAP_CLAMP:
  1621. return V_008F30_SQ_TEX_CLAMP_HALF_BORDER;
  1622. case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
  1623. return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
  1624. case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
  1625. return V_008F30_SQ_TEX_CLAMP_BORDER;
  1626. case PIPE_TEX_WRAP_MIRROR_REPEAT:
  1627. return V_008F30_SQ_TEX_MIRROR;
  1628. case PIPE_TEX_WRAP_MIRROR_CLAMP:
  1629. return V_008F30_SQ_TEX_MIRROR_ONCE_HALF_BORDER;
  1630. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
  1631. return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
  1632. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
  1633. return V_008F30_SQ_TEX_MIRROR_ONCE_BORDER;
  1634. }
  1635. }
  1636. static unsigned si_tex_mipfilter(unsigned filter)
  1637. {
  1638. switch (filter) {
  1639. case PIPE_TEX_MIPFILTER_NEAREST:
  1640. return V_008F38_SQ_TEX_Z_FILTER_POINT;
  1641. case PIPE_TEX_MIPFILTER_LINEAR:
  1642. return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
  1643. default:
  1644. case PIPE_TEX_MIPFILTER_NONE:
  1645. return V_008F38_SQ_TEX_Z_FILTER_NONE;
  1646. }
  1647. }
  1648. static unsigned si_tex_compare(unsigned compare)
  1649. {
  1650. switch (compare) {
  1651. default:
  1652. case PIPE_FUNC_NEVER:
  1653. return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
  1654. case PIPE_FUNC_LESS:
  1655. return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
  1656. case PIPE_FUNC_EQUAL:
  1657. return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
  1658. case PIPE_FUNC_LEQUAL:
  1659. return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
  1660. case PIPE_FUNC_GREATER:
  1661. return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
  1662. case PIPE_FUNC_NOTEQUAL:
  1663. return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
  1664. case PIPE_FUNC_GEQUAL:
  1665. return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
  1666. case PIPE_FUNC_ALWAYS:
  1667. return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
  1668. }
  1669. }
  1670. static unsigned si_tex_dim(struct si_screen *sscreen, struct r600_texture *rtex,
  1671. unsigned view_target, unsigned nr_samples)
  1672. {
  1673. unsigned res_target = rtex->buffer.b.b.target;
  1674. if (view_target == PIPE_TEXTURE_CUBE ||
  1675. view_target == PIPE_TEXTURE_CUBE_ARRAY)
  1676. res_target = view_target;
  1677. /* If interpreting cubemaps as something else, set 2D_ARRAY. */
  1678. else if (res_target == PIPE_TEXTURE_CUBE ||
  1679. res_target == PIPE_TEXTURE_CUBE_ARRAY)
  1680. res_target = PIPE_TEXTURE_2D_ARRAY;
  1681. /* GFX9 allocates 1D textures as 2D. */
  1682. if ((res_target == PIPE_TEXTURE_1D ||
  1683. res_target == PIPE_TEXTURE_1D_ARRAY) &&
  1684. sscreen->info.chip_class >= GFX9 &&
  1685. rtex->surface.u.gfx9.resource_type == RADEON_RESOURCE_2D) {
  1686. if (res_target == PIPE_TEXTURE_1D)
  1687. res_target = PIPE_TEXTURE_2D;
  1688. else
  1689. res_target = PIPE_TEXTURE_2D_ARRAY;
  1690. }
  1691. switch (res_target) {
  1692. default:
  1693. case PIPE_TEXTURE_1D:
  1694. return V_008F1C_SQ_RSRC_IMG_1D;
  1695. case PIPE_TEXTURE_1D_ARRAY:
  1696. return V_008F1C_SQ_RSRC_IMG_1D_ARRAY;
  1697. case PIPE_TEXTURE_2D:
  1698. case PIPE_TEXTURE_RECT:
  1699. return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA :
  1700. V_008F1C_SQ_RSRC_IMG_2D;
  1701. case PIPE_TEXTURE_2D_ARRAY:
  1702. return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY :
  1703. V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
  1704. case PIPE_TEXTURE_3D:
  1705. return V_008F1C_SQ_RSRC_IMG_3D;
  1706. case PIPE_TEXTURE_CUBE:
  1707. case PIPE_TEXTURE_CUBE_ARRAY:
  1708. return V_008F1C_SQ_RSRC_IMG_CUBE;
  1709. }
  1710. }
  1711. /*
  1712. * Format support testing
  1713. */
  1714. static bool si_is_sampler_format_supported(struct pipe_screen *screen, enum pipe_format format)
  1715. {
  1716. const struct util_format_description *desc = util_format_description(format);
  1717. if (!desc)
  1718. return false;
  1719. return si_translate_texformat(screen, format, desc,
  1720. util_format_get_first_non_void_channel(format)) != ~0U;
  1721. }
  1722. static uint32_t si_translate_buffer_dataformat(struct pipe_screen *screen,
  1723. const struct util_format_description *desc,
  1724. int first_non_void)
  1725. {
  1726. int i;
  1727. if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
  1728. return V_008F0C_BUF_DATA_FORMAT_10_11_11;
  1729. assert(first_non_void >= 0);
  1730. if (desc->nr_channels == 4 &&
  1731. desc->channel[0].size == 10 &&
  1732. desc->channel[1].size == 10 &&
  1733. desc->channel[2].size == 10 &&
  1734. desc->channel[3].size == 2)
  1735. return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
  1736. /* See whether the components are of the same size. */
  1737. for (i = 0; i < desc->nr_channels; i++) {
  1738. if (desc->channel[first_non_void].size != desc->channel[i].size)
  1739. return V_008F0C_BUF_DATA_FORMAT_INVALID;
  1740. }
  1741. switch (desc->channel[first_non_void].size) {
  1742. case 8:
  1743. switch (desc->nr_channels) {
  1744. case 1:
  1745. case 3: /* 3 loads */
  1746. return V_008F0C_BUF_DATA_FORMAT_8;
  1747. case 2:
  1748. return V_008F0C_BUF_DATA_FORMAT_8_8;
  1749. case 4:
  1750. return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
  1751. }
  1752. break;
  1753. case 16:
  1754. switch (desc->nr_channels) {
  1755. case 1:
  1756. case 3: /* 3 loads */
  1757. return V_008F0C_BUF_DATA_FORMAT_16;
  1758. case 2:
  1759. return V_008F0C_BUF_DATA_FORMAT_16_16;
  1760. case 4:
  1761. return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
  1762. }
  1763. break;
  1764. case 32:
  1765. switch (desc->nr_channels) {
  1766. case 1:
  1767. return V_008F0C_BUF_DATA_FORMAT_32;
  1768. case 2:
  1769. return V_008F0C_BUF_DATA_FORMAT_32_32;
  1770. case 3:
  1771. return V_008F0C_BUF_DATA_FORMAT_32_32_32;
  1772. case 4:
  1773. return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
  1774. }
  1775. break;
  1776. case 64:
  1777. /* Legacy double formats. */
  1778. switch (desc->nr_channels) {
  1779. case 1: /* 1 load */
  1780. return V_008F0C_BUF_DATA_FORMAT_32_32;
  1781. case 2: /* 1 load */
  1782. return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
  1783. case 3: /* 3 loads */
  1784. return V_008F0C_BUF_DATA_FORMAT_32_32;
  1785. case 4: /* 2 loads */
  1786. return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
  1787. }
  1788. break;
  1789. }
  1790. return V_008F0C_BUF_DATA_FORMAT_INVALID;
  1791. }
  1792. static uint32_t si_translate_buffer_numformat(struct pipe_screen *screen,
  1793. const struct util_format_description *desc,
  1794. int first_non_void)
  1795. {
  1796. if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
  1797. return V_008F0C_BUF_NUM_FORMAT_FLOAT;
  1798. assert(first_non_void >= 0);
  1799. switch (desc->channel[first_non_void].type) {
  1800. case UTIL_FORMAT_TYPE_SIGNED:
  1801. case UTIL_FORMAT_TYPE_FIXED:
  1802. if (desc->channel[first_non_void].size >= 32 ||
  1803. desc->channel[first_non_void].pure_integer)
  1804. return V_008F0C_BUF_NUM_FORMAT_SINT;
  1805. else if (desc->channel[first_non_void].normalized)
  1806. return V_008F0C_BUF_NUM_FORMAT_SNORM;
  1807. else
  1808. return V_008F0C_BUF_NUM_FORMAT_SSCALED;
  1809. break;
  1810. case UTIL_FORMAT_TYPE_UNSIGNED:
  1811. if (desc->channel[first_non_void].size >= 32 ||
  1812. desc->channel[first_non_void].pure_integer)
  1813. return V_008F0C_BUF_NUM_FORMAT_UINT;
  1814. else if (desc->channel[first_non_void].normalized)
  1815. return V_008F0C_BUF_NUM_FORMAT_UNORM;
  1816. else
  1817. return V_008F0C_BUF_NUM_FORMAT_USCALED;
  1818. break;
  1819. case UTIL_FORMAT_TYPE_FLOAT:
  1820. default:
  1821. return V_008F0C_BUF_NUM_FORMAT_FLOAT;
  1822. }
  1823. }
  1824. static unsigned si_is_vertex_format_supported(struct pipe_screen *screen,
  1825. enum pipe_format format,
  1826. unsigned usage)
  1827. {
  1828. const struct util_format_description *desc;
  1829. int first_non_void;
  1830. unsigned data_format;
  1831. assert((usage & ~(PIPE_BIND_SHADER_IMAGE |
  1832. PIPE_BIND_SAMPLER_VIEW |
  1833. PIPE_BIND_VERTEX_BUFFER)) == 0);
  1834. desc = util_format_description(format);
  1835. if (!desc)
  1836. return 0;
  1837. /* There are no native 8_8_8 or 16_16_16 data formats, and we currently
  1838. * select 8_8_8_8 and 16_16_16_16 instead. This works reasonably well
  1839. * for read-only access (with caveats surrounding bounds checks), but
  1840. * obviously fails for write access which we have to implement for
  1841. * shader images. Luckily, OpenGL doesn't expect this to be supported
  1842. * anyway, and so the only impact is on PBO uploads / downloads, which
  1843. * shouldn't be expected to be fast for GL_RGB anyway.
  1844. */
  1845. if (desc->block.bits == 3 * 8 ||
  1846. desc->block.bits == 3 * 16) {
  1847. if (usage & (PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW)) {
  1848. usage &= ~(PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW);
  1849. if (!usage)
  1850. return 0;
  1851. }
  1852. }
  1853. first_non_void = util_format_get_first_non_void_channel(format);
  1854. data_format = si_translate_buffer_dataformat(screen, desc, first_non_void);
  1855. if (data_format == V_008F0C_BUF_DATA_FORMAT_INVALID)
  1856. return 0;
  1857. return usage;
  1858. }
  1859. static bool si_is_colorbuffer_format_supported(enum pipe_format format)
  1860. {
  1861. return si_translate_colorformat(format) != V_028C70_COLOR_INVALID &&
  1862. si_translate_colorswap(format, false) != ~0U;
  1863. }
  1864. static bool si_is_zs_format_supported(enum pipe_format format)
  1865. {
  1866. return si_translate_dbformat(format) != V_028040_Z_INVALID;
  1867. }
  1868. static boolean si_is_format_supported(struct pipe_screen *screen,
  1869. enum pipe_format format,
  1870. enum pipe_texture_target target,
  1871. unsigned sample_count,
  1872. unsigned usage)
  1873. {
  1874. struct si_screen *sscreen = (struct si_screen *)screen;
  1875. unsigned retval = 0;
  1876. if (target >= PIPE_MAX_TEXTURE_TYPES) {
  1877. PRINT_ERR("r600: unsupported texture type %d\n", target);
  1878. return false;
  1879. }
  1880. if (!util_format_is_supported(format, usage))
  1881. return false;
  1882. if (sample_count > 1) {
  1883. if (!screen->get_param(screen, PIPE_CAP_TEXTURE_MULTISAMPLE))
  1884. return false;
  1885. if (usage & PIPE_BIND_SHADER_IMAGE)
  1886. return false;
  1887. switch (sample_count) {
  1888. case 2:
  1889. case 4:
  1890. case 8:
  1891. break;
  1892. case 16:
  1893. /* Allow resource_copy_region with nr_samples == 16. */
  1894. if (sscreen->eqaa_force_coverage_samples == 16 &&
  1895. !util_format_is_depth_or_stencil(format))
  1896. return true;
  1897. if (format == PIPE_FORMAT_NONE)
  1898. return true;
  1899. else
  1900. return false;
  1901. default:
  1902. return false;
  1903. }
  1904. }
  1905. if (usage & (PIPE_BIND_SAMPLER_VIEW |
  1906. PIPE_BIND_SHADER_IMAGE)) {
  1907. if (target == PIPE_BUFFER) {
  1908. retval |= si_is_vertex_format_supported(
  1909. screen, format, usage & (PIPE_BIND_SAMPLER_VIEW |
  1910. PIPE_BIND_SHADER_IMAGE));
  1911. } else {
  1912. if (si_is_sampler_format_supported(screen, format))
  1913. retval |= usage & (PIPE_BIND_SAMPLER_VIEW |
  1914. PIPE_BIND_SHADER_IMAGE);
  1915. }
  1916. }
  1917. if ((usage & (PIPE_BIND_RENDER_TARGET |
  1918. PIPE_BIND_DISPLAY_TARGET |
  1919. PIPE_BIND_SCANOUT |
  1920. PIPE_BIND_SHARED |
  1921. PIPE_BIND_BLENDABLE)) &&
  1922. si_is_colorbuffer_format_supported(format)) {
  1923. retval |= usage &
  1924. (PIPE_BIND_RENDER_TARGET |
  1925. PIPE_BIND_DISPLAY_TARGET |
  1926. PIPE_BIND_SCANOUT |
  1927. PIPE_BIND_SHARED);
  1928. if (!util_format_is_pure_integer(format) &&
  1929. !util_format_is_depth_or_stencil(format))
  1930. retval |= usage & PIPE_BIND_BLENDABLE;
  1931. }
  1932. if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
  1933. si_is_zs_format_supported(format)) {
  1934. retval |= PIPE_BIND_DEPTH_STENCIL;
  1935. }
  1936. if (usage & PIPE_BIND_VERTEX_BUFFER) {
  1937. retval |= si_is_vertex_format_supported(screen, format,
  1938. PIPE_BIND_VERTEX_BUFFER);
  1939. }
  1940. if ((usage & PIPE_BIND_LINEAR) &&
  1941. !util_format_is_compressed(format) &&
  1942. !(usage & PIPE_BIND_DEPTH_STENCIL))
  1943. retval |= PIPE_BIND_LINEAR;
  1944. return retval == usage;
  1945. }
  1946. /*
  1947. * framebuffer handling
  1948. */
  1949. static void si_choose_spi_color_formats(struct r600_surface *surf,
  1950. unsigned format, unsigned swap,
  1951. unsigned ntype, bool is_depth)
  1952. {
  1953. /* Alpha is needed for alpha-to-coverage.
  1954. * Blending may be with or without alpha.
  1955. */
  1956. unsigned normal = 0; /* most optimal, may not support blending or export alpha */
  1957. unsigned alpha = 0; /* exports alpha, but may not support blending */
  1958. unsigned blend = 0; /* supports blending, but may not export alpha */
  1959. unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */
  1960. /* Choose the SPI color formats. These are required values for RB+.
  1961. * Other chips have multiple choices, though they are not necessarily better.
  1962. */
  1963. switch (format) {
  1964. case V_028C70_COLOR_5_6_5:
  1965. case V_028C70_COLOR_1_5_5_5:
  1966. case V_028C70_COLOR_5_5_5_1:
  1967. case V_028C70_COLOR_4_4_4_4:
  1968. case V_028C70_COLOR_10_11_11:
  1969. case V_028C70_COLOR_11_11_10:
  1970. case V_028C70_COLOR_8:
  1971. case V_028C70_COLOR_8_8:
  1972. case V_028C70_COLOR_8_8_8_8:
  1973. case V_028C70_COLOR_10_10_10_2:
  1974. case V_028C70_COLOR_2_10_10_10:
  1975. if (ntype == V_028C70_NUMBER_UINT)
  1976. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
  1977. else if (ntype == V_028C70_NUMBER_SINT)
  1978. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
  1979. else
  1980. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
  1981. break;
  1982. case V_028C70_COLOR_16:
  1983. case V_028C70_COLOR_16_16:
  1984. case V_028C70_COLOR_16_16_16_16:
  1985. if (ntype == V_028C70_NUMBER_UNORM ||
  1986. ntype == V_028C70_NUMBER_SNORM) {
  1987. /* UNORM16 and SNORM16 don't support blending */
  1988. if (ntype == V_028C70_NUMBER_UNORM)
  1989. normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;
  1990. else
  1991. normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;
  1992. /* Use 32 bits per channel for blending. */
  1993. if (format == V_028C70_COLOR_16) {
  1994. if (swap == V_028C70_SWAP_STD) { /* R */
  1995. blend = V_028714_SPI_SHADER_32_R;
  1996. blend_alpha = V_028714_SPI_SHADER_32_AR;
  1997. } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
  1998. blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
  1999. else
  2000. assert(0);
  2001. } else if (format == V_028C70_COLOR_16_16) {
  2002. if (swap == V_028C70_SWAP_STD) { /* RG */
  2003. blend = V_028714_SPI_SHADER_32_GR;
  2004. blend_alpha = V_028714_SPI_SHADER_32_ABGR;
  2005. } else if (swap == V_028C70_SWAP_ALT) /* RA */
  2006. blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
  2007. else
  2008. assert(0);
  2009. } else /* 16_16_16_16 */
  2010. blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
  2011. } else if (ntype == V_028C70_NUMBER_UINT)
  2012. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
  2013. else if (ntype == V_028C70_NUMBER_SINT)
  2014. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
  2015. else if (ntype == V_028C70_NUMBER_FLOAT)
  2016. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
  2017. else
  2018. assert(0);
  2019. break;
  2020. case V_028C70_COLOR_32:
  2021. if (swap == V_028C70_SWAP_STD) { /* R */
  2022. blend = normal = V_028714_SPI_SHADER_32_R;
  2023. alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;
  2024. } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
  2025. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
  2026. else
  2027. assert(0);
  2028. break;
  2029. case V_028C70_COLOR_32_32:
  2030. if (swap == V_028C70_SWAP_STD) { /* RG */
  2031. blend = normal = V_028714_SPI_SHADER_32_GR;
  2032. alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
  2033. } else if (swap == V_028C70_SWAP_ALT) /* RA */
  2034. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
  2035. else
  2036. assert(0);
  2037. break;
  2038. case V_028C70_COLOR_32_32_32_32:
  2039. case V_028C70_COLOR_8_24:
  2040. case V_028C70_COLOR_24_8:
  2041. case V_028C70_COLOR_X24_8_32_FLOAT:
  2042. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
  2043. break;
  2044. default:
  2045. assert(0);
  2046. return;
  2047. }
  2048. /* The DB->CB copy needs 32_ABGR. */
  2049. if (is_depth)
  2050. alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
  2051. surf->spi_shader_col_format = normal;
  2052. surf->spi_shader_col_format_alpha = alpha;
  2053. surf->spi_shader_col_format_blend = blend;
  2054. surf->spi_shader_col_format_blend_alpha = blend_alpha;
  2055. }
  2056. static void si_initialize_color_surface(struct si_context *sctx,
  2057. struct r600_surface *surf)
  2058. {
  2059. struct r600_texture *rtex = (struct r600_texture*)surf->base.texture;
  2060. unsigned color_info, color_attrib;
  2061. unsigned format, swap, ntype, endian;
  2062. const struct util_format_description *desc;
  2063. int firstchan;
  2064. unsigned blend_clamp = 0, blend_bypass = 0;
  2065. desc = util_format_description(surf->base.format);
  2066. for (firstchan = 0; firstchan < 4; firstchan++) {
  2067. if (desc->channel[firstchan].type != UTIL_FORMAT_TYPE_VOID) {
  2068. break;
  2069. }
  2070. }
  2071. if (firstchan == 4 || desc->channel[firstchan].type == UTIL_FORMAT_TYPE_FLOAT) {
  2072. ntype = V_028C70_NUMBER_FLOAT;
  2073. } else {
  2074. ntype = V_028C70_NUMBER_UNORM;
  2075. if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
  2076. ntype = V_028C70_NUMBER_SRGB;
  2077. else if (desc->channel[firstchan].type == UTIL_FORMAT_TYPE_SIGNED) {
  2078. if (desc->channel[firstchan].pure_integer) {
  2079. ntype = V_028C70_NUMBER_SINT;
  2080. } else {
  2081. assert(desc->channel[firstchan].normalized);
  2082. ntype = V_028C70_NUMBER_SNORM;
  2083. }
  2084. } else if (desc->channel[firstchan].type == UTIL_FORMAT_TYPE_UNSIGNED) {
  2085. if (desc->channel[firstchan].pure_integer) {
  2086. ntype = V_028C70_NUMBER_UINT;
  2087. } else {
  2088. assert(desc->channel[firstchan].normalized);
  2089. ntype = V_028C70_NUMBER_UNORM;
  2090. }
  2091. }
  2092. }
  2093. format = si_translate_colorformat(surf->base.format);
  2094. if (format == V_028C70_COLOR_INVALID) {
  2095. PRINT_ERR("Invalid CB format: %d, disabling CB.\n", surf->base.format);
  2096. }
  2097. assert(format != V_028C70_COLOR_INVALID);
  2098. swap = si_translate_colorswap(surf->base.format, false);
  2099. endian = si_colorformat_endian_swap(format);
  2100. /* blend clamp should be set for all NORM/SRGB types */
  2101. if (ntype == V_028C70_NUMBER_UNORM ||
  2102. ntype == V_028C70_NUMBER_SNORM ||
  2103. ntype == V_028C70_NUMBER_SRGB)
  2104. blend_clamp = 1;
  2105. /* set blend bypass according to docs if SINT/UINT or
  2106. 8/24 COLOR variants */
  2107. if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
  2108. format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
  2109. format == V_028C70_COLOR_X24_8_32_FLOAT) {
  2110. blend_clamp = 0;
  2111. blend_bypass = 1;
  2112. }
  2113. if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) {
  2114. if (format == V_028C70_COLOR_8 ||
  2115. format == V_028C70_COLOR_8_8 ||
  2116. format == V_028C70_COLOR_8_8_8_8)
  2117. surf->color_is_int8 = true;
  2118. else if (format == V_028C70_COLOR_10_10_10_2 ||
  2119. format == V_028C70_COLOR_2_10_10_10)
  2120. surf->color_is_int10 = true;
  2121. }
  2122. color_info = S_028C70_FORMAT(format) |
  2123. S_028C70_COMP_SWAP(swap) |
  2124. S_028C70_BLEND_CLAMP(blend_clamp) |
  2125. S_028C70_BLEND_BYPASS(blend_bypass) |
  2126. S_028C70_SIMPLE_FLOAT(1) |
  2127. S_028C70_ROUND_MODE(ntype != V_028C70_NUMBER_UNORM &&
  2128. ntype != V_028C70_NUMBER_SNORM &&
  2129. ntype != V_028C70_NUMBER_SRGB &&
  2130. format != V_028C70_COLOR_8_24 &&
  2131. format != V_028C70_COLOR_24_8) |
  2132. S_028C70_NUMBER_TYPE(ntype) |
  2133. S_028C70_ENDIAN(endian);
  2134. /* Intensity is implemented as Red, so treat it that way. */
  2135. color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == PIPE_SWIZZLE_1 ||
  2136. util_format_is_intensity(surf->base.format));
  2137. if (rtex->buffer.b.b.nr_samples > 1) {
  2138. unsigned log_samples = util_logbase2(rtex->buffer.b.b.nr_samples);
  2139. unsigned log_fragments = util_logbase2(rtex->num_color_samples);
  2140. color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
  2141. S_028C74_NUM_FRAGMENTS(log_fragments);
  2142. if (rtex->surface.fmask_size) {
  2143. color_info |= S_028C70_COMPRESSION(1);
  2144. unsigned fmask_bankh = util_logbase2(rtex->surface.u.legacy.fmask.bankh);
  2145. if (sctx->chip_class == SI) {
  2146. /* due to a hw bug, FMASK_BANK_HEIGHT must be set on SI too */
  2147. color_attrib |= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh);
  2148. }
  2149. }
  2150. }
  2151. if (sctx->chip_class >= VI) {
  2152. unsigned max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_256B;
  2153. unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
  2154. /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
  2155. 64 for APU because all of our APUs to date use DIMMs which have
  2156. a request granularity size of 64B while all other chips have a
  2157. 32B request size */
  2158. if (!sctx->screen->info.has_dedicated_vram)
  2159. min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
  2160. if (rtex->num_color_samples > 1) {
  2161. if (rtex->surface.bpe == 1)
  2162. max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
  2163. else if (rtex->surface.bpe == 2)
  2164. max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
  2165. }
  2166. surf->cb_dcc_control = S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
  2167. S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
  2168. S_028C78_INDEPENDENT_64B_BLOCKS(1);
  2169. }
  2170. /* This must be set for fast clear to work without FMASK. */
  2171. if (!rtex->surface.fmask_size && sctx->chip_class == SI) {
  2172. unsigned bankh = util_logbase2(rtex->surface.u.legacy.bankh);
  2173. color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
  2174. }
  2175. unsigned color_view = S_028C6C_SLICE_START(surf->base.u.tex.first_layer) |
  2176. S_028C6C_SLICE_MAX(surf->base.u.tex.last_layer);
  2177. if (sctx->chip_class >= GFX9) {
  2178. unsigned mip0_depth = util_max_layer(&rtex->buffer.b.b, 0);
  2179. color_view |= S_028C6C_MIP_LEVEL(surf->base.u.tex.level);
  2180. color_attrib |= S_028C74_MIP0_DEPTH(mip0_depth) |
  2181. S_028C74_RESOURCE_TYPE(rtex->surface.u.gfx9.resource_type);
  2182. surf->cb_color_attrib2 = S_028C68_MIP0_WIDTH(surf->width0 - 1) |
  2183. S_028C68_MIP0_HEIGHT(surf->height0 - 1) |
  2184. S_028C68_MAX_MIP(rtex->buffer.b.b.last_level);
  2185. }
  2186. surf->cb_color_view = color_view;
  2187. surf->cb_color_info = color_info;
  2188. surf->cb_color_attrib = color_attrib;
  2189. /* Determine pixel shader export format */
  2190. si_choose_spi_color_formats(surf, format, swap, ntype, rtex->is_depth);
  2191. surf->color_initialized = true;
  2192. }
  2193. static void si_init_depth_surface(struct si_context *sctx,
  2194. struct r600_surface *surf)
  2195. {
  2196. struct r600_texture *rtex = (struct r600_texture*)surf->base.texture;
  2197. unsigned level = surf->base.u.tex.level;
  2198. unsigned format, stencil_format;
  2199. uint32_t z_info, s_info;
  2200. format = si_translate_dbformat(rtex->db_render_format);
  2201. stencil_format = rtex->surface.has_stencil ?
  2202. V_028044_STENCIL_8 : V_028044_STENCIL_INVALID;
  2203. assert(format != V_028040_Z_INVALID);
  2204. if (format == V_028040_Z_INVALID)
  2205. PRINT_ERR("Invalid DB format: %d, disabling DB.\n", rtex->buffer.b.b.format);
  2206. surf->db_depth_view = S_028008_SLICE_START(surf->base.u.tex.first_layer) |
  2207. S_028008_SLICE_MAX(surf->base.u.tex.last_layer);
  2208. surf->db_htile_data_base = 0;
  2209. surf->db_htile_surface = 0;
  2210. if (sctx->chip_class >= GFX9) {
  2211. assert(rtex->surface.u.gfx9.surf_offset == 0);
  2212. surf->db_depth_base = rtex->buffer.gpu_address >> 8;
  2213. surf->db_stencil_base = (rtex->buffer.gpu_address +
  2214. rtex->surface.u.gfx9.stencil_offset) >> 8;
  2215. z_info = S_028038_FORMAT(format) |
  2216. S_028038_NUM_SAMPLES(util_logbase2(rtex->buffer.b.b.nr_samples)) |
  2217. S_028038_SW_MODE(rtex->surface.u.gfx9.surf.swizzle_mode) |
  2218. S_028038_MAXMIP(rtex->buffer.b.b.last_level);
  2219. s_info = S_02803C_FORMAT(stencil_format) |
  2220. S_02803C_SW_MODE(rtex->surface.u.gfx9.stencil.swizzle_mode);
  2221. surf->db_z_info2 = S_028068_EPITCH(rtex->surface.u.gfx9.surf.epitch);
  2222. surf->db_stencil_info2 = S_02806C_EPITCH(rtex->surface.u.gfx9.stencil.epitch);
  2223. surf->db_depth_view |= S_028008_MIPID(level);
  2224. surf->db_depth_size = S_02801C_X_MAX(rtex->buffer.b.b.width0 - 1) |
  2225. S_02801C_Y_MAX(rtex->buffer.b.b.height0 - 1);
  2226. if (si_htile_enabled(rtex, level)) {
  2227. z_info |= S_028038_TILE_SURFACE_ENABLE(1) |
  2228. S_028038_ALLOW_EXPCLEAR(1);
  2229. if (rtex->tc_compatible_htile) {
  2230. unsigned max_zplanes = 4;
  2231. if (rtex->db_render_format == PIPE_FORMAT_Z16_UNORM &&
  2232. rtex->buffer.b.b.nr_samples > 1)
  2233. max_zplanes = 2;
  2234. z_info |= S_028038_DECOMPRESS_ON_N_ZPLANES(max_zplanes + 1) |
  2235. S_028038_ITERATE_FLUSH(1);
  2236. s_info |= S_02803C_ITERATE_FLUSH(1);
  2237. }
  2238. if (rtex->surface.has_stencil) {
  2239. /* Stencil buffer workaround ported from the SI-CI-VI code.
  2240. * See that for explanation.
  2241. */
  2242. s_info |= S_02803C_ALLOW_EXPCLEAR(rtex->buffer.b.b.nr_samples <= 1);
  2243. } else {
  2244. /* Use all HTILE for depth if there's no stencil. */
  2245. s_info |= S_02803C_TILE_STENCIL_DISABLE(1);
  2246. }
  2247. surf->db_htile_data_base = (rtex->buffer.gpu_address +
  2248. rtex->htile_offset) >> 8;
  2249. surf->db_htile_surface = S_028ABC_FULL_CACHE(1) |
  2250. S_028ABC_PIPE_ALIGNED(rtex->surface.u.gfx9.htile.pipe_aligned) |
  2251. S_028ABC_RB_ALIGNED(rtex->surface.u.gfx9.htile.rb_aligned);
  2252. }
  2253. } else {
  2254. /* SI-CI-VI */
  2255. struct legacy_surf_level *levelinfo = &rtex->surface.u.legacy.level[level];
  2256. assert(levelinfo->nblk_x % 8 == 0 && levelinfo->nblk_y % 8 == 0);
  2257. surf->db_depth_base = (rtex->buffer.gpu_address +
  2258. rtex->surface.u.legacy.level[level].offset) >> 8;
  2259. surf->db_stencil_base = (rtex->buffer.gpu_address +
  2260. rtex->surface.u.legacy.stencil_level[level].offset) >> 8;
  2261. z_info = S_028040_FORMAT(format) |
  2262. S_028040_NUM_SAMPLES(util_logbase2(rtex->buffer.b.b.nr_samples));
  2263. s_info = S_028044_FORMAT(stencil_format);
  2264. surf->db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(!rtex->tc_compatible_htile);
  2265. if (sctx->chip_class >= CIK) {
  2266. struct radeon_info *info = &sctx->screen->info;
  2267. unsigned index = rtex->surface.u.legacy.tiling_index[level];
  2268. unsigned stencil_index = rtex->surface.u.legacy.stencil_tiling_index[level];
  2269. unsigned macro_index = rtex->surface.u.legacy.macro_tile_index;
  2270. unsigned tile_mode = info->si_tile_mode_array[index];
  2271. unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
  2272. unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
  2273. surf->db_depth_info |=
  2274. S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
  2275. S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
  2276. S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
  2277. S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
  2278. S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
  2279. S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
  2280. z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
  2281. s_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
  2282. } else {
  2283. unsigned tile_mode_index = si_tile_mode_index(rtex, level, false);
  2284. z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
  2285. tile_mode_index = si_tile_mode_index(rtex, level, true);
  2286. s_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
  2287. }
  2288. surf->db_depth_size = S_028058_PITCH_TILE_MAX((levelinfo->nblk_x / 8) - 1) |
  2289. S_028058_HEIGHT_TILE_MAX((levelinfo->nblk_y / 8) - 1);
  2290. surf->db_depth_slice = S_02805C_SLICE_TILE_MAX((levelinfo->nblk_x *
  2291. levelinfo->nblk_y) / 64 - 1);
  2292. if (si_htile_enabled(rtex, level)) {
  2293. z_info |= S_028040_TILE_SURFACE_ENABLE(1) |
  2294. S_028040_ALLOW_EXPCLEAR(1);
  2295. if (rtex->surface.has_stencil) {
  2296. /* Workaround: For a not yet understood reason, the
  2297. * combination of MSAA, fast stencil clear and stencil
  2298. * decompress messes with subsequent stencil buffer
  2299. * uses. Problem was reproduced on Verde, Bonaire,
  2300. * Tonga, and Carrizo.
  2301. *
  2302. * Disabling EXPCLEAR works around the problem.
  2303. *
  2304. * Check piglit's arb_texture_multisample-stencil-clear
  2305. * test if you want to try changing this.
  2306. */
  2307. if (rtex->buffer.b.b.nr_samples <= 1)
  2308. s_info |= S_028044_ALLOW_EXPCLEAR(1);
  2309. } else if (!rtex->tc_compatible_htile) {
  2310. /* Use all of the htile_buffer for depth if there's no stencil.
  2311. * This must not be set when TC-compatible HTILE is enabled
  2312. * due to a hw bug.
  2313. */
  2314. s_info |= S_028044_TILE_STENCIL_DISABLE(1);
  2315. }
  2316. surf->db_htile_data_base = (rtex->buffer.gpu_address +
  2317. rtex->htile_offset) >> 8;
  2318. surf->db_htile_surface = S_028ABC_FULL_CACHE(1);
  2319. if (rtex->tc_compatible_htile) {
  2320. surf->db_htile_surface |= S_028ABC_TC_COMPATIBLE(1);
  2321. /* 0 = full compression. N = only compress up to N-1 Z planes. */
  2322. if (rtex->buffer.b.b.nr_samples <= 1)
  2323. z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(5);
  2324. else if (rtex->buffer.b.b.nr_samples <= 4)
  2325. z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(3);
  2326. else
  2327. z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(2);
  2328. }
  2329. }
  2330. }
  2331. surf->db_z_info = z_info;
  2332. surf->db_stencil_info = s_info;
  2333. surf->depth_initialized = true;
  2334. }
  2335. void si_update_fb_dirtiness_after_rendering(struct si_context *sctx)
  2336. {
  2337. if (sctx->decompression_enabled)
  2338. return;
  2339. if (sctx->framebuffer.state.zsbuf) {
  2340. struct pipe_surface *surf = sctx->framebuffer.state.zsbuf;
  2341. struct r600_texture *rtex = (struct r600_texture *)surf->texture;
  2342. rtex->dirty_level_mask |= 1 << surf->u.tex.level;
  2343. if (rtex->surface.has_stencil)
  2344. rtex->stencil_dirty_level_mask |= 1 << surf->u.tex.level;
  2345. }
  2346. unsigned compressed_cb_mask = sctx->framebuffer.compressed_cb_mask;
  2347. while (compressed_cb_mask) {
  2348. unsigned i = u_bit_scan(&compressed_cb_mask);
  2349. struct pipe_surface *surf = sctx->framebuffer.state.cbufs[i];
  2350. struct r600_texture *rtex = (struct r600_texture*)surf->texture;
  2351. if (rtex->surface.fmask_size)
  2352. rtex->dirty_level_mask |= 1 << surf->u.tex.level;
  2353. if (rtex->dcc_gather_statistics)
  2354. rtex->separate_dcc_dirty = true;
  2355. }
  2356. }
  2357. static void si_dec_framebuffer_counters(const struct pipe_framebuffer_state *state)
  2358. {
  2359. for (int i = 0; i < state->nr_cbufs; ++i) {
  2360. struct r600_surface *surf = NULL;
  2361. struct r600_texture *rtex;
  2362. if (!state->cbufs[i])
  2363. continue;
  2364. surf = (struct r600_surface*)state->cbufs[i];
  2365. rtex = (struct r600_texture*)surf->base.texture;
  2366. p_atomic_dec(&rtex->framebuffers_bound);
  2367. }
  2368. }
  2369. static void si_set_framebuffer_state(struct pipe_context *ctx,
  2370. const struct pipe_framebuffer_state *state)
  2371. {
  2372. struct si_context *sctx = (struct si_context *)ctx;
  2373. struct pipe_constant_buffer constbuf = {0};
  2374. struct r600_surface *surf = NULL;
  2375. struct r600_texture *rtex;
  2376. bool old_any_dst_linear = sctx->framebuffer.any_dst_linear;
  2377. unsigned old_nr_samples = sctx->framebuffer.nr_samples;
  2378. unsigned old_colorbuf_enabled_4bit = sctx->framebuffer.colorbuf_enabled_4bit;
  2379. bool old_has_zsbuf = !!sctx->framebuffer.state.zsbuf;
  2380. bool old_has_stencil =
  2381. old_has_zsbuf &&
  2382. ((struct r600_texture*)sctx->framebuffer.state.zsbuf->texture)->surface.has_stencil;
  2383. bool unbound = false;
  2384. int i;
  2385. si_update_fb_dirtiness_after_rendering(sctx);
  2386. for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
  2387. if (!sctx->framebuffer.state.cbufs[i])
  2388. continue;
  2389. rtex = (struct r600_texture*)sctx->framebuffer.state.cbufs[i]->texture;
  2390. if (rtex->dcc_gather_statistics)
  2391. vi_separate_dcc_stop_query(sctx, rtex);
  2392. }
  2393. /* Disable DCC if the formats are incompatible. */
  2394. for (i = 0; i < state->nr_cbufs; i++) {
  2395. if (!state->cbufs[i])
  2396. continue;
  2397. surf = (struct r600_surface*)state->cbufs[i];
  2398. rtex = (struct r600_texture*)surf->base.texture;
  2399. if (!surf->dcc_incompatible)
  2400. continue;
  2401. /* Since the DCC decompression calls back into set_framebuffer-
  2402. * _state, we need to unbind the framebuffer, so that
  2403. * vi_separate_dcc_stop_query isn't called twice with the same
  2404. * color buffer.
  2405. */
  2406. if (!unbound) {
  2407. util_copy_framebuffer_state(&sctx->framebuffer.state, NULL);
  2408. unbound = true;
  2409. }
  2410. if (vi_dcc_enabled(rtex, surf->base.u.tex.level))
  2411. if (!si_texture_disable_dcc(sctx, rtex))
  2412. si_decompress_dcc(sctx, rtex);
  2413. surf->dcc_incompatible = false;
  2414. }
  2415. /* Only flush TC when changing the framebuffer state, because
  2416. * the only client not using TC that can change textures is
  2417. * the framebuffer.
  2418. *
  2419. * Wait for compute shaders because of possible transitions:
  2420. * - FB write -> shader read
  2421. * - shader write -> FB read
  2422. *
  2423. * DB caches are flushed on demand (using si_decompress_textures).
  2424. *
  2425. * When MSAA is enabled, CB and TC caches are flushed on demand
  2426. * (after FMASK decompression). Shader write -> FB read transitions
  2427. * cannot happen for MSAA textures, because MSAA shader images are
  2428. * not supported.
  2429. *
  2430. * Only flush and wait for CB if there is actually a bound color buffer.
  2431. */
  2432. if (sctx->framebuffer.uncompressed_cb_mask)
  2433. si_make_CB_shader_coherent(sctx, sctx->framebuffer.nr_samples,
  2434. sctx->framebuffer.CB_has_shader_readable_metadata);
  2435. sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
  2436. /* u_blitter doesn't invoke depth decompression when it does multiple
  2437. * blits in a row, but the only case when it matters for DB is when
  2438. * doing generate_mipmap. So here we flush DB manually between
  2439. * individual generate_mipmap blits.
  2440. * Note that lower mipmap levels aren't compressed.
  2441. */
  2442. if (sctx->generate_mipmap_for_depth) {
  2443. si_make_DB_shader_coherent(sctx, 1, false,
  2444. sctx->framebuffer.DB_has_shader_readable_metadata);
  2445. } else if (sctx->chip_class == GFX9) {
  2446. /* It appears that DB metadata "leaks" in a sequence of:
  2447. * - depth clear
  2448. * - DCC decompress for shader image writes (with DB disabled)
  2449. * - render with DEPTH_BEFORE_SHADER=1
  2450. * Flushing DB metadata works around the problem.
  2451. */
  2452. sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_DB_META;
  2453. }
  2454. /* Take the maximum of the old and new count. If the new count is lower,
  2455. * dirtying is needed to disable the unbound colorbuffers.
  2456. */
  2457. sctx->framebuffer.dirty_cbufs |=
  2458. (1 << MAX2(sctx->framebuffer.state.nr_cbufs, state->nr_cbufs)) - 1;
  2459. sctx->framebuffer.dirty_zsbuf |= sctx->framebuffer.state.zsbuf != state->zsbuf;
  2460. si_dec_framebuffer_counters(&sctx->framebuffer.state);
  2461. util_copy_framebuffer_state(&sctx->framebuffer.state, state);
  2462. sctx->framebuffer.colorbuf_enabled_4bit = 0;
  2463. sctx->framebuffer.spi_shader_col_format = 0;
  2464. sctx->framebuffer.spi_shader_col_format_alpha = 0;
  2465. sctx->framebuffer.spi_shader_col_format_blend = 0;
  2466. sctx->framebuffer.spi_shader_col_format_blend_alpha = 0;
  2467. sctx->framebuffer.color_is_int8 = 0;
  2468. sctx->framebuffer.color_is_int10 = 0;
  2469. sctx->framebuffer.compressed_cb_mask = 0;
  2470. sctx->framebuffer.uncompressed_cb_mask = 0;
  2471. sctx->framebuffer.nr_samples = util_framebuffer_get_num_samples(state);
  2472. sctx->framebuffer.nr_color_samples = sctx->framebuffer.nr_samples;
  2473. sctx->framebuffer.log_samples = util_logbase2(sctx->framebuffer.nr_samples);
  2474. sctx->framebuffer.any_dst_linear = false;
  2475. sctx->framebuffer.CB_has_shader_readable_metadata = false;
  2476. sctx->framebuffer.DB_has_shader_readable_metadata = false;
  2477. for (i = 0; i < state->nr_cbufs; i++) {
  2478. if (!state->cbufs[i])
  2479. continue;
  2480. surf = (struct r600_surface*)state->cbufs[i];
  2481. rtex = (struct r600_texture*)surf->base.texture;
  2482. if (!surf->color_initialized) {
  2483. si_initialize_color_surface(sctx, surf);
  2484. }
  2485. sctx->framebuffer.colorbuf_enabled_4bit |= 0xf << (i * 4);
  2486. sctx->framebuffer.spi_shader_col_format |=
  2487. surf->spi_shader_col_format << (i * 4);
  2488. sctx->framebuffer.spi_shader_col_format_alpha |=
  2489. surf->spi_shader_col_format_alpha << (i * 4);
  2490. sctx->framebuffer.spi_shader_col_format_blend |=
  2491. surf->spi_shader_col_format_blend << (i * 4);
  2492. sctx->framebuffer.spi_shader_col_format_blend_alpha |=
  2493. surf->spi_shader_col_format_blend_alpha << (i * 4);
  2494. if (surf->color_is_int8)
  2495. sctx->framebuffer.color_is_int8 |= 1 << i;
  2496. if (surf->color_is_int10)
  2497. sctx->framebuffer.color_is_int10 |= 1 << i;
  2498. if (rtex->surface.fmask_size)
  2499. sctx->framebuffer.compressed_cb_mask |= 1 << i;
  2500. else
  2501. sctx->framebuffer.uncompressed_cb_mask |= 1 << i;
  2502. /* Don't update nr_color_samples for non-AA buffers.
  2503. * (e.g. destination of MSAA resolve)
  2504. */
  2505. if (rtex->buffer.b.b.nr_samples >= 2 &&
  2506. rtex->num_color_samples < rtex->buffer.b.b.nr_samples) {
  2507. sctx->framebuffer.nr_color_samples =
  2508. MIN2(sctx->framebuffer.nr_color_samples,
  2509. rtex->num_color_samples);
  2510. }
  2511. if (rtex->surface.is_linear)
  2512. sctx->framebuffer.any_dst_linear = true;
  2513. if (vi_dcc_enabled(rtex, surf->base.u.tex.level))
  2514. sctx->framebuffer.CB_has_shader_readable_metadata = true;
  2515. si_context_add_resource_size(sctx, surf->base.texture);
  2516. p_atomic_inc(&rtex->framebuffers_bound);
  2517. if (rtex->dcc_gather_statistics) {
  2518. /* Dirty tracking must be enabled for DCC usage analysis. */
  2519. sctx->framebuffer.compressed_cb_mask |= 1 << i;
  2520. vi_separate_dcc_start_query(sctx, rtex);
  2521. }
  2522. }
  2523. struct r600_texture *zstex = NULL;
  2524. if (state->zsbuf) {
  2525. surf = (struct r600_surface*)state->zsbuf;
  2526. zstex = (struct r600_texture*)surf->base.texture;
  2527. if (!surf->depth_initialized) {
  2528. si_init_depth_surface(sctx, surf);
  2529. }
  2530. if (vi_tc_compat_htile_enabled(zstex, surf->base.u.tex.level))
  2531. sctx->framebuffer.DB_has_shader_readable_metadata = true;
  2532. si_context_add_resource_size(sctx, surf->base.texture);
  2533. }
  2534. si_update_ps_colorbuf0_slot(sctx);
  2535. si_update_poly_offset_state(sctx);
  2536. si_mark_atom_dirty(sctx, &sctx->atoms.s.cb_render_state);
  2537. si_mark_atom_dirty(sctx, &sctx->atoms.s.framebuffer);
  2538. if (sctx->screen->dpbb_allowed)
  2539. si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
  2540. if (sctx->framebuffer.any_dst_linear != old_any_dst_linear)
  2541. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
  2542. if (sctx->screen->has_out_of_order_rast &&
  2543. (sctx->framebuffer.colorbuf_enabled_4bit != old_colorbuf_enabled_4bit ||
  2544. !!sctx->framebuffer.state.zsbuf != old_has_zsbuf ||
  2545. (zstex && zstex->surface.has_stencil != old_has_stencil)))
  2546. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
  2547. if (sctx->framebuffer.nr_samples != old_nr_samples) {
  2548. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
  2549. si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
  2550. /* Set sample locations as fragment shader constants. */
  2551. switch (sctx->framebuffer.nr_samples) {
  2552. case 1:
  2553. constbuf.user_buffer = sctx->sample_locations_1x;
  2554. break;
  2555. case 2:
  2556. constbuf.user_buffer = sctx->sample_locations_2x;
  2557. break;
  2558. case 4:
  2559. constbuf.user_buffer = sctx->sample_locations_4x;
  2560. break;
  2561. case 8:
  2562. constbuf.user_buffer = sctx->sample_locations_8x;
  2563. break;
  2564. case 16:
  2565. constbuf.user_buffer = sctx->sample_locations_16x;
  2566. break;
  2567. default:
  2568. PRINT_ERR("Requested an invalid number of samples %i.\n",
  2569. sctx->framebuffer.nr_samples);
  2570. assert(0);
  2571. }
  2572. constbuf.buffer_size = sctx->framebuffer.nr_samples * 2 * 4;
  2573. si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS, &constbuf);
  2574. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_sample_locs);
  2575. }
  2576. sctx->do_update_shaders = true;
  2577. if (!sctx->decompression_enabled) {
  2578. /* Prevent textures decompression when the framebuffer state
  2579. * changes come from the decompression passes themselves.
  2580. */
  2581. sctx->need_check_render_feedback = true;
  2582. }
  2583. }
  2584. static void si_emit_framebuffer_state(struct si_context *sctx)
  2585. {
  2586. struct radeon_winsys_cs *cs = sctx->gfx_cs;
  2587. struct pipe_framebuffer_state *state = &sctx->framebuffer.state;
  2588. unsigned i, nr_cbufs = state->nr_cbufs;
  2589. struct r600_texture *tex = NULL;
  2590. struct r600_surface *cb = NULL;
  2591. unsigned cb_color_info = 0;
  2592. /* Colorbuffers. */
  2593. for (i = 0; i < nr_cbufs; i++) {
  2594. uint64_t cb_color_base, cb_color_fmask, cb_color_cmask, cb_dcc_base;
  2595. unsigned cb_color_attrib;
  2596. if (!(sctx->framebuffer.dirty_cbufs & (1 << i)))
  2597. continue;
  2598. cb = (struct r600_surface*)state->cbufs[i];
  2599. if (!cb) {
  2600. radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + i * 0x3C,
  2601. S_028C70_FORMAT(V_028C70_COLOR_INVALID));
  2602. continue;
  2603. }
  2604. tex = (struct r600_texture *)cb->base.texture;
  2605. radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
  2606. &tex->buffer, RADEON_USAGE_READWRITE,
  2607. tex->buffer.b.b.nr_samples > 1 ?
  2608. RADEON_PRIO_COLOR_BUFFER_MSAA :
  2609. RADEON_PRIO_COLOR_BUFFER);
  2610. if (tex->cmask_buffer && tex->cmask_buffer != &tex->buffer) {
  2611. radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
  2612. tex->cmask_buffer, RADEON_USAGE_READWRITE,
  2613. RADEON_PRIO_CMASK);
  2614. }
  2615. if (tex->dcc_separate_buffer)
  2616. radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
  2617. tex->dcc_separate_buffer,
  2618. RADEON_USAGE_READWRITE,
  2619. RADEON_PRIO_DCC);
  2620. /* Compute mutable surface parameters. */
  2621. cb_color_base = tex->buffer.gpu_address >> 8;
  2622. cb_color_fmask = 0;
  2623. cb_color_cmask = tex->cmask.base_address_reg;
  2624. cb_dcc_base = 0;
  2625. cb_color_info = cb->cb_color_info | tex->cb_color_info;
  2626. cb_color_attrib = cb->cb_color_attrib;
  2627. if (cb->base.u.tex.level > 0)
  2628. cb_color_info &= C_028C70_FAST_CLEAR;
  2629. if (tex->surface.fmask_size) {
  2630. cb_color_fmask = (tex->buffer.gpu_address + tex->fmask_offset) >> 8;
  2631. cb_color_fmask |= tex->surface.fmask_tile_swizzle;
  2632. }
  2633. /* Set up DCC. */
  2634. if (vi_dcc_enabled(tex, cb->base.u.tex.level)) {
  2635. bool is_msaa_resolve_dst = state->cbufs[0] &&
  2636. state->cbufs[0]->texture->nr_samples > 1 &&
  2637. state->cbufs[1] == &cb->base &&
  2638. state->cbufs[1]->texture->nr_samples <= 1;
  2639. if (!is_msaa_resolve_dst)
  2640. cb_color_info |= S_028C70_DCC_ENABLE(1);
  2641. cb_dcc_base = ((!tex->dcc_separate_buffer ? tex->buffer.gpu_address : 0) +
  2642. tex->dcc_offset) >> 8;
  2643. cb_dcc_base |= tex->surface.tile_swizzle;
  2644. }
  2645. if (sctx->chip_class >= GFX9) {
  2646. struct gfx9_surf_meta_flags meta;
  2647. if (tex->dcc_offset)
  2648. meta = tex->surface.u.gfx9.dcc;
  2649. else
  2650. meta = tex->surface.u.gfx9.cmask;
  2651. /* Set mutable surface parameters. */
  2652. cb_color_base += tex->surface.u.gfx9.surf_offset >> 8;
  2653. cb_color_base |= tex->surface.tile_swizzle;
  2654. if (!tex->surface.fmask_size)
  2655. cb_color_fmask = cb_color_base;
  2656. if (cb->base.u.tex.level > 0)
  2657. cb_color_cmask = cb_color_base;
  2658. cb_color_attrib |= S_028C74_COLOR_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode) |
  2659. S_028C74_FMASK_SW_MODE(tex->surface.u.gfx9.fmask.swizzle_mode) |
  2660. S_028C74_RB_ALIGNED(meta.rb_aligned) |
  2661. S_028C74_PIPE_ALIGNED(meta.pipe_aligned);
  2662. radeon_set_context_reg_seq(cs, R_028C60_CB_COLOR0_BASE + i * 0x3C, 15);
  2663. radeon_emit(cs, cb_color_base); /* CB_COLOR0_BASE */
  2664. radeon_emit(cs, S_028C64_BASE_256B(cb_color_base >> 32)); /* CB_COLOR0_BASE_EXT */
  2665. radeon_emit(cs, cb->cb_color_attrib2); /* CB_COLOR0_ATTRIB2 */
  2666. radeon_emit(cs, cb->cb_color_view); /* CB_COLOR0_VIEW */
  2667. radeon_emit(cs, cb_color_info); /* CB_COLOR0_INFO */
  2668. radeon_emit(cs, cb_color_attrib); /* CB_COLOR0_ATTRIB */
  2669. radeon_emit(cs, cb->cb_dcc_control); /* CB_COLOR0_DCC_CONTROL */
  2670. radeon_emit(cs, cb_color_cmask); /* CB_COLOR0_CMASK */
  2671. radeon_emit(cs, S_028C80_BASE_256B(cb_color_cmask >> 32)); /* CB_COLOR0_CMASK_BASE_EXT */
  2672. radeon_emit(cs, cb_color_fmask); /* CB_COLOR0_FMASK */
  2673. radeon_emit(cs, S_028C88_BASE_256B(cb_color_fmask >> 32)); /* CB_COLOR0_FMASK_BASE_EXT */
  2674. radeon_emit(cs, tex->color_clear_value[0]); /* CB_COLOR0_CLEAR_WORD0 */
  2675. radeon_emit(cs, tex->color_clear_value[1]); /* CB_COLOR0_CLEAR_WORD1 */
  2676. radeon_emit(cs, cb_dcc_base); /* CB_COLOR0_DCC_BASE */
  2677. radeon_emit(cs, S_028C98_BASE_256B(cb_dcc_base >> 32)); /* CB_COLOR0_DCC_BASE_EXT */
  2678. radeon_set_context_reg(cs, R_0287A0_CB_MRT0_EPITCH + i * 4,
  2679. S_0287A0_EPITCH(tex->surface.u.gfx9.surf.epitch));
  2680. } else {
  2681. /* Compute mutable surface parameters (SI-CI-VI). */
  2682. const struct legacy_surf_level *level_info =
  2683. &tex->surface.u.legacy.level[cb->base.u.tex.level];
  2684. unsigned pitch_tile_max, slice_tile_max, tile_mode_index;
  2685. unsigned cb_color_pitch, cb_color_slice, cb_color_fmask_slice;
  2686. cb_color_base += level_info->offset >> 8;
  2687. /* Only macrotiled modes can set tile swizzle. */
  2688. if (level_info->mode == RADEON_SURF_MODE_2D)
  2689. cb_color_base |= tex->surface.tile_swizzle;
  2690. if (!tex->surface.fmask_size)
  2691. cb_color_fmask = cb_color_base;
  2692. if (cb->base.u.tex.level > 0)
  2693. cb_color_cmask = cb_color_base;
  2694. if (cb_dcc_base)
  2695. cb_dcc_base += level_info->dcc_offset >> 8;
  2696. pitch_tile_max = level_info->nblk_x / 8 - 1;
  2697. slice_tile_max = level_info->nblk_x *
  2698. level_info->nblk_y / 64 - 1;
  2699. tile_mode_index = si_tile_mode_index(tex, cb->base.u.tex.level, false);
  2700. cb_color_attrib |= S_028C74_TILE_MODE_INDEX(tile_mode_index);
  2701. cb_color_pitch = S_028C64_TILE_MAX(pitch_tile_max);
  2702. cb_color_slice = S_028C68_TILE_MAX(slice_tile_max);
  2703. if (tex->surface.fmask_size) {
  2704. if (sctx->chip_class >= CIK)
  2705. cb_color_pitch |= S_028C64_FMASK_TILE_MAX(tex->surface.u.legacy.fmask.pitch_in_pixels / 8 - 1);
  2706. cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tex->surface.u.legacy.fmask.tiling_index);
  2707. cb_color_fmask_slice = S_028C88_TILE_MAX(tex->surface.u.legacy.fmask.slice_tile_max);
  2708. } else {
  2709. /* This must be set for fast clear to work without FMASK. */
  2710. if (sctx->chip_class >= CIK)
  2711. cb_color_pitch |= S_028C64_FMASK_TILE_MAX(pitch_tile_max);
  2712. cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index);
  2713. cb_color_fmask_slice = S_028C88_TILE_MAX(slice_tile_max);
  2714. }
  2715. radeon_set_context_reg_seq(cs, R_028C60_CB_COLOR0_BASE + i * 0x3C,
  2716. sctx->chip_class >= VI ? 14 : 13);
  2717. radeon_emit(cs, cb_color_base); /* CB_COLOR0_BASE */
  2718. radeon_emit(cs, cb_color_pitch); /* CB_COLOR0_PITCH */
  2719. radeon_emit(cs, cb_color_slice); /* CB_COLOR0_SLICE */
  2720. radeon_emit(cs, cb->cb_color_view); /* CB_COLOR0_VIEW */
  2721. radeon_emit(cs, cb_color_info); /* CB_COLOR0_INFO */
  2722. radeon_emit(cs, cb_color_attrib); /* CB_COLOR0_ATTRIB */
  2723. radeon_emit(cs, cb->cb_dcc_control); /* CB_COLOR0_DCC_CONTROL */
  2724. radeon_emit(cs, cb_color_cmask); /* CB_COLOR0_CMASK */
  2725. radeon_emit(cs, tex->cmask.slice_tile_max); /* CB_COLOR0_CMASK_SLICE */
  2726. radeon_emit(cs, cb_color_fmask); /* CB_COLOR0_FMASK */
  2727. radeon_emit(cs, cb_color_fmask_slice); /* CB_COLOR0_FMASK_SLICE */
  2728. radeon_emit(cs, tex->color_clear_value[0]); /* CB_COLOR0_CLEAR_WORD0 */
  2729. radeon_emit(cs, tex->color_clear_value[1]); /* CB_COLOR0_CLEAR_WORD1 */
  2730. if (sctx->chip_class >= VI) /* R_028C94_CB_COLOR0_DCC_BASE */
  2731. radeon_emit(cs, cb_dcc_base);
  2732. }
  2733. }
  2734. for (; i < 8 ; i++)
  2735. if (sctx->framebuffer.dirty_cbufs & (1 << i))
  2736. radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + i * 0x3C, 0);
  2737. /* ZS buffer. */
  2738. if (state->zsbuf && sctx->framebuffer.dirty_zsbuf) {
  2739. struct r600_surface *zb = (struct r600_surface*)state->zsbuf;
  2740. struct r600_texture *rtex = (struct r600_texture*)zb->base.texture;
  2741. radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
  2742. &rtex->buffer, RADEON_USAGE_READWRITE,
  2743. zb->base.texture->nr_samples > 1 ?
  2744. RADEON_PRIO_DEPTH_BUFFER_MSAA :
  2745. RADEON_PRIO_DEPTH_BUFFER);
  2746. if (sctx->chip_class >= GFX9) {
  2747. radeon_set_context_reg_seq(cs, R_028014_DB_HTILE_DATA_BASE, 3);
  2748. radeon_emit(cs, zb->db_htile_data_base); /* DB_HTILE_DATA_BASE */
  2749. radeon_emit(cs, S_028018_BASE_HI(zb->db_htile_data_base >> 32)); /* DB_HTILE_DATA_BASE_HI */
  2750. radeon_emit(cs, zb->db_depth_size); /* DB_DEPTH_SIZE */
  2751. radeon_set_context_reg_seq(cs, R_028038_DB_Z_INFO, 10);
  2752. radeon_emit(cs, zb->db_z_info | /* DB_Z_INFO */
  2753. S_028038_ZRANGE_PRECISION(rtex->depth_clear_value != 0));
  2754. radeon_emit(cs, zb->db_stencil_info); /* DB_STENCIL_INFO */
  2755. radeon_emit(cs, zb->db_depth_base); /* DB_Z_READ_BASE */
  2756. radeon_emit(cs, S_028044_BASE_HI(zb->db_depth_base >> 32)); /* DB_Z_READ_BASE_HI */
  2757. radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_READ_BASE */
  2758. radeon_emit(cs, S_02804C_BASE_HI(zb->db_stencil_base >> 32)); /* DB_STENCIL_READ_BASE_HI */
  2759. radeon_emit(cs, zb->db_depth_base); /* DB_Z_WRITE_BASE */
  2760. radeon_emit(cs, S_028054_BASE_HI(zb->db_depth_base >> 32)); /* DB_Z_WRITE_BASE_HI */
  2761. radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_WRITE_BASE */
  2762. radeon_emit(cs, S_02805C_BASE_HI(zb->db_stencil_base >> 32)); /* DB_STENCIL_WRITE_BASE_HI */
  2763. radeon_set_context_reg_seq(cs, R_028068_DB_Z_INFO2, 2);
  2764. radeon_emit(cs, zb->db_z_info2); /* DB_Z_INFO2 */
  2765. radeon_emit(cs, zb->db_stencil_info2); /* DB_STENCIL_INFO2 */
  2766. } else {
  2767. radeon_set_context_reg(cs, R_028014_DB_HTILE_DATA_BASE, zb->db_htile_data_base);
  2768. radeon_set_context_reg_seq(cs, R_02803C_DB_DEPTH_INFO, 9);
  2769. radeon_emit(cs, zb->db_depth_info); /* DB_DEPTH_INFO */
  2770. radeon_emit(cs, zb->db_z_info | /* DB_Z_INFO */
  2771. S_028040_ZRANGE_PRECISION(rtex->depth_clear_value != 0));
  2772. radeon_emit(cs, zb->db_stencil_info); /* DB_STENCIL_INFO */
  2773. radeon_emit(cs, zb->db_depth_base); /* DB_Z_READ_BASE */
  2774. radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_READ_BASE */
  2775. radeon_emit(cs, zb->db_depth_base); /* DB_Z_WRITE_BASE */
  2776. radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_WRITE_BASE */
  2777. radeon_emit(cs, zb->db_depth_size); /* DB_DEPTH_SIZE */
  2778. radeon_emit(cs, zb->db_depth_slice); /* DB_DEPTH_SLICE */
  2779. }
  2780. radeon_set_context_reg_seq(cs, R_028028_DB_STENCIL_CLEAR, 2);
  2781. radeon_emit(cs, rtex->stencil_clear_value); /* R_028028_DB_STENCIL_CLEAR */
  2782. radeon_emit(cs, fui(rtex->depth_clear_value)); /* R_02802C_DB_DEPTH_CLEAR */
  2783. radeon_set_context_reg(cs, R_028008_DB_DEPTH_VIEW, zb->db_depth_view);
  2784. radeon_set_context_reg(cs, R_028ABC_DB_HTILE_SURFACE, zb->db_htile_surface);
  2785. } else if (sctx->framebuffer.dirty_zsbuf) {
  2786. if (sctx->chip_class >= GFX9)
  2787. radeon_set_context_reg_seq(cs, R_028038_DB_Z_INFO, 2);
  2788. else
  2789. radeon_set_context_reg_seq(cs, R_028040_DB_Z_INFO, 2);
  2790. radeon_emit(cs, S_028040_FORMAT(V_028040_Z_INVALID)); /* DB_Z_INFO */
  2791. radeon_emit(cs, S_028044_FORMAT(V_028044_STENCIL_INVALID)); /* DB_STENCIL_INFO */
  2792. }
  2793. /* Framebuffer dimensions. */
  2794. /* PA_SC_WINDOW_SCISSOR_TL is set in si_init_config() */
  2795. radeon_set_context_reg(cs, R_028208_PA_SC_WINDOW_SCISSOR_BR,
  2796. S_028208_BR_X(state->width) | S_028208_BR_Y(state->height));
  2797. if (sctx->screen->dfsm_allowed) {
  2798. radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
  2799. radeon_emit(cs, EVENT_TYPE(V_028A90_BREAK_BATCH) | EVENT_INDEX(0));
  2800. }
  2801. sctx->framebuffer.dirty_cbufs = 0;
  2802. sctx->framebuffer.dirty_zsbuf = false;
  2803. }
  2804. static void si_emit_msaa_sample_locs(struct si_context *sctx)
  2805. {
  2806. struct radeon_winsys_cs *cs = sctx->gfx_cs;
  2807. unsigned nr_samples = sctx->framebuffer.nr_samples;
  2808. bool has_msaa_sample_loc_bug = sctx->screen->has_msaa_sample_loc_bug;
  2809. /* Smoothing (only possible with nr_samples == 1) uses the same
  2810. * sample locations as the MSAA it simulates.
  2811. */
  2812. if (nr_samples <= 1 && sctx->smoothing_enabled)
  2813. nr_samples = SI_NUM_SMOOTH_AA_SAMPLES;
  2814. /* On Polaris, the small primitive filter uses the sample locations
  2815. * even when MSAA is off, so we need to make sure they're set to 0.
  2816. */
  2817. if (has_msaa_sample_loc_bug)
  2818. nr_samples = MAX2(nr_samples, 1);
  2819. if (nr_samples != sctx->sample_locs_num_samples) {
  2820. sctx->sample_locs_num_samples = nr_samples;
  2821. si_emit_sample_locations(cs, nr_samples);
  2822. }
  2823. if (sctx->family >= CHIP_POLARIS10) {
  2824. struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
  2825. unsigned small_prim_filter_cntl =
  2826. S_028830_SMALL_PRIM_FILTER_ENABLE(1) |
  2827. /* line bug */
  2828. S_028830_LINE_FILTER_DISABLE(sctx->family <= CHIP_POLARIS12);
  2829. /* The alternative of setting sample locations to 0 would
  2830. * require a DB flush to avoid Z errors, see
  2831. * https://bugs.freedesktop.org/show_bug.cgi?id=96908
  2832. */
  2833. if (has_msaa_sample_loc_bug &&
  2834. sctx->framebuffer.nr_samples > 1 &&
  2835. rs && !rs->multisample_enable)
  2836. small_prim_filter_cntl &= C_028830_SMALL_PRIM_FILTER_ENABLE;
  2837. radeon_opt_set_context_reg(sctx,
  2838. R_028830_PA_SU_SMALL_PRIM_FILTER_CNTL,
  2839. SI_TRACKED_PA_SU_SMALL_PRIM_FILTER_CNTL,
  2840. small_prim_filter_cntl);
  2841. }
  2842. }
  2843. static bool si_out_of_order_rasterization(struct si_context *sctx)
  2844. {
  2845. struct si_state_blend *blend = sctx->queued.named.blend;
  2846. struct si_state_dsa *dsa = sctx->queued.named.dsa;
  2847. if (!sctx->screen->has_out_of_order_rast)
  2848. return false;
  2849. unsigned colormask = sctx->framebuffer.colorbuf_enabled_4bit;
  2850. if (blend) {
  2851. colormask &= blend->cb_target_enabled_4bit;
  2852. } else {
  2853. colormask = 0;
  2854. }
  2855. /* Conservative: No logic op. */
  2856. if (colormask && blend->logicop_enable)
  2857. return false;
  2858. struct si_dsa_order_invariance dsa_order_invariant = {
  2859. .zs = true, .pass_set = true, .pass_last = false
  2860. };
  2861. if (sctx->framebuffer.state.zsbuf) {
  2862. struct r600_texture *zstex =
  2863. (struct r600_texture*)sctx->framebuffer.state.zsbuf->texture;
  2864. bool has_stencil = zstex->surface.has_stencil;
  2865. dsa_order_invariant = dsa->order_invariance[has_stencil];
  2866. if (!dsa_order_invariant.zs)
  2867. return false;
  2868. /* The set of PS invocations is always order invariant,
  2869. * except when early Z/S tests are requested. */
  2870. if (sctx->ps_shader.cso &&
  2871. sctx->ps_shader.cso->info.writes_memory &&
  2872. sctx->ps_shader.cso->info.properties[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL] &&
  2873. !dsa_order_invariant.pass_set)
  2874. return false;
  2875. if (sctx->num_perfect_occlusion_queries != 0 &&
  2876. !dsa_order_invariant.pass_set)
  2877. return false;
  2878. }
  2879. if (!colormask)
  2880. return true;
  2881. unsigned blendmask = colormask & blend->blend_enable_4bit;
  2882. if (blendmask) {
  2883. /* Only commutative blending. */
  2884. if (blendmask & ~blend->commutative_4bit)
  2885. return false;
  2886. if (!dsa_order_invariant.pass_set)
  2887. return false;
  2888. }
  2889. if (colormask & ~blendmask) {
  2890. if (!dsa_order_invariant.pass_last)
  2891. return false;
  2892. }
  2893. return true;
  2894. }
  2895. static void si_emit_msaa_config(struct si_context *sctx)
  2896. {
  2897. struct radeon_winsys_cs *cs = sctx->gfx_cs;
  2898. unsigned num_tile_pipes = sctx->screen->info.num_tile_pipes;
  2899. /* 33% faster rendering to linear color buffers */
  2900. bool dst_is_linear = sctx->framebuffer.any_dst_linear;
  2901. bool out_of_order_rast = si_out_of_order_rasterization(sctx);
  2902. unsigned sc_mode_cntl_1 =
  2903. S_028A4C_WALK_SIZE(dst_is_linear) |
  2904. S_028A4C_WALK_FENCE_ENABLE(!dst_is_linear) |
  2905. S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |
  2906. S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(out_of_order_rast) |
  2907. S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7) |
  2908. /* always 1: */
  2909. S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
  2910. S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
  2911. S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
  2912. S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
  2913. S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
  2914. S_028A4C_FORCE_EOV_REZ_ENABLE(1);
  2915. unsigned db_eqaa = S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
  2916. S_028804_INCOHERENT_EQAA_READS(1) |
  2917. S_028804_INTERPOLATE_COMP_Z(1) |
  2918. S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
  2919. unsigned coverage_samples, color_samples, z_samples;
  2920. /* S: Coverage samples (up to 16x):
  2921. * - Scan conversion samples (PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES)
  2922. * - CB FMASK samples (CB_COLORi_ATTRIB.NUM_SAMPLES)
  2923. *
  2924. * Z: Z/S samples (up to 8x, must be <= coverage samples and >= color samples):
  2925. * - Value seen by DB (DB_Z_INFO.NUM_SAMPLES)
  2926. * - Value seen by CB, must be correct even if Z/S is unbound (DB_EQAA.MAX_ANCHOR_SAMPLES)
  2927. * # Missing samples are derived from Z planes if Z is compressed (up to 16x quality), or
  2928. * # from the closest defined sample if Z is uncompressed (same quality as the number of
  2929. * # Z samples).
  2930. *
  2931. * F: Color samples (up to 8x, must be <= coverage samples):
  2932. * - CB color samples (CB_COLORi_ATTRIB.NUM_FRAGMENTS)
  2933. * - PS iter samples (DB_EQAA.PS_ITER_SAMPLES)
  2934. *
  2935. * Can be anything between coverage and color samples:
  2936. * - SampleMaskIn samples (PA_SC_AA_CONFIG.MSAA_EXPOSED_SAMPLES)
  2937. * - SampleMaskOut samples (DB_EQAA.MASK_EXPORT_NUM_SAMPLES)
  2938. * - Alpha-to-coverage samples (DB_EQAA.ALPHA_TO_MASK_NUM_SAMPLES)
  2939. * - Occlusion query samples (DB_COUNT_CONTROL.SAMPLE_RATE)
  2940. * # All are currently set the same as coverage samples.
  2941. *
  2942. * If color samples < coverage samples, FMASK has a higher bpp to store an "unknown"
  2943. * flag for undefined color samples. A shader-based resolve must handle unknowns
  2944. * or mask them out with AND. Unknowns can also be guessed from neighbors via
  2945. * an edge-detect shader-based resolve, which is required to make "color samples = 1"
  2946. * useful. The CB resolve always drops unknowns.
  2947. *
  2948. * Sensible AA configurations:
  2949. * EQAA 16s 8z 8f - might look the same as 16x MSAA if Z is compressed
  2950. * EQAA 16s 8z 4f - might look the same as 16x MSAA if Z is compressed
  2951. * EQAA 16s 4z 4f - might look the same as 16x MSAA if Z is compressed
  2952. * EQAA 8s 8z 8f = 8x MSAA
  2953. * EQAA 8s 8z 4f - might look the same as 8x MSAA
  2954. * EQAA 8s 8z 2f - might look the same as 8x MSAA with low-density geometry
  2955. * EQAA 8s 4z 4f - might look the same as 8x MSAA if Z is compressed
  2956. * EQAA 8s 4z 2f - might look the same as 8x MSAA with low-density geometry if Z is compressed
  2957. * EQAA 4s 4z 4f = 4x MSAA
  2958. * EQAA 4s 4z 2f - might look the same as 4x MSAA with low-density geometry
  2959. * EQAA 2s 2z 2f = 2x MSAA
  2960. */
  2961. if (sctx->framebuffer.nr_samples > 1) {
  2962. coverage_samples = sctx->framebuffer.nr_samples;
  2963. color_samples = sctx->framebuffer.nr_color_samples;
  2964. if (sctx->framebuffer.state.zsbuf) {
  2965. z_samples = sctx->framebuffer.state.zsbuf->texture->nr_samples;
  2966. z_samples = MAX2(1, z_samples);
  2967. } else {
  2968. z_samples = coverage_samples;
  2969. }
  2970. } else if (sctx->smoothing_enabled) {
  2971. coverage_samples = color_samples = z_samples = SI_NUM_SMOOTH_AA_SAMPLES;
  2972. } else {
  2973. coverage_samples = color_samples = z_samples = 1;
  2974. }
  2975. /* Required by OpenGL line rasterization.
  2976. *
  2977. * TODO: We should also enable perpendicular endcaps for AA lines,
  2978. * but that requires implementing line stippling in the pixel
  2979. * shader. SC can only do line stippling with axis-aligned
  2980. * endcaps.
  2981. */
  2982. unsigned sc_line_cntl = S_028BDC_DX10_DIAMOND_TEST_ENA(1);
  2983. unsigned sc_aa_config = 0;
  2984. if (coverage_samples > 1) {
  2985. /* distance from the pixel center, indexed by log2(nr_samples) */
  2986. static unsigned max_dist[] = {
  2987. 0, /* unused */
  2988. 4, /* 2x MSAA */
  2989. 6, /* 4x MSAA */
  2990. 7, /* 8x MSAA */
  2991. 8, /* 16x MSAA */
  2992. };
  2993. unsigned log_samples = util_logbase2(coverage_samples);
  2994. unsigned log_z_samples = util_logbase2(z_samples);
  2995. unsigned ps_iter_samples = si_get_ps_iter_samples(sctx);
  2996. unsigned log_ps_iter_samples = util_logbase2(ps_iter_samples);
  2997. sc_line_cntl |= S_028BDC_EXPAND_LINE_WIDTH(1);
  2998. sc_aa_config = S_028BE0_MSAA_NUM_SAMPLES(log_samples) |
  2999. S_028BE0_MAX_SAMPLE_DIST(max_dist[log_samples]) |
  3000. S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples);
  3001. if (sctx->framebuffer.nr_samples > 1) {
  3002. db_eqaa |= S_028804_MAX_ANCHOR_SAMPLES(log_z_samples) |
  3003. S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |
  3004. S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |
  3005. S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples);
  3006. sc_mode_cntl_1 |= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1);
  3007. } else if (sctx->smoothing_enabled) {
  3008. db_eqaa |= S_028804_OVERRASTERIZATION_AMOUNT(log_samples);
  3009. }
  3010. }
  3011. /* R_028BDC_PA_SC_LINE_CNTL, R_028BE0_PA_SC_AA_CONFIG */
  3012. radeon_opt_set_context_reg2(sctx, R_028BDC_PA_SC_LINE_CNTL,
  3013. SI_TRACKED_PA_SC_LINE_CNTL, sc_line_cntl,
  3014. sc_aa_config);
  3015. /* R_028804_DB_EQAA */
  3016. radeon_opt_set_context_reg(sctx, R_028804_DB_EQAA, SI_TRACKED_DB_EQAA,
  3017. db_eqaa);
  3018. /* R_028A4C_PA_SC_MODE_CNTL_1 */
  3019. radeon_opt_set_context_reg(sctx, R_028A4C_PA_SC_MODE_CNTL_1,
  3020. SI_TRACKED_PA_SC_MODE_CNTL_1, sc_mode_cntl_1);
  3021. /* GFX9: Flush DFSM when the AA mode changes. */
  3022. if (sctx->screen->dfsm_allowed) {
  3023. radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
  3024. radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
  3025. }
  3026. }
  3027. void si_update_ps_iter_samples(struct si_context *sctx)
  3028. {
  3029. if (sctx->framebuffer.nr_samples > 1)
  3030. si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
  3031. if (sctx->screen->dpbb_allowed)
  3032. si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
  3033. }
  3034. static void si_set_min_samples(struct pipe_context *ctx, unsigned min_samples)
  3035. {
  3036. struct si_context *sctx = (struct si_context *)ctx;
  3037. /* The hardware can only do sample shading with 2^n samples. */
  3038. min_samples = util_next_power_of_two(min_samples);
  3039. if (sctx->ps_iter_samples == min_samples)
  3040. return;
  3041. sctx->ps_iter_samples = min_samples;
  3042. sctx->do_update_shaders = true;
  3043. si_update_ps_iter_samples(sctx);
  3044. }
  3045. /*
  3046. * Samplers
  3047. */
  3048. /**
  3049. * Build the sampler view descriptor for a buffer texture.
  3050. * @param state 256-bit descriptor; only the high 128 bits are filled in
  3051. */
  3052. void
  3053. si_make_buffer_descriptor(struct si_screen *screen, struct r600_resource *buf,
  3054. enum pipe_format format,
  3055. unsigned offset, unsigned size,
  3056. uint32_t *state)
  3057. {
  3058. const struct util_format_description *desc;
  3059. int first_non_void;
  3060. unsigned stride;
  3061. unsigned num_records;
  3062. unsigned num_format, data_format;
  3063. desc = util_format_description(format);
  3064. first_non_void = util_format_get_first_non_void_channel(format);
  3065. stride = desc->block.bits / 8;
  3066. num_format = si_translate_buffer_numformat(&screen->b, desc, first_non_void);
  3067. data_format = si_translate_buffer_dataformat(&screen->b, desc, first_non_void);
  3068. num_records = size / stride;
  3069. num_records = MIN2(num_records, (buf->b.b.width0 - offset) / stride);
  3070. /* The NUM_RECORDS field has a different meaning depending on the chip,
  3071. * instruction type, STRIDE, and SWIZZLE_ENABLE.
  3072. *
  3073. * SI-CIK:
  3074. * - If STRIDE == 0, it's in byte units.
  3075. * - If STRIDE != 0, it's in units of STRIDE, used with inst.IDXEN.
  3076. *
  3077. * VI:
  3078. * - For SMEM and STRIDE == 0, it's in byte units.
  3079. * - For SMEM and STRIDE != 0, it's in units of STRIDE.
  3080. * - For VMEM and STRIDE == 0 or SWIZZLE_ENABLE == 0, it's in byte units.
  3081. * - For VMEM and STRIDE != 0 and SWIZZLE_ENABLE == 1, it's in units of STRIDE.
  3082. * NOTE: There is incompatibility between VMEM and SMEM opcodes due to SWIZZLE_-
  3083. * ENABLE. The workaround is to set STRIDE = 0 if SWIZZLE_ENABLE == 0 when
  3084. * using SMEM. This can be done in the shader by clearing STRIDE with s_and.
  3085. * That way the same descriptor can be used by both SMEM and VMEM.
  3086. *
  3087. * GFX9:
  3088. * - For SMEM and STRIDE == 0, it's in byte units.
  3089. * - For SMEM and STRIDE != 0, it's in units of STRIDE.
  3090. * - For VMEM and inst.IDXEN == 0 or STRIDE == 0, it's in byte units.
  3091. * - For VMEM and inst.IDXEN == 1 and STRIDE != 0, it's in units of STRIDE.
  3092. */
  3093. if (screen->info.chip_class >= GFX9)
  3094. /* When vindex == 0, LLVM sets IDXEN = 0, thus changing units
  3095. * from STRIDE to bytes. This works around it by setting
  3096. * NUM_RECORDS to at least the size of one element, so that
  3097. * the first element is readable when IDXEN == 0.
  3098. *
  3099. * TODO: Fix this in LLVM, but do we need a new intrinsic where
  3100. * IDXEN is enforced?
  3101. */
  3102. num_records = num_records ? MAX2(num_records, stride) : 0;
  3103. else if (screen->info.chip_class == VI)
  3104. num_records *= stride;
  3105. state[4] = 0;
  3106. state[5] = S_008F04_STRIDE(stride);
  3107. state[6] = num_records;
  3108. state[7] = S_008F0C_DST_SEL_X(si_map_swizzle(desc->swizzle[0])) |
  3109. S_008F0C_DST_SEL_Y(si_map_swizzle(desc->swizzle[1])) |
  3110. S_008F0C_DST_SEL_Z(si_map_swizzle(desc->swizzle[2])) |
  3111. S_008F0C_DST_SEL_W(si_map_swizzle(desc->swizzle[3])) |
  3112. S_008F0C_NUM_FORMAT(num_format) |
  3113. S_008F0C_DATA_FORMAT(data_format);
  3114. }
  3115. static unsigned gfx9_border_color_swizzle(const unsigned char swizzle[4])
  3116. {
  3117. unsigned bc_swizzle = V_008F20_BC_SWIZZLE_XYZW;
  3118. if (swizzle[3] == PIPE_SWIZZLE_X) {
  3119. /* For the pre-defined border color values (white, opaque
  3120. * black, transparent black), the only thing that matters is
  3121. * that the alpha channel winds up in the correct place
  3122. * (because the RGB channels are all the same) so either of
  3123. * these enumerations will work.
  3124. */
  3125. if (swizzle[2] == PIPE_SWIZZLE_Y)
  3126. bc_swizzle = V_008F20_BC_SWIZZLE_WZYX;
  3127. else
  3128. bc_swizzle = V_008F20_BC_SWIZZLE_WXYZ;
  3129. } else if (swizzle[0] == PIPE_SWIZZLE_X) {
  3130. if (swizzle[1] == PIPE_SWIZZLE_Y)
  3131. bc_swizzle = V_008F20_BC_SWIZZLE_XYZW;
  3132. else
  3133. bc_swizzle = V_008F20_BC_SWIZZLE_XWYZ;
  3134. } else if (swizzle[1] == PIPE_SWIZZLE_X) {
  3135. bc_swizzle = V_008F20_BC_SWIZZLE_YXWZ;
  3136. } else if (swizzle[2] == PIPE_SWIZZLE_X) {
  3137. bc_swizzle = V_008F20_BC_SWIZZLE_ZYXW;
  3138. }
  3139. return bc_swizzle;
  3140. }
  3141. /**
  3142. * Build the sampler view descriptor for a texture.
  3143. */
  3144. void
  3145. si_make_texture_descriptor(struct si_screen *screen,
  3146. struct r600_texture *tex,
  3147. bool sampler,
  3148. enum pipe_texture_target target,
  3149. enum pipe_format pipe_format,
  3150. const unsigned char state_swizzle[4],
  3151. unsigned first_level, unsigned last_level,
  3152. unsigned first_layer, unsigned last_layer,
  3153. unsigned width, unsigned height, unsigned depth,
  3154. uint32_t *state,
  3155. uint32_t *fmask_state)
  3156. {
  3157. struct pipe_resource *res = &tex->buffer.b.b;
  3158. const struct util_format_description *desc;
  3159. unsigned char swizzle[4];
  3160. int first_non_void;
  3161. unsigned num_format, data_format, type, num_samples;
  3162. uint64_t va;
  3163. desc = util_format_description(pipe_format);
  3164. num_samples = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ?
  3165. MAX2(1, res->nr_samples) : tex->num_color_samples;
  3166. if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
  3167. const unsigned char swizzle_xxxx[4] = {0, 0, 0, 0};
  3168. const unsigned char swizzle_yyyy[4] = {1, 1, 1, 1};
  3169. const unsigned char swizzle_wwww[4] = {3, 3, 3, 3};
  3170. switch (pipe_format) {
  3171. case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  3172. case PIPE_FORMAT_X32_S8X24_UINT:
  3173. case PIPE_FORMAT_X8Z24_UNORM:
  3174. util_format_compose_swizzles(swizzle_yyyy, state_swizzle, swizzle);
  3175. break;
  3176. case PIPE_FORMAT_X24S8_UINT:
  3177. /*
  3178. * X24S8 is implemented as an 8_8_8_8 data format, to
  3179. * fix texture gathers. This affects at least
  3180. * GL45-CTS.texture_cube_map_array.sampling on VI.
  3181. */
  3182. if (screen->info.chip_class <= VI)
  3183. util_format_compose_swizzles(swizzle_wwww, state_swizzle, swizzle);
  3184. else
  3185. util_format_compose_swizzles(swizzle_yyyy, state_swizzle, swizzle);
  3186. break;
  3187. default:
  3188. util_format_compose_swizzles(swizzle_xxxx, state_swizzle, swizzle);
  3189. }
  3190. } else {
  3191. util_format_compose_swizzles(desc->swizzle, state_swizzle, swizzle);
  3192. }
  3193. first_non_void = util_format_get_first_non_void_channel(pipe_format);
  3194. switch (pipe_format) {
  3195. case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  3196. num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
  3197. break;
  3198. default:
  3199. if (first_non_void < 0) {
  3200. if (util_format_is_compressed(pipe_format)) {
  3201. switch (pipe_format) {
  3202. case PIPE_FORMAT_DXT1_SRGB:
  3203. case PIPE_FORMAT_DXT1_SRGBA:
  3204. case PIPE_FORMAT_DXT3_SRGBA:
  3205. case PIPE_FORMAT_DXT5_SRGBA:
  3206. case PIPE_FORMAT_BPTC_SRGBA:
  3207. case PIPE_FORMAT_ETC2_SRGB8:
  3208. case PIPE_FORMAT_ETC2_SRGB8A1:
  3209. case PIPE_FORMAT_ETC2_SRGBA8:
  3210. num_format = V_008F14_IMG_NUM_FORMAT_SRGB;
  3211. break;
  3212. case PIPE_FORMAT_RGTC1_SNORM:
  3213. case PIPE_FORMAT_LATC1_SNORM:
  3214. case PIPE_FORMAT_RGTC2_SNORM:
  3215. case PIPE_FORMAT_LATC2_SNORM:
  3216. case PIPE_FORMAT_ETC2_R11_SNORM:
  3217. case PIPE_FORMAT_ETC2_RG11_SNORM:
  3218. /* implies float, so use SNORM/UNORM to determine
  3219. whether data is signed or not */
  3220. case PIPE_FORMAT_BPTC_RGB_FLOAT:
  3221. num_format = V_008F14_IMG_NUM_FORMAT_SNORM;
  3222. break;
  3223. default:
  3224. num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
  3225. break;
  3226. }
  3227. } else if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
  3228. num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
  3229. } else {
  3230. num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
  3231. }
  3232. } else if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
  3233. num_format = V_008F14_IMG_NUM_FORMAT_SRGB;
  3234. } else {
  3235. num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
  3236. switch (desc->channel[first_non_void].type) {
  3237. case UTIL_FORMAT_TYPE_FLOAT:
  3238. num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
  3239. break;
  3240. case UTIL_FORMAT_TYPE_SIGNED:
  3241. if (desc->channel[first_non_void].normalized)
  3242. num_format = V_008F14_IMG_NUM_FORMAT_SNORM;
  3243. else if (desc->channel[first_non_void].pure_integer)
  3244. num_format = V_008F14_IMG_NUM_FORMAT_SINT;
  3245. else
  3246. num_format = V_008F14_IMG_NUM_FORMAT_SSCALED;
  3247. break;
  3248. case UTIL_FORMAT_TYPE_UNSIGNED:
  3249. if (desc->channel[first_non_void].normalized)
  3250. num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
  3251. else if (desc->channel[first_non_void].pure_integer)
  3252. num_format = V_008F14_IMG_NUM_FORMAT_UINT;
  3253. else
  3254. num_format = V_008F14_IMG_NUM_FORMAT_USCALED;
  3255. }
  3256. }
  3257. }
  3258. data_format = si_translate_texformat(&screen->b, pipe_format, desc, first_non_void);
  3259. if (data_format == ~0) {
  3260. data_format = 0;
  3261. }
  3262. /* S8 with Z32 HTILE needs a special format. */
  3263. if (screen->info.chip_class >= GFX9 &&
  3264. pipe_format == PIPE_FORMAT_S8_UINT &&
  3265. tex->tc_compatible_htile)
  3266. data_format = V_008F14_IMG_DATA_FORMAT_S8_32;
  3267. if (!sampler &&
  3268. (res->target == PIPE_TEXTURE_CUBE ||
  3269. res->target == PIPE_TEXTURE_CUBE_ARRAY ||
  3270. (screen->info.chip_class <= VI &&
  3271. res->target == PIPE_TEXTURE_3D))) {
  3272. /* For the purpose of shader images, treat cube maps and 3D
  3273. * textures as 2D arrays. For 3D textures, the address
  3274. * calculations for mipmaps are different, so we rely on the
  3275. * caller to effectively disable mipmaps.
  3276. */
  3277. type = V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
  3278. assert(res->target != PIPE_TEXTURE_3D || (first_level == 0 && last_level == 0));
  3279. } else {
  3280. type = si_tex_dim(screen, tex, target, num_samples);
  3281. }
  3282. if (type == V_008F1C_SQ_RSRC_IMG_1D_ARRAY) {
  3283. height = 1;
  3284. depth = res->array_size;
  3285. } else if (type == V_008F1C_SQ_RSRC_IMG_2D_ARRAY ||
  3286. type == V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY) {
  3287. if (sampler || res->target != PIPE_TEXTURE_3D)
  3288. depth = res->array_size;
  3289. } else if (type == V_008F1C_SQ_RSRC_IMG_CUBE)
  3290. depth = res->array_size / 6;
  3291. state[0] = 0;
  3292. state[1] = (S_008F14_DATA_FORMAT_GFX6(data_format) |
  3293. S_008F14_NUM_FORMAT_GFX6(num_format));
  3294. state[2] = (S_008F18_WIDTH(width - 1) |
  3295. S_008F18_HEIGHT(height - 1) |
  3296. S_008F18_PERF_MOD(4));
  3297. state[3] = (S_008F1C_DST_SEL_X(si_map_swizzle(swizzle[0])) |
  3298. S_008F1C_DST_SEL_Y(si_map_swizzle(swizzle[1])) |
  3299. S_008F1C_DST_SEL_Z(si_map_swizzle(swizzle[2])) |
  3300. S_008F1C_DST_SEL_W(si_map_swizzle(swizzle[3])) |
  3301. S_008F1C_BASE_LEVEL(num_samples > 1 ? 0 : first_level) |
  3302. S_008F1C_LAST_LEVEL(num_samples > 1 ?
  3303. util_logbase2(num_samples) :
  3304. last_level) |
  3305. S_008F1C_TYPE(type));
  3306. state[4] = 0;
  3307. state[5] = S_008F24_BASE_ARRAY(first_layer);
  3308. state[6] = 0;
  3309. state[7] = 0;
  3310. if (screen->info.chip_class >= GFX9) {
  3311. unsigned bc_swizzle = gfx9_border_color_swizzle(desc->swizzle);
  3312. /* Depth is the the last accessible layer on Gfx9.
  3313. * The hw doesn't need to know the total number of layers.
  3314. */
  3315. if (type == V_008F1C_SQ_RSRC_IMG_3D)
  3316. state[4] |= S_008F20_DEPTH(depth - 1);
  3317. else
  3318. state[4] |= S_008F20_DEPTH(last_layer);
  3319. state[4] |= S_008F20_BC_SWIZZLE(bc_swizzle);
  3320. state[5] |= S_008F24_MAX_MIP(num_samples > 1 ?
  3321. util_logbase2(num_samples) :
  3322. tex->buffer.b.b.last_level);
  3323. } else {
  3324. state[3] |= S_008F1C_POW2_PAD(res->last_level > 0);
  3325. state[4] |= S_008F20_DEPTH(depth - 1);
  3326. state[5] |= S_008F24_LAST_ARRAY(last_layer);
  3327. }
  3328. if (tex->dcc_offset) {
  3329. state[6] = S_008F28_ALPHA_IS_ON_MSB(vi_alpha_is_on_msb(pipe_format));
  3330. } else {
  3331. /* The last dword is unused by hw. The shader uses it to clear
  3332. * bits in the first dword of sampler state.
  3333. */
  3334. if (screen->info.chip_class <= CIK && res->nr_samples <= 1) {
  3335. if (first_level == last_level)
  3336. state[7] = C_008F30_MAX_ANISO_RATIO;
  3337. else
  3338. state[7] = 0xffffffff;
  3339. }
  3340. }
  3341. /* Initialize the sampler view for FMASK. */
  3342. if (tex->surface.fmask_size) {
  3343. uint32_t data_format, num_format;
  3344. va = tex->buffer.gpu_address + tex->fmask_offset;
  3345. #define FMASK(s,f) (((unsigned)(s) * 16) + (f))
  3346. if (screen->info.chip_class >= GFX9) {
  3347. data_format = V_008F14_IMG_DATA_FORMAT_FMASK;
  3348. switch (FMASK(res->nr_samples, tex->num_color_samples)) {
  3349. case FMASK(2,1):
  3350. num_format = V_008F14_IMG_FMASK_8_2_1;
  3351. break;
  3352. case FMASK(2,2):
  3353. num_format = V_008F14_IMG_FMASK_8_2_2;
  3354. break;
  3355. case FMASK(4,1):
  3356. num_format = V_008F14_IMG_FMASK_8_4_1;
  3357. break;
  3358. case FMASK(4,2):
  3359. num_format = V_008F14_IMG_FMASK_8_4_2;
  3360. break;
  3361. case FMASK(4,4):
  3362. num_format = V_008F14_IMG_FMASK_8_4_4;
  3363. break;
  3364. case FMASK(8,1):
  3365. num_format = V_008F14_IMG_FMASK_8_8_1;
  3366. break;
  3367. case FMASK(8,2):
  3368. num_format = V_008F14_IMG_FMASK_16_8_2;
  3369. break;
  3370. case FMASK(8,4):
  3371. num_format = V_008F14_IMG_FMASK_32_8_4;
  3372. break;
  3373. case FMASK(8,8):
  3374. num_format = V_008F14_IMG_FMASK_32_8_8;
  3375. break;
  3376. case FMASK(16,1):
  3377. num_format = V_008F14_IMG_FMASK_16_16_1;
  3378. break;
  3379. case FMASK(16,2):
  3380. num_format = V_008F14_IMG_FMASK_32_16_2;
  3381. break;
  3382. case FMASK(16,4):
  3383. num_format = V_008F14_IMG_FMASK_64_16_4;
  3384. break;
  3385. case FMASK(16,8):
  3386. num_format = V_008F14_IMG_FMASK_64_16_8;
  3387. break;
  3388. default:
  3389. unreachable("invalid nr_samples");
  3390. }
  3391. } else {
  3392. switch (FMASK(res->nr_samples, tex->num_color_samples)) {
  3393. case FMASK(2,1):
  3394. data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F1;
  3395. break;
  3396. case FMASK(2,2):
  3397. data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2;
  3398. break;
  3399. case FMASK(4,1):
  3400. data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F1;
  3401. break;
  3402. case FMASK(4,2):
  3403. data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F2;
  3404. break;
  3405. case FMASK(4,4):
  3406. data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4;
  3407. break;
  3408. case FMASK(8,1):
  3409. data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S8_F1;
  3410. break;
  3411. case FMASK(8,2):
  3412. data_format = V_008F14_IMG_DATA_FORMAT_FMASK16_S8_F2;
  3413. break;
  3414. case FMASK(8,4):
  3415. data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F4;
  3416. break;
  3417. case FMASK(8,8):
  3418. data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8;
  3419. break;
  3420. case FMASK(16,1):
  3421. data_format = V_008F14_IMG_DATA_FORMAT_FMASK16_S16_F1;
  3422. break;
  3423. case FMASK(16,2):
  3424. data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S16_F2;
  3425. break;
  3426. case FMASK(16,4):
  3427. data_format = V_008F14_IMG_DATA_FORMAT_FMASK64_S16_F4;
  3428. break;
  3429. case FMASK(16,8):
  3430. data_format = V_008F14_IMG_DATA_FORMAT_FMASK64_S16_F8;
  3431. break;
  3432. default:
  3433. unreachable("invalid nr_samples");
  3434. }
  3435. num_format = V_008F14_IMG_NUM_FORMAT_UINT;
  3436. }
  3437. #undef FMASK
  3438. fmask_state[0] = (va >> 8) | tex->surface.fmask_tile_swizzle;
  3439. fmask_state[1] = S_008F14_BASE_ADDRESS_HI(va >> 40) |
  3440. S_008F14_DATA_FORMAT_GFX6(data_format) |
  3441. S_008F14_NUM_FORMAT_GFX6(num_format);
  3442. fmask_state[2] = S_008F18_WIDTH(width - 1) |
  3443. S_008F18_HEIGHT(height - 1);
  3444. fmask_state[3] = S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X) |
  3445. S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X) |
  3446. S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X) |
  3447. S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X) |
  3448. S_008F1C_TYPE(si_tex_dim(screen, tex, target, 0));
  3449. fmask_state[4] = 0;
  3450. fmask_state[5] = S_008F24_BASE_ARRAY(first_layer);
  3451. fmask_state[6] = 0;
  3452. fmask_state[7] = 0;
  3453. if (screen->info.chip_class >= GFX9) {
  3454. fmask_state[3] |= S_008F1C_SW_MODE(tex->surface.u.gfx9.fmask.swizzle_mode);
  3455. fmask_state[4] |= S_008F20_DEPTH(last_layer) |
  3456. S_008F20_PITCH_GFX9(tex->surface.u.gfx9.fmask.epitch);
  3457. fmask_state[5] |= S_008F24_META_PIPE_ALIGNED(tex->surface.u.gfx9.cmask.pipe_aligned) |
  3458. S_008F24_META_RB_ALIGNED(tex->surface.u.gfx9.cmask.rb_aligned);
  3459. } else {
  3460. fmask_state[3] |= S_008F1C_TILING_INDEX(tex->surface.u.legacy.fmask.tiling_index);
  3461. fmask_state[4] |= S_008F20_DEPTH(depth - 1) |
  3462. S_008F20_PITCH_GFX6(tex->surface.u.legacy.fmask.pitch_in_pixels - 1);
  3463. fmask_state[5] |= S_008F24_LAST_ARRAY(last_layer);
  3464. }
  3465. }
  3466. }
  3467. /**
  3468. * Create a sampler view.
  3469. *
  3470. * @param ctx context
  3471. * @param texture texture
  3472. * @param state sampler view template
  3473. * @param width0 width0 override (for compressed textures as int)
  3474. * @param height0 height0 override (for compressed textures as int)
  3475. * @param force_level set the base address to the level (for compressed textures)
  3476. */
  3477. struct pipe_sampler_view *
  3478. si_create_sampler_view_custom(struct pipe_context *ctx,
  3479. struct pipe_resource *texture,
  3480. const struct pipe_sampler_view *state,
  3481. unsigned width0, unsigned height0,
  3482. unsigned force_level)
  3483. {
  3484. struct si_context *sctx = (struct si_context*)ctx;
  3485. struct si_sampler_view *view = CALLOC_STRUCT(si_sampler_view);
  3486. struct r600_texture *tmp = (struct r600_texture*)texture;
  3487. unsigned base_level, first_level, last_level;
  3488. unsigned char state_swizzle[4];
  3489. unsigned height, depth, width;
  3490. unsigned last_layer = state->u.tex.last_layer;
  3491. enum pipe_format pipe_format;
  3492. const struct legacy_surf_level *surflevel;
  3493. if (!view)
  3494. return NULL;
  3495. /* initialize base object */
  3496. view->base = *state;
  3497. view->base.texture = NULL;
  3498. view->base.reference.count = 1;
  3499. view->base.context = ctx;
  3500. assert(texture);
  3501. pipe_resource_reference(&view->base.texture, texture);
  3502. if (state->format == PIPE_FORMAT_X24S8_UINT ||
  3503. state->format == PIPE_FORMAT_S8X24_UINT ||
  3504. state->format == PIPE_FORMAT_X32_S8X24_UINT ||
  3505. state->format == PIPE_FORMAT_S8_UINT)
  3506. view->is_stencil_sampler = true;
  3507. /* Buffer resource. */
  3508. if (texture->target == PIPE_BUFFER) {
  3509. si_make_buffer_descriptor(sctx->screen,
  3510. r600_resource(texture),
  3511. state->format,
  3512. state->u.buf.offset,
  3513. state->u.buf.size,
  3514. view->state);
  3515. return &view->base;
  3516. }
  3517. state_swizzle[0] = state->swizzle_r;
  3518. state_swizzle[1] = state->swizzle_g;
  3519. state_swizzle[2] = state->swizzle_b;
  3520. state_swizzle[3] = state->swizzle_a;
  3521. base_level = 0;
  3522. first_level = state->u.tex.first_level;
  3523. last_level = state->u.tex.last_level;
  3524. width = width0;
  3525. height = height0;
  3526. depth = texture->depth0;
  3527. if (sctx->chip_class <= VI && force_level) {
  3528. assert(force_level == first_level &&
  3529. force_level == last_level);
  3530. base_level = force_level;
  3531. first_level = 0;
  3532. last_level = 0;
  3533. width = u_minify(width, force_level);
  3534. height = u_minify(height, force_level);
  3535. depth = u_minify(depth, force_level);
  3536. }
  3537. /* This is not needed if state trackers set last_layer correctly. */
  3538. if (state->target == PIPE_TEXTURE_1D ||
  3539. state->target == PIPE_TEXTURE_2D ||
  3540. state->target == PIPE_TEXTURE_RECT ||
  3541. state->target == PIPE_TEXTURE_CUBE)
  3542. last_layer = state->u.tex.first_layer;
  3543. /* Texturing with separate depth and stencil. */
  3544. pipe_format = state->format;
  3545. /* Depth/stencil texturing sometimes needs separate texture. */
  3546. if (tmp->is_depth && !si_can_sample_zs(tmp, view->is_stencil_sampler)) {
  3547. if (!tmp->flushed_depth_texture &&
  3548. !si_init_flushed_depth_texture(ctx, texture, NULL)) {
  3549. pipe_resource_reference(&view->base.texture, NULL);
  3550. FREE(view);
  3551. return NULL;
  3552. }
  3553. assert(tmp->flushed_depth_texture);
  3554. /* Override format for the case where the flushed texture
  3555. * contains only Z or only S.
  3556. */
  3557. if (tmp->flushed_depth_texture->buffer.b.b.format != tmp->buffer.b.b.format)
  3558. pipe_format = tmp->flushed_depth_texture->buffer.b.b.format;
  3559. tmp = tmp->flushed_depth_texture;
  3560. }
  3561. surflevel = tmp->surface.u.legacy.level;
  3562. if (tmp->db_compatible) {
  3563. if (!view->is_stencil_sampler)
  3564. pipe_format = tmp->db_render_format;
  3565. switch (pipe_format) {
  3566. case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
  3567. pipe_format = PIPE_FORMAT_Z32_FLOAT;
  3568. break;
  3569. case PIPE_FORMAT_X8Z24_UNORM:
  3570. case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  3571. /* Z24 is always stored like this for DB
  3572. * compatibility.
  3573. */
  3574. pipe_format = PIPE_FORMAT_Z24X8_UNORM;
  3575. break;
  3576. case PIPE_FORMAT_X24S8_UINT:
  3577. case PIPE_FORMAT_S8X24_UINT:
  3578. case PIPE_FORMAT_X32_S8X24_UINT:
  3579. pipe_format = PIPE_FORMAT_S8_UINT;
  3580. surflevel = tmp->surface.u.legacy.stencil_level;
  3581. break;
  3582. default:;
  3583. }
  3584. }
  3585. view->dcc_incompatible =
  3586. vi_dcc_formats_are_incompatible(texture,
  3587. state->u.tex.first_level,
  3588. state->format);
  3589. si_make_texture_descriptor(sctx->screen, tmp, true,
  3590. state->target, pipe_format, state_swizzle,
  3591. first_level, last_level,
  3592. state->u.tex.first_layer, last_layer,
  3593. width, height, depth,
  3594. view->state, view->fmask_state);
  3595. unsigned num_format = G_008F14_NUM_FORMAT_GFX6(view->state[1]);
  3596. view->is_integer =
  3597. num_format == V_008F14_IMG_NUM_FORMAT_USCALED ||
  3598. num_format == V_008F14_IMG_NUM_FORMAT_SSCALED ||
  3599. num_format == V_008F14_IMG_NUM_FORMAT_UINT ||
  3600. num_format == V_008F14_IMG_NUM_FORMAT_SINT;
  3601. view->base_level_info = &surflevel[base_level];
  3602. view->base_level = base_level;
  3603. view->block_width = util_format_get_blockwidth(pipe_format);
  3604. return &view->base;
  3605. }
  3606. static struct pipe_sampler_view *
  3607. si_create_sampler_view(struct pipe_context *ctx,
  3608. struct pipe_resource *texture,
  3609. const struct pipe_sampler_view *state)
  3610. {
  3611. return si_create_sampler_view_custom(ctx, texture, state,
  3612. texture ? texture->width0 : 0,
  3613. texture ? texture->height0 : 0, 0);
  3614. }
  3615. static void si_sampler_view_destroy(struct pipe_context *ctx,
  3616. struct pipe_sampler_view *state)
  3617. {
  3618. struct si_sampler_view *view = (struct si_sampler_view *)state;
  3619. pipe_resource_reference(&state->texture, NULL);
  3620. FREE(view);
  3621. }
  3622. static bool wrap_mode_uses_border_color(unsigned wrap, bool linear_filter)
  3623. {
  3624. return wrap == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
  3625. wrap == PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER ||
  3626. (linear_filter &&
  3627. (wrap == PIPE_TEX_WRAP_CLAMP ||
  3628. wrap == PIPE_TEX_WRAP_MIRROR_CLAMP));
  3629. }
  3630. static uint32_t si_translate_border_color(struct si_context *sctx,
  3631. const struct pipe_sampler_state *state,
  3632. const union pipe_color_union *color,
  3633. bool is_integer)
  3634. {
  3635. bool linear_filter = state->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
  3636. state->mag_img_filter != PIPE_TEX_FILTER_NEAREST;
  3637. if (!wrap_mode_uses_border_color(state->wrap_s, linear_filter) &&
  3638. !wrap_mode_uses_border_color(state->wrap_t, linear_filter) &&
  3639. !wrap_mode_uses_border_color(state->wrap_r, linear_filter))
  3640. return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK);
  3641. #define simple_border_types(elt) \
  3642. do { \
  3643. if (color->elt[0] == 0 && color->elt[1] == 0 && \
  3644. color->elt[2] == 0 && color->elt[3] == 0) \
  3645. return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK); \
  3646. if (color->elt[0] == 0 && color->elt[1] == 0 && \
  3647. color->elt[2] == 0 && color->elt[3] == 1) \
  3648. return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK); \
  3649. if (color->elt[0] == 1 && color->elt[1] == 1 && \
  3650. color->elt[2] == 1 && color->elt[3] == 1) \
  3651. return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE); \
  3652. } while (false)
  3653. if (is_integer)
  3654. simple_border_types(ui);
  3655. else
  3656. simple_border_types(f);
  3657. #undef simple_border_types
  3658. int i;
  3659. /* Check if the border has been uploaded already. */
  3660. for (i = 0; i < sctx->border_color_count; i++)
  3661. if (memcmp(&sctx->border_color_table[i], color,
  3662. sizeof(*color)) == 0)
  3663. break;
  3664. if (i >= SI_MAX_BORDER_COLORS) {
  3665. /* Getting 4096 unique border colors is very unlikely. */
  3666. fprintf(stderr, "radeonsi: The border color table is full. "
  3667. "Any new border colors will be just black. "
  3668. "Please file a bug.\n");
  3669. return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK);
  3670. }
  3671. if (i == sctx->border_color_count) {
  3672. /* Upload a new border color. */
  3673. memcpy(&sctx->border_color_table[i], color,
  3674. sizeof(*color));
  3675. util_memcpy_cpu_to_le32(&sctx->border_color_map[i],
  3676. color, sizeof(*color));
  3677. sctx->border_color_count++;
  3678. }
  3679. return S_008F3C_BORDER_COLOR_PTR(i) |
  3680. S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_REGISTER);
  3681. }
  3682. static inline int S_FIXED(float value, unsigned frac_bits)
  3683. {
  3684. return value * (1 << frac_bits);
  3685. }
  3686. static inline unsigned si_tex_filter(unsigned filter, unsigned max_aniso)
  3687. {
  3688. if (filter == PIPE_TEX_FILTER_LINEAR)
  3689. return max_aniso > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR
  3690. : V_008F38_SQ_TEX_XY_FILTER_BILINEAR;
  3691. else
  3692. return max_aniso > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT
  3693. : V_008F38_SQ_TEX_XY_FILTER_POINT;
  3694. }
  3695. static inline unsigned si_tex_aniso_filter(unsigned filter)
  3696. {
  3697. if (filter < 2)
  3698. return 0;
  3699. if (filter < 4)
  3700. return 1;
  3701. if (filter < 8)
  3702. return 2;
  3703. if (filter < 16)
  3704. return 3;
  3705. return 4;
  3706. }
  3707. static void *si_create_sampler_state(struct pipe_context *ctx,
  3708. const struct pipe_sampler_state *state)
  3709. {
  3710. struct si_context *sctx = (struct si_context *)ctx;
  3711. struct si_screen *sscreen = sctx->screen;
  3712. struct si_sampler_state *rstate = CALLOC_STRUCT(si_sampler_state);
  3713. unsigned max_aniso = sscreen->force_aniso >= 0 ? sscreen->force_aniso
  3714. : state->max_anisotropy;
  3715. unsigned max_aniso_ratio = si_tex_aniso_filter(max_aniso);
  3716. union pipe_color_union clamped_border_color;
  3717. if (!rstate) {
  3718. return NULL;
  3719. }
  3720. #ifdef DEBUG
  3721. rstate->magic = SI_SAMPLER_STATE_MAGIC;
  3722. #endif
  3723. rstate->val[0] = (S_008F30_CLAMP_X(si_tex_wrap(state->wrap_s)) |
  3724. S_008F30_CLAMP_Y(si_tex_wrap(state->wrap_t)) |
  3725. S_008F30_CLAMP_Z(si_tex_wrap(state->wrap_r)) |
  3726. S_008F30_MAX_ANISO_RATIO(max_aniso_ratio) |
  3727. S_008F30_DEPTH_COMPARE_FUNC(si_tex_compare(state->compare_func)) |
  3728. S_008F30_FORCE_UNNORMALIZED(!state->normalized_coords) |
  3729. S_008F30_ANISO_THRESHOLD(max_aniso_ratio >> 1) |
  3730. S_008F30_ANISO_BIAS(max_aniso_ratio) |
  3731. S_008F30_DISABLE_CUBE_WRAP(!state->seamless_cube_map) |
  3732. S_008F30_COMPAT_MODE(sctx->chip_class >= VI));
  3733. rstate->val[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(state->min_lod, 0, 15), 8)) |
  3734. S_008F34_MAX_LOD(S_FIXED(CLAMP(state->max_lod, 0, 15), 8)) |
  3735. S_008F34_PERF_MIP(max_aniso_ratio ? max_aniso_ratio + 6 : 0));
  3736. rstate->val[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(state->lod_bias, -16, 16), 8)) |
  3737. S_008F38_XY_MAG_FILTER(si_tex_filter(state->mag_img_filter, max_aniso)) |
  3738. S_008F38_XY_MIN_FILTER(si_tex_filter(state->min_img_filter, max_aniso)) |
  3739. S_008F38_MIP_FILTER(si_tex_mipfilter(state->min_mip_filter)) |
  3740. S_008F38_MIP_POINT_PRECLAMP(0) |
  3741. S_008F38_DISABLE_LSB_CEIL(sctx->chip_class <= VI) |
  3742. S_008F38_FILTER_PREC_FIX(1) |
  3743. S_008F38_ANISO_OVERRIDE(sctx->chip_class >= VI));
  3744. rstate->val[3] = si_translate_border_color(sctx, state, &state->border_color, false);
  3745. /* Create sampler resource for integer textures. */
  3746. memcpy(rstate->integer_val, rstate->val, sizeof(rstate->val));
  3747. rstate->integer_val[3] = si_translate_border_color(sctx, state, &state->border_color, true);
  3748. /* Create sampler resource for upgraded depth textures. */
  3749. memcpy(rstate->upgraded_depth_val, rstate->val, sizeof(rstate->val));
  3750. for (unsigned i = 0; i < 4; ++i) {
  3751. /* Use channel 0 on purpose, so that we can use OPAQUE_WHITE
  3752. * when the border color is 1.0. */
  3753. clamped_border_color.f[i] = CLAMP(state->border_color.f[0], 0, 1);
  3754. }
  3755. if (memcmp(&state->border_color, &clamped_border_color, sizeof(clamped_border_color)) == 0)
  3756. rstate->upgraded_depth_val[3] |= S_008F3C_UPGRADED_DEPTH(1);
  3757. else
  3758. rstate->upgraded_depth_val[3] =
  3759. si_translate_border_color(sctx, state, &clamped_border_color, false) |
  3760. S_008F3C_UPGRADED_DEPTH(1);
  3761. return rstate;
  3762. }
  3763. static void si_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
  3764. {
  3765. struct si_context *sctx = (struct si_context *)ctx;
  3766. if (sctx->sample_mask == (uint16_t)sample_mask)
  3767. return;
  3768. sctx->sample_mask = sample_mask;
  3769. si_mark_atom_dirty(sctx, &sctx->atoms.s.sample_mask);
  3770. }
  3771. static void si_emit_sample_mask(struct si_context *sctx)
  3772. {
  3773. struct radeon_winsys_cs *cs = sctx->gfx_cs;
  3774. unsigned mask = sctx->sample_mask;
  3775. /* Needed for line and polygon smoothing as well as for the Polaris
  3776. * small primitive filter. We expect the state tracker to take care of
  3777. * this for us.
  3778. */
  3779. assert(mask == 0xffff || sctx->framebuffer.nr_samples > 1 ||
  3780. (mask & 1 && sctx->blitter->running));
  3781. radeon_set_context_reg_seq(cs, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0, 2);
  3782. radeon_emit(cs, mask | (mask << 16));
  3783. radeon_emit(cs, mask | (mask << 16));
  3784. }
  3785. static void si_delete_sampler_state(struct pipe_context *ctx, void *state)
  3786. {
  3787. #ifdef DEBUG
  3788. struct si_sampler_state *s = state;
  3789. assert(s->magic == SI_SAMPLER_STATE_MAGIC);
  3790. s->magic = 0;
  3791. #endif
  3792. free(state);
  3793. }
  3794. /*
  3795. * Vertex elements & buffers
  3796. */
  3797. static void *si_create_vertex_elements(struct pipe_context *ctx,
  3798. unsigned count,
  3799. const struct pipe_vertex_element *elements)
  3800. {
  3801. struct si_screen *sscreen = (struct si_screen*)ctx->screen;
  3802. struct si_vertex_elements *v = CALLOC_STRUCT(si_vertex_elements);
  3803. bool used[SI_NUM_VERTEX_BUFFERS] = {};
  3804. int i;
  3805. assert(count <= SI_MAX_ATTRIBS);
  3806. if (!v)
  3807. return NULL;
  3808. v->count = count;
  3809. v->desc_list_byte_size = align(count * 16, SI_CPDMA_ALIGNMENT);
  3810. for (i = 0; i < count; ++i) {
  3811. const struct util_format_description *desc;
  3812. const struct util_format_channel_description *channel;
  3813. unsigned data_format, num_format;
  3814. int first_non_void;
  3815. unsigned vbo_index = elements[i].vertex_buffer_index;
  3816. unsigned char swizzle[4];
  3817. if (vbo_index >= SI_NUM_VERTEX_BUFFERS) {
  3818. FREE(v);
  3819. return NULL;
  3820. }
  3821. if (elements[i].instance_divisor) {
  3822. v->uses_instance_divisors = true;
  3823. v->instance_divisors[i] = elements[i].instance_divisor;
  3824. if (v->instance_divisors[i] == 1)
  3825. v->instance_divisor_is_one |= 1u << i;
  3826. else
  3827. v->instance_divisor_is_fetched |= 1u << i;
  3828. }
  3829. if (!used[vbo_index]) {
  3830. v->first_vb_use_mask |= 1 << i;
  3831. used[vbo_index] = true;
  3832. }
  3833. desc = util_format_description(elements[i].src_format);
  3834. first_non_void = util_format_get_first_non_void_channel(elements[i].src_format);
  3835. data_format = si_translate_buffer_dataformat(ctx->screen, desc, first_non_void);
  3836. num_format = si_translate_buffer_numformat(ctx->screen, desc, first_non_void);
  3837. channel = first_non_void >= 0 ? &desc->channel[first_non_void] : NULL;
  3838. memcpy(swizzle, desc->swizzle, sizeof(swizzle));
  3839. v->format_size[i] = desc->block.bits / 8;
  3840. v->src_offset[i] = elements[i].src_offset;
  3841. v->vertex_buffer_index[i] = vbo_index;
  3842. /* The hardware always treats the 2-bit alpha channel as
  3843. * unsigned, so a shader workaround is needed. The affected
  3844. * chips are VI and older except Stoney (GFX8.1).
  3845. */
  3846. if (data_format == V_008F0C_BUF_DATA_FORMAT_2_10_10_10 &&
  3847. sscreen->info.chip_class <= VI &&
  3848. sscreen->info.family != CHIP_STONEY) {
  3849. if (num_format == V_008F0C_BUF_NUM_FORMAT_SNORM) {
  3850. v->fix_fetch[i] = SI_FIX_FETCH_A2_SNORM;
  3851. } else if (num_format == V_008F0C_BUF_NUM_FORMAT_SSCALED) {
  3852. v->fix_fetch[i] = SI_FIX_FETCH_A2_SSCALED;
  3853. } else if (num_format == V_008F0C_BUF_NUM_FORMAT_SINT) {
  3854. /* This isn't actually used in OpenGL. */
  3855. v->fix_fetch[i] = SI_FIX_FETCH_A2_SINT;
  3856. }
  3857. } else if (channel && channel->type == UTIL_FORMAT_TYPE_FIXED) {
  3858. if (desc->swizzle[3] == PIPE_SWIZZLE_1)
  3859. v->fix_fetch[i] = SI_FIX_FETCH_RGBX_32_FIXED;
  3860. else
  3861. v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_FIXED;
  3862. } else if (channel && channel->size == 32 && !channel->pure_integer) {
  3863. if (channel->type == UTIL_FORMAT_TYPE_SIGNED) {
  3864. if (channel->normalized) {
  3865. if (desc->swizzle[3] == PIPE_SWIZZLE_1)
  3866. v->fix_fetch[i] = SI_FIX_FETCH_RGBX_32_SNORM;
  3867. else
  3868. v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_SNORM;
  3869. } else {
  3870. v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_SSCALED;
  3871. }
  3872. } else if (channel->type == UTIL_FORMAT_TYPE_UNSIGNED) {
  3873. if (channel->normalized) {
  3874. if (desc->swizzle[3] == PIPE_SWIZZLE_1)
  3875. v->fix_fetch[i] = SI_FIX_FETCH_RGBX_32_UNORM;
  3876. else
  3877. v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_UNORM;
  3878. } else {
  3879. v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_USCALED;
  3880. }
  3881. }
  3882. } else if (channel && channel->size == 64 &&
  3883. channel->type == UTIL_FORMAT_TYPE_FLOAT) {
  3884. switch (desc->nr_channels) {
  3885. case 1:
  3886. case 2:
  3887. v->fix_fetch[i] = SI_FIX_FETCH_RG_64_FLOAT;
  3888. swizzle[0] = PIPE_SWIZZLE_X;
  3889. swizzle[1] = PIPE_SWIZZLE_Y;
  3890. swizzle[2] = desc->nr_channels == 2 ? PIPE_SWIZZLE_Z : PIPE_SWIZZLE_0;
  3891. swizzle[3] = desc->nr_channels == 2 ? PIPE_SWIZZLE_W : PIPE_SWIZZLE_0;
  3892. break;
  3893. case 3:
  3894. v->fix_fetch[i] = SI_FIX_FETCH_RGB_64_FLOAT;
  3895. swizzle[0] = PIPE_SWIZZLE_X; /* 3 loads */
  3896. swizzle[1] = PIPE_SWIZZLE_Y;
  3897. swizzle[2] = PIPE_SWIZZLE_0;
  3898. swizzle[3] = PIPE_SWIZZLE_0;
  3899. break;
  3900. case 4:
  3901. v->fix_fetch[i] = SI_FIX_FETCH_RGBA_64_FLOAT;
  3902. swizzle[0] = PIPE_SWIZZLE_X; /* 2 loads */
  3903. swizzle[1] = PIPE_SWIZZLE_Y;
  3904. swizzle[2] = PIPE_SWIZZLE_Z;
  3905. swizzle[3] = PIPE_SWIZZLE_W;
  3906. break;
  3907. default:
  3908. assert(0);
  3909. }
  3910. } else if (channel && desc->nr_channels == 3) {
  3911. assert(desc->swizzle[0] == PIPE_SWIZZLE_X);
  3912. if (channel->size == 8) {
  3913. if (channel->pure_integer)
  3914. v->fix_fetch[i] = SI_FIX_FETCH_RGB_8_INT;
  3915. else
  3916. v->fix_fetch[i] = SI_FIX_FETCH_RGB_8;
  3917. } else if (channel->size == 16) {
  3918. if (channel->pure_integer)
  3919. v->fix_fetch[i] = SI_FIX_FETCH_RGB_16_INT;
  3920. else
  3921. v->fix_fetch[i] = SI_FIX_FETCH_RGB_16;
  3922. }
  3923. }
  3924. v->rsrc_word3[i] = S_008F0C_DST_SEL_X(si_map_swizzle(swizzle[0])) |
  3925. S_008F0C_DST_SEL_Y(si_map_swizzle(swizzle[1])) |
  3926. S_008F0C_DST_SEL_Z(si_map_swizzle(swizzle[2])) |
  3927. S_008F0C_DST_SEL_W(si_map_swizzle(swizzle[3])) |
  3928. S_008F0C_NUM_FORMAT(num_format) |
  3929. S_008F0C_DATA_FORMAT(data_format);
  3930. }
  3931. return v;
  3932. }
  3933. static void si_bind_vertex_elements(struct pipe_context *ctx, void *state)
  3934. {
  3935. struct si_context *sctx = (struct si_context *)ctx;
  3936. struct si_vertex_elements *old = sctx->vertex_elements;
  3937. struct si_vertex_elements *v = (struct si_vertex_elements*)state;
  3938. sctx->vertex_elements = v;
  3939. sctx->vertex_buffers_dirty = true;
  3940. if (v &&
  3941. (!old ||
  3942. old->count != v->count ||
  3943. old->uses_instance_divisors != v->uses_instance_divisors ||
  3944. v->uses_instance_divisors || /* we don't check which divisors changed */
  3945. memcmp(old->fix_fetch, v->fix_fetch, sizeof(v->fix_fetch[0]) * v->count)))
  3946. sctx->do_update_shaders = true;
  3947. if (v && v->instance_divisor_is_fetched) {
  3948. struct pipe_constant_buffer cb;
  3949. cb.buffer = NULL;
  3950. cb.user_buffer = v->instance_divisors;
  3951. cb.buffer_offset = 0;
  3952. cb.buffer_size = sizeof(uint32_t) * v->count;
  3953. si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS, &cb);
  3954. }
  3955. }
  3956. static void si_delete_vertex_element(struct pipe_context *ctx, void *state)
  3957. {
  3958. struct si_context *sctx = (struct si_context *)ctx;
  3959. if (sctx->vertex_elements == state)
  3960. sctx->vertex_elements = NULL;
  3961. FREE(state);
  3962. }
  3963. static void si_set_vertex_buffers(struct pipe_context *ctx,
  3964. unsigned start_slot, unsigned count,
  3965. const struct pipe_vertex_buffer *buffers)
  3966. {
  3967. struct si_context *sctx = (struct si_context *)ctx;
  3968. struct pipe_vertex_buffer *dst = sctx->vertex_buffer + start_slot;
  3969. int i;
  3970. assert(start_slot + count <= ARRAY_SIZE(sctx->vertex_buffer));
  3971. if (buffers) {
  3972. for (i = 0; i < count; i++) {
  3973. const struct pipe_vertex_buffer *src = buffers + i;
  3974. struct pipe_vertex_buffer *dsti = dst + i;
  3975. struct pipe_resource *buf = src->buffer.resource;
  3976. pipe_resource_reference(&dsti->buffer.resource, buf);
  3977. dsti->buffer_offset = src->buffer_offset;
  3978. dsti->stride = src->stride;
  3979. si_context_add_resource_size(sctx, buf);
  3980. if (buf)
  3981. r600_resource(buf)->bind_history |= PIPE_BIND_VERTEX_BUFFER;
  3982. }
  3983. } else {
  3984. for (i = 0; i < count; i++) {
  3985. pipe_resource_reference(&dst[i].buffer.resource, NULL);
  3986. }
  3987. }
  3988. sctx->vertex_buffers_dirty = true;
  3989. }
  3990. /*
  3991. * Misc
  3992. */
  3993. static void si_set_tess_state(struct pipe_context *ctx,
  3994. const float default_outer_level[4],
  3995. const float default_inner_level[2])
  3996. {
  3997. struct si_context *sctx = (struct si_context *)ctx;
  3998. struct pipe_constant_buffer cb;
  3999. float array[8];
  4000. memcpy(array, default_outer_level, sizeof(float) * 4);
  4001. memcpy(array+4, default_inner_level, sizeof(float) * 2);
  4002. cb.buffer = NULL;
  4003. cb.user_buffer = NULL;
  4004. cb.buffer_size = sizeof(array);
  4005. si_upload_const_buffer(sctx, (struct r600_resource**)&cb.buffer,
  4006. (void*)array, sizeof(array),
  4007. &cb.buffer_offset);
  4008. si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS, &cb);
  4009. pipe_resource_reference(&cb.buffer, NULL);
  4010. }
  4011. static void si_texture_barrier(struct pipe_context *ctx, unsigned flags)
  4012. {
  4013. struct si_context *sctx = (struct si_context *)ctx;
  4014. si_update_fb_dirtiness_after_rendering(sctx);
  4015. /* Multisample surfaces are flushed in si_decompress_textures. */
  4016. if (sctx->framebuffer.uncompressed_cb_mask)
  4017. si_make_CB_shader_coherent(sctx, sctx->framebuffer.nr_samples,
  4018. sctx->framebuffer.CB_has_shader_readable_metadata);
  4019. }
  4020. /* This only ensures coherency for shader image/buffer stores. */
  4021. static void si_memory_barrier(struct pipe_context *ctx, unsigned flags)
  4022. {
  4023. struct si_context *sctx = (struct si_context *)ctx;
  4024. /* Subsequent commands must wait for all shader invocations to
  4025. * complete. */
  4026. sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
  4027. SI_CONTEXT_CS_PARTIAL_FLUSH;
  4028. if (flags & PIPE_BARRIER_CONSTANT_BUFFER)
  4029. sctx->flags |= SI_CONTEXT_INV_SMEM_L1 |
  4030. SI_CONTEXT_INV_VMEM_L1;
  4031. if (flags & (PIPE_BARRIER_VERTEX_BUFFER |
  4032. PIPE_BARRIER_SHADER_BUFFER |
  4033. PIPE_BARRIER_TEXTURE |
  4034. PIPE_BARRIER_IMAGE |
  4035. PIPE_BARRIER_STREAMOUT_BUFFER |
  4036. PIPE_BARRIER_GLOBAL_BUFFER)) {
  4037. /* As far as I can tell, L1 contents are written back to L2
  4038. * automatically at end of shader, but the contents of other
  4039. * L1 caches might still be stale. */
  4040. sctx->flags |= SI_CONTEXT_INV_VMEM_L1;
  4041. }
  4042. if (flags & PIPE_BARRIER_INDEX_BUFFER) {
  4043. /* Indices are read through TC L2 since VI.
  4044. * L1 isn't used.
  4045. */
  4046. if (sctx->screen->info.chip_class <= CIK)
  4047. sctx->flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
  4048. }
  4049. /* MSAA color, any depth and any stencil are flushed in
  4050. * si_decompress_textures when needed.
  4051. */
  4052. if (flags & PIPE_BARRIER_FRAMEBUFFER &&
  4053. sctx->framebuffer.uncompressed_cb_mask) {
  4054. sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;
  4055. if (sctx->chip_class <= VI)
  4056. sctx->flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
  4057. }
  4058. /* Indirect buffers use TC L2 on GFX9, but not older hw. */
  4059. if (sctx->screen->info.chip_class <= VI &&
  4060. flags & PIPE_BARRIER_INDIRECT_BUFFER)
  4061. sctx->flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
  4062. }
  4063. static void *si_create_blend_custom(struct si_context *sctx, unsigned mode)
  4064. {
  4065. struct pipe_blend_state blend;
  4066. memset(&blend, 0, sizeof(blend));
  4067. blend.independent_blend_enable = true;
  4068. blend.rt[0].colormask = 0xf;
  4069. return si_create_blend_state_mode(&sctx->b, &blend, mode);
  4070. }
  4071. static void si_init_config(struct si_context *sctx);
  4072. void si_init_state_functions(struct si_context *sctx)
  4073. {
  4074. sctx->atoms.s.framebuffer.emit = si_emit_framebuffer_state;
  4075. sctx->atoms.s.msaa_sample_locs.emit = si_emit_msaa_sample_locs;
  4076. sctx->atoms.s.db_render_state.emit = si_emit_db_render_state;
  4077. sctx->atoms.s.dpbb_state.emit = si_emit_dpbb_state;
  4078. sctx->atoms.s.msaa_config.emit = si_emit_msaa_config;
  4079. sctx->atoms.s.sample_mask.emit = si_emit_sample_mask;
  4080. sctx->atoms.s.cb_render_state.emit = si_emit_cb_render_state;
  4081. sctx->atoms.s.blend_color.emit = si_emit_blend_color;
  4082. sctx->atoms.s.clip_regs.emit = si_emit_clip_regs;
  4083. sctx->atoms.s.clip_state.emit = si_emit_clip_state;
  4084. sctx->atoms.s.stencil_ref.emit = si_emit_stencil_ref;
  4085. sctx->b.create_blend_state = si_create_blend_state;
  4086. sctx->b.bind_blend_state = si_bind_blend_state;
  4087. sctx->b.delete_blend_state = si_delete_blend_state;
  4088. sctx->b.set_blend_color = si_set_blend_color;
  4089. sctx->b.create_rasterizer_state = si_create_rs_state;
  4090. sctx->b.bind_rasterizer_state = si_bind_rs_state;
  4091. sctx->b.delete_rasterizer_state = si_delete_rs_state;
  4092. sctx->b.create_depth_stencil_alpha_state = si_create_dsa_state;
  4093. sctx->b.bind_depth_stencil_alpha_state = si_bind_dsa_state;
  4094. sctx->b.delete_depth_stencil_alpha_state = si_delete_dsa_state;
  4095. sctx->custom_dsa_flush = si_create_db_flush_dsa(sctx);
  4096. sctx->custom_blend_resolve = si_create_blend_custom(sctx, V_028808_CB_RESOLVE);
  4097. sctx->custom_blend_fmask_decompress = si_create_blend_custom(sctx, V_028808_CB_FMASK_DECOMPRESS);
  4098. sctx->custom_blend_eliminate_fastclear = si_create_blend_custom(sctx, V_028808_CB_ELIMINATE_FAST_CLEAR);
  4099. sctx->custom_blend_dcc_decompress = si_create_blend_custom(sctx, V_028808_CB_DCC_DECOMPRESS);
  4100. sctx->b.set_clip_state = si_set_clip_state;
  4101. sctx->b.set_stencil_ref = si_set_stencil_ref;
  4102. sctx->b.set_framebuffer_state = si_set_framebuffer_state;
  4103. sctx->b.create_sampler_state = si_create_sampler_state;
  4104. sctx->b.delete_sampler_state = si_delete_sampler_state;
  4105. sctx->b.create_sampler_view = si_create_sampler_view;
  4106. sctx->b.sampler_view_destroy = si_sampler_view_destroy;
  4107. sctx->b.set_sample_mask = si_set_sample_mask;
  4108. sctx->b.create_vertex_elements_state = si_create_vertex_elements;
  4109. sctx->b.bind_vertex_elements_state = si_bind_vertex_elements;
  4110. sctx->b.delete_vertex_elements_state = si_delete_vertex_element;
  4111. sctx->b.set_vertex_buffers = si_set_vertex_buffers;
  4112. sctx->b.texture_barrier = si_texture_barrier;
  4113. sctx->b.memory_barrier = si_memory_barrier;
  4114. sctx->b.set_min_samples = si_set_min_samples;
  4115. sctx->b.set_tess_state = si_set_tess_state;
  4116. sctx->b.set_active_query_state = si_set_active_query_state;
  4117. sctx->b.draw_vbo = si_draw_vbo;
  4118. si_init_config(sctx);
  4119. }
  4120. void si_init_screen_state_functions(struct si_screen *sscreen)
  4121. {
  4122. sscreen->b.is_format_supported = si_is_format_supported;
  4123. }
  4124. static void si_set_grbm_gfx_index(struct si_context *sctx,
  4125. struct si_pm4_state *pm4, unsigned value)
  4126. {
  4127. unsigned reg = sctx->chip_class >= CIK ? R_030800_GRBM_GFX_INDEX :
  4128. R_00802C_GRBM_GFX_INDEX;
  4129. si_pm4_set_reg(pm4, reg, value);
  4130. }
  4131. static void si_set_grbm_gfx_index_se(struct si_context *sctx,
  4132. struct si_pm4_state *pm4, unsigned se)
  4133. {
  4134. assert(se == ~0 || se < sctx->screen->info.max_se);
  4135. si_set_grbm_gfx_index(sctx, pm4,
  4136. (se == ~0 ? S_030800_SE_BROADCAST_WRITES(1) :
  4137. S_030800_SE_INDEX(se)) |
  4138. S_030800_SH_BROADCAST_WRITES(1) |
  4139. S_030800_INSTANCE_BROADCAST_WRITES(1));
  4140. }
  4141. static void
  4142. si_write_harvested_raster_configs(struct si_context *sctx,
  4143. struct si_pm4_state *pm4,
  4144. unsigned raster_config,
  4145. unsigned raster_config_1)
  4146. {
  4147. unsigned num_se = MAX2(sctx->screen->info.max_se, 1);
  4148. unsigned raster_config_se[4];
  4149. unsigned se;
  4150. ac_get_harvested_configs(&sctx->screen->info,
  4151. raster_config,
  4152. &raster_config_1,
  4153. raster_config_se);
  4154. for (se = 0; se < num_se; se++) {
  4155. si_set_grbm_gfx_index_se(sctx, pm4, se);
  4156. si_pm4_set_reg(pm4, R_028350_PA_SC_RASTER_CONFIG, raster_config_se[se]);
  4157. }
  4158. si_set_grbm_gfx_index(sctx, pm4, ~0);
  4159. if (sctx->chip_class >= CIK) {
  4160. si_pm4_set_reg(pm4, R_028354_PA_SC_RASTER_CONFIG_1, raster_config_1);
  4161. }
  4162. }
  4163. static void si_set_raster_config(struct si_context *sctx, struct si_pm4_state *pm4)
  4164. {
  4165. unsigned num_rb = MIN2(sctx->screen->info.num_render_backends, 16);
  4166. unsigned rb_mask = sctx->screen->info.enabled_rb_mask;
  4167. unsigned raster_config, raster_config_1;
  4168. ac_get_raster_config(&sctx->screen->info,
  4169. &raster_config,
  4170. &raster_config_1);
  4171. if (!rb_mask || util_bitcount(rb_mask) >= num_rb) {
  4172. /* Always use the default config when all backends are enabled
  4173. * (or when we failed to determine the enabled backends).
  4174. */
  4175. si_pm4_set_reg(pm4, R_028350_PA_SC_RASTER_CONFIG,
  4176. raster_config);
  4177. if (sctx->chip_class >= CIK)
  4178. si_pm4_set_reg(pm4, R_028354_PA_SC_RASTER_CONFIG_1,
  4179. raster_config_1);
  4180. } else {
  4181. si_write_harvested_raster_configs(sctx, pm4, raster_config, raster_config_1);
  4182. }
  4183. }
  4184. static void si_init_config(struct si_context *sctx)
  4185. {
  4186. struct si_screen *sscreen = sctx->screen;
  4187. uint64_t border_color_va = sctx->border_color_buffer->gpu_address;
  4188. bool has_clear_state = sscreen->has_clear_state;
  4189. struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
  4190. /* Only SI can disable CLEAR_STATE for now. */
  4191. assert(has_clear_state || sscreen->info.chip_class == SI);
  4192. if (!pm4)
  4193. return;
  4194. si_pm4_cmd_begin(pm4, PKT3_CONTEXT_CONTROL);
  4195. si_pm4_cmd_add(pm4, CONTEXT_CONTROL_LOAD_ENABLE(1));
  4196. si_pm4_cmd_add(pm4, CONTEXT_CONTROL_SHADOW_ENABLE(1));
  4197. si_pm4_cmd_end(pm4, false);
  4198. if (has_clear_state) {
  4199. si_pm4_cmd_begin(pm4, PKT3_CLEAR_STATE);
  4200. si_pm4_cmd_add(pm4, 0);
  4201. si_pm4_cmd_end(pm4, false);
  4202. }
  4203. if (sctx->chip_class <= VI)
  4204. si_set_raster_config(sctx, pm4);
  4205. si_pm4_set_reg(pm4, R_028A18_VGT_HOS_MAX_TESS_LEVEL, fui(64));
  4206. if (!has_clear_state)
  4207. si_pm4_set_reg(pm4, R_028A1C_VGT_HOS_MIN_TESS_LEVEL, fui(0));
  4208. /* FIXME calculate these values somehow ??? */
  4209. if (sctx->chip_class <= VI) {
  4210. si_pm4_set_reg(pm4, R_028A54_VGT_GS_PER_ES, SI_GS_PER_ES);
  4211. si_pm4_set_reg(pm4, R_028A58_VGT_ES_PER_GS, 0x40);
  4212. }
  4213. if (!has_clear_state) {
  4214. si_pm4_set_reg(pm4, R_028A5C_VGT_GS_PER_VS, 0x2);
  4215. si_pm4_set_reg(pm4, R_028A8C_VGT_PRIMITIVEID_RESET, 0x0);
  4216. si_pm4_set_reg(pm4, R_028B98_VGT_STRMOUT_BUFFER_CONFIG, 0x0);
  4217. }
  4218. si_pm4_set_reg(pm4, R_028AA0_VGT_INSTANCE_STEP_RATE_0, 1);
  4219. if (!has_clear_state)
  4220. si_pm4_set_reg(pm4, R_028AB8_VGT_VTX_CNT_EN, 0x0);
  4221. if (sctx->chip_class < CIK)
  4222. si_pm4_set_reg(pm4, R_008A14_PA_CL_ENHANCE, S_008A14_NUM_CLIP_SEQ(3) |
  4223. S_008A14_CLIP_VTX_REORDER_ENA(1));
  4224. if (!has_clear_state)
  4225. si_pm4_set_reg(pm4, R_02882C_PA_SU_PRIM_FILTER_CNTL, 0);
  4226. /* CLEAR_STATE doesn't clear these correctly on certain generations.
  4227. * I don't know why. Deduced by trial and error.
  4228. */
  4229. if (sctx->chip_class <= CIK) {
  4230. si_pm4_set_reg(pm4, R_028B28_VGT_STRMOUT_DRAW_OPAQUE_OFFSET, 0);
  4231. si_pm4_set_reg(pm4, R_028204_PA_SC_WINDOW_SCISSOR_TL, S_028204_WINDOW_OFFSET_DISABLE(1));
  4232. si_pm4_set_reg(pm4, R_028240_PA_SC_GENERIC_SCISSOR_TL, S_028240_WINDOW_OFFSET_DISABLE(1));
  4233. si_pm4_set_reg(pm4, R_028244_PA_SC_GENERIC_SCISSOR_BR,
  4234. S_028244_BR_X(16384) | S_028244_BR_Y(16384));
  4235. si_pm4_set_reg(pm4, R_028030_PA_SC_SCREEN_SCISSOR_TL, 0);
  4236. si_pm4_set_reg(pm4, R_028034_PA_SC_SCREEN_SCISSOR_BR,
  4237. S_028034_BR_X(16384) | S_028034_BR_Y(16384));
  4238. }
  4239. if (!has_clear_state) {
  4240. si_pm4_set_reg(pm4, R_02820C_PA_SC_CLIPRECT_RULE, 0xFFFF);
  4241. si_pm4_set_reg(pm4, R_028230_PA_SC_EDGERULE,
  4242. S_028230_ER_TRI(0xA) |
  4243. S_028230_ER_POINT(0xA) |
  4244. S_028230_ER_RECT(0xA) |
  4245. /* Required by DX10_DIAMOND_TEST_ENA: */
  4246. S_028230_ER_LINE_LR(0x1A) |
  4247. S_028230_ER_LINE_RL(0x26) |
  4248. S_028230_ER_LINE_TB(0xA) |
  4249. S_028230_ER_LINE_BT(0xA));
  4250. /* PA_SU_HARDWARE_SCREEN_OFFSET must be 0 due to hw bug on SI */
  4251. si_pm4_set_reg(pm4, R_028234_PA_SU_HARDWARE_SCREEN_OFFSET, 0);
  4252. si_pm4_set_reg(pm4, R_028820_PA_CL_NANINF_CNTL, 0);
  4253. si_pm4_set_reg(pm4, R_028AC0_DB_SRESULTS_COMPARE_STATE0, 0x0);
  4254. si_pm4_set_reg(pm4, R_028AC4_DB_SRESULTS_COMPARE_STATE1, 0x0);
  4255. si_pm4_set_reg(pm4, R_028AC8_DB_PRELOAD_CONTROL, 0x0);
  4256. si_pm4_set_reg(pm4, R_02800C_DB_RENDER_OVERRIDE, 0);
  4257. }
  4258. if (sctx->chip_class >= GFX9) {
  4259. si_pm4_set_reg(pm4, R_030920_VGT_MAX_VTX_INDX, ~0);
  4260. si_pm4_set_reg(pm4, R_030924_VGT_MIN_VTX_INDX, 0);
  4261. si_pm4_set_reg(pm4, R_030928_VGT_INDX_OFFSET, 0);
  4262. } else {
  4263. /* These registers, when written, also overwrite the CLEAR_STATE
  4264. * context, so we can't rely on CLEAR_STATE setting them.
  4265. * It would be an issue if there was another UMD changing them.
  4266. */
  4267. si_pm4_set_reg(pm4, R_028400_VGT_MAX_VTX_INDX, ~0);
  4268. si_pm4_set_reg(pm4, R_028404_VGT_MIN_VTX_INDX, 0);
  4269. si_pm4_set_reg(pm4, R_028408_VGT_INDX_OFFSET, 0);
  4270. }
  4271. if (sctx->chip_class >= CIK) {
  4272. if (sctx->chip_class >= GFX9) {
  4273. si_pm4_set_reg(pm4, R_00B41C_SPI_SHADER_PGM_RSRC3_HS,
  4274. S_00B41C_CU_EN(0xffff) | S_00B41C_WAVE_LIMIT(0x3F));
  4275. } else {
  4276. si_pm4_set_reg(pm4, R_00B51C_SPI_SHADER_PGM_RSRC3_LS,
  4277. S_00B51C_CU_EN(0xffff) | S_00B51C_WAVE_LIMIT(0x3F));
  4278. si_pm4_set_reg(pm4, R_00B41C_SPI_SHADER_PGM_RSRC3_HS,
  4279. S_00B41C_WAVE_LIMIT(0x3F));
  4280. si_pm4_set_reg(pm4, R_00B31C_SPI_SHADER_PGM_RSRC3_ES,
  4281. S_00B31C_CU_EN(0xffff) | S_00B31C_WAVE_LIMIT(0x3F));
  4282. /* If this is 0, Bonaire can hang even if GS isn't being used.
  4283. * Other chips are unaffected. These are suboptimal values,
  4284. * but we don't use on-chip GS.
  4285. */
  4286. si_pm4_set_reg(pm4, R_028A44_VGT_GS_ONCHIP_CNTL,
  4287. S_028A44_ES_VERTS_PER_SUBGRP(64) |
  4288. S_028A44_GS_PRIMS_PER_SUBGRP(4));
  4289. }
  4290. si_pm4_set_reg(pm4, R_00B21C_SPI_SHADER_PGM_RSRC3_GS,
  4291. S_00B21C_CU_EN(0xffff) | S_00B21C_WAVE_LIMIT(0x3F));
  4292. /* Compute LATE_ALLOC_VS.LIMIT. */
  4293. unsigned num_cu_per_sh = sscreen->info.num_good_compute_units /
  4294. (sscreen->info.max_se *
  4295. sscreen->info.max_sh_per_se);
  4296. unsigned late_alloc_limit; /* The limit is per SH. */
  4297. if (sctx->family == CHIP_KABINI) {
  4298. late_alloc_limit = 0; /* Potential hang on Kabini. */
  4299. } else if (num_cu_per_sh <= 4) {
  4300. /* Too few available compute units per SH. Disallowing
  4301. * VS to run on one CU could hurt us more than late VS
  4302. * allocation would help.
  4303. *
  4304. * 2 is the highest safe number that allows us to keep
  4305. * all CUs enabled.
  4306. */
  4307. late_alloc_limit = 2;
  4308. } else {
  4309. /* This is a good initial value, allowing 1 late_alloc
  4310. * wave per SIMD on num_cu - 2.
  4311. */
  4312. late_alloc_limit = (num_cu_per_sh - 2) * 4;
  4313. /* The limit is 0-based, so 0 means 1. */
  4314. assert(late_alloc_limit > 0 && late_alloc_limit <= 64);
  4315. late_alloc_limit -= 1;
  4316. }
  4317. /* VS can't execute on one CU if the limit is > 2. */
  4318. si_pm4_set_reg(pm4, R_00B118_SPI_SHADER_PGM_RSRC3_VS,
  4319. S_00B118_CU_EN(late_alloc_limit > 2 ? 0xfffe : 0xffff) |
  4320. S_00B118_WAVE_LIMIT(0x3F));
  4321. si_pm4_set_reg(pm4, R_00B11C_SPI_SHADER_LATE_ALLOC_VS,
  4322. S_00B11C_LIMIT(late_alloc_limit));
  4323. si_pm4_set_reg(pm4, R_00B01C_SPI_SHADER_PGM_RSRC3_PS,
  4324. S_00B01C_CU_EN(0xffff) | S_00B01C_WAVE_LIMIT(0x3F));
  4325. }
  4326. if (sctx->chip_class >= VI) {
  4327. unsigned vgt_tess_distribution;
  4328. vgt_tess_distribution =
  4329. S_028B50_ACCUM_ISOLINE(32) |
  4330. S_028B50_ACCUM_TRI(11) |
  4331. S_028B50_ACCUM_QUAD(11) |
  4332. S_028B50_DONUT_SPLIT(16);
  4333. /* Testing with Unigine Heaven extreme tesselation yielded best results
  4334. * with TRAP_SPLIT = 3.
  4335. */
  4336. if (sctx->family == CHIP_FIJI ||
  4337. sctx->family >= CHIP_POLARIS10)
  4338. vgt_tess_distribution |= S_028B50_TRAP_SPLIT(3);
  4339. si_pm4_set_reg(pm4, R_028B50_VGT_TESS_DISTRIBUTION, vgt_tess_distribution);
  4340. } else if (!has_clear_state) {
  4341. si_pm4_set_reg(pm4, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 14);
  4342. si_pm4_set_reg(pm4, R_028C5C_VGT_OUT_DEALLOC_CNTL, 16);
  4343. }
  4344. si_pm4_set_reg(pm4, R_028080_TA_BC_BASE_ADDR, border_color_va >> 8);
  4345. if (sctx->chip_class >= CIK) {
  4346. si_pm4_set_reg(pm4, R_028084_TA_BC_BASE_ADDR_HI,
  4347. S_028084_ADDRESS(border_color_va >> 40));
  4348. }
  4349. si_pm4_add_bo(pm4, sctx->border_color_buffer, RADEON_USAGE_READ,
  4350. RADEON_PRIO_BORDER_COLORS);
  4351. if (sctx->chip_class >= GFX9) {
  4352. unsigned num_se = sscreen->info.max_se;
  4353. unsigned pc_lines = 0;
  4354. switch (sctx->family) {
  4355. case CHIP_VEGA10:
  4356. case CHIP_VEGA12:
  4357. pc_lines = 4096;
  4358. break;
  4359. case CHIP_RAVEN:
  4360. pc_lines = 1024;
  4361. break;
  4362. default:
  4363. assert(0);
  4364. }
  4365. si_pm4_set_reg(pm4, R_028C48_PA_SC_BINNER_CNTL_1,
  4366. S_028C48_MAX_ALLOC_COUNT(MIN2(128, pc_lines / (4 * num_se))) |
  4367. S_028C48_MAX_PRIM_PER_BATCH(1023));
  4368. si_pm4_set_reg(pm4, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL,
  4369. S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1));
  4370. si_pm4_set_reg(pm4, R_030968_VGT_INSTANCE_BASE_ID, 0);
  4371. }
  4372. si_pm4_upload_indirect_buffer(sctx, pm4);
  4373. sctx->init_config = pm4;
  4374. }