renderer_webgpu.cpp 151 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "bgfx_p.h"
  6. //#define DAWN_ENABLE_BACKEND_D3D12
  7. #define DAWN_ENABLE_BACKEND_VULKAN
  8. #if BGFX_CONFIG_RENDERER_WEBGPU
  9. # include "renderer_webgpu.h"
  10. # include "renderer.h"
  11. # include "debug_renderdoc.h"
  12. # include "emscripten.h"
  13. # include "shader_spirv.h"
  14. # if BX_PLATFORM_ANDROID
  15. # define VK_USE_PLATFORM_ANDROID_KHR
  16. # elif BX_PLATFORM_LINUX
  17. # define VK_USE_PLATFORM_XLIB_KHR
  18. # define VK_USE_PLATFORM_XCB_KHR
  19. # elif BX_PLATFORM_WINDOWS
  20. # define VK_USE_PLATFORM_WIN32_KHR
  21. # elif BX_PLATFORM_OSX
  22. # define VK_USE_PLATFORM_MACOS_MVK
  23. # endif // BX_PLATFORM_*
  24. # define VK_NO_STDINT_H
  25. # define VK_NO_PROTOTYPES
  26. # include <vulkan-local/vulkan.h>
  27. # if BX_PLATFORM_EMSCRIPTEN
  28. # include "emscripten.h"
  29. # include "emscripten/html5_webgpu.h"
  30. # else
  31. # ifdef DAWN_ENABLE_BACKEND_D3D12
  32. # include <dawn_native/D3D12Backend.h>
  33. # endif // !BX_PLATFORM_EMSCRIPTEN
  34. # ifdef DAWN_ENABLE_BACKEND_VULKAN
  35. # include <dawn_native/VulkanBackend.h>
  36. # endif // DAWN_ENABLE_BACKEND_VULKAN
  37. # include <dawn_native/DawnNative.h>
  38. # include <dawn/dawn_wsi.h>
  39. # include <dawn/dawn_proc.h>
  40. # endif // !BX_PLATFORM_EMSCRIPTEN
  41. namespace bgfx { namespace webgpu
  42. {
  43. // TODO (hugoam) cleanup
  44. template <class T>
  45. T defaultDescriptor() { return T(); }
  46. template <> wgpu::BlendComponent defaultDescriptor() { return { wgpu::BlendOperation::Add, wgpu::BlendFactor::One, wgpu::BlendFactor::Zero }; }
  47. template <> wgpu::ColorTargetState defaultDescriptor() { return { NULL, wgpu::TextureFormat::RGBA8Unorm, NULL, wgpu::ColorWriteMask::All }; }
  48. template <> wgpu::StencilFaceState defaultDescriptor() { return { wgpu::CompareFunction::Always, wgpu::StencilOperation::Keep, wgpu::StencilOperation::Keep, wgpu::StencilOperation::Keep }; }
  49. template <> wgpu::VertexState defaultDescriptor() { return { NULL, {}, "main", 0, NULL }; }
  50. template <> wgpu::FragmentState defaultDescriptor() { return { NULL, {}, "main", 0, NULL }; }
  51. template <> wgpu::VertexBufferLayout defaultDescriptor() { return { 0, wgpu::InputStepMode::Vertex, 0, NULL }; }
  52. template <> wgpu::VertexAttribute defaultDescriptor() { return { wgpu::VertexFormat::Float, 0, 0 }; }
  53. template <> wgpu::PrimitiveState defaultDescriptor() { return { NULL, wgpu::PrimitiveTopology::TriangleList, wgpu::IndexFormat::Undefined, wgpu::FrontFace::CCW, wgpu::CullMode::None }; }
  54. template <> wgpu::DepthStencilState defaultDescriptor() { return { NULL, wgpu::TextureFormat::Depth24PlusStencil8, false, wgpu::CompareFunction::Always, defaultDescriptor<wgpu::StencilFaceState>(), defaultDescriptor<wgpu::StencilFaceState>(), 0xff, 0xff }; }
  55. template <> wgpu::PipelineLayoutDescriptor defaultDescriptor() { return { NULL, "", 0, NULL }; }
  56. template <> wgpu::TextureViewDescriptor defaultDescriptor() { return {}; }
  57. template <> wgpu::RenderPassColorAttachment defaultDescriptor() { return { {}, {}, wgpu::LoadOp::Clear, wgpu::StoreOp::Store, { 0.0f, 0.0f, 0.0f, 0.0f } }; }
  58. template <> wgpu::RenderPassDepthStencilAttachment defaultDescriptor() { return { {}, wgpu::LoadOp::Clear, wgpu::StoreOp::Store, 1.0f, false, wgpu::LoadOp::Clear, wgpu::StoreOp::Store, 0, false }; }
  59. RenderPassDescriptor::RenderPassDescriptor()
  60. {
  61. depthStencilAttachment = defaultDescriptor<wgpu::RenderPassDepthStencilAttachment>();
  62. for(uint32_t i = 0; i < kMaxColorAttachments; ++i)
  63. {
  64. colorAttachments[i] = defaultDescriptor<wgpu::RenderPassColorAttachment>();
  65. }
  66. desc = defaultDescriptor<wgpu::RenderPassDescriptor>();
  67. //desc.colorAttachmentCount = colorAttachmentCount;
  68. desc.colorAttachments = colorAttachments;
  69. desc.colorAttachmentCount = 1; // TODO (hugoam) set it properly everywhere
  70. }
  71. VertexStateDescriptor::VertexStateDescriptor()
  72. {
  73. for(uint32_t i = 0; i < kMaxVertexInputs; ++i)
  74. {
  75. buffers[i] = defaultDescriptor<wgpu::VertexBufferLayout>();
  76. }
  77. for (uint32_t i = 0; i < kMaxVertexAttributes; ++i)
  78. {
  79. attributes[i] = defaultDescriptor<wgpu::VertexAttribute>();
  80. }
  81. buffers[0].attributes = &attributes[0];
  82. //buffers[0].attributeCount = numAttributes;
  83. desc = defaultDescriptor<wgpu::VertexState>();
  84. desc.buffers = buffers;
  85. //desc.vertexBufferCount = numVertexBuffers;
  86. }
  87. RenderPipelineDescriptor::RenderPipelineDescriptor()
  88. {
  89. //vertex = defaultDescriptor<wgpu::VertexState>();
  90. fragment = defaultDescriptor<wgpu::FragmentState>();
  91. depthStencil = defaultDescriptor<wgpu::DepthStencilState>();
  92. for(uint32_t i = 0; i < kMaxColorAttachments; ++i)
  93. {
  94. targets[i] = defaultDescriptor<wgpu::ColorTargetState>();
  95. }
  96. desc = defaultDescriptor<wgpu::RenderPipelineDescriptor2>();
  97. desc.primitive.topology = wgpu::PrimitiveTopology::TriangleList;
  98. desc.multisample.count = 1;
  99. fragment.targetCount = 1;
  100. fragment.targets = targets;
  101. //wgpu::VertexStateDescriptor inputState = inputState.descriptor();
  102. desc.vertex = defaultDescriptor<wgpu::VertexState>();
  103. desc.fragment = NULL;
  104. //desc.vertexState = &inputState;
  105. desc.primitive = defaultDescriptor<wgpu::PrimitiveState>();
  106. desc.depthStencil = NULL;
  107. }
  108. // TODO (hugoam) cleanup (end)
  109. static char s_viewName[BGFX_CONFIG_MAX_VIEWS][BGFX_CONFIG_MAX_VIEW_NAME];
  110. inline void setViewType(ViewId _view, const bx::StringView _str)
  111. {
  112. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION || BGFX_CONFIG_PROFILER) )
  113. {
  114. bx::memCopy(&s_viewName[_view][3], _str.getPtr(), _str.getLength() );
  115. }
  116. }
  117. struct PrimInfo
  118. {
  119. wgpu::PrimitiveTopology m_type;
  120. uint32_t m_min;
  121. uint32_t m_div;
  122. uint32_t m_sub;
  123. };
  124. static const PrimInfo s_primInfo[] =
  125. {
  126. { wgpu::PrimitiveTopology::TriangleList, 3, 3, 0 },
  127. { wgpu::PrimitiveTopology::TriangleStrip, 3, 1, 2 },
  128. { wgpu::PrimitiveTopology::LineList, 2, 2, 0 },
  129. { wgpu::PrimitiveTopology::LineStrip, 2, 1, 1 },
  130. { wgpu::PrimitiveTopology::PointList, 1, 1, 0 },
  131. };
  132. BX_STATIC_ASSERT(Topology::Count == BX_COUNTOF(s_primInfo) );
  133. static const wgpu::VertexFormat s_attribType[][4][2] =
  134. {
  135. { // Uint8
  136. { wgpu::VertexFormat::Uint8x2, wgpu::VertexFormat::Unorm8x2 },
  137. { wgpu::VertexFormat::Uint8x2, wgpu::VertexFormat::Unorm8x2 },
  138. { wgpu::VertexFormat::Uint8x4, wgpu::VertexFormat::Unorm8x4 },
  139. { wgpu::VertexFormat::Uint8x4, wgpu::VertexFormat::Unorm8x4 },
  140. },
  141. { // Uint10
  142. { wgpu::VertexFormat::Uint16x2, wgpu::VertexFormat::Unorm16x2 },
  143. { wgpu::VertexFormat::Uint16x2, wgpu::VertexFormat::Unorm16x2 },
  144. { wgpu::VertexFormat::Uint16x4, wgpu::VertexFormat::Unorm16x4 },
  145. { wgpu::VertexFormat::Uint16x4, wgpu::VertexFormat::Unorm16x4 },
  146. },
  147. { // Int16
  148. { wgpu::VertexFormat::Sint16x2, wgpu::VertexFormat::Snorm16x2 },
  149. { wgpu::VertexFormat::Sint16x2, wgpu::VertexFormat::Snorm16x2 },
  150. { wgpu::VertexFormat::Sint16x4, wgpu::VertexFormat::Snorm16x4 },
  151. { wgpu::VertexFormat::Sint16x4, wgpu::VertexFormat::Snorm16x4 },
  152. },
  153. { // Half
  154. { wgpu::VertexFormat::Float16x2, wgpu::VertexFormat::Float16x2 },
  155. { wgpu::VertexFormat::Float16x2, wgpu::VertexFormat::Float16x2 },
  156. { wgpu::VertexFormat::Float16x4, wgpu::VertexFormat::Float16x4 },
  157. { wgpu::VertexFormat::Float16x4, wgpu::VertexFormat::Float16x4 },
  158. },
  159. { // Float
  160. { wgpu::VertexFormat::Float32, wgpu::VertexFormat::Float32 },
  161. { wgpu::VertexFormat::Float32x2, wgpu::VertexFormat::Float32x2 },
  162. { wgpu::VertexFormat::Float32x3, wgpu::VertexFormat::Float32x3 },
  163. { wgpu::VertexFormat::Float32x4, wgpu::VertexFormat::Float32x4 },
  164. },
  165. };
  166. BX_STATIC_ASSERT(AttribType::Count == BX_COUNTOF(s_attribType) );
  167. static const wgpu::CullMode s_cullMode[] =
  168. {
  169. wgpu::CullMode::None,
  170. wgpu::CullMode::Front,
  171. wgpu::CullMode::Back,
  172. wgpu::CullMode::None,
  173. };
  174. static const wgpu::BlendFactor s_blendFactor[][2] =
  175. {
  176. { wgpu::BlendFactor(0), wgpu::BlendFactor(0) }, // ignored
  177. { wgpu::BlendFactor::Zero, wgpu::BlendFactor::Zero }, // ZERO
  178. { wgpu::BlendFactor::One, wgpu::BlendFactor::One }, // ONE
  179. { wgpu::BlendFactor::SrcColor, wgpu::BlendFactor::SrcAlpha }, // SRC_COLOR
  180. { wgpu::BlendFactor::OneMinusSrcColor, wgpu::BlendFactor::OneMinusSrcAlpha }, // INV_SRC_COLOR
  181. { wgpu::BlendFactor::SrcAlpha, wgpu::BlendFactor::SrcAlpha }, // SRC_ALPHA
  182. { wgpu::BlendFactor::OneMinusSrcAlpha, wgpu::BlendFactor::OneMinusSrcAlpha }, // INV_SRC_ALPHA
  183. { wgpu::BlendFactor::DstAlpha, wgpu::BlendFactor::DstAlpha }, // DST_ALPHA
  184. { wgpu::BlendFactor::OneMinusDstAlpha, wgpu::BlendFactor::OneMinusDstAlpha }, // INV_DST_ALPHA
  185. { wgpu::BlendFactor::DstColor, wgpu::BlendFactor::DstAlpha }, // DST_COLOR
  186. { wgpu::BlendFactor::OneMinusDstColor, wgpu::BlendFactor::OneMinusDstAlpha }, // INV_DST_COLOR
  187. { wgpu::BlendFactor::SrcAlphaSaturated, wgpu::BlendFactor::One }, // SRC_ALPHA_SAT
  188. { wgpu::BlendFactor::BlendColor, wgpu::BlendFactor::BlendColor }, // FACTOR
  189. { wgpu::BlendFactor::OneMinusBlendColor, wgpu::BlendFactor::OneMinusBlendColor }, // INV_FACTOR
  190. };
  191. static const wgpu::BlendOperation s_blendEquation[] =
  192. {
  193. wgpu::BlendOperation::Add,
  194. wgpu::BlendOperation::Subtract,
  195. wgpu::BlendOperation::ReverseSubtract,
  196. wgpu::BlendOperation::Min,
  197. wgpu::BlendOperation::Max,
  198. };
  199. static const wgpu::CompareFunction s_cmpFunc[] =
  200. {
  201. wgpu::CompareFunction::Always, // ignored
  202. wgpu::CompareFunction::Less,
  203. wgpu::CompareFunction::LessEqual,
  204. wgpu::CompareFunction::Equal,
  205. wgpu::CompareFunction::GreaterEqual,
  206. wgpu::CompareFunction::Greater,
  207. wgpu::CompareFunction::NotEqual,
  208. wgpu::CompareFunction::Never,
  209. wgpu::CompareFunction::Always,
  210. };
  211. static const wgpu::StencilOperation s_stencilOp[] =
  212. {
  213. wgpu::StencilOperation::Zero,
  214. wgpu::StencilOperation::Keep,
  215. wgpu::StencilOperation::Replace,
  216. wgpu::StencilOperation::IncrementWrap,
  217. wgpu::StencilOperation::IncrementClamp,
  218. wgpu::StencilOperation::DecrementWrap,
  219. wgpu::StencilOperation::DecrementClamp,
  220. wgpu::StencilOperation::Invert,
  221. };
  222. static const wgpu::AddressMode s_textureAddress[] =
  223. {
  224. wgpu::AddressMode::Repeat,
  225. wgpu::AddressMode::MirrorRepeat,
  226. wgpu::AddressMode::ClampToEdge,
  227. wgpu::AddressMode(0), // Border ? ClampToZero ?
  228. };
  229. static const wgpu::FilterMode s_textureFilterMinMag[] =
  230. {
  231. wgpu::FilterMode::Linear,
  232. wgpu::FilterMode::Nearest,
  233. wgpu::FilterMode::Linear,
  234. };
  235. static const wgpu::FilterMode s_textureFilterMip[] =
  236. {
  237. wgpu::FilterMode::Linear,
  238. wgpu::FilterMode::Nearest,
  239. };
  240. struct TextureFormatInfo
  241. {
  242. wgpu::TextureFormat m_fmt;
  243. wgpu::TextureFormat m_fmtSrgb;
  244. };
  245. static TextureFormatInfo s_textureFormat[] =
  246. {
  247. { wgpu::TextureFormat::BC1RGBAUnorm, wgpu::TextureFormat::BC1RGBAUnormSrgb }, // BC1
  248. { wgpu::TextureFormat::BC2RGBAUnorm, wgpu::TextureFormat::BC2RGBAUnormSrgb }, // BC2
  249. { wgpu::TextureFormat::BC3RGBAUnorm, wgpu::TextureFormat::BC3RGBAUnormSrgb }, // BC3
  250. { wgpu::TextureFormat::BC4RUnorm, wgpu::TextureFormat::Undefined }, // BC4 // BC4RSnorm ??
  251. { wgpu::TextureFormat::BC5RGUnorm, wgpu::TextureFormat::Undefined }, // BC5 // BC5RGSnorm ??
  252. { wgpu::TextureFormat::BC6HRGBUfloat, wgpu::TextureFormat::Undefined }, // BC6H // BC6HRGBSfloat ??
  253. { wgpu::TextureFormat::BC7RGBAUnorm, wgpu::TextureFormat::BC7RGBAUnormSrgb }, // BC7
  254. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ETC1
  255. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ETC2
  256. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ETC2A
  257. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ETC2A1
  258. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // PTC12
  259. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // PTC14
  260. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // PTC12A
  261. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // PTC14A
  262. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // PTC22
  263. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // PTC24
  264. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ATC
  265. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ATCE
  266. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ATCI
  267. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ASTC4x4
  268. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ASTC5x5
  269. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ASTC6x6
  270. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ASTC8x5
  271. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ASTC8x6
  272. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // ASTC10x5
  273. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // Unknown
  274. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // R1
  275. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // A8
  276. { wgpu::TextureFormat::R8Unorm, wgpu::TextureFormat::Undefined }, // R8
  277. { wgpu::TextureFormat::R8Sint, wgpu::TextureFormat::Undefined }, // R8I
  278. { wgpu::TextureFormat::R8Uint, wgpu::TextureFormat::Undefined }, // R8U
  279. { wgpu::TextureFormat::R8Snorm, wgpu::TextureFormat::Undefined }, // R8S
  280. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // R16
  281. { wgpu::TextureFormat::R16Sint, wgpu::TextureFormat::Undefined }, // R16I
  282. { wgpu::TextureFormat::R16Uint, wgpu::TextureFormat::Undefined }, // R16U
  283. { wgpu::TextureFormat::R16Float, wgpu::TextureFormat::Undefined }, // R16F
  284. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // R16S
  285. { wgpu::TextureFormat::R32Sint, wgpu::TextureFormat::Undefined }, // R32I
  286. { wgpu::TextureFormat::R32Uint, wgpu::TextureFormat::Undefined }, // R32U
  287. { wgpu::TextureFormat::R32Float, wgpu::TextureFormat::Undefined }, // R32F
  288. { wgpu::TextureFormat::RG8Unorm, wgpu::TextureFormat::Undefined }, // RG8
  289. { wgpu::TextureFormat::RG8Sint, wgpu::TextureFormat::Undefined }, // RG8I
  290. { wgpu::TextureFormat::RG8Uint, wgpu::TextureFormat::Undefined }, // RG8U
  291. { wgpu::TextureFormat::RG8Snorm, wgpu::TextureFormat::Undefined }, // RG8S
  292. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RG16
  293. { wgpu::TextureFormat::RG16Sint, wgpu::TextureFormat::Undefined }, // RG16I
  294. { wgpu::TextureFormat::RG16Uint, wgpu::TextureFormat::Undefined }, // RG16U
  295. { wgpu::TextureFormat::RG16Float, wgpu::TextureFormat::Undefined }, // RG16F
  296. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RG16S
  297. { wgpu::TextureFormat::RG32Sint, wgpu::TextureFormat::Undefined }, // RG32I
  298. { wgpu::TextureFormat::RG32Uint, wgpu::TextureFormat::Undefined }, // RG32U
  299. { wgpu::TextureFormat::RG32Float, wgpu::TextureFormat::Undefined }, // RG32F
  300. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGB8
  301. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGB8I
  302. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGB8U
  303. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGB8S
  304. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGB9E5F
  305. { wgpu::TextureFormat::BGRA8Unorm, wgpu::TextureFormat::BGRA8UnormSrgb }, // BGRA8
  306. { wgpu::TextureFormat::RGBA8Unorm, wgpu::TextureFormat::RGBA8UnormSrgb }, // RGBA8
  307. { wgpu::TextureFormat::RGBA8Sint, wgpu::TextureFormat::Undefined }, // RGBA8I
  308. { wgpu::TextureFormat::RGBA8Uint, wgpu::TextureFormat::Undefined }, // RGBA8U
  309. { wgpu::TextureFormat::RGBA8Snorm, wgpu::TextureFormat::Undefined }, // RGBA8S
  310. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGBA16
  311. { wgpu::TextureFormat::RGBA16Sint, wgpu::TextureFormat::Undefined }, // RGBA16I
  312. { wgpu::TextureFormat::RGBA16Uint, wgpu::TextureFormat::Undefined }, // RGBA16U
  313. { wgpu::TextureFormat::RGBA16Float, wgpu::TextureFormat::Undefined }, // RGBA16F
  314. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGBA16S
  315. { wgpu::TextureFormat::RGBA32Sint, wgpu::TextureFormat::Undefined }, // RGBA32I
  316. { wgpu::TextureFormat::RGBA32Uint, wgpu::TextureFormat::Undefined }, // RGBA32U
  317. { wgpu::TextureFormat::RGBA32Float, wgpu::TextureFormat::Undefined }, // RGBA32F
  318. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // B5G6R5
  319. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // R5G6B5
  320. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // BGRA4
  321. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGBA4
  322. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // BGR5A1
  323. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // RGB5A1
  324. { wgpu::TextureFormat::RGB10A2Unorm, wgpu::TextureFormat::Undefined }, // RGB10A2
  325. { wgpu::TextureFormat::RG11B10Ufloat, wgpu::TextureFormat::Undefined }, // RG11B10F
  326. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // UnknownDepth
  327. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // D16
  328. { wgpu::TextureFormat::Depth24Plus, wgpu::TextureFormat::Undefined }, // D24
  329. { wgpu::TextureFormat::Depth24PlusStencil8, wgpu::TextureFormat::Undefined }, // D24S8
  330. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // D32
  331. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // D16F
  332. { wgpu::TextureFormat::Undefined, wgpu::TextureFormat::Undefined }, // D24F
  333. { wgpu::TextureFormat::Depth32Float, wgpu::TextureFormat::Undefined }, // D32F
  334. { wgpu::TextureFormat::Stencil8, wgpu::TextureFormat::Undefined }, // D0S8
  335. };
  336. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormat));
  337. int32_t s_msaa[] =
  338. {
  339. 1,
  340. 2,
  341. 4,
  342. 8,
  343. 16,
  344. };
  345. struct RendererContextWgpu;
  346. static RendererContextWgpu* s_renderWgpu;
  347. static bool s_ignoreError = false;
  348. #if !BX_PLATFORM_EMSCRIPTEN
  349. DawnSwapChainImplementation(*createSwapChain)(wgpu::Device device, void* nwh);
  350. # if defined(DAWN_ENABLE_BACKEND_D3D12)
  351. DawnSwapChainImplementation CreateSwapChainD3D12(wgpu::Device device, void* nwh)
  352. {
  353. HWND win32Window = (HWND)nwh;
  354. return dawn_native::d3d12::CreateNativeSwapChainImpl(device.Get(), win32Window);
  355. }
  356. # endif // defined(DAWN_ENABLE_BACKEND_D3D12)
  357. # if defined(DAWN_ENABLE_BACKEND_VULKAN)
  358. DawnSwapChainImplementation CreateSwapChainVulkan(wgpu::Device device, void* nwh)
  359. {
  360. VkInstance instance = dawn_native::vulkan::GetInstance(device.Get());
  361. PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR)dawn_native::vulkan::GetInstanceProcAddr(device.Get(), "vkCreateWin32SurfaceKHR");
  362. VkSurfaceKHR surface;
  363. # if BX_PLATFORM_WINDOWS
  364. // Copied from renderer_vk.cpp -> needs refactor
  365. {
  366. VkWin32SurfaceCreateInfoKHR sci;
  367. sci.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
  368. sci.pNext = NULL;
  369. sci.flags = 0;
  370. sci.hinstance = (HINSTANCE)GetModuleHandle(NULL);
  371. sci.hwnd = (HWND)nwh;
  372. VkResult result = vkCreateWin32SurfaceKHR(instance, &sci, NULL, &surface);
  373. }
  374. # endif // BX_PLATFORM_WINDOWS
  375. return dawn_native::vulkan::CreateNativeSwapChainImpl(device.Get(), surface);
  376. }
  377. # endif // defined(DAWN_ENABLE_BACKEND_VULKAN)
  378. #endif // !BX_PLATFORM_EMSCRIPTEN
  379. struct RendererContextWgpu : public RendererContextI
  380. {
  381. RendererContextWgpu()
  382. : m_frameIndex(0)
  383. , m_numWindows(0)
  384. , m_rtMsaa(false)
  385. , m_capture(NULL)
  386. , m_captureSize(0)
  387. {
  388. bx::memSet(&m_windows, 0xff, sizeof(m_windows) );
  389. }
  390. ~RendererContextWgpu()
  391. {
  392. }
  393. bool init(const Init& _init)
  394. {
  395. BX_UNUSED(_init);
  396. BX_TRACE("Init.");
  397. if (_init.debug
  398. || _init.profile)
  399. {
  400. m_renderDocDll = loadRenderDoc();
  401. }
  402. setGraphicsDebuggerPresent(NULL != m_renderDocDll);
  403. m_fbh.idx = kInvalidHandle;
  404. bx::memSet(m_uniforms, 0, sizeof(m_uniforms) );
  405. bx::memSet(&m_resolution, 0, sizeof(m_resolution) );
  406. #if !BX_PLATFORM_EMSCRIPTEN
  407. // Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on
  408. // their respective platforms, and Vulkan is preferred to OpenGL
  409. # if defined(DAWN_ENABLE_BACKEND_D3D12)
  410. static wgpu::BackendType backendType = wgpu::BackendType::D3D12;
  411. # elif defined(DAWN_ENABLE_BACKEND_METAL)
  412. static wgpu::BackendType backendType = wgpu::BackendType::Metal;
  413. # elif defined(DAWN_ENABLE_BACKEND_OPENGL)
  414. static wgpu::BackendType backendType = wgpu::BackendType::OpenGL;
  415. # elif defined(DAWN_ENABLE_BACKEND_VULKAN)
  416. static wgpu::BackendType backendType = wgpu::BackendType::Vulkan;
  417. # else
  418. # error "Unknown platform."
  419. # endif // defined(DAWN_ENABLE_BACKEND_*)
  420. if (BX_ENABLED(BGFX_CONFIG_DEBUG))
  421. {
  422. m_instance.EnableBackendValidation(true);
  423. }
  424. m_instance.DiscoverDefaultAdapters();
  425. dawn_native::Adapter backendAdapter;
  426. std::vector<dawn_native::Adapter> adapters = m_instance.GetAdapters();
  427. for (dawn_native::Adapter& adapter : adapters)
  428. {
  429. wgpu::AdapterProperties properties;
  430. adapter.GetProperties(&properties);
  431. if (properties.backendType == backendType)
  432. {
  433. backendAdapter = adapter;
  434. break;
  435. }
  436. }
  437. //BX_ASSERT(adapterIt != adapters.end());
  438. dawn_native::DeviceDescriptor desc;
  439. # if defined(DAWN_ENABLE_BACKEND_D3D12)
  440. desc.forceEnabledToggles.push_back("use_dxc");
  441. # endif
  442. desc.forceDisabledToggles.push_back("disallow_unsafe_apis");
  443. WGPUDevice backendDevice = backendAdapter.CreateDevice(&desc);
  444. DawnProcTable backendProcs = dawn_native::GetProcs();
  445. using CreateSwapChain = DawnSwapChainImplementation (*)(wgpu::Device device, void* nwh);
  446. # if defined(DAWN_ENABLE_BACKEND_D3D12)
  447. createSwapChain = CreateSwapChainD3D12;
  448. # elif defined(DAWN_ENABLE_BACKEND_METAL)
  449. createSwapChain = CreateSwapChainMetal;
  450. # elif defined(DAWN_ENABLE_BACKEND_NULL)
  451. createSwapChain = CreateSwapChainNull;
  452. # elif defined(DAWN_ENABLE_BACKEND_OPENGL)
  453. createSwapChain = CreateSwapChainOpenGL;
  454. # elif defined(DAWN_ENABLE_BACKEND_VULKAN)
  455. createSwapChain = CreateSwapChainVulkan;
  456. # endif // defined(DAWN_ENABLE_BACKEND_*)
  457. // Choose whether to use the backend procs and devices directly, or set up the wire.
  458. WGPUDevice cDevice = backendDevice;
  459. DawnProcTable procs = backendProcs;
  460. dawnProcSetProcs(&procs);
  461. m_device = wgpu::Device::Acquire(cDevice);
  462. #else
  463. m_device = wgpu::Device(emscripten_webgpu_get_device());
  464. #endif // !BX_PLATFORM_EMSCRIPTEN
  465. auto PrintDeviceError = [](WGPUErrorType errorType, const char* message, void*) {
  466. BX_UNUSED(errorType);
  467. if (s_ignoreError)
  468. {
  469. BX_TRACE("Device error: %s", message);
  470. }
  471. else
  472. {
  473. BX_ASSERT(false, "Device error: %s", message);
  474. }
  475. s_ignoreError = false;
  476. };
  477. if (!m_device)
  478. {
  479. BX_WARN(!m_device, "Unable to create WebGPU device.");
  480. return false;
  481. }
  482. m_device.SetUncapturedErrorCallback(PrintDeviceError, NULL);
  483. bool success = m_mainFrameBuffer.create(
  484. 0
  485. , g_platformData.nwh
  486. , _init.resolution.width
  487. , _init.resolution.height
  488. , TextureFormat::Unknown
  489. , TextureFormat::UnknownDepth
  490. );
  491. m_numWindows = 1;
  492. if (!success)
  493. {
  494. return false;
  495. }
  496. m_queue = m_device.GetQueue();
  497. m_cmd.init(m_queue);
  498. //BGFX_FATAL(NULL != m_cmd.m_commandQueue, Fatal::UnableToInitialize, "Unable to create Metal device.");
  499. for (uint8_t ii = 0; ii < BGFX_CONFIG_MAX_FRAME_LATENCY; ++ii)
  500. {
  501. BX_TRACE("Create scratch buffer %d", ii);
  502. m_scratchBuffers[ii].create(BGFX_CONFIG_MAX_DRAW_CALLS * 128);
  503. m_bindStateCache[ii].create(); // (1024);
  504. }
  505. for (uint8_t ii = 0; ii < WEBGPU_NUM_UNIFORM_BUFFERS; ++ii)
  506. {
  507. bool mapped = true; // ii == WEBGPU_NUM_UNIFORM_BUFFERS - 1;
  508. m_uniformBuffers[ii].create(BGFX_CONFIG_MAX_DRAW_CALLS * 128, mapped);
  509. }
  510. g_caps.supported |= (0
  511. | BGFX_CAPS_ALPHA_TO_COVERAGE
  512. | BGFX_CAPS_BLEND_INDEPENDENT
  513. | BGFX_CAPS_FRAGMENT_DEPTH
  514. | BGFX_CAPS_INDEX32
  515. | BGFX_CAPS_INSTANCING
  516. // | BGFX_CAPS_OCCLUSION_QUERY
  517. | BGFX_CAPS_SWAP_CHAIN
  518. | BGFX_CAPS_TEXTURE_2D_ARRAY
  519. | BGFX_CAPS_TEXTURE_3D
  520. | BGFX_CAPS_TEXTURE_BLIT
  521. | BGFX_CAPS_TEXTURE_COMPARE_ALL
  522. | BGFX_CAPS_TEXTURE_COMPARE_LEQUAL
  523. | BGFX_CAPS_TEXTURE_READ_BACK
  524. | BGFX_CAPS_VERTEX_ATTRIB_HALF
  525. | BGFX_CAPS_VERTEX_ATTRIB_UINT10
  526. | BGFX_CAPS_COMPUTE
  527. );
  528. g_caps.limits.maxTextureSize = 8192;
  529. g_caps.limits.maxFBAttachments = 4;
  530. g_caps.supported |= BGFX_CAPS_TEXTURE_CUBE_ARRAY;
  531. g_caps.supported |= BGFX_CAPS_DRAW_INDIRECT;
  532. g_caps.limits.maxTextureLayers = 2048;
  533. g_caps.limits.maxVertexStreams = BGFX_CONFIG_MAX_VERTEX_STREAMS;
  534. // Maximum number of entries in the buffer argument table, per graphics or compute function are 31.
  535. // It is decremented by 1 because 1 entry is used for uniforms.
  536. g_caps.limits.maxComputeBindings = bx::uint32_min(30, BGFX_MAX_COMPUTE_BINDINGS);
  537. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  538. {
  539. uint16_t support = 0;
  540. support |= wgpu::TextureFormat::Undefined != s_textureFormat[ii].m_fmt
  541. ? BGFX_CAPS_FORMAT_TEXTURE_2D
  542. | BGFX_CAPS_FORMAT_TEXTURE_3D
  543. | BGFX_CAPS_FORMAT_TEXTURE_CUBE
  544. | BGFX_CAPS_FORMAT_TEXTURE_VERTEX
  545. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  546. ;
  547. support |= wgpu::TextureFormat::Undefined != s_textureFormat[ii].m_fmtSrgb
  548. ? BGFX_CAPS_FORMAT_TEXTURE_2D_SRGB
  549. | BGFX_CAPS_FORMAT_TEXTURE_3D_SRGB
  550. | BGFX_CAPS_FORMAT_TEXTURE_CUBE_SRGB
  551. | BGFX_CAPS_FORMAT_TEXTURE_VERTEX
  552. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  553. ;
  554. if (!bimg::isCompressed(bimg::TextureFormat::Enum(ii) ) )
  555. {
  556. support |= 0
  557. | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER
  558. // | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA
  559. ;
  560. }
  561. g_caps.formats[ii] = support;
  562. }
  563. g_caps.formats[TextureFormat::A8 ] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  564. g_caps.formats[TextureFormat::RG32I ] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  565. g_caps.formats[TextureFormat::RG32U ] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  566. g_caps.formats[TextureFormat::RGBA32I] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  567. g_caps.formats[TextureFormat::RGBA32U] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  568. g_caps.formats[TextureFormat::ETC2 ] =
  569. g_caps.formats[TextureFormat::ETC2A ] =
  570. g_caps.formats[TextureFormat::ETC2A1] =
  571. g_caps.formats[TextureFormat::PTC12 ] =
  572. g_caps.formats[TextureFormat::PTC14 ] =
  573. g_caps.formats[TextureFormat::PTC12A] =
  574. g_caps.formats[TextureFormat::PTC14A] =
  575. g_caps.formats[TextureFormat::B5G6R5] =
  576. g_caps.formats[TextureFormat::R5G6B5] =
  577. g_caps.formats[TextureFormat::BGRA4 ] =
  578. g_caps.formats[TextureFormat::RGBA4 ] =
  579. g_caps.formats[TextureFormat::BGR5A1] =
  580. g_caps.formats[TextureFormat::RGB5A1] = BGFX_CAPS_FORMAT_TEXTURE_NONE;
  581. g_caps.formats[TextureFormat::RGB9E5F] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  582. g_caps.formats[TextureFormat::RG11B10F] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  583. // disable compressed formats
  584. for (uint32_t ii = 0; ii < TextureFormat::Unknown; ++ii)
  585. {
  586. s_textureFormat[ii].m_fmt = wgpu::TextureFormat::Undefined;
  587. }
  588. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  589. {
  590. if (BGFX_CAPS_FORMAT_TEXTURE_NONE == g_caps.formats[ii])
  591. {
  592. s_textureFormat[ii].m_fmt = wgpu::TextureFormat::Undefined;
  593. s_textureFormat[ii].m_fmtSrgb = wgpu::TextureFormat::Undefined;
  594. }
  595. }
  596. for (uint32_t ii = 1, last = 0; ii < BX_COUNTOF(s_msaa); ++ii)
  597. {
  598. // TODO (hugoam)
  599. //const int32_t sampleCount = 1; //1<<ii;
  600. //if (m_device.supportsTextureSampleCount(sampleCount) )
  601. //{
  602. // s_msaa[ii] = sampleCount;
  603. // last = ii;
  604. //}
  605. //else
  606. {
  607. s_msaa[ii] = s_msaa[last];
  608. }
  609. }
  610. // Init reserved part of view name.
  611. for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
  612. {
  613. bx::snprintf(s_viewName[ii], BGFX_CONFIG_MAX_VIEW_NAME_RESERVED+1, "%3d ", ii);
  614. }
  615. m_gpuTimer.init();
  616. g_internalData.context = &m_device;
  617. return true;
  618. }
  619. void shutdown()
  620. {
  621. m_gpuTimer.shutdown();
  622. m_pipelineStateCache.invalidate();
  623. for (uint32_t ii = 0; ii < BX_COUNTOF(m_shaders); ++ii)
  624. {
  625. m_shaders[ii].destroy();
  626. }
  627. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  628. {
  629. m_textures[ii].destroy();
  630. }
  631. captureFinish();
  632. m_mainFrameBuffer.destroy();
  633. for (uint32_t ii = 0; ii < BX_COUNTOF(m_scratchBuffers); ++ii)
  634. {
  635. m_scratchBuffers[ii].destroy();
  636. }
  637. m_cmd.shutdown();
  638. }
  639. RendererType::Enum getRendererType() const override
  640. {
  641. return RendererType::WebGPU;
  642. }
  643. const char* getRendererName() const override
  644. {
  645. return BGFX_RENDERER_WEBGPU_NAME;
  646. }
  647. void createIndexBuffer(IndexBufferHandle _handle, const Memory* _mem, uint16_t _flags) override
  648. {
  649. m_indexBuffers[_handle.idx].create(_mem->size, _mem->data, _flags);
  650. }
  651. void destroyIndexBuffer(IndexBufferHandle _handle) override
  652. {
  653. m_indexBuffers[_handle.idx].destroy();
  654. }
  655. void createVertexLayout(VertexLayoutHandle _handle, const VertexLayout& _decl) override
  656. {
  657. VertexLayout& decl = m_vertexDecls[_handle.idx];
  658. bx::memCopy(&decl, &_decl, sizeof(VertexLayout) );
  659. dump(decl);
  660. }
  661. void destroyVertexLayout(VertexLayoutHandle /*_handle*/) override
  662. {
  663. }
  664. void createVertexBuffer(VertexBufferHandle _handle, const Memory* _mem, VertexLayoutHandle _declHandle, uint16_t _flags) override
  665. {
  666. m_vertexBuffers[_handle.idx].create(_mem->size, _mem->data, _declHandle, _flags);
  667. }
  668. void destroyVertexBuffer(VertexBufferHandle _handle) override
  669. {
  670. m_vertexBuffers[_handle.idx].destroy();
  671. }
  672. void createDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size, uint16_t _flags) override
  673. {
  674. m_indexBuffers[_handle.idx].create(_size, NULL, _flags);
  675. }
  676. void updateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _offset, uint32_t _size, const Memory* _mem) override
  677. {
  678. m_indexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  679. }
  680. void destroyDynamicIndexBuffer(IndexBufferHandle _handle) override
  681. {
  682. m_indexBuffers[_handle.idx].destroy();
  683. }
  684. void createDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _size, uint16_t _flags) override
  685. {
  686. VertexLayoutHandle decl = BGFX_INVALID_HANDLE;
  687. m_vertexBuffers[_handle.idx].create(_size, NULL, decl, _flags);
  688. }
  689. void updateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _offset, uint32_t _size, const Memory* _mem) override
  690. {
  691. m_vertexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  692. }
  693. void destroyDynamicVertexBuffer(VertexBufferHandle _handle) override
  694. {
  695. m_vertexBuffers[_handle.idx].destroy();
  696. }
  697. void createShader(ShaderHandle _handle, const Memory* _mem) override
  698. {
  699. m_shaders[_handle.idx].create(_handle, _mem);
  700. }
  701. void destroyShader(ShaderHandle _handle) override
  702. {
  703. m_shaders[_handle.idx].destroy();
  704. }
  705. void createProgram(ProgramHandle _handle, ShaderHandle _vsh, ShaderHandle _fsh) override
  706. {
  707. m_program[_handle.idx].create(&m_shaders[_vsh.idx], isValid(_fsh) ? &m_shaders[_fsh.idx] : NULL);
  708. }
  709. void destroyProgram(ProgramHandle _handle) override
  710. {
  711. m_program[_handle.idx].destroy();
  712. }
  713. void* createTexture(TextureHandle _handle, const Memory* _mem, uint64_t _flags, uint8_t _skip) override
  714. {
  715. m_textures[_handle.idx].create(_handle, _mem, _flags, _skip);
  716. return NULL;
  717. }
  718. void updateTextureBegin(TextureHandle /*_handle*/, uint8_t /*_side*/, uint8_t /*_mip*/) override
  719. {
  720. }
  721. void updateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem) override
  722. {
  723. m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _pitch, _mem);
  724. }
  725. void updateTextureEnd() override
  726. {
  727. }
  728. void readback(ReadbackWgpu& readback, const TextureWgpu& texture, void* _data)
  729. {
  730. m_cmd.kick(false, true);
  731. m_cmd.beginRender();
  732. if (readback.m_mapped)
  733. return;
  734. BX_ASSERT(readback.m_mip<texture.m_numMips,"Invalid mip: %d num mips:", readback.m_mip,texture.m_numMips);
  735. uint32_t srcWidth = bx::uint32_max(1, texture.m_width >> readback.m_mip);
  736. uint32_t srcHeight = bx::uint32_max(1, texture.m_height >> readback.m_mip);
  737. const uint32_t bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(texture.m_textureFormat));
  738. const uint32_t pitch = srcWidth * bpp / 8;
  739. const uint32_t dstpitch = bx::strideAlign(pitch, kMinBufferOffsetAlignment);
  740. const uint32_t size = dstpitch * srcHeight;
  741. // TODO move inside ReadbackWgpu::create
  742. if (!readback.m_buffer)
  743. {
  744. wgpu::BufferDescriptor desc;
  745. desc.size = size;
  746. desc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead;
  747. readback.m_buffer = m_device.CreateBuffer(&desc);
  748. }
  749. wgpu::ImageCopyTexture imageCopyTexture;
  750. imageCopyTexture.texture = texture.m_ptr;
  751. imageCopyTexture.origin = { 0, 0, 0 };
  752. wgpu::ImageCopyBuffer imageCopyBuffer;
  753. imageCopyBuffer.buffer = readback.m_buffer;
  754. imageCopyBuffer.layout.bytesPerRow = dstpitch;
  755. imageCopyBuffer.layout.rowsPerImage = srcHeight;
  756. wgpu::Extent3D extent3D = { srcWidth, srcHeight, 1 };
  757. getBlitCommandEncoder().CopyTextureToBuffer(&imageCopyTexture, &imageCopyBuffer, &extent3D);
  758. auto finish = [](WGPUBufferMapAsyncStatus status, void* userdata)
  759. {
  760. ReadbackWgpu* readback = static_cast<ReadbackWgpu*>(userdata);
  761. void const* data = readback->m_buffer.GetConstMappedRange();
  762. if(status == WGPUBufferMapAsyncStatus_Success)
  763. readback->readback(data);
  764. };
  765. m_cmd.finish();
  766. m_cmd.kick(true);
  767. readback.m_mapped = true;
  768. readback.m_data = _data;
  769. readback.m_size = pitch * srcHeight;
  770. readback.m_buffer.MapAsync(wgpu::MapMode::Read, 0, size, finish, &readback);
  771. }
  772. void readTexture(TextureHandle _handle, void* _data, uint8_t _mip) override
  773. {
  774. TextureWgpu& texture = m_textures[_handle.idx];
  775. readback(texture.m_readback, texture, _data);
  776. }
  777. void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height, uint8_t _numMips, uint16_t _numLayers) override
  778. {
  779. TextureWgpu& texture = m_textures[_handle.idx];
  780. uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate);
  781. const Memory* mem = alloc(size);
  782. bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
  783. uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
  784. bx::write(&writer, magic);
  785. TextureCreate tc;
  786. tc.m_width = _width;
  787. tc.m_height = _height;
  788. tc.m_depth = 0;
  789. tc.m_numLayers = _numLayers;
  790. tc.m_numMips = _numMips;
  791. tc.m_format = TextureFormat::Enum(texture.m_requestedFormat);
  792. tc.m_cubeMap = false;
  793. tc.m_mem = NULL;
  794. bx::write(&writer, tc);
  795. texture.destroy();
  796. texture.create(_handle, mem, texture.m_flags, 0);
  797. release(mem);
  798. }
  799. void overrideInternal(TextureHandle _handle, uintptr_t _ptr) override
  800. {
  801. BX_UNUSED(_handle, _ptr);
  802. }
  803. uintptr_t getInternal(TextureHandle _handle) override
  804. {
  805. BX_UNUSED(_handle);
  806. return 0;
  807. }
  808. void destroyTexture(TextureHandle _handle) override
  809. {
  810. m_textures[_handle.idx].destroy();
  811. }
  812. void createFrameBuffer(FrameBufferHandle _handle, uint8_t _num, const Attachment* _attachment) override
  813. {
  814. m_frameBuffers[_handle.idx].create(_num, _attachment);
  815. }
  816. void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, TextureFormat::Enum _depthFormat) override
  817. {
  818. for (uint32_t ii = 0, num = m_numWindows; ii < num; ++ii)
  819. {
  820. FrameBufferHandle handle = m_windows[ii];
  821. if (isValid(handle)
  822. && m_frameBuffers[handle.idx].m_nwh == _nwh)
  823. {
  824. destroyFrameBuffer(handle);
  825. }
  826. }
  827. uint16_t denseIdx = m_numWindows++;
  828. m_windows[denseIdx] = _handle;
  829. FrameBufferWgpu& fb = m_frameBuffers[_handle.idx];
  830. fb.create(denseIdx, _nwh, _width, _height, _format, _depthFormat);
  831. fb.m_swapChain->resize(m_frameBuffers[_handle.idx], _width, _height, 0);
  832. }
  833. void destroyFrameBuffer(FrameBufferHandle _handle) override
  834. {
  835. uint16_t denseIdx = m_frameBuffers[_handle.idx].destroy();
  836. if (UINT16_MAX != denseIdx)
  837. {
  838. --m_numWindows;
  839. if (m_numWindows > 1)
  840. {
  841. FrameBufferHandle handle = m_windows[m_numWindows];
  842. m_windows[m_numWindows] = {kInvalidHandle};
  843. if (m_numWindows != denseIdx)
  844. {
  845. m_windows[denseIdx] = handle;
  846. m_frameBuffers[handle.idx].m_denseIdx = denseIdx;
  847. }
  848. }
  849. }
  850. }
  851. void createUniform(UniformHandle _handle, UniformType::Enum _type, uint16_t _num, const char* _name) override
  852. {
  853. if (NULL != m_uniforms[_handle.idx])
  854. {
  855. BX_FREE(g_allocator, m_uniforms[_handle.idx]);
  856. }
  857. uint32_t size = bx::alignUp(g_uniformTypeSize[_type]*_num, 16);
  858. void* data = BX_ALLOC(g_allocator, size);
  859. bx::memSet(data, 0, size);
  860. m_uniforms[_handle.idx] = data;
  861. m_uniformReg.add(_handle, _name);
  862. }
  863. void destroyUniform(UniformHandle _handle) override
  864. {
  865. BX_FREE(g_allocator, m_uniforms[_handle.idx]);
  866. m_uniforms[_handle.idx] = NULL;
  867. m_uniformReg.remove(_handle);
  868. }
  869. void requestScreenShot(FrameBufferHandle _handle, const char* _filePath) override
  870. {
  871. BX_UNUSED(_handle); BX_UNUSED(_filePath);
  872. }
  873. void updateViewName(ViewId _id, const char* _name) override
  874. {
  875. bx::strCopy(
  876. &s_viewName[_id][BGFX_CONFIG_MAX_VIEW_NAME_RESERVED]
  877. , BX_COUNTOF(s_viewName[0])-BGFX_CONFIG_MAX_VIEW_NAME_RESERVED
  878. , _name
  879. );
  880. }
  881. void updateUniform(uint16_t _loc, const void* _data, uint32_t _size) override
  882. {
  883. bx::memCopy(m_uniforms[_loc], _data, _size);
  884. }
  885. void invalidateOcclusionQuery(OcclusionQueryHandle _handle) override
  886. {
  887. BX_UNUSED(_handle);
  888. }
  889. void setMarker(const char* _marker, uint16_t _len) override
  890. {
  891. BX_UNUSED(_len);
  892. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION) )
  893. {
  894. m_renderEncoder.InsertDebugMarker(_marker);
  895. }
  896. }
  897. virtual void setName(Handle _handle, const char* _name, uint16_t _len) override
  898. {
  899. BX_UNUSED(_handle); BX_UNUSED(_name); BX_UNUSED(_len);
  900. BX_UNUSED(_len);
  901. switch (_handle.type)
  902. {
  903. case Handle::IndexBuffer:
  904. m_indexBuffers[_handle.idx].m_label.clear();
  905. m_indexBuffers[_handle.idx].m_label.append(_name);
  906. break;
  907. case Handle::Shader:
  908. m_shaders[_handle.idx].m_label.clear();
  909. m_shaders[_handle.idx].m_label.append(_name);
  910. break;
  911. case Handle::Texture:
  912. m_textures[_handle.idx].m_label.clear();
  913. m_textures[_handle.idx].m_label.append(_name);
  914. break;
  915. case Handle::VertexBuffer:
  916. m_vertexBuffers[_handle.idx].m_label.clear();
  917. m_vertexBuffers[_handle.idx].m_label.append(_name);
  918. break;
  919. default:
  920. BX_ASSERT(false, "Invalid handle type?! %d", _handle.type);
  921. break;
  922. }
  923. }
  924. void submitBlit(BlitState& _bs, uint16_t _view);
  925. void submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter) override;
  926. void blitSetup(TextVideoMemBlitter& _blitter) override
  927. {
  928. BX_UNUSED(_blitter);
  929. }
  930. void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) override
  931. {
  932. const uint32_t numVertices = _numIndices*4/6;
  933. if (0 < numVertices)
  934. {
  935. m_indexBuffers [_blitter.m_ib->handle.idx].update(
  936. 0
  937. , bx::strideAlign(_numIndices*2, 4)
  938. , _blitter.m_ib->data
  939. , true
  940. );
  941. m_vertexBuffers[_blitter.m_vb->handle.idx].update(
  942. 0
  943. , numVertices*_blitter.m_layout.m_stride
  944. , _blitter.m_vb->data
  945. , true
  946. );
  947. endEncoding();
  948. uint32_t width = m_resolution.width;
  949. uint32_t height = m_resolution.height;
  950. FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
  951. uint64_t state = 0
  952. | BGFX_STATE_WRITE_RGB
  953. | BGFX_STATE_WRITE_A
  954. | BGFX_STATE_DEPTH_TEST_ALWAYS
  955. ;
  956. PipelineStateWgpu* pso = getPipelineState(
  957. state
  958. , 0
  959. , 0
  960. , fbh
  961. , _blitter.m_vb->layoutHandle
  962. , false
  963. , _blitter.m_program
  964. , 0
  965. );
  966. RenderPassDescriptor renderPassDescriptor;
  967. wgpu::RenderPassColorAttachment& color = renderPassDescriptor.colorAttachments[0];
  968. setFrameBuffer(renderPassDescriptor, fbh);
  969. color.loadOp = wgpu::LoadOp::Load;
  970. color.storeOp = wgpu::StoreOp::Store;
  971. // NULL != renderPassDescriptor.colorAttachments[0].resolveTexture
  972. // ? wgpu::StoreOp::MultisampleResolve
  973. // : wgpu::StoreOp::Store
  974. //;
  975. wgpu::RenderPassEncoder rce = m_cmd.m_renderEncoder.BeginRenderPass(&renderPassDescriptor.desc);
  976. m_renderEncoder = rce;
  977. rce.SetViewport(0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f);
  978. rce.SetScissorRect(0.0f, 0.0f, (float)width, (float)height);
  979. rce.SetPipeline(pso->m_rps);
  980. ProgramWgpu& program = m_program[_blitter.m_program.idx];
  981. ScratchBufferWgpu& scratchBuffer = m_scratchBuffers[0];
  982. BindStateCacheWgpu& bindStates = m_bindStateCache[0];
  983. float proj[16];
  984. bx::mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f, 0.0f, false);
  985. PredefinedUniform& predefined = program.m_predefined[0];
  986. uint8_t flags = predefined.m_type;
  987. setShaderUniform(flags, predefined.m_loc, proj, 4);
  988. BX_ASSERT(program.m_vsh->m_size > 0, "Not supposed to happen");
  989. const uint32_t voffset = scratchBuffer.write(m_vsScratch, program.m_vsh->m_gpuSize);
  990. const uint32_t fsize = (NULL != program.m_fsh ? program.m_fsh->m_gpuSize : 0);
  991. BX_ASSERT(fsize == 0, "Not supposed to happen");
  992. TextureWgpu& texture = m_textures[_blitter.m_texture.idx];
  993. BindingsWgpu b;
  994. BindStateWgpu& bindState = allocBindState(program, bindStates, b, scratchBuffer);
  995. wgpu::BindGroupEntry& textureEntry = b.m_entries[b.numEntries++];
  996. textureEntry.binding = program.m_textures[0].binding;
  997. textureEntry.textureView = texture.m_ptr.CreateView();
  998. wgpu::BindGroupEntry& samplerEntry = b.m_entries[b.numEntries++];
  999. samplerEntry.binding = program.m_samplers[0].binding;
  1000. samplerEntry.sampler = 0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & state)
  1001. ? getSamplerState(state)
  1002. : texture.m_sampler;
  1003. bindGroups(program, bindState, b);
  1004. uint32_t numOffset = 1;
  1005. uint32_t offsets[1] = { voffset };
  1006. bindProgram(rce, program, bindState, numOffset, offsets);
  1007. VertexBufferWgpu& vb = m_vertexBuffers[_blitter.m_vb->handle.idx];
  1008. rce.SetVertexBuffer(0, vb.m_ptr);
  1009. IndexBufferWgpu& ib = m_indexBuffers[_blitter.m_ib->handle.idx];
  1010. rce.SetIndexBuffer(ib.m_ptr, ib.m_format);
  1011. rce.DrawIndexed(_numIndices, 1, 0, 0, 0);
  1012. }
  1013. }
  1014. bool isDeviceRemoved() override
  1015. {
  1016. return false;
  1017. }
  1018. void flip() override
  1019. {
  1020. for (uint32_t ii = 0, num = m_numWindows; ii < num; ++ii)
  1021. {
  1022. FrameBufferWgpu& frameBuffer = ii == 0 ? m_mainFrameBuffer : m_frameBuffers[m_windows[ii].idx];
  1023. if (NULL != frameBuffer.m_swapChain)
  1024. //&& frameBuffer.m_swapChain->m_drawable)
  1025. {
  1026. SwapChainWgpu& swapChain = *frameBuffer.m_swapChain;
  1027. swapChain.flip();
  1028. }
  1029. }
  1030. m_cmd.m_stagingEncoder = NULL;
  1031. m_cmd.m_renderEncoder = NULL;
  1032. }
  1033. void updateResolution(const Resolution& _resolution)
  1034. {
  1035. m_resolution = _resolution;
  1036. return; // TODO (hugoam)
  1037. m_mainFrameBuffer.m_swapChain->m_maxAnisotropy = !!(_resolution.reset & BGFX_RESET_MAXANISOTROPY)
  1038. ? 16
  1039. : 1
  1040. ;
  1041. const uint32_t maskFlags = ~(0
  1042. | BGFX_RESET_MAXANISOTROPY
  1043. | BGFX_RESET_DEPTH_CLAMP
  1044. | BGFX_RESET_SUSPEND
  1045. );
  1046. if (m_resolution.width != _resolution.width
  1047. || m_resolution.height != _resolution.height
  1048. || (m_resolution.reset&maskFlags) != (_resolution.reset&maskFlags) )
  1049. {
  1050. wgpu::TextureFormat prevMetalLayerPixelFormat; // = m_mainFrameBuffer.m_swapChain->m_metalLayer.pixelFormat;
  1051. BX_UNUSED(prevMetalLayerPixelFormat);
  1052. m_resolution = _resolution;
  1053. m_resolution.reset &= ~BGFX_RESET_INTERNAL_FORCE;
  1054. m_mainFrameBuffer.m_swapChain->resize(m_mainFrameBuffer, _resolution.width, _resolution.height, _resolution.reset);
  1055. for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii)
  1056. {
  1057. m_frameBuffers[ii].postReset();
  1058. }
  1059. updateCapture();
  1060. m_textVideoMem.resize(false, _resolution.width, _resolution.height);
  1061. m_textVideoMem.clear();
  1062. //if (prevMetalLayerPixelFormat != m_mainFrameBuffer.m_swapChain->m_metalLayer.pixelFormat)
  1063. {
  1064. //MTL_RELEASE(m_screenshotBlitRenderPipelineState)
  1065. //reset(m_renderPipelineDescriptor);
  1066. //m_renderPipelineDescriptor.colorAttachments[0].pixelFormat = m_mainFrameBuffer.m_swapChain->m_metalLayer.pixelFormat;
  1067. //m_renderPipelineDescriptor.vertexFunction = m_screenshotBlitProgram.m_vsh->m_function;
  1068. //m_renderPipelineDescriptor.fragmentFunction = m_screenshotBlitProgram.m_fsh->m_function;
  1069. //m_screenshotBlitRenderPipelineState = m_device.newRenderPipelineStateWithDescriptor(m_renderPipelineDescriptor);
  1070. }
  1071. }
  1072. }
  1073. void invalidateCompute()
  1074. {
  1075. if (m_computeEncoder)
  1076. {
  1077. m_computeEncoder.EndPass();
  1078. m_computeEncoder = NULL;
  1079. }
  1080. }
  1081. void updateCapture()
  1082. {
  1083. }
  1084. void capture()
  1085. {
  1086. }
  1087. void captureFinish()
  1088. {
  1089. }
  1090. BindStateWgpu& allocBindState(const ProgramWgpu& program, BindStateCacheWgpu& bindStates, BindingsWgpu& bindings, ScratchBufferWgpu& scratchBuffer)
  1091. {
  1092. BindStateWgpu& bindState = bindStates.m_bindStates[bindStates.m_currentBindState];
  1093. bindStates.m_currentBindState++;
  1094. bindState.numOffset = program.m_numUniforms;
  1095. // first two bindings are always uniform buffer (vertex/fragment)
  1096. if (0 < program.m_vsh->m_gpuSize)
  1097. {
  1098. bindings.m_entries[0].binding = kSpirvVertexBinding;
  1099. bindings.m_entries[0].offset = 0;
  1100. bindings.m_entries[0].size = program.m_vsh->m_gpuSize;
  1101. bindings.m_entries[0].buffer = scratchBuffer.m_buffer;
  1102. bindings.numEntries++;
  1103. }
  1104. if (NULL != program.m_fsh
  1105. && 0 < program.m_fsh->m_gpuSize)
  1106. {
  1107. bindings.m_entries[1].binding = kSpirvFragmentBinding;
  1108. bindings.m_entries[1].offset = 0;
  1109. bindings.m_entries[1].size = program.m_fsh->m_gpuSize;
  1110. bindings.m_entries[1].buffer = scratchBuffer.m_buffer;
  1111. bindings.numEntries++;
  1112. }
  1113. return bindState;
  1114. }
  1115. void bindGroups(const ProgramWgpu& program, BindStateWgpu& bindState, BindingsWgpu& bindings)
  1116. {
  1117. wgpu::BindGroupDescriptor bindGroupDesc;
  1118. bindGroupDesc.layout = program.m_bindGroupLayout;
  1119. bindGroupDesc.entryCount = bindings.numEntries;
  1120. bindGroupDesc.entries = bindings.m_entries;
  1121. bindState.m_bindGroup = m_device.CreateBindGroup(&bindGroupDesc);
  1122. }
  1123. template <class Encoder>
  1124. void bindProgram(Encoder& encoder, const ProgramWgpu& program, BindStateWgpu& bindState, uint32_t numOffset, uint32_t* offsets)
  1125. {
  1126. BX_ASSERT(bindState.numOffset == numOffset, "We're obviously doing something wrong");
  1127. encoder.SetBindGroup(0, bindState.m_bindGroup, numOffset, offsets);
  1128. }
  1129. BindStateWgpu& allocAndFillBindState(const ProgramWgpu& program, BindStateCacheWgpu& bindStates, ScratchBufferWgpu& scratchBuffer, const RenderBind& renderBind)
  1130. {
  1131. BindingsWgpu b;
  1132. BindStateWgpu& bindState = allocBindState(program, bindStates, b, scratchBuffer);
  1133. for (uint8_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage)
  1134. {
  1135. const Binding& bind = renderBind.m_bind[stage];
  1136. const BindInfo& bindInfo = program.m_bindInfo[stage];
  1137. bool isUsed = isValid(program.m_bindInfo[stage].m_uniform);
  1138. BX_ASSERT(!isUsed || kInvalidHandle != bind.m_idx, "All expected bindings must be bound with WebGPU");
  1139. if (kInvalidHandle != bind.m_idx)
  1140. {
  1141. switch (bind.m_type)
  1142. {
  1143. case Binding::Image:
  1144. {
  1145. TextureWgpu& texture = m_textures[bind.m_idx];
  1146. wgpu::BindGroupEntry& entry = b.m_entries[b.numEntries++];
  1147. entry.binding = bindInfo.m_binding;
  1148. entry.textureView = texture.getTextureMipLevel(bind.m_mip);
  1149. if (Access::Read == bind.m_access)
  1150. {
  1151. wgpu::BindGroupEntry& samplerEntry = b.m_entries[b.numEntries++];
  1152. samplerEntry.binding = bindInfo.m_binding + 16;
  1153. samplerEntry.sampler = texture.m_sampler;
  1154. }
  1155. }
  1156. break;
  1157. case Binding::Texture:
  1158. {
  1159. // apparently bgfx allows to set a texture to a stage that a program does not even use
  1160. if (isUsed)
  1161. {
  1162. TextureWgpu& texture = m_textures[bind.m_idx];
  1163. uint32_t flags = bind.m_samplerFlags;
  1164. wgpu::TextureViewDescriptor viewDesc = defaultDescriptor<wgpu::TextureViewDescriptor>();
  1165. viewDesc.dimension = program.m_textures[bindInfo.m_index].texture.viewDimension;
  1166. wgpu::BindGroupEntry& textureEntry = b.m_entries[b.numEntries++];
  1167. textureEntry.binding = bindInfo.m_binding;
  1168. //textureEntry.textureView = texture.m_ptr.CreateView();
  1169. textureEntry.textureView = texture.m_ptr.CreateView(&viewDesc);
  1170. wgpu::BindGroupEntry& samplerEntry = b.m_entries[b.numEntries++];
  1171. samplerEntry.binding = bindInfo.m_binding + kSpirvSamplerShift;
  1172. samplerEntry.sampler = 0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & flags)
  1173. ? getSamplerState(flags)
  1174. : texture.m_sampler;
  1175. }
  1176. }
  1177. break;
  1178. case Binding::IndexBuffer:
  1179. case Binding::VertexBuffer:
  1180. {
  1181. const BufferWgpu& buffer = Binding::IndexBuffer == bind.m_type
  1182. ? (const BufferWgpu&) m_indexBuffers[bind.m_idx]
  1183. : (const BufferWgpu&) m_vertexBuffers[bind.m_idx]
  1184. ;
  1185. wgpu::BindGroupEntry& entry = b.m_entries[b.numEntries++];
  1186. entry.binding = bindInfo.m_binding;
  1187. entry.offset = 0;
  1188. entry.size = buffer.m_size;
  1189. entry.buffer = buffer.m_ptr;
  1190. }
  1191. break;
  1192. }
  1193. }
  1194. }
  1195. bindGroups(program, bindState, b);
  1196. return bindState;
  1197. };
  1198. void setShaderUniform(uint8_t _flags, uint32_t _regIndex, const void* _val, uint32_t _numRegs)
  1199. {
  1200. if(_flags&kUniformFragmentBit)
  1201. {
  1202. bx::memCopy(&m_fsScratch[_regIndex], _val, _numRegs * 16);
  1203. }
  1204. else
  1205. {
  1206. bx::memCopy(&m_vsScratch[_regIndex], _val, _numRegs * 16);
  1207. }
  1208. }
  1209. void setShaderUniform4f(uint8_t _flags, uint32_t _loc, const void* _val, uint32_t _numRegs)
  1210. {
  1211. setShaderUniform(_flags, _loc, _val, _numRegs);
  1212. }
  1213. void setShaderUniform4x4f(uint8_t _flags, uint32_t _loc, const void* _val, uint32_t _numRegs)
  1214. {
  1215. setShaderUniform(_flags, _loc, _val, _numRegs);
  1216. }
  1217. void commit(UniformBuffer& _uniformBuffer)
  1218. {
  1219. _uniformBuffer.reset();
  1220. for (;;)
  1221. {
  1222. uint32_t opcode = _uniformBuffer.read();
  1223. if (UniformType::End == opcode)
  1224. {
  1225. break;
  1226. }
  1227. UniformType::Enum type;
  1228. uint16_t loc;
  1229. uint16_t num;
  1230. uint16_t copy;
  1231. UniformBuffer::decodeOpcode(opcode, type, loc, num, copy);
  1232. const char* data;
  1233. if (copy)
  1234. {
  1235. data = _uniformBuffer.read(g_uniformTypeSize[type]*num);
  1236. }
  1237. else
  1238. {
  1239. UniformHandle handle;
  1240. bx::memCopy(&handle, _uniformBuffer.read(sizeof(UniformHandle) ), sizeof(UniformHandle) );
  1241. data = (const char*)m_uniforms[handle.idx];
  1242. }
  1243. switch ( (uint32_t)type)
  1244. {
  1245. case UniformType::Mat3:
  1246. case UniformType::Mat3|kUniformFragmentBit:
  1247. {
  1248. float* value = (float*)data;
  1249. for (uint32_t ii = 0, count = num/3; ii < count; ++ii, loc += 3*16, value += 9)
  1250. {
  1251. Matrix4 mtx;
  1252. mtx.un.val[ 0] = value[0];
  1253. mtx.un.val[ 1] = value[1];
  1254. mtx.un.val[ 2] = value[2];
  1255. mtx.un.val[ 3] = 0.0f;
  1256. mtx.un.val[ 4] = value[3];
  1257. mtx.un.val[ 5] = value[4];
  1258. mtx.un.val[ 6] = value[5];
  1259. mtx.un.val[ 7] = 0.0f;
  1260. mtx.un.val[ 8] = value[6];
  1261. mtx.un.val[ 9] = value[7];
  1262. mtx.un.val[10] = value[8];
  1263. mtx.un.val[11] = 0.0f;
  1264. setShaderUniform(uint8_t(type), loc, &mtx.un.val[0], 3);
  1265. }
  1266. }
  1267. break;
  1268. case UniformType::Sampler:
  1269. case UniformType::Sampler | kUniformFragmentBit:
  1270. case UniformType::Vec4:
  1271. case UniformType::Vec4 | kUniformFragmentBit:
  1272. case UniformType::Mat4:
  1273. case UniformType::Mat4 | kUniformFragmentBit:
  1274. {
  1275. setShaderUniform(uint8_t(type), loc, data, num);
  1276. }
  1277. break;
  1278. case UniformType::End:
  1279. break;
  1280. default:
  1281. BX_TRACE("%4d: INVALID 0x%08x, t %d, l %d, n %d, c %d", _uniformBuffer.getPos(), opcode, type, loc, num, copy);
  1282. break;
  1283. }
  1284. }
  1285. }
  1286. void clearQuad(ClearQuad& _clearQuad, const Rect& _rect, const Clear& _clear, const float _palette[][4])
  1287. {
  1288. uint32_t width;
  1289. uint32_t height;
  1290. if (isValid(m_fbh) )
  1291. {
  1292. const FrameBufferWgpu& fb = m_frameBuffers[m_fbh.idx];
  1293. width = fb.m_width;
  1294. height = fb.m_height;
  1295. }
  1296. else
  1297. {
  1298. width = m_resolution.width;
  1299. height = m_resolution.height;
  1300. }
  1301. uint64_t state = 0;
  1302. state |= _clear.m_flags & BGFX_CLEAR_COLOR ? BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A : 0;
  1303. state |= _clear.m_flags & BGFX_CLEAR_DEPTH ? BGFX_STATE_DEPTH_TEST_ALWAYS|BGFX_STATE_WRITE_Z : 0;
  1304. state |= BGFX_STATE_PT_TRISTRIP;
  1305. uint64_t stencil = 0;
  1306. stencil |= _clear.m_flags & BGFX_CLEAR_STENCIL ? 0
  1307. | BGFX_STENCIL_TEST_ALWAYS
  1308. | BGFX_STENCIL_FUNC_REF(_clear.m_stencil)
  1309. | BGFX_STENCIL_FUNC_RMASK(0xff)
  1310. | BGFX_STENCIL_OP_FAIL_S_REPLACE
  1311. | BGFX_STENCIL_OP_FAIL_Z_REPLACE
  1312. | BGFX_STENCIL_OP_PASS_Z_REPLACE
  1313. : 0
  1314. ;
  1315. uint32_t numMrt = 1;
  1316. FrameBufferHandle fbh = m_fbh;
  1317. if (isValid(fbh) && m_frameBuffers[fbh.idx].m_swapChain == NULL)
  1318. {
  1319. const FrameBufferWgpu& fb = m_frameBuffers[fbh.idx];
  1320. numMrt = bx::uint32_max(1, fb.m_num);
  1321. }
  1322. wgpu::RenderPassEncoder rce = m_renderEncoder;
  1323. ProgramHandle programHandle = _clearQuad.m_program[numMrt-1];
  1324. const VertexLayout* decl = &_clearQuad.m_layout;
  1325. const PipelineStateWgpu* pso = getPipelineState(
  1326. state
  1327. , stencil
  1328. , 0
  1329. , fbh
  1330. , 1
  1331. , &decl
  1332. , false
  1333. , programHandle
  1334. , 0
  1335. );
  1336. rce.SetPipeline(pso->m_rps);
  1337. float mrtClearColor[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS][4];
  1338. float mrtClearDepth[4] = { _clear.m_depth };
  1339. if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags)
  1340. {
  1341. for (uint32_t ii = 0; ii < numMrt; ++ii)
  1342. {
  1343. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[ii]);
  1344. bx::memCopy(mrtClearColor[ii], _palette[index], 16);
  1345. }
  1346. }
  1347. else
  1348. {
  1349. float rgba[4] =
  1350. {
  1351. _clear.m_index[0]*1.0f/255.0f,
  1352. _clear.m_index[1]*1.0f/255.0f,
  1353. _clear.m_index[2]*1.0f/255.0f,
  1354. _clear.m_index[3]*1.0f/255.0f,
  1355. };
  1356. for (uint32_t ii = 0; ii < numMrt; ++ii)
  1357. {
  1358. bx::memCopy( mrtClearColor[ii]
  1359. , rgba
  1360. , 16
  1361. );
  1362. }
  1363. }
  1364. ProgramWgpu& program = m_program[programHandle.idx];
  1365. ScratchBufferWgpu& scratchBuffer = m_scratchBuffers[0];
  1366. BindStateCacheWgpu& bindStates = m_bindStateCache[0];
  1367. BindingsWgpu b;
  1368. BindStateWgpu& bindState = allocBindState(program, bindStates, b, scratchBuffer);
  1369. const uint32_t voffset = scratchBuffer.write(mrtClearDepth, sizeof(mrtClearDepth), program.m_vsh->m_gpuSize);
  1370. const uint32_t foffset = scratchBuffer.write(mrtClearColor, sizeof(mrtClearColor), program.m_fsh->m_gpuSize);
  1371. uint32_t numOffset = 2;
  1372. uint32_t offsets[2] = { voffset, foffset };
  1373. bindGroups(program, bindState, b);
  1374. const VertexBufferWgpu& vb = m_vertexBuffers[_clearQuad.m_vb.idx];
  1375. bindProgram(rce, program, bindState, numOffset, offsets);
  1376. rce.SetViewport(_rect.m_x, _rect.m_y, _rect.m_width, _rect.m_height, 0.0f, 1.0f);
  1377. rce.SetScissorRect(_rect.m_x, _rect.m_y, _rect.m_width, _rect.m_height);
  1378. rce.SetVertexBuffer(0, vb.m_ptr);
  1379. rce.Draw(4, 1, 0, 0);
  1380. }
  1381. wgpu::TextureViewDescriptor attachmentView(const Attachment& _at, const TextureWgpu& _texture)
  1382. {
  1383. bool _resolve = bool(_texture.m_ptrMsaa);
  1384. BX_UNUSED(_resolve);
  1385. wgpu::TextureViewDescriptor desc;
  1386. if (1 < _texture.m_numSides)
  1387. {
  1388. desc.baseArrayLayer = _at.layer;
  1389. }
  1390. desc.baseMipLevel = _at.mip;
  1391. desc.arrayLayerCount = 1;
  1392. desc.mipLevelCount = 1;
  1393. if (_texture.m_type == TextureWgpu::Texture3D)
  1394. {
  1395. desc.dimension = wgpu::TextureViewDimension::e3D;
  1396. }
  1397. return desc;
  1398. }
  1399. void setFrameBuffer(RenderPassDescriptor& _renderPassDescriptor, FrameBufferHandle _fbh, bool _msaa = true)
  1400. {
  1401. if (!isValid(_fbh)
  1402. || m_frameBuffers[_fbh.idx].m_swapChain)
  1403. {
  1404. SwapChainWgpu* swapChain = !isValid(_fbh)
  1405. ? m_mainFrameBuffer.m_swapChain
  1406. : m_frameBuffers[_fbh.idx].m_swapChain
  1407. ;
  1408. _renderPassDescriptor.colorAttachments[0] = defaultDescriptor<wgpu::RenderPassColorAttachment>();
  1409. _renderPassDescriptor.desc.colorAttachmentCount = 1;
  1410. // Force 1 array layers for attachments
  1411. wgpu::TextureViewDescriptor desc;
  1412. desc.arrayLayerCount = 1;
  1413. if (swapChain->m_backBufferColorMsaa)
  1414. {
  1415. _renderPassDescriptor.colorAttachments[0].view = swapChain->m_backBufferColorMsaa.CreateView(&desc);
  1416. _renderPassDescriptor.colorAttachments[0].resolveTarget = swapChain->current();
  1417. }
  1418. else
  1419. {
  1420. _renderPassDescriptor.colorAttachments[0].view = swapChain->current();
  1421. }
  1422. _renderPassDescriptor.depthStencilAttachment = defaultDescriptor<wgpu::RenderPassDepthStencilAttachment>();
  1423. _renderPassDescriptor.depthStencilAttachment.view = swapChain->m_backBufferDepth.CreateView();
  1424. _renderPassDescriptor.desc.depthStencilAttachment = &_renderPassDescriptor.depthStencilAttachment;
  1425. }
  1426. else
  1427. {
  1428. FrameBufferWgpu& frameBuffer = m_frameBuffers[_fbh.idx];
  1429. _renderPassDescriptor.desc.colorAttachmentCount = frameBuffer.m_num;
  1430. for (uint32_t ii = 0; ii < frameBuffer.m_num; ++ii)
  1431. {
  1432. const TextureWgpu& texture = m_textures[frameBuffer.m_colorHandle[ii].idx];
  1433. const wgpu::TextureViewDescriptor desc = attachmentView(frameBuffer.m_colorAttachment[ii], texture);
  1434. _renderPassDescriptor.colorAttachments[ii] = defaultDescriptor<wgpu::RenderPassColorAttachment>();
  1435. _renderPassDescriptor.colorAttachments[ii].view = texture.m_ptrMsaa
  1436. ? texture.m_ptrMsaa.CreateView(&desc)
  1437. : texture.m_ptr.CreateView(&desc)
  1438. ;
  1439. _renderPassDescriptor.colorAttachments[ii].resolveTarget = texture.m_ptrMsaa
  1440. ? texture.m_ptr.CreateView(&desc)
  1441. : wgpu::TextureView()
  1442. ;
  1443. }
  1444. if (isValid(frameBuffer.m_depthHandle) )
  1445. {
  1446. const TextureWgpu& texture = m_textures[frameBuffer.m_depthHandle.idx];
  1447. const wgpu::TextureViewDescriptor desc = attachmentView(frameBuffer.m_depthAttachment, texture);
  1448. _renderPassDescriptor.depthStencilAttachment = defaultDescriptor<wgpu::RenderPassDepthStencilAttachment>();
  1449. _renderPassDescriptor.depthStencilAttachment.view = texture.m_ptrMsaa
  1450. ? texture.m_ptrMsaa.CreateView(&desc)
  1451. : texture.m_ptr.CreateView(&desc)
  1452. ;
  1453. _renderPassDescriptor.desc.depthStencilAttachment = &_renderPassDescriptor.depthStencilAttachment;
  1454. }
  1455. }
  1456. m_fbh = _fbh;
  1457. m_rtMsaa = _msaa;
  1458. }
  1459. void setDepthStencilState(wgpu::DepthStencilState& desc, uint64_t _state, uint64_t _stencil = 0)
  1460. {
  1461. const uint32_t fstencil = unpackStencil(0, _stencil);
  1462. const uint32_t func = (_state&BGFX_STATE_DEPTH_TEST_MASK) >> BGFX_STATE_DEPTH_TEST_SHIFT;
  1463. desc.depthWriteEnabled = !!(BGFX_STATE_WRITE_Z & _state);
  1464. desc.depthCompare = s_cmpFunc[func];
  1465. uint32_t bstencil = unpackStencil(1, _stencil);
  1466. const uint32_t frontAndBack = bstencil != BGFX_STENCIL_NONE && bstencil != fstencil;
  1467. bstencil = frontAndBack ? bstencil : fstencil;
  1468. desc.stencilFront = defaultDescriptor<wgpu::StencilFaceState>();
  1469. desc.stencilBack = defaultDescriptor<wgpu::StencilFaceState>();
  1470. if (0 != _stencil)
  1471. {
  1472. // TODO (hugoam)
  1473. const uint32_t readMask = (fstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  1474. const uint32_t writeMask = 0xff;
  1475. desc.stencilReadMask = readMask;
  1476. desc.stencilWriteMask = writeMask;
  1477. desc.stencilFront.failOp = s_stencilOp[(fstencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT];
  1478. desc.stencilFront.depthFailOp = s_stencilOp[(fstencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT];
  1479. desc.stencilFront.passOp = s_stencilOp[(fstencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT];
  1480. desc.stencilFront.compare = s_cmpFunc[(fstencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT];
  1481. desc.stencilBack.failOp = s_stencilOp[(bstencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT];
  1482. desc.stencilBack.depthFailOp = s_stencilOp[(bstencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT];
  1483. desc.stencilBack.passOp = s_stencilOp[(bstencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT];
  1484. desc.stencilBack.compare = s_cmpFunc[(bstencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT];
  1485. }
  1486. }
  1487. RenderPassStateWgpu* getRenderPassState(bgfx::FrameBufferHandle fbh, bool clear, Clear clr)
  1488. {
  1489. bx::HashMurmur2A murmur;
  1490. murmur.begin();
  1491. murmur.add(fbh.idx);
  1492. murmur.add(clear);
  1493. murmur.add(&clr, sizeof(clr));
  1494. uint32_t hash = murmur.end();
  1495. RenderPassStateWgpu* rps = m_renderPassStateCache.find(hash);
  1496. if (NULL == rps)
  1497. {
  1498. rps = BX_NEW(g_allocator, RenderPassStateWgpu);
  1499. m_renderPassStateCache.add(hash, rps);
  1500. }
  1501. return rps;
  1502. }
  1503. PipelineStateWgpu* getPipelineState(
  1504. uint64_t _state
  1505. , uint64_t _stencil
  1506. , uint32_t _rgba
  1507. , FrameBufferHandle _fbh
  1508. , uint8_t _numStreams
  1509. , const VertexLayout** _vertexDecls
  1510. , bool _isIndex16
  1511. , ProgramHandle _program
  1512. , uint8_t _numInstanceData
  1513. )
  1514. {
  1515. _state &= 0
  1516. | BGFX_STATE_WRITE_RGB
  1517. | BGFX_STATE_WRITE_A
  1518. | BGFX_STATE_WRITE_Z
  1519. | BGFX_STATE_DEPTH_TEST_MASK
  1520. | BGFX_STATE_BLEND_MASK
  1521. | BGFX_STATE_BLEND_EQUATION_MASK
  1522. | BGFX_STATE_BLEND_INDEPENDENT
  1523. | BGFX_STATE_BLEND_ALPHA_TO_COVERAGE
  1524. | BGFX_STATE_CULL_MASK
  1525. | BGFX_STATE_MSAA
  1526. | BGFX_STATE_LINEAA
  1527. | BGFX_STATE_CONSERVATIVE_RASTER
  1528. | BGFX_STATE_PT_MASK
  1529. ;
  1530. const bool independentBlendEnable = !!(BGFX_STATE_BLEND_INDEPENDENT & _state);
  1531. const ProgramWgpu& program = m_program[_program.idx];
  1532. bx::HashMurmur2A murmur;
  1533. murmur.begin();
  1534. murmur.add(_state);
  1535. murmur.add(_stencil);
  1536. murmur.add(independentBlendEnable ? _rgba : 0);
  1537. murmur.add(_numInstanceData);
  1538. FrameBufferWgpu& frameBuffer = !isValid(_fbh) ? m_mainFrameBuffer : m_frameBuffers[_fbh.idx];
  1539. murmur.add(frameBuffer.m_pixelFormatHash);
  1540. murmur.add(program.m_vsh->m_hash);
  1541. if (NULL != program.m_fsh)
  1542. {
  1543. murmur.add(program.m_fsh->m_hash);
  1544. }
  1545. for (uint8_t ii = 0; ii < _numStreams; ++ii)
  1546. {
  1547. murmur.add(_vertexDecls[ii]->m_hash);
  1548. }
  1549. uint32_t hash = murmur.end();
  1550. PipelineStateWgpu* pso = m_pipelineStateCache.find(hash);
  1551. if (NULL == pso)
  1552. {
  1553. pso = BX_NEW(g_allocator, PipelineStateWgpu);
  1554. //pd.alphaToCoverageEnabled = !!(BGFX_STATE_BLEND_ALPHA_TO_COVERAGE & _state);
  1555. RenderPipelineDescriptor& pd = pso->m_rpd;
  1556. uint32_t frameBufferAttachment = 1;
  1557. uint32_t sampleCount = 1;
  1558. if (!isValid(_fbh)
  1559. || s_renderWgpu->m_frameBuffers[_fbh.idx].m_swapChain)
  1560. {
  1561. SwapChainWgpu& swapChain = !isValid(_fbh)
  1562. ? *s_renderWgpu->m_mainFrameBuffer.m_swapChain
  1563. : *s_renderWgpu->m_frameBuffers[_fbh.idx].m_swapChain
  1564. ;
  1565. sampleCount = swapChain.m_backBufferColorMsaa
  1566. ? swapChain.m_sampleCount
  1567. : 1
  1568. ;
  1569. pd.targets[0].format = swapChain.m_colorFormat;
  1570. pd.depthStencil.format = swapChain.m_depthFormat;
  1571. pd.desc.depthStencil = &pd.depthStencil;
  1572. }
  1573. else
  1574. {
  1575. frameBufferAttachment = frameBuffer.m_num;
  1576. for (uint32_t ii = 0; ii < frameBuffer.m_num; ++ii)
  1577. {
  1578. const TextureWgpu& texture = m_textures[frameBuffer.m_colorHandle[ii].idx];
  1579. sampleCount = texture.m_ptrMsaa
  1580. ? texture.m_sampleCount
  1581. : 1
  1582. ;
  1583. pd.targets[ii].format = s_textureFormat[texture.m_textureFormat].m_fmt;
  1584. }
  1585. pd.fragment.targetCount = frameBuffer.m_num;
  1586. if (isValid(frameBuffer.m_depthHandle) )
  1587. {
  1588. const TextureWgpu& texture = m_textures[frameBuffer.m_depthHandle.idx];
  1589. pd.depthStencil.format = s_textureFormat[texture.m_textureFormat].m_fmt;
  1590. pd.desc.depthStencil = &pd.depthStencil;
  1591. }
  1592. }
  1593. const uint32_t blend = uint32_t( (_state&BGFX_STATE_BLEND_MASK )>>BGFX_STATE_BLEND_SHIFT);
  1594. const uint32_t equation = uint32_t( (_state&BGFX_STATE_BLEND_EQUATION_MASK)>>BGFX_STATE_BLEND_EQUATION_SHIFT);
  1595. const uint32_t srcRGB = (blend )&0xf;
  1596. const uint32_t dstRGB = (blend>> 4)&0xf;
  1597. const uint32_t srcA = (blend>> 8)&0xf;
  1598. const uint32_t dstA = (blend>>12)&0xf;
  1599. const uint32_t equRGB = (equation )&0x7;
  1600. const uint32_t equA = (equation>>3)&0x7;
  1601. wgpu::ColorWriteMask writeMask = wgpu::ColorWriteMask::None;
  1602. writeMask |= (_state&BGFX_STATE_WRITE_R) ? wgpu::ColorWriteMask::Red : wgpu::ColorWriteMask::None;
  1603. writeMask |= (_state&BGFX_STATE_WRITE_G) ? wgpu::ColorWriteMask::Green : wgpu::ColorWriteMask::None;
  1604. writeMask |= (_state&BGFX_STATE_WRITE_B) ? wgpu::ColorWriteMask::Blue : wgpu::ColorWriteMask::None;
  1605. writeMask |= (_state&BGFX_STATE_WRITE_A) ? wgpu::ColorWriteMask::Alpha : wgpu::ColorWriteMask::None;
  1606. for (uint32_t ii = 0; ii < (independentBlendEnable ? 1 : frameBufferAttachment); ++ii)
  1607. {
  1608. wgpu::ColorTargetState& drt = pd.targets[ii];
  1609. wgpu::BlendState& blend = pd.blends[ii];
  1610. if(!(BGFX_STATE_BLEND_MASK & _state))
  1611. {
  1612. // useless
  1613. blend.color = defaultDescriptor<wgpu::BlendComponent>();
  1614. blend.alpha = defaultDescriptor<wgpu::BlendComponent>();
  1615. drt.blend = NULL;
  1616. }
  1617. else
  1618. {
  1619. blend.color.srcFactor = s_blendFactor[srcRGB][0];
  1620. blend.color.dstFactor = s_blendFactor[dstRGB][0];
  1621. blend.color.operation = s_blendEquation[equRGB];
  1622. blend.alpha.srcFactor = s_blendFactor[srcA][1];
  1623. blend.alpha.dstFactor = s_blendFactor[dstA][1];
  1624. blend.alpha.operation = s_blendEquation[equA];
  1625. drt.blend = &blend;
  1626. }
  1627. drt.writeMask = writeMask;
  1628. }
  1629. if (independentBlendEnable)
  1630. {
  1631. for (uint32_t ii = 1, rgba = _rgba; ii < frameBufferAttachment; ++ii, rgba >>= 11)
  1632. {
  1633. wgpu::ColorTargetState& drt = pd.targets[ii];
  1634. wgpu::BlendState& blend = pd.blends[ii];
  1635. //drt.blendingEnabled = 0 != (rgba&0x7ff);
  1636. const uint32_t src = (rgba )&0xf;
  1637. const uint32_t dst = (rgba>>4)&0xf;
  1638. const uint32_t equationIndex = (rgba>>8)&0x7;
  1639. blend.color.srcFactor = s_blendFactor[src][0];
  1640. blend.color.dstFactor = s_blendFactor[dst][0];
  1641. blend.color.operation = s_blendEquation[equationIndex];
  1642. blend.alpha.srcFactor = s_blendFactor[src][1];
  1643. blend.alpha.dstFactor = s_blendFactor[dst][1];
  1644. blend.alpha.operation = s_blendEquation[equationIndex];
  1645. drt.writeMask = writeMask;
  1646. }
  1647. }
  1648. pd.desc.vertex.module = program.m_vsh->m_module;
  1649. if (NULL != program.m_fsh)
  1650. {
  1651. pd.fragment.module = program.m_fsh->m_module;
  1652. pd.desc.fragment = &pd.fragment;
  1653. }
  1654. setDepthStencilState(pd.depthStencil, _state, _stencil);
  1655. const uint64_t cull = _state & BGFX_STATE_CULL_MASK;
  1656. const uint8_t cullIndex = uint8_t(cull >> BGFX_STATE_CULL_SHIFT);
  1657. pd.desc.primitive.cullMode = s_cullMode[cullIndex];
  1658. pd.desc.primitive.frontFace = (_state & BGFX_STATE_FRONT_CCW) ? wgpu::FrontFace::CCW : wgpu::FrontFace::CW;
  1659. // pd.desc = m_renderPipelineDescriptor;
  1660. pd.desc.multisample.count = sampleCount;
  1661. wgpu::PipelineLayoutDescriptor layout = defaultDescriptor<wgpu::PipelineLayoutDescriptor>();
  1662. layout.bindGroupLayouts = &program.m_bindGroupLayout;
  1663. layout.bindGroupLayoutCount = 1;
  1664. BX_TRACE("Creating WebGPU render pipeline layout for program %s", program.m_vsh->name());
  1665. pd.desc.layout = m_device.CreatePipelineLayout(&layout);
  1666. // TODO (hugoam) this should be cached too ?
  1667. //uint32_t ref = (_state&BGFX_STATE_ALPHA_REF_MASK) >> BGFX_STATE_ALPHA_REF_SHIFT;
  1668. //viewState.m_alphaRef = ref / 255.0f;
  1669. const uint64_t primType = _state & BGFX_STATE_PT_MASK;
  1670. uint8_t primIndex = uint8_t(primType >> BGFX_STATE_PT_SHIFT);
  1671. PrimInfo prim = s_primInfo[primIndex];
  1672. pd.desc.primitive.topology = prim.m_type;
  1673. VertexStateDescriptor vertex;
  1674. vertex.desc.module = program.m_vsh->m_module;
  1675. vertex.desc.bufferCount = 0;
  1676. wgpu::VertexBufferLayout* inputBinding = vertex.buffers;
  1677. wgpu::VertexAttribute* inputAttrib = vertex.attributes;
  1678. auto fillVertexDecl = [&](const ShaderWgpu* _vsh, const VertexLayout& _decl)
  1679. {
  1680. vertex.desc.bufferCount += 1;
  1681. inputBinding->arrayStride = _decl.m_stride;
  1682. inputBinding->stepMode = wgpu::InputStepMode::Vertex;
  1683. inputBinding->attributes = inputAttrib;
  1684. uint32_t numAttribs = 0;
  1685. for(uint32_t attr = 0; attr < Attrib::Count; ++attr)
  1686. {
  1687. if(UINT16_MAX != _decl.m_attributes[attr])
  1688. {
  1689. if(UINT8_MAX == _vsh->m_attrRemap[attr])
  1690. continue;
  1691. inputAttrib->shaderLocation = _vsh->m_attrRemap[attr];
  1692. if(0 == _decl.m_attributes[attr])
  1693. {
  1694. inputAttrib->format = wgpu::VertexFormat::Float3;
  1695. inputAttrib->offset = 0;
  1696. }
  1697. else
  1698. {
  1699. uint8_t num;
  1700. AttribType::Enum type;
  1701. bool normalized;
  1702. bool asInt;
  1703. _decl.decode(Attrib::Enum(attr), num, type, normalized, asInt);
  1704. inputAttrib->format = s_attribType[type][num-1][normalized];
  1705. inputAttrib->offset = _decl.m_offset[attr];
  1706. }
  1707. ++inputAttrib;
  1708. ++numAttribs;
  1709. }
  1710. }
  1711. inputBinding->attributeCount = numAttribs;
  1712. inputBinding++;
  1713. return numAttribs;
  1714. };
  1715. //bool attrSet[Attrib::Count] = {};
  1716. uint16_t unsettedAttr[Attrib::Count];
  1717. bx::memCopy(unsettedAttr, program.m_vsh->m_attrMask, sizeof(uint16_t) * Attrib::Count);
  1718. uint8_t stream = 0;
  1719. for (; stream < _numStreams; ++stream)
  1720. {
  1721. VertexLayout layout;
  1722. bx::memCopy(&layout, _vertexDecls[stream], sizeof(VertexLayout));
  1723. const uint16_t* attrMask = program.m_vsh->m_attrMask;
  1724. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  1725. {
  1726. Attrib::Enum iiattr = Attrib::Enum(ii);
  1727. uint16_t mask = attrMask[ii];
  1728. uint16_t attr = (layout.m_attributes[ii] & mask);
  1729. if (attr == 0)
  1730. {
  1731. layout.m_attributes[ii] = UINT16_MAX;
  1732. }
  1733. if (unsettedAttr[ii] && attr != UINT16_MAX)
  1734. {
  1735. unsettedAttr[ii] = 0;
  1736. }
  1737. }
  1738. fillVertexDecl(program.m_vsh, layout);
  1739. }
  1740. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  1741. {
  1742. Attrib::Enum iiattr = Attrib::Enum(ii);
  1743. if (0 < unsettedAttr[ii])
  1744. {
  1745. //uint32_t numAttribs = vertexs.buffers[stream].attributeCount;
  1746. //uint32_t numAttribs = inputBinding->attributeCount;
  1747. //wgpu::VertexBufferLayout* inputAttrib = const_cast<VkVertexInputAttributeDescription*>(_vertexInputState.pVertexAttributeDescriptions + numAttribs);
  1748. inputAttrib->shaderLocation = program.m_vsh->m_attrRemap[ii];
  1749. //inputAttrib->binding = 0;
  1750. inputAttrib->format = wgpu::VertexFormat::Float3; // VK_FORMAT_R32G32B32_SFLOAT;
  1751. inputAttrib->offset = 0;
  1752. vertex.buffers[stream-1].attributeCount++;
  1753. ++inputAttrib;
  1754. }
  1755. }
  1756. // TODO (hugoam) WebGPU will crash whenever we are not supplying the correct number of attributes (which depends on the stride passed to bgfx::allocInstanceDataBuffer)
  1757. // so we need to know the number of live instance attributes in the shader and if they aren't all supplied:
  1758. // - fail the pipeline state creation
  1759. // - bind dummy attributes
  1760. if (0 < _numInstanceData)
  1761. {
  1762. uint32_t numBindings = vertex.desc.bufferCount; // == stream+1 // .vertexBindingDescriptionCount;
  1763. uint32_t firstAttrib = vertex.buffers[stream-1].attributeCount;
  1764. uint32_t numAttribs = firstAttrib;
  1765. inputBinding->arrayStride = _numInstanceData * 16;
  1766. inputBinding->stepMode = wgpu::InputStepMode::Instance;
  1767. for (uint32_t inst = 0; inst < _numInstanceData; ++inst)
  1768. {
  1769. inputAttrib->shaderLocation = numAttribs;
  1770. inputAttrib->format = wgpu::VertexFormat::Float32x4;
  1771. inputAttrib->offset = inst * 16;
  1772. ++numAttribs;
  1773. ++inputAttrib;
  1774. }
  1775. vertex.desc.bufferCount = numBindings + 1;
  1776. vertex.buffers[stream].attributeCount = numAttribs - firstAttrib;
  1777. vertex.buffers[stream].attributes = &vertex.attributes[firstAttrib];
  1778. }
  1779. bool isStrip = prim.m_type == wgpu::PrimitiveTopology::LineStrip
  1780. || prim.m_type == wgpu::PrimitiveTopology::TriangleStrip;
  1781. if (isStrip)
  1782. pd.desc.primitive.stripIndexFormat = _isIndex16 ? wgpu::IndexFormat::Uint16 : wgpu::IndexFormat::Uint32;
  1783. else
  1784. pd.desc.primitive.stripIndexFormat = wgpu::IndexFormat::Undefined;
  1785. pd.desc.vertex = vertex.desc;
  1786. BX_TRACE("Creating WebGPU render pipeline state for program %s", program.m_vsh->name());
  1787. pso->m_rps = m_device.CreateRenderPipeline2(&pd.desc);
  1788. m_pipelineStateCache.add(hash, pso);
  1789. }
  1790. return pso;
  1791. }
  1792. PipelineStateWgpu* getPipelineState(
  1793. uint64_t _state
  1794. , uint64_t _stencil
  1795. , uint32_t _rgba
  1796. , FrameBufferHandle _fbh
  1797. , VertexLayoutHandle _declHandle
  1798. , bool _isIndex16
  1799. , ProgramHandle _program
  1800. , uint8_t _numInstanceData
  1801. )
  1802. {
  1803. const VertexLayout* decl = &m_vertexDecls[_declHandle.idx];
  1804. return getPipelineState(
  1805. _state
  1806. , _stencil
  1807. , _rgba
  1808. , _fbh
  1809. , 1
  1810. , &decl
  1811. , _isIndex16
  1812. , _program
  1813. , _numInstanceData
  1814. );
  1815. }
  1816. PipelineStateWgpu* getComputePipelineState(ProgramHandle _program)
  1817. {
  1818. ProgramWgpu& program = m_program[_program.idx];
  1819. if (NULL == program.m_computePS)
  1820. {
  1821. PipelineStateWgpu* pso = BX_NEW(g_allocator, PipelineStateWgpu);
  1822. program.m_computePS = pso;
  1823. wgpu::PipelineLayoutDescriptor layout = defaultDescriptor<wgpu::PipelineLayoutDescriptor>();
  1824. layout.bindGroupLayouts = &program.m_bindGroupLayout;
  1825. layout.bindGroupLayoutCount = 1;
  1826. BX_TRACE("Creating WebGPU render pipeline layout for program %s", program.m_vsh->name());
  1827. pso->m_layout = m_device.CreatePipelineLayout(&layout);
  1828. wgpu::ComputePipelineDescriptor desc;
  1829. desc.layout = pso->m_layout;
  1830. desc.computeStage = { NULL, program.m_vsh->m_module, "main" };
  1831. BX_TRACE("Creating WebGPU render pipeline state for program %s", program.m_vsh->name());
  1832. pso->m_cps = m_device.CreateComputePipeline(&desc);
  1833. }
  1834. return program.m_computePS;
  1835. }
  1836. wgpu::Sampler getSamplerState(uint32_t _flags)
  1837. {
  1838. _flags &= BGFX_SAMPLER_BITS_MASK;
  1839. SamplerStateWgpu* sampler = m_samplerStateCache.find(_flags);
  1840. if (NULL == sampler)
  1841. {
  1842. sampler = BX_NEW(g_allocator, SamplerStateWgpu);
  1843. wgpu::SamplerDescriptor desc;
  1844. desc.addressModeU = s_textureAddress[(_flags&BGFX_SAMPLER_U_MASK)>>BGFX_SAMPLER_U_SHIFT];
  1845. desc.addressModeV = s_textureAddress[(_flags&BGFX_SAMPLER_V_MASK)>>BGFX_SAMPLER_V_SHIFT];
  1846. desc.addressModeW = s_textureAddress[(_flags&BGFX_SAMPLER_W_MASK)>>BGFX_SAMPLER_W_SHIFT];
  1847. desc.minFilter = s_textureFilterMinMag[(_flags&BGFX_SAMPLER_MIN_MASK)>>BGFX_SAMPLER_MIN_SHIFT];
  1848. desc.magFilter = s_textureFilterMinMag[(_flags&BGFX_SAMPLER_MAG_MASK)>>BGFX_SAMPLER_MAG_SHIFT];
  1849. desc.mipmapFilter = s_textureFilterMip[(_flags&BGFX_SAMPLER_MIP_MASK)>>BGFX_SAMPLER_MIP_SHIFT];
  1850. desc.lodMinClamp = 0;
  1851. desc.lodMaxClamp = bx::kFloatMax;
  1852. const uint32_t cmpFunc = (_flags&BGFX_SAMPLER_COMPARE_MASK)>>BGFX_SAMPLER_COMPARE_SHIFT;
  1853. desc.compare = 0 == cmpFunc
  1854. ? wgpu::CompareFunction::Undefined
  1855. : s_cmpFunc[cmpFunc]
  1856. ;
  1857. sampler->m_sampler = s_renderWgpu->m_device.CreateSampler(&desc);
  1858. m_samplerStateCache.add(_flags, sampler);
  1859. }
  1860. return sampler->m_sampler;
  1861. }
  1862. wgpu::CommandEncoder& getRenderEncoder()
  1863. {
  1864. if (!m_cmd.m_renderEncoder)
  1865. m_cmd.beginRender();
  1866. return m_cmd.m_renderEncoder;
  1867. }
  1868. wgpu::CommandEncoder& getStagingEncoder()
  1869. {
  1870. if (!m_cmd.m_stagingEncoder)
  1871. m_cmd.beginStaging();
  1872. return m_cmd.m_stagingEncoder;
  1873. }
  1874. wgpu::CommandEncoder& getBlitCommandEncoder()
  1875. {
  1876. if (m_renderEncoder || m_computeEncoder)
  1877. endEncoding();
  1878. return getRenderEncoder();
  1879. }
  1880. wgpu::RenderPassEncoder renderPass(bgfx::Frame* _render, bgfx::FrameBufferHandle fbh, bool clear, Clear clr, const char* name = NULL)
  1881. {
  1882. RenderPassStateWgpu* rps = s_renderWgpu->getRenderPassState(fbh, clear, clr);
  1883. RenderPassDescriptor& renderPassDescriptor = rps->m_rpd;
  1884. renderPassDescriptor.desc.label = name;
  1885. setFrameBuffer(renderPassDescriptor, fbh);
  1886. if(clear)
  1887. {
  1888. for(uint32_t ii = 0; ii < g_caps.limits.maxFBAttachments; ++ii)
  1889. {
  1890. wgpu::RenderPassColorAttachment& color = renderPassDescriptor.colorAttachments[ii];
  1891. if(0 != (BGFX_CLEAR_COLOR & clr.m_flags))
  1892. {
  1893. if(0 != (BGFX_CLEAR_COLOR_USE_PALETTE & clr.m_flags))
  1894. {
  1895. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE - 1, clr.m_index[ii]);
  1896. const float* rgba = _render->m_colorPalette[index];
  1897. const float rr = rgba[0];
  1898. const float gg = rgba[1];
  1899. const float bb = rgba[2];
  1900. const float aa = rgba[3];
  1901. color.clearColor = { rr, gg, bb, aa };
  1902. }
  1903. else
  1904. {
  1905. float rr = clr.m_index[0] * 1.0f / 255.0f;
  1906. float gg = clr.m_index[1] * 1.0f / 255.0f;
  1907. float bb = clr.m_index[2] * 1.0f / 255.0f;
  1908. float aa = clr.m_index[3] * 1.0f / 255.0f;
  1909. color.clearColor = { rr, gg, bb, aa };
  1910. }
  1911. color.loadOp = wgpu::LoadOp::Clear;
  1912. }
  1913. else
  1914. {
  1915. color.loadOp = wgpu::LoadOp::Load;
  1916. }
  1917. //desc.storeOp = desc.view.sampleCount > 1 ? wgpu::StoreOp::MultisampleResolve : wgpu::StoreOp::Store;
  1918. color.storeOp = wgpu::StoreOp::Store;
  1919. }
  1920. wgpu::RenderPassDepthStencilAttachment& depthStencil = renderPassDescriptor.depthStencilAttachment;
  1921. if(depthStencil.view)
  1922. {
  1923. depthStencil.clearDepth = clr.m_depth;
  1924. depthStencil.depthLoadOp = 0 != (BGFX_CLEAR_DEPTH & clr.m_flags)
  1925. ? wgpu::LoadOp::Clear
  1926. : wgpu::LoadOp::Load
  1927. ;
  1928. depthStencil.depthStoreOp = m_mainFrameBuffer.m_swapChain->m_backBufferColorMsaa
  1929. ? wgpu::StoreOp(0) //wgpu::StoreOp::DontCare
  1930. : wgpu::StoreOp::Store
  1931. ;
  1932. depthStencil.clearStencil = clr.m_stencil;
  1933. depthStencil.stencilLoadOp = 0 != (BGFX_CLEAR_STENCIL & clr.m_flags)
  1934. ? wgpu::LoadOp::Clear
  1935. : wgpu::LoadOp::Load
  1936. ;
  1937. depthStencil.stencilStoreOp = m_mainFrameBuffer.m_swapChain->m_backBufferColorMsaa
  1938. ? wgpu::StoreOp(0) //wgpu::StoreOp::DontCare
  1939. : wgpu::StoreOp::Store
  1940. ;
  1941. }
  1942. }
  1943. else
  1944. {
  1945. for(uint32_t ii = 0; ii < g_caps.limits.maxFBAttachments; ++ii)
  1946. {
  1947. wgpu::RenderPassColorAttachment& color = renderPassDescriptor.colorAttachments[ii];
  1948. if(color.view)
  1949. {
  1950. color.loadOp = wgpu::LoadOp::Load;
  1951. }
  1952. }
  1953. wgpu::RenderPassDepthStencilAttachment& depthStencil = renderPassDescriptor.depthStencilAttachment;
  1954. if(depthStencil.view)
  1955. {
  1956. depthStencil.depthLoadOp = wgpu::LoadOp::Load;
  1957. depthStencil.depthStoreOp = wgpu::StoreOp::Store;
  1958. depthStencil.stencilLoadOp = wgpu::LoadOp::Load;
  1959. depthStencil.stencilStoreOp = wgpu::StoreOp::Store;
  1960. }
  1961. }
  1962. wgpu::RenderPassEncoder rce = m_cmd.m_renderEncoder.BeginRenderPass(&renderPassDescriptor.desc);
  1963. m_renderEncoder = rce;
  1964. return rce;
  1965. }
  1966. void endEncoding()
  1967. {
  1968. if (m_renderEncoder)
  1969. {
  1970. m_renderEncoder.EndPass();
  1971. m_renderEncoder = NULL;
  1972. }
  1973. if (m_computeEncoder)
  1974. {
  1975. m_computeEncoder.EndPass();
  1976. m_computeEncoder = NULL;
  1977. }
  1978. }
  1979. void* m_renderDocDll;
  1980. #if !BX_PLATFORM_EMSCRIPTEN
  1981. dawn_native::Instance m_instance;
  1982. #endif
  1983. wgpu::Device m_device;
  1984. wgpu::Queue m_queue;
  1985. TimerQueryWgpu m_gpuTimer;
  1986. CommandQueueWgpu m_cmd;
  1987. StagingBufferWgpu m_uniformBuffers[WEBGPU_NUM_UNIFORM_BUFFERS];
  1988. ScratchBufferWgpu m_scratchBuffers[BGFX_CONFIG_MAX_FRAME_LATENCY];
  1989. BindStateCacheWgpu m_bindStateCache[BGFX_CONFIG_MAX_FRAME_LATENCY];
  1990. uint8_t m_frameIndex;
  1991. uint16_t m_numWindows;
  1992. FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  1993. IndexBufferWgpu m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS];
  1994. VertexBufferWgpu m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
  1995. ShaderWgpu m_shaders[BGFX_CONFIG_MAX_SHADERS];
  1996. ProgramWgpu m_program[BGFX_CONFIG_MAX_PROGRAMS];
  1997. TextureWgpu m_textures[BGFX_CONFIG_MAX_TEXTURES];
  1998. ReadbackWgpu m_readbacks[BGFX_CONFIG_MAX_TEXTURES];
  1999. FrameBufferWgpu m_mainFrameBuffer;
  2000. FrameBufferWgpu m_frameBuffers[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  2001. VertexLayout m_vertexDecls[BGFX_CONFIG_MAX_VERTEX_LAYOUTS];
  2002. UniformRegistry m_uniformReg;
  2003. void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS];
  2004. //StateCacheT<BindStateWgpu*> m_bindStateCache;
  2005. StateCacheT<RenderPassStateWgpu*> m_renderPassStateCache;
  2006. StateCacheT<PipelineStateWgpu*> m_pipelineStateCache;
  2007. StateCacheT<SamplerStateWgpu*> m_samplerStateCache;
  2008. TextVideoMem m_textVideoMem;
  2009. uint8_t m_fsScratch[64 << 10];
  2010. uint8_t m_vsScratch[64 << 10];
  2011. FrameBufferHandle m_fbh;
  2012. bool m_rtMsaa;
  2013. Resolution m_resolution;
  2014. void* m_capture;
  2015. uint32_t m_captureSize;
  2016. wgpu::RenderPassEncoder m_renderEncoder;
  2017. wgpu::ComputePassEncoder m_computeEncoder;
  2018. };
  2019. RendererContextI* rendererCreate(const Init& _init)
  2020. {
  2021. s_renderWgpu = BX_NEW(g_allocator, RendererContextWgpu);
  2022. if (!s_renderWgpu->init(_init) )
  2023. {
  2024. BX_DELETE(g_allocator, s_renderWgpu);
  2025. s_renderWgpu = NULL;
  2026. }
  2027. return s_renderWgpu;
  2028. }
  2029. void rendererDestroy()
  2030. {
  2031. s_renderWgpu->shutdown();
  2032. BX_DELETE(g_allocator, s_renderWgpu);
  2033. s_renderWgpu = NULL;
  2034. }
  2035. void writeString(bx::WriterI* _writer, const char* _str)
  2036. {
  2037. bx::write(_writer, _str, (int32_t)bx::strLen(_str) );
  2038. }
  2039. void ShaderWgpu::create(ShaderHandle _handle, const Memory* _mem)
  2040. {
  2041. m_handle = _handle;
  2042. BX_TRACE("Creating shader %s", getName(_handle));
  2043. bx::MemoryReader reader(_mem->data, _mem->size);
  2044. bx::ErrorAssert err;
  2045. uint32_t magic;
  2046. bx::read(&reader, magic, &err);
  2047. wgpu::ShaderStage shaderStage;
  2048. if (isShaderType(magic, 'C'))
  2049. {
  2050. shaderStage = wgpu::ShaderStage::Compute;
  2051. }
  2052. else if (isShaderType(magic, 'F'))
  2053. {
  2054. shaderStage = wgpu::ShaderStage::Fragment;
  2055. }
  2056. else if (isShaderType(magic, 'G'))
  2057. {
  2058. //shaderStage = wgpu::ShaderStage::Geometry;
  2059. }
  2060. else if (isShaderType(magic, 'V'))
  2061. {
  2062. shaderStage = wgpu::ShaderStage::Vertex;
  2063. }
  2064. m_stage = shaderStage;
  2065. uint32_t hashIn;
  2066. bx::read(&reader, hashIn, &err);
  2067. uint32_t hashOut;
  2068. if (isShaderVerLess(magic, 6) )
  2069. {
  2070. hashOut = hashIn;
  2071. }
  2072. else
  2073. {
  2074. bx::read(&reader, hashOut, &err);
  2075. }
  2076. uint16_t count;
  2077. bx::read(&reader, count, &err);
  2078. m_numPredefined = 0;
  2079. m_numUniforms = count;
  2080. BX_TRACE("%s Shader consts %d"
  2081. , getShaderTypeName(magic)
  2082. , count
  2083. );
  2084. const bool fragment = isShaderType(magic, 'F');
  2085. uint8_t fragmentBit = fragment ? kUniformFragmentBit : 0;
  2086. BX_ASSERT(!isShaderVerLess(magic, 11), "WebGPU backend supports only shader binary version >= 11");
  2087. if (0 < count)
  2088. {
  2089. for (uint32_t ii = 0; ii < count; ++ii)
  2090. {
  2091. uint8_t nameSize = 0;
  2092. bx::read(&reader, nameSize, &err);
  2093. char name[256];
  2094. bx::read(&reader, &name, nameSize, &err);
  2095. name[nameSize] = '\0';
  2096. uint8_t type = 0;
  2097. bx::read(&reader, type, &err);
  2098. uint8_t num;
  2099. bx::read(&reader, num, &err);
  2100. uint16_t regIndex;
  2101. bx::read(&reader, regIndex, &err);
  2102. uint16_t regCount;
  2103. bx::read(&reader, regCount, &err);
  2104. uint8_t texComponent;
  2105. bx::read(&reader, texComponent, &err);
  2106. uint8_t texDimension;
  2107. bx::read(&reader, texDimension, &err);
  2108. uint16_t texFormat = 0;
  2109. bx::read(&reader, texFormat, &err);
  2110. const char* kind = "invalid";
  2111. PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
  2112. if (PredefinedUniform::Count != predefined)
  2113. {
  2114. kind = "predefined";
  2115. m_predefined[m_numPredefined].m_loc = regIndex;
  2116. m_predefined[m_numPredefined].m_count = regCount;
  2117. m_predefined[m_numPredefined].m_type = uint8_t(predefined|fragmentBit);
  2118. m_numPredefined++;
  2119. }
  2120. else if (UniformType::End == (~kUniformMask & type))
  2121. {
  2122. // regCount is used for descriptor type
  2123. const bool buffer = idToDescriptorType(regCount) == DescriptorType::StorageBuffer;
  2124. const bool readonly = (type & kUniformReadOnlyBit) != 0;
  2125. const uint8_t reverseShift = kSpirvBindShift;
  2126. const uint8_t stage = regIndex - reverseShift;
  2127. m_bindInfo[stage].m_index = m_numBuffers;
  2128. m_bindInfo[stage].m_binding = regIndex;
  2129. m_bindInfo[stage].m_uniform = { 0 };
  2130. m_buffers[m_numBuffers] = wgpu::BindGroupLayoutEntry();
  2131. m_buffers[m_numBuffers].binding = regIndex;
  2132. m_buffers[m_numBuffers].visibility = shaderStage;
  2133. if (buffer)
  2134. {
  2135. m_buffers[m_numBuffers].buffer.type = readonly
  2136. ? wgpu::BufferBindingType::ReadOnlyStorage
  2137. : wgpu::BufferBindingType::Storage;
  2138. }
  2139. else
  2140. {
  2141. m_buffers[m_numBuffers].storageTexture.access = readonly
  2142. ? wgpu::StorageTextureAccess::ReadOnly
  2143. : wgpu::StorageTextureAccess::WriteOnly;
  2144. m_buffers[m_numBuffers].storageTexture.format = s_textureFormat[texFormat].m_fmt;
  2145. }
  2146. m_numBuffers++;
  2147. kind = "storage";
  2148. }
  2149. else if (UniformType::Sampler == (~kUniformMask & type))
  2150. {
  2151. const UniformRegInfo* info = s_renderWgpu->m_uniformReg.find(name);
  2152. BX_ASSERT(NULL != info, "User defined uniform '%s' is not found, it won't be set.", name);
  2153. const uint8_t reverseShift = kSpirvBindShift;
  2154. const uint8_t stage = regIndex - reverseShift;
  2155. m_bindInfo[stage].m_index = m_numSamplers;
  2156. m_bindInfo[stage].m_binding = regIndex;
  2157. m_bindInfo[stage].m_uniform = info->m_handle;
  2158. auto textureDimensionToWgpu = [](TextureDimension::Enum dimension)
  2159. {
  2160. switch (dimension)
  2161. {
  2162. case TextureDimension::Dimension1D: return wgpu::TextureViewDimension::e1D;
  2163. case TextureDimension::Dimension2D: return wgpu::TextureViewDimension::e2D;
  2164. case TextureDimension::Dimension2DArray: return wgpu::TextureViewDimension::e2DArray;
  2165. case TextureDimension::DimensionCube: return wgpu::TextureViewDimension::Cube;
  2166. case TextureDimension::DimensionCubeArray: return wgpu::TextureViewDimension::CubeArray;
  2167. case TextureDimension::Dimension3D: return wgpu::TextureViewDimension::e3D;
  2168. default: return wgpu::TextureViewDimension::Undefined;
  2169. }
  2170. };
  2171. auto textureComponentToWgpuSampleType = [](TextureComponentType::Enum componentType)
  2172. {
  2173. switch (componentType)
  2174. {
  2175. case TextureComponentType::Float: return wgpu::TextureSampleType::Float;
  2176. case TextureComponentType::Int: return wgpu::TextureSampleType::Sint;
  2177. case TextureComponentType::Uint: return wgpu::TextureSampleType::Uint;
  2178. default: return wgpu::TextureSampleType::Float;
  2179. }
  2180. };
  2181. m_textures[m_numSamplers] = wgpu::BindGroupLayoutEntry();
  2182. m_textures[m_numSamplers].binding = regIndex;
  2183. m_textures[m_numSamplers].visibility = shaderStage;
  2184. m_textures[m_numSamplers].texture.viewDimension = textureDimensionToWgpu(idToTextureDimension(texDimension));
  2185. m_textures[m_numSamplers].texture.sampleType = textureComponentToWgpuSampleType(idToTextureComponentType(texComponent));
  2186. const bool comparisonSampler = (type & kUniformCompareBit) != 0;
  2187. m_samplers[m_numSamplers] = wgpu::BindGroupLayoutEntry();
  2188. m_samplers[m_numSamplers].binding = regIndex + kSpirvSamplerShift;
  2189. m_samplers[m_numSamplers].visibility = shaderStage;
  2190. m_samplers[m_numSamplers].sampler.type = comparisonSampler
  2191. ? wgpu::SamplerBindingType::Comparison
  2192. : wgpu::SamplerBindingType::Filtering
  2193. ;
  2194. m_numSamplers++;
  2195. kind = "sampler";
  2196. }
  2197. else
  2198. {
  2199. const UniformRegInfo* info = s_renderWgpu->m_uniformReg.find(name);
  2200. BX_ASSERT(NULL != info, "User defined uniform '%s' is not found, it won't be set.", name);
  2201. if(NULL == m_constantBuffer)
  2202. {
  2203. m_constantBuffer = UniformBuffer::create(1024);
  2204. }
  2205. kind = "user";
  2206. m_constantBuffer->writeUniformHandle((UniformType::Enum)(type | fragmentBit), regIndex, info->m_handle, regCount);
  2207. }
  2208. BX_TRACE("\t%s: %s (%s), r.index %3d, r.count %2d, r.texComponent %1d, r.texDimension %1d"
  2209. , kind
  2210. , name
  2211. , getUniformTypeName(UniformType::Enum(type&~kUniformMask) )
  2212. , regIndex
  2213. , regCount
  2214. , texComponent
  2215. , texDimension
  2216. );
  2217. BX_UNUSED(kind);
  2218. }
  2219. if (NULL != m_constantBuffer)
  2220. {
  2221. m_constantBuffer->finish();
  2222. }
  2223. }
  2224. uint32_t shaderSize;
  2225. bx::read(&reader, shaderSize, &err);
  2226. BX_TRACE("Shader body is at %lld size %u remaining %lld", reader.getPos(), shaderSize, reader.remaining());
  2227. const uint32_t* code = (const uint32_t*)reader.getDataPtr();
  2228. bx::skip(&reader, shaderSize+1);
  2229. m_code = (uint32_t*)BX_ALLOC(g_allocator, shaderSize);
  2230. m_codeSize = shaderSize;
  2231. bx::memCopy(m_code, code, shaderSize);
  2232. // TODO (hugoam) delete this
  2233. BX_TRACE("First word %08" PRIx32, code[0]);
  2234. uint8_t numAttrs = 0;
  2235. bx::read(&reader, numAttrs, &err);
  2236. m_numAttrs = numAttrs;
  2237. bx::memSet(m_attrMask, 0, sizeof(m_attrMask));
  2238. bx::memSet(m_attrRemap, UINT8_MAX, sizeof(m_attrRemap));
  2239. for(uint8_t ii = 0; ii < numAttrs; ++ii)
  2240. {
  2241. uint16_t id;
  2242. bx::read(&reader, id, &err);
  2243. auto toString = [](Attrib::Enum attr)
  2244. {
  2245. if (attr == Attrib::Position) return "Position";
  2246. else if (attr == Attrib::Normal) return "Normal";
  2247. else if (attr == Attrib::Tangent) return "Tangent";
  2248. else if (attr == Attrib::Bitangent) return "Bitangent";
  2249. else if (attr == Attrib::Color0) return "Color0";
  2250. else if (attr == Attrib::Color1) return "Color1";
  2251. else if (attr == Attrib::Color2) return "Color2";
  2252. else if (attr == Attrib::Color3) return "Color3";
  2253. else if (attr == Attrib::Indices) return "Indices";
  2254. else if (attr == Attrib::Weight) return "Weight";
  2255. else if (attr == Attrib::TexCoord0) return "TexCoord0";
  2256. else if (attr == Attrib::TexCoord1) return "TexCoord1";
  2257. else if (attr == Attrib::TexCoord2) return "TexCoord2";
  2258. else if (attr == Attrib::TexCoord3) return "TexCoord3";
  2259. else if (attr == Attrib::TexCoord4) return "TexCoord4";
  2260. else if (attr == Attrib::TexCoord5) return "TexCoord5";
  2261. else if (attr == Attrib::TexCoord6) return "TexCoord6";
  2262. else if (attr == Attrib::TexCoord7) return "TexCoord7";
  2263. return "Invalid";
  2264. };
  2265. Attrib::Enum attr = idToAttrib(id);
  2266. if(Attrib::Count != attr)
  2267. {
  2268. m_attrMask[attr] = UINT16_MAX;
  2269. m_attrRemap[attr] = ii;
  2270. BX_TRACE("\tattrib: %s (%i) at index %i", toString(attr), attr, ii);
  2271. }
  2272. }
  2273. wgpu::ShaderModuleSPIRVDescriptor spirv;
  2274. spirv.code = m_code;
  2275. spirv.codeSize = shaderSize / 4;
  2276. wgpu::ShaderModuleDescriptor desc;
  2277. desc.label = getName(_handle);
  2278. desc.nextInChain = &spirv;
  2279. m_module = s_renderWgpu->m_device.CreateShaderModule(&desc);
  2280. BGFX_FATAL(m_module
  2281. , bgfx::Fatal::InvalidShader
  2282. , "Failed to create %s shader."
  2283. , getShaderTypeName(magic)
  2284. );
  2285. bx::HashMurmur2A murmur;
  2286. murmur.begin();
  2287. murmur.add(hashIn);
  2288. murmur.add(hashOut);
  2289. murmur.add(code, shaderSize);
  2290. murmur.add(numAttrs);
  2291. murmur.add(m_attrMask, numAttrs);
  2292. m_hash = murmur.end();
  2293. auto roundUp = [](auto value, auto multiple)
  2294. {
  2295. return ((value + multiple - 1) / multiple) * multiple;
  2296. };
  2297. bx::read(&reader, m_size, &err);
  2298. const uint32_t align = kMinBufferOffsetAlignment;
  2299. m_gpuSize = uint16_t(bx::strideAlign(m_size, align) );
  2300. BX_TRACE("shader size %d (used=%d) (prev=%d)", (int)m_size, (int)m_gpuSize, (int)bx::strideAlign(roundUp(m_size, 4), align));
  2301. }
  2302. void ProgramWgpu::create(const ShaderWgpu* _vsh, const ShaderWgpu* _fsh)
  2303. {
  2304. BX_ASSERT(_vsh->m_module, "Vertex shader doesn't exist.");
  2305. m_vsh = _vsh;
  2306. m_fsh = _fsh;
  2307. m_gpuSize = _vsh->m_gpuSize + (_fsh ? _fsh->m_gpuSize : 0);
  2308. //BX_ASSERT(NULL != _vsh->m_code, "Vertex shader doesn't exist.");
  2309. m_vsh = _vsh;
  2310. bx::memCopy(&m_predefined[0], _vsh->m_predefined, _vsh->m_numPredefined * sizeof(PredefinedUniform));
  2311. m_numPredefined = _vsh->m_numPredefined;
  2312. if(NULL != _fsh)
  2313. {
  2314. //BX_ASSERT(NULL != _fsh->m_code, "Fragment shader doesn't exist.");
  2315. m_fsh = _fsh;
  2316. bx::memCopy(&m_predefined[m_numPredefined], _fsh->m_predefined, _fsh->m_numPredefined * sizeof(PredefinedUniform));
  2317. m_numPredefined += _fsh->m_numPredefined;
  2318. }
  2319. wgpu::BindGroupLayoutEntry bindings[2 + BGFX_CONFIG_MAX_TEXTURE_SAMPLERS * 3];
  2320. m_numUniforms = 0 + (_vsh->m_size > 0 ? 1 : 0) + (NULL != _fsh && _fsh->m_size > 0 ? 1 : 0);
  2321. uint8_t numBindings = 0;
  2322. if (_vsh->m_size > 0)
  2323. {
  2324. bindings[numBindings].binding = kSpirvVertexBinding;
  2325. bindings[numBindings].visibility = _vsh->m_stage;
  2326. bindings[numBindings].buffer.type = wgpu::BufferBindingType::Uniform;
  2327. bindings[numBindings].buffer.hasDynamicOffset = true;
  2328. numBindings++;
  2329. }
  2330. if (NULL != _fsh && _fsh->m_size > 0)
  2331. {
  2332. bindings[numBindings].binding = kSpirvFragmentBinding;
  2333. bindings[numBindings].visibility = wgpu::ShaderStage::Fragment;
  2334. bindings[numBindings].buffer.type = wgpu::BufferBindingType::Uniform;
  2335. bindings[numBindings].buffer.hasDynamicOffset = true;
  2336. numBindings++;
  2337. }
  2338. uint8_t numSamplers = 0;
  2339. for (uint32_t ii = 0; ii < _vsh->m_numSamplers; ++ii)
  2340. {
  2341. m_textures[ii] = _vsh->m_textures[ii];
  2342. m_samplers[ii] = _vsh->m_samplers[ii];
  2343. bindings[numBindings++] = _vsh->m_textures[ii];
  2344. bindings[numBindings++] = _vsh->m_samplers[ii];
  2345. }
  2346. numSamplers += _vsh->m_numSamplers;
  2347. if (NULL != _fsh)
  2348. {
  2349. for (uint32_t ii = 0; ii < _fsh->m_numSamplers; ++ii)
  2350. {
  2351. m_textures[numSamplers + ii] = _fsh->m_textures[ii];
  2352. m_samplers[numSamplers + ii] = _fsh->m_samplers[ii];
  2353. bindings[numBindings++] = _fsh->m_textures[ii];
  2354. bindings[numBindings++] = _fsh->m_samplers[ii];
  2355. }
  2356. numSamplers += _fsh->m_numSamplers;
  2357. }
  2358. for (uint8_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage)
  2359. {
  2360. if (isValid(m_vsh->m_bindInfo[stage].m_uniform))
  2361. {
  2362. m_bindInfo[stage] = m_vsh->m_bindInfo[stage];
  2363. }
  2364. else if (NULL != m_fsh && isValid(m_fsh->m_bindInfo[stage].m_uniform))
  2365. {
  2366. m_bindInfo[stage] = m_fsh->m_bindInfo[stage];
  2367. m_bindInfo[stage].m_index += _vsh->m_numSamplers;
  2368. }
  2369. }
  2370. m_numSamplers = numSamplers;
  2371. uint8_t numBuffers = 0;
  2372. for (uint32_t ii = 0; ii < _vsh->m_numBuffers; ++ii)
  2373. {
  2374. m_buffers[ii] = _vsh->m_buffers[ii];
  2375. bindings[numBindings++] = _vsh->m_buffers[ii];
  2376. }
  2377. numBuffers += _vsh->m_numBuffers;
  2378. if (NULL != _fsh)
  2379. {
  2380. for (uint32_t ii = 0; ii < _fsh->m_numBuffers; ++ii)
  2381. {
  2382. m_buffers[numBuffers + ii] = _fsh->m_buffers[ii];
  2383. bindings[numBindings++] = _fsh->m_buffers[ii];
  2384. }
  2385. numBuffers += _fsh->m_numBuffers;
  2386. }
  2387. m_numBuffers = numBuffers;
  2388. BX_ASSERT(m_numUniforms + m_numSamplers * 2 + m_numBuffers == numBindings, "");
  2389. wgpu::BindGroupLayoutDescriptor bindGroupDesc;
  2390. bindGroupDesc.entryCount = numBindings;
  2391. bindGroupDesc.entries = bindings;
  2392. m_bindGroupLayout = s_renderWgpu->m_device.CreateBindGroupLayout(&bindGroupDesc);
  2393. bx::HashMurmur2A murmur;
  2394. murmur.begin();
  2395. murmur.add(m_numUniforms);
  2396. murmur.add(m_textures, sizeof(wgpu::BindGroupLayoutEntry) * numSamplers);
  2397. murmur.add(m_samplers, sizeof(wgpu::BindGroupLayoutEntry) * numSamplers);
  2398. murmur.add(m_buffers, sizeof(wgpu::BindGroupLayoutEntry) * m_numBuffers);
  2399. m_bindGroupLayoutHash = murmur.end();
  2400. }
  2401. void ProgramWgpu::destroy()
  2402. {
  2403. m_vsh = NULL;
  2404. m_fsh = NULL;
  2405. if ( NULL != m_computePS )
  2406. {
  2407. BX_DELETE(g_allocator, m_computePS);
  2408. m_computePS = NULL;
  2409. }
  2410. }
  2411. void BufferWgpu::create(uint32_t _size, void* _data, uint16_t _flags, uint16_t _stride, bool _vertex)
  2412. {
  2413. BX_UNUSED(_stride);
  2414. m_size = _size;
  2415. m_flags = _flags;
  2416. m_vertex = _vertex;
  2417. const uint32_t paddedSize = bx::strideAlign(_size, 4);
  2418. bool storage = m_flags & BGFX_BUFFER_COMPUTE_READ_WRITE;
  2419. bool indirect = m_flags & BGFX_BUFFER_DRAW_INDIRECT;
  2420. wgpu::BufferDescriptor desc;
  2421. desc.size = paddedSize;
  2422. desc.usage = _vertex ? wgpu::BufferUsage::Vertex : wgpu::BufferUsage::Index;
  2423. desc.usage |= (storage || indirect) ? wgpu::BufferUsage::Storage : wgpu::BufferUsage::None;
  2424. desc.usage |= indirect ? wgpu::BufferUsage::Indirect : wgpu::BufferUsage::None;
  2425. desc.usage |= NULL == _data ? wgpu::BufferUsage::CopyDst : wgpu::BufferUsage::None;
  2426. desc.mappedAtCreation = NULL != _data;
  2427. m_ptr = s_renderWgpu->m_device.CreateBuffer(&desc);
  2428. if(NULL != _data)
  2429. {
  2430. bx::memCopy(m_ptr.GetMappedRange(), _data, _size);
  2431. m_ptr.Unmap();
  2432. }
  2433. }
  2434. void BufferWgpu::update(uint32_t _offset, uint32_t _size, void* _data, bool _discard)
  2435. {
  2436. wgpu::CommandEncoder& bce = s_renderWgpu->getBlitCommandEncoder();
  2437. if (!m_vertex && !_discard)
  2438. {
  2439. if ( m_dynamic == NULL )
  2440. {
  2441. m_dynamic = (uint8_t*)BX_ALLOC(g_allocator, m_size);
  2442. }
  2443. bx::memCopy(m_dynamic + _offset, _data, _size);
  2444. uint32_t start = _offset & 4;
  2445. uint32_t end = bx::strideAlign(_offset + _size, 4);
  2446. wgpu::BufferDescriptor desc;
  2447. desc.size = end - start;
  2448. desc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::CopySrc;
  2449. desc.mappedAtCreation = true;
  2450. wgpu::Buffer staging = s_renderWgpu->m_device.CreateBuffer(&desc);
  2451. bx::memCopy(staging.GetMappedRange(), m_dynamic, end - start);
  2452. staging.Unmap();
  2453. // TODO pad to 4 bytes
  2454. bce.CopyBufferToBuffer(staging, 0, m_ptr, start, end - start);
  2455. s_renderWgpu->m_cmd.release(staging);
  2456. }
  2457. else
  2458. {
  2459. wgpu::BufferDescriptor desc;
  2460. desc.size = _size;
  2461. desc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::CopySrc;
  2462. desc.mappedAtCreation = true;
  2463. wgpu::Buffer staging = s_renderWgpu->m_device.CreateBuffer(&desc);
  2464. bx::memCopy(staging.GetMappedRange(), _data, _size);
  2465. staging.Unmap();
  2466. bce.CopyBufferToBuffer(staging, 0, m_ptr, _offset, _size);
  2467. s_renderWgpu->m_cmd.release(staging);
  2468. }
  2469. }
  2470. void IndexBufferWgpu::create(uint32_t _size, void* _data, uint16_t _flags)
  2471. {
  2472. m_format = (_flags & BGFX_BUFFER_INDEX32) != 0
  2473. ? wgpu::IndexFormat::Uint32
  2474. : wgpu::IndexFormat::Uint16;
  2475. BufferWgpu::create(_size, _data, _flags);
  2476. }
  2477. void VertexBufferWgpu::create(uint32_t _size, void* _data, VertexLayoutHandle _layoutHandle, uint16_t _flags)
  2478. {
  2479. m_layoutHandle = _layoutHandle;
  2480. uint16_t stride = isValid(_layoutHandle)
  2481. ? s_renderWgpu->m_vertexDecls[_layoutHandle.idx].m_stride
  2482. : 0
  2483. ;
  2484. BufferWgpu::create(_size, _data, _flags, stride, true);
  2485. }
  2486. void TextureWgpu::create(TextureHandle _handle, const Memory* _mem, uint64_t _flags, uint8_t _skip)
  2487. {
  2488. m_handle = _handle;
  2489. m_sampler = s_renderWgpu->getSamplerState(uint32_t(_flags) );
  2490. bimg::ImageContainer imageContainer;
  2491. if (bimg::imageParse(imageContainer, _mem->data, _mem->size) )
  2492. {
  2493. const bimg::ImageBlockInfo& blockInfo = getBlockInfo(bimg::TextureFormat::Enum(imageContainer.m_format) );
  2494. const uint8_t startLod = bx::min<uint8_t>(_skip, imageContainer.m_numMips-1);
  2495. bimg::TextureInfo ti;
  2496. bimg::imageGetSize(
  2497. &ti
  2498. , uint16_t(imageContainer.m_width >>startLod)
  2499. , uint16_t(imageContainer.m_height>>startLod)
  2500. , uint16_t(imageContainer.m_depth >>startLod)
  2501. , imageContainer.m_cubeMap
  2502. , 1 < imageContainer.m_numMips
  2503. , imageContainer.m_numLayers
  2504. , imageContainer.m_format
  2505. );
  2506. ti.numMips = bx::min<uint8_t>(imageContainer.m_numMips-startLod, ti.numMips);
  2507. m_flags = _flags;
  2508. m_width = ti.width;
  2509. m_height = ti.height;
  2510. m_depth = ti.depth;
  2511. m_numLayers = ti.numLayers;
  2512. m_numMips = ti.numMips;
  2513. m_numSides = ti.numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  2514. m_requestedFormat = TextureFormat::Enum(imageContainer.m_format);
  2515. m_textureFormat = getViableTextureFormat(imageContainer);
  2516. if (m_requestedFormat == bgfx::TextureFormat::D16)
  2517. m_textureFormat = bgfx::TextureFormat::D32F;
  2518. const bool compressed = bimg::isCompressed(bimg::TextureFormat::Enum(imageContainer.m_format));
  2519. if (compressed)
  2520. m_textureFormat = bgfx::TextureFormat::BGRA8;
  2521. const bool convert = m_textureFormat != m_requestedFormat;
  2522. const uint8_t bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(m_textureFormat) );
  2523. wgpu::TextureDescriptor desc = defaultDescriptor<wgpu::TextureDescriptor>();
  2524. //desc.label = getName(_handle);
  2525. if (1 < ti.numLayers)
  2526. {
  2527. if (imageContainer.m_cubeMap)
  2528. {
  2529. m_type = TextureCube;
  2530. desc.dimension = wgpu::TextureDimension::e2D;
  2531. }
  2532. else
  2533. {
  2534. m_type = Texture2D;
  2535. desc.dimension = wgpu::TextureDimension::e2D;
  2536. }
  2537. }
  2538. else if (imageContainer.m_cubeMap)
  2539. {
  2540. m_type = TextureCube;
  2541. desc.dimension = wgpu::TextureDimension::e2D;
  2542. }
  2543. else if (1 < imageContainer.m_depth)
  2544. {
  2545. m_type = Texture3D;
  2546. desc.dimension = wgpu::TextureDimension::e3D;
  2547. }
  2548. else
  2549. {
  2550. m_type = Texture2D;
  2551. desc.dimension = wgpu::TextureDimension::e2D;
  2552. }
  2553. const uint16_t numSides = ti.numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  2554. const uint32_t numSrd = numSides * ti.numMips;
  2555. const bool writeOnly = 0 != (_flags&BGFX_TEXTURE_RT_WRITE_ONLY);
  2556. const bool computeWrite = 0 != (_flags&BGFX_TEXTURE_COMPUTE_WRITE);
  2557. const bool renderTarget = 0 != (_flags&BGFX_TEXTURE_RT_MASK);
  2558. const bool srgb = 0 != (_flags&BGFX_TEXTURE_SRGB);
  2559. BX_TRACE("Texture %3d: %s (requested: %s), layers %d, %dx%d%s RT[%c], WO[%c], CW[%c], sRGB[%c]"
  2560. , this - s_renderWgpu->m_textures
  2561. , getName( (TextureFormat::Enum)m_textureFormat)
  2562. , getName( (TextureFormat::Enum)m_requestedFormat)
  2563. , ti.numLayers
  2564. , ti.width
  2565. , ti.height
  2566. , imageContainer.m_cubeMap ? "x6" : ""
  2567. , renderTarget ? 'x' : ' '
  2568. , writeOnly ? 'x' : ' '
  2569. , computeWrite ? 'x' : ' '
  2570. , srgb ? 'x' : ' '
  2571. );
  2572. const uint32_t msaaQuality = bx::uint32_satsub( (_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT, 1);
  2573. const int32_t sampleCount = s_msaa[msaaQuality];
  2574. wgpu::TextureFormat format = wgpu::TextureFormat::Undefined;
  2575. if (srgb)
  2576. {
  2577. format = s_textureFormat[m_textureFormat].m_fmtSrgb;
  2578. BX_WARN(format != wgpu::TextureFormat::Undefined
  2579. , "sRGB not supported for texture format %d"
  2580. , m_textureFormat
  2581. );
  2582. }
  2583. if (format == wgpu::TextureFormat::Undefined)
  2584. {
  2585. // not swizzled and not sRGB, or sRGB unsupported
  2586. format = s_textureFormat[m_textureFormat].m_fmt;
  2587. }
  2588. desc.format = format;
  2589. desc.size.width = m_width;
  2590. desc.size.height = m_height;
  2591. desc.size.depthOrArrayLayers = m_numSides * bx::uint32_max(1,imageContainer.m_depth);
  2592. desc.mipLevelCount = m_numMips;
  2593. desc.sampleCount = 1;
  2594. desc.usage = wgpu::TextureUsage::Sampled;
  2595. desc.usage |= wgpu::TextureUsage::CopyDst;
  2596. desc.usage |= wgpu::TextureUsage::CopySrc;
  2597. if (computeWrite)
  2598. {
  2599. desc.usage |= wgpu::TextureUsage::Storage;
  2600. }
  2601. if (renderTarget)
  2602. {
  2603. desc.usage |= wgpu::TextureUsage::OutputAttachment;
  2604. }
  2605. m_ptr = s_renderWgpu->m_device.CreateTexture(&desc);
  2606. if (sampleCount > 1)
  2607. {
  2608. desc.sampleCount = sampleCount;
  2609. m_ptrMsaa = s_renderWgpu->m_device.CreateTexture(&desc);
  2610. }
  2611. // decode images
  2612. struct ImageInfo
  2613. {
  2614. uint8_t* data;
  2615. uint32_t width;
  2616. uint32_t height;
  2617. uint32_t depth;
  2618. uint32_t pitch;
  2619. uint32_t slice;
  2620. uint32_t size;
  2621. uint8_t mipLevel;
  2622. uint8_t layer;
  2623. };
  2624. ImageInfo* imageInfos = (ImageInfo*)BX_ALLOC(g_allocator, sizeof(ImageInfo) * numSrd);
  2625. bx::memSet(imageInfos, 0, sizeof(ImageInfo) * numSrd);
  2626. uint32_t alignment = 1; // tightly aligned buffer
  2627. uint32_t kk = 0;
  2628. for (uint8_t side = 0; side < numSides; ++side)
  2629. {
  2630. for (uint8_t lod = 0; lod < ti.numMips; ++lod)
  2631. {
  2632. bimg::ImageMip mip;
  2633. if (bimg::imageGetRawData(imageContainer, side, lod + startLod, _mem->data, _mem->size, mip))
  2634. {
  2635. if (convert)
  2636. {
  2637. const uint32_t pitch = bx::strideAlign(bx::max<uint32_t>(mip.m_width, 4) * bpp / 8, alignment);
  2638. const uint32_t slice = bx::strideAlign(bx::max<uint32_t>(mip.m_height, 4) * pitch, alignment);
  2639. const uint32_t size = slice * mip.m_depth;
  2640. uint8_t* temp = (uint8_t*)BX_ALLOC(g_allocator, size);
  2641. bimg::imageDecodeToBgra8(
  2642. g_allocator
  2643. , temp
  2644. , mip.m_data
  2645. , mip.m_width
  2646. , mip.m_height
  2647. , pitch
  2648. , mip.m_format
  2649. );
  2650. imageInfos[kk].data = temp;
  2651. imageInfos[kk].width = mip.m_width;
  2652. imageInfos[kk].height = mip.m_height;
  2653. imageInfos[kk].depth = mip.m_depth;
  2654. imageInfos[kk].pitch = pitch;
  2655. imageInfos[kk].slice = slice;
  2656. imageInfos[kk].size = size;
  2657. imageInfos[kk].mipLevel = lod;
  2658. imageInfos[kk].layer = side;
  2659. }
  2660. else if (compressed)
  2661. {
  2662. const uint32_t pitch = bx::strideAlign((mip.m_width / blockInfo.blockWidth) * mip.m_blockSize, alignment);
  2663. const uint32_t slice = bx::strideAlign((mip.m_height / blockInfo.blockHeight) * pitch, alignment);
  2664. const uint32_t size = slice * mip.m_depth;
  2665. uint8_t* temp = (uint8_t*)BX_ALLOC(g_allocator, size);
  2666. bimg::imageCopy(
  2667. temp
  2668. , mip.m_height / blockInfo.blockHeight
  2669. , (mip.m_width / blockInfo.blockWidth) * mip.m_blockSize
  2670. , mip.m_depth
  2671. , mip.m_data
  2672. , pitch
  2673. );
  2674. imageInfos[kk].data = temp;
  2675. imageInfos[kk].width = mip.m_width;
  2676. imageInfos[kk].height = mip.m_height;
  2677. imageInfos[kk].depth = mip.m_depth;
  2678. imageInfos[kk].pitch = pitch;
  2679. imageInfos[kk].slice = slice;
  2680. imageInfos[kk].size = size;
  2681. imageInfos[kk].mipLevel = lod;
  2682. imageInfos[kk].layer = side;
  2683. }
  2684. else
  2685. {
  2686. const uint32_t pitch = bx::strideAlign(mip.m_width * mip.m_bpp / 8, alignment);
  2687. const uint32_t slice = bx::strideAlign(mip.m_height * pitch, alignment);
  2688. const uint32_t size = slice * mip.m_depth;
  2689. uint8_t* temp = (uint8_t*)BX_ALLOC(g_allocator, size);
  2690. bimg::imageCopy(temp
  2691. , mip.m_height
  2692. , mip.m_width * mip.m_bpp / 8
  2693. , mip.m_depth
  2694. , mip.m_data
  2695. , pitch
  2696. );
  2697. imageInfos[kk].data = temp;
  2698. imageInfos[kk].width = mip.m_width;
  2699. imageInfos[kk].height = mip.m_height;
  2700. imageInfos[kk].depth = mip.m_depth;
  2701. imageInfos[kk].pitch = pitch;
  2702. imageInfos[kk].slice = slice;
  2703. imageInfos[kk].size = size;
  2704. imageInfos[kk].mipLevel = lod;
  2705. imageInfos[kk].layer = side;
  2706. }
  2707. }
  2708. ++kk;
  2709. }
  2710. }
  2711. uint32_t totalMemSize = 0;
  2712. for (uint32_t ii = 0; ii < numSrd; ++ii)
  2713. {
  2714. const uint32_t dstpitch = bx::strideAlign(imageInfos[ii].pitch, kMinBufferOffsetAlignment);
  2715. totalMemSize += dstpitch * imageInfos[ii].height;
  2716. //totalMemSize += imageInfos[ii].size;
  2717. }
  2718. wgpu::Buffer stagingBuffer;
  2719. if (totalMemSize > 0)
  2720. {
  2721. wgpu::BufferDescriptor staginBufferDesc;
  2722. staginBufferDesc.size = totalMemSize;
  2723. staginBufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::CopySrc;
  2724. staginBufferDesc.mappedAtCreation = true;
  2725. stagingBuffer = s_renderWgpu->m_device.CreateBuffer(&staginBufferDesc);
  2726. void* stagingData = stagingBuffer.GetMappedRange();
  2727. uint64_t offset = 0;
  2728. for (uint32_t ii = 0; ii < numSrd; ++ii)
  2729. {
  2730. const uint32_t dstpitch = bx::strideAlign(imageInfos[ii].pitch, kMinBufferOffsetAlignment);
  2731. const uint8_t* src = (uint8_t*)imageInfos[ii].data;
  2732. uint8_t* dst = (uint8_t*)stagingData;
  2733. for (uint32_t yy = 0; yy < imageInfos[ii].height; ++yy, src += imageInfos[ii].pitch, offset += dstpitch)
  2734. {
  2735. bx::memCopy(dst + offset, src, imageInfos[ii].pitch);
  2736. }
  2737. //bx::memCopy(dst + offset, imageInfos[ii].data, imageInfos[ii].size);
  2738. //offset += imageInfos[ii].size;
  2739. }
  2740. stagingBuffer.Unmap();
  2741. }
  2742. wgpu::ImageCopyBuffer* imageCopyBuffer = (wgpu::ImageCopyBuffer*)BX_ALLOC(g_allocator, sizeof(wgpu::ImageCopyBuffer) * numSrd);
  2743. wgpu::ImageCopyTexture* imageCopyTexture = (wgpu::ImageCopyTexture*)BX_ALLOC(g_allocator, sizeof(wgpu::ImageCopyTexture) * numSrd);
  2744. wgpu::Extent3D* textureCopySize = (wgpu::Extent3D*)BX_ALLOC(g_allocator, sizeof(wgpu::Extent3D) * numSrd);
  2745. uint64_t offset = 0;
  2746. for (uint32_t ii = 0; ii < numSrd; ++ii)
  2747. {
  2748. const uint32_t dstpitch = bx::strideAlign(imageInfos[ii].pitch, kMinBufferOffsetAlignment);
  2749. uint32_t idealWidth = bx::max<uint32_t>(1, m_width >> imageInfos[ii].mipLevel);
  2750. uint32_t idealHeight = bx::max<uint32_t>(1, m_height >> imageInfos[ii].mipLevel);
  2751. BX_PLACEMENT_NEW(&imageCopyBuffer[ii], wgpu::ImageCopyBuffer)();
  2752. BX_PLACEMENT_NEW(&imageCopyTexture[ii], wgpu::ImageCopyTexture)();
  2753. BX_PLACEMENT_NEW(&textureCopySize[ii], wgpu::Extent3D)();
  2754. imageCopyBuffer[ii].buffer = stagingBuffer;
  2755. imageCopyBuffer[ii].layout.offset = offset;
  2756. imageCopyBuffer[ii].layout.bytesPerRow = dstpitch; // assume that image data are tightly aligned
  2757. imageCopyBuffer[ii].layout.rowsPerImage = 0; // assume that image data are tightly aligned
  2758. imageCopyTexture[ii].texture = m_ptr;
  2759. imageCopyTexture[ii].mipLevel = imageInfos[ii].mipLevel;
  2760. imageCopyTexture[ii].origin = { 0, 0, imageInfos[ii].layer };
  2761. textureCopySize[ii] = { idealWidth, idealHeight, imageInfos[ii].depth };
  2762. offset += dstpitch * imageInfos[ii].height;
  2763. //offset += imageInfos[ii].size;
  2764. }
  2765. if (stagingBuffer)
  2766. {
  2767. wgpu::CommandEncoder encoder = s_renderWgpu->getBlitCommandEncoder();
  2768. //wgpu::CommandEncoder encoder = s_renderWgpu->m_cmd.m_encoder;
  2769. for (uint32_t ii = 0; ii < numSrd; ++ii)
  2770. {
  2771. encoder.CopyBufferToTexture(&imageCopyBuffer[ii], &imageCopyTexture[ii], &textureCopySize[ii]);
  2772. }
  2773. }
  2774. else
  2775. {
  2776. //VkCommandBuffer commandBuffer = s_renderVK->beginNewCommand();
  2777. //setImageMemoryBarrier(
  2778. // commandBuffer
  2779. // , (m_flags & BGFX_TEXTURE_COMPUTE_WRITE
  2780. // ? VK_IMAGE_LAYOUT_GENERAL
  2781. // : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
  2782. // )
  2783. //);
  2784. //s_renderVK->submitCommandAndWait(commandBuffer);
  2785. }
  2786. //vkFreeMemory(device, stagingDeviceMem, allocatorCb);
  2787. //vkDestroy(stagingBuffer);
  2788. BX_FREE(g_allocator, imageCopyBuffer);
  2789. BX_FREE(g_allocator, imageCopyTexture);
  2790. BX_FREE(g_allocator, textureCopySize);
  2791. for (uint32_t ii = 0; ii < numSrd; ++ii)
  2792. {
  2793. BX_FREE(g_allocator, imageInfos[ii].data);
  2794. }
  2795. BX_FREE(g_allocator, imageInfos);
  2796. }
  2797. }
  2798. void TextureWgpu::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem)
  2799. {
  2800. const uint32_t bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(m_textureFormat) );
  2801. const uint32_t rectpitch = _rect.m_width*bpp/8;
  2802. const uint32_t srcpitch = UINT16_MAX == _pitch ? rectpitch : _pitch;
  2803. const uint16_t zz = (m_type == Texture3D) ? _z : _side;
  2804. // TODO (hugoam) This won't work for 3D texture arrays, but do we even support that
  2805. const bool convert = m_textureFormat != m_requestedFormat;
  2806. uint8_t* data = _mem->data;
  2807. uint8_t* temp = NULL;
  2808. if (convert)
  2809. {
  2810. temp = (uint8_t*)BX_ALLOC(g_allocator, rectpitch*_rect.m_height);
  2811. bimg::imageDecodeToBgra8(
  2812. g_allocator
  2813. , temp
  2814. , data
  2815. , _rect.m_width
  2816. , _rect.m_height
  2817. , srcpitch
  2818. , bimg::TextureFormat::Enum(m_requestedFormat)
  2819. );
  2820. data = temp;
  2821. }
  2822. const uint32_t dstpitch = bx::strideAlign(rectpitch, kMinBufferOffsetAlignment);
  2823. wgpu::BufferDescriptor desc;
  2824. desc.size = dstpitch * _rect.m_height;
  2825. desc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::CopySrc;
  2826. desc.mappedAtCreation = true;
  2827. wgpu::Buffer staging = s_renderWgpu->m_device.CreateBuffer(&desc);
  2828. const uint8_t* src = (uint8_t*)data;
  2829. uint8_t* dst = (uint8_t*)staging.GetMappedRange();
  2830. uint64_t offset = 0;
  2831. for (uint32_t yy = 0; yy < _rect.m_height; ++yy, src += srcpitch, offset += dstpitch)
  2832. {
  2833. const uint32_t size = bx::strideAlign(rectpitch, 4);
  2834. bx::memCopy(dst + offset, src, size);
  2835. }
  2836. staging.Unmap();
  2837. wgpu::ImageCopyBuffer srcView;
  2838. srcView.buffer = staging;
  2839. srcView.layout.bytesPerRow = dstpitch;
  2840. srcView.layout.rowsPerImage = 0;
  2841. wgpu::ImageCopyTexture destView;
  2842. destView.texture = m_ptr;
  2843. destView.mipLevel = _mip;
  2844. destView.origin = { _rect.m_x, _rect.m_y, zz };
  2845. wgpu::Extent3D destExtent = { _rect.m_width, _rect.m_height, _depth };
  2846. //region.imageSubresource.aspectMask = m_vkTextureAspect;
  2847. wgpu::CommandEncoder encoder = s_renderWgpu->getBlitCommandEncoder();
  2848. //wgpu::CommandEncoder encoder = s_renderWgpu->m_cmd.m_encoder;
  2849. encoder.CopyBufferToTexture(&srcView, &destView, &destExtent);
  2850. //wgpu::CommandBuffer copy = encoder.Finish();
  2851. //wgpu::Queue queue = s_renderWgpu->m_queue;
  2852. //queue.Submit(1, &copy);
  2853. //staging.Destroy();
  2854. if (NULL != temp)
  2855. {
  2856. BX_FREE(g_allocator, temp);
  2857. }
  2858. }
  2859. void BindStateWgpu::clear()
  2860. {
  2861. m_bindGroup = NULL;
  2862. }
  2863. void StagingBufferWgpu::create(uint32_t _size, bool mapped)
  2864. {
  2865. m_size = _size;
  2866. wgpu::BufferDescriptor desc;
  2867. desc.size = _size;
  2868. desc.usage = wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc;
  2869. desc.mappedAtCreation = mapped;
  2870. m_buffer = s_renderWgpu->m_device.CreateBuffer(&desc);
  2871. if (mapped)
  2872. {
  2873. m_data = m_buffer.GetMappedRange();
  2874. }
  2875. else
  2876. {
  2877. map();
  2878. }
  2879. }
  2880. void StagingBufferWgpu::map()
  2881. {
  2882. auto ready = [](WGPUBufferMapAsyncStatus status, void* userdata)
  2883. {
  2884. StagingBufferWgpu* staging = static_cast<StagingBufferWgpu*>(userdata);
  2885. BX_WARN(status == WGPUBufferMapAsyncStatus_Success, "Failed mapping staging buffer (size %d) for writing with error %d", staging->m_size, status);
  2886. if (status == WGPUBufferMapAsyncStatus_Success)
  2887. {
  2888. void* data = staging->m_buffer.GetMappedRange();
  2889. staging->mapped(data);
  2890. }
  2891. };
  2892. m_buffer.MapAsync(wgpu::MapMode::Write, 0, m_size, ready, this);
  2893. }
  2894. void StagingBufferWgpu::unmap()
  2895. {
  2896. m_data = NULL;
  2897. m_buffer.Unmap();
  2898. }
  2899. void StagingBufferWgpu::destroy()
  2900. {
  2901. m_buffer = NULL;
  2902. }
  2903. void StagingBufferWgpu::mapped(void* _data)
  2904. {
  2905. m_data = _data;
  2906. }
  2907. void ScratchBufferWgpu::create(uint32_t _size)
  2908. {
  2909. m_offset = 0;
  2910. m_size = _size;
  2911. wgpu::BufferDescriptor desc;
  2912. desc.size = BGFX_CONFIG_MAX_DRAW_CALLS * 128;
  2913. desc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
  2914. m_buffer = s_renderWgpu->m_device.CreateBuffer(&desc);
  2915. }
  2916. void ScratchBufferWgpu::destroy()
  2917. {
  2918. }
  2919. void ScratchBufferWgpu::begin()
  2920. {
  2921. for (uint8_t ii = 0; ii < WEBGPU_NUM_UNIFORM_BUFFERS; ++ii)
  2922. {
  2923. if (NULL != s_renderWgpu->m_uniformBuffers[ii].m_data)
  2924. {
  2925. m_staging = &s_renderWgpu->m_uniformBuffers[ii];
  2926. break;
  2927. }
  2928. }
  2929. BX_ASSERT(NULL != m_staging, "No available mapped uniform buffer");
  2930. }
  2931. uint32_t ScratchBufferWgpu::write(void* data, uint64_t _size, uint64_t _offset)
  2932. {
  2933. BX_ASSERT(nullptr != m_staging, "Cannot write uniforms outside of begin()/submit() calls");
  2934. BX_ASSERT(m_size > m_offset + _offset, "Out-of-bounds scratch buffer write");
  2935. uint32_t offset = m_offset;
  2936. bx::memCopy((void*)((uint8_t*)m_staging->m_data + offset), data, _size);
  2937. m_offset += _offset;
  2938. return offset;
  2939. }
  2940. uint32_t ScratchBufferWgpu::write(void* data, uint64_t _size)
  2941. {
  2942. BX_ASSERT(nullptr != m_staging, "Cannot write uniforms outside of begin()/submit() calls");
  2943. BX_ASSERT(m_size > m_offset + _size, "Out-of-bounds scratch buffer write");
  2944. uint32_t offset = m_offset;
  2945. bx::memCopy((void*)((uint8_t*)m_staging->m_data + offset), data, _size);
  2946. m_offset += _size;
  2947. return offset;
  2948. }
  2949. void ScratchBufferWgpu::submit()
  2950. {
  2951. m_staging->unmap();
  2952. if (m_offset != 0)
  2953. {
  2954. wgpu::CommandEncoder& bce = s_renderWgpu->getStagingEncoder();
  2955. bce.CopyBufferToBuffer(m_staging->m_buffer, 0, m_buffer, 0, m_offset);
  2956. }
  2957. }
  2958. void ScratchBufferWgpu::release()
  2959. {
  2960. m_staging->map();
  2961. m_staging = NULL;
  2962. m_offset = 0;
  2963. }
  2964. void BindStateCacheWgpu::create() //(uint32_t _maxBindGroups)
  2965. {
  2966. //m_maxBindStates = 1024; // _maxBindStates;
  2967. m_currentBindState = 0;
  2968. }
  2969. void BindStateCacheWgpu::destroy()
  2970. {
  2971. reset();
  2972. }
  2973. void BindStateCacheWgpu::reset()
  2974. {
  2975. for (size_t i = 0; i < m_currentBindState; ++i)
  2976. {
  2977. m_bindStates[i] = {};
  2978. }
  2979. m_currentBindState = 0;
  2980. }
  2981. wgpu::TextureView TextureWgpu::getTextureMipLevel(int _mip)
  2982. {
  2983. if (_mip >= 0
  2984. && _mip < m_numMips
  2985. && m_ptr)
  2986. {
  2987. if (!m_ptrMips[_mip])
  2988. {
  2989. wgpu::TextureViewDescriptor desc;
  2990. desc.baseMipLevel = _mip;
  2991. desc.mipLevelCount = 1;
  2992. desc.format = s_textureFormat[m_textureFormat].m_fmt;
  2993. if (TextureCube == m_type)
  2994. {
  2995. //desc.dimension = MTLTextureType2DArray;
  2996. desc.baseArrayLayer = 0;
  2997. desc.arrayLayerCount = m_numLayers * 6;
  2998. }
  2999. else
  3000. {
  3001. desc.baseArrayLayer = 0;
  3002. desc.arrayLayerCount = m_numLayers;
  3003. }
  3004. m_ptrMips[_mip] = m_ptr.CreateView(&desc);
  3005. }
  3006. return m_ptrMips[_mip];
  3007. }
  3008. return wgpu::TextureView();
  3009. }
  3010. void SwapChainWgpu::init(wgpu::Device _device, void* _nwh, uint32_t _width, uint32_t _height)
  3011. {
  3012. BX_UNUSED(_nwh);
  3013. wgpu::SwapChainDescriptor desc;
  3014. desc.usage = wgpu::TextureUsage::OutputAttachment;
  3015. desc.width = _width;
  3016. desc.height = _height;
  3017. #if !BX_PLATFORM_EMSCRIPTEN
  3018. m_impl = createSwapChain(_device, _nwh);
  3019. desc.presentMode = wgpu::PresentMode::Immediate;
  3020. desc.format = wgpu::TextureFormat::RGBA8Unorm;
  3021. desc.implementation = reinterpret_cast<uint64_t>(&m_impl);
  3022. m_swapChain = _device.CreateSwapChain(nullptr, &desc);
  3023. #else
  3024. wgpu::SurfaceDescriptorFromCanvasHTMLSelector canvasDesc{};
  3025. canvasDesc.selector = "#canvas";
  3026. wgpu::SurfaceDescriptor surfDesc{};
  3027. surfDesc.nextInChain = &canvasDesc;
  3028. wgpu::Surface surface = wgpu::Instance().CreateSurface(&surfDesc);
  3029. desc.presentMode = wgpu::PresentMode::Fifo;
  3030. desc.format = wgpu::TextureFormat::BGRA8Unorm;
  3031. m_swapChain = _device.CreateSwapChain(surface, &desc);
  3032. #endif
  3033. m_colorFormat = desc.format;
  3034. m_depthFormat = wgpu::TextureFormat::Depth24PlusStencil8;
  3035. }
  3036. void SwapChainWgpu::resize(FrameBufferWgpu& _frameBuffer, uint32_t _width, uint32_t _height, uint32_t _flags)
  3037. {
  3038. BX_TRACE("SwapChainWgpu::resize");
  3039. const int32_t sampleCount = s_msaa[(_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT];
  3040. wgpu::TextureFormat format = (_flags & BGFX_RESET_SRGB_BACKBUFFER)
  3041. #ifdef DAWN_ENABLE_BACKEND_VULKAN
  3042. ? wgpu::TextureFormat::BGRA8UnormSrgb
  3043. : wgpu::TextureFormat::BGRA8Unorm
  3044. #else
  3045. ? wgpu::TextureFormat::RGBA8UnormSrgb
  3046. : wgpu::TextureFormat::RGBA8Unorm
  3047. #endif
  3048. ;
  3049. #if !BX_PLATFORM_EMSCRIPTEN
  3050. m_swapChain.Configure(format, wgpu::TextureUsage::OutputAttachment, _width, _height);
  3051. #endif
  3052. m_colorFormat = format;
  3053. m_depthFormat = wgpu::TextureFormat::Depth24PlusStencil8;
  3054. bx::HashMurmur2A murmur;
  3055. murmur.begin();
  3056. murmur.add(1);
  3057. murmur.add((uint32_t)m_colorFormat);
  3058. murmur.add((uint32_t)m_depthFormat);
  3059. murmur.add((uint32_t)sampleCount);
  3060. _frameBuffer.m_pixelFormatHash = murmur.end();
  3061. wgpu::TextureDescriptor desc;
  3062. desc.dimension = wgpu::TextureDimension::e2D;
  3063. desc.size.width = _width;
  3064. desc.size.height = _height;
  3065. desc.size.depthOrArrayLayers = 1;
  3066. desc.mipLevelCount = 1;
  3067. desc.sampleCount = sampleCount;
  3068. desc.usage = wgpu::TextureUsage::OutputAttachment;
  3069. if (m_backBufferDepth)
  3070. {
  3071. m_backBufferDepth.Destroy();
  3072. }
  3073. desc.format = wgpu::TextureFormat::Depth24PlusStencil8;
  3074. m_backBufferDepth = s_renderWgpu->m_device.CreateTexture(&desc);
  3075. if (sampleCount > 1)
  3076. {
  3077. if (m_backBufferColorMsaa)
  3078. {
  3079. m_backBufferColorMsaa.Destroy();
  3080. }
  3081. desc.format = m_colorFormat;
  3082. desc.sampleCount = sampleCount;
  3083. m_backBufferColorMsaa = s_renderWgpu->m_device.CreateTexture(&desc);
  3084. }
  3085. }
  3086. void SwapChainWgpu::flip()
  3087. {
  3088. m_drawable = m_swapChain.GetCurrentTextureView();
  3089. }
  3090. wgpu::TextureView SwapChainWgpu::current()
  3091. {
  3092. if (!m_drawable)
  3093. m_drawable = m_swapChain.GetCurrentTextureView();
  3094. return m_drawable;
  3095. }
  3096. void FrameBufferWgpu::create(uint8_t _num, const Attachment* _attachment)
  3097. {
  3098. m_swapChain = NULL;
  3099. m_denseIdx = UINT16_MAX;
  3100. m_num = 0;
  3101. m_width = 0;
  3102. m_height = 0;
  3103. for (uint32_t ii = 0; ii < _num; ++ii)
  3104. {
  3105. const Attachment& at = _attachment[ii];
  3106. TextureHandle handle = at.handle;
  3107. if (isValid(handle) )
  3108. {
  3109. const TextureWgpu& texture = s_renderWgpu->m_textures[handle.idx];
  3110. if (0 == m_width)
  3111. {
  3112. m_width = texture.m_width;
  3113. m_height = texture.m_height;
  3114. }
  3115. if (bimg::isDepth(bimg::TextureFormat::Enum(texture.m_textureFormat) ) )
  3116. {
  3117. m_depthHandle = handle;
  3118. m_depthAttachment = at;
  3119. }
  3120. else
  3121. {
  3122. m_colorHandle[m_num] = handle;
  3123. m_colorAttachment[m_num] = at;
  3124. m_num++;
  3125. }
  3126. }
  3127. }
  3128. bx::HashMurmur2A murmur;
  3129. murmur.begin();
  3130. murmur.add(m_num);
  3131. for (uint32_t ii = 0; ii < m_num; ++ii)
  3132. {
  3133. const TextureWgpu& texture = s_renderWgpu->m_textures[m_colorHandle[ii].idx];
  3134. murmur.add(uint32_t(s_textureFormat[texture.m_textureFormat].m_fmt) );
  3135. }
  3136. if (!isValid(m_depthHandle) )
  3137. {
  3138. murmur.add(uint32_t(wgpu::TextureFormat::Undefined) );
  3139. }
  3140. else
  3141. {
  3142. const TextureWgpu& depthTexture = s_renderWgpu->m_textures[m_depthHandle.idx];
  3143. murmur.add(uint32_t(s_textureFormat[depthTexture.m_textureFormat].m_fmt) );
  3144. }
  3145. murmur.add(1); // SampleCount
  3146. m_pixelFormatHash = murmur.end();
  3147. }
  3148. bool FrameBufferWgpu::create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, TextureFormat::Enum _depthFormat)
  3149. {
  3150. BX_UNUSED(_format, _depthFormat);
  3151. m_swapChain = BX_NEW(g_allocator, SwapChainWgpu);
  3152. m_num = 0;
  3153. m_width = _width;
  3154. m_height = _height;
  3155. m_nwh = _nwh;
  3156. m_denseIdx = _denseIdx;
  3157. m_swapChain->init(s_renderWgpu->m_device, _nwh, _width, _height);
  3158. m_swapChain->resize(*this, _width, _height, 0);
  3159. return m_swapChain->m_swapChain != NULL;
  3160. }
  3161. void FrameBufferWgpu::postReset()
  3162. {
  3163. }
  3164. uint16_t FrameBufferWgpu::destroy()
  3165. {
  3166. if (NULL != m_swapChain)
  3167. {
  3168. BX_DELETE(g_allocator, m_swapChain);
  3169. m_swapChain = NULL;
  3170. }
  3171. m_num = 0;
  3172. m_nwh = NULL;
  3173. m_depthHandle.idx = kInvalidHandle;
  3174. uint16_t denseIdx = m_denseIdx;
  3175. m_denseIdx = UINT16_MAX;
  3176. return denseIdx;
  3177. }
  3178. void CommandQueueWgpu::init(wgpu::Queue _queue)
  3179. {
  3180. m_queue = _queue;
  3181. #if BGFX_CONFIG_MULTITHREADED
  3182. //m_framesSemaphore.post(BGFX_CONFIG_MAX_FRAME_LATENCY);
  3183. #endif
  3184. }
  3185. void CommandQueueWgpu::shutdown()
  3186. {
  3187. finish(true);
  3188. }
  3189. void CommandQueueWgpu::beginRender()
  3190. {
  3191. m_renderEncoder = s_renderWgpu->m_device.CreateCommandEncoder();
  3192. }
  3193. void CommandQueueWgpu::beginStaging()
  3194. {
  3195. m_stagingEncoder = s_renderWgpu->m_device.CreateCommandEncoder();
  3196. }
  3197. inline void commandBufferFinishedCallback(void* _data)
  3198. {
  3199. #if BGFX_CONFIG_MULTITHREADED
  3200. CommandQueueWgpu* queue = (CommandQueueWgpu*)_data;
  3201. if (queue)
  3202. {
  3203. //queue->m_framesSemaphore.post();
  3204. }
  3205. #else
  3206. BX_UNUSED(_data);
  3207. #endif
  3208. }
  3209. void CommandQueueWgpu::kick(bool _endFrame, bool _waitForFinish)
  3210. {
  3211. if (m_renderEncoder)
  3212. {
  3213. if (_endFrame)
  3214. {
  3215. m_releaseWriteIndex = (m_releaseWriteIndex + 1) % BGFX_CONFIG_MAX_FRAME_LATENCY;
  3216. //m_encoder.addCompletedHandler(commandBufferFinishedCallback, this);
  3217. }
  3218. if (m_stagingEncoder)
  3219. {
  3220. wgpu::CommandBuffer commands = m_stagingEncoder.Finish();
  3221. m_queue.Submit(1, &commands);
  3222. }
  3223. wgpu::CommandBuffer commands = m_renderEncoder.Finish();
  3224. m_queue.Submit(1, &commands);
  3225. if (_waitForFinish)
  3226. {
  3227. #if BGFX_CONFIG_MULTITHREADED
  3228. //m_framesSemaphore.post();
  3229. #endif
  3230. }
  3231. m_stagingEncoder = NULL;
  3232. m_renderEncoder = NULL;
  3233. }
  3234. }
  3235. void CommandQueueWgpu::finish(bool _finishAll)
  3236. {
  3237. if (_finishAll)
  3238. {
  3239. uint32_t count = m_renderEncoder
  3240. ? 2
  3241. : 3
  3242. ;
  3243. for (uint32_t ii = 0; ii < count; ++ii)
  3244. {
  3245. consume();
  3246. }
  3247. #if BGFX_CONFIG_MULTITHREADED
  3248. //m_framesSemaphore.post(count);
  3249. #endif
  3250. }
  3251. else
  3252. {
  3253. consume();
  3254. }
  3255. }
  3256. void CommandQueueWgpu::release(wgpu::Buffer _buffer)
  3257. {
  3258. m_release[m_releaseWriteIndex].push_back(_buffer);
  3259. }
  3260. void CommandQueueWgpu::consume()
  3261. {
  3262. #if BGFX_CONFIG_MULTITHREADED
  3263. //m_framesSemaphore.wait();
  3264. #endif
  3265. m_releaseReadIndex = (m_releaseReadIndex + 1) % BGFX_CONFIG_MAX_FRAME_LATENCY;
  3266. for (wgpu::Buffer& buffer : m_release[m_releaseReadIndex])
  3267. {
  3268. buffer.Destroy();
  3269. }
  3270. m_release[m_releaseReadIndex].clear();
  3271. }
  3272. void TimerQueryWgpu::init()
  3273. {
  3274. m_frequency = bx::getHPFrequency();
  3275. }
  3276. void TimerQueryWgpu::shutdown()
  3277. {
  3278. }
  3279. uint32_t TimerQueryWgpu::begin(uint32_t _resultIdx, uint32_t _frameNum)
  3280. {
  3281. BX_UNUSED(_resultIdx);
  3282. BX_UNUSED(_frameNum);
  3283. return 0;
  3284. }
  3285. void TimerQueryWgpu::end(uint32_t _idx)
  3286. {
  3287. BX_UNUSED(_idx);
  3288. }
  3289. #if 0
  3290. static void setTimestamp(void* _data)
  3291. {
  3292. *( (int64_t*)_data) = bx::getHPCounter();
  3293. }
  3294. #endif
  3295. void TimerQueryWgpu::addHandlers(wgpu::CommandBuffer& _commandBuffer)
  3296. {
  3297. BX_UNUSED(_commandBuffer);
  3298. while (0 == m_control.reserve(1) )
  3299. {
  3300. m_control.consume(1);
  3301. }
  3302. //uint32_t offset = m_control.m_current;
  3303. //_commandBuffer.addScheduledHandler(setTimestamp, &m_result[offset].m_begin);
  3304. //_commandBuffer.addCompletedHandler(setTimestamp, &m_result[offset].m_end);
  3305. m_control.commit(1);
  3306. }
  3307. bool TimerQueryWgpu::get()
  3308. {
  3309. if (0 != m_control.available() )
  3310. {
  3311. uint32_t offset = m_control.m_read;
  3312. m_begin = m_result[offset].m_begin;
  3313. m_end = m_result[offset].m_end;
  3314. m_elapsed = m_end - m_begin;
  3315. m_control.consume(1);
  3316. return true;
  3317. }
  3318. return false;
  3319. }
  3320. void RendererContextWgpu::submitBlit(BlitState& _bs, uint16_t _view)
  3321. {
  3322. if (!_bs.hasItem(_view) )
  3323. {
  3324. return;
  3325. }
  3326. endEncoding();
  3327. wgpu::CommandEncoder& bce = getBlitCommandEncoder();
  3328. while (_bs.hasItem(_view) )
  3329. {
  3330. const BlitItem& blit = _bs.advance();
  3331. const TextureWgpu& src = m_textures[blit.m_src.idx];
  3332. const TextureWgpu& dst = m_textures[blit.m_dst.idx];
  3333. bool readBack = !!(dst.m_flags & BGFX_TEXTURE_READ_BACK);
  3334. wgpu::ImageCopyTexture srcView;
  3335. srcView.texture = src.m_ptr;
  3336. srcView.origin = { blit.m_srcX, blit.m_srcY, blit.m_srcZ };
  3337. srcView.mipLevel = blit.m_srcMip;
  3338. wgpu::ImageCopyTexture dstView;
  3339. dstView.texture = dst.m_ptr;
  3340. dstView.origin = { blit.m_dstX, blit.m_dstY, blit.m_dstZ };
  3341. dstView.mipLevel = blit.m_dstMip;
  3342. if (blit.m_depth == 0)
  3343. {
  3344. wgpu::Extent3D copyExtent = { blit.m_width, blit.m_height, 1 };
  3345. bce.CopyTextureToTexture(&srcView, &dstView, &copyExtent);
  3346. }
  3347. else
  3348. {
  3349. wgpu::Extent3D copyExtent = { blit.m_width, blit.m_height, blit.m_depth };
  3350. bce.CopyTextureToTexture(&srcView, &dstView, &copyExtent);
  3351. }
  3352. if (readBack)
  3353. {
  3354. //bce..synchronizeTexture(dst.m_ptr, 0, blit.m_dstMip);
  3355. }
  3356. }
  3357. //if (bce)
  3358. //{
  3359. // bce.endEncoding();
  3360. // bce = 0;
  3361. //}
  3362. }
  3363. void RendererContextWgpu::submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter)
  3364. {
  3365. if(_render->m_capture)
  3366. {
  3367. renderDocTriggerCapture();
  3368. }
  3369. m_cmd.finish(false);
  3370. if (!m_cmd.m_renderEncoder)
  3371. {
  3372. m_cmd.beginRender();
  3373. }
  3374. BGFX_WEBGPU_PROFILER_BEGIN_LITERAL("rendererSubmit", kColorFrame);
  3375. int64_t timeBegin = bx::getHPCounter();
  3376. int64_t captureElapsed = 0;
  3377. //m_gpuTimer.addHandlers(m_encoder);
  3378. updateResolution(_render->m_resolution);
  3379. m_frameIndex = 0; // (m_frameIndex + 1) % BGFX_CONFIG_MAX_FRAME_LATENCY;
  3380. ScratchBufferWgpu& scratchBuffer = m_scratchBuffers[m_frameIndex];
  3381. scratchBuffer.begin();
  3382. BindStateCacheWgpu& bindStates = m_bindStateCache[m_frameIndex];
  3383. bindStates.reset();
  3384. if (0 < _render->m_iboffset)
  3385. {
  3386. BGFX_PROFILER_SCOPE("bgfx/Update transient index buffer", kColorResource);
  3387. TransientIndexBuffer* ib = _render->m_transientIb;
  3388. m_indexBuffers[ib->handle.idx].update(0, bx::strideAlign(_render->m_iboffset,4), ib->data, true);
  3389. }
  3390. if (0 < _render->m_vboffset)
  3391. {
  3392. BGFX_PROFILER_SCOPE("bgfx/Update transient vertex buffer", kColorResource);
  3393. TransientVertexBuffer* vb = _render->m_transientVb;
  3394. m_vertexBuffers[vb->handle.idx].update(0, bx::strideAlign(_render->m_vboffset,4), vb->data, true);
  3395. }
  3396. _render->sort();
  3397. RenderDraw currentState;
  3398. currentState.clear();
  3399. currentState.m_stateFlags = BGFX_STATE_NONE;
  3400. currentState.m_stencil = packStencil(BGFX_STENCIL_NONE, BGFX_STENCIL_NONE);
  3401. RenderBind currentBind;
  3402. currentBind.clear();
  3403. static ViewState viewState;
  3404. viewState.reset(_render);
  3405. uint32_t blendFactor = 0;
  3406. //bool wireframe = !!(_render->m_debug&BGFX_DEBUG_WIREFRAME);
  3407. ProgramHandle currentProgram = BGFX_INVALID_HANDLE;
  3408. uint32_t currentBindHash = 0;
  3409. uint32_t currentBindLayoutHash = 0;
  3410. BindStateWgpu* previousBindState = NULL;
  3411. SortKey key;
  3412. uint16_t view = UINT16_MAX;
  3413. FrameBufferHandle fbh = { BGFX_CONFIG_MAX_FRAME_BUFFERS };
  3414. BlitState bs(_render);
  3415. const uint64_t primType = 0;
  3416. uint8_t primIndex = uint8_t(primType >> BGFX_STATE_PT_SHIFT);
  3417. PrimInfo prim = s_primInfo[primIndex];
  3418. const uint32_t maxComputeBindings = g_caps.limits.maxComputeBindings;
  3419. // TODO store this
  3420. static wgpu::RenderPassEncoder rce;
  3421. PipelineStateWgpu* currentPso = NULL;
  3422. bool wasCompute = false;
  3423. bool viewHasScissor = false;
  3424. Rect viewScissorRect;
  3425. viewScissorRect.clear();
  3426. uint32_t statsNumPrimsSubmitted[BX_COUNTOF(s_primInfo)] = {};
  3427. uint32_t statsNumPrimsRendered[BX_COUNTOF(s_primInfo)] = {};
  3428. uint32_t statsNumInstances[BX_COUNTOF(s_primInfo)] = {};
  3429. uint32_t statsNumDrawIndirect[BX_COUNTOF(s_primInfo)] = {};
  3430. uint32_t statsNumIndices = 0;
  3431. uint32_t statsKeyType[2] = {};
  3432. Profiler<TimerQueryWgpu> profiler(
  3433. _render
  3434. , m_gpuTimer
  3435. , s_viewName
  3436. );
  3437. if (0 == (_render->m_debug & BGFX_DEBUG_IFH))
  3438. {
  3439. viewState.m_rect = _render->m_view[0].m_rect;
  3440. int32_t numItems = _render->m_numRenderItems;
  3441. for (int32_t item = 0; item < numItems;)
  3442. {
  3443. const uint64_t encodedKey = _render->m_sortKeys[item];
  3444. const bool isCompute = key.decode(encodedKey, _render->m_viewRemap);
  3445. statsKeyType[isCompute]++;
  3446. const bool viewChanged = 0
  3447. || key.m_view != view
  3448. || item == numItems
  3449. ;
  3450. const uint32_t itemIdx = _render->m_sortValues[item];
  3451. const RenderItem& renderItem = _render->m_renderItem[itemIdx];
  3452. const RenderBind& renderBind = _render->m_renderItemBind[itemIdx];
  3453. ++item;
  3454. if (viewChanged
  3455. || (!isCompute && wasCompute))
  3456. {
  3457. view = key.m_view;
  3458. currentProgram = BGFX_INVALID_HANDLE;
  3459. if (item > 1)
  3460. {
  3461. profiler.end();
  3462. }
  3463. BGFX_WEBGPU_PROFILER_END();
  3464. setViewType(view, " ");
  3465. BGFX_WEBGPU_PROFILER_BEGIN(view, kColorView);
  3466. profiler.begin(view);
  3467. viewState.m_rect = _render->m_view[view].m_rect;
  3468. submitBlit(bs, view);
  3469. if (!isCompute)
  3470. {
  3471. const Rect& scissorRect = _render->m_view[view].m_scissor;
  3472. viewHasScissor = !scissorRect.isZero();
  3473. viewScissorRect = viewHasScissor ? scissorRect : viewState.m_rect;
  3474. Clear& clr = _render->m_view[view].m_clear;
  3475. Rect viewRect = viewState.m_rect;
  3476. bool clearWithRenderPass = false;
  3477. if (!m_renderEncoder
  3478. || fbh.idx != _render->m_view[view].m_fbh.idx)
  3479. {
  3480. endEncoding();
  3481. fbh = _render->m_view[view].m_fbh;
  3482. uint32_t width = m_resolution.width;
  3483. uint32_t height = m_resolution.height;
  3484. if (isValid(fbh))
  3485. {
  3486. FrameBufferWgpu& frameBuffer = m_frameBuffers[fbh.idx];
  3487. width = frameBuffer.m_width;
  3488. height = frameBuffer.m_height;
  3489. }
  3490. clearWithRenderPass = true
  3491. && 0 == viewRect.m_x
  3492. && 0 == viewRect.m_y
  3493. && width == viewRect.m_width
  3494. && height == viewRect.m_height
  3495. ;
  3496. rce = renderPass(_render, fbh, clearWithRenderPass, clr, s_viewName[view]);
  3497. }
  3498. else if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION))
  3499. {
  3500. rce.PopDebugGroup();
  3501. }
  3502. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION))
  3503. {
  3504. rce.PushDebugGroup(s_viewName[view]);
  3505. }
  3506. //rce.setTriangleFillMode(wireframe ? MTLTriangleFillModeLines : MTLTriangleFillModeFill);
  3507. const Rect& rect = viewState.m_rect;
  3508. rce.SetViewport(rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0.0f, 1.0f);
  3509. rce.SetScissorRect(rect.m_x, rect.m_y, rect.m_width, rect.m_height);
  3510. if (BGFX_CLEAR_NONE != (clr.m_flags & BGFX_CLEAR_MASK)
  3511. && !clearWithRenderPass)
  3512. {
  3513. clearQuad(_clearQuad, viewState.m_rect, clr, _render->m_colorPalette);
  3514. }
  3515. }
  3516. }
  3517. if (isCompute)
  3518. {
  3519. if (!wasCompute)
  3520. {
  3521. wasCompute = true;
  3522. endEncoding();
  3523. rce = NULL;
  3524. setViewType(view, "C");
  3525. BGFX_WEBGPU_PROFILER_END();
  3526. BGFX_WEBGPU_PROFILER_BEGIN(view, kColorCompute);
  3527. m_computeEncoder = m_cmd.m_renderEncoder.BeginComputePass();
  3528. }
  3529. else if (viewChanged)
  3530. {
  3531. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION))
  3532. {
  3533. m_computeEncoder.PopDebugGroup();
  3534. }
  3535. endEncoding();
  3536. m_computeEncoder = m_cmd.m_renderEncoder.BeginComputePass();
  3537. }
  3538. if (viewChanged)
  3539. {
  3540. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION))
  3541. {
  3542. s_viewName[view][3] = L'C';
  3543. m_computeEncoder.PushDebugGroup(s_viewName[view]);
  3544. s_viewName[view][3] = L' ';
  3545. }
  3546. }
  3547. const RenderCompute& compute = renderItem.compute;
  3548. bool programChanged = false;
  3549. bool constantsChanged = compute.m_uniformBegin < compute.m_uniformEnd;
  3550. rendererUpdateUniforms(this, _render->m_uniformBuffer[compute.m_uniformIdx], compute.m_uniformBegin, compute.m_uniformEnd);
  3551. if (key.m_program.idx != currentProgram.idx)
  3552. {
  3553. currentProgram = key.m_program;
  3554. currentPso = getComputePipelineState(currentProgram);
  3555. if (NULL == currentPso)
  3556. {
  3557. currentProgram = BGFX_INVALID_HANDLE;
  3558. continue;
  3559. }
  3560. m_computeEncoder.SetPipeline(currentPso->m_cps);
  3561. programChanged =
  3562. constantsChanged = true;
  3563. }
  3564. if (!isValid(currentProgram)
  3565. || NULL == currentPso)
  3566. BX_WARN(false, "Invalid program / No PSO");
  3567. const ProgramWgpu& program = m_program[currentProgram.idx];
  3568. if (constantsChanged)
  3569. {
  3570. UniformBuffer* vcb = program.m_vsh->m_constantBuffer;
  3571. if (NULL != vcb)
  3572. {
  3573. commit(*vcb);
  3574. }
  3575. }
  3576. viewState.setPredefined<4>(this, view, program, _render, compute);
  3577. uint32_t numOffset = 0;
  3578. uint32_t offsets[2] = { 0, 0 };
  3579. if (program.m_vsh->m_size > 0)
  3580. {
  3581. offsets[numOffset++] = scratchBuffer.write(m_vsScratch, program.m_vsh->m_gpuSize);
  3582. }
  3583. BindStateWgpu& bindState = allocAndFillBindState(program, bindStates, scratchBuffer, renderBind);
  3584. bindProgram(m_computeEncoder, program, bindState, numOffset, offsets);
  3585. if (isValid(compute.m_indirectBuffer))
  3586. {
  3587. const VertexBufferWgpu& vb = m_vertexBuffers[compute.m_indirectBuffer.idx];
  3588. uint32_t numDrawIndirect = UINT16_MAX == compute.m_numIndirect
  3589. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  3590. : compute.m_numIndirect
  3591. ;
  3592. uint32_t args = compute.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  3593. for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
  3594. {
  3595. m_computeEncoder.DispatchIndirect(
  3596. vb.m_ptr
  3597. , args
  3598. );
  3599. args += BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  3600. }
  3601. }
  3602. else
  3603. {
  3604. m_computeEncoder.Dispatch(compute.m_numX, compute.m_numY, compute.m_numZ);
  3605. }
  3606. continue;
  3607. }
  3608. bool resetState = viewChanged || wasCompute;
  3609. if (wasCompute)
  3610. {
  3611. wasCompute = false;
  3612. currentProgram = BGFX_INVALID_HANDLE;
  3613. setViewType(view, " ");
  3614. BGFX_WEBGPU_PROFILER_END();
  3615. BGFX_WEBGPU_PROFILER_BEGIN(view, kColorDraw);
  3616. }
  3617. const RenderDraw& draw = renderItem.draw;
  3618. // TODO (hugoam)
  3619. //const bool depthWrite = !!(BGFX_STATE_WRITE_Z & draw.m_stateFlags);
  3620. const uint64_t newFlags = draw.m_stateFlags;
  3621. uint64_t changedFlags = currentState.m_stateFlags ^ draw.m_stateFlags;
  3622. currentState.m_stateFlags = newFlags;
  3623. const uint64_t newStencil = draw.m_stencil;
  3624. uint64_t changedStencil = (currentState.m_stencil ^ draw.m_stencil) & BGFX_STENCIL_FUNC_REF_MASK;
  3625. currentState.m_stencil = newStencil;
  3626. if (resetState)
  3627. {
  3628. wasCompute = false;
  3629. currentState.clear();
  3630. currentState.m_scissor = !draw.m_scissor;
  3631. changedFlags = BGFX_STATE_MASK;
  3632. changedStencil = packStencil(BGFX_STENCIL_MASK, BGFX_STENCIL_MASK);
  3633. currentState.m_stateFlags = newFlags;
  3634. currentState.m_stencil = newStencil;
  3635. currentBind.clear();
  3636. currentProgram = BGFX_INVALID_HANDLE;
  3637. const uint64_t pt = newFlags & BGFX_STATE_PT_MASK;
  3638. primIndex = uint8_t(pt >> BGFX_STATE_PT_SHIFT);
  3639. }
  3640. if (prim.m_type != s_primInfo[primIndex].m_type)
  3641. {
  3642. prim = s_primInfo[primIndex];
  3643. }
  3644. uint16_t scissor = draw.m_scissor;
  3645. if (currentState.m_scissor != scissor)
  3646. {
  3647. currentState.m_scissor = scissor;
  3648. if (UINT16_MAX == scissor)
  3649. {
  3650. if (viewHasScissor)
  3651. {
  3652. const auto& r = viewScissorRect;
  3653. rce.SetScissorRect(r.m_x, r.m_y, r.m_width, r.m_height);
  3654. }
  3655. else
  3656. { // can't disable: set to view rect
  3657. const auto& r = viewState.m_rect;
  3658. rce.SetScissorRect(r.m_x, r.m_y, r.m_width, r.m_height);
  3659. }
  3660. }
  3661. else
  3662. {
  3663. Rect scissorRect;
  3664. scissorRect.setIntersect(viewScissorRect, _render->m_frameCache.m_rectCache.m_cache[scissor]);
  3665. const auto& r = scissorRect;
  3666. if (r.m_width == 0 || r.m_height == 0)
  3667. {
  3668. continue;
  3669. }
  3670. rce.SetScissorRect(r.m_x, r.m_y, r.m_width, r.m_height);
  3671. }
  3672. }
  3673. if (0 != changedStencil)
  3674. {
  3675. const uint32_t fstencil = unpackStencil(0, draw.m_stencil);
  3676. const uint32_t ref = (fstencil & BGFX_STENCIL_FUNC_REF_MASK) >> BGFX_STENCIL_FUNC_REF_SHIFT;
  3677. rce.SetStencilReference(ref);
  3678. }
  3679. if ((0 | BGFX_STATE_PT_MASK) & changedFlags)
  3680. {
  3681. const uint64_t pt = newFlags & BGFX_STATE_PT_MASK;
  3682. primIndex = uint8_t(pt >> BGFX_STATE_PT_SHIFT);
  3683. if (prim.m_type != s_primInfo[primIndex].m_type)
  3684. {
  3685. prim = s_primInfo[primIndex];
  3686. }
  3687. }
  3688. if (blendFactor != draw.m_rgba
  3689. && !(newFlags & BGFX_STATE_BLEND_INDEPENDENT))
  3690. {
  3691. const uint32_t rgba = draw.m_rgba;
  3692. float rr = ((rgba >> 24)) / 255.0f;
  3693. float gg = ((rgba >> 16) & 0xff) / 255.0f;
  3694. float bb = ((rgba >> 8) & 0xff) / 255.0f;
  3695. float aa = ((rgba) & 0xff) / 255.0f;
  3696. wgpu::Color color = { rr, gg, bb, aa };
  3697. rce.SetBlendColor(&color);
  3698. blendFactor = draw.m_rgba;
  3699. }
  3700. bool programChanged = false;
  3701. bool constantsChanged = draw.m_uniformBegin < draw.m_uniformEnd;
  3702. rendererUpdateUniforms(this, _render->m_uniformBuffer[draw.m_uniformIdx], draw.m_uniformBegin, draw.m_uniformEnd);
  3703. bool vertexStreamChanged = hasVertexStreamChanged(currentState, draw);
  3704. if (key.m_program.idx != currentProgram.idx
  3705. || vertexStreamChanged
  3706. || (0
  3707. | BGFX_STATE_BLEND_MASK
  3708. | BGFX_STATE_BLEND_EQUATION_MASK
  3709. | BGFX_STATE_WRITE_RGB
  3710. | BGFX_STATE_WRITE_A
  3711. | BGFX_STATE_BLEND_INDEPENDENT
  3712. | BGFX_STATE_MSAA
  3713. | BGFX_STATE_BLEND_ALPHA_TO_COVERAGE
  3714. ) & changedFlags
  3715. || ((blendFactor != draw.m_rgba) && !!(newFlags & BGFX_STATE_BLEND_INDEPENDENT)))
  3716. {
  3717. currentProgram = key.m_program;
  3718. currentState.m_streamMask = draw.m_streamMask;
  3719. currentState.m_instanceDataBuffer.idx = draw.m_instanceDataBuffer.idx;
  3720. currentState.m_instanceDataOffset = draw.m_instanceDataOffset;
  3721. currentState.m_instanceDataStride = draw.m_instanceDataStride;
  3722. const VertexLayout* decls[BGFX_CONFIG_MAX_VERTEX_STREAMS];
  3723. uint32_t numVertices = draw.m_numVertices;
  3724. uint8_t numStreams = 0;
  3725. for (uint32_t idx = 0, streamMask = draw.m_streamMask
  3726. ; 0 != streamMask
  3727. ; streamMask >>= 1, idx += 1, ++numStreams
  3728. )
  3729. {
  3730. const uint32_t ntz = bx::uint32_cnttz(streamMask);
  3731. streamMask >>= ntz;
  3732. idx += ntz;
  3733. currentState.m_stream[idx].m_layoutHandle = draw.m_stream[idx].m_layoutHandle;
  3734. currentState.m_stream[idx].m_handle = draw.m_stream[idx].m_handle;
  3735. currentState.m_stream[idx].m_startVertex = draw.m_stream[idx].m_startVertex;
  3736. const uint16_t handle = draw.m_stream[idx].m_handle.idx;
  3737. const VertexBufferWgpu& vb = m_vertexBuffers[handle];
  3738. const uint16_t decl = isValid(draw.m_stream[idx].m_layoutHandle)
  3739. ? draw.m_stream[idx].m_layoutHandle.idx
  3740. : vb.m_layoutHandle.idx;
  3741. const VertexLayout& vertexDecl = m_vertexDecls[decl];
  3742. const uint32_t stride = vertexDecl.m_stride;
  3743. decls[numStreams] = &vertexDecl;
  3744. numVertices = bx::uint32_min(UINT32_MAX == draw.m_numVertices
  3745. ? vb.m_size / stride
  3746. : draw.m_numVertices
  3747. , numVertices
  3748. );
  3749. const uint32_t offset = draw.m_stream[idx].m_startVertex * stride;
  3750. rce.SetVertexBuffer(idx, vb.m_ptr, offset);
  3751. }
  3752. if (!isValid(currentProgram))
  3753. {
  3754. continue;
  3755. }
  3756. else
  3757. {
  3758. currentPso = NULL;
  3759. if (0 < numStreams)
  3760. {
  3761. currentPso = getPipelineState(
  3762. newFlags
  3763. , newStencil
  3764. , draw.m_rgba
  3765. , fbh
  3766. , numStreams
  3767. , decls
  3768. , draw.isIndex16()
  3769. , currentProgram
  3770. , uint8_t(draw.m_instanceDataStride / 16)
  3771. );
  3772. }
  3773. if (NULL == currentPso)
  3774. {
  3775. currentProgram = BGFX_INVALID_HANDLE;
  3776. continue;
  3777. }
  3778. rce.SetPipeline(currentPso->m_rps);
  3779. }
  3780. if (isValid(draw.m_instanceDataBuffer))
  3781. {
  3782. const VertexBufferWgpu& inst = m_vertexBuffers[draw.m_instanceDataBuffer.idx];
  3783. rce.SetVertexBuffer(numStreams/*+1*/, inst.m_ptr, draw.m_instanceDataOffset);
  3784. }
  3785. programChanged =
  3786. constantsChanged = true;
  3787. }
  3788. if (isValid(currentProgram))
  3789. {
  3790. const ProgramWgpu& program = m_program[currentProgram.idx];
  3791. if (constantsChanged)
  3792. {
  3793. UniformBuffer* vcb = program.m_vsh->m_constantBuffer;
  3794. if (NULL != vcb)
  3795. {
  3796. commit(*vcb);
  3797. }
  3798. }
  3799. if (constantsChanged)
  3800. {
  3801. UniformBuffer* fcb = program.m_fsh->m_constantBuffer;
  3802. if (NULL != fcb)
  3803. {
  3804. commit(*fcb);
  3805. }
  3806. }
  3807. viewState.setPredefined<4>(this, view, program, _render, draw);
  3808. bool hasPredefined = 0 < program.m_numPredefined;
  3809. uint32_t numOffset = 0;
  3810. uint32_t offsets[2] = { 0, 0 };
  3811. if (constantsChanged
  3812. || hasPredefined)
  3813. {
  3814. //viewState.setPredefined<4>(this, view, program, _render, draw, programChanged || viewChanged);
  3815. const uint32_t vsize = program.m_vsh->m_gpuSize;
  3816. const uint32_t fsize = (NULL != program.m_fsh ? program.m_fsh->m_gpuSize : 0);
  3817. if (program.m_vsh->m_size > 0)
  3818. {
  3819. offsets[numOffset++] = scratchBuffer.write(m_vsScratch, vsize);
  3820. }
  3821. if (fsize > 0)
  3822. {
  3823. offsets[numOffset++] = scratchBuffer.write(m_fsScratch, fsize);
  3824. }
  3825. }
  3826. uint32_t bindHash = bx::hash<bx::HashMurmur2A>(renderBind.m_bind, sizeof(renderBind.m_bind));
  3827. if (currentBindHash != bindHash
  3828. || currentBindLayoutHash != program.m_bindGroupLayoutHash)
  3829. {
  3830. currentBindHash = bindHash;
  3831. currentBindLayoutHash = program.m_bindGroupLayoutHash;
  3832. previousBindState = &bindStates.m_bindStates[bindStates.m_currentBindState];
  3833. allocAndFillBindState(program, bindStates, scratchBuffer, renderBind);
  3834. }
  3835. BindStateWgpu& bindState = bindStates.m_bindStates[bindStates.m_currentBindState-1];
  3836. bindProgram(rce, program, bindState, numOffset, offsets);
  3837. }
  3838. if (0 != currentState.m_streamMask)
  3839. {
  3840. uint32_t numVertices = draw.m_numVertices;
  3841. if (UINT32_MAX == numVertices)
  3842. {
  3843. const VertexBufferWgpu& vb = m_vertexBuffers[currentState.m_stream[0].m_handle.idx];
  3844. uint16_t decl = !isValid(vb.m_layoutHandle) ? draw.m_stream[0].m_layoutHandle.idx : vb.m_layoutHandle.idx;
  3845. const VertexLayout& vertexDecl = m_vertexDecls[decl];
  3846. numVertices = vb.m_size/vertexDecl.m_stride;
  3847. }
  3848. uint32_t numIndices = 0;
  3849. uint32_t numPrimsSubmitted = 0;
  3850. uint32_t numInstances = 0;
  3851. uint32_t numPrimsRendered = 0;
  3852. uint32_t numDrawIndirect = 0;
  3853. if (isValid(draw.m_indirectBuffer) )
  3854. {
  3855. const VertexBufferWgpu& vb = m_vertexBuffers[draw.m_indirectBuffer.idx];
  3856. if (isValid(draw.m_indexBuffer) )
  3857. {
  3858. const IndexBufferWgpu& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  3859. numDrawIndirect = UINT16_MAX == draw.m_numIndirect
  3860. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  3861. : draw.m_numIndirect
  3862. ;
  3863. for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
  3864. {
  3865. rce.SetIndexBuffer(ib.m_ptr, ib.m_format, 0);
  3866. rce.DrawIndexedIndirect(vb.m_ptr, (draw.m_startIndirect + ii)* BGFX_CONFIG_DRAW_INDIRECT_STRIDE);
  3867. }
  3868. }
  3869. else
  3870. {
  3871. numDrawIndirect = UINT16_MAX == draw.m_numIndirect
  3872. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  3873. : draw.m_numIndirect
  3874. ;
  3875. for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
  3876. {
  3877. rce.DrawIndirect(vb.m_ptr, (draw.m_startIndirect + ii)* BGFX_CONFIG_DRAW_INDIRECT_STRIDE);
  3878. }
  3879. }
  3880. }
  3881. else
  3882. {
  3883. if (isValid(draw.m_indexBuffer) )
  3884. {
  3885. const IndexBufferWgpu& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  3886. const uint32_t indexSize = draw.isIndex16() ? 2 : 4;
  3887. if (UINT32_MAX == draw.m_numIndices)
  3888. {
  3889. numIndices = ib.m_size/indexSize;
  3890. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  3891. numInstances = draw.m_numInstances;
  3892. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  3893. rce.SetIndexBuffer(ib.m_ptr, ib.m_format, 0);
  3894. rce.DrawIndexed(numIndices, draw.m_numInstances, 0, 0, 0);
  3895. }
  3896. else if (prim.m_min <= draw.m_numIndices)
  3897. {
  3898. numIndices = draw.m_numIndices;
  3899. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  3900. numInstances = draw.m_numInstances;
  3901. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  3902. rce.SetIndexBuffer(ib.m_ptr, ib.m_format, 0);
  3903. rce.DrawIndexed(numIndices, numInstances, draw.m_startIndex, 0, 0);
  3904. }
  3905. }
  3906. else
  3907. {
  3908. numPrimsSubmitted = numVertices/prim.m_div - prim.m_sub;
  3909. numInstances = draw.m_numInstances;
  3910. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  3911. rce.Draw(numVertices, draw.m_numInstances, 0, 0);
  3912. }
  3913. }
  3914. statsNumPrimsSubmitted[primIndex] += numPrimsSubmitted;
  3915. statsNumPrimsRendered[primIndex] += numPrimsRendered;
  3916. statsNumInstances[primIndex] += numInstances;
  3917. statsNumDrawIndirect[primIndex] += numDrawIndirect;
  3918. statsNumIndices += numIndices;
  3919. }
  3920. }
  3921. if (wasCompute)
  3922. {
  3923. invalidateCompute();
  3924. setViewType(view, "C");
  3925. BGFX_WEBGPU_PROFILER_END();
  3926. BGFX_WEBGPU_PROFILER_BEGIN(view, kColorCompute);
  3927. }
  3928. submitBlit(bs, BGFX_CONFIG_MAX_VIEWS);
  3929. if (0 < _render->m_numRenderItems)
  3930. {
  3931. captureElapsed = -bx::getHPCounter();
  3932. capture();
  3933. rce = m_renderEncoder;
  3934. captureElapsed += bx::getHPCounter();
  3935. profiler.end();
  3936. }
  3937. }
  3938. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION) )
  3939. {
  3940. if (0 < _render->m_numRenderItems)
  3941. {
  3942. rce.PopDebugGroup();
  3943. }
  3944. }
  3945. BGFX_WEBGPU_PROFILER_END();
  3946. int64_t timeEnd = bx::getHPCounter();
  3947. int64_t frameTime = timeEnd - timeBegin;
  3948. static int64_t min = frameTime;
  3949. static int64_t max = frameTime;
  3950. min = bx::min<int64_t>(min, frameTime);
  3951. max = bx::max<int64_t>(max, frameTime);
  3952. static uint32_t maxGpuLatency = 0;
  3953. static double maxGpuElapsed = 0.0f;
  3954. double elapsedGpuMs = 0.0;
  3955. do
  3956. {
  3957. double toGpuMs = 1000.0 / double(m_gpuTimer.m_frequency);
  3958. elapsedGpuMs = m_gpuTimer.m_elapsed * toGpuMs;
  3959. maxGpuElapsed = elapsedGpuMs > maxGpuElapsed ? elapsedGpuMs : maxGpuElapsed;
  3960. }
  3961. while (m_gpuTimer.get() );
  3962. maxGpuLatency = bx::uint32_imax(maxGpuLatency, m_gpuTimer.m_control.available()-1);
  3963. const int64_t timerFreq = bx::getHPFrequency();
  3964. Stats& perfStats = _render->m_perfStats;
  3965. perfStats.cpuTimeBegin = timeBegin;
  3966. perfStats.cpuTimeEnd = timeEnd;
  3967. perfStats.cpuTimerFreq = timerFreq;
  3968. perfStats.gpuTimeBegin = m_gpuTimer.m_begin;
  3969. perfStats.gpuTimeEnd = m_gpuTimer.m_end;
  3970. perfStats.gpuTimerFreq = m_gpuTimer.m_frequency;
  3971. perfStats.numDraw = statsKeyType[0];
  3972. perfStats.numCompute = statsKeyType[1];
  3973. perfStats.numBlit = _render->m_numBlitItems;
  3974. perfStats.maxGpuLatency = maxGpuLatency;
  3975. perfStats.gpuFrameNum = result.m_frameNum;
  3976. bx::memCopy(perfStats.numPrims, statsNumPrimsRendered, sizeof(perfStats.numPrims) );
  3977. perfStats.gpuMemoryMax = -INT64_MAX;
  3978. perfStats.gpuMemoryUsed = -INT64_MAX;
  3979. //rce.setTriangleFillMode(MTLTriangleFillModeFill);
  3980. if (_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) )
  3981. {
  3982. rce = renderPass(_render, BGFX_INVALID_HANDLE, false, Clear());
  3983. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION))
  3984. {
  3985. rce.PushDebugGroup("debugstats");
  3986. }
  3987. TextVideoMem& tvm = m_textVideoMem;
  3988. static int64_t next = timeEnd;
  3989. if (timeEnd >= next)
  3990. {
  3991. next = timeEnd + timerFreq;
  3992. double freq = double(timerFreq);
  3993. double toMs = 1000.0/freq;
  3994. tvm.clear();
  3995. uint16_t pos = 0;
  3996. tvm.printf(0, pos++, BGFX_CONFIG_DEBUG ? 0x8c : 0x8f
  3997. , " %s / " BX_COMPILER_NAME
  3998. " / " BX_CPU_NAME
  3999. " / " BX_ARCH_NAME
  4000. " / " BX_PLATFORM_NAME
  4001. " / Version 1.%d.%d (commit: " BGFX_REV_SHA1 ")"
  4002. , getRendererName()
  4003. , BGFX_API_VERSION
  4004. , BGFX_REV_NUMBER
  4005. );
  4006. pos = 10;
  4007. tvm.printf(10, pos++, 0x8b, " Frame: %7.3f, % 7.3f \x1f, % 7.3f \x1e [ms] / % 6.2f FPS "
  4008. , double(frameTime)*toMs
  4009. , double(min)*toMs
  4010. , double(max)*toMs
  4011. , freq/frameTime
  4012. );
  4013. const uint32_t msaa = (m_resolution.reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  4014. tvm.printf(10, pos++, 0x8b, " Reset flags: [%c] vsync, [%c] MSAAx%d, [%c] MaxAnisotropy "
  4015. , !!(m_resolution.reset&BGFX_RESET_VSYNC) ? '\xfe' : ' '
  4016. , 0 != msaa ? '\xfe' : ' '
  4017. , 1<<msaa
  4018. , !!(m_resolution.reset&BGFX_RESET_MAXANISOTROPY) ? '\xfe' : ' '
  4019. );
  4020. double elapsedCpuMs = double(frameTime)*toMs;
  4021. tvm.printf(10, pos++, 0x8b, " Submitted: %4d (draw %4d, compute %4d) / CPU %3.4f [ms] %c GPU %3.4f [ms] (latency %d)"
  4022. , _render->m_numRenderItems
  4023. , statsKeyType[0]
  4024. , statsKeyType[1]
  4025. , elapsedCpuMs
  4026. , elapsedCpuMs > maxGpuElapsed ? '>' : '<'
  4027. , maxGpuElapsed
  4028. , maxGpuLatency
  4029. );
  4030. maxGpuLatency = 0;
  4031. maxGpuElapsed = 0.0;
  4032. for (uint32_t ii = 0; ii < Topology::Count; ++ii)
  4033. {
  4034. tvm.printf(10, pos++, 0x8b, " %10s: %7d (#inst: %5d), submitted: %7d"
  4035. , getName(Topology::Enum(ii) )
  4036. , statsNumPrimsRendered[ii]
  4037. , statsNumInstances[ii]
  4038. , statsNumPrimsSubmitted[ii]
  4039. );
  4040. }
  4041. tvm.printf(10, pos++, 0x8b, " Indices: %7d ", statsNumIndices);
  4042. // tvm.printf(10, pos++, 0x8b, " Uniform size: %7d, Max: %7d ", _render->m_uniformEnd, _render->m_uniformMax);
  4043. tvm.printf(10, pos++, 0x8b, " DVB size: %7d ", _render->m_vboffset);
  4044. tvm.printf(10, pos++, 0x8b, " DIB size: %7d ", _render->m_iboffset);
  4045. pos++;
  4046. double captureMs = double(captureElapsed)*toMs;
  4047. tvm.printf(10, pos++, 0x8b, " Capture: %3.4f [ms]", captureMs);
  4048. uint8_t attr[2] = { 0x8c, 0x8a };
  4049. uint8_t attrIndex = _render->m_waitSubmit < _render->m_waitRender;
  4050. tvm.printf(10, pos++, attr[attrIndex &1], " Submit wait: %3.4f [ms]", _render->m_waitSubmit*toMs);
  4051. tvm.printf(10, pos++, attr[(attrIndex+1)&1], " Render wait: %3.4f [ms]", _render->m_waitRender*toMs);
  4052. min = frameTime;
  4053. max = frameTime;
  4054. }
  4055. blit(this, _textVideoMemBlitter, tvm);
  4056. rce = m_renderEncoder;
  4057. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION))
  4058. {
  4059. rce.PopDebugGroup();
  4060. }
  4061. }
  4062. else if (_render->m_debug & BGFX_DEBUG_TEXT)
  4063. {
  4064. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION))
  4065. {
  4066. rce.PushDebugGroup("debugtext");
  4067. }
  4068. blit(this, _textVideoMemBlitter, _render->m_textVideoMem);
  4069. rce = m_renderEncoder;
  4070. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION))
  4071. {
  4072. rce.PopDebugGroup();
  4073. }
  4074. }
  4075. endEncoding();
  4076. scratchBuffer.submit();
  4077. m_cmd.kick(true);
  4078. scratchBuffer.release();
  4079. #if !BX_PLATFORM_EMSCRIPTEN
  4080. for (uint32_t ii = 0, num = m_numWindows; ii < num; ++ii)
  4081. {
  4082. FrameBufferWgpu& frameBuffer = ii == 0 ? m_mainFrameBuffer : m_frameBuffers[m_windows[ii].idx];
  4083. if (NULL != frameBuffer.m_swapChain
  4084. && frameBuffer.m_swapChain->m_drawable)
  4085. {
  4086. SwapChainWgpu& swapChain = *frameBuffer.m_swapChain;
  4087. swapChain.m_swapChain.Present();
  4088. }
  4089. }
  4090. #endif
  4091. }
  4092. } /* namespace webgpu */ } // namespace bgfx
  4093. #else
  4094. namespace bgfx { namespace webgpu
  4095. {
  4096. RendererContextI* rendererCreate(const Init& _init)
  4097. {
  4098. BX_UNUSED(_init);
  4099. return NULL;
  4100. }
  4101. void rendererDestroy()
  4102. {
  4103. }
  4104. } /* namespace webgpu */ } // namespace bgfx
  4105. #endif // BGFX_CONFIG_RENDERER_WEBGPU