Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

radv_device.c 198KB

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