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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309
  1. /*
  2. * Copyright © 2016 Red Hat.
  3. * Copyright © 2016 Bas Nieuwenhuizen
  4. *
  5. * based in part on anv driver which is:
  6. * Copyright © 2015 Intel Corporation
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the next
  16. * paragraph) shall be included in all copies or substantial portions of the
  17. * Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  25. * IN THE SOFTWARE.
  26. */
  27. #include <stdbool.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include "radv_debug.h"
  32. #include "radv_private.h"
  33. #include "radv_shader.h"
  34. #include "radv_cs.h"
  35. #include "util/disk_cache.h"
  36. #include "util/strtod.h"
  37. #include "vk_util.h"
  38. #include <xf86drm.h>
  39. #include <amdgpu.h>
  40. #include <amdgpu_drm.h>
  41. #include "winsys/amdgpu/radv_amdgpu_winsys_public.h"
  42. #include "ac_llvm_util.h"
  43. #include "vk_format.h"
  44. #include "sid.h"
  45. #include "git_sha1.h"
  46. #include "gfx9d.h"
  47. #include "util/build_id.h"
  48. #include "util/debug.h"
  49. #include "util/mesa-sha1.h"
  50. #include "compiler/glsl_types.h"
  51. static int
  52. radv_device_get_cache_uuid(enum radeon_family family, void *uuid)
  53. {
  54. struct mesa_sha1 ctx;
  55. unsigned char sha1[20];
  56. unsigned ptr_size = sizeof(void*);
  57. memset(uuid, 0, VK_UUID_SIZE);
  58. _mesa_sha1_init(&ctx);
  59. if (!disk_cache_get_function_identifier(radv_device_get_cache_uuid, &ctx) ||
  60. !disk_cache_get_function_identifier(LLVMInitializeAMDGPUTargetInfo, &ctx))
  61. return -1;
  62. _mesa_sha1_update(&ctx, &family, sizeof(family));
  63. _mesa_sha1_update(&ctx, &ptr_size, sizeof(ptr_size));
  64. _mesa_sha1_final(&ctx, sha1);
  65. memcpy(uuid, sha1, VK_UUID_SIZE);
  66. return 0;
  67. }
  68. static void
  69. radv_get_driver_uuid(void *uuid)
  70. {
  71. ac_compute_driver_uuid(uuid, VK_UUID_SIZE);
  72. }
  73. static void
  74. radv_get_device_uuid(struct radeon_info *info, void *uuid)
  75. {
  76. ac_compute_device_uuid(info, uuid, VK_UUID_SIZE);
  77. }
  78. static void
  79. radv_get_device_name(enum radeon_family family, char *name, size_t name_len)
  80. {
  81. const char *chip_string;
  82. switch (family) {
  83. case CHIP_TAHITI: chip_string = "AMD RADV TAHITI"; break;
  84. case CHIP_PITCAIRN: chip_string = "AMD RADV PITCAIRN"; break;
  85. case CHIP_VERDE: chip_string = "AMD RADV CAPE VERDE"; break;
  86. case CHIP_OLAND: chip_string = "AMD RADV OLAND"; break;
  87. case CHIP_HAINAN: chip_string = "AMD RADV HAINAN"; break;
  88. case CHIP_BONAIRE: chip_string = "AMD RADV BONAIRE"; break;
  89. case CHIP_KAVERI: chip_string = "AMD RADV KAVERI"; break;
  90. case CHIP_KABINI: chip_string = "AMD RADV KABINI"; break;
  91. case CHIP_HAWAII: chip_string = "AMD RADV HAWAII"; break;
  92. case CHIP_MULLINS: chip_string = "AMD RADV MULLINS"; break;
  93. case CHIP_TONGA: chip_string = "AMD RADV TONGA"; break;
  94. case CHIP_ICELAND: chip_string = "AMD RADV ICELAND"; break;
  95. case CHIP_CARRIZO: chip_string = "AMD RADV CARRIZO"; break;
  96. case CHIP_FIJI: chip_string = "AMD RADV FIJI"; break;
  97. case CHIP_POLARIS10: chip_string = "AMD RADV POLARIS10"; break;
  98. case CHIP_POLARIS11: chip_string = "AMD RADV POLARIS11"; break;
  99. case CHIP_POLARIS12: chip_string = "AMD RADV POLARIS12"; break;
  100. case CHIP_STONEY: chip_string = "AMD RADV STONEY"; break;
  101. case CHIP_VEGAM: chip_string = "AMD RADV VEGA M"; break;
  102. case CHIP_VEGA10: chip_string = "AMD RADV VEGA10"; break;
  103. case CHIP_VEGA12: chip_string = "AMD RADV VEGA12"; break;
  104. case CHIP_RAVEN: chip_string = "AMD RADV RAVEN"; break;
  105. case CHIP_RAVEN2: chip_string = "AMD RADV RAVEN2"; break;
  106. default: chip_string = "AMD RADV unknown"; break;
  107. }
  108. snprintf(name, name_len, "%s (LLVM " MESA_LLVM_VERSION_STRING ")", chip_string);
  109. }
  110. static uint64_t
  111. radv_get_visible_vram_size(struct radv_physical_device *device)
  112. {
  113. return MIN2(device->rad_info.vram_size, device->rad_info.vram_vis_size);
  114. }
  115. static uint64_t
  116. radv_get_vram_size(struct radv_physical_device *device)
  117. {
  118. return device->rad_info.vram_size - radv_get_visible_vram_size(device);
  119. }
  120. static void
  121. radv_physical_device_init_mem_types(struct radv_physical_device *device)
  122. {
  123. STATIC_ASSERT(RADV_MEM_HEAP_COUNT <= VK_MAX_MEMORY_HEAPS);
  124. uint64_t visible_vram_size = radv_get_visible_vram_size(device);
  125. uint64_t vram_size = radv_get_vram_size(device);
  126. int vram_index = -1, visible_vram_index = -1, gart_index = -1;
  127. device->memory_properties.memoryHeapCount = 0;
  128. if (vram_size > 0) {
  129. vram_index = device->memory_properties.memoryHeapCount++;
  130. device->memory_properties.memoryHeaps[vram_index] = (VkMemoryHeap) {
  131. .size = vram_size,
  132. .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
  133. };
  134. }
  135. if (visible_vram_size) {
  136. visible_vram_index = device->memory_properties.memoryHeapCount++;
  137. device->memory_properties.memoryHeaps[visible_vram_index] = (VkMemoryHeap) {
  138. .size = visible_vram_size,
  139. .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
  140. };
  141. }
  142. if (device->rad_info.gart_size > 0) {
  143. gart_index = device->memory_properties.memoryHeapCount++;
  144. device->memory_properties.memoryHeaps[gart_index] = (VkMemoryHeap) {
  145. .size = device->rad_info.gart_size,
  146. .flags = device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
  147. };
  148. }
  149. STATIC_ASSERT(RADV_MEM_TYPE_COUNT <= VK_MAX_MEMORY_TYPES);
  150. unsigned type_count = 0;
  151. if (vram_index >= 0) {
  152. device->mem_type_indices[type_count] = RADV_MEM_TYPE_VRAM;
  153. device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
  154. .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
  155. .heapIndex = vram_index,
  156. };
  157. }
  158. if (gart_index >= 0) {
  159. device->mem_type_indices[type_count] = RADV_MEM_TYPE_GTT_WRITE_COMBINE;
  160. device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
  161. .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  162. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
  163. (device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
  164. .heapIndex = gart_index,
  165. };
  166. }
  167. if (visible_vram_index >= 0) {
  168. device->mem_type_indices[type_count] = RADV_MEM_TYPE_VRAM_CPU_ACCESS;
  169. device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
  170. .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
  171. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  172. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
  173. .heapIndex = visible_vram_index,
  174. };
  175. }
  176. if (gart_index >= 0) {
  177. device->mem_type_indices[type_count] = RADV_MEM_TYPE_GTT_CACHED;
  178. device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
  179. .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  180. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
  181. VK_MEMORY_PROPERTY_HOST_CACHED_BIT |
  182. (device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
  183. .heapIndex = gart_index,
  184. };
  185. }
  186. device->memory_properties.memoryTypeCount = type_count;
  187. }
  188. static void
  189. radv_handle_env_var_force_family(struct radv_physical_device *device)
  190. {
  191. const char *family = getenv("RADV_FORCE_FAMILY");
  192. unsigned i;
  193. if (!family)
  194. return;
  195. for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
  196. if (!strcmp(family, ac_get_llvm_processor_name(i))) {
  197. /* Override family and chip_class. */
  198. device->rad_info.family = i;
  199. if (i >= CHIP_VEGA10)
  200. device->rad_info.chip_class = GFX9;
  201. else if (i >= CHIP_TONGA)
  202. device->rad_info.chip_class = VI;
  203. else if (i >= CHIP_BONAIRE)
  204. device->rad_info.chip_class = CIK;
  205. else
  206. device->rad_info.chip_class = SI;
  207. return;
  208. }
  209. }
  210. fprintf(stderr, "radv: Unknown family: %s\n", family);
  211. exit(1);
  212. }
  213. static VkResult
  214. radv_physical_device_init(struct radv_physical_device *device,
  215. struct radv_instance *instance,
  216. drmDevicePtr drm_device)
  217. {
  218. const char *path = drm_device->nodes[DRM_NODE_RENDER];
  219. VkResult result;
  220. drmVersionPtr version;
  221. int fd;
  222. int master_fd = -1;
  223. fd = open(path, O_RDWR | O_CLOEXEC);
  224. if (fd < 0) {
  225. if (instance->debug_flags & RADV_DEBUG_STARTUP)
  226. radv_logi("Could not open device '%s'", path);
  227. return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
  228. }
  229. version = drmGetVersion(fd);
  230. if (!version) {
  231. close(fd);
  232. if (instance->debug_flags & RADV_DEBUG_STARTUP)
  233. radv_logi("Could not get the kernel driver version for device '%s'", path);
  234. return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
  235. "failed to get version %s: %m", path);
  236. }
  237. if (strcmp(version->name, "amdgpu")) {
  238. drmFreeVersion(version);
  239. close(fd);
  240. if (instance->debug_flags & RADV_DEBUG_STARTUP)
  241. radv_logi("Device '%s' is not using the amdgpu kernel driver.", path);
  242. return VK_ERROR_INCOMPATIBLE_DRIVER;
  243. }
  244. drmFreeVersion(version);
  245. if (instance->debug_flags & RADV_DEBUG_STARTUP)
  246. radv_logi("Found compatible device '%s'.", path);
  247. device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
  248. device->instance = instance;
  249. device->ws = radv_amdgpu_winsys_create(fd, instance->debug_flags,
  250. instance->perftest_flags);
  251. if (!device->ws) {
  252. result = vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
  253. goto fail;
  254. }
  255. if (instance->enabled_extensions.KHR_display) {
  256. master_fd = open(drm_device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
  257. if (master_fd >= 0) {
  258. uint32_t accel_working = 0;
  259. struct drm_amdgpu_info request = {
  260. .return_pointer = (uintptr_t)&accel_working,
  261. .return_size = sizeof(accel_working),
  262. .query = AMDGPU_INFO_ACCEL_WORKING
  263. };
  264. if (drmCommandWrite(master_fd, DRM_AMDGPU_INFO, &request, sizeof (struct drm_amdgpu_info)) < 0 || !accel_working) {
  265. close(master_fd);
  266. master_fd = -1;
  267. }
  268. }
  269. }
  270. device->master_fd = master_fd;
  271. device->local_fd = fd;
  272. device->ws->query_info(device->ws, &device->rad_info);
  273. radv_handle_env_var_force_family(device);
  274. radv_get_device_name(device->rad_info.family, device->name, sizeof(device->name));
  275. if (radv_device_get_cache_uuid(device->rad_info.family, device->cache_uuid)) {
  276. device->ws->destroy(device->ws);
  277. result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
  278. "cannot generate UUID");
  279. goto fail;
  280. }
  281. /* These flags affect shader compilation. */
  282. uint64_t shader_env_flags =
  283. (device->instance->perftest_flags & RADV_PERFTEST_SISCHED ? 0x1 : 0) |
  284. (device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH ? 0x2 : 0);
  285. /* The gpu id is already embedded in the uuid so we just pass "radv"
  286. * when creating the cache.
  287. */
  288. char buf[VK_UUID_SIZE * 2 + 1];
  289. disk_cache_format_hex_id(buf, device->cache_uuid, VK_UUID_SIZE * 2);
  290. device->disk_cache = disk_cache_create(device->name, buf, shader_env_flags);
  291. if (device->rad_info.chip_class < VI ||
  292. device->rad_info.chip_class > GFX9)
  293. fprintf(stderr, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
  294. radv_get_driver_uuid(&device->driver_uuid);
  295. radv_get_device_uuid(&device->rad_info, &device->device_uuid);
  296. if (device->rad_info.family == CHIP_STONEY ||
  297. device->rad_info.chip_class >= GFX9) {
  298. device->has_rbplus = true;
  299. device->rbplus_allowed = device->rad_info.family == CHIP_STONEY ||
  300. device->rad_info.family == CHIP_VEGA12 ||
  301. device->rad_info.family == CHIP_RAVEN ||
  302. device->rad_info.family == CHIP_RAVEN2;
  303. }
  304. /* The mere presence of CLEAR_STATE in the IB causes random GPU hangs
  305. * on SI.
  306. */
  307. device->has_clear_state = device->rad_info.chip_class >= CIK;
  308. device->cpdma_prefetch_writes_memory = device->rad_info.chip_class <= VI;
  309. /* Vega10/Raven need a special workaround for a hardware bug. */
  310. device->has_scissor_bug = device->rad_info.family == CHIP_VEGA10 ||
  311. device->rad_info.family == CHIP_RAVEN;
  312. /* Out-of-order primitive rasterization. */
  313. device->has_out_of_order_rast = device->rad_info.chip_class >= VI &&
  314. device->rad_info.max_se >= 2;
  315. device->out_of_order_rast_allowed = device->has_out_of_order_rast &&
  316. !(device->instance->debug_flags & RADV_DEBUG_NO_OUT_OF_ORDER);
  317. device->dcc_msaa_allowed =
  318. (device->instance->perftest_flags & RADV_PERFTEST_DCC_MSAA);
  319. /* TODO: Figure out how to use LOAD_CONTEXT_REG on SI/CIK. */
  320. device->has_load_ctx_reg_pkt = device->rad_info.chip_class >= GFX9 ||
  321. (device->rad_info.chip_class >= VI &&
  322. device->rad_info.me_fw_feature >= 41);
  323. radv_physical_device_init_mem_types(device);
  324. radv_fill_device_extension_table(device, &device->supported_extensions);
  325. device->bus_info = *drm_device->businfo.pci;
  326. if ((device->instance->debug_flags & RADV_DEBUG_INFO))
  327. ac_print_gpu_info(&device->rad_info);
  328. /* The WSI is structured as a layer on top of the driver, so this has
  329. * to be the last part of initialization (at least until we get other
  330. * semi-layers).
  331. */
  332. result = radv_init_wsi(device);
  333. if (result != VK_SUCCESS) {
  334. device->ws->destroy(device->ws);
  335. vk_error(instance, result);
  336. goto fail;
  337. }
  338. return VK_SUCCESS;
  339. fail:
  340. close(fd);
  341. if (master_fd != -1)
  342. close(master_fd);
  343. return result;
  344. }
  345. static void
  346. radv_physical_device_finish(struct radv_physical_device *device)
  347. {
  348. radv_finish_wsi(device);
  349. device->ws->destroy(device->ws);
  350. disk_cache_destroy(device->disk_cache);
  351. close(device->local_fd);
  352. if (device->master_fd != -1)
  353. close(device->master_fd);
  354. }
  355. static void *
  356. default_alloc_func(void *pUserData, size_t size, size_t align,
  357. VkSystemAllocationScope allocationScope)
  358. {
  359. return malloc(size);
  360. }
  361. static void *
  362. default_realloc_func(void *pUserData, void *pOriginal, size_t size,
  363. size_t align, VkSystemAllocationScope allocationScope)
  364. {
  365. return realloc(pOriginal, size);
  366. }
  367. static void
  368. default_free_func(void *pUserData, void *pMemory)
  369. {
  370. free(pMemory);
  371. }
  372. static const VkAllocationCallbacks default_alloc = {
  373. .pUserData = NULL,
  374. .pfnAllocation = default_alloc_func,
  375. .pfnReallocation = default_realloc_func,
  376. .pfnFree = default_free_func,
  377. };
  378. static const struct debug_control radv_debug_options[] = {
  379. {"nofastclears", RADV_DEBUG_NO_FAST_CLEARS},
  380. {"nodcc", RADV_DEBUG_NO_DCC},
  381. {"shaders", RADV_DEBUG_DUMP_SHADERS},
  382. {"nocache", RADV_DEBUG_NO_CACHE},
  383. {"shaderstats", RADV_DEBUG_DUMP_SHADER_STATS},
  384. {"nohiz", RADV_DEBUG_NO_HIZ},
  385. {"nocompute", RADV_DEBUG_NO_COMPUTE_QUEUE},
  386. {"unsafemath", RADV_DEBUG_UNSAFE_MATH},
  387. {"allbos", RADV_DEBUG_ALL_BOS},
  388. {"noibs", RADV_DEBUG_NO_IBS},
  389. {"spirv", RADV_DEBUG_DUMP_SPIRV},
  390. {"vmfaults", RADV_DEBUG_VM_FAULTS},
  391. {"zerovram", RADV_DEBUG_ZERO_VRAM},
  392. {"syncshaders", RADV_DEBUG_SYNC_SHADERS},
  393. {"nosisched", RADV_DEBUG_NO_SISCHED},
  394. {"preoptir", RADV_DEBUG_PREOPTIR},
  395. {"nodynamicbounds", RADV_DEBUG_NO_DYNAMIC_BOUNDS},
  396. {"nooutoforder", RADV_DEBUG_NO_OUT_OF_ORDER},
  397. {"info", RADV_DEBUG_INFO},
  398. {"errors", RADV_DEBUG_ERRORS},
  399. {"startup", RADV_DEBUG_STARTUP},
  400. {"checkir", RADV_DEBUG_CHECKIR},
  401. {"nothreadllvm", RADV_DEBUG_NOTHREADLLVM},
  402. {"nobinning", RADV_DEBUG_NOBINNING},
  403. {NULL, 0}
  404. };
  405. const char *
  406. radv_get_debug_option_name(int id)
  407. {
  408. assert(id < ARRAY_SIZE(radv_debug_options) - 1);
  409. return radv_debug_options[id].string;
  410. }
  411. static const struct debug_control radv_perftest_options[] = {
  412. {"nobatchchain", RADV_PERFTEST_NO_BATCHCHAIN},
  413. {"sisched", RADV_PERFTEST_SISCHED},
  414. {"localbos", RADV_PERFTEST_LOCAL_BOS},
  415. {"dccmsaa", RADV_PERFTEST_DCC_MSAA},
  416. {"bolist", RADV_PERFTEST_BO_LIST},
  417. {NULL, 0}
  418. };
  419. const char *
  420. radv_get_perftest_option_name(int id)
  421. {
  422. assert(id < ARRAY_SIZE(radv_perftest_options) - 1);
  423. return radv_perftest_options[id].string;
  424. }
  425. static void
  426. radv_handle_per_app_options(struct radv_instance *instance,
  427. const VkApplicationInfo *info)
  428. {
  429. const char *name = info ? info->pApplicationName : NULL;
  430. if (!name)
  431. return;
  432. if (!strcmp(name, "Talos - Linux - 32bit") ||
  433. !strcmp(name, "Talos - Linux - 64bit")) {
  434. if (!(instance->debug_flags & RADV_DEBUG_NO_SISCHED)) {
  435. /* Force enable LLVM sisched for Talos because it looks
  436. * safe and it gives few more FPS.
  437. */
  438. instance->perftest_flags |= RADV_PERFTEST_SISCHED;
  439. }
  440. } else if (!strcmp(name, "DOOM_VFR")) {
  441. /* Work around a Doom VFR game bug */
  442. instance->debug_flags |= RADV_DEBUG_NO_DYNAMIC_BOUNDS;
  443. }
  444. }
  445. static int radv_get_instance_extension_index(const char *name)
  446. {
  447. for (unsigned i = 0; i < RADV_INSTANCE_EXTENSION_COUNT; ++i) {
  448. if (strcmp(name, radv_instance_extensions[i].extensionName) == 0)
  449. return i;
  450. }
  451. return -1;
  452. }
  453. VkResult radv_CreateInstance(
  454. const VkInstanceCreateInfo* pCreateInfo,
  455. const VkAllocationCallbacks* pAllocator,
  456. VkInstance* pInstance)
  457. {
  458. struct radv_instance *instance;
  459. VkResult result;
  460. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
  461. uint32_t client_version;
  462. if (pCreateInfo->pApplicationInfo &&
  463. pCreateInfo->pApplicationInfo->apiVersion != 0) {
  464. client_version = pCreateInfo->pApplicationInfo->apiVersion;
  465. } else {
  466. client_version = VK_API_VERSION_1_0;
  467. }
  468. instance = vk_zalloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
  469. VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
  470. if (!instance)
  471. return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
  472. instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
  473. if (pAllocator)
  474. instance->alloc = *pAllocator;
  475. else
  476. instance->alloc = default_alloc;
  477. instance->apiVersion = client_version;
  478. instance->physicalDeviceCount = -1;
  479. instance->debug_flags = parse_debug_string(getenv("RADV_DEBUG"),
  480. radv_debug_options);
  481. instance->perftest_flags = parse_debug_string(getenv("RADV_PERFTEST"),
  482. radv_perftest_options);
  483. if (instance->debug_flags & RADV_DEBUG_STARTUP)
  484. radv_logi("Created an instance");
  485. for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
  486. const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
  487. int index = radv_get_instance_extension_index(ext_name);
  488. if (index < 0 || !radv_supported_instance_extensions.extensions[index]) {
  489. vk_free2(&default_alloc, pAllocator, instance);
  490. return vk_error(instance, VK_ERROR_EXTENSION_NOT_PRESENT);
  491. }
  492. instance->enabled_extensions.extensions[index] = true;
  493. }
  494. result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
  495. if (result != VK_SUCCESS) {
  496. vk_free2(&default_alloc, pAllocator, instance);
  497. return vk_error(instance, result);
  498. }
  499. _mesa_locale_init();
  500. glsl_type_singleton_init_or_ref();
  501. VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
  502. radv_handle_per_app_options(instance, pCreateInfo->pApplicationInfo);
  503. *pInstance = radv_instance_to_handle(instance);
  504. return VK_SUCCESS;
  505. }
  506. void radv_DestroyInstance(
  507. VkInstance _instance,
  508. const VkAllocationCallbacks* pAllocator)
  509. {
  510. RADV_FROM_HANDLE(radv_instance, instance, _instance);
  511. if (!instance)
  512. return;
  513. for (int i = 0; i < instance->physicalDeviceCount; ++i) {
  514. radv_physical_device_finish(instance->physicalDevices + i);
  515. }
  516. VG(VALGRIND_DESTROY_MEMPOOL(instance));
  517. glsl_type_singleton_decref();
  518. _mesa_locale_fini();
  519. vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
  520. vk_free(&instance->alloc, instance);
  521. }
  522. static VkResult
  523. radv_enumerate_devices(struct radv_instance *instance)
  524. {
  525. /* TODO: Check for more devices ? */
  526. drmDevicePtr devices[8];
  527. VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
  528. int max_devices;
  529. instance->physicalDeviceCount = 0;
  530. max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
  531. if (instance->debug_flags & RADV_DEBUG_STARTUP)
  532. radv_logi("Found %d drm nodes", max_devices);
  533. if (max_devices < 1)
  534. return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
  535. for (unsigned i = 0; i < (unsigned)max_devices; i++) {
  536. if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
  537. devices[i]->bustype == DRM_BUS_PCI &&
  538. devices[i]->deviceinfo.pci->vendor_id == ATI_VENDOR_ID) {
  539. result = radv_physical_device_init(instance->physicalDevices +
  540. instance->physicalDeviceCount,
  541. instance,
  542. devices[i]);
  543. if (result == VK_SUCCESS)
  544. ++instance->physicalDeviceCount;
  545. else if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
  546. break;
  547. }
  548. }
  549. drmFreeDevices(devices, max_devices);
  550. return result;
  551. }
  552. VkResult radv_EnumeratePhysicalDevices(
  553. VkInstance _instance,
  554. uint32_t* pPhysicalDeviceCount,
  555. VkPhysicalDevice* pPhysicalDevices)
  556. {
  557. RADV_FROM_HANDLE(radv_instance, instance, _instance);
  558. VkResult result;
  559. if (instance->physicalDeviceCount < 0) {
  560. result = radv_enumerate_devices(instance);
  561. if (result != VK_SUCCESS &&
  562. result != VK_ERROR_INCOMPATIBLE_DRIVER)
  563. return result;
  564. }
  565. if (!pPhysicalDevices) {
  566. *pPhysicalDeviceCount = instance->physicalDeviceCount;
  567. } else {
  568. *pPhysicalDeviceCount = MIN2(*pPhysicalDeviceCount, instance->physicalDeviceCount);
  569. for (unsigned i = 0; i < *pPhysicalDeviceCount; ++i)
  570. pPhysicalDevices[i] = radv_physical_device_to_handle(instance->physicalDevices + i);
  571. }
  572. return *pPhysicalDeviceCount < instance->physicalDeviceCount ? VK_INCOMPLETE
  573. : VK_SUCCESS;
  574. }
  575. VkResult radv_EnumeratePhysicalDeviceGroups(
  576. VkInstance _instance,
  577. uint32_t* pPhysicalDeviceGroupCount,
  578. VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties)
  579. {
  580. RADV_FROM_HANDLE(radv_instance, instance, _instance);
  581. VkResult result;
  582. if (instance->physicalDeviceCount < 0) {
  583. result = radv_enumerate_devices(instance);
  584. if (result != VK_SUCCESS &&
  585. result != VK_ERROR_INCOMPATIBLE_DRIVER)
  586. return result;
  587. }
  588. if (!pPhysicalDeviceGroupProperties) {
  589. *pPhysicalDeviceGroupCount = instance->physicalDeviceCount;
  590. } else {
  591. *pPhysicalDeviceGroupCount = MIN2(*pPhysicalDeviceGroupCount, instance->physicalDeviceCount);
  592. for (unsigned i = 0; i < *pPhysicalDeviceGroupCount; ++i) {
  593. pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
  594. pPhysicalDeviceGroupProperties[i].physicalDevices[0] = radv_physical_device_to_handle(instance->physicalDevices + i);
  595. pPhysicalDeviceGroupProperties[i].subsetAllocation = false;
  596. }
  597. }
  598. return *pPhysicalDeviceGroupCount < instance->physicalDeviceCount ? VK_INCOMPLETE
  599. : VK_SUCCESS;
  600. }
  601. void radv_GetPhysicalDeviceFeatures(
  602. VkPhysicalDevice physicalDevice,
  603. VkPhysicalDeviceFeatures* pFeatures)
  604. {
  605. RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
  606. memset(pFeatures, 0, sizeof(*pFeatures));
  607. *pFeatures = (VkPhysicalDeviceFeatures) {
  608. .robustBufferAccess = true,
  609. .fullDrawIndexUint32 = true,
  610. .imageCubeArray = true,
  611. .independentBlend = true,
  612. .geometryShader = true,
  613. .tessellationShader = true,
  614. .sampleRateShading = true,
  615. .dualSrcBlend = true,
  616. .logicOp = true,
  617. .multiDrawIndirect = true,
  618. .drawIndirectFirstInstance = true,
  619. .depthClamp = true,
  620. .depthBiasClamp = true,
  621. .fillModeNonSolid = true,
  622. .depthBounds = true,
  623. .wideLines = true,
  624. .largePoints = true,
  625. .alphaToOne = true,
  626. .multiViewport = true,
  627. .samplerAnisotropy = true,
  628. .textureCompressionETC2 = radv_device_supports_etc(pdevice),
  629. .textureCompressionASTC_LDR = false,
  630. .textureCompressionBC = true,
  631. .occlusionQueryPrecise = true,
  632. .pipelineStatisticsQuery = true,
  633. .vertexPipelineStoresAndAtomics = true,
  634. .fragmentStoresAndAtomics = true,
  635. .shaderTessellationAndGeometryPointSize = true,
  636. .shaderImageGatherExtended = true,
  637. .shaderStorageImageExtendedFormats = true,
  638. .shaderStorageImageMultisample = pdevice->rad_info.chip_class >= VI,
  639. .shaderUniformBufferArrayDynamicIndexing = true,
  640. .shaderSampledImageArrayDynamicIndexing = true,
  641. .shaderStorageBufferArrayDynamicIndexing = true,
  642. .shaderStorageImageArrayDynamicIndexing = true,
  643. .shaderStorageImageReadWithoutFormat = true,
  644. .shaderStorageImageWriteWithoutFormat = true,
  645. .shaderClipDistance = true,
  646. .shaderCullDistance = true,
  647. .shaderFloat64 = true,
  648. .shaderInt64 = true,
  649. .shaderInt16 = pdevice->rad_info.chip_class >= GFX9,
  650. .sparseBinding = true,
  651. .variableMultisampleRate = true,
  652. .inheritedQueries = true,
  653. };
  654. }
  655. void radv_GetPhysicalDeviceFeatures2(
  656. VkPhysicalDevice physicalDevice,
  657. VkPhysicalDeviceFeatures2 *pFeatures)
  658. {
  659. RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
  660. vk_foreach_struct(ext, pFeatures->pNext) {
  661. switch (ext->sType) {
  662. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES: {
  663. VkPhysicalDeviceVariablePointerFeatures *features = (void *)ext;
  664. features->variablePointersStorageBuffer = true;
  665. features->variablePointers = true;
  666. break;
  667. }
  668. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
  669. VkPhysicalDeviceMultiviewFeatures *features = (VkPhysicalDeviceMultiviewFeatures*)ext;
  670. features->multiview = true;
  671. features->multiviewGeometryShader = true;
  672. features->multiviewTessellationShader = true;
  673. break;
  674. }
  675. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES: {
  676. VkPhysicalDeviceShaderDrawParameterFeatures *features =
  677. (VkPhysicalDeviceShaderDrawParameterFeatures*)ext;
  678. features->shaderDrawParameters = true;
  679. break;
  680. }
  681. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
  682. VkPhysicalDeviceProtectedMemoryFeatures *features =
  683. (VkPhysicalDeviceProtectedMemoryFeatures*)ext;
  684. features->protectedMemory = false;
  685. break;
  686. }
  687. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
  688. VkPhysicalDevice16BitStorageFeatures *features =
  689. (VkPhysicalDevice16BitStorageFeatures*)ext;
  690. bool enabled = pdevice->rad_info.chip_class >= VI;
  691. features->storageBuffer16BitAccess = enabled;
  692. features->uniformAndStorageBuffer16BitAccess = enabled;
  693. features->storagePushConstant16 = enabled;
  694. features->storageInputOutput16 = enabled && HAVE_LLVM >= 0x900;
  695. break;
  696. }
  697. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
  698. VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
  699. (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)ext;
  700. features->samplerYcbcrConversion = false;
  701. break;
  702. }
  703. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT: {
  704. VkPhysicalDeviceDescriptorIndexingFeaturesEXT *features =
  705. (VkPhysicalDeviceDescriptorIndexingFeaturesEXT*)ext;
  706. features->shaderInputAttachmentArrayDynamicIndexing = true;
  707. features->shaderUniformTexelBufferArrayDynamicIndexing = true;
  708. features->shaderStorageTexelBufferArrayDynamicIndexing = true;
  709. features->shaderUniformBufferArrayNonUniformIndexing = true;
  710. features->shaderSampledImageArrayNonUniformIndexing = true;
  711. features->shaderStorageBufferArrayNonUniformIndexing = true;
  712. features->shaderStorageImageArrayNonUniformIndexing = true;
  713. features->shaderInputAttachmentArrayNonUniformIndexing = true;
  714. features->shaderUniformTexelBufferArrayNonUniformIndexing = true;
  715. features->shaderStorageTexelBufferArrayNonUniformIndexing = true;
  716. features->descriptorBindingUniformBufferUpdateAfterBind = true;
  717. features->descriptorBindingSampledImageUpdateAfterBind = true;
  718. features->descriptorBindingStorageImageUpdateAfterBind = true;
  719. features->descriptorBindingStorageBufferUpdateAfterBind = true;
  720. features->descriptorBindingUniformTexelBufferUpdateAfterBind = true;
  721. features->descriptorBindingStorageTexelBufferUpdateAfterBind = true;
  722. features->descriptorBindingUpdateUnusedWhilePending = true;
  723. features->descriptorBindingPartiallyBound = true;
  724. features->descriptorBindingVariableDescriptorCount = true;
  725. features->runtimeDescriptorArray = true;
  726. break;
  727. }
  728. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
  729. VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
  730. (VkPhysicalDeviceConditionalRenderingFeaturesEXT*)ext;
  731. features->conditionalRendering = true;
  732. features->inheritedConditionalRendering = false;
  733. break;
  734. }
  735. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
  736. VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
  737. (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
  738. features->vertexAttributeInstanceRateDivisor = VK_TRUE;
  739. features->vertexAttributeInstanceRateZeroDivisor = VK_TRUE;
  740. break;
  741. }
  742. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT: {
  743. VkPhysicalDeviceTransformFeedbackFeaturesEXT *features =
  744. (VkPhysicalDeviceTransformFeedbackFeaturesEXT*)ext;
  745. features->transformFeedback = true;
  746. features->geometryStreams = true;
  747. break;
  748. }
  749. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT: {
  750. VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *features =
  751. (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *)ext;
  752. features->scalarBlockLayout = pdevice->rad_info.chip_class >= CIK;
  753. break;
  754. }
  755. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PRIORITY_FEATURES_EXT: {
  756. VkPhysicalDeviceMemoryPriorityFeaturesEXT *features =
  757. (VkPhysicalDeviceMemoryPriorityFeaturesEXT *)ext;
  758. features->memoryPriority = VK_TRUE;
  759. break;
  760. }
  761. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_ADDRESS_FEATURES_EXT: {
  762. VkPhysicalDeviceBufferAddressFeaturesEXT *features =
  763. (VkPhysicalDeviceBufferAddressFeaturesEXT *)ext;
  764. features->bufferDeviceAddress = true;
  765. features->bufferDeviceAddressCaptureReplay = false;
  766. features->bufferDeviceAddressMultiDevice = false;
  767. break;
  768. }
  769. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT: {
  770. VkPhysicalDeviceDepthClipEnableFeaturesEXT *features =
  771. (VkPhysicalDeviceDepthClipEnableFeaturesEXT *)ext;
  772. features->depthClipEnable = true;
  773. break;
  774. }
  775. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT: {
  776. VkPhysicalDeviceHostQueryResetFeaturesEXT *features =
  777. (VkPhysicalDeviceHostQueryResetFeaturesEXT *)ext;
  778. features->hostQueryReset = true;
  779. break;
  780. }
  781. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR: {
  782. VkPhysicalDevice8BitStorageFeaturesKHR *features =
  783. (VkPhysicalDevice8BitStorageFeaturesKHR*)ext;
  784. bool enabled = pdevice->rad_info.chip_class >= VI;
  785. features->storageBuffer8BitAccess = enabled;
  786. features->uniformAndStorageBuffer8BitAccess = enabled;
  787. features->storagePushConstant8 = enabled;
  788. break;
  789. }
  790. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR: {
  791. VkPhysicalDeviceFloat16Int8FeaturesKHR *features =
  792. (VkPhysicalDeviceFloat16Int8FeaturesKHR*)ext;
  793. features->shaderFloat16 = pdevice->rad_info.chip_class >= VI && HAVE_LLVM >= 0x0800;
  794. features->shaderInt8 = true;
  795. break;
  796. }
  797. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES_KHR: {
  798. VkPhysicalDeviceShaderAtomicInt64FeaturesKHR *features =
  799. (VkPhysicalDeviceShaderAtomicInt64FeaturesKHR *)ext;
  800. /* TODO: Enable this once the driver supports 64-bit
  801. * compare&swap atomic operations.
  802. */
  803. features->shaderBufferInt64Atomics = false;
  804. features->shaderSharedInt64Atomics = false;
  805. break;
  806. }
  807. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT: {
  808. VkPhysicalDeviceInlineUniformBlockFeaturesEXT *features =
  809. (VkPhysicalDeviceInlineUniformBlockFeaturesEXT *)ext;
  810. features->inlineUniformBlock = true;
  811. features->descriptorBindingInlineUniformBlockUpdateAfterBind = true;
  812. break;
  813. }
  814. default:
  815. break;
  816. }
  817. }
  818. return radv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
  819. }
  820. void radv_GetPhysicalDeviceProperties(
  821. VkPhysicalDevice physicalDevice,
  822. VkPhysicalDeviceProperties* pProperties)
  823. {
  824. RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
  825. VkSampleCountFlags sample_counts = 0xf;
  826. /* make sure that the entire descriptor set is addressable with a signed
  827. * 32-bit int. So the sum of all limits scaled by descriptor size has to
  828. * be at most 2 GiB. the combined image & samples object count as one of
  829. * both. This limit is for the pipeline layout, not for the set layout, but
  830. * there is no set limit, so we just set a pipeline limit. I don't think
  831. * any app is going to hit this soon. */
  832. size_t max_descriptor_set_size = ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS) /
  833. (32 /* uniform buffer, 32 due to potential space wasted on alignment */ +
  834. 32 /* storage buffer, 32 due to potential space wasted on alignment */ +
  835. 32 /* sampler, largest when combined with image */ +
  836. 64 /* sampled image */ +
  837. 64 /* storage image */);
  838. VkPhysicalDeviceLimits limits = {
  839. .maxImageDimension1D = (1 << 14),
  840. .maxImageDimension2D = (1 << 14),
  841. .maxImageDimension3D = (1 << 11),
  842. .maxImageDimensionCube = (1 << 14),
  843. .maxImageArrayLayers = (1 << 11),
  844. .maxTexelBufferElements = 128 * 1024 * 1024,
  845. .maxUniformBufferRange = UINT32_MAX,
  846. .maxStorageBufferRange = UINT32_MAX,
  847. .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
  848. .maxMemoryAllocationCount = UINT32_MAX,
  849. .maxSamplerAllocationCount = 64 * 1024,
  850. .bufferImageGranularity = 64, /* A cache line */
  851. .sparseAddressSpaceSize = 0xffffffffu, /* buffer max size */
  852. .maxBoundDescriptorSets = MAX_SETS,
  853. .maxPerStageDescriptorSamplers = max_descriptor_set_size,
  854. .maxPerStageDescriptorUniformBuffers = max_descriptor_set_size,
  855. .maxPerStageDescriptorStorageBuffers = max_descriptor_set_size,
  856. .maxPerStageDescriptorSampledImages = max_descriptor_set_size,
  857. .maxPerStageDescriptorStorageImages = max_descriptor_set_size,
  858. .maxPerStageDescriptorInputAttachments = max_descriptor_set_size,
  859. .maxPerStageResources = max_descriptor_set_size,
  860. .maxDescriptorSetSamplers = max_descriptor_set_size,
  861. .maxDescriptorSetUniformBuffers = max_descriptor_set_size,
  862. .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_UNIFORM_BUFFERS,
  863. .maxDescriptorSetStorageBuffers = max_descriptor_set_size,
  864. .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_STORAGE_BUFFERS,
  865. .maxDescriptorSetSampledImages = max_descriptor_set_size,
  866. .maxDescriptorSetStorageImages = max_descriptor_set_size,
  867. .maxDescriptorSetInputAttachments = max_descriptor_set_size,
  868. .maxVertexInputAttributes = MAX_VERTEX_ATTRIBS,
  869. .maxVertexInputBindings = MAX_VBS,
  870. .maxVertexInputAttributeOffset = 2047,
  871. .maxVertexInputBindingStride = 2048,
  872. .maxVertexOutputComponents = 128,
  873. .maxTessellationGenerationLevel = 64,
  874. .maxTessellationPatchSize = 32,
  875. .maxTessellationControlPerVertexInputComponents = 128,
  876. .maxTessellationControlPerVertexOutputComponents = 128,
  877. .maxTessellationControlPerPatchOutputComponents = 120,
  878. .maxTessellationControlTotalOutputComponents = 4096,
  879. .maxTessellationEvaluationInputComponents = 128,
  880. .maxTessellationEvaluationOutputComponents = 128,
  881. .maxGeometryShaderInvocations = 127,
  882. .maxGeometryInputComponents = 64,
  883. .maxGeometryOutputComponents = 128,
  884. .maxGeometryOutputVertices = 256,
  885. .maxGeometryTotalOutputComponents = 1024,
  886. .maxFragmentInputComponents = 128,
  887. .maxFragmentOutputAttachments = 8,
  888. .maxFragmentDualSrcAttachments = 1,
  889. .maxFragmentCombinedOutputResources = 8,
  890. .maxComputeSharedMemorySize = 32768,
  891. .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
  892. .maxComputeWorkGroupInvocations = 2048,
  893. .maxComputeWorkGroupSize = {
  894. 2048,
  895. 2048,
  896. 2048
  897. },
  898. .subPixelPrecisionBits = 8,
  899. .subTexelPrecisionBits = 8,
  900. .mipmapPrecisionBits = 8,
  901. .maxDrawIndexedIndexValue = UINT32_MAX,
  902. .maxDrawIndirectCount = UINT32_MAX,
  903. .maxSamplerLodBias = 16,
  904. .maxSamplerAnisotropy = 16,
  905. .maxViewports = MAX_VIEWPORTS,
  906. .maxViewportDimensions = { (1 << 14), (1 << 14) },
  907. .viewportBoundsRange = { INT16_MIN, INT16_MAX },
  908. .viewportSubPixelBits = 8,
  909. .minMemoryMapAlignment = 4096, /* A page */
  910. .minTexelBufferOffsetAlignment = 1,
  911. .minUniformBufferOffsetAlignment = 4,
  912. .minStorageBufferOffsetAlignment = 4,
  913. .minTexelOffset = -32,
  914. .maxTexelOffset = 31,
  915. .minTexelGatherOffset = -32,
  916. .maxTexelGatherOffset = 31,
  917. .minInterpolationOffset = -2,
  918. .maxInterpolationOffset = 2,
  919. .subPixelInterpolationOffsetBits = 8,
  920. .maxFramebufferWidth = (1 << 14),
  921. .maxFramebufferHeight = (1 << 14),
  922. .maxFramebufferLayers = (1 << 10),
  923. .framebufferColorSampleCounts = sample_counts,
  924. .framebufferDepthSampleCounts = sample_counts,
  925. .framebufferStencilSampleCounts = sample_counts,
  926. .framebufferNoAttachmentsSampleCounts = sample_counts,
  927. .maxColorAttachments = MAX_RTS,
  928. .sampledImageColorSampleCounts = sample_counts,
  929. .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
  930. .sampledImageDepthSampleCounts = sample_counts,
  931. .sampledImageStencilSampleCounts = sample_counts,
  932. .storageImageSampleCounts = pdevice->rad_info.chip_class >= VI ? sample_counts : VK_SAMPLE_COUNT_1_BIT,
  933. .maxSampleMaskWords = 1,
  934. .timestampComputeAndGraphics = true,
  935. .timestampPeriod = 1000000.0 / pdevice->rad_info.clock_crystal_freq,
  936. .maxClipDistances = 8,
  937. .maxCullDistances = 8,
  938. .maxCombinedClipAndCullDistances = 8,
  939. .discreteQueuePriorities = 2,
  940. .pointSizeRange = { 0.0, 8192.0 },
  941. .lineWidthRange = { 0.0, 7.9921875 },
  942. .pointSizeGranularity = (1.0 / 8.0),
  943. .lineWidthGranularity = (1.0 / 128.0),
  944. .strictLines = false, /* FINISHME */
  945. .standardSampleLocations = true,
  946. .optimalBufferCopyOffsetAlignment = 128,
  947. .optimalBufferCopyRowPitchAlignment = 128,
  948. .nonCoherentAtomSize = 64,
  949. };
  950. *pProperties = (VkPhysicalDeviceProperties) {
  951. .apiVersion = radv_physical_device_api_version(pdevice),
  952. .driverVersion = vk_get_driver_version(),
  953. .vendorID = ATI_VENDOR_ID,
  954. .deviceID = pdevice->rad_info.pci_id,
  955. .deviceType = pdevice->rad_info.has_dedicated_vram ? VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU : VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
  956. .limits = limits,
  957. .sparseProperties = {0},
  958. };
  959. strcpy(pProperties->deviceName, pdevice->name);
  960. memcpy(pProperties->pipelineCacheUUID, pdevice->cache_uuid, VK_UUID_SIZE);
  961. }
  962. void radv_GetPhysicalDeviceProperties2(
  963. VkPhysicalDevice physicalDevice,
  964. VkPhysicalDeviceProperties2 *pProperties)
  965. {
  966. RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
  967. radv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
  968. vk_foreach_struct(ext, pProperties->pNext) {
  969. switch (ext->sType) {
  970. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
  971. VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
  972. (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
  973. properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
  974. break;
  975. }
  976. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
  977. VkPhysicalDeviceIDProperties *properties = (VkPhysicalDeviceIDProperties*)ext;
  978. memcpy(properties->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
  979. memcpy(properties->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
  980. properties->deviceLUIDValid = false;
  981. break;
  982. }
  983. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
  984. VkPhysicalDeviceMultiviewProperties *properties = (VkPhysicalDeviceMultiviewProperties*)ext;
  985. properties->maxMultiviewViewCount = MAX_VIEWS;
  986. properties->maxMultiviewInstanceIndex = INT_MAX;
  987. break;
  988. }
  989. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
  990. VkPhysicalDevicePointClippingProperties *properties =
  991. (VkPhysicalDevicePointClippingProperties*)ext;
  992. properties->pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES;
  993. break;
  994. }
  995. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DISCARD_RECTANGLE_PROPERTIES_EXT: {
  996. VkPhysicalDeviceDiscardRectanglePropertiesEXT *properties =
  997. (VkPhysicalDeviceDiscardRectanglePropertiesEXT*)ext;
  998. properties->maxDiscardRectangles = MAX_DISCARD_RECTANGLES;
  999. break;
  1000. }
  1001. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT: {
  1002. VkPhysicalDeviceExternalMemoryHostPropertiesEXT *properties =
  1003. (VkPhysicalDeviceExternalMemoryHostPropertiesEXT *) ext;
  1004. properties->minImportedHostPointerAlignment = 4096;
  1005. break;
  1006. }
  1007. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
  1008. VkPhysicalDeviceSubgroupProperties *properties =
  1009. (VkPhysicalDeviceSubgroupProperties*)ext;
  1010. properties->subgroupSize = 64;
  1011. properties->supportedStages = VK_SHADER_STAGE_ALL;
  1012. properties->supportedOperations =
  1013. VK_SUBGROUP_FEATURE_BASIC_BIT |
  1014. VK_SUBGROUP_FEATURE_BALLOT_BIT |
  1015. VK_SUBGROUP_FEATURE_QUAD_BIT |
  1016. VK_SUBGROUP_FEATURE_VOTE_BIT;
  1017. if (pdevice->rad_info.chip_class >= VI) {
  1018. properties->supportedOperations |=
  1019. VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
  1020. VK_SUBGROUP_FEATURE_SHUFFLE_BIT |
  1021. VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT;
  1022. }
  1023. properties->quadOperationsInAllStages = true;
  1024. break;
  1025. }
  1026. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
  1027. VkPhysicalDeviceMaintenance3Properties *properties =
  1028. (VkPhysicalDeviceMaintenance3Properties*)ext;
  1029. /* Make sure everything is addressable by a signed 32-bit int, and
  1030. * our largest descriptors are 96 bytes. */
  1031. properties->maxPerSetDescriptors = (1ull << 31) / 96;
  1032. /* Our buffer size fields allow only this much */
  1033. properties->maxMemoryAllocationSize = 0xFFFFFFFFull;
  1034. break;
  1035. }
  1036. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT: {
  1037. VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *properties =
  1038. (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *)ext;
  1039. /* GFX6-8 only support single channel min/max filter. */
  1040. properties->filterMinmaxImageComponentMapping = pdevice->rad_info.chip_class >= GFX9;
  1041. properties->filterMinmaxSingleComponentFormats = true;
  1042. break;
  1043. }
  1044. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_CORE_PROPERTIES_AMD: {
  1045. VkPhysicalDeviceShaderCorePropertiesAMD *properties =
  1046. (VkPhysicalDeviceShaderCorePropertiesAMD *)ext;
  1047. /* Shader engines. */
  1048. properties->shaderEngineCount =
  1049. pdevice->rad_info.max_se;
  1050. properties->shaderArraysPerEngineCount =
  1051. pdevice->rad_info.max_sh_per_se;
  1052. properties->computeUnitsPerShaderArray =
  1053. pdevice->rad_info.num_good_cu_per_sh;
  1054. properties->simdPerComputeUnit = 4;
  1055. properties->wavefrontsPerSimd =
  1056. pdevice->rad_info.family == CHIP_TONGA ||
  1057. pdevice->rad_info.family == CHIP_ICELAND ||
  1058. pdevice->rad_info.family == CHIP_POLARIS10 ||
  1059. pdevice->rad_info.family == CHIP_POLARIS11 ||
  1060. pdevice->rad_info.family == CHIP_POLARIS12 ||
  1061. pdevice->rad_info.family == CHIP_VEGAM ? 8 : 10;
  1062. properties->wavefrontSize = 64;
  1063. /* SGPR. */
  1064. properties->sgprsPerSimd =
  1065. ac_get_num_physical_sgprs(pdevice->rad_info.chip_class);
  1066. properties->minSgprAllocation =
  1067. pdevice->rad_info.chip_class >= VI ? 16 : 8;
  1068. properties->maxSgprAllocation =
  1069. pdevice->rad_info.family == CHIP_TONGA ||
  1070. pdevice->rad_info.family == CHIP_ICELAND ? 96 : 104;
  1071. properties->sgprAllocationGranularity =
  1072. pdevice->rad_info.chip_class >= VI ? 16 : 8;
  1073. /* VGPR. */
  1074. properties->vgprsPerSimd = RADV_NUM_PHYSICAL_VGPRS;
  1075. properties->minVgprAllocation = 4;
  1076. properties->maxVgprAllocation = 256;
  1077. properties->vgprAllocationGranularity = 4;
  1078. break;
  1079. }
  1080. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
  1081. VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *properties =
  1082. (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
  1083. properties->maxVertexAttribDivisor = UINT32_MAX;
  1084. break;
  1085. }
  1086. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_PROPERTIES_EXT: {
  1087. VkPhysicalDeviceDescriptorIndexingPropertiesEXT *properties =
  1088. (VkPhysicalDeviceDescriptorIndexingPropertiesEXT*)ext;
  1089. properties->maxUpdateAfterBindDescriptorsInAllPools = UINT32_MAX / 64;
  1090. properties->shaderUniformBufferArrayNonUniformIndexingNative = false;
  1091. properties->shaderSampledImageArrayNonUniformIndexingNative = false;
  1092. properties->shaderStorageBufferArrayNonUniformIndexingNative = false;
  1093. properties->shaderStorageImageArrayNonUniformIndexingNative = false;
  1094. properties->shaderInputAttachmentArrayNonUniformIndexingNative = false;
  1095. properties->robustBufferAccessUpdateAfterBind = false;
  1096. properties->quadDivergentImplicitLod = false;
  1097. size_t max_descriptor_set_size = ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS -
  1098. MAX_INLINE_UNIFORM_BLOCK_SIZE * MAX_INLINE_UNIFORM_BLOCK_COUNT) /
  1099. (32 /* uniform buffer, 32 due to potential space wasted on alignment */ +
  1100. 32 /* storage buffer, 32 due to potential space wasted on alignment */ +
  1101. 32 /* sampler, largest when combined with image */ +
  1102. 64 /* sampled image */ +
  1103. 64 /* storage image */);
  1104. properties->maxPerStageDescriptorUpdateAfterBindSamplers = max_descriptor_set_size;
  1105. properties->maxPerStageDescriptorUpdateAfterBindUniformBuffers = max_descriptor_set_size;
  1106. properties->maxPerStageDescriptorUpdateAfterBindStorageBuffers = max_descriptor_set_size;
  1107. properties->maxPerStageDescriptorUpdateAfterBindSampledImages = max_descriptor_set_size;
  1108. properties->maxPerStageDescriptorUpdateAfterBindStorageImages = max_descriptor_set_size;
  1109. properties->maxPerStageDescriptorUpdateAfterBindInputAttachments = max_descriptor_set_size;
  1110. properties->maxPerStageUpdateAfterBindResources = max_descriptor_set_size;
  1111. properties->maxDescriptorSetUpdateAfterBindSamplers = max_descriptor_set_size;
  1112. properties->maxDescriptorSetUpdateAfterBindUniformBuffers = max_descriptor_set_size;
  1113. properties->maxDescriptorSetUpdateAfterBindUniformBuffersDynamic = MAX_DYNAMIC_UNIFORM_BUFFERS;
  1114. properties->maxDescriptorSetUpdateAfterBindStorageBuffers = max_descriptor_set_size;
  1115. properties->maxDescriptorSetUpdateAfterBindStorageBuffersDynamic = MAX_DYNAMIC_STORAGE_BUFFERS;
  1116. properties->maxDescriptorSetUpdateAfterBindSampledImages = max_descriptor_set_size;
  1117. properties->maxDescriptorSetUpdateAfterBindStorageImages = max_descriptor_set_size;
  1118. properties->maxDescriptorSetUpdateAfterBindInputAttachments = max_descriptor_set_size;
  1119. break;
  1120. }
  1121. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES: {
  1122. VkPhysicalDeviceProtectedMemoryProperties *properties =
  1123. (VkPhysicalDeviceProtectedMemoryProperties *)ext;
  1124. properties->protectedNoFault = false;
  1125. break;
  1126. }
  1127. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONSERVATIVE_RASTERIZATION_PROPERTIES_EXT: {
  1128. VkPhysicalDeviceConservativeRasterizationPropertiesEXT *properties =
  1129. (VkPhysicalDeviceConservativeRasterizationPropertiesEXT *)ext;
  1130. properties->primitiveOverestimationSize = 0;
  1131. properties->maxExtraPrimitiveOverestimationSize = 0;
  1132. properties->extraPrimitiveOverestimationSizeGranularity = 0;
  1133. properties->primitiveUnderestimation = VK_FALSE;
  1134. properties->conservativePointAndLineRasterization = VK_FALSE;
  1135. properties->degenerateTrianglesRasterized = VK_FALSE;
  1136. properties->degenerateLinesRasterized = VK_FALSE;
  1137. properties->fullyCoveredFragmentShaderInputVariable = VK_FALSE;
  1138. properties->conservativeRasterizationPostDepthCoverage = VK_FALSE;
  1139. break;
  1140. }
  1141. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT: {
  1142. VkPhysicalDevicePCIBusInfoPropertiesEXT *properties =
  1143. (VkPhysicalDevicePCIBusInfoPropertiesEXT *)ext;
  1144. properties->pciDomain = pdevice->bus_info.domain;
  1145. properties->pciBus = pdevice->bus_info.bus;
  1146. properties->pciDevice = pdevice->bus_info.dev;
  1147. properties->pciFunction = pdevice->bus_info.func;
  1148. break;
  1149. }
  1150. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR: {
  1151. VkPhysicalDeviceDriverPropertiesKHR *driver_props =
  1152. (VkPhysicalDeviceDriverPropertiesKHR *) ext;
  1153. driver_props->driverID = VK_DRIVER_ID_MESA_RADV_KHR;
  1154. memset(driver_props->driverName, 0, VK_MAX_DRIVER_NAME_SIZE_KHR);
  1155. strcpy(driver_props->driverName, "radv");
  1156. memset(driver_props->driverInfo, 0, VK_MAX_DRIVER_INFO_SIZE_KHR);
  1157. snprintf(driver_props->driverInfo, VK_MAX_DRIVER_INFO_SIZE_KHR,
  1158. "Mesa " PACKAGE_VERSION MESA_GIT_SHA1
  1159. " (LLVM " MESA_LLVM_VERSION_STRING ")");
  1160. driver_props->conformanceVersion = (VkConformanceVersionKHR) {
  1161. .major = 1,
  1162. .minor = 1,
  1163. .subminor = 2,
  1164. .patch = 0,
  1165. };
  1166. break;
  1167. }
  1168. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
  1169. VkPhysicalDeviceTransformFeedbackPropertiesEXT *properties =
  1170. (VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
  1171. properties->maxTransformFeedbackStreams = MAX_SO_STREAMS;
  1172. properties->maxTransformFeedbackBuffers = MAX_SO_BUFFERS;
  1173. properties->maxTransformFeedbackBufferSize = UINT32_MAX;
  1174. properties->maxTransformFeedbackStreamDataSize = 512;
  1175. properties->maxTransformFeedbackBufferDataSize = UINT32_MAX;
  1176. properties->maxTransformFeedbackBufferDataStride = 512;
  1177. properties->transformFeedbackQueries = true;
  1178. properties->transformFeedbackStreamsLinesTriangles = false;
  1179. properties->transformFeedbackRasterizationStreamSelect = false;
  1180. properties->transformFeedbackDraw = true;
  1181. break;
  1182. }
  1183. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT: {
  1184. VkPhysicalDeviceInlineUniformBlockPropertiesEXT *props =
  1185. (VkPhysicalDeviceInlineUniformBlockPropertiesEXT *)ext;
  1186. props->maxInlineUniformBlockSize = MAX_INLINE_UNIFORM_BLOCK_SIZE;
  1187. props->maxPerStageDescriptorInlineUniformBlocks = MAX_INLINE_UNIFORM_BLOCK_SIZE * MAX_SETS;
  1188. props->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks = MAX_INLINE_UNIFORM_BLOCK_SIZE * MAX_SETS;
  1189. props->maxDescriptorSetInlineUniformBlocks = MAX_INLINE_UNIFORM_BLOCK_COUNT;
  1190. props->maxDescriptorSetUpdateAfterBindInlineUniformBlocks = MAX_INLINE_UNIFORM_BLOCK_COUNT;
  1191. break;
  1192. }
  1193. default:
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. static void radv_get_physical_device_queue_family_properties(
  1199. struct radv_physical_device* pdevice,
  1200. uint32_t* pCount,
  1201. VkQueueFamilyProperties** pQueueFamilyProperties)
  1202. {
  1203. int num_queue_families = 1;
  1204. int idx;
  1205. if (pdevice->rad_info.num_compute_rings > 0 &&
  1206. !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE))
  1207. num_queue_families++;
  1208. if (pQueueFamilyProperties == NULL) {
  1209. *pCount = num_queue_families;
  1210. return;
  1211. }
  1212. if (!*pCount)
  1213. return;
  1214. idx = 0;
  1215. if (*pCount >= 1) {
  1216. *pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
  1217. .queueFlags = VK_QUEUE_GRAPHICS_BIT |
  1218. VK_QUEUE_COMPUTE_BIT |
  1219. VK_QUEUE_TRANSFER_BIT |
  1220. VK_QUEUE_SPARSE_BINDING_BIT,
  1221. .queueCount = 1,
  1222. .timestampValidBits = 64,
  1223. .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
  1224. };
  1225. idx++;
  1226. }
  1227. if (pdevice->rad_info.num_compute_rings > 0 &&
  1228. !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE)) {
  1229. if (*pCount > idx) {
  1230. *pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
  1231. .queueFlags = VK_QUEUE_COMPUTE_BIT |
  1232. VK_QUEUE_TRANSFER_BIT |
  1233. VK_QUEUE_SPARSE_BINDING_BIT,
  1234. .queueCount = pdevice->rad_info.num_compute_rings,
  1235. .timestampValidBits = 64,
  1236. .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
  1237. };
  1238. idx++;
  1239. }
  1240. }
  1241. *pCount = idx;
  1242. }
  1243. void radv_GetPhysicalDeviceQueueFamilyProperties(
  1244. VkPhysicalDevice physicalDevice,
  1245. uint32_t* pCount,
  1246. VkQueueFamilyProperties* pQueueFamilyProperties)
  1247. {
  1248. RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
  1249. if (!pQueueFamilyProperties) {
  1250. radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
  1251. return;
  1252. }
  1253. VkQueueFamilyProperties *properties[] = {
  1254. pQueueFamilyProperties + 0,
  1255. pQueueFamilyProperties + 1,
  1256. pQueueFamilyProperties + 2,
  1257. };
  1258. radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
  1259. assert(*pCount <= 3);
  1260. }
  1261. void radv_GetPhysicalDeviceQueueFamilyProperties2(
  1262. VkPhysicalDevice physicalDevice,
  1263. uint32_t* pCount,
  1264. VkQueueFamilyProperties2 *pQueueFamilyProperties)
  1265. {
  1266. RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
  1267. if (!pQueueFamilyProperties) {
  1268. radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
  1269. return;
  1270. }
  1271. VkQueueFamilyProperties *properties[] = {
  1272. &pQueueFamilyProperties[0].queueFamilyProperties,
  1273. &pQueueFamilyProperties[1].queueFamilyProperties,
  1274. &pQueueFamilyProperties[2].queueFamilyProperties,
  1275. };
  1276. radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
  1277. assert(*pCount <= 3);
  1278. }
  1279. void radv_GetPhysicalDeviceMemoryProperties(
  1280. VkPhysicalDevice physicalDevice,
  1281. VkPhysicalDeviceMemoryProperties *pMemoryProperties)
  1282. {
  1283. RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
  1284. *pMemoryProperties = physical_device->memory_properties;
  1285. }
  1286. static void
  1287. radv_get_memory_budget_properties(VkPhysicalDevice physicalDevice,
  1288. VkPhysicalDeviceMemoryBudgetPropertiesEXT *memoryBudget)
  1289. {
  1290. RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
  1291. VkPhysicalDeviceMemoryProperties *memory_properties = &device->memory_properties;
  1292. uint64_t visible_vram_size = radv_get_visible_vram_size(device);
  1293. uint64_t vram_size = radv_get_vram_size(device);
  1294. uint64_t gtt_size = device->rad_info.gart_size;
  1295. uint64_t heap_budget, heap_usage;
  1296. /* For all memory heaps, the computation of budget is as follow:
  1297. * heap_budget = heap_size - global_heap_usage + app_heap_usage
  1298. *
  1299. * The Vulkan spec 1.1.97 says that the budget should include any
  1300. * currently allocated device memory.
  1301. *
  1302. * Note that the application heap usages are not really accurate (eg.
  1303. * in presence of shared buffers).
  1304. */
  1305. if (vram_size) {
  1306. heap_usage = device->ws->query_value(device->ws,
  1307. RADEON_ALLOCATED_VRAM);
  1308. heap_budget = vram_size -
  1309. device->ws->query_value(device->ws, RADEON_VRAM_USAGE) +
  1310. heap_usage;
  1311. memoryBudget->heapBudget[RADV_MEM_HEAP_VRAM] = heap_budget;
  1312. memoryBudget->heapUsage[RADV_MEM_HEAP_VRAM] = heap_usage;
  1313. }
  1314. if (visible_vram_size) {
  1315. heap_usage = device->ws->query_value(device->ws,
  1316. RADEON_ALLOCATED_VRAM_VIS);
  1317. heap_budget = visible_vram_size -
  1318. device->ws->query_value(device->ws, RADEON_VRAM_VIS_USAGE) +
  1319. heap_usage;
  1320. memoryBudget->heapBudget[RADV_MEM_HEAP_VRAM_CPU_ACCESS] = heap_budget;
  1321. memoryBudget->heapUsage[RADV_MEM_HEAP_VRAM_CPU_ACCESS] = heap_usage;
  1322. }
  1323. if (gtt_size) {
  1324. heap_usage = device->ws->query_value(device->ws,
  1325. RADEON_ALLOCATED_GTT);
  1326. heap_budget = gtt_size -
  1327. device->ws->query_value(device->ws, RADEON_GTT_USAGE) +
  1328. heap_usage;
  1329. memoryBudget->heapBudget[RADV_MEM_HEAP_GTT] = heap_budget;
  1330. memoryBudget->heapUsage[RADV_MEM_HEAP_GTT] = heap_usage;
  1331. }
  1332. /* The heapBudget and heapUsage values must be zero for array elements
  1333. * greater than or equal to
  1334. * VkPhysicalDeviceMemoryProperties::memoryHeapCount.
  1335. */
  1336. for (uint32_t i = memory_properties->memoryHeapCount; i < VK_MAX_MEMORY_HEAPS; i++) {
  1337. memoryBudget->heapBudget[i] = 0;
  1338. memoryBudget->heapUsage[i] = 0;
  1339. }
  1340. }
  1341. void radv_GetPhysicalDeviceMemoryProperties2(
  1342. VkPhysicalDevice physicalDevice,
  1343. VkPhysicalDeviceMemoryProperties2 *pMemoryProperties)
  1344. {
  1345. radv_GetPhysicalDeviceMemoryProperties(physicalDevice,
  1346. &pMemoryProperties->memoryProperties);
  1347. VkPhysicalDeviceMemoryBudgetPropertiesEXT *memory_budget =
  1348. vk_find_struct(pMemoryProperties->pNext,
  1349. PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT);
  1350. if (memory_budget)
  1351. radv_get_memory_budget_properties(physicalDevice, memory_budget);
  1352. }
  1353. VkResult radv_GetMemoryHostPointerPropertiesEXT(
  1354. VkDevice _device,
  1355. VkExternalMemoryHandleTypeFlagBits handleType,
  1356. const void *pHostPointer,
  1357. VkMemoryHostPointerPropertiesEXT *pMemoryHostPointerProperties)
  1358. {
  1359. RADV_FROM_HANDLE(radv_device, device, _device);
  1360. switch (handleType)
  1361. {
  1362. case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT: {
  1363. const struct radv_physical_device *physical_device = device->physical_device;
  1364. uint32_t memoryTypeBits = 0;
  1365. for (int i = 0; i < physical_device->memory_properties.memoryTypeCount; i++) {
  1366. if (physical_device->mem_type_indices[i] == RADV_MEM_TYPE_GTT_CACHED) {
  1367. memoryTypeBits = (1 << i);
  1368. break;
  1369. }
  1370. }
  1371. pMemoryHostPointerProperties->memoryTypeBits = memoryTypeBits;
  1372. return VK_SUCCESS;
  1373. }
  1374. default:
  1375. return VK_ERROR_INVALID_EXTERNAL_HANDLE;
  1376. }
  1377. }
  1378. static enum radeon_ctx_priority
  1379. radv_get_queue_global_priority(const VkDeviceQueueGlobalPriorityCreateInfoEXT *pObj)
  1380. {
  1381. /* Default to MEDIUM when a specific global priority isn't requested */
  1382. if (!pObj)
  1383. return RADEON_CTX_PRIORITY_MEDIUM;
  1384. switch(pObj->globalPriority) {
  1385. case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT:
  1386. return RADEON_CTX_PRIORITY_REALTIME;
  1387. case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT:
  1388. return RADEON_CTX_PRIORITY_HIGH;
  1389. case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT:
  1390. return RADEON_CTX_PRIORITY_MEDIUM;
  1391. case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT:
  1392. return RADEON_CTX_PRIORITY_LOW;
  1393. default:
  1394. unreachable("Illegal global priority value");
  1395. return RADEON_CTX_PRIORITY_INVALID;
  1396. }
  1397. }
  1398. static int
  1399. radv_queue_init(struct radv_device *device, struct radv_queue *queue,
  1400. uint32_t queue_family_index, int idx,
  1401. VkDeviceQueueCreateFlags flags,
  1402. const VkDeviceQueueGlobalPriorityCreateInfoEXT *global_priority)
  1403. {
  1404. queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
  1405. queue->device = device;
  1406. queue->queue_family_index = queue_family_index;
  1407. queue->queue_idx = idx;
  1408. queue->priority = radv_get_queue_global_priority(global_priority);
  1409. queue->flags = flags;
  1410. queue->hw_ctx = device->ws->ctx_create(device->ws, queue->priority);
  1411. if (!queue->hw_ctx)
  1412. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  1413. return VK_SUCCESS;
  1414. }
  1415. static void
  1416. radv_queue_finish(struct radv_queue *queue)
  1417. {
  1418. if (queue->hw_ctx)
  1419. queue->device->ws->ctx_destroy(queue->hw_ctx);
  1420. if (queue->initial_full_flush_preamble_cs)
  1421. queue->device->ws->cs_destroy(queue->initial_full_flush_preamble_cs);
  1422. if (queue->initial_preamble_cs)
  1423. queue->device->ws->cs_destroy(queue->initial_preamble_cs);
  1424. if (queue->continue_preamble_cs)
  1425. queue->device->ws->cs_destroy(queue->continue_preamble_cs);
  1426. if (queue->descriptor_bo)
  1427. queue->device->ws->buffer_destroy(queue->descriptor_bo);
  1428. if (queue->scratch_bo)
  1429. queue->device->ws->buffer_destroy(queue->scratch_bo);
  1430. if (queue->esgs_ring_bo)
  1431. queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
  1432. if (queue->gsvs_ring_bo)
  1433. queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
  1434. if (queue->tess_rings_bo)
  1435. queue->device->ws->buffer_destroy(queue->tess_rings_bo);
  1436. if (queue->compute_scratch_bo)
  1437. queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
  1438. }
  1439. static void
  1440. radv_bo_list_init(struct radv_bo_list *bo_list)
  1441. {
  1442. pthread_mutex_init(&bo_list->mutex, NULL);
  1443. bo_list->list.count = bo_list->capacity = 0;
  1444. bo_list->list.bos = NULL;
  1445. }
  1446. static void
  1447. radv_bo_list_finish(struct radv_bo_list *bo_list)
  1448. {
  1449. free(bo_list->list.bos);
  1450. pthread_mutex_destroy(&bo_list->mutex);
  1451. }
  1452. static VkResult radv_bo_list_add(struct radv_device *device,
  1453. struct radeon_winsys_bo *bo)
  1454. {
  1455. struct radv_bo_list *bo_list = &device->bo_list;
  1456. if (bo->is_local)
  1457. return VK_SUCCESS;
  1458. if (unlikely(!device->use_global_bo_list))
  1459. return VK_SUCCESS;
  1460. pthread_mutex_lock(&bo_list->mutex);
  1461. if (bo_list->list.count == bo_list->capacity) {
  1462. unsigned capacity = MAX2(4, bo_list->capacity * 2);
  1463. void *data = realloc(bo_list->list.bos, capacity * sizeof(struct radeon_winsys_bo*));
  1464. if (!data) {
  1465. pthread_mutex_unlock(&bo_list->mutex);
  1466. return VK_ERROR_OUT_OF_HOST_MEMORY;
  1467. }
  1468. bo_list->list.bos = (struct radeon_winsys_bo**)data;
  1469. bo_list->capacity = capacity;
  1470. }
  1471. bo_list->list.bos[bo_list->list.count++] = bo;
  1472. pthread_mutex_unlock(&bo_list->mutex);
  1473. return VK_SUCCESS;
  1474. }
  1475. static void radv_bo_list_remove(struct radv_device *device,
  1476. struct radeon_winsys_bo *bo)
  1477. {
  1478. struct radv_bo_list *bo_list = &device->bo_list;
  1479. if (bo->is_local)
  1480. return;
  1481. if (unlikely(!device->use_global_bo_list))
  1482. return;
  1483. pthread_mutex_lock(&bo_list->mutex);
  1484. for(unsigned i = 0; i < bo_list->list.count; ++i) {
  1485. if (bo_list->list.bos[i] == bo) {
  1486. bo_list->list.bos[i] = bo_list->list.bos[bo_list->list.count - 1];
  1487. --bo_list->list.count;
  1488. break;
  1489. }
  1490. }
  1491. pthread_mutex_unlock(&bo_list->mutex);
  1492. }
  1493. static void
  1494. radv_device_init_gs_info(struct radv_device *device)
  1495. {
  1496. device->gs_table_depth = ac_get_gs_table_depth(device->physical_device->rad_info.chip_class,
  1497. device->physical_device->rad_info.family);
  1498. }
  1499. static int radv_get_device_extension_index(const char *name)
  1500. {
  1501. for (unsigned i = 0; i < RADV_DEVICE_EXTENSION_COUNT; ++i) {
  1502. if (strcmp(name, radv_device_extensions[i].extensionName) == 0)
  1503. return i;
  1504. }
  1505. return -1;
  1506. }
  1507. static int
  1508. radv_get_int_debug_option(const char *name, int default_value)
  1509. {
  1510. const char *str;
  1511. int result;
  1512. str = getenv(name);
  1513. if (!str) {
  1514. result = default_value;
  1515. } else {
  1516. char *endptr;
  1517. result = strtol(str, &endptr, 0);
  1518. if (str == endptr) {
  1519. /* No digits founs. */
  1520. result = default_value;
  1521. }
  1522. }
  1523. return result;
  1524. }
  1525. VkResult radv_CreateDevice(
  1526. VkPhysicalDevice physicalDevice,
  1527. const VkDeviceCreateInfo* pCreateInfo,
  1528. const VkAllocationCallbacks* pAllocator,
  1529. VkDevice* pDevice)
  1530. {
  1531. RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
  1532. VkResult result;
  1533. struct radv_device *device;
  1534. bool keep_shader_info = false;
  1535. /* Check enabled features */
  1536. if (pCreateInfo->pEnabledFeatures) {
  1537. VkPhysicalDeviceFeatures supported_features;
  1538. radv_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
  1539. VkBool32 *supported_feature = (VkBool32 *)&supported_features;
  1540. VkBool32 *enabled_feature = (VkBool32 *)pCreateInfo->pEnabledFeatures;
  1541. unsigned num_features = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
  1542. for (uint32_t i = 0; i < num_features; i++) {
  1543. if (enabled_feature[i] && !supported_feature[i])
  1544. return vk_error(physical_device->instance, VK_ERROR_FEATURE_NOT_PRESENT);
  1545. }
  1546. }
  1547. device = vk_zalloc2(&physical_device->instance->alloc, pAllocator,
  1548. sizeof(*device), 8,
  1549. VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
  1550. if (!device)
  1551. return vk_error(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  1552. device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
  1553. device->instance = physical_device->instance;
  1554. device->physical_device = physical_device;
  1555. device->ws = physical_device->ws;
  1556. if (pAllocator)
  1557. device->alloc = *pAllocator;
  1558. else
  1559. device->alloc = physical_device->instance->alloc;
  1560. for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
  1561. const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
  1562. int index = radv_get_device_extension_index(ext_name);
  1563. if (index < 0 || !physical_device->supported_extensions.extensions[index]) {
  1564. vk_free(&device->alloc, device);
  1565. return vk_error(physical_device->instance, VK_ERROR_EXTENSION_NOT_PRESENT);
  1566. }
  1567. device->enabled_extensions.extensions[index] = true;
  1568. }
  1569. keep_shader_info = device->enabled_extensions.AMD_shader_info;
  1570. /* With update after bind we can't attach bo's to the command buffer
  1571. * from the descriptor set anymore, so we have to use a global BO list.
  1572. */
  1573. device->use_global_bo_list =
  1574. (device->instance->perftest_flags & RADV_PERFTEST_BO_LIST) ||
  1575. device->enabled_extensions.EXT_descriptor_indexing ||
  1576. device->enabled_extensions.EXT_buffer_device_address;
  1577. mtx_init(&device->shader_slab_mutex, mtx_plain);
  1578. list_inithead(&device->shader_slabs);
  1579. radv_bo_list_init(&device->bo_list);
  1580. for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
  1581. const VkDeviceQueueCreateInfo *queue_create = &pCreateInfo->pQueueCreateInfos[i];
  1582. uint32_t qfi = queue_create->queueFamilyIndex;
  1583. const VkDeviceQueueGlobalPriorityCreateInfoEXT *global_priority =
  1584. vk_find_struct_const(queue_create->pNext, DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT);
  1585. assert(!global_priority || device->physical_device->rad_info.has_ctx_priority);
  1586. device->queues[qfi] = vk_alloc(&device->alloc,
  1587. queue_create->queueCount * sizeof(struct radv_queue), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
  1588. if (!device->queues[qfi]) {
  1589. result = VK_ERROR_OUT_OF_HOST_MEMORY;
  1590. goto fail;
  1591. }
  1592. memset(device->queues[qfi], 0, queue_create->queueCount * sizeof(struct radv_queue));
  1593. device->queue_count[qfi] = queue_create->queueCount;
  1594. for (unsigned q = 0; q < queue_create->queueCount; q++) {
  1595. result = radv_queue_init(device, &device->queues[qfi][q],
  1596. qfi, q, queue_create->flags,
  1597. global_priority);
  1598. if (result != VK_SUCCESS)
  1599. goto fail;
  1600. }
  1601. }
  1602. device->pbb_allowed = device->physical_device->rad_info.chip_class >= GFX9 &&
  1603. !(device->instance->debug_flags & RADV_DEBUG_NOBINNING);
  1604. /* Disabled and not implemented for now. */
  1605. device->dfsm_allowed = device->pbb_allowed &&
  1606. (device->physical_device->rad_info.family == CHIP_RAVEN ||
  1607. device->physical_device->rad_info.family == CHIP_RAVEN2);
  1608. #ifdef ANDROID
  1609. device->always_use_syncobj = device->physical_device->rad_info.has_syncobj_wait_for_submit;
  1610. #endif
  1611. /* The maximum number of scratch waves. Scratch space isn't divided
  1612. * evenly between CUs. The number is only a function of the number of CUs.
  1613. * We can decrease the constant to decrease the scratch buffer size.
  1614. *
  1615. * sctx->scratch_waves must be >= the maximum possible size of
  1616. * 1 threadgroup, so that the hw doesn't hang from being unable
  1617. * to start any.
  1618. *
  1619. * The recommended value is 4 per CU at most. Higher numbers don't
  1620. * bring much benefit, but they still occupy chip resources (think
  1621. * async compute). I've seen ~2% performance difference between 4 and 32.
  1622. */
  1623. uint32_t max_threads_per_block = 2048;
  1624. device->scratch_waves = MAX2(32 * physical_device->rad_info.num_good_compute_units,
  1625. max_threads_per_block / 64);
  1626. device->dispatch_initiator = S_00B800_COMPUTE_SHADER_EN(1);
  1627. if (device->physical_device->rad_info.chip_class >= CIK) {
  1628. /* If the KMD allows it (there is a KMD hw register for it),
  1629. * allow launching waves out-of-order.
  1630. */
  1631. device->dispatch_initiator |= S_00B800_ORDER_MODE(1);
  1632. }
  1633. radv_device_init_gs_info(device);
  1634. device->tess_offchip_block_dw_size =
  1635. device->physical_device->rad_info.family == CHIP_HAWAII ? 4096 : 8192;
  1636. device->has_distributed_tess =
  1637. device->physical_device->rad_info.chip_class >= VI &&
  1638. device->physical_device->rad_info.max_se >= 2;
  1639. if (getenv("RADV_TRACE_FILE")) {
  1640. const char *filename = getenv("RADV_TRACE_FILE");
  1641. keep_shader_info = true;
  1642. if (!radv_init_trace(device))
  1643. goto fail;
  1644. fprintf(stderr, "*****************************************************************************\n");
  1645. fprintf(stderr, "* WARNING: RADV_TRACE_FILE is costly and should only be used for debugging! *\n");
  1646. fprintf(stderr, "*****************************************************************************\n");
  1647. fprintf(stderr, "Trace file will be dumped to %s\n", filename);
  1648. radv_dump_enabled_options(device, stderr);
  1649. }
  1650. device->keep_shader_info = keep_shader_info;
  1651. result = radv_device_init_meta(device);
  1652. if (result != VK_SUCCESS)
  1653. goto fail;
  1654. radv_device_init_msaa(device);
  1655. for (int family = 0; family < RADV_MAX_QUEUE_FAMILIES; ++family) {
  1656. device->empty_cs[family] = device->ws->cs_create(device->ws, family);
  1657. switch (family) {
  1658. case RADV_QUEUE_GENERAL:
  1659. radeon_emit(device->empty_cs[family], PKT3(PKT3_CONTEXT_CONTROL, 1, 0));
  1660. radeon_emit(device->empty_cs[family], CONTEXT_CONTROL_LOAD_ENABLE(1));
  1661. radeon_emit(device->empty_cs[family], CONTEXT_CONTROL_SHADOW_ENABLE(1));
  1662. break;
  1663. case RADV_QUEUE_COMPUTE:
  1664. radeon_emit(device->empty_cs[family], PKT3(PKT3_NOP, 0, 0));
  1665. radeon_emit(device->empty_cs[family], 0);
  1666. break;
  1667. }
  1668. device->ws->cs_finalize(device->empty_cs[family]);
  1669. }
  1670. if (device->physical_device->rad_info.chip_class >= CIK)
  1671. cik_create_gfx_config(device);
  1672. VkPipelineCacheCreateInfo ci;
  1673. ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
  1674. ci.pNext = NULL;
  1675. ci.flags = 0;
  1676. ci.pInitialData = NULL;
  1677. ci.initialDataSize = 0;
  1678. VkPipelineCache pc;
  1679. result = radv_CreatePipelineCache(radv_device_to_handle(device),
  1680. &ci, NULL, &pc);
  1681. if (result != VK_SUCCESS)
  1682. goto fail_meta;
  1683. device->mem_cache = radv_pipeline_cache_from_handle(pc);
  1684. device->force_aniso =
  1685. MIN2(16, radv_get_int_debug_option("RADV_TEX_ANISO", -1));
  1686. if (device->force_aniso >= 0) {
  1687. fprintf(stderr, "radv: Forcing anisotropy filter to %ix\n",
  1688. 1 << util_logbase2(device->force_aniso));
  1689. }
  1690. *pDevice = radv_device_to_handle(device);
  1691. return VK_SUCCESS;
  1692. fail_meta:
  1693. radv_device_finish_meta(device);
  1694. fail:
  1695. radv_bo_list_finish(&device->bo_list);
  1696. if (device->trace_bo)
  1697. device->ws->buffer_destroy(device->trace_bo);
  1698. if (device->gfx_init)
  1699. device->ws->buffer_destroy(device->gfx_init);
  1700. for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
  1701. for (unsigned q = 0; q < device->queue_count[i]; q++)
  1702. radv_queue_finish(&device->queues[i][q]);
  1703. if (device->queue_count[i])
  1704. vk_free(&device->alloc, device->queues[i]);
  1705. }
  1706. vk_free(&device->alloc, device);
  1707. return result;
  1708. }
  1709. void radv_DestroyDevice(
  1710. VkDevice _device,
  1711. const VkAllocationCallbacks* pAllocator)
  1712. {
  1713. RADV_FROM_HANDLE(radv_device, device, _device);
  1714. if (!device)
  1715. return;
  1716. if (device->trace_bo)
  1717. device->ws->buffer_destroy(device->trace_bo);
  1718. if (device->gfx_init)
  1719. device->ws->buffer_destroy(device->gfx_init);
  1720. for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
  1721. for (unsigned q = 0; q < device->queue_count[i]; q++)
  1722. radv_queue_finish(&device->queues[i][q]);
  1723. if (device->queue_count[i])
  1724. vk_free(&device->alloc, device->queues[i]);
  1725. if (device->empty_cs[i])
  1726. device->ws->cs_destroy(device->empty_cs[i]);
  1727. }
  1728. radv_device_finish_meta(device);
  1729. VkPipelineCache pc = radv_pipeline_cache_to_handle(device->mem_cache);
  1730. radv_DestroyPipelineCache(radv_device_to_handle(device), pc, NULL);
  1731. radv_destroy_shader_slabs(device);
  1732. radv_bo_list_finish(&device->bo_list);
  1733. vk_free(&device->alloc, device);
  1734. }
  1735. VkResult radv_EnumerateInstanceLayerProperties(
  1736. uint32_t* pPropertyCount,
  1737. VkLayerProperties* pProperties)
  1738. {
  1739. if (pProperties == NULL) {
  1740. *pPropertyCount = 0;
  1741. return VK_SUCCESS;
  1742. }
  1743. /* None supported at this time */
  1744. return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
  1745. }
  1746. VkResult radv_EnumerateDeviceLayerProperties(
  1747. VkPhysicalDevice physicalDevice,
  1748. uint32_t* pPropertyCount,
  1749. VkLayerProperties* pProperties)
  1750. {
  1751. if (pProperties == NULL) {
  1752. *pPropertyCount = 0;
  1753. return VK_SUCCESS;
  1754. }
  1755. /* None supported at this time */
  1756. return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
  1757. }
  1758. void radv_GetDeviceQueue2(
  1759. VkDevice _device,
  1760. const VkDeviceQueueInfo2* pQueueInfo,
  1761. VkQueue* pQueue)
  1762. {
  1763. RADV_FROM_HANDLE(radv_device, device, _device);
  1764. struct radv_queue *queue;
  1765. queue = &device->queues[pQueueInfo->queueFamilyIndex][pQueueInfo->queueIndex];
  1766. if (pQueueInfo->flags != queue->flags) {
  1767. /* From the Vulkan 1.1.70 spec:
  1768. *
  1769. * "The queue returned by vkGetDeviceQueue2 must have the same
  1770. * flags value from this structure as that used at device
  1771. * creation time in a VkDeviceQueueCreateInfo instance. If no
  1772. * matching flags were specified at device creation time then
  1773. * pQueue will return VK_NULL_HANDLE."
  1774. */
  1775. *pQueue = VK_NULL_HANDLE;
  1776. return;
  1777. }
  1778. *pQueue = radv_queue_to_handle(queue);
  1779. }
  1780. void radv_GetDeviceQueue(
  1781. VkDevice _device,
  1782. uint32_t queueFamilyIndex,
  1783. uint32_t queueIndex,
  1784. VkQueue* pQueue)
  1785. {
  1786. const VkDeviceQueueInfo2 info = (VkDeviceQueueInfo2) {
  1787. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
  1788. .queueFamilyIndex = queueFamilyIndex,
  1789. .queueIndex = queueIndex
  1790. };
  1791. radv_GetDeviceQueue2(_device, &info, pQueue);
  1792. }
  1793. static void
  1794. fill_geom_tess_rings(struct radv_queue *queue,
  1795. uint32_t *map,
  1796. bool add_sample_positions,
  1797. uint32_t esgs_ring_size,
  1798. struct radeon_winsys_bo *esgs_ring_bo,
  1799. uint32_t gsvs_ring_size,
  1800. struct radeon_winsys_bo *gsvs_ring_bo,
  1801. uint32_t tess_factor_ring_size,
  1802. uint32_t tess_offchip_ring_offset,
  1803. uint32_t tess_offchip_ring_size,
  1804. struct radeon_winsys_bo *tess_rings_bo)
  1805. {
  1806. uint32_t *desc = &map[4];
  1807. if (esgs_ring_bo) {
  1808. uint64_t esgs_va = radv_buffer_get_va(esgs_ring_bo);
  1809. /* stride 0, num records - size, add tid, swizzle, elsize4,
  1810. index stride 64 */
  1811. desc[0] = esgs_va;
  1812. desc[1] = S_008F04_BASE_ADDRESS_HI(esgs_va >> 32) |
  1813. S_008F04_STRIDE(0) |
  1814. S_008F04_SWIZZLE_ENABLE(true);
  1815. desc[2] = esgs_ring_size;
  1816. desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
  1817. S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
  1818. S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
  1819. S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
  1820. S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
  1821. S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
  1822. S_008F0C_ELEMENT_SIZE(1) |
  1823. S_008F0C_INDEX_STRIDE(3) |
  1824. S_008F0C_ADD_TID_ENABLE(true);
  1825. /* GS entry for ES->GS ring */
  1826. /* stride 0, num records - size, elsize0,
  1827. index stride 0 */
  1828. desc[4] = esgs_va;
  1829. desc[5] = S_008F04_BASE_ADDRESS_HI(esgs_va >> 32)|
  1830. S_008F04_STRIDE(0) |
  1831. S_008F04_SWIZZLE_ENABLE(false);
  1832. desc[6] = esgs_ring_size;
  1833. desc[7] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
  1834. S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
  1835. S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
  1836. S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
  1837. S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
  1838. S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
  1839. S_008F0C_ELEMENT_SIZE(0) |
  1840. S_008F0C_INDEX_STRIDE(0) |
  1841. S_008F0C_ADD_TID_ENABLE(false);
  1842. }
  1843. desc += 8;
  1844. if (gsvs_ring_bo) {
  1845. uint64_t gsvs_va = radv_buffer_get_va(gsvs_ring_bo);
  1846. /* VS entry for GS->VS ring */
  1847. /* stride 0, num records - size, elsize0,
  1848. index stride 0 */
  1849. desc[0] = gsvs_va;
  1850. desc[1] = S_008F04_BASE_ADDRESS_HI(gsvs_va >> 32)|
  1851. S_008F04_STRIDE(0) |
  1852. S_008F04_SWIZZLE_ENABLE(false);
  1853. desc[2] = gsvs_ring_size;
  1854. desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
  1855. S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
  1856. S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
  1857. S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
  1858. S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
  1859. S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
  1860. S_008F0C_ELEMENT_SIZE(0) |
  1861. S_008F0C_INDEX_STRIDE(0) |
  1862. S_008F0C_ADD_TID_ENABLE(false);
  1863. /* stride gsvs_itemsize, num records 64
  1864. elsize 4, index stride 16 */
  1865. /* shader will patch stride and desc[2] */
  1866. desc[4] = gsvs_va;
  1867. desc[5] = S_008F04_BASE_ADDRESS_HI(gsvs_va >> 32)|
  1868. S_008F04_STRIDE(0) |
  1869. S_008F04_SWIZZLE_ENABLE(true);
  1870. desc[6] = 0;
  1871. desc[7] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
  1872. S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
  1873. S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
  1874. S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
  1875. S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
  1876. S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
  1877. S_008F0C_ELEMENT_SIZE(1) |
  1878. S_008F0C_INDEX_STRIDE(1) |
  1879. S_008F0C_ADD_TID_ENABLE(true);
  1880. }
  1881. desc += 8;
  1882. if (tess_rings_bo) {
  1883. uint64_t tess_va = radv_buffer_get_va(tess_rings_bo);
  1884. uint64_t tess_offchip_va = tess_va + tess_offchip_ring_offset;
  1885. desc[0] = tess_va;
  1886. desc[1] = S_008F04_BASE_ADDRESS_HI(tess_va >> 32) |
  1887. S_008F04_STRIDE(0) |
  1888. S_008F04_SWIZZLE_ENABLE(false);
  1889. desc[2] = tess_factor_ring_size;
  1890. desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
  1891. S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
  1892. S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
  1893. S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
  1894. S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
  1895. S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
  1896. S_008F0C_ELEMENT_SIZE(0) |
  1897. S_008F0C_INDEX_STRIDE(0) |
  1898. S_008F0C_ADD_TID_ENABLE(false);
  1899. desc[4] = tess_offchip_va;
  1900. desc[5] = S_008F04_BASE_ADDRESS_HI(tess_offchip_va >> 32) |
  1901. S_008F04_STRIDE(0) |
  1902. S_008F04_SWIZZLE_ENABLE(false);
  1903. desc[6] = tess_offchip_ring_size;
  1904. desc[7] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
  1905. S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
  1906. S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
  1907. S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
  1908. S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
  1909. S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
  1910. S_008F0C_ELEMENT_SIZE(0) |
  1911. S_008F0C_INDEX_STRIDE(0) |
  1912. S_008F0C_ADD_TID_ENABLE(false);
  1913. }
  1914. desc += 8;
  1915. if (add_sample_positions) {
  1916. /* add sample positions after all rings */
  1917. memcpy(desc, queue->device->sample_locations_1x, 8);
  1918. desc += 2;
  1919. memcpy(desc, queue->device->sample_locations_2x, 16);
  1920. desc += 4;
  1921. memcpy(desc, queue->device->sample_locations_4x, 32);
  1922. desc += 8;
  1923. memcpy(desc, queue->device->sample_locations_8x, 64);
  1924. }
  1925. }
  1926. static unsigned
  1927. radv_get_hs_offchip_param(struct radv_device *device, uint32_t *max_offchip_buffers_p)
  1928. {
  1929. bool double_offchip_buffers = device->physical_device->rad_info.chip_class >= CIK &&
  1930. device->physical_device->rad_info.family != CHIP_CARRIZO &&
  1931. device->physical_device->rad_info.family != CHIP_STONEY;
  1932. unsigned max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;
  1933. unsigned max_offchip_buffers;
  1934. unsigned offchip_granularity;
  1935. unsigned hs_offchip_param;
  1936. /*
  1937. * Per RadeonSI:
  1938. * This must be one less than the maximum number due to a hw limitation.
  1939. * Various hardware bugs in SI, CIK, and GFX9 need this.
  1940. *
  1941. * Per AMDVLK:
  1942. * Vega10 should limit max_offchip_buffers to 508 (4 * 127).
  1943. * Gfx7 should limit max_offchip_buffers to 508
  1944. * Gfx6 should limit max_offchip_buffers to 126 (2 * 63)
  1945. *
  1946. * Follow AMDVLK here.
  1947. */
  1948. if (device->physical_device->rad_info.family == CHIP_VEGA10 ||
  1949. device->physical_device->rad_info.chip_class == CIK ||
  1950. device->physical_device->rad_info.chip_class == SI)
  1951. --max_offchip_buffers_per_se;
  1952. max_offchip_buffers = max_offchip_buffers_per_se *
  1953. device->physical_device->rad_info.max_se;
  1954. /* Hawaii has a bug with offchip buffers > 256 that can be worked
  1955. * around by setting 4K granularity.
  1956. */
  1957. if (device->tess_offchip_block_dw_size == 4096) {
  1958. assert(device->physical_device->rad_info.family == CHIP_HAWAII);
  1959. offchip_granularity = V_03093C_X_4K_DWORDS;
  1960. } else {
  1961. assert(device->tess_offchip_block_dw_size == 8192);
  1962. offchip_granularity = V_03093C_X_8K_DWORDS;
  1963. }
  1964. switch (device->physical_device->rad_info.chip_class) {
  1965. case SI:
  1966. max_offchip_buffers = MIN2(max_offchip_buffers, 126);
  1967. break;
  1968. case CIK:
  1969. case VI:
  1970. case GFX9:
  1971. default:
  1972. max_offchip_buffers = MIN2(max_offchip_buffers, 508);
  1973. break;
  1974. }
  1975. *max_offchip_buffers_p = max_offchip_buffers;
  1976. if (device->physical_device->rad_info.chip_class >= CIK) {
  1977. if (device->physical_device->rad_info.chip_class >= VI)
  1978. --max_offchip_buffers;
  1979. hs_offchip_param =
  1980. S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers) |
  1981. S_03093C_OFFCHIP_GRANULARITY(offchip_granularity);
  1982. } else {
  1983. hs_offchip_param =
  1984. S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers);
  1985. }
  1986. return hs_offchip_param;
  1987. }
  1988. static void
  1989. radv_emit_gs_ring_sizes(struct radv_queue *queue, struct radeon_cmdbuf *cs,
  1990. struct radeon_winsys_bo *esgs_ring_bo,
  1991. uint32_t esgs_ring_size,
  1992. struct radeon_winsys_bo *gsvs_ring_bo,
  1993. uint32_t gsvs_ring_size)
  1994. {
  1995. if (!esgs_ring_bo && !gsvs_ring_bo)
  1996. return;
  1997. if (esgs_ring_bo)
  1998. radv_cs_add_buffer(queue->device->ws, cs, esgs_ring_bo);
  1999. if (gsvs_ring_bo)
  2000. radv_cs_add_buffer(queue->device->ws, cs, gsvs_ring_bo);
  2001. if (queue->device->physical_device->rad_info.chip_class >= CIK) {
  2002. radeon_set_uconfig_reg_seq(cs, R_030900_VGT_ESGS_RING_SIZE, 2);
  2003. radeon_emit(cs, esgs_ring_size >> 8);
  2004. radeon_emit(cs, gsvs_ring_size >> 8);
  2005. } else {
  2006. radeon_set_config_reg_seq(cs, R_0088C8_VGT_ESGS_RING_SIZE, 2);
  2007. radeon_emit(cs, esgs_ring_size >> 8);
  2008. radeon_emit(cs, gsvs_ring_size >> 8);
  2009. }
  2010. }
  2011. static void
  2012. radv_emit_tess_factor_ring(struct radv_queue *queue, struct radeon_cmdbuf *cs,
  2013. unsigned hs_offchip_param, unsigned tf_ring_size,
  2014. struct radeon_winsys_bo *tess_rings_bo)
  2015. {
  2016. uint64_t tf_va;
  2017. if (!tess_rings_bo)
  2018. return;
  2019. tf_va = radv_buffer_get_va(tess_rings_bo);
  2020. radv_cs_add_buffer(queue->device->ws, cs, tess_rings_bo);
  2021. if (queue->device->physical_device->rad_info.chip_class >= CIK) {
  2022. radeon_set_uconfig_reg(cs, R_030938_VGT_TF_RING_SIZE,
  2023. S_030938_SIZE(tf_ring_size / 4));
  2024. radeon_set_uconfig_reg(cs, R_030940_VGT_TF_MEMORY_BASE,
  2025. tf_va >> 8);
  2026. if (queue->device->physical_device->rad_info.chip_class >= GFX9) {
  2027. radeon_set_uconfig_reg(cs, R_030944_VGT_TF_MEMORY_BASE_HI,
  2028. S_030944_BASE_HI(tf_va >> 40));
  2029. }
  2030. radeon_set_uconfig_reg(cs, R_03093C_VGT_HS_OFFCHIP_PARAM,
  2031. hs_offchip_param);
  2032. } else {
  2033. radeon_set_config_reg(cs, R_008988_VGT_TF_RING_SIZE,
  2034. S_008988_SIZE(tf_ring_size / 4));
  2035. radeon_set_config_reg(cs, R_0089B8_VGT_TF_MEMORY_BASE,
  2036. tf_va >> 8);
  2037. radeon_set_config_reg(cs, R_0089B0_VGT_HS_OFFCHIP_PARAM,
  2038. hs_offchip_param);
  2039. }
  2040. }
  2041. static void
  2042. radv_emit_compute_scratch(struct radv_queue *queue, struct radeon_cmdbuf *cs,
  2043. struct radeon_winsys_bo *compute_scratch_bo)
  2044. {
  2045. uint64_t scratch_va;
  2046. if (!compute_scratch_bo)
  2047. return;
  2048. scratch_va = radv_buffer_get_va(compute_scratch_bo);
  2049. radv_cs_add_buffer(queue->device->ws, cs, compute_scratch_bo);
  2050. radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0, 2);
  2051. radeon_emit(cs, scratch_va);
  2052. radeon_emit(cs, S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
  2053. S_008F04_SWIZZLE_ENABLE(1));
  2054. }
  2055. static void
  2056. radv_emit_global_shader_pointers(struct radv_queue *queue,
  2057. struct radeon_cmdbuf *cs,
  2058. struct radeon_winsys_bo *descriptor_bo)
  2059. {
  2060. uint64_t va;
  2061. if (!descriptor_bo)
  2062. return;
  2063. va = radv_buffer_get_va(descriptor_bo);
  2064. radv_cs_add_buffer(queue->device->ws, cs, descriptor_bo);
  2065. if (queue->device->physical_device->rad_info.chip_class >= GFX9) {
  2066. uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
  2067. R_00B130_SPI_SHADER_USER_DATA_VS_0,
  2068. R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS,
  2069. R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS};
  2070. for (int i = 0; i < ARRAY_SIZE(regs); ++i) {
  2071. radv_emit_shader_pointer(queue->device, cs, regs[i],
  2072. va, true);
  2073. }
  2074. } else {
  2075. uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
  2076. R_00B130_SPI_SHADER_USER_DATA_VS_0,
  2077. R_00B230_SPI_SHADER_USER_DATA_GS_0,
  2078. R_00B330_SPI_SHADER_USER_DATA_ES_0,
  2079. R_00B430_SPI_SHADER_USER_DATA_HS_0,
  2080. R_00B530_SPI_SHADER_USER_DATA_LS_0};
  2081. for (int i = 0; i < ARRAY_SIZE(regs); ++i) {
  2082. radv_emit_shader_pointer(queue->device, cs, regs[i],
  2083. va, true);
  2084. }
  2085. }
  2086. }
  2087. static void
  2088. radv_init_graphics_state(struct radeon_cmdbuf *cs, struct radv_queue *queue)
  2089. {
  2090. struct radv_device *device = queue->device;
  2091. if (device->gfx_init) {
  2092. uint64_t va = radv_buffer_get_va(device->gfx_init);
  2093. radeon_emit(cs, PKT3(PKT3_INDIRECT_BUFFER_CIK, 2, 0));
  2094. radeon_emit(cs, va);
  2095. radeon_emit(cs, va >> 32);
  2096. radeon_emit(cs, device->gfx_init_size_dw & 0xffff);
  2097. radv_cs_add_buffer(device->ws, cs, device->gfx_init);
  2098. } else {
  2099. struct radv_physical_device *physical_device = device->physical_device;
  2100. si_emit_graphics(physical_device, cs);
  2101. }
  2102. }
  2103. static void
  2104. radv_init_compute_state(struct radeon_cmdbuf *cs, struct radv_queue *queue)
  2105. {
  2106. struct radv_physical_device *physical_device = queue->device->physical_device;
  2107. si_emit_compute(physical_device, cs);
  2108. }
  2109. static VkResult
  2110. radv_get_preamble_cs(struct radv_queue *queue,
  2111. uint32_t scratch_size,
  2112. uint32_t compute_scratch_size,
  2113. uint32_t esgs_ring_size,
  2114. uint32_t gsvs_ring_size,
  2115. bool needs_tess_rings,
  2116. bool needs_sample_positions,
  2117. struct radeon_cmdbuf **initial_full_flush_preamble_cs,
  2118. struct radeon_cmdbuf **initial_preamble_cs,
  2119. struct radeon_cmdbuf **continue_preamble_cs)
  2120. {
  2121. struct radeon_winsys_bo *scratch_bo = NULL;
  2122. struct radeon_winsys_bo *descriptor_bo = NULL;
  2123. struct radeon_winsys_bo *compute_scratch_bo = NULL;
  2124. struct radeon_winsys_bo *esgs_ring_bo = NULL;
  2125. struct radeon_winsys_bo *gsvs_ring_bo = NULL;
  2126. struct radeon_winsys_bo *tess_rings_bo = NULL;
  2127. struct radeon_cmdbuf *dest_cs[3] = {0};
  2128. bool add_tess_rings = false, add_sample_positions = false;
  2129. unsigned tess_factor_ring_size = 0, tess_offchip_ring_size = 0;
  2130. unsigned max_offchip_buffers;
  2131. unsigned hs_offchip_param = 0;
  2132. unsigned tess_offchip_ring_offset;
  2133. uint32_t ring_bo_flags = RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING;
  2134. if (!queue->has_tess_rings) {
  2135. if (needs_tess_rings)
  2136. add_tess_rings = true;
  2137. }
  2138. if (!queue->has_sample_positions) {
  2139. if (needs_sample_positions)
  2140. add_sample_positions = true;
  2141. }
  2142. tess_factor_ring_size = 32768 * queue->device->physical_device->rad_info.max_se;
  2143. hs_offchip_param = radv_get_hs_offchip_param(queue->device,
  2144. &max_offchip_buffers);
  2145. tess_offchip_ring_offset = align(tess_factor_ring_size, 64 * 1024);
  2146. tess_offchip_ring_size = max_offchip_buffers *
  2147. queue->device->tess_offchip_block_dw_size * 4;
  2148. if (scratch_size <= queue->scratch_size &&
  2149. compute_scratch_size <= queue->compute_scratch_size &&
  2150. esgs_ring_size <= queue->esgs_ring_size &&
  2151. gsvs_ring_size <= queue->gsvs_ring_size &&
  2152. !add_tess_rings && !add_sample_positions &&
  2153. queue->initial_preamble_cs) {
  2154. *initial_full_flush_preamble_cs = queue->initial_full_flush_preamble_cs;
  2155. *initial_preamble_cs = queue->initial_preamble_cs;
  2156. *continue_preamble_cs = queue->continue_preamble_cs;
  2157. if (!scratch_size && !compute_scratch_size && !esgs_ring_size && !gsvs_ring_size)
  2158. *continue_preamble_cs = NULL;
  2159. return VK_SUCCESS;
  2160. }
  2161. if (scratch_size > queue->scratch_size) {
  2162. scratch_bo = queue->device->ws->buffer_create(queue->device->ws,
  2163. scratch_size,
  2164. 4096,
  2165. RADEON_DOMAIN_VRAM,
  2166. ring_bo_flags,
  2167. RADV_BO_PRIORITY_SCRATCH);
  2168. if (!scratch_bo)
  2169. goto fail;
  2170. } else
  2171. scratch_bo = queue->scratch_bo;
  2172. if (compute_scratch_size > queue->compute_scratch_size) {
  2173. compute_scratch_bo = queue->device->ws->buffer_create(queue->device->ws,
  2174. compute_scratch_size,
  2175. 4096,
  2176. RADEON_DOMAIN_VRAM,
  2177. ring_bo_flags,
  2178. RADV_BO_PRIORITY_SCRATCH);
  2179. if (!compute_scratch_bo)
  2180. goto fail;
  2181. } else
  2182. compute_scratch_bo = queue->compute_scratch_bo;
  2183. if (esgs_ring_size > queue->esgs_ring_size) {
  2184. esgs_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
  2185. esgs_ring_size,
  2186. 4096,
  2187. RADEON_DOMAIN_VRAM,
  2188. ring_bo_flags,
  2189. RADV_BO_PRIORITY_SCRATCH);
  2190. if (!esgs_ring_bo)
  2191. goto fail;
  2192. } else {
  2193. esgs_ring_bo = queue->esgs_ring_bo;
  2194. esgs_ring_size = queue->esgs_ring_size;
  2195. }
  2196. if (gsvs_ring_size > queue->gsvs_ring_size) {
  2197. gsvs_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
  2198. gsvs_ring_size,
  2199. 4096,
  2200. RADEON_DOMAIN_VRAM,
  2201. ring_bo_flags,
  2202. RADV_BO_PRIORITY_SCRATCH);
  2203. if (!gsvs_ring_bo)
  2204. goto fail;
  2205. } else {
  2206. gsvs_ring_bo = queue->gsvs_ring_bo;
  2207. gsvs_ring_size = queue->gsvs_ring_size;
  2208. }
  2209. if (add_tess_rings) {
  2210. tess_rings_bo = queue->device->ws->buffer_create(queue->device->ws,
  2211. tess_offchip_ring_offset + tess_offchip_ring_size,
  2212. 256,
  2213. RADEON_DOMAIN_VRAM,
  2214. ring_bo_flags,
  2215. RADV_BO_PRIORITY_SCRATCH);
  2216. if (!tess_rings_bo)
  2217. goto fail;
  2218. } else {
  2219. tess_rings_bo = queue->tess_rings_bo;
  2220. }
  2221. if (scratch_bo != queue->scratch_bo ||
  2222. esgs_ring_bo != queue->esgs_ring_bo ||
  2223. gsvs_ring_bo != queue->gsvs_ring_bo ||
  2224. tess_rings_bo != queue->tess_rings_bo ||
  2225. add_sample_positions) {
  2226. uint32_t size = 0;
  2227. if (gsvs_ring_bo || esgs_ring_bo ||
  2228. tess_rings_bo || add_sample_positions) {
  2229. size = 112; /* 2 dword + 2 padding + 4 dword * 6 */
  2230. if (add_sample_positions)
  2231. size += 128; /* 64+32+16+8 = 120 bytes */
  2232. }
  2233. else if (scratch_bo)
  2234. size = 8; /* 2 dword */
  2235. descriptor_bo = queue->device->ws->buffer_create(queue->device->ws,
  2236. size,
  2237. 4096,
  2238. RADEON_DOMAIN_VRAM,
  2239. RADEON_FLAG_CPU_ACCESS |
  2240. RADEON_FLAG_NO_INTERPROCESS_SHARING |
  2241. RADEON_FLAG_READ_ONLY,
  2242. RADV_BO_PRIORITY_DESCRIPTOR);
  2243. if (!descriptor_bo)
  2244. goto fail;
  2245. } else
  2246. descriptor_bo = queue->descriptor_bo;
  2247. if (descriptor_bo != queue->descriptor_bo) {
  2248. uint32_t *map = (uint32_t*)queue->device->ws->buffer_map(descriptor_bo);
  2249. if (scratch_bo) {
  2250. uint64_t scratch_va = radv_buffer_get_va(scratch_bo);
  2251. uint32_t rsrc1 = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
  2252. S_008F04_SWIZZLE_ENABLE(1);
  2253. map[0] = scratch_va;
  2254. map[1] = rsrc1;
  2255. }
  2256. if (esgs_ring_bo || gsvs_ring_bo || tess_rings_bo || add_sample_positions)
  2257. fill_geom_tess_rings(queue, map, add_sample_positions,
  2258. esgs_ring_size, esgs_ring_bo,
  2259. gsvs_ring_size, gsvs_ring_bo,
  2260. tess_factor_ring_size,
  2261. tess_offchip_ring_offset,
  2262. tess_offchip_ring_size,
  2263. tess_rings_bo);
  2264. queue->device->ws->buffer_unmap(descriptor_bo);
  2265. }
  2266. for(int i = 0; i < 3; ++i) {
  2267. struct radeon_cmdbuf *cs = NULL;
  2268. cs = queue->device->ws->cs_create(queue->device->ws,
  2269. queue->queue_family_index ? RING_COMPUTE : RING_GFX);
  2270. if (!cs)
  2271. goto fail;
  2272. dest_cs[i] = cs;
  2273. if (scratch_bo)
  2274. radv_cs_add_buffer(queue->device->ws, cs, scratch_bo);
  2275. /* Emit initial configuration. */
  2276. switch (queue->queue_family_index) {
  2277. case RADV_QUEUE_GENERAL:
  2278. radv_init_graphics_state(cs, queue);
  2279. break;
  2280. case RADV_QUEUE_COMPUTE:
  2281. radv_init_compute_state(cs, queue);
  2282. break;
  2283. case RADV_QUEUE_TRANSFER:
  2284. break;
  2285. }
  2286. if (esgs_ring_bo || gsvs_ring_bo || tess_rings_bo) {
  2287. radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
  2288. radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
  2289. radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
  2290. radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
  2291. }
  2292. radv_emit_gs_ring_sizes(queue, cs, esgs_ring_bo, esgs_ring_size,
  2293. gsvs_ring_bo, gsvs_ring_size);
  2294. radv_emit_tess_factor_ring(queue, cs, hs_offchip_param,
  2295. tess_factor_ring_size, tess_rings_bo);
  2296. radv_emit_global_shader_pointers(queue, cs, descriptor_bo);
  2297. radv_emit_compute_scratch(queue, cs, compute_scratch_bo);
  2298. if (i == 0) {
  2299. si_cs_emit_cache_flush(cs,
  2300. queue->device->physical_device->rad_info.chip_class,
  2301. NULL, 0,
  2302. queue->queue_family_index == RING_COMPUTE &&
  2303. queue->device->physical_device->rad_info.chip_class >= CIK,
  2304. (queue->queue_family_index == RADV_QUEUE_COMPUTE ? RADV_CMD_FLAG_CS_PARTIAL_FLUSH : (RADV_CMD_FLAG_CS_PARTIAL_FLUSH | RADV_CMD_FLAG_PS_PARTIAL_FLUSH)) |
  2305. RADV_CMD_FLAG_INV_ICACHE |
  2306. RADV_CMD_FLAG_INV_SMEM_L1 |
  2307. RADV_CMD_FLAG_INV_VMEM_L1 |
  2308. RADV_CMD_FLAG_INV_GLOBAL_L2 |
  2309. RADV_CMD_FLAG_START_PIPELINE_STATS, 0);
  2310. } else if (i == 1) {
  2311. si_cs_emit_cache_flush(cs,
  2312. queue->device->physical_device->rad_info.chip_class,
  2313. NULL, 0,
  2314. queue->queue_family_index == RING_COMPUTE &&
  2315. queue->device->physical_device->rad_info.chip_class >= CIK,
  2316. RADV_CMD_FLAG_INV_ICACHE |
  2317. RADV_CMD_FLAG_INV_SMEM_L1 |
  2318. RADV_CMD_FLAG_INV_VMEM_L1 |
  2319. RADV_CMD_FLAG_INV_GLOBAL_L2 |
  2320. RADV_CMD_FLAG_START_PIPELINE_STATS, 0);
  2321. }
  2322. if (!queue->device->ws->cs_finalize(cs))
  2323. goto fail;
  2324. }
  2325. if (queue->initial_full_flush_preamble_cs)
  2326. queue->device->ws->cs_destroy(queue->initial_full_flush_preamble_cs);
  2327. if (queue->initial_preamble_cs)
  2328. queue->device->ws->cs_destroy(queue->initial_preamble_cs);
  2329. if (queue->continue_preamble_cs)
  2330. queue->device->ws->cs_destroy(queue->continue_preamble_cs);
  2331. queue->initial_full_flush_preamble_cs = dest_cs[0];
  2332. queue->initial_preamble_cs = dest_cs[1];
  2333. queue->continue_preamble_cs = dest_cs[2];
  2334. if (scratch_bo != queue->scratch_bo) {
  2335. if (queue->scratch_bo)
  2336. queue->device->ws->buffer_destroy(queue->scratch_bo);
  2337. queue->scratch_bo = scratch_bo;
  2338. queue->scratch_size = scratch_size;
  2339. }
  2340. if (compute_scratch_bo != queue->compute_scratch_bo) {
  2341. if (queue->compute_scratch_bo)
  2342. queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
  2343. queue->compute_scratch_bo = compute_scratch_bo;
  2344. queue->compute_scratch_size = compute_scratch_size;
  2345. }
  2346. if (esgs_ring_bo != queue->esgs_ring_bo) {
  2347. if (queue->esgs_ring_bo)
  2348. queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
  2349. queue->esgs_ring_bo = esgs_ring_bo;
  2350. queue->esgs_ring_size = esgs_ring_size;
  2351. }
  2352. if (gsvs_ring_bo != queue->gsvs_ring_bo) {
  2353. if (queue->gsvs_ring_bo)
  2354. queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
  2355. queue->gsvs_ring_bo = gsvs_ring_bo;
  2356. queue->gsvs_ring_size = gsvs_ring_size;
  2357. }
  2358. if (tess_rings_bo != queue->tess_rings_bo) {
  2359. queue->tess_rings_bo = tess_rings_bo;
  2360. queue->has_tess_rings = true;
  2361. }
  2362. if (descriptor_bo != queue->descriptor_bo) {
  2363. if (queue->descriptor_bo)
  2364. queue->device->ws->buffer_destroy(queue->descriptor_bo);
  2365. queue->descriptor_bo = descriptor_bo;
  2366. }
  2367. if (add_sample_positions)
  2368. queue->has_sample_positions = true;
  2369. *initial_full_flush_preamble_cs = queue->initial_full_flush_preamble_cs;
  2370. *initial_preamble_cs = queue->initial_preamble_cs;
  2371. *continue_preamble_cs = queue->continue_preamble_cs;
  2372. if (!scratch_size && !compute_scratch_size && !esgs_ring_size && !gsvs_ring_size)
  2373. *continue_preamble_cs = NULL;
  2374. return VK_SUCCESS;
  2375. fail:
  2376. for (int i = 0; i < ARRAY_SIZE(dest_cs); ++i)
  2377. if (dest_cs[i])
  2378. queue->device->ws->cs_destroy(dest_cs[i]);
  2379. if (descriptor_bo && descriptor_bo != queue->descriptor_bo)
  2380. queue->device->ws->buffer_destroy(descriptor_bo);
  2381. if (scratch_bo && scratch_bo != queue->scratch_bo)
  2382. queue->device->ws->buffer_destroy(scratch_bo);
  2383. if (compute_scratch_bo && compute_scratch_bo != queue->compute_scratch_bo)
  2384. queue->device->ws->buffer_destroy(compute_scratch_bo);
  2385. if (esgs_ring_bo && esgs_ring_bo != queue->esgs_ring_bo)
  2386. queue->device->ws->buffer_destroy(esgs_ring_bo);
  2387. if (gsvs_ring_bo && gsvs_ring_bo != queue->gsvs_ring_bo)
  2388. queue->device->ws->buffer_destroy(gsvs_ring_bo);
  2389. if (tess_rings_bo && tess_rings_bo != queue->tess_rings_bo)
  2390. queue->device->ws->buffer_destroy(tess_rings_bo);
  2391. return vk_error(queue->device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
  2392. }
  2393. static VkResult radv_alloc_sem_counts(struct radv_instance *instance,
  2394. struct radv_winsys_sem_counts *counts,
  2395. int num_sems,
  2396. const VkSemaphore *sems,
  2397. VkFence _fence,
  2398. bool reset_temp)
  2399. {
  2400. int syncobj_idx = 0, sem_idx = 0;
  2401. if (num_sems == 0 && _fence == VK_NULL_HANDLE)
  2402. return VK_SUCCESS;
  2403. for (uint32_t i = 0; i < num_sems; i++) {
  2404. RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
  2405. if (sem->temp_syncobj || sem->syncobj)
  2406. counts->syncobj_count++;
  2407. else
  2408. counts->sem_count++;
  2409. }
  2410. if (_fence != VK_NULL_HANDLE) {
  2411. RADV_FROM_HANDLE(radv_fence, fence, _fence);
  2412. if (fence->temp_syncobj || fence->syncobj)
  2413. counts->syncobj_count++;
  2414. }
  2415. if (counts->syncobj_count) {
  2416. counts->syncobj = (uint32_t *)malloc(sizeof(uint32_t) * counts->syncobj_count);
  2417. if (!counts->syncobj)
  2418. return vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  2419. }
  2420. if (counts->sem_count) {
  2421. counts->sem = (struct radeon_winsys_sem **)malloc(sizeof(struct radeon_winsys_sem *) * counts->sem_count);
  2422. if (!counts->sem) {
  2423. free(counts->syncobj);
  2424. return vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  2425. }
  2426. }
  2427. for (uint32_t i = 0; i < num_sems; i++) {
  2428. RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
  2429. if (sem->temp_syncobj) {
  2430. counts->syncobj[syncobj_idx++] = sem->temp_syncobj;
  2431. }
  2432. else if (sem->syncobj)
  2433. counts->syncobj[syncobj_idx++] = sem->syncobj;
  2434. else {
  2435. assert(sem->sem);
  2436. counts->sem[sem_idx++] = sem->sem;
  2437. }
  2438. }
  2439. if (_fence != VK_NULL_HANDLE) {
  2440. RADV_FROM_HANDLE(radv_fence, fence, _fence);
  2441. if (fence->temp_syncobj)
  2442. counts->syncobj[syncobj_idx++] = fence->temp_syncobj;
  2443. else if (fence->syncobj)
  2444. counts->syncobj[syncobj_idx++] = fence->syncobj;
  2445. }
  2446. return VK_SUCCESS;
  2447. }
  2448. static void
  2449. radv_free_sem_info(struct radv_winsys_sem_info *sem_info)
  2450. {
  2451. free(sem_info->wait.syncobj);
  2452. free(sem_info->wait.sem);
  2453. free(sem_info->signal.syncobj);
  2454. free(sem_info->signal.sem);
  2455. }
  2456. static void radv_free_temp_syncobjs(struct radv_device *device,
  2457. int num_sems,
  2458. const VkSemaphore *sems)
  2459. {
  2460. for (uint32_t i = 0; i < num_sems; i++) {
  2461. RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
  2462. if (sem->temp_syncobj) {
  2463. device->ws->destroy_syncobj(device->ws, sem->temp_syncobj);
  2464. sem->temp_syncobj = 0;
  2465. }
  2466. }
  2467. }
  2468. static VkResult
  2469. radv_alloc_sem_info(struct radv_instance *instance,
  2470. struct radv_winsys_sem_info *sem_info,
  2471. int num_wait_sems,
  2472. const VkSemaphore *wait_sems,
  2473. int num_signal_sems,
  2474. const VkSemaphore *signal_sems,
  2475. VkFence fence)
  2476. {
  2477. VkResult ret;
  2478. memset(sem_info, 0, sizeof(*sem_info));
  2479. ret = radv_alloc_sem_counts(instance, &sem_info->wait, num_wait_sems, wait_sems, VK_NULL_HANDLE, true);
  2480. if (ret)
  2481. return ret;
  2482. ret = radv_alloc_sem_counts(instance, &sem_info->signal, num_signal_sems, signal_sems, fence, false);
  2483. if (ret)
  2484. radv_free_sem_info(sem_info);
  2485. /* caller can override these */
  2486. sem_info->cs_emit_wait = true;
  2487. sem_info->cs_emit_signal = true;
  2488. return ret;
  2489. }
  2490. /* Signals fence as soon as all the work currently put on queue is done. */
  2491. static VkResult radv_signal_fence(struct radv_queue *queue,
  2492. struct radv_fence *fence)
  2493. {
  2494. int ret;
  2495. VkResult result;
  2496. struct radv_winsys_sem_info sem_info;
  2497. result = radv_alloc_sem_info(queue->device->instance, &sem_info, 0, NULL, 0, NULL,
  2498. radv_fence_to_handle(fence));
  2499. if (result != VK_SUCCESS)
  2500. return result;
  2501. ret = queue->device->ws->cs_submit(queue->hw_ctx, queue->queue_idx,
  2502. &queue->device->empty_cs[queue->queue_family_index],
  2503. 1, NULL, NULL, &sem_info, NULL,
  2504. false, fence->fence);
  2505. radv_free_sem_info(&sem_info);
  2506. if (ret)
  2507. return vk_error(queue->device->instance, VK_ERROR_DEVICE_LOST);
  2508. return VK_SUCCESS;
  2509. }
  2510. VkResult radv_QueueSubmit(
  2511. VkQueue _queue,
  2512. uint32_t submitCount,
  2513. const VkSubmitInfo* pSubmits,
  2514. VkFence _fence)
  2515. {
  2516. RADV_FROM_HANDLE(radv_queue, queue, _queue);
  2517. RADV_FROM_HANDLE(radv_fence, fence, _fence);
  2518. struct radeon_winsys_fence *base_fence = fence ? fence->fence : NULL;
  2519. struct radeon_winsys_ctx *ctx = queue->hw_ctx;
  2520. int ret;
  2521. uint32_t max_cs_submission = queue->device->trace_bo ? 1 : RADV_MAX_IBS_PER_SUBMIT;
  2522. uint32_t scratch_size = 0;
  2523. uint32_t compute_scratch_size = 0;
  2524. uint32_t esgs_ring_size = 0, gsvs_ring_size = 0;
  2525. struct radeon_cmdbuf *initial_preamble_cs = NULL, *initial_flush_preamble_cs = NULL, *continue_preamble_cs = NULL;
  2526. VkResult result;
  2527. bool fence_emitted = false;
  2528. bool tess_rings_needed = false;
  2529. bool sample_positions_needed = false;
  2530. /* Do this first so failing to allocate scratch buffers can't result in
  2531. * partially executed submissions. */
  2532. for (uint32_t i = 0; i < submitCount; i++) {
  2533. for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
  2534. RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
  2535. pSubmits[i].pCommandBuffers[j]);
  2536. scratch_size = MAX2(scratch_size, cmd_buffer->scratch_size_needed);
  2537. compute_scratch_size = MAX2(compute_scratch_size,
  2538. cmd_buffer->compute_scratch_size_needed);
  2539. esgs_ring_size = MAX2(esgs_ring_size, cmd_buffer->esgs_ring_size_needed);
  2540. gsvs_ring_size = MAX2(gsvs_ring_size, cmd_buffer->gsvs_ring_size_needed);
  2541. tess_rings_needed |= cmd_buffer->tess_rings_needed;
  2542. sample_positions_needed |= cmd_buffer->sample_positions_needed;
  2543. }
  2544. }
  2545. result = radv_get_preamble_cs(queue, scratch_size, compute_scratch_size,
  2546. esgs_ring_size, gsvs_ring_size, tess_rings_needed,
  2547. sample_positions_needed, &initial_flush_preamble_cs,
  2548. &initial_preamble_cs, &continue_preamble_cs);
  2549. if (result != VK_SUCCESS)
  2550. return result;
  2551. for (uint32_t i = 0; i < submitCount; i++) {
  2552. struct radeon_cmdbuf **cs_array;
  2553. bool do_flush = !i || pSubmits[i].pWaitDstStageMask;
  2554. bool can_patch = true;
  2555. uint32_t advance;
  2556. struct radv_winsys_sem_info sem_info;
  2557. result = radv_alloc_sem_info(queue->device->instance,
  2558. &sem_info,
  2559. pSubmits[i].waitSemaphoreCount,
  2560. pSubmits[i].pWaitSemaphores,
  2561. pSubmits[i].signalSemaphoreCount,
  2562. pSubmits[i].pSignalSemaphores,
  2563. _fence);
  2564. if (result != VK_SUCCESS)
  2565. return result;
  2566. if (!pSubmits[i].commandBufferCount) {
  2567. if (pSubmits[i].waitSemaphoreCount || pSubmits[i].signalSemaphoreCount) {
  2568. ret = queue->device->ws->cs_submit(ctx, queue->queue_idx,
  2569. &queue->device->empty_cs[queue->queue_family_index],
  2570. 1, NULL, NULL,
  2571. &sem_info, NULL,
  2572. false, base_fence);
  2573. if (ret) {
  2574. radv_loge("failed to submit CS %d\n", i);
  2575. abort();
  2576. }
  2577. fence_emitted = true;
  2578. }
  2579. radv_free_sem_info(&sem_info);
  2580. continue;
  2581. }
  2582. cs_array = malloc(sizeof(struct radeon_cmdbuf *) *
  2583. (pSubmits[i].commandBufferCount));
  2584. for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
  2585. RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
  2586. pSubmits[i].pCommandBuffers[j]);
  2587. assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
  2588. cs_array[j] = cmd_buffer->cs;
  2589. if ((cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT))
  2590. can_patch = false;
  2591. cmd_buffer->status = RADV_CMD_BUFFER_STATUS_PENDING;
  2592. }
  2593. for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j += advance) {
  2594. struct radeon_cmdbuf *initial_preamble = (do_flush && !j) ? initial_flush_preamble_cs : initial_preamble_cs;
  2595. const struct radv_winsys_bo_list *bo_list = NULL;
  2596. advance = MIN2(max_cs_submission,
  2597. pSubmits[i].commandBufferCount - j);
  2598. if (queue->device->trace_bo)
  2599. *queue->device->trace_id_ptr = 0;
  2600. sem_info.cs_emit_wait = j == 0;
  2601. sem_info.cs_emit_signal = j + advance == pSubmits[i].commandBufferCount;
  2602. if (unlikely(queue->device->use_global_bo_list)) {
  2603. pthread_mutex_lock(&queue->device->bo_list.mutex);
  2604. bo_list = &queue->device->bo_list.list;
  2605. }
  2606. ret = queue->device->ws->cs_submit(ctx, queue->queue_idx, cs_array + j,
  2607. advance, initial_preamble, continue_preamble_cs,
  2608. &sem_info, bo_list,
  2609. can_patch, base_fence);
  2610. if (unlikely(queue->device->use_global_bo_list))
  2611. pthread_mutex_unlock(&queue->device->bo_list.mutex);
  2612. if (ret) {
  2613. radv_loge("failed to submit CS %d\n", i);
  2614. abort();
  2615. }
  2616. fence_emitted = true;
  2617. if (queue->device->trace_bo) {
  2618. radv_check_gpu_hangs(queue, cs_array[j]);
  2619. }
  2620. }
  2621. radv_free_temp_syncobjs(queue->device,
  2622. pSubmits[i].waitSemaphoreCount,
  2623. pSubmits[i].pWaitSemaphores);
  2624. radv_free_sem_info(&sem_info);
  2625. free(cs_array);
  2626. }
  2627. if (fence) {
  2628. if (!fence_emitted) {
  2629. result = radv_signal_fence(queue, fence);
  2630. if (result != VK_SUCCESS)
  2631. return result;
  2632. }
  2633. fence->submitted = true;
  2634. }
  2635. return VK_SUCCESS;
  2636. }
  2637. VkResult radv_QueueWaitIdle(
  2638. VkQueue _queue)
  2639. {
  2640. RADV_FROM_HANDLE(radv_queue, queue, _queue);
  2641. queue->device->ws->ctx_wait_idle(queue->hw_ctx,
  2642. radv_queue_family_to_ring(queue->queue_family_index),
  2643. queue->queue_idx);
  2644. return VK_SUCCESS;
  2645. }
  2646. VkResult radv_DeviceWaitIdle(
  2647. VkDevice _device)
  2648. {
  2649. RADV_FROM_HANDLE(radv_device, device, _device);
  2650. for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
  2651. for (unsigned q = 0; q < device->queue_count[i]; q++) {
  2652. radv_QueueWaitIdle(radv_queue_to_handle(&device->queues[i][q]));
  2653. }
  2654. }
  2655. return VK_SUCCESS;
  2656. }
  2657. VkResult radv_EnumerateInstanceExtensionProperties(
  2658. const char* pLayerName,
  2659. uint32_t* pPropertyCount,
  2660. VkExtensionProperties* pProperties)
  2661. {
  2662. VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
  2663. for (int i = 0; i < RADV_INSTANCE_EXTENSION_COUNT; i++) {
  2664. if (radv_supported_instance_extensions.extensions[i]) {
  2665. vk_outarray_append(&out, prop) {
  2666. *prop = radv_instance_extensions[i];
  2667. }
  2668. }
  2669. }
  2670. return vk_outarray_status(&out);
  2671. }
  2672. VkResult radv_EnumerateDeviceExtensionProperties(
  2673. VkPhysicalDevice physicalDevice,
  2674. const char* pLayerName,
  2675. uint32_t* pPropertyCount,
  2676. VkExtensionProperties* pProperties)
  2677. {
  2678. RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
  2679. VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
  2680. for (int i = 0; i < RADV_DEVICE_EXTENSION_COUNT; i++) {
  2681. if (device->supported_extensions.extensions[i]) {
  2682. vk_outarray_append(&out, prop) {
  2683. *prop = radv_device_extensions[i];
  2684. }
  2685. }
  2686. }
  2687. return vk_outarray_status(&out);
  2688. }
  2689. PFN_vkVoidFunction radv_GetInstanceProcAddr(
  2690. VkInstance _instance,
  2691. const char* pName)
  2692. {
  2693. RADV_FROM_HANDLE(radv_instance, instance, _instance);
  2694. return radv_lookup_entrypoint_checked(pName,
  2695. instance ? instance->apiVersion : 0,
  2696. instance ? &instance->enabled_extensions : NULL,
  2697. NULL);
  2698. }
  2699. /* The loader wants us to expose a second GetInstanceProcAddr function
  2700. * to work around certain LD_PRELOAD issues seen in apps.
  2701. */
  2702. PUBLIC
  2703. VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
  2704. VkInstance instance,
  2705. const char* pName);
  2706. PUBLIC
  2707. VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
  2708. VkInstance instance,
  2709. const char* pName)
  2710. {
  2711. return radv_GetInstanceProcAddr(instance, pName);
  2712. }
  2713. PFN_vkVoidFunction radv_GetDeviceProcAddr(
  2714. VkDevice _device,
  2715. const char* pName)
  2716. {
  2717. RADV_FROM_HANDLE(radv_device, device, _device);
  2718. return radv_lookup_entrypoint_checked(pName,
  2719. device->instance->apiVersion,
  2720. &device->instance->enabled_extensions,
  2721. &device->enabled_extensions);
  2722. }
  2723. bool radv_get_memory_fd(struct radv_device *device,
  2724. struct radv_device_memory *memory,
  2725. int *pFD)
  2726. {
  2727. struct radeon_bo_metadata metadata;
  2728. if (memory->image) {
  2729. radv_init_metadata(device, memory->image, &metadata);
  2730. device->ws->buffer_set_metadata(memory->bo, &metadata);
  2731. }
  2732. return device->ws->buffer_get_fd(device->ws, memory->bo,
  2733. pFD);
  2734. }
  2735. static VkResult radv_alloc_memory(struct radv_device *device,
  2736. const VkMemoryAllocateInfo* pAllocateInfo,
  2737. const VkAllocationCallbacks* pAllocator,
  2738. VkDeviceMemory* pMem)
  2739. {
  2740. struct radv_device_memory *mem;
  2741. VkResult result;
  2742. enum radeon_bo_domain domain;
  2743. uint32_t flags = 0;
  2744. enum radv_mem_type mem_type_index = device->physical_device->mem_type_indices[pAllocateInfo->memoryTypeIndex];
  2745. assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
  2746. if (pAllocateInfo->allocationSize == 0) {
  2747. /* Apparently, this is allowed */
  2748. *pMem = VK_NULL_HANDLE;
  2749. return VK_SUCCESS;
  2750. }
  2751. const VkImportMemoryFdInfoKHR *import_info =
  2752. vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
  2753. const VkMemoryDedicatedAllocateInfo *dedicate_info =
  2754. vk_find_struct_const(pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO);
  2755. const VkExportMemoryAllocateInfo *export_info =
  2756. vk_find_struct_const(pAllocateInfo->pNext, EXPORT_MEMORY_ALLOCATE_INFO);
  2757. const VkImportMemoryHostPointerInfoEXT *host_ptr_info =
  2758. vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_HOST_POINTER_INFO_EXT);
  2759. const struct wsi_memory_allocate_info *wsi_info =
  2760. vk_find_struct_const(pAllocateInfo->pNext, WSI_MEMORY_ALLOCATE_INFO_MESA);
  2761. mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
  2762. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  2763. if (mem == NULL)
  2764. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  2765. if (wsi_info && wsi_info->implicit_sync)
  2766. flags |= RADEON_FLAG_IMPLICIT_SYNC;
  2767. if (dedicate_info) {
  2768. mem->image = radv_image_from_handle(dedicate_info->image);
  2769. mem->buffer = radv_buffer_from_handle(dedicate_info->buffer);
  2770. } else {
  2771. mem->image = NULL;
  2772. mem->buffer = NULL;
  2773. }
  2774. float priority_float = 0.5;
  2775. const struct VkMemoryPriorityAllocateInfoEXT *priority_ext =
  2776. vk_find_struct_const(pAllocateInfo->pNext,
  2777. MEMORY_PRIORITY_ALLOCATE_INFO_EXT);
  2778. if (priority_ext)
  2779. priority_float = priority_ext->priority;
  2780. unsigned priority = MIN2(RADV_BO_PRIORITY_APPLICATION_MAX - 1,
  2781. (int)(priority_float * RADV_BO_PRIORITY_APPLICATION_MAX));
  2782. mem->user_ptr = NULL;
  2783. if (import_info) {
  2784. assert(import_info->handleType ==
  2785. VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
  2786. import_info->handleType ==
  2787. VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
  2788. mem->bo = device->ws->buffer_from_fd(device->ws, import_info->fd,
  2789. priority, NULL, NULL);
  2790. if (!mem->bo) {
  2791. result = VK_ERROR_INVALID_EXTERNAL_HANDLE;
  2792. goto fail;
  2793. } else {
  2794. close(import_info->fd);
  2795. }
  2796. } else if (host_ptr_info) {
  2797. assert(host_ptr_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT);
  2798. assert(mem_type_index == RADV_MEM_TYPE_GTT_CACHED);
  2799. mem->bo = device->ws->buffer_from_ptr(device->ws, host_ptr_info->pHostPointer,
  2800. pAllocateInfo->allocationSize,
  2801. priority);
  2802. if (!mem->bo) {
  2803. result = VK_ERROR_INVALID_EXTERNAL_HANDLE;
  2804. goto fail;
  2805. } else {
  2806. mem->user_ptr = host_ptr_info->pHostPointer;
  2807. }
  2808. } else {
  2809. uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
  2810. if (mem_type_index == RADV_MEM_TYPE_GTT_WRITE_COMBINE ||
  2811. mem_type_index == RADV_MEM_TYPE_GTT_CACHED)
  2812. domain = RADEON_DOMAIN_GTT;
  2813. else
  2814. domain = RADEON_DOMAIN_VRAM;
  2815. if (mem_type_index == RADV_MEM_TYPE_VRAM)
  2816. flags |= RADEON_FLAG_NO_CPU_ACCESS;
  2817. else
  2818. flags |= RADEON_FLAG_CPU_ACCESS;
  2819. if (mem_type_index == RADV_MEM_TYPE_GTT_WRITE_COMBINE)
  2820. flags |= RADEON_FLAG_GTT_WC;
  2821. if (!dedicate_info && !import_info && (!export_info || !export_info->handleTypes)) {
  2822. flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING;
  2823. if (device->use_global_bo_list) {
  2824. flags |= RADEON_FLAG_PREFER_LOCAL_BO;
  2825. }
  2826. }
  2827. mem->bo = device->ws->buffer_create(device->ws, alloc_size, device->physical_device->rad_info.max_alignment,
  2828. domain, flags, priority);
  2829. if (!mem->bo) {
  2830. result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
  2831. goto fail;
  2832. }
  2833. mem->type_index = mem_type_index;
  2834. }
  2835. result = radv_bo_list_add(device, mem->bo);
  2836. if (result != VK_SUCCESS)
  2837. goto fail_bo;
  2838. *pMem = radv_device_memory_to_handle(mem);
  2839. return VK_SUCCESS;
  2840. fail_bo:
  2841. device->ws->buffer_destroy(mem->bo);
  2842. fail:
  2843. vk_free2(&device->alloc, pAllocator, mem);
  2844. return result;
  2845. }
  2846. VkResult radv_AllocateMemory(
  2847. VkDevice _device,
  2848. const VkMemoryAllocateInfo* pAllocateInfo,
  2849. const VkAllocationCallbacks* pAllocator,
  2850. VkDeviceMemory* pMem)
  2851. {
  2852. RADV_FROM_HANDLE(radv_device, device, _device);
  2853. return radv_alloc_memory(device, pAllocateInfo, pAllocator, pMem);
  2854. }
  2855. void radv_FreeMemory(
  2856. VkDevice _device,
  2857. VkDeviceMemory _mem,
  2858. const VkAllocationCallbacks* pAllocator)
  2859. {
  2860. RADV_FROM_HANDLE(radv_device, device, _device);
  2861. RADV_FROM_HANDLE(radv_device_memory, mem, _mem);
  2862. if (mem == NULL)
  2863. return;
  2864. radv_bo_list_remove(device, mem->bo);
  2865. device->ws->buffer_destroy(mem->bo);
  2866. mem->bo = NULL;
  2867. vk_free2(&device->alloc, pAllocator, mem);
  2868. }
  2869. VkResult radv_MapMemory(
  2870. VkDevice _device,
  2871. VkDeviceMemory _memory,
  2872. VkDeviceSize offset,
  2873. VkDeviceSize size,
  2874. VkMemoryMapFlags flags,
  2875. void** ppData)
  2876. {
  2877. RADV_FROM_HANDLE(radv_device, device, _device);
  2878. RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
  2879. if (mem == NULL) {
  2880. *ppData = NULL;
  2881. return VK_SUCCESS;
  2882. }
  2883. if (mem->user_ptr)
  2884. *ppData = mem->user_ptr;
  2885. else
  2886. *ppData = device->ws->buffer_map(mem->bo);
  2887. if (*ppData) {
  2888. *ppData += offset;
  2889. return VK_SUCCESS;
  2890. }
  2891. return vk_error(device->instance, VK_ERROR_MEMORY_MAP_FAILED);
  2892. }
  2893. void radv_UnmapMemory(
  2894. VkDevice _device,
  2895. VkDeviceMemory _memory)
  2896. {
  2897. RADV_FROM_HANDLE(radv_device, device, _device);
  2898. RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
  2899. if (mem == NULL)
  2900. return;
  2901. if (mem->user_ptr == NULL)
  2902. device->ws->buffer_unmap(mem->bo);
  2903. }
  2904. VkResult radv_FlushMappedMemoryRanges(
  2905. VkDevice _device,
  2906. uint32_t memoryRangeCount,
  2907. const VkMappedMemoryRange* pMemoryRanges)
  2908. {
  2909. return VK_SUCCESS;
  2910. }
  2911. VkResult radv_InvalidateMappedMemoryRanges(
  2912. VkDevice _device,
  2913. uint32_t memoryRangeCount,
  2914. const VkMappedMemoryRange* pMemoryRanges)
  2915. {
  2916. return VK_SUCCESS;
  2917. }
  2918. void radv_GetBufferMemoryRequirements(
  2919. VkDevice _device,
  2920. VkBuffer _buffer,
  2921. VkMemoryRequirements* pMemoryRequirements)
  2922. {
  2923. RADV_FROM_HANDLE(radv_device, device, _device);
  2924. RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
  2925. pMemoryRequirements->memoryTypeBits = (1u << device->physical_device->memory_properties.memoryTypeCount) - 1;
  2926. if (buffer->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
  2927. pMemoryRequirements->alignment = 4096;
  2928. else
  2929. pMemoryRequirements->alignment = 16;
  2930. pMemoryRequirements->size = align64(buffer->size, pMemoryRequirements->alignment);
  2931. }
  2932. void radv_GetBufferMemoryRequirements2(
  2933. VkDevice device,
  2934. const VkBufferMemoryRequirementsInfo2 *pInfo,
  2935. VkMemoryRequirements2 *pMemoryRequirements)
  2936. {
  2937. radv_GetBufferMemoryRequirements(device, pInfo->buffer,
  2938. &pMemoryRequirements->memoryRequirements);
  2939. RADV_FROM_HANDLE(radv_buffer, buffer, pInfo->buffer);
  2940. vk_foreach_struct(ext, pMemoryRequirements->pNext) {
  2941. switch (ext->sType) {
  2942. case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
  2943. VkMemoryDedicatedRequirements *req =
  2944. (VkMemoryDedicatedRequirements *) ext;
  2945. req->requiresDedicatedAllocation = buffer->shareable;
  2946. req->prefersDedicatedAllocation = req->requiresDedicatedAllocation;
  2947. break;
  2948. }
  2949. default:
  2950. break;
  2951. }
  2952. }
  2953. }
  2954. void radv_GetImageMemoryRequirements(
  2955. VkDevice _device,
  2956. VkImage _image,
  2957. VkMemoryRequirements* pMemoryRequirements)
  2958. {
  2959. RADV_FROM_HANDLE(radv_device, device, _device);
  2960. RADV_FROM_HANDLE(radv_image, image, _image);
  2961. pMemoryRequirements->memoryTypeBits = (1u << device->physical_device->memory_properties.memoryTypeCount) - 1;
  2962. pMemoryRequirements->size = image->size;
  2963. pMemoryRequirements->alignment = image->alignment;
  2964. }
  2965. void radv_GetImageMemoryRequirements2(
  2966. VkDevice device,
  2967. const VkImageMemoryRequirementsInfo2 *pInfo,
  2968. VkMemoryRequirements2 *pMemoryRequirements)
  2969. {
  2970. radv_GetImageMemoryRequirements(device, pInfo->image,
  2971. &pMemoryRequirements->memoryRequirements);
  2972. RADV_FROM_HANDLE(radv_image, image, pInfo->image);
  2973. vk_foreach_struct(ext, pMemoryRequirements->pNext) {
  2974. switch (ext->sType) {
  2975. case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
  2976. VkMemoryDedicatedRequirements *req =
  2977. (VkMemoryDedicatedRequirements *) ext;
  2978. req->requiresDedicatedAllocation = image->shareable;
  2979. req->prefersDedicatedAllocation = req->requiresDedicatedAllocation;
  2980. break;
  2981. }
  2982. default:
  2983. break;
  2984. }
  2985. }
  2986. }
  2987. void radv_GetImageSparseMemoryRequirements(
  2988. VkDevice device,
  2989. VkImage image,
  2990. uint32_t* pSparseMemoryRequirementCount,
  2991. VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
  2992. {
  2993. stub();
  2994. }
  2995. void radv_GetImageSparseMemoryRequirements2(
  2996. VkDevice device,
  2997. const VkImageSparseMemoryRequirementsInfo2 *pInfo,
  2998. uint32_t* pSparseMemoryRequirementCount,
  2999. VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
  3000. {
  3001. stub();
  3002. }
  3003. void radv_GetDeviceMemoryCommitment(
  3004. VkDevice device,
  3005. VkDeviceMemory memory,
  3006. VkDeviceSize* pCommittedMemoryInBytes)
  3007. {
  3008. *pCommittedMemoryInBytes = 0;
  3009. }
  3010. VkResult radv_BindBufferMemory2(VkDevice device,
  3011. uint32_t bindInfoCount,
  3012. const VkBindBufferMemoryInfo *pBindInfos)
  3013. {
  3014. for (uint32_t i = 0; i < bindInfoCount; ++i) {
  3015. RADV_FROM_HANDLE(radv_device_memory, mem, pBindInfos[i].memory);
  3016. RADV_FROM_HANDLE(radv_buffer, buffer, pBindInfos[i].buffer);
  3017. if (mem) {
  3018. buffer->bo = mem->bo;
  3019. buffer->offset = pBindInfos[i].memoryOffset;
  3020. } else {
  3021. buffer->bo = NULL;
  3022. }
  3023. }
  3024. return VK_SUCCESS;
  3025. }
  3026. VkResult radv_BindBufferMemory(
  3027. VkDevice device,
  3028. VkBuffer buffer,
  3029. VkDeviceMemory memory,
  3030. VkDeviceSize memoryOffset)
  3031. {
  3032. const VkBindBufferMemoryInfo info = {
  3033. .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
  3034. .buffer = buffer,
  3035. .memory = memory,
  3036. .memoryOffset = memoryOffset
  3037. };
  3038. return radv_BindBufferMemory2(device, 1, &info);
  3039. }
  3040. VkResult radv_BindImageMemory2(VkDevice device,
  3041. uint32_t bindInfoCount,
  3042. const VkBindImageMemoryInfo *pBindInfos)
  3043. {
  3044. for (uint32_t i = 0; i < bindInfoCount; ++i) {
  3045. RADV_FROM_HANDLE(radv_device_memory, mem, pBindInfos[i].memory);
  3046. RADV_FROM_HANDLE(radv_image, image, pBindInfos[i].image);
  3047. if (mem) {
  3048. image->bo = mem->bo;
  3049. image->offset = pBindInfos[i].memoryOffset;
  3050. } else {
  3051. image->bo = NULL;
  3052. image->offset = 0;
  3053. }
  3054. }
  3055. return VK_SUCCESS;
  3056. }
  3057. VkResult radv_BindImageMemory(
  3058. VkDevice device,
  3059. VkImage image,
  3060. VkDeviceMemory memory,
  3061. VkDeviceSize memoryOffset)
  3062. {
  3063. const VkBindImageMemoryInfo info = {
  3064. .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
  3065. .image = image,
  3066. .memory = memory,
  3067. .memoryOffset = memoryOffset
  3068. };
  3069. return radv_BindImageMemory2(device, 1, &info);
  3070. }
  3071. static void
  3072. radv_sparse_buffer_bind_memory(struct radv_device *device,
  3073. const VkSparseBufferMemoryBindInfo *bind)
  3074. {
  3075. RADV_FROM_HANDLE(radv_buffer, buffer, bind->buffer);
  3076. for (uint32_t i = 0; i < bind->bindCount; ++i) {
  3077. struct radv_device_memory *mem = NULL;
  3078. if (bind->pBinds[i].memory != VK_NULL_HANDLE)
  3079. mem = radv_device_memory_from_handle(bind->pBinds[i].memory);
  3080. device->ws->buffer_virtual_bind(buffer->bo,
  3081. bind->pBinds[i].resourceOffset,
  3082. bind->pBinds[i].size,
  3083. mem ? mem->bo : NULL,
  3084. bind->pBinds[i].memoryOffset);
  3085. }
  3086. }
  3087. static void
  3088. radv_sparse_image_opaque_bind_memory(struct radv_device *device,
  3089. const VkSparseImageOpaqueMemoryBindInfo *bind)
  3090. {
  3091. RADV_FROM_HANDLE(radv_image, image, bind->image);
  3092. for (uint32_t i = 0; i < bind->bindCount; ++i) {
  3093. struct radv_device_memory *mem = NULL;
  3094. if (bind->pBinds[i].memory != VK_NULL_HANDLE)
  3095. mem = radv_device_memory_from_handle(bind->pBinds[i].memory);
  3096. device->ws->buffer_virtual_bind(image->bo,
  3097. bind->pBinds[i].resourceOffset,
  3098. bind->pBinds[i].size,
  3099. mem ? mem->bo : NULL,
  3100. bind->pBinds[i].memoryOffset);
  3101. }
  3102. }
  3103. VkResult radv_QueueBindSparse(
  3104. VkQueue _queue,
  3105. uint32_t bindInfoCount,
  3106. const VkBindSparseInfo* pBindInfo,
  3107. VkFence _fence)
  3108. {
  3109. RADV_FROM_HANDLE(radv_fence, fence, _fence);
  3110. RADV_FROM_HANDLE(radv_queue, queue, _queue);
  3111. struct radeon_winsys_fence *base_fence = fence ? fence->fence : NULL;
  3112. bool fence_emitted = false;
  3113. VkResult result;
  3114. int ret;
  3115. for (uint32_t i = 0; i < bindInfoCount; ++i) {
  3116. struct radv_winsys_sem_info sem_info;
  3117. for (uint32_t j = 0; j < pBindInfo[i].bufferBindCount; ++j) {
  3118. radv_sparse_buffer_bind_memory(queue->device,
  3119. pBindInfo[i].pBufferBinds + j);
  3120. }
  3121. for (uint32_t j = 0; j < pBindInfo[i].imageOpaqueBindCount; ++j) {
  3122. radv_sparse_image_opaque_bind_memory(queue->device,
  3123. pBindInfo[i].pImageOpaqueBinds + j);
  3124. }
  3125. VkResult result;
  3126. result = radv_alloc_sem_info(queue->device->instance,
  3127. &sem_info,
  3128. pBindInfo[i].waitSemaphoreCount,
  3129. pBindInfo[i].pWaitSemaphores,
  3130. pBindInfo[i].signalSemaphoreCount,
  3131. pBindInfo[i].pSignalSemaphores,
  3132. _fence);
  3133. if (result != VK_SUCCESS)
  3134. return result;
  3135. if (pBindInfo[i].waitSemaphoreCount || pBindInfo[i].signalSemaphoreCount) {
  3136. ret = queue->device->ws->cs_submit(queue->hw_ctx, queue->queue_idx,
  3137. &queue->device->empty_cs[queue->queue_family_index],
  3138. 1, NULL, NULL,
  3139. &sem_info, NULL,
  3140. false, base_fence);
  3141. if (ret) {
  3142. radv_loge("failed to submit CS %d\n", i);
  3143. abort();
  3144. }
  3145. fence_emitted = true;
  3146. if (fence)
  3147. fence->submitted = true;
  3148. }
  3149. radv_free_sem_info(&sem_info);
  3150. }
  3151. if (fence) {
  3152. if (!fence_emitted) {
  3153. result = radv_signal_fence(queue, fence);
  3154. if (result != VK_SUCCESS)
  3155. return result;
  3156. }
  3157. fence->submitted = true;
  3158. }
  3159. return VK_SUCCESS;
  3160. }
  3161. VkResult radv_CreateFence(
  3162. VkDevice _device,
  3163. const VkFenceCreateInfo* pCreateInfo,
  3164. const VkAllocationCallbacks* pAllocator,
  3165. VkFence* pFence)
  3166. {
  3167. RADV_FROM_HANDLE(radv_device, device, _device);
  3168. const VkExportFenceCreateInfo *export =
  3169. vk_find_struct_const(pCreateInfo->pNext, EXPORT_FENCE_CREATE_INFO);
  3170. VkExternalFenceHandleTypeFlags handleTypes =
  3171. export ? export->handleTypes : 0;
  3172. struct radv_fence *fence = vk_alloc2(&device->alloc, pAllocator,
  3173. sizeof(*fence), 8,
  3174. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  3175. if (!fence)
  3176. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3177. fence->fence_wsi = NULL;
  3178. fence->submitted = false;
  3179. fence->signalled = !!(pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT);
  3180. fence->temp_syncobj = 0;
  3181. if (device->always_use_syncobj || handleTypes) {
  3182. int ret = device->ws->create_syncobj(device->ws, &fence->syncobj);
  3183. if (ret) {
  3184. vk_free2(&device->alloc, pAllocator, fence);
  3185. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3186. }
  3187. if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
  3188. device->ws->signal_syncobj(device->ws, fence->syncobj);
  3189. }
  3190. fence->fence = NULL;
  3191. } else {
  3192. fence->fence = device->ws->create_fence();
  3193. if (!fence->fence) {
  3194. vk_free2(&device->alloc, pAllocator, fence);
  3195. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3196. }
  3197. fence->syncobj = 0;
  3198. }
  3199. *pFence = radv_fence_to_handle(fence);
  3200. return VK_SUCCESS;
  3201. }
  3202. void radv_DestroyFence(
  3203. VkDevice _device,
  3204. VkFence _fence,
  3205. const VkAllocationCallbacks* pAllocator)
  3206. {
  3207. RADV_FROM_HANDLE(radv_device, device, _device);
  3208. RADV_FROM_HANDLE(radv_fence, fence, _fence);
  3209. if (!fence)
  3210. return;
  3211. if (fence->temp_syncobj)
  3212. device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
  3213. if (fence->syncobj)
  3214. device->ws->destroy_syncobj(device->ws, fence->syncobj);
  3215. if (fence->fence)
  3216. device->ws->destroy_fence(fence->fence);
  3217. if (fence->fence_wsi)
  3218. fence->fence_wsi->destroy(fence->fence_wsi);
  3219. vk_free2(&device->alloc, pAllocator, fence);
  3220. }
  3221. uint64_t radv_get_current_time(void)
  3222. {
  3223. struct timespec tv;
  3224. clock_gettime(CLOCK_MONOTONIC, &tv);
  3225. return tv.tv_nsec + tv.tv_sec*1000000000ull;
  3226. }
  3227. static uint64_t radv_get_absolute_timeout(uint64_t timeout)
  3228. {
  3229. uint64_t current_time = radv_get_current_time();
  3230. timeout = MIN2(UINT64_MAX - current_time, timeout);
  3231. return current_time + timeout;
  3232. }
  3233. static bool radv_all_fences_plain_and_submitted(uint32_t fenceCount, const VkFence *pFences)
  3234. {
  3235. for (uint32_t i = 0; i < fenceCount; ++i) {
  3236. RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
  3237. if (fence->fence == NULL || fence->syncobj ||
  3238. fence->temp_syncobj ||
  3239. (!fence->signalled && !fence->submitted))
  3240. return false;
  3241. }
  3242. return true;
  3243. }
  3244. static bool radv_all_fences_syncobj(uint32_t fenceCount, const VkFence *pFences)
  3245. {
  3246. for (uint32_t i = 0; i < fenceCount; ++i) {
  3247. RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
  3248. if (fence->syncobj == 0 && fence->temp_syncobj == 0)
  3249. return false;
  3250. }
  3251. return true;
  3252. }
  3253. VkResult radv_WaitForFences(
  3254. VkDevice _device,
  3255. uint32_t fenceCount,
  3256. const VkFence* pFences,
  3257. VkBool32 waitAll,
  3258. uint64_t timeout)
  3259. {
  3260. RADV_FROM_HANDLE(radv_device, device, _device);
  3261. timeout = radv_get_absolute_timeout(timeout);
  3262. if (device->always_use_syncobj &&
  3263. radv_all_fences_syncobj(fenceCount, pFences))
  3264. {
  3265. uint32_t *handles = malloc(sizeof(uint32_t) * fenceCount);
  3266. if (!handles)
  3267. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3268. for (uint32_t i = 0; i < fenceCount; ++i) {
  3269. RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
  3270. handles[i] = fence->temp_syncobj ? fence->temp_syncobj : fence->syncobj;
  3271. }
  3272. bool success = device->ws->wait_syncobj(device->ws, handles, fenceCount, waitAll, timeout);
  3273. free(handles);
  3274. return success ? VK_SUCCESS : VK_TIMEOUT;
  3275. }
  3276. if (!waitAll && fenceCount > 1) {
  3277. /* Not doing this by default for waitAll, due to needing to allocate twice. */
  3278. if (device->physical_device->rad_info.drm_minor >= 10 && radv_all_fences_plain_and_submitted(fenceCount, pFences)) {
  3279. uint32_t wait_count = 0;
  3280. struct radeon_winsys_fence **fences = malloc(sizeof(struct radeon_winsys_fence *) * fenceCount);
  3281. if (!fences)
  3282. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3283. for (uint32_t i = 0; i < fenceCount; ++i) {
  3284. RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
  3285. if (fence->signalled) {
  3286. free(fences);
  3287. return VK_SUCCESS;
  3288. }
  3289. fences[wait_count++] = fence->fence;
  3290. }
  3291. bool success = device->ws->fences_wait(device->ws, fences, wait_count,
  3292. waitAll, timeout - radv_get_current_time());
  3293. free(fences);
  3294. return success ? VK_SUCCESS : VK_TIMEOUT;
  3295. }
  3296. while(radv_get_current_time() <= timeout) {
  3297. for (uint32_t i = 0; i < fenceCount; ++i) {
  3298. if (radv_GetFenceStatus(_device, pFences[i]) == VK_SUCCESS)
  3299. return VK_SUCCESS;
  3300. }
  3301. }
  3302. return VK_TIMEOUT;
  3303. }
  3304. for (uint32_t i = 0; i < fenceCount; ++i) {
  3305. RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
  3306. bool expired = false;
  3307. if (fence->temp_syncobj) {
  3308. if (!device->ws->wait_syncobj(device->ws, &fence->temp_syncobj, 1, true, timeout))
  3309. return VK_TIMEOUT;
  3310. continue;
  3311. }
  3312. if (fence->syncobj) {
  3313. if (!device->ws->wait_syncobj(device->ws, &fence->syncobj, 1, true, timeout))
  3314. return VK_TIMEOUT;
  3315. continue;
  3316. }
  3317. if (fence->signalled)
  3318. continue;
  3319. if (fence->fence) {
  3320. if (!fence->submitted) {
  3321. while(radv_get_current_time() <= timeout &&
  3322. !fence->submitted)
  3323. /* Do nothing */;
  3324. if (!fence->submitted)
  3325. return VK_TIMEOUT;
  3326. /* Recheck as it may have been set by
  3327. * submitting operations. */
  3328. if (fence->signalled)
  3329. continue;
  3330. }
  3331. expired = device->ws->fence_wait(device->ws,
  3332. fence->fence,
  3333. true, timeout);
  3334. if (!expired)
  3335. return VK_TIMEOUT;
  3336. }
  3337. if (fence->fence_wsi) {
  3338. VkResult result = fence->fence_wsi->wait(fence->fence_wsi, timeout);
  3339. if (result != VK_SUCCESS)
  3340. return result;
  3341. }
  3342. fence->signalled = true;
  3343. }
  3344. return VK_SUCCESS;
  3345. }
  3346. VkResult radv_ResetFences(VkDevice _device,
  3347. uint32_t fenceCount,
  3348. const VkFence *pFences)
  3349. {
  3350. RADV_FROM_HANDLE(radv_device, device, _device);
  3351. for (unsigned i = 0; i < fenceCount; ++i) {
  3352. RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
  3353. fence->submitted = fence->signalled = false;
  3354. /* Per spec, we first restore the permanent payload, and then reset, so
  3355. * having a temp syncobj should not skip resetting the permanent syncobj. */
  3356. if (fence->temp_syncobj) {
  3357. device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
  3358. fence->temp_syncobj = 0;
  3359. }
  3360. if (fence->syncobj) {
  3361. device->ws->reset_syncobj(device->ws, fence->syncobj);
  3362. }
  3363. }
  3364. return VK_SUCCESS;
  3365. }
  3366. VkResult radv_GetFenceStatus(VkDevice _device, VkFence _fence)
  3367. {
  3368. RADV_FROM_HANDLE(radv_device, device, _device);
  3369. RADV_FROM_HANDLE(radv_fence, fence, _fence);
  3370. if (fence->temp_syncobj) {
  3371. bool success = device->ws->wait_syncobj(device->ws, &fence->temp_syncobj, 1, true, 0);
  3372. return success ? VK_SUCCESS : VK_NOT_READY;
  3373. }
  3374. if (fence->syncobj) {
  3375. bool success = device->ws->wait_syncobj(device->ws, &fence->syncobj, 1, true, 0);
  3376. return success ? VK_SUCCESS : VK_NOT_READY;
  3377. }
  3378. if (fence->signalled)
  3379. return VK_SUCCESS;
  3380. if (!fence->submitted)
  3381. return VK_NOT_READY;
  3382. if (fence->fence) {
  3383. if (!device->ws->fence_wait(device->ws, fence->fence, false, 0))
  3384. return VK_NOT_READY;
  3385. }
  3386. if (fence->fence_wsi) {
  3387. VkResult result = fence->fence_wsi->wait(fence->fence_wsi, 0);
  3388. if (result != VK_SUCCESS) {
  3389. if (result == VK_TIMEOUT)
  3390. return VK_NOT_READY;
  3391. return result;
  3392. }
  3393. }
  3394. return VK_SUCCESS;
  3395. }
  3396. // Queue semaphore functions
  3397. VkResult radv_CreateSemaphore(
  3398. VkDevice _device,
  3399. const VkSemaphoreCreateInfo* pCreateInfo,
  3400. const VkAllocationCallbacks* pAllocator,
  3401. VkSemaphore* pSemaphore)
  3402. {
  3403. RADV_FROM_HANDLE(radv_device, device, _device);
  3404. const VkExportSemaphoreCreateInfo *export =
  3405. vk_find_struct_const(pCreateInfo->pNext, EXPORT_SEMAPHORE_CREATE_INFO);
  3406. VkExternalSemaphoreHandleTypeFlags handleTypes =
  3407. export ? export->handleTypes : 0;
  3408. struct radv_semaphore *sem = vk_alloc2(&device->alloc, pAllocator,
  3409. sizeof(*sem), 8,
  3410. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  3411. if (!sem)
  3412. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3413. sem->temp_syncobj = 0;
  3414. /* create a syncobject if we are going to export this semaphore */
  3415. if (device->always_use_syncobj || handleTypes) {
  3416. assert (device->physical_device->rad_info.has_syncobj);
  3417. int ret = device->ws->create_syncobj(device->ws, &sem->syncobj);
  3418. if (ret) {
  3419. vk_free2(&device->alloc, pAllocator, sem);
  3420. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3421. }
  3422. sem->sem = NULL;
  3423. } else {
  3424. sem->sem = device->ws->create_sem(device->ws);
  3425. if (!sem->sem) {
  3426. vk_free2(&device->alloc, pAllocator, sem);
  3427. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3428. }
  3429. sem->syncobj = 0;
  3430. }
  3431. *pSemaphore = radv_semaphore_to_handle(sem);
  3432. return VK_SUCCESS;
  3433. }
  3434. void radv_DestroySemaphore(
  3435. VkDevice _device,
  3436. VkSemaphore _semaphore,
  3437. const VkAllocationCallbacks* pAllocator)
  3438. {
  3439. RADV_FROM_HANDLE(radv_device, device, _device);
  3440. RADV_FROM_HANDLE(radv_semaphore, sem, _semaphore);
  3441. if (!_semaphore)
  3442. return;
  3443. if (sem->syncobj)
  3444. device->ws->destroy_syncobj(device->ws, sem->syncobj);
  3445. else
  3446. device->ws->destroy_sem(sem->sem);
  3447. vk_free2(&device->alloc, pAllocator, sem);
  3448. }
  3449. VkResult radv_CreateEvent(
  3450. VkDevice _device,
  3451. const VkEventCreateInfo* pCreateInfo,
  3452. const VkAllocationCallbacks* pAllocator,
  3453. VkEvent* pEvent)
  3454. {
  3455. RADV_FROM_HANDLE(radv_device, device, _device);
  3456. struct radv_event *event = vk_alloc2(&device->alloc, pAllocator,
  3457. sizeof(*event), 8,
  3458. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  3459. if (!event)
  3460. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3461. event->bo = device->ws->buffer_create(device->ws, 8, 8,
  3462. RADEON_DOMAIN_GTT,
  3463. RADEON_FLAG_VA_UNCACHED | RADEON_FLAG_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING,
  3464. RADV_BO_PRIORITY_FENCE);
  3465. if (!event->bo) {
  3466. vk_free2(&device->alloc, pAllocator, event);
  3467. return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
  3468. }
  3469. event->map = (uint64_t*)device->ws->buffer_map(event->bo);
  3470. *pEvent = radv_event_to_handle(event);
  3471. return VK_SUCCESS;
  3472. }
  3473. void radv_DestroyEvent(
  3474. VkDevice _device,
  3475. VkEvent _event,
  3476. const VkAllocationCallbacks* pAllocator)
  3477. {
  3478. RADV_FROM_HANDLE(radv_device, device, _device);
  3479. RADV_FROM_HANDLE(radv_event, event, _event);
  3480. if (!event)
  3481. return;
  3482. device->ws->buffer_destroy(event->bo);
  3483. vk_free2(&device->alloc, pAllocator, event);
  3484. }
  3485. VkResult radv_GetEventStatus(
  3486. VkDevice _device,
  3487. VkEvent _event)
  3488. {
  3489. RADV_FROM_HANDLE(radv_event, event, _event);
  3490. if (*event->map == 1)
  3491. return VK_EVENT_SET;
  3492. return VK_EVENT_RESET;
  3493. }
  3494. VkResult radv_SetEvent(
  3495. VkDevice _device,
  3496. VkEvent _event)
  3497. {
  3498. RADV_FROM_HANDLE(radv_event, event, _event);
  3499. *event->map = 1;
  3500. return VK_SUCCESS;
  3501. }
  3502. VkResult radv_ResetEvent(
  3503. VkDevice _device,
  3504. VkEvent _event)
  3505. {
  3506. RADV_FROM_HANDLE(radv_event, event, _event);
  3507. *event->map = 0;
  3508. return VK_SUCCESS;
  3509. }
  3510. VkResult radv_CreateBuffer(
  3511. VkDevice _device,
  3512. const VkBufferCreateInfo* pCreateInfo,
  3513. const VkAllocationCallbacks* pAllocator,
  3514. VkBuffer* pBuffer)
  3515. {
  3516. RADV_FROM_HANDLE(radv_device, device, _device);
  3517. struct radv_buffer *buffer;
  3518. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
  3519. buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
  3520. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  3521. if (buffer == NULL)
  3522. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3523. buffer->size = pCreateInfo->size;
  3524. buffer->usage = pCreateInfo->usage;
  3525. buffer->bo = NULL;
  3526. buffer->offset = 0;
  3527. buffer->flags = pCreateInfo->flags;
  3528. buffer->shareable = vk_find_struct_const(pCreateInfo->pNext,
  3529. EXTERNAL_MEMORY_BUFFER_CREATE_INFO) != NULL;
  3530. if (pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) {
  3531. buffer->bo = device->ws->buffer_create(device->ws,
  3532. align64(buffer->size, 4096),
  3533. 4096, 0, RADEON_FLAG_VIRTUAL,
  3534. RADV_BO_PRIORITY_VIRTUAL);
  3535. if (!buffer->bo) {
  3536. vk_free2(&device->alloc, pAllocator, buffer);
  3537. return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
  3538. }
  3539. }
  3540. *pBuffer = radv_buffer_to_handle(buffer);
  3541. return VK_SUCCESS;
  3542. }
  3543. void radv_DestroyBuffer(
  3544. VkDevice _device,
  3545. VkBuffer _buffer,
  3546. const VkAllocationCallbacks* pAllocator)
  3547. {
  3548. RADV_FROM_HANDLE(radv_device, device, _device);
  3549. RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
  3550. if (!buffer)
  3551. return;
  3552. if (buffer->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
  3553. device->ws->buffer_destroy(buffer->bo);
  3554. vk_free2(&device->alloc, pAllocator, buffer);
  3555. }
  3556. VkDeviceAddress radv_GetBufferDeviceAddressEXT(
  3557. VkDevice device,
  3558. const VkBufferDeviceAddressInfoEXT* pInfo)
  3559. {
  3560. RADV_FROM_HANDLE(radv_buffer, buffer, pInfo->buffer);
  3561. return radv_buffer_get_va(buffer->bo) + buffer->offset;
  3562. }
  3563. static inline unsigned
  3564. si_tile_mode_index(const struct radv_image *image, unsigned level, bool stencil)
  3565. {
  3566. if (stencil)
  3567. return image->surface.u.legacy.stencil_tiling_index[level];
  3568. else
  3569. return image->surface.u.legacy.tiling_index[level];
  3570. }
  3571. static uint32_t radv_surface_max_layer_count(struct radv_image_view *iview)
  3572. {
  3573. return iview->type == VK_IMAGE_VIEW_TYPE_3D ? iview->extent.depth : (iview->base_layer + iview->layer_count);
  3574. }
  3575. static uint32_t
  3576. radv_init_dcc_control_reg(struct radv_device *device,
  3577. struct radv_image_view *iview)
  3578. {
  3579. unsigned max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_256B;
  3580. unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
  3581. unsigned max_compressed_block_size;
  3582. unsigned independent_64b_blocks;
  3583. if (!radv_image_has_dcc(iview->image))
  3584. return 0;
  3585. if (iview->image->info.samples > 1) {
  3586. if (iview->image->surface.bpe == 1)
  3587. max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
  3588. else if (iview->image->surface.bpe == 2)
  3589. max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
  3590. }
  3591. if (!device->physical_device->rad_info.has_dedicated_vram) {
  3592. /* amdvlk: [min-compressed-block-size] should be set to 32 for
  3593. * dGPU and 64 for APU because all of our APUs to date use
  3594. * DIMMs which have a request granularity size of 64B while all
  3595. * other chips have a 32B request size.
  3596. */
  3597. min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
  3598. }
  3599. if (iview->image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
  3600. VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
  3601. VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
  3602. /* If this DCC image is potentially going to be used in texture
  3603. * fetches, we need some special settings.
  3604. */
  3605. independent_64b_blocks = 1;
  3606. max_compressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
  3607. } else {
  3608. /* MAX_UNCOMPRESSED_BLOCK_SIZE must be >=
  3609. * MAX_COMPRESSED_BLOCK_SIZE. Set MAX_COMPRESSED_BLOCK_SIZE as
  3610. * big as possible for better compression state.
  3611. */
  3612. independent_64b_blocks = 0;
  3613. max_compressed_block_size = max_uncompressed_block_size;
  3614. }
  3615. return S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
  3616. S_028C78_MAX_COMPRESSED_BLOCK_SIZE(max_compressed_block_size) |
  3617. S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
  3618. S_028C78_INDEPENDENT_64B_BLOCKS(independent_64b_blocks);
  3619. }
  3620. static void
  3621. radv_initialise_color_surface(struct radv_device *device,
  3622. struct radv_color_buffer_info *cb,
  3623. struct radv_image_view *iview)
  3624. {
  3625. const struct vk_format_description *desc;
  3626. unsigned ntype, format, swap, endian;
  3627. unsigned blend_clamp = 0, blend_bypass = 0;
  3628. uint64_t va;
  3629. const struct radeon_surf *surf = &iview->image->surface;
  3630. desc = vk_format_description(iview->vk_format);
  3631. memset(cb, 0, sizeof(*cb));
  3632. /* Intensity is implemented as Red, so treat it that way. */
  3633. cb->cb_color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == VK_SWIZZLE_1);
  3634. va = radv_buffer_get_va(iview->bo) + iview->image->offset;
  3635. cb->cb_color_base = va >> 8;
  3636. if (device->physical_device->rad_info.chip_class >= GFX9) {
  3637. struct gfx9_surf_meta_flags meta;
  3638. if (iview->image->dcc_offset)
  3639. meta = iview->image->surface.u.gfx9.dcc;
  3640. else
  3641. meta = iview->image->surface.u.gfx9.cmask;
  3642. cb->cb_color_attrib |= S_028C74_COLOR_SW_MODE(iview->image->surface.u.gfx9.surf.swizzle_mode) |
  3643. S_028C74_FMASK_SW_MODE(iview->image->surface.u.gfx9.fmask.swizzle_mode) |
  3644. S_028C74_RB_ALIGNED(meta.rb_aligned) |
  3645. S_028C74_PIPE_ALIGNED(meta.pipe_aligned);
  3646. cb->cb_color_base += iview->image->surface.u.gfx9.surf_offset >> 8;
  3647. cb->cb_color_base |= iview->image->surface.tile_swizzle;
  3648. } else {
  3649. const struct legacy_surf_level *level_info = &surf->u.legacy.level[iview->base_mip];
  3650. unsigned pitch_tile_max, slice_tile_max, tile_mode_index;
  3651. cb->cb_color_base += level_info->offset >> 8;
  3652. if (level_info->mode == RADEON_SURF_MODE_2D)
  3653. cb->cb_color_base |= iview->image->surface.tile_swizzle;
  3654. pitch_tile_max = level_info->nblk_x / 8 - 1;
  3655. slice_tile_max = (level_info->nblk_x * level_info->nblk_y) / 64 - 1;
  3656. tile_mode_index = si_tile_mode_index(iview->image, iview->base_mip, false);
  3657. cb->cb_color_pitch = S_028C64_TILE_MAX(pitch_tile_max);
  3658. cb->cb_color_slice = S_028C68_TILE_MAX(slice_tile_max);
  3659. cb->cb_color_cmask_slice = iview->image->cmask.slice_tile_max;
  3660. cb->cb_color_attrib |= S_028C74_TILE_MODE_INDEX(tile_mode_index);
  3661. if (radv_image_has_fmask(iview->image)) {
  3662. if (device->physical_device->rad_info.chip_class >= CIK)
  3663. cb->cb_color_pitch |= S_028C64_FMASK_TILE_MAX(iview->image->fmask.pitch_in_pixels / 8 - 1);
  3664. cb->cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(iview->image->fmask.tile_mode_index);
  3665. cb->cb_color_fmask_slice = S_028C88_TILE_MAX(iview->image->fmask.slice_tile_max);
  3666. } else {
  3667. /* This must be set for fast clear to work without FMASK. */
  3668. if (device->physical_device->rad_info.chip_class >= CIK)
  3669. cb->cb_color_pitch |= S_028C64_FMASK_TILE_MAX(pitch_tile_max);
  3670. cb->cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index);
  3671. cb->cb_color_fmask_slice = S_028C88_TILE_MAX(slice_tile_max);
  3672. }
  3673. }
  3674. /* CMASK variables */
  3675. va = radv_buffer_get_va(iview->bo) + iview->image->offset;
  3676. va += iview->image->cmask.offset;
  3677. cb->cb_color_cmask = va >> 8;
  3678. va = radv_buffer_get_va(iview->bo) + iview->image->offset;
  3679. va += iview->image->dcc_offset;
  3680. cb->cb_dcc_base = va >> 8;
  3681. cb->cb_dcc_base |= iview->image->surface.tile_swizzle;
  3682. uint32_t max_slice = radv_surface_max_layer_count(iview) - 1;
  3683. cb->cb_color_view = S_028C6C_SLICE_START(iview->base_layer) |
  3684. S_028C6C_SLICE_MAX(max_slice);
  3685. if (iview->image->info.samples > 1) {
  3686. unsigned log_samples = util_logbase2(iview->image->info.samples);
  3687. cb->cb_color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
  3688. S_028C74_NUM_FRAGMENTS(log_samples);
  3689. }
  3690. if (radv_image_has_fmask(iview->image)) {
  3691. va = radv_buffer_get_va(iview->bo) + iview->image->offset + iview->image->fmask.offset;
  3692. cb->cb_color_fmask = va >> 8;
  3693. cb->cb_color_fmask |= iview->image->fmask.tile_swizzle;
  3694. } else {
  3695. cb->cb_color_fmask = cb->cb_color_base;
  3696. }
  3697. ntype = radv_translate_color_numformat(iview->vk_format,
  3698. desc,
  3699. vk_format_get_first_non_void_channel(iview->vk_format));
  3700. format = radv_translate_colorformat(iview->vk_format);
  3701. if (format == V_028C70_COLOR_INVALID || ntype == ~0u)
  3702. radv_finishme("Illegal color\n");
  3703. swap = radv_translate_colorswap(iview->vk_format, FALSE);
  3704. endian = radv_colorformat_endian_swap(format);
  3705. /* blend clamp should be set for all NORM/SRGB types */
  3706. if (ntype == V_028C70_NUMBER_UNORM ||
  3707. ntype == V_028C70_NUMBER_SNORM ||
  3708. ntype == V_028C70_NUMBER_SRGB)
  3709. blend_clamp = 1;
  3710. /* set blend bypass according to docs if SINT/UINT or
  3711. 8/24 COLOR variants */
  3712. if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
  3713. format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
  3714. format == V_028C70_COLOR_X24_8_32_FLOAT) {
  3715. blend_clamp = 0;
  3716. blend_bypass = 1;
  3717. }
  3718. #if 0
  3719. if ((ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) &&
  3720. (format == V_028C70_COLOR_8 ||
  3721. format == V_028C70_COLOR_8_8 ||
  3722. format == V_028C70_COLOR_8_8_8_8))
  3723. ->color_is_int8 = true;
  3724. #endif
  3725. cb->cb_color_info = S_028C70_FORMAT(format) |
  3726. S_028C70_COMP_SWAP(swap) |
  3727. S_028C70_BLEND_CLAMP(blend_clamp) |
  3728. S_028C70_BLEND_BYPASS(blend_bypass) |
  3729. S_028C70_SIMPLE_FLOAT(1) |
  3730. S_028C70_ROUND_MODE(ntype != V_028C70_NUMBER_UNORM &&
  3731. ntype != V_028C70_NUMBER_SNORM &&
  3732. ntype != V_028C70_NUMBER_SRGB &&
  3733. format != V_028C70_COLOR_8_24 &&
  3734. format != V_028C70_COLOR_24_8) |
  3735. S_028C70_NUMBER_TYPE(ntype) |
  3736. S_028C70_ENDIAN(endian);
  3737. if (radv_image_has_fmask(iview->image)) {
  3738. cb->cb_color_info |= S_028C70_COMPRESSION(1);
  3739. if (device->physical_device->rad_info.chip_class == SI) {
  3740. unsigned fmask_bankh = util_logbase2(iview->image->fmask.bank_height);
  3741. cb->cb_color_attrib |= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh);
  3742. }
  3743. }
  3744. if (radv_image_has_cmask(iview->image) &&
  3745. !(device->instance->debug_flags & RADV_DEBUG_NO_FAST_CLEARS))
  3746. cb->cb_color_info |= S_028C70_FAST_CLEAR(1);
  3747. if (radv_dcc_enabled(iview->image, iview->base_mip))
  3748. cb->cb_color_info |= S_028C70_DCC_ENABLE(1);
  3749. cb->cb_dcc_control = radv_init_dcc_control_reg(device, iview);
  3750. /* This must be set for fast clear to work without FMASK. */
  3751. if (!radv_image_has_fmask(iview->image) &&
  3752. device->physical_device->rad_info.chip_class == SI) {
  3753. unsigned bankh = util_logbase2(iview->image->surface.u.legacy.bankh);
  3754. cb->cb_color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
  3755. }
  3756. if (device->physical_device->rad_info.chip_class >= GFX9) {
  3757. unsigned mip0_depth = iview->image->type == VK_IMAGE_TYPE_3D ?
  3758. (iview->extent.depth - 1) : (iview->image->info.array_size - 1);
  3759. cb->cb_color_view |= S_028C6C_MIP_LEVEL(iview->base_mip);
  3760. cb->cb_color_attrib |= S_028C74_MIP0_DEPTH(mip0_depth) |
  3761. S_028C74_RESOURCE_TYPE(iview->image->surface.u.gfx9.resource_type);
  3762. cb->cb_color_attrib2 = S_028C68_MIP0_WIDTH(iview->extent.width - 1) |
  3763. S_028C68_MIP0_HEIGHT(iview->extent.height - 1) |
  3764. S_028C68_MAX_MIP(iview->image->info.levels - 1);
  3765. }
  3766. }
  3767. static unsigned
  3768. radv_calc_decompress_on_z_planes(struct radv_device *device,
  3769. struct radv_image_view *iview)
  3770. {
  3771. unsigned max_zplanes = 0;
  3772. assert(radv_image_is_tc_compat_htile(iview->image));
  3773. if (device->physical_device->rad_info.chip_class >= GFX9) {
  3774. /* Default value for 32-bit depth surfaces. */
  3775. max_zplanes = 4;
  3776. if (iview->vk_format == VK_FORMAT_D16_UNORM &&
  3777. iview->image->info.samples > 1)
  3778. max_zplanes = 2;
  3779. max_zplanes = max_zplanes + 1;
  3780. } else {
  3781. if (iview->vk_format == VK_FORMAT_D16_UNORM) {
  3782. /* Do not enable Z plane compression for 16-bit depth
  3783. * surfaces because isn't supported on GFX8. Only
  3784. * 32-bit depth surfaces are supported by the hardware.
  3785. * This allows to maintain shader compatibility and to
  3786. * reduce the number of depth decompressions.
  3787. */
  3788. max_zplanes = 1;
  3789. } else {
  3790. if (iview->image->info.samples <= 1)
  3791. max_zplanes = 5;
  3792. else if (iview->image->info.samples <= 4)
  3793. max_zplanes = 3;
  3794. else
  3795. max_zplanes = 2;
  3796. }
  3797. }
  3798. return max_zplanes;
  3799. }
  3800. static void
  3801. radv_initialise_ds_surface(struct radv_device *device,
  3802. struct radv_ds_buffer_info *ds,
  3803. struct radv_image_view *iview)
  3804. {
  3805. unsigned level = iview->base_mip;
  3806. unsigned format, stencil_format;
  3807. uint64_t va, s_offs, z_offs;
  3808. bool stencil_only = false;
  3809. memset(ds, 0, sizeof(*ds));
  3810. switch (iview->image->vk_format) {
  3811. case VK_FORMAT_D24_UNORM_S8_UINT:
  3812. case VK_FORMAT_X8_D24_UNORM_PACK32:
  3813. ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
  3814. ds->offset_scale = 2.0f;
  3815. break;
  3816. case VK_FORMAT_D16_UNORM:
  3817. case VK_FORMAT_D16_UNORM_S8_UINT:
  3818. ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
  3819. ds->offset_scale = 4.0f;
  3820. break;
  3821. case VK_FORMAT_D32_SFLOAT:
  3822. case VK_FORMAT_D32_SFLOAT_S8_UINT:
  3823. ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
  3824. S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
  3825. ds->offset_scale = 1.0f;
  3826. break;
  3827. case VK_FORMAT_S8_UINT:
  3828. stencil_only = true;
  3829. break;
  3830. default:
  3831. break;
  3832. }
  3833. format = radv_translate_dbformat(iview->image->vk_format);
  3834. stencil_format = iview->image->surface.has_stencil ?
  3835. V_028044_STENCIL_8 : V_028044_STENCIL_INVALID;
  3836. uint32_t max_slice = radv_surface_max_layer_count(iview) - 1;
  3837. ds->db_depth_view = S_028008_SLICE_START(iview->base_layer) |
  3838. S_028008_SLICE_MAX(max_slice);
  3839. ds->db_htile_data_base = 0;
  3840. ds->db_htile_surface = 0;
  3841. va = radv_buffer_get_va(iview->bo) + iview->image->offset;
  3842. s_offs = z_offs = va;
  3843. if (device->physical_device->rad_info.chip_class >= GFX9) {
  3844. assert(iview->image->surface.u.gfx9.surf_offset == 0);
  3845. s_offs += iview->image->surface.u.gfx9.stencil_offset;
  3846. ds->db_z_info = S_028038_FORMAT(format) |
  3847. S_028038_NUM_SAMPLES(util_logbase2(iview->image->info.samples)) |
  3848. S_028038_SW_MODE(iview->image->surface.u.gfx9.surf.swizzle_mode) |
  3849. S_028038_MAXMIP(iview->image->info.levels - 1) |
  3850. S_028038_ZRANGE_PRECISION(1);
  3851. ds->db_stencil_info = S_02803C_FORMAT(stencil_format) |
  3852. S_02803C_SW_MODE(iview->image->surface.u.gfx9.stencil.swizzle_mode);
  3853. ds->db_z_info2 = S_028068_EPITCH(iview->image->surface.u.gfx9.surf.epitch);
  3854. ds->db_stencil_info2 = S_02806C_EPITCH(iview->image->surface.u.gfx9.stencil.epitch);
  3855. ds->db_depth_view |= S_028008_MIPID(level);
  3856. ds->db_depth_size = S_02801C_X_MAX(iview->image->info.width - 1) |
  3857. S_02801C_Y_MAX(iview->image->info.height - 1);
  3858. if (radv_htile_enabled(iview->image, level)) {
  3859. ds->db_z_info |= S_028038_TILE_SURFACE_ENABLE(1);
  3860. if (radv_image_is_tc_compat_htile(iview->image)) {
  3861. unsigned max_zplanes =
  3862. radv_calc_decompress_on_z_planes(device, iview);
  3863. ds->db_z_info |= S_028038_DECOMPRESS_ON_N_ZPLANES(max_zplanes) |
  3864. S_028038_ITERATE_FLUSH(1);
  3865. ds->db_stencil_info |= S_02803C_ITERATE_FLUSH(1);
  3866. }
  3867. if (!iview->image->surface.has_stencil)
  3868. /* Use all of the htile_buffer for depth if there's no stencil. */
  3869. ds->db_stencil_info |= S_02803C_TILE_STENCIL_DISABLE(1);
  3870. va = radv_buffer_get_va(iview->bo) + iview->image->offset +
  3871. iview->image->htile_offset;
  3872. ds->db_htile_data_base = va >> 8;
  3873. ds->db_htile_surface = S_028ABC_FULL_CACHE(1) |
  3874. S_028ABC_PIPE_ALIGNED(iview->image->surface.u.gfx9.htile.pipe_aligned) |
  3875. S_028ABC_RB_ALIGNED(iview->image->surface.u.gfx9.htile.rb_aligned);
  3876. }
  3877. } else {
  3878. const struct legacy_surf_level *level_info = &iview->image->surface.u.legacy.level[level];
  3879. if (stencil_only)
  3880. level_info = &iview->image->surface.u.legacy.stencil_level[level];
  3881. z_offs += iview->image->surface.u.legacy.level[level].offset;
  3882. s_offs += iview->image->surface.u.legacy.stencil_level[level].offset;
  3883. ds->db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(!radv_image_is_tc_compat_htile(iview->image));
  3884. ds->db_z_info = S_028040_FORMAT(format) | S_028040_ZRANGE_PRECISION(1);
  3885. ds->db_stencil_info = S_028044_FORMAT(stencil_format);
  3886. if (iview->image->info.samples > 1)
  3887. ds->db_z_info |= S_028040_NUM_SAMPLES(util_logbase2(iview->image->info.samples));
  3888. if (device->physical_device->rad_info.chip_class >= CIK) {
  3889. struct radeon_info *info = &device->physical_device->rad_info;
  3890. unsigned tiling_index = iview->image->surface.u.legacy.tiling_index[level];
  3891. unsigned stencil_index = iview->image->surface.u.legacy.stencil_tiling_index[level];
  3892. unsigned macro_index = iview->image->surface.u.legacy.macro_tile_index;
  3893. unsigned tile_mode = info->si_tile_mode_array[tiling_index];
  3894. unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
  3895. unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
  3896. if (stencil_only)
  3897. tile_mode = stencil_tile_mode;
  3898. ds->db_depth_info |=
  3899. S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
  3900. S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
  3901. S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
  3902. S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
  3903. S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
  3904. S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
  3905. ds->db_z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
  3906. ds->db_stencil_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
  3907. } else {
  3908. unsigned tile_mode_index = si_tile_mode_index(iview->image, level, false);
  3909. ds->db_z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
  3910. tile_mode_index = si_tile_mode_index(iview->image, level, true);
  3911. ds->db_stencil_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
  3912. if (stencil_only)
  3913. ds->db_z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
  3914. }
  3915. ds->db_depth_size = S_028058_PITCH_TILE_MAX((level_info->nblk_x / 8) - 1) |
  3916. S_028058_HEIGHT_TILE_MAX((level_info->nblk_y / 8) - 1);
  3917. ds->db_depth_slice = S_02805C_SLICE_TILE_MAX((level_info->nblk_x * level_info->nblk_y) / 64 - 1);
  3918. if (radv_htile_enabled(iview->image, level)) {
  3919. ds->db_z_info |= S_028040_TILE_SURFACE_ENABLE(1);
  3920. if (!iview->image->surface.has_stencil &&
  3921. !radv_image_is_tc_compat_htile(iview->image))
  3922. /* Use all of the htile_buffer for depth if there's no stencil. */
  3923. ds->db_stencil_info |= S_028044_TILE_STENCIL_DISABLE(1);
  3924. va = radv_buffer_get_va(iview->bo) + iview->image->offset +
  3925. iview->image->htile_offset;
  3926. ds->db_htile_data_base = va >> 8;
  3927. ds->db_htile_surface = S_028ABC_FULL_CACHE(1);
  3928. if (radv_image_is_tc_compat_htile(iview->image)) {
  3929. unsigned max_zplanes =
  3930. radv_calc_decompress_on_z_planes(device, iview);
  3931. ds->db_htile_surface |= S_028ABC_TC_COMPATIBLE(1);
  3932. ds->db_z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(max_zplanes);
  3933. }
  3934. }
  3935. }
  3936. ds->db_z_read_base = ds->db_z_write_base = z_offs >> 8;
  3937. ds->db_stencil_read_base = ds->db_stencil_write_base = s_offs >> 8;
  3938. }
  3939. VkResult radv_CreateFramebuffer(
  3940. VkDevice _device,
  3941. const VkFramebufferCreateInfo* pCreateInfo,
  3942. const VkAllocationCallbacks* pAllocator,
  3943. VkFramebuffer* pFramebuffer)
  3944. {
  3945. RADV_FROM_HANDLE(radv_device, device, _device);
  3946. struct radv_framebuffer *framebuffer;
  3947. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
  3948. size_t size = sizeof(*framebuffer) +
  3949. sizeof(struct radv_attachment_info) * pCreateInfo->attachmentCount;
  3950. framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
  3951. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  3952. if (framebuffer == NULL)
  3953. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  3954. framebuffer->attachment_count = pCreateInfo->attachmentCount;
  3955. framebuffer->width = pCreateInfo->width;
  3956. framebuffer->height = pCreateInfo->height;
  3957. framebuffer->layers = pCreateInfo->layers;
  3958. for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
  3959. VkImageView _iview = pCreateInfo->pAttachments[i];
  3960. struct radv_image_view *iview = radv_image_view_from_handle(_iview);
  3961. framebuffer->attachments[i].attachment = iview;
  3962. if (iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) {
  3963. radv_initialise_color_surface(device, &framebuffer->attachments[i].cb, iview);
  3964. } else if (iview->aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
  3965. radv_initialise_ds_surface(device, &framebuffer->attachments[i].ds, iview);
  3966. }
  3967. framebuffer->width = MIN2(framebuffer->width, iview->extent.width);
  3968. framebuffer->height = MIN2(framebuffer->height, iview->extent.height);
  3969. framebuffer->layers = MIN2(framebuffer->layers, radv_surface_max_layer_count(iview));
  3970. }
  3971. *pFramebuffer = radv_framebuffer_to_handle(framebuffer);
  3972. return VK_SUCCESS;
  3973. }
  3974. void radv_DestroyFramebuffer(
  3975. VkDevice _device,
  3976. VkFramebuffer _fb,
  3977. const VkAllocationCallbacks* pAllocator)
  3978. {
  3979. RADV_FROM_HANDLE(radv_device, device, _device);
  3980. RADV_FROM_HANDLE(radv_framebuffer, fb, _fb);
  3981. if (!fb)
  3982. return;
  3983. vk_free2(&device->alloc, pAllocator, fb);
  3984. }
  3985. static unsigned radv_tex_wrap(VkSamplerAddressMode address_mode)
  3986. {
  3987. switch (address_mode) {
  3988. case VK_SAMPLER_ADDRESS_MODE_REPEAT:
  3989. return V_008F30_SQ_TEX_WRAP;
  3990. case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT:
  3991. return V_008F30_SQ_TEX_MIRROR;
  3992. case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE:
  3993. return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
  3994. case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER:
  3995. return V_008F30_SQ_TEX_CLAMP_BORDER;
  3996. case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE:
  3997. return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
  3998. default:
  3999. unreachable("illegal tex wrap mode");
  4000. break;
  4001. }
  4002. }
  4003. static unsigned
  4004. radv_tex_compare(VkCompareOp op)
  4005. {
  4006. switch (op) {
  4007. case VK_COMPARE_OP_NEVER:
  4008. return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
  4009. case VK_COMPARE_OP_LESS:
  4010. return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
  4011. case VK_COMPARE_OP_EQUAL:
  4012. return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
  4013. case VK_COMPARE_OP_LESS_OR_EQUAL:
  4014. return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
  4015. case VK_COMPARE_OP_GREATER:
  4016. return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
  4017. case VK_COMPARE_OP_NOT_EQUAL:
  4018. return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
  4019. case VK_COMPARE_OP_GREATER_OR_EQUAL:
  4020. return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
  4021. case VK_COMPARE_OP_ALWAYS:
  4022. return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
  4023. default:
  4024. unreachable("illegal compare mode");
  4025. break;
  4026. }
  4027. }
  4028. static unsigned
  4029. radv_tex_filter(VkFilter filter, unsigned max_ansio)
  4030. {
  4031. switch (filter) {
  4032. case VK_FILTER_NEAREST:
  4033. return (max_ansio > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT :
  4034. V_008F38_SQ_TEX_XY_FILTER_POINT);
  4035. case VK_FILTER_LINEAR:
  4036. return (max_ansio > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR :
  4037. V_008F38_SQ_TEX_XY_FILTER_BILINEAR);
  4038. case VK_FILTER_CUBIC_IMG:
  4039. default:
  4040. fprintf(stderr, "illegal texture filter");
  4041. return 0;
  4042. }
  4043. }
  4044. static unsigned
  4045. radv_tex_mipfilter(VkSamplerMipmapMode mode)
  4046. {
  4047. switch (mode) {
  4048. case VK_SAMPLER_MIPMAP_MODE_NEAREST:
  4049. return V_008F38_SQ_TEX_Z_FILTER_POINT;
  4050. case VK_SAMPLER_MIPMAP_MODE_LINEAR:
  4051. return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
  4052. default:
  4053. return V_008F38_SQ_TEX_Z_FILTER_NONE;
  4054. }
  4055. }
  4056. static unsigned
  4057. radv_tex_bordercolor(VkBorderColor bcolor)
  4058. {
  4059. switch (bcolor) {
  4060. case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
  4061. case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
  4062. return V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK;
  4063. case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
  4064. case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
  4065. return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK;
  4066. case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
  4067. case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
  4068. return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE;
  4069. default:
  4070. break;
  4071. }
  4072. return 0;
  4073. }
  4074. static unsigned
  4075. radv_tex_aniso_filter(unsigned filter)
  4076. {
  4077. if (filter < 2)
  4078. return 0;
  4079. if (filter < 4)
  4080. return 1;
  4081. if (filter < 8)
  4082. return 2;
  4083. if (filter < 16)
  4084. return 3;
  4085. return 4;
  4086. }
  4087. static unsigned
  4088. radv_tex_filter_mode(VkSamplerReductionModeEXT mode)
  4089. {
  4090. switch (mode) {
  4091. case VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE_EXT:
  4092. return V_008F30_SQ_IMG_FILTER_MODE_BLEND;
  4093. case VK_SAMPLER_REDUCTION_MODE_MIN_EXT:
  4094. return V_008F30_SQ_IMG_FILTER_MODE_MIN;
  4095. case VK_SAMPLER_REDUCTION_MODE_MAX_EXT:
  4096. return V_008F30_SQ_IMG_FILTER_MODE_MAX;
  4097. default:
  4098. break;
  4099. }
  4100. return 0;
  4101. }
  4102. static uint32_t
  4103. radv_get_max_anisotropy(struct radv_device *device,
  4104. const VkSamplerCreateInfo *pCreateInfo)
  4105. {
  4106. if (device->force_aniso >= 0)
  4107. return device->force_aniso;
  4108. if (pCreateInfo->anisotropyEnable &&
  4109. pCreateInfo->maxAnisotropy > 1.0f)
  4110. return (uint32_t)pCreateInfo->maxAnisotropy;
  4111. return 0;
  4112. }
  4113. static void
  4114. radv_init_sampler(struct radv_device *device,
  4115. struct radv_sampler *sampler,
  4116. const VkSamplerCreateInfo *pCreateInfo)
  4117. {
  4118. uint32_t max_aniso = radv_get_max_anisotropy(device, pCreateInfo);
  4119. uint32_t max_aniso_ratio = radv_tex_aniso_filter(max_aniso);
  4120. bool is_vi = (device->physical_device->rad_info.chip_class >= VI);
  4121. unsigned filter_mode = V_008F30_SQ_IMG_FILTER_MODE_BLEND;
  4122. const struct VkSamplerReductionModeCreateInfoEXT *sampler_reduction =
  4123. vk_find_struct_const(pCreateInfo->pNext,
  4124. SAMPLER_REDUCTION_MODE_CREATE_INFO_EXT);
  4125. if (sampler_reduction)
  4126. filter_mode = radv_tex_filter_mode(sampler_reduction->reductionMode);
  4127. sampler->state[0] = (S_008F30_CLAMP_X(radv_tex_wrap(pCreateInfo->addressModeU)) |
  4128. S_008F30_CLAMP_Y(radv_tex_wrap(pCreateInfo->addressModeV)) |
  4129. S_008F30_CLAMP_Z(radv_tex_wrap(pCreateInfo->addressModeW)) |
  4130. S_008F30_MAX_ANISO_RATIO(max_aniso_ratio) |
  4131. S_008F30_DEPTH_COMPARE_FUNC(radv_tex_compare(pCreateInfo->compareOp)) |
  4132. S_008F30_FORCE_UNNORMALIZED(pCreateInfo->unnormalizedCoordinates ? 1 : 0) |
  4133. S_008F30_ANISO_THRESHOLD(max_aniso_ratio >> 1) |
  4134. S_008F30_ANISO_BIAS(max_aniso_ratio) |
  4135. S_008F30_DISABLE_CUBE_WRAP(0) |
  4136. S_008F30_COMPAT_MODE(is_vi) |
  4137. S_008F30_FILTER_MODE(filter_mode));
  4138. sampler->state[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(pCreateInfo->minLod, 0, 15), 8)) |
  4139. S_008F34_MAX_LOD(S_FIXED(CLAMP(pCreateInfo->maxLod, 0, 15), 8)) |
  4140. S_008F34_PERF_MIP(max_aniso_ratio ? max_aniso_ratio + 6 : 0));
  4141. sampler->state[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(pCreateInfo->mipLodBias, -16, 16), 8)) |
  4142. S_008F38_XY_MAG_FILTER(radv_tex_filter(pCreateInfo->magFilter, max_aniso)) |
  4143. S_008F38_XY_MIN_FILTER(radv_tex_filter(pCreateInfo->minFilter, max_aniso)) |
  4144. S_008F38_MIP_FILTER(radv_tex_mipfilter(pCreateInfo->mipmapMode)) |
  4145. S_008F38_MIP_POINT_PRECLAMP(0) |
  4146. S_008F38_DISABLE_LSB_CEIL(device->physical_device->rad_info.chip_class <= VI) |
  4147. S_008F38_FILTER_PREC_FIX(1) |
  4148. S_008F38_ANISO_OVERRIDE(is_vi));
  4149. sampler->state[3] = (S_008F3C_BORDER_COLOR_PTR(0) |
  4150. S_008F3C_BORDER_COLOR_TYPE(radv_tex_bordercolor(pCreateInfo->borderColor)));
  4151. }
  4152. VkResult radv_CreateSampler(
  4153. VkDevice _device,
  4154. const VkSamplerCreateInfo* pCreateInfo,
  4155. const VkAllocationCallbacks* pAllocator,
  4156. VkSampler* pSampler)
  4157. {
  4158. RADV_FROM_HANDLE(radv_device, device, _device);
  4159. struct radv_sampler *sampler;
  4160. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
  4161. sampler = vk_alloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
  4162. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  4163. if (!sampler)
  4164. return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  4165. radv_init_sampler(device, sampler, pCreateInfo);
  4166. *pSampler = radv_sampler_to_handle(sampler);
  4167. return VK_SUCCESS;
  4168. }
  4169. void radv_DestroySampler(
  4170. VkDevice _device,
  4171. VkSampler _sampler,
  4172. const VkAllocationCallbacks* pAllocator)
  4173. {
  4174. RADV_FROM_HANDLE(radv_device, device, _device);
  4175. RADV_FROM_HANDLE(radv_sampler, sampler, _sampler);
  4176. if (!sampler)
  4177. return;
  4178. vk_free2(&device->alloc, pAllocator, sampler);
  4179. }
  4180. /* vk_icd.h does not declare this function, so we declare it here to
  4181. * suppress Wmissing-prototypes.
  4182. */
  4183. PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
  4184. vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion);
  4185. PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
  4186. vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
  4187. {
  4188. /* For the full details on loader interface versioning, see
  4189. * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
  4190. * What follows is a condensed summary, to help you navigate the large and
  4191. * confusing official doc.
  4192. *
  4193. * - Loader interface v0 is incompatible with later versions. We don't
  4194. * support it.
  4195. *
  4196. * - In loader interface v1:
  4197. * - The first ICD entrypoint called by the loader is
  4198. * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
  4199. * entrypoint.
  4200. * - The ICD must statically expose no other Vulkan symbol unless it is
  4201. * linked with -Bsymbolic.
  4202. * - Each dispatchable Vulkan handle created by the ICD must be
  4203. * a pointer to a struct whose first member is VK_LOADER_DATA. The
  4204. * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
  4205. * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
  4206. * vkDestroySurfaceKHR(). The ICD must be capable of working with
  4207. * such loader-managed surfaces.
  4208. *
  4209. * - Loader interface v2 differs from v1 in:
  4210. * - The first ICD entrypoint called by the loader is
  4211. * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
  4212. * statically expose this entrypoint.
  4213. *
  4214. * - Loader interface v3 differs from v2 in:
  4215. * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
  4216. * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
  4217. * because the loader no longer does so.
  4218. */
  4219. *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
  4220. return VK_SUCCESS;
  4221. }
  4222. VkResult radv_GetMemoryFdKHR(VkDevice _device,
  4223. const VkMemoryGetFdInfoKHR *pGetFdInfo,
  4224. int *pFD)
  4225. {
  4226. RADV_FROM_HANDLE(radv_device, device, _device);
  4227. RADV_FROM_HANDLE(radv_device_memory, memory, pGetFdInfo->memory);
  4228. assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
  4229. /* At the moment, we support only the below handle types. */
  4230. assert(pGetFdInfo->handleType ==
  4231. VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
  4232. pGetFdInfo->handleType ==
  4233. VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
  4234. bool ret = radv_get_memory_fd(device, memory, pFD);
  4235. if (ret == false)
  4236. return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
  4237. return VK_SUCCESS;
  4238. }
  4239. VkResult radv_GetMemoryFdPropertiesKHR(VkDevice _device,
  4240. VkExternalMemoryHandleTypeFlagBits handleType,
  4241. int fd,
  4242. VkMemoryFdPropertiesKHR *pMemoryFdProperties)
  4243. {
  4244. RADV_FROM_HANDLE(radv_device, device, _device);
  4245. switch (handleType) {
  4246. case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
  4247. pMemoryFdProperties->memoryTypeBits = (1 << RADV_MEM_TYPE_COUNT) - 1;
  4248. return VK_SUCCESS;
  4249. default:
  4250. /* The valid usage section for this function says:
  4251. *
  4252. * "handleType must not be one of the handle types defined as
  4253. * opaque."
  4254. *
  4255. * So opaque handle types fall into the default "unsupported" case.
  4256. */
  4257. return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
  4258. }
  4259. }
  4260. static VkResult radv_import_opaque_fd(struct radv_device *device,
  4261. int fd,
  4262. uint32_t *syncobj)
  4263. {
  4264. uint32_t syncobj_handle = 0;
  4265. int ret = device->ws->import_syncobj(device->ws, fd, &syncobj_handle);
  4266. if (ret != 0)
  4267. return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
  4268. if (*syncobj)
  4269. device->ws->destroy_syncobj(device->ws, *syncobj);
  4270. *syncobj = syncobj_handle;
  4271. close(fd);
  4272. return VK_SUCCESS;
  4273. }
  4274. static VkResult radv_import_sync_fd(struct radv_device *device,
  4275. int fd,
  4276. uint32_t *syncobj)
  4277. {
  4278. /* If we create a syncobj we do it locally so that if we have an error, we don't
  4279. * leave a syncobj in an undetermined state in the fence. */
  4280. uint32_t syncobj_handle = *syncobj;
  4281. if (!syncobj_handle) {
  4282. int ret = device->ws->create_syncobj(device->ws, &syncobj_handle);
  4283. if (ret) {
  4284. return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
  4285. }
  4286. }
  4287. if (fd == -1) {
  4288. device->ws->signal_syncobj(device->ws, syncobj_handle);
  4289. } else {
  4290. int ret = device->ws->import_syncobj_from_sync_file(device->ws, syncobj_handle, fd);
  4291. if (ret != 0)
  4292. return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
  4293. }
  4294. *syncobj = syncobj_handle;
  4295. if (fd != -1)
  4296. close(fd);
  4297. return VK_SUCCESS;
  4298. }
  4299. VkResult radv_ImportSemaphoreFdKHR(VkDevice _device,
  4300. const VkImportSemaphoreFdInfoKHR *pImportSemaphoreFdInfo)
  4301. {
  4302. RADV_FROM_HANDLE(radv_device, device, _device);
  4303. RADV_FROM_HANDLE(radv_semaphore, sem, pImportSemaphoreFdInfo->semaphore);
  4304. uint32_t *syncobj_dst = NULL;
  4305. if (pImportSemaphoreFdInfo->flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT) {
  4306. syncobj_dst = &sem->temp_syncobj;
  4307. } else {
  4308. syncobj_dst = &sem->syncobj;
  4309. }
  4310. switch(pImportSemaphoreFdInfo->handleType) {
  4311. case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT:
  4312. return radv_import_opaque_fd(device, pImportSemaphoreFdInfo->fd, syncobj_dst);
  4313. case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT:
  4314. return radv_import_sync_fd(device, pImportSemaphoreFdInfo->fd, syncobj_dst);
  4315. default:
  4316. unreachable("Unhandled semaphore handle type");
  4317. }
  4318. }
  4319. VkResult radv_GetSemaphoreFdKHR(VkDevice _device,
  4320. const VkSemaphoreGetFdInfoKHR *pGetFdInfo,
  4321. int *pFd)
  4322. {
  4323. RADV_FROM_HANDLE(radv_device, device, _device);
  4324. RADV_FROM_HANDLE(radv_semaphore, sem, pGetFdInfo->semaphore);
  4325. int ret;
  4326. uint32_t syncobj_handle;
  4327. if (sem->temp_syncobj)
  4328. syncobj_handle = sem->temp_syncobj;
  4329. else
  4330. syncobj_handle = sem->syncobj;
  4331. switch(pGetFdInfo->handleType) {
  4332. case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT:
  4333. ret = device->ws->export_syncobj(device->ws, syncobj_handle, pFd);
  4334. break;
  4335. case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT:
  4336. ret = device->ws->export_syncobj_to_sync_file(device->ws, syncobj_handle, pFd);
  4337. if (!ret) {
  4338. if (sem->temp_syncobj) {
  4339. close (sem->temp_syncobj);
  4340. sem->temp_syncobj = 0;
  4341. } else {
  4342. device->ws->reset_syncobj(device->ws, syncobj_handle);
  4343. }
  4344. }
  4345. break;
  4346. default:
  4347. unreachable("Unhandled semaphore handle type");
  4348. }
  4349. if (ret)
  4350. return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
  4351. return VK_SUCCESS;
  4352. }
  4353. void radv_GetPhysicalDeviceExternalSemaphoreProperties(
  4354. VkPhysicalDevice physicalDevice,
  4355. const VkPhysicalDeviceExternalSemaphoreInfo *pExternalSemaphoreInfo,
  4356. VkExternalSemaphoreProperties *pExternalSemaphoreProperties)
  4357. {
  4358. RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
  4359. /* Require has_syncobj_wait_for_submit for the syncobj signal ioctl introduced at virtually the same time */
  4360. if (pdevice->rad_info.has_syncobj_wait_for_submit &&
  4361. (pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT ||
  4362. pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT)) {
  4363. pExternalSemaphoreProperties->exportFromImportedHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
  4364. pExternalSemaphoreProperties->compatibleHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
  4365. pExternalSemaphoreProperties->externalSemaphoreFeatures = VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT |
  4366. VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
  4367. } else if (pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT) {
  4368. pExternalSemaphoreProperties->exportFromImportedHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
  4369. pExternalSemaphoreProperties->compatibleHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
  4370. pExternalSemaphoreProperties->externalSemaphoreFeatures = VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT |
  4371. VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
  4372. } else {
  4373. pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
  4374. pExternalSemaphoreProperties->compatibleHandleTypes = 0;
  4375. pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
  4376. }
  4377. }
  4378. VkResult radv_ImportFenceFdKHR(VkDevice _device,
  4379. const VkImportFenceFdInfoKHR *pImportFenceFdInfo)
  4380. {
  4381. RADV_FROM_HANDLE(radv_device, device, _device);
  4382. RADV_FROM_HANDLE(radv_fence, fence, pImportFenceFdInfo->fence);
  4383. uint32_t *syncobj_dst = NULL;
  4384. if (pImportFenceFdInfo->flags & VK_FENCE_IMPORT_TEMPORARY_BIT) {
  4385. syncobj_dst = &fence->temp_syncobj;
  4386. } else {
  4387. syncobj_dst = &fence->syncobj;
  4388. }
  4389. switch(pImportFenceFdInfo->handleType) {
  4390. case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT:
  4391. return radv_import_opaque_fd(device, pImportFenceFdInfo->fd, syncobj_dst);
  4392. case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT:
  4393. return radv_import_sync_fd(device, pImportFenceFdInfo->fd, syncobj_dst);
  4394. default:
  4395. unreachable("Unhandled fence handle type");
  4396. }
  4397. }
  4398. VkResult radv_GetFenceFdKHR(VkDevice _device,
  4399. const VkFenceGetFdInfoKHR *pGetFdInfo,
  4400. int *pFd)
  4401. {
  4402. RADV_FROM_HANDLE(radv_device, device, _device);
  4403. RADV_FROM_HANDLE(radv_fence, fence, pGetFdInfo->fence);
  4404. int ret;
  4405. uint32_t syncobj_handle;
  4406. if (fence->temp_syncobj)
  4407. syncobj_handle = fence->temp_syncobj;
  4408. else
  4409. syncobj_handle = fence->syncobj;
  4410. switch(pGetFdInfo->handleType) {
  4411. case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT:
  4412. ret = device->ws->export_syncobj(device->ws, syncobj_handle, pFd);
  4413. break;
  4414. case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT:
  4415. ret = device->ws->export_syncobj_to_sync_file(device->ws, syncobj_handle, pFd);
  4416. if (!ret) {
  4417. if (fence->temp_syncobj) {
  4418. close (fence->temp_syncobj);
  4419. fence->temp_syncobj = 0;
  4420. } else {
  4421. device->ws->reset_syncobj(device->ws, syncobj_handle);
  4422. }
  4423. }
  4424. break;
  4425. default:
  4426. unreachable("Unhandled fence handle type");
  4427. }
  4428. if (ret)
  4429. return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
  4430. return VK_SUCCESS;
  4431. }
  4432. void radv_GetPhysicalDeviceExternalFenceProperties(
  4433. VkPhysicalDevice physicalDevice,
  4434. const VkPhysicalDeviceExternalFenceInfo *pExternalFenceInfo,
  4435. VkExternalFenceProperties *pExternalFenceProperties)
  4436. {
  4437. RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
  4438. if (pdevice->rad_info.has_syncobj_wait_for_submit &&
  4439. (pExternalFenceInfo->handleType == VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT ||
  4440. pExternalFenceInfo->handleType == VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT)) {
  4441. pExternalFenceProperties->exportFromImportedHandleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
  4442. pExternalFenceProperties->compatibleHandleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
  4443. pExternalFenceProperties->externalFenceFeatures = VK_EXTERNAL_FENCE_FEATURE_EXPORTABLE_BIT |
  4444. VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
  4445. } else {
  4446. pExternalFenceProperties->exportFromImportedHandleTypes = 0;
  4447. pExternalFenceProperties->compatibleHandleTypes = 0;
  4448. pExternalFenceProperties->externalFenceFeatures = 0;
  4449. }
  4450. }
  4451. VkResult
  4452. radv_CreateDebugReportCallbackEXT(VkInstance _instance,
  4453. const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
  4454. const VkAllocationCallbacks* pAllocator,
  4455. VkDebugReportCallbackEXT* pCallback)
  4456. {
  4457. RADV_FROM_HANDLE(radv_instance, instance, _instance);
  4458. return vk_create_debug_report_callback(&instance->debug_report_callbacks,
  4459. pCreateInfo, pAllocator, &instance->alloc,
  4460. pCallback);
  4461. }
  4462. void
  4463. radv_DestroyDebugReportCallbackEXT(VkInstance _instance,
  4464. VkDebugReportCallbackEXT _callback,
  4465. const VkAllocationCallbacks* pAllocator)
  4466. {
  4467. RADV_FROM_HANDLE(radv_instance, instance, _instance);
  4468. vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
  4469. _callback, pAllocator, &instance->alloc);
  4470. }
  4471. void
  4472. radv_DebugReportMessageEXT(VkInstance _instance,
  4473. VkDebugReportFlagsEXT flags,
  4474. VkDebugReportObjectTypeEXT objectType,
  4475. uint64_t object,
  4476. size_t location,
  4477. int32_t messageCode,
  4478. const char* pLayerPrefix,
  4479. const char* pMessage)
  4480. {
  4481. RADV_FROM_HANDLE(radv_instance, instance, _instance);
  4482. vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
  4483. object, location, messageCode, pLayerPrefix, pMessage);
  4484. }
  4485. void
  4486. radv_GetDeviceGroupPeerMemoryFeatures(
  4487. VkDevice device,
  4488. uint32_t heapIndex,
  4489. uint32_t localDeviceIndex,
  4490. uint32_t remoteDeviceIndex,
  4491. VkPeerMemoryFeatureFlags* pPeerMemoryFeatures)
  4492. {
  4493. assert(localDeviceIndex == remoteDeviceIndex);
  4494. *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
  4495. VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
  4496. VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
  4497. VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
  4498. }
  4499. static const VkTimeDomainEXT radv_time_domains[] = {
  4500. VK_TIME_DOMAIN_DEVICE_EXT,
  4501. VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT,
  4502. VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT,
  4503. };
  4504. VkResult radv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
  4505. VkPhysicalDevice physicalDevice,
  4506. uint32_t *pTimeDomainCount,
  4507. VkTimeDomainEXT *pTimeDomains)
  4508. {
  4509. int d;
  4510. VK_OUTARRAY_MAKE(out, pTimeDomains, pTimeDomainCount);
  4511. for (d = 0; d < ARRAY_SIZE(radv_time_domains); d++) {
  4512. vk_outarray_append(&out, i) {
  4513. *i = radv_time_domains[d];
  4514. }
  4515. }
  4516. return vk_outarray_status(&out);
  4517. }
  4518. static uint64_t
  4519. radv_clock_gettime(clockid_t clock_id)
  4520. {
  4521. struct timespec current;
  4522. int ret;
  4523. ret = clock_gettime(clock_id, &current);
  4524. if (ret < 0 && clock_id == CLOCK_MONOTONIC_RAW)
  4525. ret = clock_gettime(CLOCK_MONOTONIC, &current);
  4526. if (ret < 0)
  4527. return 0;
  4528. return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;
  4529. }
  4530. VkResult radv_GetCalibratedTimestampsEXT(
  4531. VkDevice _device,
  4532. uint32_t timestampCount,
  4533. const VkCalibratedTimestampInfoEXT *pTimestampInfos,
  4534. uint64_t *pTimestamps,
  4535. uint64_t *pMaxDeviation)
  4536. {
  4537. RADV_FROM_HANDLE(radv_device, device, _device);
  4538. uint32_t clock_crystal_freq = device->physical_device->rad_info.clock_crystal_freq;
  4539. int d;
  4540. uint64_t begin, end;
  4541. uint64_t max_clock_period = 0;
  4542. begin = radv_clock_gettime(CLOCK_MONOTONIC_RAW);
  4543. for (d = 0; d < timestampCount; d++) {
  4544. switch (pTimestampInfos[d].timeDomain) {
  4545. case VK_TIME_DOMAIN_DEVICE_EXT:
  4546. pTimestamps[d] = device->ws->query_value(device->ws,
  4547. RADEON_TIMESTAMP);
  4548. uint64_t device_period = DIV_ROUND_UP(1000000, clock_crystal_freq);
  4549. max_clock_period = MAX2(max_clock_period, device_period);
  4550. break;
  4551. case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
  4552. pTimestamps[d] = radv_clock_gettime(CLOCK_MONOTONIC);
  4553. max_clock_period = MAX2(max_clock_period, 1);
  4554. break;
  4555. case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
  4556. pTimestamps[d] = begin;
  4557. break;
  4558. default:
  4559. pTimestamps[d] = 0;
  4560. break;
  4561. }
  4562. }
  4563. end = radv_clock_gettime(CLOCK_MONOTONIC_RAW);
  4564. /*
  4565. * The maximum deviation is the sum of the interval over which we
  4566. * perform the sampling and the maximum period of any sampled
  4567. * clock. That's because the maximum skew between any two sampled
  4568. * clock edges is when the sampled clock with the largest period is
  4569. * sampled at the end of that period but right at the beginning of the
  4570. * sampling interval and some other clock is sampled right at the
  4571. * begining of its sampling period and right at the end of the
  4572. * sampling interval. Let's assume the GPU has the longest clock
  4573. * period and that the application is sampling GPU and monotonic:
  4574. *
  4575. * s e
  4576. * w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
  4577. * Raw -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
  4578. *
  4579. * g
  4580. * 0 1 2 3
  4581. * GPU -----_____-----_____-----_____-----_____
  4582. *
  4583. * m
  4584. * x y z 0 1 2 3 4 5 6 7 8 9 a b c
  4585. * Monotonic -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
  4586. *
  4587. * Interval <----------------->
  4588. * Deviation <-------------------------->
  4589. *
  4590. * s = read(raw) 2
  4591. * g = read(GPU) 1
  4592. * m = read(monotonic) 2
  4593. * e = read(raw) b
  4594. *
  4595. * We round the sample interval up by one tick to cover sampling error
  4596. * in the interval clock
  4597. */
  4598. uint64_t sample_interval = end - begin + 1;
  4599. *pMaxDeviation = sample_interval + max_clock_period;
  4600. return VK_SUCCESS;
  4601. }