renderer_mtl.mm 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230
  1. /*
  2. * Copyright 2011-2016 Attila Kocsis. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "bgfx_p.h"
  6. #if BGFX_CONFIG_RENDERER_METAL
  7. #include "renderer_mtl.h"
  8. #include "renderer.h"
  9. #if BX_PLATFORM_OSX
  10. # include <Cocoa/Cocoa.h>
  11. #endif
  12. #import <Foundation/Foundation.h>
  13. #define UNIFORM_BUFFER_SIZE (8*1024*1024)
  14. /*
  15. // known metal shader generation issues:
  16. 03-raymarch: OSX10.11.3 nothing is visible ( depth/color swap in fragment output struct fixed this )
  17. 24-nbody: no generated compute shaders for metal
  18. Known issues(driver problems??):
  19. OSX mac mini(late 2014), OSX10.11.3 : nanovg-rendering: color writemask off causes problem...
  20. 03-raymarch: OSX nothing is visible ( depth/color order should be swapped in fragment output struct)
  21. works fine with newer OSX
  22. iPad mini 2, iOS 8.1.1: 21-deferred: scissor not working properly
  23. 26-occlusion: query doesn't work with two rendercommandencoders
  24. Only on this device ( no problem on iPad Air 2 with iOS9.3.1)
  25. TODOs:
  26. - support multiple vertex buffers: 34-mvs
  27. - framebufferMtl and TextureMtl resolve
  28. - FrameBufferMtl::postReset recreate framebuffer???
  29. renderpass load/resolve
  30. - capture with msaa: 07-callback
  31. - implement fb discard. problematic with multiple views that has same fb...
  32. - msaa color/depth/stencil is not saved. could have problem when we switch back to msaa framebuffer
  33. - refactor store/load actions to support msaa/discard/capture/readback etc...
  34. - finish savescreenshot with screenshotbegin/end
  35. - support multiple windows: 22-windows
  36. - multithreading with multiple commandbuffer
  37. - compute and drawindirect: 24-nbody (needs compute shaders)
  38. INFO:
  39. - 15-shadowmaps-simple (example needs modification mtxCrop znew = z * 0.5 + 0.5 is not needed ) could be hacked in shader too
  40. */
  41. namespace bgfx { namespace mtl
  42. {
  43. static char s_viewName[BGFX_CONFIG_MAX_VIEWS][BGFX_CONFIG_MAX_VIEW_NAME];
  44. struct PrimInfo
  45. {
  46. MTLPrimitiveType m_type;
  47. uint32_t m_min;
  48. uint32_t m_div;
  49. uint32_t m_sub;
  50. };
  51. static const PrimInfo s_primInfo[] =
  52. {
  53. { MTLPrimitiveTypeTriangle, 3, 3, 0 },
  54. { MTLPrimitiveTypeTriangleStrip, 3, 1, 2 },
  55. { MTLPrimitiveTypeLine, 2, 2, 0 },
  56. { MTLPrimitiveTypeLineStrip, 2, 1, 1 },
  57. { MTLPrimitiveTypePoint, 1, 1, 0 },
  58. };
  59. BX_STATIC_ASSERT(Topology::Count == BX_COUNTOF(s_primInfo) );
  60. static const char* s_attribName[] =
  61. {
  62. "a_position",
  63. "a_normal",
  64. "a_tangent",
  65. "a_bitangent",
  66. "a_color0",
  67. "a_color1",
  68. "a_color2",
  69. "a_color3",
  70. "a_indices",
  71. "a_weight",
  72. "a_texcoord0",
  73. "a_texcoord1",
  74. "a_texcoord2",
  75. "a_texcoord3",
  76. "a_texcoord4",
  77. "a_texcoord5",
  78. "a_texcoord6",
  79. "a_texcoord7",
  80. };
  81. BX_STATIC_ASSERT(Attrib::Count == BX_COUNTOF(s_attribName) );
  82. static const char* s_instanceDataName[] =
  83. {
  84. "i_data0",
  85. "i_data1",
  86. "i_data2",
  87. "i_data3",
  88. "i_data4",
  89. };
  90. BX_STATIC_ASSERT(BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT == BX_COUNTOF(s_instanceDataName) );
  91. static const MTLVertexFormat s_attribType[][4][2] = //type, count, normalized
  92. {
  93. // Uint8
  94. {
  95. { MTLVertexFormatUChar2, MTLVertexFormatUChar2Normalized },
  96. { MTLVertexFormatUChar2, MTLVertexFormatUChar2Normalized },
  97. { MTLVertexFormatUChar3, MTLVertexFormatUChar3Normalized },
  98. { MTLVertexFormatUChar4, MTLVertexFormatUChar4Normalized },
  99. },
  100. //Uint10
  101. //Note: unnormalized is handled as normalized now
  102. {
  103. { MTLVertexFormatUInt1010102Normalized, MTLVertexFormatUInt1010102Normalized },
  104. { MTLVertexFormatUInt1010102Normalized, MTLVertexFormatUInt1010102Normalized },
  105. { MTLVertexFormatUInt1010102Normalized, MTLVertexFormatUInt1010102Normalized },
  106. { MTLVertexFormatUInt1010102Normalized, MTLVertexFormatUInt1010102Normalized },
  107. },
  108. //Int16
  109. {
  110. { MTLVertexFormatShort2, MTLVertexFormatShort2Normalized },
  111. { MTLVertexFormatShort2, MTLVertexFormatShort2Normalized },
  112. { MTLVertexFormatShort3, MTLVertexFormatShort3Normalized },
  113. { MTLVertexFormatShort4, MTLVertexFormatShort4Normalized },
  114. },
  115. //Half
  116. {
  117. { MTLVertexFormatHalf2, MTLVertexFormatHalf2 },
  118. { MTLVertexFormatHalf2, MTLVertexFormatHalf2 },
  119. { MTLVertexFormatHalf3, MTLVertexFormatHalf3 },
  120. { MTLVertexFormatHalf4, MTLVertexFormatHalf4 },
  121. },
  122. //Float
  123. {
  124. { MTLVertexFormatFloat, MTLVertexFormatFloat },
  125. { MTLVertexFormatFloat2, MTLVertexFormatFloat2 },
  126. { MTLVertexFormatFloat3, MTLVertexFormatFloat3 },
  127. { MTLVertexFormatFloat4, MTLVertexFormatFloat4 },
  128. },
  129. };
  130. BX_STATIC_ASSERT(AttribType::Count == BX_COUNTOF(s_attribType) );
  131. static const MTLCullMode s_cullMode[] =
  132. {
  133. MTLCullModeNone,
  134. MTLCullModeFront,
  135. MTLCullModeBack,
  136. MTLCullModeNone
  137. };
  138. static const MTLBlendFactor s_blendFactor[][2] =
  139. {
  140. { (MTLBlendFactor)0, (MTLBlendFactor)0 }, // ignored
  141. { MTLBlendFactorZero, MTLBlendFactorZero }, // ZERO
  142. { MTLBlendFactorOne, MTLBlendFactorOne }, // ONE
  143. { MTLBlendFactorSourceColor, MTLBlendFactorSourceAlpha }, // SRC_COLOR
  144. { MTLBlendFactorOneMinusSourceColor, MTLBlendFactorOneMinusSourceAlpha }, // INV_SRC_COLOR
  145. { MTLBlendFactorSourceAlpha, MTLBlendFactorSourceAlpha }, // SRC_ALPHA
  146. { MTLBlendFactorOneMinusSourceAlpha, MTLBlendFactorOneMinusSourceAlpha }, // INV_SRC_ALPHA
  147. { MTLBlendFactorDestinationAlpha, MTLBlendFactorDestinationAlpha }, // DST_ALPHA
  148. { MTLBlendFactorOneMinusDestinationAlpha, MTLBlendFactorOneMinusDestinationAlpha }, // INV_DST_ALPHA
  149. { MTLBlendFactorDestinationColor, MTLBlendFactorDestinationAlpha }, // DST_COLOR
  150. { MTLBlendFactorOneMinusDestinationColor, MTLBlendFactorOneMinusDestinationAlpha }, // INV_DST_COLOR
  151. { MTLBlendFactorSourceAlphaSaturated, MTLBlendFactorOne }, // SRC_ALPHA_SAT
  152. { MTLBlendFactorBlendColor, MTLBlendFactorBlendColor }, // FACTOR
  153. { MTLBlendFactorOneMinusBlendColor, MTLBlendFactorOneMinusBlendColor }, // INV_FACTOR
  154. };
  155. static const MTLBlendOperation s_blendEquation[] =
  156. {
  157. MTLBlendOperationAdd,
  158. MTLBlendOperationSubtract,
  159. MTLBlendOperationReverseSubtract,
  160. MTLBlendOperationMin,
  161. MTLBlendOperationMax,
  162. };
  163. static const MTLCompareFunction s_cmpFunc[] =
  164. {
  165. MTLCompareFunctionAlways,
  166. MTLCompareFunctionLess,
  167. MTLCompareFunctionLessEqual,
  168. MTLCompareFunctionEqual,
  169. MTLCompareFunctionGreaterEqual,
  170. MTLCompareFunctionGreater,
  171. MTLCompareFunctionNotEqual,
  172. MTLCompareFunctionNever,
  173. MTLCompareFunctionAlways,
  174. };
  175. static const MTLStencilOperation s_stencilOp[] =
  176. {
  177. MTLStencilOperationZero,
  178. MTLStencilOperationKeep,
  179. MTLStencilOperationReplace,
  180. MTLStencilOperationIncrementWrap,
  181. MTLStencilOperationIncrementClamp,
  182. MTLStencilOperationDecrementWrap,
  183. MTLStencilOperationDecrementClamp,
  184. MTLStencilOperationInvert,
  185. };
  186. static const MTLSamplerAddressMode s_textureAddress[] =
  187. {
  188. MTLSamplerAddressModeRepeat,
  189. MTLSamplerAddressModeMirrorRepeat,
  190. MTLSamplerAddressModeClampToEdge,
  191. MTLSamplerAddressModeClampToZero,
  192. };
  193. static const MTLSamplerMinMagFilter s_textureFilterMinMag[] =
  194. {
  195. MTLSamplerMinMagFilterLinear,
  196. MTLSamplerMinMagFilterNearest,
  197. MTLSamplerMinMagFilterLinear,
  198. };
  199. static const MTLSamplerMipFilter s_textureFilterMip[] =
  200. {
  201. MTLSamplerMipFilterLinear,
  202. MTLSamplerMipFilterNearest,
  203. };
  204. struct TextureFormatInfo
  205. {
  206. MTLPixelFormat m_fmt;
  207. MTLPixelFormat m_fmtSrgb;
  208. };
  209. static TextureFormatInfo s_textureFormat[] =
  210. {
  211. { MTLPixelFormat(130/*BC1_RGBA*/), MTLPixelFormat(131/*BC1_RGBA_sRGB*/) }, // BC1
  212. { MTLPixelFormat(132/*BC2_RGBA*/), MTLPixelFormat(133/*BC2_RGBA_sRGB*/) }, // BC2
  213. { MTLPixelFormat(134/*BC3_RGBA*/), MTLPixelFormat(135/*BC3_RGBA_sRGB*/) }, // BC3
  214. { MTLPixelFormat(140/*BC4_RUnorm*/), MTLPixelFormatInvalid }, // BC4
  215. { MTLPixelFormat(142/*BC5_RGUnorm*/), MTLPixelFormatInvalid }, // BC5
  216. { MTLPixelFormat(150/*BC6H_RGBFloat*/), MTLPixelFormatInvalid }, // BC6H
  217. { MTLPixelFormat(152/*BC7_RGBAUnorm*/), MTLPixelFormat(153/*BC7_RGBAUnorm_sRGB*/) }, // BC7
  218. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ETC1
  219. { MTLPixelFormat(180/*ETC2_RGB8*/), MTLPixelFormat(181/*ETC2_RGB8_sRGB*/) }, // ETC2
  220. { MTLPixelFormat(178/*EAC_RGBA8*/), MTLPixelFormat(179/*EAC_RGBA8_sRGB*/) }, // ETC2A
  221. { MTLPixelFormat(182/*ETC2_RGB8A1*/), MTLPixelFormat(183/*ETC2_RGB8A1_sRGB*/) }, // ETC2A1
  222. { MTLPixelFormat(160/*PVRTC_RGB_2BPP*/), MTLPixelFormat(161/*PVRTC_RGB_2BPP_sRGB*/) }, // PTC12
  223. { MTLPixelFormat(162/*PVRTC_RGB_4BPP*/), MTLPixelFormat(163/*PVRTC_RGB_4BPP_sRGB*/) }, // PTC14
  224. { MTLPixelFormat(164/*PVRTC_RGBA_2BPP*/), MTLPixelFormat(165/*PVRTC_RGBA_2BPP_sRGB*/) }, // PTC12A
  225. { MTLPixelFormat(166/*PVRTC_RGBA_4BPP*/), MTLPixelFormat(167/*PVRTC_RGBA_4BPP_sRGB*/) }, // PTC14A
  226. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // PTC22
  227. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // PTC24
  228. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATC
  229. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATCE
  230. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ATCI
  231. #if BX_PLATFORM_IOS
  232. { MTLPixelFormatASTC_4x4_LDR, MTLPixelFormatASTC_4x4_sRGB }, // ASTC4x4
  233. { MTLPixelFormatASTC_5x5_LDR, MTLPixelFormatASTC_5x5_sRGB }, // ASTC5x5
  234. { MTLPixelFormatASTC_6x6_LDR, MTLPixelFormatASTC_6x6_sRGB }, // ASTC6x6
  235. { MTLPixelFormatASTC_8x5_LDR, MTLPixelFormatASTC_8x5_sRGB }, // ASTC8x5
  236. { MTLPixelFormatASTC_8x6_LDR, MTLPixelFormatASTC_8x6_sRGB }, // ASTC8x6
  237. { MTLPixelFormatASTC_10x5_LDR, MTLPixelFormatASTC_10x5_sRGB }, // ASTC10x5
  238. #else
  239. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC4x4
  240. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC5x5
  241. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC6x6
  242. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC8x5
  243. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC8x6
  244. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // ASTC10x5
  245. #endif
  246. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // Unknown
  247. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // R1
  248. { MTLPixelFormatA8Unorm, MTLPixelFormatInvalid }, // A8
  249. { MTLPixelFormatR8Unorm, MTLPixelFormat(11/*R8Unorm_sRGB*/) }, // R8
  250. { MTLPixelFormatR8Sint, MTLPixelFormatInvalid }, // R8I
  251. { MTLPixelFormatR8Uint, MTLPixelFormatInvalid }, // R8U
  252. { MTLPixelFormatR8Snorm, MTLPixelFormatInvalid }, // R8S
  253. { MTLPixelFormatR16Unorm, MTLPixelFormatInvalid }, // R16
  254. { MTLPixelFormatR16Sint, MTLPixelFormatInvalid }, // R16I
  255. { MTLPixelFormatR16Uint, MTLPixelFormatInvalid }, // R16U
  256. { MTLPixelFormatR16Float, MTLPixelFormatInvalid }, // R16F
  257. { MTLPixelFormatR16Snorm, MTLPixelFormatInvalid }, // R16S
  258. { MTLPixelFormatR32Sint, MTLPixelFormatInvalid }, // R32I
  259. { MTLPixelFormatR32Uint, MTLPixelFormatInvalid }, // R32U
  260. { MTLPixelFormatR32Float, MTLPixelFormatInvalid }, // R32F
  261. { MTLPixelFormatRG8Unorm, MTLPixelFormat(31/*RG8Unorm_sRGB*/) }, // RG8
  262. { MTLPixelFormatRG8Sint, MTLPixelFormatInvalid }, // RG8I
  263. { MTLPixelFormatRG8Uint, MTLPixelFormatInvalid }, // RG8U
  264. { MTLPixelFormatRG8Snorm, MTLPixelFormatInvalid }, // RG8S
  265. { MTLPixelFormatRG16Unorm, MTLPixelFormatInvalid }, // RG16
  266. { MTLPixelFormatRG16Sint, MTLPixelFormatInvalid }, // RG16I
  267. { MTLPixelFormatRG16Uint, MTLPixelFormatInvalid }, // RG16U
  268. { MTLPixelFormatRG16Float, MTLPixelFormatInvalid }, // RG16F
  269. { MTLPixelFormatRG16Snorm, MTLPixelFormatInvalid }, // RG16S
  270. { MTLPixelFormatRG32Sint, MTLPixelFormatInvalid }, // RG32I
  271. { MTLPixelFormatRG32Uint, MTLPixelFormatInvalid }, // RG32U
  272. { MTLPixelFormatRG32Float, MTLPixelFormatInvalid }, // RG32F
  273. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // RGB8
  274. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // RGB8I
  275. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // RGB8U
  276. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // RGB8S
  277. { MTLPixelFormatRGB9E5Float, MTLPixelFormatInvalid }, // RGB9E5F
  278. { MTLPixelFormatBGRA8Unorm, MTLPixelFormatBGRA8Unorm_sRGB }, // BGRA8
  279. { MTLPixelFormatRGBA8Unorm, MTLPixelFormatRGBA8Unorm_sRGB }, // RGBA8
  280. { MTLPixelFormatRGBA8Sint, MTLPixelFormatInvalid }, // RGBA8I
  281. { MTLPixelFormatRGBA8Uint, MTLPixelFormatInvalid }, // RGBA8U
  282. { MTLPixelFormatRGBA8Snorm, MTLPixelFormatInvalid }, // RGBA8S
  283. { MTLPixelFormatRGBA16Unorm, MTLPixelFormatInvalid }, // RGBA16
  284. { MTLPixelFormatRGBA16Sint, MTLPixelFormatInvalid }, // RGBA16I
  285. { MTLPixelFormatRGBA16Uint, MTLPixelFormatInvalid }, // RGBA16U
  286. { MTLPixelFormatRGBA16Float, MTLPixelFormatInvalid }, // RGBA16F
  287. { MTLPixelFormatRGBA16Snorm, MTLPixelFormatInvalid }, // RGBA16S
  288. { MTLPixelFormatRGBA32Sint, MTLPixelFormatInvalid }, // RGBA32I
  289. { MTLPixelFormatRGBA32Uint, MTLPixelFormatInvalid }, // RGBA32U
  290. { MTLPixelFormatRGBA32Float, MTLPixelFormatInvalid }, // RGBA32F
  291. { MTLPixelFormat(40/*B5G6R5Unorm*/), MTLPixelFormatInvalid }, // R5G6B5
  292. { MTLPixelFormat(42/*ABGR4Unorm*/), MTLPixelFormatInvalid }, // RGBA4
  293. { MTLPixelFormat(41/*A1BGR5Unorm*/), MTLPixelFormatInvalid }, // RGB5A1
  294. { MTLPixelFormatRGB10A2Unorm, MTLPixelFormatInvalid }, // RGB10A2
  295. { MTLPixelFormatRG11B10Float, MTLPixelFormatInvalid }, // RG11B10F
  296. { MTLPixelFormatInvalid, MTLPixelFormatInvalid }, // UnknownDepth
  297. { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D16
  298. { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D24
  299. { MTLPixelFormat(255/*Depth24Unorm_Stencil8*/), MTLPixelFormatInvalid }, // D24S8
  300. { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D32
  301. { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D16F
  302. { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D24F
  303. { MTLPixelFormatDepth32Float, MTLPixelFormatInvalid }, // D32F
  304. { MTLPixelFormatStencil8, MTLPixelFormatInvalid }, // D0S8
  305. };
  306. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormat) );
  307. int s_msaa[] =
  308. {
  309. 1,
  310. 2,
  311. 4,
  312. 8,
  313. 16,
  314. };
  315. static UniformType::Enum convertMtlType(MTLDataType _type)
  316. {
  317. switch (_type)
  318. {
  319. case MTLDataTypeUInt:
  320. case MTLDataTypeInt:
  321. return UniformType::Int1;
  322. case MTLDataTypeFloat:
  323. case MTLDataTypeFloat2:
  324. case MTLDataTypeFloat3:
  325. case MTLDataTypeFloat4:
  326. return UniformType::Vec4;
  327. case MTLDataTypeFloat3x3:
  328. return UniformType::Mat3;
  329. case MTLDataTypeFloat4x4:
  330. return UniformType::Mat4;
  331. default:
  332. break;
  333. };
  334. BX_CHECK(false, "Unrecognized Mtl Data type 0x%04x.", _type);
  335. return UniformType::End;
  336. }
  337. #define SHADER_FUNCTION_NAME ("xlatMtlMain")
  338. #define SHADER_UNIFORM_NAME ("_mtl_u")
  339. struct RendererContextMtl : public RendererContextI
  340. {
  341. RendererContextMtl()
  342. : m_metalLayer(NULL)
  343. , m_backBufferPixelFormatHash(0)
  344. , m_maxAnisotropy(1)
  345. , m_bufferIndex(0)
  346. , m_numWindows(1)
  347. , m_rtMsaa(false)
  348. , m_capture(NULL)
  349. , m_captureSize(0)
  350. , m_drawable(NULL)
  351. , m_saveScreenshot(false)
  352. {
  353. }
  354. ~RendererContextMtl()
  355. {
  356. }
  357. bool init(const Init& _init)
  358. {
  359. BX_UNUSED(_init);
  360. BX_TRACE("Init.");
  361. m_fbh.idx = kInvalidHandle;
  362. bx::memSet(m_uniforms, 0, sizeof(m_uniforms) );
  363. bx::memSet(&m_resolution, 0, sizeof(m_resolution) );
  364. if (NULL != NSClassFromString(@"MTKView") )
  365. {
  366. MTKView *view = (MTKView *)g_platformData.nwh;
  367. if (NULL != view && [view isKindOfClass:NSClassFromString(@"MTKView")])
  368. {
  369. m_metalLayer = (CAMetalLayer *)view.layer;
  370. }
  371. }
  372. if (NULL != NSClassFromString(@"CAMetalLayer") )
  373. {
  374. if (NULL == m_metalLayer)
  375. #if BX_PLATFORM_IOS
  376. {
  377. CAMetalLayer* metalLayer = (CAMetalLayer*)g_platformData.nwh;
  378. if (NULL == metalLayer
  379. || ![metalLayer isKindOfClass:NSClassFromString(@"CAMetalLayer")])
  380. {
  381. BX_WARN(NULL != m_device, "Unable to create Metal device. Please set platform data window to a CAMetalLayer");
  382. return false;
  383. }
  384. m_metalLayer = metalLayer;
  385. }
  386. #elif BX_PLATFORM_OSX
  387. {
  388. NSObject* nvh = (NSObject*)g_platformData.nwh;
  389. if ([nvh isKindOfClass:[CAMetalLayer class]])
  390. {
  391. CAMetalLayer* metalLayer = (CAMetalLayer*)g_platformData.nwh;
  392. m_metalLayer = metalLayer;
  393. }
  394. else
  395. {
  396. NSWindow* nsWindow = (NSWindow*)g_platformData.nwh;
  397. [nsWindow.contentView setWantsLayer:YES];
  398. m_metalLayer = [CAMetalLayer layer];
  399. [nsWindow.contentView setLayer:m_metalLayer];
  400. }
  401. }
  402. #endif // BX_PLATFORM_*
  403. m_device = (id<MTLDevice>)g_platformData.context;
  404. if (NULL == m_device)
  405. {
  406. m_device = MTLCreateSystemDefaultDevice();
  407. }
  408. }
  409. if (NULL == m_device
  410. || NULL == m_metalLayer)
  411. {
  412. BX_WARN(NULL != m_device, "Unable to create Metal device.");
  413. return false;
  414. }
  415. retain(m_device);
  416. m_metalLayer.device = m_device;
  417. m_metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
  418. m_cmd.init(m_device);
  419. BGFX_FATAL(NULL != m_cmd.m_commandQueue, Fatal::UnableToInitialize, "Unable to create Metal device.");
  420. m_renderPipelineDescriptor = newRenderPipelineDescriptor();
  421. m_depthStencilDescriptor = newDepthStencilDescriptor();
  422. m_frontFaceStencilDescriptor = newStencilDescriptor();
  423. m_backFaceStencilDescriptor = newStencilDescriptor();
  424. m_vertexDescriptor = newVertexDescriptor();
  425. m_textureDescriptor = newTextureDescriptor();
  426. m_samplerDescriptor = newSamplerDescriptor();
  427. for (uint8_t i=0; i < MTL_MAX_FRAMES_IN_FLIGHT; ++i)
  428. {
  429. m_uniformBuffers[i] = m_device.newBufferWithLength(UNIFORM_BUFFER_SIZE, 0);
  430. }
  431. m_uniformBufferVertexOffset = 0;
  432. m_uniformBufferFragmentOffset = 0;
  433. const char* vshSource =
  434. "using namespace metal;\n"
  435. "struct xlatMtlShaderOutput { float4 gl_Position [[position]]; float2 v_texcoord0; }; \n"
  436. "vertex xlatMtlShaderOutput xlatMtlMain (uint v_id [[ vertex_id ]]) \n"
  437. "{\n"
  438. " xlatMtlShaderOutput _mtl_o;\n"
  439. " if (v_id==0) { _mtl_o.gl_Position = float4(-1.0,-1.0,0.0,1.0); _mtl_o.v_texcoord0 = float2(0.0,1.0); } \n"
  440. " else if (v_id==1) { _mtl_o.gl_Position = float4(3.0,-1.0,0.0,1.0); _mtl_o.v_texcoord0 = float2(2.0,1.0); } \n"
  441. " else { _mtl_o.gl_Position = float4(-1.0,3.0,0.0,1.0); _mtl_o.v_texcoord0 = float2(0.0,-1.0); }\n"
  442. " return _mtl_o;\n"
  443. "}\n"
  444. ;
  445. const char* fshSource =
  446. "using namespace metal;\n"
  447. "struct xlatMtlShaderInput { float2 v_texcoord0; };\n"
  448. "fragment half4 xlatMtlMain (xlatMtlShaderInput _mtl_i[[stage_in]], texture2d<float> s_texColor [[texture(0)]], sampler _mtlsmp_s_texColor [[sampler(0)]] )\n"
  449. "{\n"
  450. " return half4(s_texColor.sample(_mtlsmp_s_texColor, _mtl_i.v_texcoord0) );\n"
  451. "}\n"
  452. ;
  453. Library lib = m_device.newLibraryWithSource(vshSource);
  454. if (NULL != lib)
  455. {
  456. m_screenshotBlitProgramVsh.m_function = lib.newFunctionWithName(SHADER_FUNCTION_NAME);
  457. }
  458. lib = m_device.newLibraryWithSource(fshSource);
  459. if (NULL != lib)
  460. {
  461. m_screenshotBlitProgramFsh.m_function = lib.newFunctionWithName(SHADER_FUNCTION_NAME);
  462. }
  463. m_screenshotBlitProgram.create(&m_screenshotBlitProgramVsh, &m_screenshotBlitProgramFsh);
  464. release(lib);
  465. reset(m_renderPipelineDescriptor);
  466. m_renderPipelineDescriptor.colorAttachments[0].pixelFormat = m_metalLayer.pixelFormat;
  467. m_renderPipelineDescriptor.vertexFunction = m_screenshotBlitProgram.m_vsh->m_function;
  468. m_renderPipelineDescriptor.fragmentFunction = m_screenshotBlitProgram.m_fsh->m_function;
  469. m_screenshotBlitRenderPipelineState = m_device.newRenderPipelineStateWithDescriptor(m_renderPipelineDescriptor);
  470. g_caps.supported |= (0
  471. | BGFX_CAPS_TEXTURE_COMPARE_LEQUAL
  472. | BGFX_CAPS_TEXTURE_COMPARE_ALL
  473. | BGFX_CAPS_TEXTURE_3D
  474. | BGFX_CAPS_VERTEX_ATTRIB_HALF
  475. | BGFX_CAPS_VERTEX_ATTRIB_UINT10
  476. | BGFX_CAPS_INSTANCING
  477. | BGFX_CAPS_FRAGMENT_DEPTH
  478. | BGFX_CAPS_BLEND_INDEPENDENT
  479. | BGFX_CAPS_INDEX32
  480. | BGFX_CAPS_TEXTURE_BLIT
  481. | BGFX_CAPS_TEXTURE_READ_BACK
  482. | BGFX_CAPS_OCCLUSION_QUERY
  483. | BGFX_CAPS_ALPHA_TO_COVERAGE
  484. | BGFX_CAPS_TEXTURE_2D_ARRAY
  485. );
  486. if (BX_ENABLED(BX_PLATFORM_IOS) )
  487. {
  488. if (iOSVersionEqualOrGreater("9.0.0") )
  489. {
  490. g_caps.limits.maxTextureSize = m_device.supportsFeatureSet( (MTLFeatureSet)4 /* iOS_GPUFamily3_v1 */) ? 16384 : 8192;
  491. }
  492. else
  493. {
  494. g_caps.limits.maxTextureSize = 4096;
  495. }
  496. g_caps.limits.maxFBAttachments = uint8_t(bx::uint32_min(m_device.supportsFeatureSet( (MTLFeatureSet)1 /* MTLFeatureSet_iOS_GPUFamily2_v1 */) ? 8 : 4, BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS) );
  497. }
  498. else if (BX_ENABLED(BX_PLATFORM_OSX) )
  499. {
  500. g_caps.limits.maxTextureSize = 16384;
  501. g_caps.limits.maxFBAttachments = 8;
  502. g_caps.supported |= BGFX_CAPS_TEXTURE_CUBE_ARRAY;
  503. }
  504. g_caps.limits.maxTextureLayers = 2048;
  505. g_caps.limits.maxVertexStreams = BGFX_CONFIG_MAX_VERTEX_STREAMS;
  506. m_hasPixelFormatDepth32Float_Stencil8 = false
  507. || BX_ENABLED(BX_PLATFORM_OSX)
  508. || (BX_ENABLED(BX_PLATFORM_IOS) && iOSVersionEqualOrGreater("9.0.0") )
  509. ;
  510. m_macOS11Runtime = true
  511. && BX_ENABLED(BX_PLATFORM_OSX)
  512. && macOSVersionEqualOrGreater(10,11,0)
  513. ;
  514. m_iOS9Runtime = true
  515. && BX_ENABLED(BX_PLATFORM_IOS)
  516. && iOSVersionEqualOrGreater("9.0.0")
  517. ;
  518. if (BX_ENABLED(BX_PLATFORM_OSX) )
  519. {
  520. s_textureFormat[TextureFormat::R8].m_fmtSrgb = MTLPixelFormatInvalid;
  521. s_textureFormat[TextureFormat::RG8].m_fmtSrgb = MTLPixelFormatInvalid;
  522. }
  523. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  524. {
  525. uint16_t support = 0;
  526. support |= MTLPixelFormatInvalid != s_textureFormat[ii].m_fmt
  527. ? BGFX_CAPS_FORMAT_TEXTURE_2D
  528. | BGFX_CAPS_FORMAT_TEXTURE_3D
  529. | BGFX_CAPS_FORMAT_TEXTURE_CUBE
  530. | BGFX_CAPS_FORMAT_TEXTURE_VERTEX
  531. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  532. ;
  533. support |= MTLPixelFormatInvalid != s_textureFormat[ii].m_fmtSrgb
  534. ? BGFX_CAPS_FORMAT_TEXTURE_2D_SRGB
  535. | BGFX_CAPS_FORMAT_TEXTURE_3D_SRGB
  536. | BGFX_CAPS_FORMAT_TEXTURE_CUBE_SRGB
  537. | BGFX_CAPS_FORMAT_TEXTURE_VERTEX
  538. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  539. ;
  540. if (!bimg::isCompressed(bimg::TextureFormat::Enum(ii) ) )
  541. {
  542. support |= 0
  543. | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER
  544. | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA
  545. ;
  546. }
  547. g_caps.formats[ii] = support;
  548. }
  549. g_caps.formats[TextureFormat::A8 ] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  550. g_caps.formats[TextureFormat::RG32I ] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  551. g_caps.formats[TextureFormat::RG32U ] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  552. g_caps.formats[TextureFormat::RGBA32I] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  553. g_caps.formats[TextureFormat::RGBA32U] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  554. if (BX_ENABLED(BX_PLATFORM_IOS) )
  555. {
  556. s_textureFormat[TextureFormat::D24S8].m_fmt = MTLPixelFormatDepth32Float_Stencil8;
  557. g_caps.formats[TextureFormat::BC1 ] =
  558. g_caps.formats[TextureFormat::BC2 ] =
  559. g_caps.formats[TextureFormat::BC3 ] =
  560. g_caps.formats[TextureFormat::BC4 ] =
  561. g_caps.formats[TextureFormat::BC5 ] =
  562. g_caps.formats[TextureFormat::BC6H] =
  563. g_caps.formats[TextureFormat::BC7 ] = BGFX_CAPS_FORMAT_TEXTURE_NONE;
  564. g_caps.formats[TextureFormat::RG32F ] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  565. g_caps.formats[TextureFormat::RGBA32F] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  566. }
  567. if (BX_ENABLED(BX_PLATFORM_OSX) )
  568. {
  569. s_textureFormat[TextureFormat::D24S8].m_fmt = (MTLPixelFormat)(m_device.depth24Stencil8PixelFormatSupported()
  570. ? 255 /* Depth24Unorm_Stencil8 */
  571. : MTLPixelFormatDepth32Float_Stencil8)
  572. ;
  573. g_caps.formats[TextureFormat::ETC2 ] =
  574. g_caps.formats[TextureFormat::ETC2A ] =
  575. g_caps.formats[TextureFormat::ETC2A1] =
  576. g_caps.formats[TextureFormat::PTC12 ] =
  577. g_caps.formats[TextureFormat::PTC14 ] =
  578. g_caps.formats[TextureFormat::PTC12A] =
  579. g_caps.formats[TextureFormat::PTC14A] =
  580. g_caps.formats[TextureFormat::R5G6B5] =
  581. g_caps.formats[TextureFormat::RGBA4 ] =
  582. g_caps.formats[TextureFormat::RGB5A1] = BGFX_CAPS_FORMAT_TEXTURE_NONE;
  583. g_caps.formats[TextureFormat::RGB9E5F] &= ~(BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER | BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA);
  584. }
  585. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  586. {
  587. if (BGFX_CAPS_FORMAT_TEXTURE_NONE == g_caps.formats[ii])
  588. {
  589. s_textureFormat[ii].m_fmt = MTLPixelFormatInvalid;
  590. s_textureFormat[ii].m_fmtSrgb = MTLPixelFormatInvalid;
  591. }
  592. }
  593. for (uint32_t ii=1; ii<5; ++ii)
  594. {
  595. if (!m_device.supportsTextureSampleCount(s_msaa[ii]) )
  596. {
  597. s_msaa[ii] = s_msaa[ii-1];
  598. }
  599. }
  600. // Init reserved part of view name.
  601. for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
  602. {
  603. bx::snprintf(s_viewName[ii], BGFX_CONFIG_MAX_VIEW_NAME_RESERVED+1, "%3d ", ii);
  604. }
  605. m_occlusionQuery.preReset();
  606. m_gpuTimer.init();
  607. g_internalData.context = m_device;
  608. return true;
  609. }
  610. void shutdown()
  611. {
  612. m_occlusionQuery.postReset();
  613. m_gpuTimer.shutdown();
  614. m_pipelineStateCache.invalidate();
  615. for (uint32_t ii = 0; ii < BX_COUNTOF(m_shaders); ++ii)
  616. {
  617. m_shaders[ii].destroy();
  618. }
  619. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  620. {
  621. m_textures[ii].destroy();
  622. }
  623. m_screenshotBlitProgramVsh.destroy();
  624. m_screenshotBlitProgramFsh.destroy();
  625. m_screenshotBlitProgram.destroy();
  626. MTL_RELEASE(m_screenshotBlitRenderPipelineState);
  627. captureFinish();
  628. MTL_RELEASE(m_depthStencilDescriptor);
  629. MTL_RELEASE(m_frontFaceStencilDescriptor);
  630. MTL_RELEASE(m_backFaceStencilDescriptor);
  631. MTL_RELEASE(m_renderPipelineDescriptor);
  632. MTL_RELEASE(m_vertexDescriptor);
  633. MTL_RELEASE(m_textureDescriptor);
  634. MTL_RELEASE(m_samplerDescriptor);
  635. MTL_RELEASE(m_backBufferDepth);
  636. MTL_RELEASE(m_backBufferStencil);
  637. for (uint8_t i=0; i < MTL_MAX_FRAMES_IN_FLIGHT; ++i)
  638. {
  639. MTL_RELEASE(m_uniformBuffers[i]);
  640. }
  641. m_cmd.shutdown();
  642. MTL_RELEASE(m_device);
  643. }
  644. RendererType::Enum getRendererType() const override
  645. {
  646. return RendererType::Metal;
  647. }
  648. const char* getRendererName() const override
  649. {
  650. return BGFX_RENDERER_METAL_NAME;
  651. }
  652. void createIndexBuffer(IndexBufferHandle _handle, const Memory* _mem, uint16_t _flags) override
  653. {
  654. m_indexBuffers[_handle.idx].create(_mem->size, _mem->data, _flags);
  655. }
  656. void destroyIndexBuffer(IndexBufferHandle _handle) override
  657. {
  658. m_indexBuffers[_handle.idx].destroy();
  659. }
  660. void createVertexDecl(VertexDeclHandle _handle, const VertexDecl& _decl) override
  661. {
  662. VertexDecl& decl = m_vertexDecls[_handle.idx];
  663. bx::memCopy(&decl, &_decl, sizeof(VertexDecl) );
  664. dump(decl);
  665. }
  666. void destroyVertexDecl(VertexDeclHandle /*_handle*/) override
  667. {
  668. }
  669. void createVertexBuffer(VertexBufferHandle _handle, const Memory* _mem, VertexDeclHandle _declHandle, uint16_t _flags) override
  670. {
  671. m_vertexBuffers[_handle.idx].create(_mem->size, _mem->data, _declHandle, _flags);
  672. }
  673. void destroyVertexBuffer(VertexBufferHandle _handle) override
  674. {
  675. m_vertexBuffers[_handle.idx].destroy();
  676. }
  677. void createDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size, uint16_t _flags) override
  678. {
  679. m_indexBuffers[_handle.idx].create(_size, NULL, _flags);
  680. }
  681. void updateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _offset, uint32_t _size, const Memory* _mem) override
  682. {
  683. m_indexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  684. }
  685. void destroyDynamicIndexBuffer(IndexBufferHandle _handle) override
  686. {
  687. m_indexBuffers[_handle.idx].destroy();
  688. }
  689. void createDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _size, uint16_t _flags) override
  690. {
  691. VertexDeclHandle decl = BGFX_INVALID_HANDLE;
  692. m_vertexBuffers[_handle.idx].create(_size, NULL, decl, _flags);
  693. }
  694. void updateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _offset, uint32_t _size, const Memory* _mem) override
  695. {
  696. m_vertexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  697. }
  698. void destroyDynamicVertexBuffer(VertexBufferHandle _handle) override
  699. {
  700. m_vertexBuffers[_handle.idx].destroy();
  701. }
  702. void createShader(ShaderHandle _handle, const Memory* _mem) override
  703. {
  704. m_shaders[_handle.idx].create(_mem);
  705. }
  706. void destroyShader(ShaderHandle _handle) override
  707. {
  708. m_shaders[_handle.idx].destroy();
  709. }
  710. void createProgram(ProgramHandle _handle, ShaderHandle _vsh, ShaderHandle _fsh) override
  711. {
  712. m_program[_handle.idx].create(&m_shaders[_vsh.idx], &m_shaders[_fsh.idx]);
  713. }
  714. void destroyProgram(ProgramHandle _handle) override
  715. {
  716. m_program[_handle.idx].destroy();
  717. }
  718. void* createTexture(TextureHandle _handle, const Memory* _mem, uint32_t _flags, uint8_t _skip) override
  719. {
  720. m_textures[_handle.idx].create(_mem, _flags, _skip);
  721. return NULL;
  722. }
  723. void updateTextureBegin(TextureHandle /*_handle*/, uint8_t /*_side*/, uint8_t /*_mip*/) override
  724. {
  725. }
  726. 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
  727. {
  728. m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _pitch, _mem);
  729. }
  730. void updateTextureEnd() override
  731. {
  732. }
  733. void readTexture(TextureHandle _handle, void* _data, uint8_t _mip) override
  734. {
  735. m_cmd.kick(false, true);
  736. m_commandBuffer = m_cmd.alloc();
  737. const TextureMtl& texture = m_textures[_handle.idx];
  738. BX_CHECK(_mip<texture.m_numMips,"Invalid mip: %d num mips:",_mip,texture.m_numMips);
  739. uint32_t srcWidth = bx::uint32_max(1, texture.m_ptr.width() >> _mip);
  740. uint32_t srcHeight = bx::uint32_max(1, texture.m_ptr.height() >> _mip);
  741. const uint8_t bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(texture.m_textureFormat) );
  742. MTLRegion region =
  743. {
  744. { 0, 0, 0 },
  745. { srcWidth, srcHeight, 1 },
  746. };
  747. texture.m_ptr.getBytes(_data, srcWidth*bpp/8, 0, region, _mip, 0);
  748. }
  749. void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height, uint8_t _numMips) override
  750. {
  751. TextureMtl& texture = m_textures[_handle.idx];
  752. uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate);
  753. const Memory* mem = alloc(size);
  754. bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
  755. uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
  756. bx::write(&writer, magic);
  757. TextureCreate tc;
  758. tc.m_width = _width;
  759. tc.m_height = _height;
  760. tc.m_depth = 0;
  761. tc.m_numLayers = 1;
  762. tc.m_numMips = _numMips;
  763. tc.m_format = TextureFormat::Enum(texture.m_requestedFormat);
  764. tc.m_cubeMap = false;
  765. tc.m_mem = NULL;
  766. bx::write(&writer, tc);
  767. texture.destroy();
  768. texture.create(mem, texture.m_flags, 0);
  769. release(mem);
  770. }
  771. void overrideInternal(TextureHandle _handle, uintptr_t _ptr) override
  772. {
  773. BX_UNUSED(_handle, _ptr);
  774. }
  775. uintptr_t getInternal(TextureHandle _handle) override
  776. {
  777. BX_UNUSED(_handle);
  778. return 0;
  779. }
  780. void destroyTexture(TextureHandle _handle) override
  781. {
  782. m_textures[_handle.idx].destroy();
  783. }
  784. void createFrameBuffer(FrameBufferHandle _handle, uint8_t _num, const Attachment* _attachment) override
  785. {
  786. m_frameBuffers[_handle.idx].create(_num, _attachment);
  787. }
  788. void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) override
  789. {
  790. uint16_t denseIdx = m_numWindows++;
  791. m_windows[denseIdx] = _handle;
  792. m_frameBuffers[_handle.idx].create(denseIdx, _nwh, _width, _height, _depthFormat);
  793. }
  794. void destroyFrameBuffer(FrameBufferHandle _handle) override
  795. {
  796. uint16_t denseIdx = m_frameBuffers[_handle.idx].destroy();
  797. if (UINT16_MAX != denseIdx)
  798. {
  799. --m_numWindows;
  800. if (m_numWindows > 1)
  801. {
  802. FrameBufferHandle handle = m_windows[m_numWindows];
  803. m_windows[denseIdx] = handle;
  804. m_frameBuffers[handle.idx].m_denseIdx = denseIdx;
  805. }
  806. }
  807. }
  808. void createUniform(UniformHandle _handle, UniformType::Enum _type, uint16_t _num, const char* _name) override
  809. {
  810. if (NULL != m_uniforms[_handle.idx])
  811. {
  812. BX_FREE(g_allocator, m_uniforms[_handle.idx]);
  813. }
  814. uint32_t size = BX_ALIGN_16(g_uniformTypeSize[_type]*_num);
  815. void* data = BX_ALLOC(g_allocator, size);
  816. bx::memSet(data, 0, size);
  817. m_uniforms[_handle.idx] = data;
  818. m_uniformReg.add(_handle, _name);
  819. }
  820. void destroyUniform(UniformHandle _handle) override
  821. {
  822. BX_FREE(g_allocator, m_uniforms[_handle.idx]);
  823. m_uniforms[_handle.idx] = NULL;
  824. m_uniformReg.remove(_handle);
  825. }
  826. void requestScreenShotPre(const char* _filePath)
  827. {
  828. BX_UNUSED(_filePath);
  829. m_saveScreenshot = true;
  830. }
  831. void requestScreenShot(FrameBufferHandle _handle, const char* _filePath) override
  832. {
  833. BX_UNUSED(_handle);
  834. if (NULL == m_screenshotTarget)
  835. {
  836. return;
  837. }
  838. m_cmd.kick(false, true);
  839. m_commandBuffer = 0;
  840. uint32_t width = m_screenshotTarget.width();
  841. uint32_t height = m_screenshotTarget.height();
  842. uint32_t length = width*height*4;
  843. uint8_t* data = (uint8_t*)BX_ALLOC(g_allocator, length);
  844. MTLRegion region = { { 0, 0, 0 }, { width, height, 1 } };
  845. m_screenshotTarget.getBytes(data, 4*width, 0, region, 0, 0);
  846. g_callback->screenShot(
  847. _filePath
  848. , m_screenshotTarget.width()
  849. , m_screenshotTarget.height()
  850. , width*4
  851. , data
  852. , length
  853. , false
  854. );
  855. BX_FREE(g_allocator, data);
  856. m_commandBuffer = m_cmd.alloc();
  857. }
  858. void updateViewName(ViewId _id, const char* _name) override
  859. {
  860. if (BX_ENABLED(BGFX_CONFIG_DEBUG_PIX) )
  861. {
  862. bx::strCopy(
  863. &s_viewName[_id][BGFX_CONFIG_MAX_VIEW_NAME_RESERVED]
  864. , BX_COUNTOF(s_viewName[0])-BGFX_CONFIG_MAX_VIEW_NAME_RESERVED
  865. , _name
  866. );
  867. }
  868. }
  869. void updateUniform(uint16_t _loc, const void* _data, uint32_t _size) override
  870. {
  871. bx::memCopy(m_uniforms[_loc], _data, _size);
  872. }
  873. void setMarker(const char* _marker, uint32_t /*_size*/) override
  874. {
  875. if (BX_ENABLED(BGFX_CONFIG_DEBUG_MTL) )
  876. {
  877. m_renderCommandEncoder.insertDebugSignpost(_marker);
  878. }
  879. }
  880. void invalidateOcclusionQuery(OcclusionQueryHandle _handle) override
  881. {
  882. m_occlusionQuery.invalidate(_handle);
  883. }
  884. virtual void setName(Handle _handle, const char* _name) override
  885. {
  886. switch (_handle.type)
  887. {
  888. case Handle::Shader:
  889. m_shaders[_handle.idx].m_function.setLabel(_name);
  890. break;
  891. case Handle::Texture:
  892. m_textures[_handle.idx].m_ptr.setLabel(_name);
  893. break;
  894. default:
  895. BX_CHECK(false, "Invalid handle type?! %d", _handle.type);
  896. break;
  897. }
  898. }
  899. void submitBlit(BlitState& _bs, uint16_t _view);
  900. void submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter) override;
  901. void blitSetup(TextVideoMemBlitter& _blitter) override
  902. {
  903. RenderCommandEncoder rce = m_renderCommandEncoder;
  904. uint32_t width = m_resolution.width;
  905. uint32_t height = m_resolution.height;
  906. FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
  907. if (NULL == rce
  908. || m_renderCommandEncoderFrameBufferHandle.idx != kInvalidHandle)
  909. {
  910. if (m_renderCommandEncoder)
  911. {
  912. m_renderCommandEncoder.endEncoding();
  913. }
  914. RenderPassDescriptor renderPassDescriptor = newRenderPassDescriptor();
  915. setFrameBuffer(renderPassDescriptor, fbh);
  916. renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionLoad;
  917. renderPassDescriptor.colorAttachments[0].storeAction =
  918. NULL != renderPassDescriptor.colorAttachments[0].resolveTexture
  919. ? MTLStoreActionMultisampleResolve
  920. : MTLStoreActionStore
  921. ;
  922. rce = m_commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor);
  923. m_renderCommandEncoder = rce;
  924. m_renderCommandEncoderFrameBufferHandle = fbh;
  925. MTL_RELEASE(renderPassDescriptor);
  926. }
  927. MTLViewport viewport = { 0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f};
  928. rce.setViewport(viewport);
  929. MTLScissorRect rc = { 0,0,width,height };
  930. rce.setScissorRect(rc);
  931. rce.setCullMode(MTLCullModeNone);
  932. uint64_t state = 0
  933. | BGFX_STATE_WRITE_RGB
  934. | BGFX_STATE_WRITE_A
  935. | BGFX_STATE_DEPTH_TEST_ALWAYS
  936. ;
  937. setDepthStencilState(state);
  938. RenderPipelineState pso = getPipelineState(
  939. state
  940. , 0
  941. , fbh
  942. , _blitter.m_vb->decl
  943. , _blitter.m_program.idx
  944. , 0
  945. );
  946. rce.setRenderPipelineState(pso);
  947. ProgramMtl& program = m_program[_blitter.m_program.idx];
  948. uint32_t vertexUniformBufferSize = program.m_vshConstantBufferSize;
  949. uint32_t fragmentUniformBufferSize = program.m_fshConstantBufferSize;
  950. if (vertexUniformBufferSize )
  951. {
  952. m_uniformBufferVertexOffset = BX_ALIGN_MASK(m_uniformBufferVertexOffset, program.m_vshConstantBufferAlignmentMask);
  953. rce.setVertexBuffer(m_uniformBuffer, m_uniformBufferVertexOffset, 0);
  954. }
  955. m_uniformBufferFragmentOffset = m_uniformBufferVertexOffset + vertexUniformBufferSize;
  956. if (fragmentUniformBufferSize )
  957. {
  958. m_uniformBufferFragmentOffset = BX_ALIGN_MASK(m_uniformBufferFragmentOffset, program.m_fshConstantBufferAlignmentMask);
  959. rce.setFragmentBuffer(m_uniformBuffer, m_uniformBufferFragmentOffset, 0);
  960. }
  961. float proj[16];
  962. bx::mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f, 0.0f, false);
  963. PredefinedUniform& predefined = program.m_predefined[0];
  964. uint8_t flags = predefined.m_type;
  965. setShaderUniform(flags, predefined.m_loc, proj, 4);
  966. m_textures[_blitter.m_texture.idx].commit(0, false, true);
  967. }
  968. void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) override
  969. {
  970. const uint32_t numVertices = _numIndices*4/6;
  971. if (0 < numVertices)
  972. {
  973. m_indexBuffers [_blitter.m_ib->handle.idx].update(0, _numIndices*2, _blitter.m_ib->data, true);
  974. m_vertexBuffers[_blitter.m_vb->handle.idx].update(0, numVertices*_blitter.m_decl.m_stride, _blitter.m_vb->data, true);
  975. VertexBufferMtl& vb = m_vertexBuffers[_blitter.m_vb->handle.idx];
  976. m_renderCommandEncoder.setVertexBuffer(vb.getBuffer(), 0, 1);
  977. m_renderCommandEncoder.drawIndexedPrimitives(MTLPrimitiveTypeTriangle, _numIndices, MTLIndexTypeUInt16, m_indexBuffers[_blitter.m_ib->handle.idx].getBuffer(), 0, 1);
  978. }
  979. }
  980. bool isDeviceRemoved() override
  981. {
  982. return false;
  983. }
  984. void flip() override
  985. {
  986. if (NULL == m_commandBuffer)
  987. {
  988. return;
  989. }
  990. if (NULL != m_drawable)
  991. {
  992. m_commandBuffer.presentDrawable(m_drawable);
  993. MTL_RELEASE(m_drawable);
  994. }
  995. m_cmd.kick(true);
  996. m_commandBuffer = 0;
  997. }
  998. void updateResolution(const Resolution& _resolution)
  999. {
  1000. m_maxAnisotropy = !!(_resolution.reset & BGFX_RESET_MAXANISOTROPY)
  1001. ? 16
  1002. : 1
  1003. ;
  1004. const uint32_t maskFlags = ~(0
  1005. | BGFX_RESET_MAXANISOTROPY
  1006. | BGFX_RESET_DEPTH_CLAMP
  1007. | BGFX_RESET_SUSPEND
  1008. );
  1009. if (m_resolution.width != _resolution.width
  1010. || m_resolution.height != _resolution.height
  1011. || (m_resolution.reset&maskFlags) != (_resolution.reset&maskFlags) )
  1012. {
  1013. int sampleCount = s_msaa[(_resolution.reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT];
  1014. MTLPixelFormat prevMetalLayerPixelFormat = m_metalLayer.pixelFormat;
  1015. #if BX_PLATFORM_OSX > 101300
  1016. m_metalLayer.displaySyncEnabled = 0 != (_resolution.reset&BGFX_RESET_VSYNC);
  1017. #endif // BX_PLATFORM_OSX > 101300
  1018. m_metalLayer.drawableSize = CGSizeMake(_resolution.width, _resolution.height);
  1019. m_metalLayer.pixelFormat = (m_resolution.reset & BGFX_RESET_SRGB_BACKBUFFER)
  1020. ? MTLPixelFormatBGRA8Unorm_sRGB
  1021. : MTLPixelFormatBGRA8Unorm
  1022. ;
  1023. m_resolution = _resolution;
  1024. m_resolution.reset &= ~BGFX_RESET_INTERNAL_FORCE;
  1025. m_textureDescriptor.textureType = sampleCount > 1 ? MTLTextureType2DMultisample : MTLTextureType2D;
  1026. if (m_hasPixelFormatDepth32Float_Stencil8)
  1027. {
  1028. m_textureDescriptor.pixelFormat = MTLPixelFormatDepth32Float_Stencil8;
  1029. }
  1030. else
  1031. {
  1032. m_textureDescriptor.pixelFormat = MTLPixelFormatDepth32Float;
  1033. }
  1034. m_textureDescriptor.width = _resolution.width;
  1035. m_textureDescriptor.height = _resolution.height;
  1036. m_textureDescriptor.depth = 1;
  1037. m_textureDescriptor.mipmapLevelCount = 1;
  1038. m_textureDescriptor.sampleCount = sampleCount;
  1039. m_textureDescriptor.arrayLength = 1;
  1040. if (m_iOS9Runtime
  1041. || m_macOS11Runtime)
  1042. {
  1043. m_textureDescriptor.cpuCacheMode = MTLCPUCacheModeDefaultCache;
  1044. m_textureDescriptor.storageMode = MTLStorageModePrivate;
  1045. m_textureDescriptor.usage = MTLTextureUsageRenderTarget;
  1046. }
  1047. if (NULL != m_backBufferDepth)
  1048. {
  1049. release(m_backBufferDepth);
  1050. }
  1051. m_backBufferDepth = m_device.newTextureWithDescriptor(m_textureDescriptor);
  1052. if (NULL != m_backBufferStencil)
  1053. {
  1054. release(m_backBufferStencil);
  1055. }
  1056. if (m_hasPixelFormatDepth32Float_Stencil8)
  1057. {
  1058. m_backBufferStencil = m_backBufferDepth;
  1059. retain(m_backBufferStencil);
  1060. }
  1061. else
  1062. {
  1063. m_textureDescriptor.pixelFormat = MTLPixelFormatStencil8;
  1064. m_backBufferStencil = m_device.newTextureWithDescriptor(m_textureDescriptor);
  1065. }
  1066. if (sampleCount > 1)
  1067. {
  1068. if (NULL != m_backBufferColorMSAA)
  1069. {
  1070. release(m_backBufferColorMSAA);
  1071. }
  1072. m_textureDescriptor.pixelFormat = m_metalLayer.pixelFormat;
  1073. m_backBufferColorMSAA = m_device.newTextureWithDescriptor(m_textureDescriptor);
  1074. }
  1075. bx::HashMurmur2A murmur;
  1076. murmur.begin();
  1077. murmur.add(1);
  1078. murmur.add( (uint32_t)m_metalLayer.pixelFormat);
  1079. murmur.add( (uint32_t)m_backBufferDepth.pixelFormat() );
  1080. murmur.add( (uint32_t)m_backBufferStencil.pixelFormat() );
  1081. murmur.add( (uint32_t)sampleCount);
  1082. m_backBufferPixelFormatHash = murmur.end();
  1083. for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii)
  1084. {
  1085. m_frameBuffers[ii].postReset();
  1086. }
  1087. updateCapture();
  1088. m_textVideoMem.resize(false, _resolution.width, _resolution.height);
  1089. m_textVideoMem.clear();
  1090. if (prevMetalLayerPixelFormat != m_metalLayer.pixelFormat)
  1091. {
  1092. MTL_RELEASE(m_screenshotBlitRenderPipelineState)
  1093. reset(m_renderPipelineDescriptor);
  1094. m_renderPipelineDescriptor.colorAttachments[0].pixelFormat = m_metalLayer.pixelFormat;
  1095. m_renderPipelineDescriptor.vertexFunction = m_screenshotBlitProgram.m_vsh->m_function;
  1096. m_renderPipelineDescriptor.fragmentFunction = m_screenshotBlitProgram.m_fsh->m_function;
  1097. m_screenshotBlitRenderPipelineState = m_device.newRenderPipelineStateWithDescriptor(m_renderPipelineDescriptor);
  1098. }
  1099. }
  1100. }
  1101. void updateCapture()
  1102. {
  1103. if (m_resolution.reset&BGFX_RESET_CAPTURE)
  1104. {
  1105. m_captureSize = m_resolution.width*m_resolution.height*4;
  1106. m_capture = BX_REALLOC(g_allocator, m_capture, m_captureSize);
  1107. g_callback->captureBegin(m_resolution.width, m_resolution.height, m_resolution.width*4, TextureFormat::BGRA8, false);
  1108. }
  1109. else
  1110. {
  1111. captureFinish();
  1112. }
  1113. }
  1114. void capture()
  1115. {
  1116. if (NULL != m_capture)
  1117. {
  1118. if (NULL == m_screenshotTarget)
  1119. {
  1120. return;
  1121. }
  1122. m_renderCommandEncoder.endEncoding();
  1123. m_cmd.kick(false, true);
  1124. m_commandBuffer = 0;
  1125. MTLRegion region = { { 0, 0, 0 }, { m_resolution.width, m_resolution.height, 1 } };
  1126. m_screenshotTarget.getBytes(m_capture, 4*m_resolution.width, 0, region, 0, 0);
  1127. m_commandBuffer = m_cmd.alloc();
  1128. if (m_screenshotTarget.pixelFormat() == MTLPixelFormatRGBA8Uint)
  1129. {
  1130. bimg::imageSwizzleBgra8(
  1131. m_capture
  1132. , m_resolution.width*4
  1133. , m_resolution.width
  1134. , m_resolution.height
  1135. , m_capture
  1136. , m_resolution.width*4
  1137. );
  1138. }
  1139. g_callback->captureFrame(m_capture, m_captureSize);
  1140. RenderPassDescriptor renderPassDescriptor = newRenderPassDescriptor();
  1141. setFrameBuffer(renderPassDescriptor, m_renderCommandEncoderFrameBufferHandle);
  1142. for (uint32_t ii = 0; ii < g_caps.limits.maxFBAttachments; ++ii)
  1143. {
  1144. MTLRenderPassColorAttachmentDescriptor* desc = renderPassDescriptor.colorAttachments[ii];
  1145. if (NULL != desc.texture)
  1146. {
  1147. desc.loadAction = MTLLoadActionLoad;
  1148. }
  1149. }
  1150. RenderPassDepthAttachmentDescriptor depthAttachment = renderPassDescriptor.depthAttachment;
  1151. if (NULL != depthAttachment.texture)
  1152. {
  1153. depthAttachment.loadAction = MTLLoadActionLoad;
  1154. depthAttachment.storeAction = MTLStoreActionStore;
  1155. }
  1156. RenderPassStencilAttachmentDescriptor stencilAttachment = renderPassDescriptor.stencilAttachment;
  1157. if (NULL != stencilAttachment.texture)
  1158. {
  1159. stencilAttachment.loadAction = MTLLoadActionLoad;
  1160. stencilAttachment.storeAction = MTLStoreActionStore;
  1161. }
  1162. m_renderCommandEncoder = m_commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor);
  1163. MTL_RELEASE(renderPassDescriptor);
  1164. }
  1165. }
  1166. void captureFinish()
  1167. {
  1168. if (NULL != m_capture)
  1169. {
  1170. g_callback->captureEnd();
  1171. BX_FREE(g_allocator, m_capture);
  1172. m_capture = NULL;
  1173. m_captureSize = 0;
  1174. }
  1175. }
  1176. void setShaderUniform(uint8_t _flags, uint32_t _loc, const void* _val, uint32_t _numRegs)
  1177. {
  1178. uint32_t offset = 0 != (_flags&BGFX_UNIFORM_FRAGMENTBIT)
  1179. ? m_uniformBufferFragmentOffset
  1180. : m_uniformBufferVertexOffset
  1181. ;
  1182. uint8_t* dst = (uint8_t*)m_uniformBuffer.contents();
  1183. bx::memCopy(&dst[offset + _loc], _val, _numRegs*16);
  1184. }
  1185. void setShaderUniform4f(uint8_t _flags, uint32_t _loc, const void* _val, uint32_t _numRegs)
  1186. {
  1187. setShaderUniform(_flags, _loc, _val, _numRegs);
  1188. }
  1189. void setShaderUniform4x4f(uint8_t _flags, uint32_t _loc, const void* _val, uint32_t _numRegs)
  1190. {
  1191. setShaderUniform(_flags, _loc, _val, _numRegs);
  1192. }
  1193. void commit(UniformBuffer& _uniformBuffer)
  1194. {
  1195. _uniformBuffer.reset();
  1196. for (;;)
  1197. {
  1198. uint32_t opcode = _uniformBuffer.read();
  1199. if (UniformType::End == opcode)
  1200. {
  1201. break;
  1202. }
  1203. UniformType::Enum type;
  1204. uint16_t loc;
  1205. uint16_t num;
  1206. uint16_t copy;
  1207. UniformBuffer::decodeOpcode(opcode, type, loc, num, copy);
  1208. const char* data;
  1209. if (copy)
  1210. {
  1211. data = _uniformBuffer.read(g_uniformTypeSize[type]*num);
  1212. }
  1213. else
  1214. {
  1215. UniformHandle handle;
  1216. bx::memCopy(&handle, _uniformBuffer.read(sizeof(UniformHandle) ), sizeof(UniformHandle) );
  1217. data = (const char*)m_uniforms[handle.idx];
  1218. }
  1219. #define CASE_IMPLEMENT_UNIFORM(_uniform, _dxsuffix, _type) \
  1220. case UniformType::_uniform: \
  1221. case UniformType::_uniform|BGFX_UNIFORM_FRAGMENTBIT: \
  1222. { \
  1223. setShaderUniform(uint8_t(type), loc, data, num); \
  1224. } \
  1225. break;
  1226. switch ( (uint32_t)type)
  1227. {
  1228. case UniformType::Mat3:
  1229. case UniformType::Mat3|BGFX_UNIFORM_FRAGMENTBIT:
  1230. {
  1231. float* value = (float*)data;
  1232. for (uint32_t ii = 0, count = num/3; ii < count; ++ii, loc += 3*16, value += 9)
  1233. {
  1234. Matrix4 mtx;
  1235. mtx.un.val[ 0] = value[0];
  1236. mtx.un.val[ 1] = value[1];
  1237. mtx.un.val[ 2] = value[2];
  1238. mtx.un.val[ 3] = 0.0f;
  1239. mtx.un.val[ 4] = value[3];
  1240. mtx.un.val[ 5] = value[4];
  1241. mtx.un.val[ 6] = value[5];
  1242. mtx.un.val[ 7] = 0.0f;
  1243. mtx.un.val[ 8] = value[6];
  1244. mtx.un.val[ 9] = value[7];
  1245. mtx.un.val[10] = value[8];
  1246. mtx.un.val[11] = 0.0f;
  1247. setShaderUniform(uint8_t(type), loc, &mtx.un.val[0], 3);
  1248. }
  1249. }
  1250. break;
  1251. CASE_IMPLEMENT_UNIFORM(Int1, I, int);
  1252. CASE_IMPLEMENT_UNIFORM(Vec4, F, float);
  1253. CASE_IMPLEMENT_UNIFORM(Mat4, F, float);
  1254. case UniformType::End:
  1255. break;
  1256. default:
  1257. BX_TRACE("%4d: INVALID 0x%08x, t %d, l %d, n %d, c %d", _uniformBuffer.getPos(), opcode, type, loc, num, copy);
  1258. break;
  1259. }
  1260. #undef CASE_IMPLEMENT_UNIFORM
  1261. }
  1262. }
  1263. void clearQuad(ClearQuad& _clearQuad, const Rect& /*_rect*/, const Clear& _clear, const float _palette[][4])
  1264. {
  1265. uint32_t width;
  1266. uint32_t height;
  1267. if (isValid(m_fbh) )
  1268. {
  1269. const FrameBufferMtl& fb = m_frameBuffers[m_fbh.idx];
  1270. width = fb.m_width;
  1271. height = fb.m_height;
  1272. }
  1273. else
  1274. {
  1275. width = m_resolution.width;
  1276. height = m_resolution.height;
  1277. }
  1278. uint64_t state = 0;
  1279. state |= _clear.m_flags & BGFX_CLEAR_COLOR ? BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A : 0;
  1280. state |= _clear.m_flags & BGFX_CLEAR_DEPTH ? BGFX_STATE_DEPTH_TEST_ALWAYS|BGFX_STATE_WRITE_Z : 0;
  1281. uint64_t stencil = 0;
  1282. stencil |= _clear.m_flags & BGFX_CLEAR_STENCIL ? 0
  1283. | BGFX_STENCIL_TEST_ALWAYS
  1284. | BGFX_STENCIL_FUNC_REF(_clear.m_stencil)
  1285. | BGFX_STENCIL_FUNC_RMASK(0xff)
  1286. | BGFX_STENCIL_OP_FAIL_S_REPLACE
  1287. | BGFX_STENCIL_OP_FAIL_Z_REPLACE
  1288. | BGFX_STENCIL_OP_PASS_Z_REPLACE
  1289. : 0
  1290. ;
  1291. setDepthStencilState(state, stencil);
  1292. uint32_t numMrt = 1;
  1293. FrameBufferHandle fbh = m_fbh;
  1294. if (isValid(fbh) )
  1295. {
  1296. const FrameBufferMtl& fb = m_frameBuffers[fbh.idx];
  1297. numMrt = bx::uint32_max(1, fb.m_num);
  1298. }
  1299. const ProgramMtl& program = m_program[_clearQuad.m_program[numMrt-1].idx];
  1300. RenderPipelineState pso = getPipelineState(
  1301. state
  1302. , 0
  1303. , fbh
  1304. , _clearQuad.m_vb->decl
  1305. , _clearQuad.m_program[numMrt-1].idx
  1306. , 0
  1307. );
  1308. m_renderCommandEncoder.setRenderPipelineState(pso);
  1309. uint32_t fragmentUniformBufferSize = program.m_fshConstantBufferSize;
  1310. m_uniformBufferFragmentOffset = m_uniformBufferVertexOffset;
  1311. if (fragmentUniformBufferSize)
  1312. {
  1313. m_uniformBufferFragmentOffset = BX_ALIGN_MASK(m_uniformBufferFragmentOffset, program.m_fshConstantBufferAlignmentMask);
  1314. m_renderCommandEncoder.setFragmentBuffer(m_uniformBuffer, m_uniformBufferFragmentOffset, 0);
  1315. }
  1316. if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags)
  1317. {
  1318. float mrtClear[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS][4];
  1319. for (uint32_t ii = 0; ii < numMrt; ++ii)
  1320. {
  1321. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[ii]);
  1322. bx::memCopy(mrtClear[ii], _palette[index], 16);
  1323. }
  1324. bx::memCopy( (uint8_t*)m_uniformBuffer.contents() + m_uniformBufferFragmentOffset
  1325. , mrtClear
  1326. , bx::uint32_min(fragmentUniformBufferSize, sizeof(mrtClear) )
  1327. );
  1328. }
  1329. else
  1330. {
  1331. float rgba[4] =
  1332. {
  1333. _clear.m_index[0]*1.0f/255.0f,
  1334. _clear.m_index[1]*1.0f/255.0f,
  1335. _clear.m_index[2]*1.0f/255.0f,
  1336. _clear.m_index[3]*1.0f/255.0f,
  1337. };
  1338. bx::memCopy( (uint8_t*)m_uniformBuffer.contents() + m_uniformBufferFragmentOffset
  1339. , rgba
  1340. , bx::uint32_min(fragmentUniformBufferSize, sizeof(rgba) )
  1341. );
  1342. }
  1343. m_uniformBufferFragmentOffset += fragmentUniformBufferSize;
  1344. m_uniformBufferVertexOffset = m_uniformBufferFragmentOffset;
  1345. const VertexBufferMtl& vb = m_vertexBuffers[_clearQuad.m_vb->handle.idx];
  1346. const VertexDecl& vertexDecl = m_vertexDecls[_clearQuad.m_vb->decl.idx];
  1347. const uint32_t stride = vertexDecl.m_stride;
  1348. const uint32_t offset = 0;
  1349. {
  1350. struct Vertex
  1351. {
  1352. float m_x;
  1353. float m_y;
  1354. float m_z;
  1355. };
  1356. Vertex* vertex = (Vertex*)_clearQuad.m_vb->data;
  1357. BX_CHECK(stride == sizeof(Vertex)
  1358. , "Stride/Vertex mismatch (stride %d, sizeof(Vertex) %d)"
  1359. , stride
  1360. , sizeof(Vertex)
  1361. );
  1362. BX_UNUSED(stride);
  1363. const float depth = _clear.m_depth;
  1364. vertex->m_x = -1.0f;
  1365. vertex->m_y = -1.0f;
  1366. vertex->m_z = depth;
  1367. vertex++;
  1368. vertex->m_x = 1.0f;
  1369. vertex->m_y = -1.0f;
  1370. vertex->m_z = depth;
  1371. vertex++;
  1372. vertex->m_x = -1.0f;
  1373. vertex->m_y = 1.0f;
  1374. vertex->m_z = depth;
  1375. vertex++;
  1376. vertex->m_x = 1.0f;
  1377. vertex->m_y = 1.0f;
  1378. vertex->m_z = depth;
  1379. }
  1380. m_vertexBuffers[_clearQuad.m_vb->handle.idx].update(0, 4*_clearQuad.m_decl.m_stride, _clearQuad.m_vb->data);
  1381. m_renderCommandEncoder.setCullMode(MTLCullModeNone);
  1382. m_renderCommandEncoder.setVertexBuffer(vb.getBuffer(), offset, 1);
  1383. m_renderCommandEncoder.drawPrimitives(MTLPrimitiveTypeTriangleStrip, 0, 4, 1);
  1384. }
  1385. void setFrameBuffer(RenderPassDescriptor renderPassDescriptor, FrameBufferHandle _fbh, bool _msaa = true)
  1386. {
  1387. if (!isValid(_fbh) )
  1388. {
  1389. if (NULL != m_backBufferColorMSAA)
  1390. {
  1391. renderPassDescriptor.colorAttachments[0].texture = m_backBufferColorMSAA;
  1392. renderPassDescriptor.colorAttachments[0].resolveTexture = NULL != m_screenshotTarget
  1393. ? m_screenshotTarget.m_obj
  1394. : currentDrawable().texture
  1395. ;
  1396. }
  1397. else
  1398. {
  1399. renderPassDescriptor.colorAttachments[0].texture = NULL != m_screenshotTarget
  1400. ? m_screenshotTarget.m_obj
  1401. : currentDrawable().texture
  1402. ;
  1403. }
  1404. renderPassDescriptor.depthAttachment.texture = m_backBufferDepth;
  1405. renderPassDescriptor.stencilAttachment.texture = m_backBufferStencil;
  1406. }
  1407. else
  1408. {
  1409. FrameBufferMtl& frameBuffer = m_frameBuffers[_fbh.idx];
  1410. for (uint32_t ii = 0; ii < frameBuffer.m_num; ++ii)
  1411. {
  1412. const TextureMtl& texture = m_textures[frameBuffer.m_colorHandle[ii].idx];
  1413. renderPassDescriptor.colorAttachments[ii].texture = texture.m_ptrMSAA
  1414. ? texture.m_ptrMSAA
  1415. : texture.m_ptr
  1416. ;
  1417. renderPassDescriptor.colorAttachments[ii].resolveTexture = texture.m_ptrMSAA
  1418. ? texture.m_ptr.m_obj
  1419. : NULL
  1420. ;
  1421. }
  1422. if (isValid(frameBuffer.m_depthHandle) )
  1423. {
  1424. const TextureMtl& texture = m_textures[frameBuffer.m_depthHandle.idx];
  1425. renderPassDescriptor.depthAttachment.texture = texture.m_ptrMSAA
  1426. ? texture.m_ptrMSAA
  1427. : texture.m_ptr
  1428. ;
  1429. renderPassDescriptor.stencilAttachment.texture = texture.m_ptrStencil;
  1430. if (texture.m_textureFormat == TextureFormat::D24S8)
  1431. {
  1432. if (texture.m_ptr.pixelFormat() == 255 /* Depth24Unorm_Stencil8 */
  1433. || texture.m_ptr.pixelFormat() == 260 /* Depth32Float_Stencil8 */)
  1434. {
  1435. renderPassDescriptor.stencilAttachment.texture = renderPassDescriptor.depthAttachment.texture;
  1436. }
  1437. else
  1438. {
  1439. renderPassDescriptor.stencilAttachment.texture = texture.m_ptrMSAA
  1440. ? texture.m_ptrMSAA
  1441. : texture.m_ptrStencil
  1442. ;
  1443. }
  1444. }
  1445. }
  1446. }
  1447. m_fbh = _fbh;
  1448. m_rtMsaa = _msaa;
  1449. }
  1450. void setDepthStencilState(uint64_t _state, uint64_t _stencil = 0)
  1451. {
  1452. _state &= BGFX_STATE_WRITE_Z|BGFX_STATE_DEPTH_TEST_MASK;
  1453. uint32_t fstencil = unpackStencil(0, _stencil);
  1454. uint32_t ref = (fstencil&BGFX_STENCIL_FUNC_REF_MASK)>>BGFX_STENCIL_FUNC_REF_SHIFT;
  1455. _stencil &= packStencil(~BGFX_STENCIL_FUNC_REF_MASK, ~BGFX_STENCIL_FUNC_REF_MASK);
  1456. bx::HashMurmur2A murmur;
  1457. murmur.begin();
  1458. murmur.add(_state);
  1459. murmur.add(_stencil);
  1460. uint32_t hash = murmur.end();
  1461. DepthStencilState dss = m_depthStencilStateCache.find(hash);
  1462. if (NULL == dss)
  1463. {
  1464. DepthStencilDescriptor desc = m_depthStencilDescriptor;
  1465. uint32_t func = (_state&BGFX_STATE_DEPTH_TEST_MASK)>>BGFX_STATE_DEPTH_TEST_SHIFT;
  1466. desc.depthWriteEnabled = !!(BGFX_STATE_WRITE_Z & _state);
  1467. desc.depthCompareFunction = s_cmpFunc[func];
  1468. uint32_t bstencil = unpackStencil(1, _stencil);
  1469. uint32_t frontAndBack = bstencil != BGFX_STENCIL_NONE && bstencil != fstencil;
  1470. bstencil = frontAndBack ? bstencil : fstencil;
  1471. if (0 != _stencil)
  1472. {
  1473. StencilDescriptor frontFaceDesc = m_frontFaceStencilDescriptor;
  1474. StencilDescriptor backfaceDesc = m_backFaceStencilDescriptor;
  1475. uint32_t readMask = (fstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  1476. uint32_t writeMask = 0xff;
  1477. frontFaceDesc.stencilFailureOperation = s_stencilOp[(fstencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT];
  1478. frontFaceDesc.depthFailureOperation = s_stencilOp[(fstencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT];
  1479. frontFaceDesc.depthStencilPassOperation = s_stencilOp[(fstencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT];
  1480. frontFaceDesc.stencilCompareFunction = s_cmpFunc[(fstencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT];
  1481. frontFaceDesc.readMask = readMask;
  1482. frontFaceDesc.writeMask = writeMask;
  1483. backfaceDesc.stencilFailureOperation = s_stencilOp[(bstencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT];
  1484. backfaceDesc.depthFailureOperation = s_stencilOp[(bstencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT];
  1485. backfaceDesc.depthStencilPassOperation = s_stencilOp[(bstencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT];
  1486. backfaceDesc.stencilCompareFunction = s_cmpFunc[(bstencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT];
  1487. backfaceDesc.readMask = readMask;
  1488. backfaceDesc.writeMask = writeMask;
  1489. desc.frontFaceStencil = frontFaceDesc;
  1490. desc.backFaceStencil = backfaceDesc;
  1491. }
  1492. else
  1493. {
  1494. desc.backFaceStencil = NULL;
  1495. desc.frontFaceStencil = NULL;
  1496. }
  1497. dss = m_device.newDepthStencilStateWithDescriptor(desc);
  1498. m_depthStencilStateCache.add(hash, dss);
  1499. }
  1500. m_renderCommandEncoder.setDepthStencilState(dss);
  1501. m_renderCommandEncoder.setStencilReferenceValue(ref);
  1502. }
  1503. RenderPipelineState getPipelineState(
  1504. uint64_t _state
  1505. , uint32_t _rgba
  1506. , FrameBufferHandle _fbh
  1507. , uint8_t _numStreams
  1508. , const VertexDecl** _vertexDecls
  1509. , uint16_t _programIdx
  1510. , uint8_t _numInstanceData
  1511. )
  1512. {
  1513. _state &= (0
  1514. | BGFX_STATE_BLEND_MASK
  1515. | BGFX_STATE_BLEND_EQUATION_MASK
  1516. | BGFX_STATE_WRITE_RGB
  1517. | BGFX_STATE_WRITE_A
  1518. | BGFX_STATE_BLEND_INDEPENDENT
  1519. | BGFX_STATE_MSAA
  1520. | BGFX_STATE_BLEND_ALPHA_TO_COVERAGE
  1521. );
  1522. const bool independentBlendEnable = !!(BGFX_STATE_BLEND_INDEPENDENT & _state);
  1523. ProgramMtl& program = m_program[_programIdx];
  1524. bx::HashMurmur2A murmur;
  1525. murmur.begin();
  1526. murmur.add(_state);
  1527. murmur.add(independentBlendEnable ? _rgba : 0);
  1528. murmur.add(_numInstanceData);
  1529. if (!isValid(_fbh) )
  1530. {
  1531. murmur.add(m_backBufferPixelFormatHash);
  1532. }
  1533. else
  1534. {
  1535. FrameBufferMtl& frameBuffer = m_frameBuffers[_fbh.idx];
  1536. murmur.add(frameBuffer.m_pixelFormatHash);
  1537. }
  1538. murmur.add(program.m_vsh->m_hash);
  1539. if (NULL != program.m_fsh)
  1540. {
  1541. murmur.add(program.m_fsh->m_hash);
  1542. }
  1543. for (uint8_t ii = 0; ii < _numStreams; ++ii)
  1544. {
  1545. murmur.add(_vertexDecls[ii]->m_hash);
  1546. }
  1547. uint32_t hash = murmur.end();
  1548. RenderPipelineState pso = m_pipelineStateCache.find(hash);
  1549. if (NULL == pso)
  1550. {
  1551. RenderPipelineDescriptor pd = m_renderPipelineDescriptor;
  1552. reset(pd);
  1553. pd.alphaToCoverageEnabled = !!(BGFX_STATE_BLEND_ALPHA_TO_COVERAGE & _state);
  1554. uint32_t frameBufferAttachment = 1;
  1555. if (!isValid(_fbh) )
  1556. {
  1557. pd.sampleCount = NULL != m_backBufferColorMSAA
  1558. ? m_backBufferColorMSAA.sampleCount()
  1559. : 1
  1560. ;
  1561. pd.colorAttachments[0].pixelFormat = currentDrawable().texture.pixelFormat;
  1562. pd.depthAttachmentPixelFormat = m_backBufferDepth.m_obj.pixelFormat;
  1563. pd.stencilAttachmentPixelFormat = m_backBufferStencil.m_obj.pixelFormat;
  1564. }
  1565. else
  1566. {
  1567. const FrameBufferMtl& frameBuffer = m_frameBuffers[_fbh.idx];
  1568. frameBufferAttachment = frameBuffer.m_num;
  1569. for (uint32_t ii = 0; ii < frameBuffer.m_num; ++ii)
  1570. {
  1571. const TextureMtl& texture = m_textures[frameBuffer.m_colorHandle[ii].idx];
  1572. pd.sampleCount = NULL != texture.m_ptrMSAA
  1573. ? texture.m_ptrMSAA.sampleCount()
  1574. : 1
  1575. ;
  1576. pd.colorAttachments[ii].pixelFormat = texture.m_ptr.m_obj.pixelFormat;
  1577. }
  1578. if (isValid(frameBuffer.m_depthHandle) )
  1579. {
  1580. const TextureMtl& texture = m_textures[frameBuffer.m_depthHandle.idx];
  1581. pd.depthAttachmentPixelFormat = texture.m_ptr.m_obj.pixelFormat;
  1582. if (NULL != texture.m_ptrStencil)
  1583. {
  1584. pd.stencilAttachmentPixelFormat = texture.m_ptrStencil.m_obj.pixelFormat;
  1585. }
  1586. else
  1587. {
  1588. if (texture.m_textureFormat == TextureFormat::D24S8)
  1589. {
  1590. pd.stencilAttachmentPixelFormat = texture.m_ptr.m_obj.pixelFormat;
  1591. }
  1592. }
  1593. }
  1594. }
  1595. const uint32_t blend = uint32_t( (_state&BGFX_STATE_BLEND_MASK )>>BGFX_STATE_BLEND_SHIFT);
  1596. const uint32_t equation = uint32_t( (_state&BGFX_STATE_BLEND_EQUATION_MASK)>>BGFX_STATE_BLEND_EQUATION_SHIFT);
  1597. const uint32_t srcRGB = (blend )&0xf;
  1598. const uint32_t dstRGB = (blend>> 4)&0xf;
  1599. const uint32_t srcA = (blend>> 8)&0xf;
  1600. const uint32_t dstA = (blend>>12)&0xf;
  1601. const uint32_t equRGB = (equation )&0x7;
  1602. const uint32_t equA = (equation>>3)&0x7;
  1603. uint8_t writeMask = 0;
  1604. writeMask |= (_state&BGFX_STATE_WRITE_R) ? MTLColorWriteMaskRed : 0;
  1605. writeMask |= (_state&BGFX_STATE_WRITE_G) ? MTLColorWriteMaskGreen : 0;
  1606. writeMask |= (_state&BGFX_STATE_WRITE_B) ? MTLColorWriteMaskBlue : 0;
  1607. writeMask |= (_state&BGFX_STATE_WRITE_A) ? MTLColorWriteMaskAlpha : 0;
  1608. for (uint32_t ii = 0; ii < (independentBlendEnable ? 1 : frameBufferAttachment); ++ii)
  1609. {
  1610. RenderPipelineColorAttachmentDescriptor drt = pd.colorAttachments[ii];
  1611. drt.blendingEnabled = !!(BGFX_STATE_BLEND_MASK & _state);
  1612. drt.sourceRGBBlendFactor = s_blendFactor[srcRGB][0];
  1613. drt.destinationRGBBlendFactor = s_blendFactor[dstRGB][0];
  1614. drt.rgbBlendOperation = s_blendEquation[equRGB];
  1615. drt.sourceAlphaBlendFactor = s_blendFactor[srcA][1];
  1616. drt.destinationAlphaBlendFactor = s_blendFactor[dstA][1];
  1617. drt.alphaBlendOperation = s_blendEquation[equA];
  1618. drt.writeMask = writeMask;
  1619. }
  1620. if (independentBlendEnable)
  1621. {
  1622. for (uint32_t ii = 1, rgba = _rgba; ii < frameBufferAttachment; ++ii, rgba >>= 11)
  1623. {
  1624. RenderPipelineColorAttachmentDescriptor drt = pd.colorAttachments[ii];
  1625. drt.blendingEnabled = 0 != (rgba&0x7ff);
  1626. const uint32_t src = (rgba )&0xf;
  1627. const uint32_t dst = (rgba>>4)&0xf;
  1628. const uint32_t equationIndex = (rgba>>8)&0x7;
  1629. drt.sourceRGBBlendFactor = s_blendFactor[src][0];
  1630. drt.destinationRGBBlendFactor = s_blendFactor[dst][0];
  1631. drt.rgbBlendOperation = s_blendEquation[equationIndex];
  1632. drt.sourceAlphaBlendFactor = s_blendFactor[src][1];
  1633. drt.destinationAlphaBlendFactor = s_blendFactor[dst][1];
  1634. drt.alphaBlendOperation = s_blendEquation[equationIndex];
  1635. drt.writeMask = writeMask;
  1636. }
  1637. }
  1638. pd.vertexFunction = program.m_vsh->m_function;
  1639. pd.fragmentFunction = program.m_fsh != NULL ? program.m_fsh->m_function : NULL;
  1640. VertexDescriptor vertexDesc = m_vertexDescriptor;
  1641. reset(vertexDesc);
  1642. uint8_t stream = 0;
  1643. for (; stream < _numStreams; ++stream)
  1644. {
  1645. const VertexDecl& vertexDecl = *_vertexDecls[stream];
  1646. for (uint32_t ii = 0; Attrib::Count != program.m_used[ii]; ++ii)
  1647. {
  1648. Attrib::Enum attr = Attrib::Enum(program.m_used[ii]);
  1649. uint32_t loc = program.m_attributes[attr];
  1650. uint8_t num;
  1651. AttribType::Enum type;
  1652. bool normalized;
  1653. bool asInt;
  1654. vertexDecl.decode(attr, num, type, normalized, asInt);
  1655. BX_CHECK(num <= 4, "num must be <= 4");
  1656. if (UINT16_MAX != vertexDecl.m_attributes[attr])
  1657. {
  1658. vertexDesc.attributes[loc].format = s_attribType[type][num-1][normalized?1:0];
  1659. vertexDesc.attributes[loc].bufferIndex = stream+1;
  1660. vertexDesc.attributes[loc].offset = vertexDecl.m_offset[attr];
  1661. BX_TRACE("attrib: %s format: %d offset: %d", s_attribName[attr], (int)vertexDesc.attributes[loc].format, (int)vertexDesc.attributes[loc].offset);
  1662. }
  1663. // else
  1664. // { // NOTE: missing attribute: using dummy attribute with smallest possible size
  1665. // vertexDesc.attributes[loc].format = MTLVertexFormatUChar2;
  1666. // vertexDesc.attributes[loc].bufferIndex = 1;
  1667. // vertexDesc.attributes[loc].offset = 0;
  1668. // }
  1669. }
  1670. vertexDesc.layouts[stream+1].stride = vertexDecl.getStride();
  1671. vertexDesc.layouts[stream+1].stepFunction = MTLVertexStepFunctionPerVertex;
  1672. }
  1673. if (0 < _numInstanceData)
  1674. {
  1675. for (uint32_t ii = 0; UINT16_MAX != program.m_instanceData[ii]; ++ii)
  1676. {
  1677. uint32_t loc = program.m_instanceData[ii];
  1678. vertexDesc.attributes[loc].format = MTLVertexFormatFloat4;
  1679. vertexDesc.attributes[loc].bufferIndex = stream+1;
  1680. vertexDesc.attributes[loc].offset = ii*16;
  1681. }
  1682. vertexDesc.layouts[stream+1].stride = _numInstanceData * 16;
  1683. vertexDesc.layouts[stream+1].stepFunction = MTLVertexStepFunctionPerInstance;
  1684. vertexDesc.layouts[stream+1].stepRate = 1;
  1685. }
  1686. pd.vertexDescriptor = vertexDesc;
  1687. if (program.m_processedUniforms)
  1688. {
  1689. pso = m_device.newRenderPipelineStateWithDescriptor(pd);
  1690. }
  1691. else
  1692. {
  1693. program.m_numPredefined = 0;
  1694. RenderPipelineReflection reflection = NULL;
  1695. pso = m_device.newRenderPipelineStateWithDescriptor(pd, MTLPipelineOptionBufferTypeInfo, &reflection);
  1696. if (NULL != reflection)
  1697. {
  1698. for (uint32_t shaderType = 0; shaderType < 2; ++shaderType)
  1699. {
  1700. UniformBuffer*& constantBuffer = shaderType == 0
  1701. ? program.m_vshConstantBuffer
  1702. : program.m_fshConstantBuffer
  1703. ;
  1704. uint8_t fragmentBit = (1 == shaderType ? BGFX_UNIFORM_FRAGMENTBIT : 0);
  1705. for (MTLArgument* arg in (shaderType == 0 ? reflection.vertexArguments : reflection.fragmentArguments) )
  1706. {
  1707. BX_TRACE("arg: %s type:%d", utf8String(arg.name), arg.type);
  1708. if (arg.active)
  1709. {
  1710. if (arg.type == MTLArgumentTypeBuffer
  1711. && 0 == bx::strCmp(utf8String(arg.name), SHADER_UNIFORM_NAME) )
  1712. {
  1713. BX_CHECK( arg.index == 0, "Uniform buffer must be in the buffer slot 0.");
  1714. BX_CHECK( MTLDataTypeStruct == arg.bufferDataType, "%s's type must be a struct",SHADER_UNIFORM_NAME );
  1715. if (MTLDataTypeStruct == arg.bufferDataType)
  1716. {
  1717. if (shaderType == 0)
  1718. {
  1719. program.m_vshConstantBufferSize = (uint32_t)arg.bufferDataSize;
  1720. program.m_vshConstantBufferAlignmentMask = (uint32_t)arg.bufferAlignment - 1;
  1721. }
  1722. else
  1723. {
  1724. program.m_fshConstantBufferSize = (uint32_t)arg.bufferDataSize;
  1725. program.m_fshConstantBufferAlignmentMask = (uint32_t)arg.bufferAlignment - 1;
  1726. }
  1727. for (MTLStructMember* uniform in arg.bufferStructType.members )
  1728. {
  1729. const char* name = utf8String(uniform.name);
  1730. BX_TRACE("uniform: %s type:%d", name, uniform.dataType);
  1731. MTLDataType dataType = uniform.dataType;
  1732. uint32_t num = 1;
  1733. if (dataType == MTLDataTypeArray)
  1734. {
  1735. dataType = uniform.arrayType.elementType;
  1736. num = (uint32_t)uniform.arrayType.arrayLength;
  1737. }
  1738. switch (dataType)
  1739. {
  1740. case MTLDataTypeFloat4: num *= 1; break;
  1741. case MTLDataTypeFloat4x4: num *= 4; break;
  1742. case MTLDataTypeFloat3x3: num *= 3; break;
  1743. default:
  1744. BX_WARN(0, "Unsupported uniform MTLDataType: %d", uniform.dataType);
  1745. break;
  1746. }
  1747. PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
  1748. if (PredefinedUniform::Count != predefined)
  1749. {
  1750. program.m_predefined[program.m_numPredefined].m_loc = uint32_t(uniform.offset);
  1751. program.m_predefined[program.m_numPredefined].m_count = uint16_t(num);
  1752. program.m_predefined[program.m_numPredefined].m_type = uint8_t(predefined|fragmentBit);
  1753. ++program.m_numPredefined;
  1754. }
  1755. else
  1756. {
  1757. const UniformRegInfo* info = m_uniformReg.find(name);
  1758. BX_WARN(NULL != info, "User defined uniform '%s' is not found, it won't be set.", name);
  1759. if (NULL != info)
  1760. {
  1761. if (NULL == constantBuffer)
  1762. {
  1763. constantBuffer = UniformBuffer::create(1024);
  1764. }
  1765. UniformType::Enum type = convertMtlType(dataType);
  1766. constantBuffer->writeUniformHandle( (UniformType::Enum)(type|fragmentBit), uint32_t(uniform.offset), info->m_handle, uint16_t(num) );
  1767. BX_TRACE("store %s %d offset:%d", name, info->m_handle, uint32_t(uniform.offset) );
  1768. }
  1769. }
  1770. }
  1771. }
  1772. }
  1773. else if (arg.type == MTLArgumentTypeTexture)
  1774. {
  1775. const char* name = utf8String(arg.name);
  1776. const UniformRegInfo* info = m_uniformReg.find(name);
  1777. BX_WARN(NULL != info, "User defined uniform '%s' is not found, it won't be set.", name);
  1778. if (NULL != info)
  1779. {
  1780. if (program.m_samplerCount >= BGFX_CONFIG_MAX_TEXTURE_SAMPLERS)
  1781. {
  1782. BX_WARN(NULL != info, "Too many samplers in shader(only %d is supported). User defined uniform '%s' won't be set.", BGFX_CONFIG_MAX_TEXTURE_SAMPLERS, name);
  1783. }
  1784. else
  1785. {
  1786. program.m_samplers[program.m_samplerCount].m_index = uint32_t(arg.index);
  1787. program.m_samplers[program.m_samplerCount].m_uniform = info->m_handle;
  1788. program.m_samplers[program.m_samplerCount].m_fragment = fragmentBit ? 1 : 0;
  1789. ++program.m_samplerCount;
  1790. BX_TRACE("texture %s %d index:%d", name, info->m_handle, uint32_t(arg.index) );
  1791. }
  1792. }
  1793. }
  1794. else if (arg.type == MTLArgumentTypeSampler)
  1795. {
  1796. BX_TRACE("sampler: %s index:%d", utf8String(arg.name), arg.index);
  1797. }
  1798. }
  1799. }
  1800. if (NULL != constantBuffer)
  1801. {
  1802. constantBuffer->finish();
  1803. }
  1804. }
  1805. }
  1806. program.m_processedUniforms = true;
  1807. }
  1808. m_pipelineStateCache.add(hash, pso);
  1809. }
  1810. return pso;
  1811. }
  1812. RenderPipelineState getPipelineState(
  1813. uint64_t _state
  1814. , uint32_t _rgba
  1815. , FrameBufferHandle _fbh
  1816. , VertexDeclHandle _declHandle
  1817. , uint16_t _programIdx
  1818. , uint16_t _numInstanceData
  1819. )
  1820. {
  1821. const VertexDecl* decl = &m_vertexDecls[_declHandle.idx];
  1822. return getPipelineState(
  1823. _state
  1824. , _rgba
  1825. , _fbh
  1826. , 1
  1827. , &decl
  1828. , _programIdx
  1829. , _numInstanceData
  1830. );
  1831. }
  1832. SamplerState getSamplerState(uint32_t _flags)
  1833. {
  1834. _flags &= BGFX_TEXTURE_SAMPLER_BITS_MASK;
  1835. SamplerState sampler = m_samplerStateCache.find(_flags);
  1836. if (NULL == sampler)
  1837. {
  1838. m_samplerDescriptor.sAddressMode = s_textureAddress[(_flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT];
  1839. m_samplerDescriptor.tAddressMode = s_textureAddress[(_flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT];
  1840. m_samplerDescriptor.rAddressMode = s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT];
  1841. m_samplerDescriptor.minFilter = s_textureFilterMinMag[(_flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT];
  1842. m_samplerDescriptor.magFilter = s_textureFilterMinMag[(_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT];
  1843. m_samplerDescriptor.mipFilter = s_textureFilterMip[(_flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT];
  1844. m_samplerDescriptor.lodMinClamp = 0;
  1845. m_samplerDescriptor.lodMaxClamp = FLT_MAX;
  1846. m_samplerDescriptor.normalizedCoordinates = TRUE;
  1847. m_samplerDescriptor.maxAnisotropy = (0 != (_flags & (BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC) ) ) ? m_maxAnisotropy : 1;
  1848. if (m_macOS11Runtime
  1849. || [m_device supportsFeatureSet:(MTLFeatureSet)4 /*MTLFeatureSet_iOS_GPUFamily3_v1*/])
  1850. {
  1851. const uint32_t cmpFunc = (_flags&BGFX_TEXTURE_COMPARE_MASK)>>BGFX_TEXTURE_COMPARE_SHIFT;
  1852. m_samplerDescriptor.compareFunction = 0 == cmpFunc ? MTLCompareFunctionNever : s_cmpFunc[cmpFunc];
  1853. }
  1854. sampler = m_device.newSamplerStateWithDescriptor(m_samplerDescriptor);
  1855. m_samplerStateCache.add(_flags, sampler);
  1856. }
  1857. return sampler;
  1858. }
  1859. bool isVisible(Frame* _render, OcclusionQueryHandle _handle, bool _visible)
  1860. {
  1861. m_occlusionQuery.resolve(_render);
  1862. return _visible == (0 != _render->m_occlusion[_handle.idx]);
  1863. }
  1864. BlitCommandEncoder getBlitCommandEncoder()
  1865. {
  1866. if (m_blitCommandEncoder == NULL)
  1867. {
  1868. if (m_commandBuffer == NULL)
  1869. {
  1870. m_commandBuffer = m_cmd.alloc();
  1871. }
  1872. m_blitCommandEncoder = m_commandBuffer.blitCommandEncoder();
  1873. }
  1874. return m_blitCommandEncoder;
  1875. }
  1876. id<CAMetalDrawable> currentDrawable()
  1877. {
  1878. if (m_drawable == nil)
  1879. {
  1880. m_drawable = m_metalLayer.nextDrawable;
  1881. retain(m_drawable); // keep alive to be useable at 'flip'
  1882. }
  1883. return m_drawable;
  1884. }
  1885. Device m_device;
  1886. OcclusionQueryMTL m_occlusionQuery;
  1887. TimerQueryMtl m_gpuTimer;
  1888. CommandQueueMtl m_cmd;
  1889. CAMetalLayer* m_metalLayer;
  1890. Texture m_backBufferColorMSAA;
  1891. Texture m_backBufferDepth;
  1892. Texture m_backBufferStencil;
  1893. uint32_t m_backBufferPixelFormatHash;
  1894. uint32_t m_maxAnisotropy;
  1895. bool m_iOS9Runtime;
  1896. bool m_macOS11Runtime;
  1897. bool m_hasPixelFormatDepth32Float_Stencil8;
  1898. Buffer m_uniformBuffer;
  1899. Buffer m_uniformBuffers[MTL_MAX_FRAMES_IN_FLIGHT];
  1900. uint32_t m_uniformBufferVertexOffset;
  1901. uint32_t m_uniformBufferFragmentOffset;
  1902. uint8_t m_bufferIndex;
  1903. uint16_t m_numWindows;
  1904. FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  1905. IndexBufferMtl m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS];
  1906. VertexBufferMtl m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
  1907. ShaderMtl m_shaders[BGFX_CONFIG_MAX_SHADERS];
  1908. ProgramMtl m_program[BGFX_CONFIG_MAX_PROGRAMS];
  1909. TextureMtl m_textures[BGFX_CONFIG_MAX_TEXTURES];
  1910. FrameBufferMtl m_frameBuffers[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  1911. VertexDecl m_vertexDecls[BGFX_CONFIG_MAX_VERTEX_DECLS];
  1912. UniformRegistry m_uniformReg;
  1913. void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS];
  1914. StateCacheT<RenderPipelineState> m_pipelineStateCache;
  1915. StateCacheT<DepthStencilState> m_depthStencilStateCache;
  1916. StateCacheT<SamplerState> m_samplerStateCache;
  1917. TextVideoMem m_textVideoMem;
  1918. FrameBufferHandle m_fbh;
  1919. bool m_rtMsaa;
  1920. Resolution m_resolution;
  1921. void* m_capture;
  1922. uint32_t m_captureSize;
  1923. // descriptors
  1924. RenderPipelineDescriptor m_renderPipelineDescriptor;
  1925. DepthStencilDescriptor m_depthStencilDescriptor;
  1926. StencilDescriptor m_frontFaceStencilDescriptor;
  1927. StencilDescriptor m_backFaceStencilDescriptor;
  1928. VertexDescriptor m_vertexDescriptor;
  1929. TextureDescriptor m_textureDescriptor;
  1930. SamplerDescriptor m_samplerDescriptor;
  1931. // currently active objects data
  1932. id <CAMetalDrawable> m_drawable;
  1933. bool m_saveScreenshot;
  1934. Texture m_screenshotTarget;
  1935. ShaderMtl m_screenshotBlitProgramVsh;
  1936. ShaderMtl m_screenshotBlitProgramFsh;
  1937. ProgramMtl m_screenshotBlitProgram;
  1938. RenderPipelineState m_screenshotBlitRenderPipelineState;
  1939. CommandBuffer m_commandBuffer;
  1940. CommandBuffer m_prevCommandBuffer;
  1941. BlitCommandEncoder m_blitCommandEncoder;
  1942. RenderCommandEncoder m_renderCommandEncoder;
  1943. FrameBufferHandle m_renderCommandEncoderFrameBufferHandle;
  1944. };
  1945. static RendererContextMtl* s_renderMtl;
  1946. RendererContextI* rendererCreate(const Init& _init)
  1947. {
  1948. s_renderMtl = BX_NEW(g_allocator, RendererContextMtl);
  1949. if (!s_renderMtl->init(_init) )
  1950. {
  1951. BX_DELETE(g_allocator, s_renderMtl);
  1952. s_renderMtl = NULL;
  1953. }
  1954. return s_renderMtl;
  1955. }
  1956. void rendererDestroy()
  1957. {
  1958. s_renderMtl->shutdown();
  1959. BX_DELETE(g_allocator, s_renderMtl);
  1960. s_renderMtl = NULL;
  1961. }
  1962. void writeString(bx::WriterI* _writer, const char* _str)
  1963. {
  1964. bx::write(_writer, _str, (int32_t)bx::strLen(_str) );
  1965. }
  1966. void ShaderMtl::create(const Memory* _mem)
  1967. {
  1968. bx::MemoryReader reader(_mem->data, _mem->size);
  1969. uint32_t magic;
  1970. bx::read(&reader, magic);
  1971. switch (magic)
  1972. {
  1973. case BGFX_CHUNK_MAGIC_CSH:
  1974. case BGFX_CHUNK_MAGIC_FSH:
  1975. case BGFX_CHUNK_MAGIC_VSH:
  1976. break;
  1977. default:
  1978. BGFX_FATAL(false, Fatal::InvalidShader, "Unknown shader format %x.", magic);
  1979. break;
  1980. }
  1981. uint32_t iohash;
  1982. bx::read(&reader, iohash);
  1983. uint16_t count;
  1984. bx::read(&reader, count);
  1985. BX_TRACE("%s Shader consts %d"
  1986. , BGFX_CHUNK_MAGIC_FSH == magic ? "Fragment" : BGFX_CHUNK_MAGIC_VSH == magic ? "Vertex" : "Compute"
  1987. , count
  1988. );
  1989. for (uint32_t ii = 0; ii < count; ++ii)
  1990. {
  1991. uint8_t nameSize;
  1992. bx::read(&reader, nameSize);
  1993. char name[256];
  1994. bx::read(&reader, &name, nameSize);
  1995. name[nameSize] = '\0';
  1996. uint8_t type;
  1997. bx::read(&reader, type);
  1998. uint8_t num;
  1999. bx::read(&reader, num);
  2000. uint16_t regIndex;
  2001. bx::read(&reader, regIndex);
  2002. uint16_t regCount;
  2003. bx::read(&reader, regCount);
  2004. }
  2005. uint32_t shaderSize;
  2006. bx::read(&reader, shaderSize);
  2007. const char* code = (const char*)reader.getDataPtr();
  2008. bx::skip(&reader, shaderSize+1);
  2009. Library lib = s_renderMtl->m_device.newLibraryWithSource(code);
  2010. if (NULL != lib)
  2011. {
  2012. m_function = lib.newFunctionWithName(SHADER_FUNCTION_NAME);
  2013. release(lib);
  2014. }
  2015. BGFX_FATAL(NULL != m_function
  2016. , bgfx::Fatal::InvalidShader
  2017. , "Failed to create %s shader."
  2018. , BGFX_CHUNK_MAGIC_FSH == magic ? "Fragment" : BGFX_CHUNK_MAGIC_VSH == magic ? "Vertex" : "Compute"
  2019. );
  2020. bx::HashMurmur2A murmur;
  2021. murmur.begin();
  2022. murmur.add(iohash);
  2023. murmur.add(code, shaderSize);
  2024. // murmur.add(numAttrs);
  2025. // murmur.add(m_attrMask, numAttrs);
  2026. m_hash = murmur.end();
  2027. }
  2028. void ProgramMtl::create(const ShaderMtl* _vsh, const ShaderMtl* _fsh)
  2029. {
  2030. BX_CHECK(NULL != _vsh->m_function.m_obj, "Vertex shader doesn't exist.");
  2031. m_vsh = _vsh;
  2032. m_fsh = _fsh;
  2033. // get attributes
  2034. bx::memSet(m_attributes, 0xff, sizeof(m_attributes) );
  2035. uint32_t used = 0;
  2036. uint32_t instUsed = 0;
  2037. if (NULL != _vsh->m_function.m_obj)
  2038. {
  2039. for (MTLVertexAttribute* attrib in _vsh->m_function.m_obj.vertexAttributes)
  2040. {
  2041. if (attrib.active)
  2042. {
  2043. const char* name = utf8String(attrib.name);
  2044. uint32_t loc = (uint32_t)attrib.attributeIndex;
  2045. BX_TRACE("attr %s: %d", name, loc);
  2046. for (uint8_t ii = 0; ii < Attrib::Count; ++ii)
  2047. {
  2048. if (0 == bx::strCmp(s_attribName[ii],name) )
  2049. {
  2050. m_attributes[ii] = loc;
  2051. m_used[used++] = ii;
  2052. break;
  2053. }
  2054. }
  2055. for (uint32_t ii = 0; ii < BX_COUNTOF(s_instanceDataName); ++ii)
  2056. {
  2057. if (0 == bx::strCmp(s_instanceDataName[ii],name) )
  2058. {
  2059. m_instanceData[instUsed++] = loc;
  2060. }
  2061. }
  2062. }
  2063. }
  2064. }
  2065. m_used[used] = Attrib::Count;
  2066. m_instanceData[instUsed] = UINT16_MAX;
  2067. }
  2068. void ProgramMtl::destroy()
  2069. {
  2070. m_vsh = NULL;
  2071. m_fsh = NULL;
  2072. if (NULL != m_vshConstantBuffer)
  2073. {
  2074. UniformBuffer::destroy(m_vshConstantBuffer);
  2075. m_vshConstantBuffer = NULL;
  2076. }
  2077. if (NULL != m_fshConstantBuffer)
  2078. {
  2079. UniformBuffer::destroy(m_fshConstantBuffer);
  2080. m_fshConstantBuffer = NULL;
  2081. }
  2082. m_vshConstantBufferSize = 0;
  2083. m_vshConstantBufferAlignmentMask = 0;
  2084. m_fshConstantBufferSize = 0;
  2085. m_fshConstantBufferAlignmentMask = 0;
  2086. m_samplerCount = 0;
  2087. m_processedUniforms = false;
  2088. m_numPredefined = 0;
  2089. }
  2090. void BufferMtl::create(uint32_t _size, void* _data, uint16_t _flags, uint16_t _stride, bool _vertex)
  2091. {
  2092. BX_UNUSED(_stride, _vertex);
  2093. m_size = _size;
  2094. m_flags = _flags;
  2095. m_dynamic = (NULL == _data);
  2096. if (NULL == _data)
  2097. {
  2098. for (uint32_t ii = 0; ii < MTL_MAX_FRAMES_IN_FLIGHT; ++ii)
  2099. {
  2100. m_buffers[ii] = s_renderMtl->m_device.newBufferWithLength(_size, 0);
  2101. }
  2102. }
  2103. else
  2104. {
  2105. m_buffers[0] = s_renderMtl->m_device.newBufferWithBytes(_data, _size, 0);
  2106. }
  2107. }
  2108. void BufferMtl::update(uint32_t _offset, uint32_t _size, void* _data, bool _discard)
  2109. {
  2110. BX_UNUSED(_discard);
  2111. if (m_dynamic
  2112. && _discard)
  2113. {
  2114. m_bufferIndex = (m_bufferIndex + 1) % MTL_MAX_FRAMES_IN_FLIGHT;
  2115. bx::memCopy( (uint8_t*)getBuffer().contents() + _offset, _data, _size);
  2116. }
  2117. else if (NULL != s_renderMtl->m_renderCommandEncoder)
  2118. {
  2119. s_renderMtl->m_cmd.release(m_buffers[m_bufferIndex]);
  2120. if (_offset == 0
  2121. && _size == m_size)
  2122. {
  2123. m_buffers[m_bufferIndex] = s_renderMtl->m_device.newBufferWithBytes(_data, _size, 0);
  2124. }
  2125. else
  2126. {
  2127. const void* oldContent = m_buffers[m_bufferIndex].contents();
  2128. m_buffers[m_bufferIndex] = s_renderMtl->m_device.newBufferWithBytes(oldContent, m_size, 0);
  2129. bx::memCopy( (uint8_t*)m_buffers[m_bufferIndex].contents() + _offset, _data, _size);
  2130. }
  2131. }
  2132. else
  2133. {
  2134. BlitCommandEncoder bce = s_renderMtl->getBlitCommandEncoder();
  2135. Buffer temp = s_renderMtl->m_device.newBufferWithBytes(_data, _size, 0);
  2136. bce.copyFromBuffer(temp, 0, getBuffer(), _offset, _size);
  2137. release(temp);
  2138. }
  2139. }
  2140. void VertexBufferMtl::create(uint32_t _size, void* _data, VertexDeclHandle _declHandle, uint16_t _flags)
  2141. {
  2142. m_decl = _declHandle;
  2143. uint16_t stride = isValid(_declHandle)
  2144. ? s_renderMtl->m_vertexDecls[_declHandle.idx].m_stride
  2145. : 0
  2146. ;
  2147. BufferMtl::create(_size, _data, _flags, stride, true);
  2148. }
  2149. void TextureMtl::create(const Memory* _mem, uint32_t _flags, uint8_t _skip)
  2150. {
  2151. m_sampler = s_renderMtl->getSamplerState(_flags);
  2152. bimg::ImageContainer imageContainer;
  2153. if (bimg::imageParse(imageContainer, _mem->data, _mem->size) )
  2154. {
  2155. uint8_t numMips = imageContainer.m_numMips;
  2156. const uint8_t startLod = uint8_t(bx::uint32_min(_skip, numMips-1) );
  2157. numMips -= startLod;
  2158. const bimg::ImageBlockInfo& blockInfo = getBlockInfo(bimg::TextureFormat::Enum(imageContainer.m_format) );
  2159. const uint32_t textureWidth = bx::uint32_max(blockInfo.blockWidth, imageContainer.m_width >>startLod);
  2160. const uint32_t textureHeight = bx::uint32_max(blockInfo.blockHeight, imageContainer.m_height>>startLod);
  2161. const uint16_t numLayers = imageContainer.m_numLayers;
  2162. m_flags = _flags;
  2163. m_width = textureWidth;
  2164. m_height = textureHeight;
  2165. m_depth = imageContainer.m_depth;
  2166. m_requestedFormat = uint8_t(imageContainer.m_format);
  2167. m_textureFormat = uint8_t(getViableTextureFormat(imageContainer) );
  2168. const bool convert = m_textureFormat != m_requestedFormat;
  2169. const uint8_t bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(m_textureFormat) );
  2170. TextureDescriptor desc = s_renderMtl->m_textureDescriptor;
  2171. if (1 < numLayers)
  2172. {
  2173. if (imageContainer.m_cubeMap)
  2174. {
  2175. desc.textureType = MTLTextureType(6); // MTLTextureTypeCubeArray
  2176. m_type = TextureCube;
  2177. }
  2178. else
  2179. {
  2180. desc.textureType = MTLTextureType2DArray;
  2181. m_type = Texture2D;
  2182. }
  2183. }
  2184. else if (imageContainer.m_cubeMap)
  2185. {
  2186. desc.textureType = MTLTextureTypeCube;
  2187. m_type = TextureCube;
  2188. }
  2189. else if (imageContainer.m_depth > 1)
  2190. {
  2191. desc.textureType = MTLTextureType3D;
  2192. m_type = Texture3D;
  2193. }
  2194. else
  2195. {
  2196. desc.textureType = MTLTextureType2D;
  2197. m_type = Texture2D;
  2198. }
  2199. m_numMips = numMips;
  2200. const uint16_t numSides = numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  2201. const bool compressed = bimg::isCompressed(bimg::TextureFormat::Enum(m_textureFormat) );
  2202. const bool writeOnly = 0 != (_flags&BGFX_TEXTURE_RT_WRITE_ONLY);
  2203. const bool computeWrite = 0 != (_flags&BGFX_TEXTURE_COMPUTE_WRITE);
  2204. const bool renderTarget = 0 != (_flags&BGFX_TEXTURE_RT_MASK);
  2205. const bool srgb = 0 != (_flags&BGFX_TEXTURE_SRGB);
  2206. BX_TRACE("Texture %3d: %s (requested: %s), layers %d, %dx%d%s RT[%c], WO[%c], CW[%c], sRGB[%c]"
  2207. , this - s_renderMtl->m_textures
  2208. , getName( (TextureFormat::Enum)m_textureFormat)
  2209. , getName( (TextureFormat::Enum)m_requestedFormat)
  2210. , numLayers
  2211. , textureWidth
  2212. , textureHeight
  2213. , imageContainer.m_cubeMap ? "x6" : ""
  2214. , renderTarget ? 'x' : '.'
  2215. , writeOnly ? 'x' : '.'
  2216. , computeWrite ? 'x' : '.'
  2217. , srgb ? 'x' : '.'
  2218. );
  2219. const uint32_t msaaQuality = bx::uint32_satsub( (_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT, 1);
  2220. int sampleCount = s_msaa[msaaQuality];
  2221. MTLPixelFormat format = MTLPixelFormatInvalid;
  2222. if (srgb)
  2223. {
  2224. format = s_textureFormat[m_textureFormat].m_fmtSrgb;
  2225. BX_WARN(format != MTLPixelFormatInvalid
  2226. , "sRGB not supported for texture format %d"
  2227. , m_textureFormat
  2228. );
  2229. }
  2230. if (format == MTLPixelFormatInvalid)
  2231. {
  2232. // not swizzled and not sRGB, or sRGB unsupported
  2233. format = s_textureFormat[m_textureFormat].m_fmt;
  2234. }
  2235. desc.pixelFormat = format;
  2236. desc.width = textureWidth;
  2237. desc.height = textureHeight;
  2238. desc.depth = bx::uint32_max(1,imageContainer.m_depth);
  2239. desc.mipmapLevelCount = numMips;
  2240. desc.sampleCount = 1;
  2241. desc.arrayLength = numLayers;
  2242. if (s_renderMtl->m_iOS9Runtime || s_renderMtl->m_macOS11Runtime)
  2243. {
  2244. desc.cpuCacheMode = MTLCPUCacheModeDefaultCache;
  2245. desc.storageMode = (MTLStorageMode)(false
  2246. || writeOnly
  2247. || bimg::isDepth(bimg::TextureFormat::Enum(m_textureFormat) )
  2248. ? 2 /* MTLStorageModePrivate */
  2249. : (BX_ENABLED(BX_PLATFORM_IOS)
  2250. ? 0 /* MTLStorageModeShared */
  2251. : 1 /* MTLStorageModeManaged */
  2252. ) );
  2253. desc.usage = MTLTextureUsageShaderRead;
  2254. if (computeWrite)
  2255. {
  2256. desc.usage |= MTLTextureUsageShaderWrite;
  2257. }
  2258. if (renderTarget)
  2259. {
  2260. desc.usage |= MTLTextureUsageRenderTarget;
  2261. }
  2262. }
  2263. m_ptr = s_renderMtl->m_device.newTextureWithDescriptor(desc);
  2264. if (sampleCount > 1)
  2265. {
  2266. desc.textureType = MTLTextureType2DMultisample;
  2267. desc.sampleCount = sampleCount;
  2268. if (s_renderMtl->m_iOS9Runtime
  2269. || s_renderMtl->m_macOS11Runtime)
  2270. {
  2271. desc.storageMode = (MTLStorageMode)(2 /* MTLStorageModePrivate */);
  2272. }
  2273. m_ptrMSAA = s_renderMtl->m_device.newTextureWithDescriptor(desc);
  2274. }
  2275. if (m_requestedFormat == TextureFormat::D24S8
  2276. && desc.pixelFormat == MTLPixelFormatDepth32Float)
  2277. {
  2278. desc.pixelFormat = MTLPixelFormatStencil8;
  2279. m_ptrStencil = s_renderMtl->m_device.newTextureWithDescriptor(desc);
  2280. }
  2281. uint8_t* temp = NULL;
  2282. if (convert)
  2283. {
  2284. temp = (uint8_t*)BX_ALLOC(g_allocator, textureWidth*textureHeight*4);
  2285. }
  2286. for (uint8_t side = 0; side < numSides; ++side)
  2287. {
  2288. uint32_t width = textureWidth;
  2289. uint32_t height = textureHeight;
  2290. uint32_t depth = imageContainer.m_depth;
  2291. for (uint8_t lod = 0, num = numMips; lod < num; ++lod)
  2292. {
  2293. width = bx::uint32_max(1, width);
  2294. height = bx::uint32_max(1, height);
  2295. depth = bx::uint32_max(1, depth);
  2296. bimg::ImageMip mip;
  2297. if (bimg::imageGetRawData(imageContainer, side, lod+startLod, _mem->data, _mem->size, mip) )
  2298. {
  2299. const uint8_t* data = mip.m_data;
  2300. if (convert)
  2301. {
  2302. bimg::imageDecodeToBgra8(
  2303. g_allocator
  2304. , temp
  2305. , mip.m_data
  2306. , mip.m_width
  2307. , mip.m_height
  2308. , mip.m_width*4
  2309. , mip.m_format
  2310. );
  2311. data = temp;
  2312. }
  2313. MTLRegion region = { { 0, 0, 0 }, { width, height, depth } };
  2314. uint32_t bytesPerRow = 0;
  2315. uint32_t bytesPerImage = 0;
  2316. if (compressed && !convert)
  2317. {
  2318. if (format >= 160 /*PVRTC_RGB_2BPP*/
  2319. && format <= 167 /*PVRTC_RGBA_4BPP_sRGB*/)
  2320. {
  2321. bytesPerRow = 0;
  2322. bytesPerImage = 0;
  2323. }
  2324. else
  2325. {
  2326. bytesPerRow = (mip.m_width / blockInfo.blockWidth)*mip.m_blockSize;
  2327. bytesPerImage = desc.textureType == MTLTextureType3D
  2328. ? (mip.m_height/blockInfo.blockHeight)*bytesPerRow
  2329. : 0
  2330. ;
  2331. }
  2332. }
  2333. else
  2334. {
  2335. bytesPerRow = width * bpp / 8;
  2336. bytesPerImage = desc.textureType == MTLTextureType3D
  2337. ? bytesPerRow * height
  2338. : 0
  2339. ;
  2340. }
  2341. m_ptr.replaceRegion(region, lod, side, data, bytesPerRow, bytesPerImage);
  2342. }
  2343. width >>= 1;
  2344. height >>= 1;
  2345. depth >>= 1;
  2346. }
  2347. }
  2348. if (NULL != temp)
  2349. {
  2350. BX_FREE(g_allocator, temp);
  2351. }
  2352. }
  2353. }
  2354. void TextureMtl::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem)
  2355. {
  2356. const uint32_t bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(m_textureFormat) );
  2357. const uint32_t rectpitch = _rect.m_width*bpp/8;
  2358. const uint32_t srcpitch = UINT16_MAX == _pitch ? rectpitch : _pitch;
  2359. const uint32_t slice = ( (m_type == Texture3D) ? 0 : _side + _z * (m_type == TextureCube ? 6 : 1) );
  2360. const uint16_t z = (m_type == Texture3D) ? _z : 0 ;
  2361. const bool convert = m_textureFormat != m_requestedFormat;
  2362. uint8_t* data = _mem->data;
  2363. uint8_t* temp = NULL;
  2364. if (convert)
  2365. {
  2366. temp = (uint8_t*)BX_ALLOC(g_allocator, rectpitch*_rect.m_height);
  2367. bimg::imageDecodeToBgra8(
  2368. g_allocator
  2369. , temp
  2370. , data
  2371. , _rect.m_width
  2372. , _rect.m_height
  2373. , srcpitch
  2374. , bimg::TextureFormat::Enum(m_requestedFormat)
  2375. );
  2376. data = temp;
  2377. }
  2378. if (NULL != s_renderMtl->m_renderCommandEncoder)
  2379. {
  2380. s_renderMtl->m_cmd.finish(true);
  2381. MTLRegion region =
  2382. {
  2383. { _rect.m_x, _rect.m_y, z },
  2384. { _rect.m_width, _rect.m_height, _depth },
  2385. };
  2386. m_ptr.replaceRegion(region, _mip, slice, data, srcpitch, srcpitch * _rect.m_height);
  2387. }
  2388. else
  2389. {
  2390. BlitCommandEncoder bce = s_renderMtl->getBlitCommandEncoder();
  2391. const uint32_t dstpitch = bx::strideAlign(rectpitch, 64);
  2392. Buffer tempBuffer = s_renderMtl->m_device.newBufferWithLength(dstpitch*_rect.m_height, 0);
  2393. const uint8_t* src = (uint8_t*)data;
  2394. uint8_t* dst = (uint8_t*)tempBuffer.contents();
  2395. for (uint32_t yy = 0; yy < _rect.m_height; ++yy, src += srcpitch, dst += dstpitch)
  2396. {
  2397. bx::memCopy(dst, src, rectpitch);
  2398. }
  2399. bce.copyFromBuffer(
  2400. tempBuffer
  2401. , 0
  2402. , dstpitch
  2403. , dstpitch * _rect.m_height
  2404. , MTLSizeMake(_rect.m_width, _rect.m_height, _depth)
  2405. , m_ptr
  2406. , slice
  2407. , _mip
  2408. , MTLOriginMake(_rect.m_x, _rect.m_y, z)
  2409. );
  2410. release(tempBuffer);
  2411. }
  2412. if (NULL != temp)
  2413. {
  2414. BX_FREE(g_allocator, temp);
  2415. }
  2416. }
  2417. void TextureMtl::commit(uint8_t _stage, bool _vertex, bool _fragment, uint32_t _flags)
  2418. {
  2419. if (_vertex)
  2420. {
  2421. s_renderMtl->m_renderCommandEncoder.setVertexTexture(m_ptr, _stage);
  2422. s_renderMtl->m_renderCommandEncoder.setVertexSamplerState(
  2423. 0 == (BGFX_TEXTURE_INTERNAL_DEFAULT_SAMPLER & _flags)
  2424. ? s_renderMtl->getSamplerState(_flags)
  2425. : m_sampler
  2426. , _stage
  2427. );
  2428. }
  2429. if (_fragment)
  2430. {
  2431. s_renderMtl->m_renderCommandEncoder.setFragmentTexture(m_ptr, _stage);
  2432. s_renderMtl->m_renderCommandEncoder.setFragmentSamplerState(
  2433. 0 == (BGFX_TEXTURE_INTERNAL_DEFAULT_SAMPLER & _flags)
  2434. ? s_renderMtl->getSamplerState(_flags)
  2435. : m_sampler
  2436. , _stage
  2437. );
  2438. }
  2439. }
  2440. void FrameBufferMtl::create(uint8_t _num, const Attachment* _attachment)
  2441. {
  2442. m_denseIdx = UINT16_MAX;
  2443. m_num = 0;
  2444. m_width = 0;
  2445. m_height = 0;
  2446. for (uint32_t ii = 0; ii < _num; ++ii)
  2447. {
  2448. TextureHandle handle = _attachment[ii].handle;
  2449. if (isValid(handle) )
  2450. {
  2451. const TextureMtl& texture = s_renderMtl->m_textures[handle.idx];
  2452. if (0 == m_width)
  2453. {
  2454. m_width = texture.m_width;
  2455. m_height = texture.m_height;
  2456. }
  2457. if (bimg::isDepth(bimg::TextureFormat::Enum(texture.m_textureFormat) ) )
  2458. {
  2459. m_depthHandle = handle;
  2460. }
  2461. else
  2462. {
  2463. m_colorHandle[m_num] = handle;
  2464. m_num++;
  2465. }
  2466. }
  2467. }
  2468. bx::HashMurmur2A murmur;
  2469. murmur.begin();
  2470. murmur.add(m_num);
  2471. for (uint32_t ii = 0; ii < m_num; ++ii)
  2472. {
  2473. const TextureMtl& texture = s_renderMtl->m_textures[m_colorHandle[ii].idx];
  2474. murmur.add(uint32_t(texture.m_ptr.pixelFormat() ) );
  2475. }
  2476. if (!isValid(m_depthHandle) )
  2477. {
  2478. murmur.add(uint32_t(MTLPixelFormatInvalid) );
  2479. murmur.add(uint32_t(MTLPixelFormatInvalid) );
  2480. }
  2481. else
  2482. {
  2483. const TextureMtl& depthTexture = s_renderMtl->m_textures[m_depthHandle.idx];
  2484. murmur.add(uint32_t(depthTexture.m_ptr.pixelFormat() ) );
  2485. murmur.add(NULL != depthTexture.m_ptrStencil
  2486. ? uint32_t(depthTexture.m_ptrStencil.pixelFormat() )
  2487. : uint32_t(MTLPixelFormatInvalid)
  2488. );
  2489. }
  2490. murmur.add(1); // SampleCount
  2491. m_pixelFormatHash = murmur.end();
  2492. }
  2493. void FrameBufferMtl::create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat)
  2494. {
  2495. BX_UNUSED(_denseIdx, _nwh, _width, _height, _depthFormat);
  2496. }
  2497. void FrameBufferMtl::postReset()
  2498. {
  2499. }
  2500. uint16_t FrameBufferMtl::destroy()
  2501. {
  2502. m_num = 0;
  2503. m_depthHandle.idx = kInvalidHandle;
  2504. uint16_t denseIdx = m_denseIdx;
  2505. m_denseIdx = UINT16_MAX;
  2506. return denseIdx;
  2507. }
  2508. void CommandQueueMtl::init(Device _device)
  2509. {
  2510. m_commandQueue = _device.newCommandQueue();
  2511. m_framesSemaphore.post(MTL_MAX_FRAMES_IN_FLIGHT);
  2512. }
  2513. void CommandQueueMtl::shutdown()
  2514. {
  2515. finish(true);
  2516. MTL_RELEASE(m_commandQueue);
  2517. }
  2518. CommandBuffer CommandQueueMtl::alloc()
  2519. {
  2520. m_activeCommandBuffer = m_commandQueue.commandBuffer();
  2521. retain(m_activeCommandBuffer);
  2522. return m_activeCommandBuffer;
  2523. }
  2524. static void commandBufferFinishedCallback(void* _data)
  2525. {
  2526. CommandQueueMtl* queue = (CommandQueueMtl*)_data;
  2527. if (queue)
  2528. {
  2529. queue->m_framesSemaphore.post();
  2530. }
  2531. }
  2532. void CommandQueueMtl::kick(bool _endFrame, bool _waitForFinish)
  2533. {
  2534. if (m_activeCommandBuffer)
  2535. {
  2536. if (_endFrame)
  2537. {
  2538. m_releaseWriteIndex = (m_releaseWriteIndex + 1) % MTL_MAX_FRAMES_IN_FLIGHT;
  2539. m_activeCommandBuffer.addCompletedHandler(commandBufferFinishedCallback, this);
  2540. }
  2541. m_activeCommandBuffer.commit();
  2542. if (_waitForFinish)
  2543. {
  2544. m_activeCommandBuffer.waitUntilCompleted();
  2545. }
  2546. MTL_RELEASE(m_activeCommandBuffer);
  2547. }
  2548. }
  2549. void CommandQueueMtl::finish(bool _finishAll)
  2550. {
  2551. if (_finishAll)
  2552. {
  2553. uint32_t count = m_activeCommandBuffer != NULL
  2554. ? 2
  2555. : 3
  2556. ;
  2557. for (uint32_t ii = 0; ii < count; ++ii)
  2558. {
  2559. consume();
  2560. }
  2561. m_framesSemaphore.post(count);
  2562. }
  2563. else
  2564. {
  2565. consume();
  2566. }
  2567. }
  2568. void CommandQueueMtl::release(NSObject* _ptr)
  2569. {
  2570. m_release[m_releaseWriteIndex].push_back(_ptr);
  2571. }
  2572. void CommandQueueMtl::consume()
  2573. {
  2574. m_framesSemaphore.wait();
  2575. m_releaseReadIndex = (m_releaseReadIndex + 1) % MTL_MAX_FRAMES_IN_FLIGHT;
  2576. ResourceArray& ra = m_release[m_releaseReadIndex];
  2577. for (ResourceArray::iterator it = ra.begin(), itEnd = ra.end(); it != itEnd; ++it)
  2578. {
  2579. bgfx::mtl::release(*it);
  2580. }
  2581. ra.clear();
  2582. }
  2583. void TimerQueryMtl::init()
  2584. {
  2585. m_frequency = bx::getHPFrequency();
  2586. }
  2587. void TimerQueryMtl::shutdown()
  2588. {
  2589. }
  2590. static void setTimestamp(void* _data)
  2591. {
  2592. *( (int64_t*)_data) = bx::getHPCounter();
  2593. }
  2594. void TimerQueryMtl::addHandlers(CommandBuffer& _commandBuffer)
  2595. {
  2596. while (0 == m_control.reserve(1) )
  2597. {
  2598. m_control.consume(1);
  2599. }
  2600. uint32_t offset = m_control.m_current * 2 + 0;
  2601. _commandBuffer.addScheduledHandler(setTimestamp, &m_result[offset]);
  2602. _commandBuffer.addCompletedHandler(setTimestamp, &m_result[offset+1]);
  2603. m_control.commit(1);
  2604. }
  2605. bool TimerQueryMtl::get()
  2606. {
  2607. if (0 != m_control.available() )
  2608. {
  2609. uint32_t offset = m_control.m_read * 2;
  2610. m_begin = m_result[offset+0];
  2611. m_end = m_result[offset+1];
  2612. m_elapsed = m_end - m_begin;
  2613. m_control.consume(1);
  2614. return true;
  2615. }
  2616. return false;
  2617. }
  2618. void OcclusionQueryMTL::postReset()
  2619. {
  2620. MTL_RELEASE(m_buffer);
  2621. }
  2622. void OcclusionQueryMTL::preReset()
  2623. {
  2624. m_buffer = s_renderMtl->m_device.newBufferWithLength(BX_COUNTOF(m_query) * 8, 0);
  2625. }
  2626. void OcclusionQueryMTL::begin(RenderCommandEncoder& _rce, Frame* _render, OcclusionQueryHandle _handle)
  2627. {
  2628. while (0 == m_control.reserve(1) )
  2629. {
  2630. resolve(_render, true);
  2631. }
  2632. Query& query = m_query[m_control.m_current];
  2633. query.m_handle = _handle;
  2634. uint32_t offset = _handle.idx * 8;
  2635. _rce.setVisibilityResultMode(MTLVisibilityResultModeBoolean, offset);
  2636. }
  2637. void OcclusionQueryMTL::end(RenderCommandEncoder& _rce)
  2638. {
  2639. Query& query = m_query[m_control.m_current];
  2640. uint32_t offset = query.m_handle.idx * 8;
  2641. _rce.setVisibilityResultMode(MTLVisibilityResultModeDisabled, offset);
  2642. m_control.commit(1);
  2643. }
  2644. void OcclusionQueryMTL::resolve(Frame* _render, bool _wait)
  2645. {
  2646. BX_UNUSED(_wait);
  2647. while (0 != m_control.available() )
  2648. {
  2649. Query& query = m_query[m_control.m_read];
  2650. if (isValid(query.m_handle) )
  2651. {
  2652. uint64_t result = ( (uint64_t*)m_buffer.contents() )[query.m_handle.idx];
  2653. _render->m_occlusion[query.m_handle.idx] = int32_t(result);
  2654. }
  2655. m_control.consume(1);
  2656. }
  2657. }
  2658. void OcclusionQueryMTL::invalidate(OcclusionQueryHandle _handle)
  2659. {
  2660. const uint32_t size = m_control.m_size;
  2661. for (uint32_t ii = 0, num = m_control.available(); ii < num; ++ii)
  2662. {
  2663. Query& query = m_query[(m_control.m_read + ii) % size];
  2664. if (query.m_handle.idx == _handle.idx)
  2665. {
  2666. query.m_handle.idx = bgfx::kInvalidHandle;
  2667. }
  2668. }
  2669. }
  2670. void RendererContextMtl::submitBlit(BlitState& _bs, uint16_t _view)
  2671. {
  2672. if (!_bs.hasItem(_view))
  2673. return;
  2674. if (0 != m_renderCommandEncoder)
  2675. {
  2676. m_renderCommandEncoder.endEncoding();
  2677. m_renderCommandEncoder = 0;
  2678. }
  2679. m_blitCommandEncoder = getBlitCommandEncoder();
  2680. while (_bs.hasItem(_view) )
  2681. {
  2682. const BlitItem& blit = _bs.advance();
  2683. const TextureMtl& src = m_textures[blit.m_src.idx];
  2684. const TextureMtl& dst = m_textures[blit.m_dst.idx];
  2685. uint32_t srcWidth = bx::uint32_min(src.m_width, blit.m_srcX + blit.m_width) - blit.m_srcX;
  2686. uint32_t srcHeight = bx::uint32_min(src.m_height, blit.m_srcY + blit.m_height) - blit.m_srcY;
  2687. uint32_t srcDepth = bx::uint32_min(src.m_depth, blit.m_srcZ + blit.m_depth) - blit.m_srcZ;
  2688. uint32_t dstWidth = bx::uint32_min(dst.m_width, blit.m_dstX + blit.m_width) - blit.m_dstX;
  2689. uint32_t dstHeight = bx::uint32_min(dst.m_height, blit.m_dstY + blit.m_height) - blit.m_dstY;
  2690. uint32_t dstDepth = bx::uint32_min(dst.m_depth, blit.m_dstZ + blit.m_depth) - blit.m_dstZ;
  2691. uint32_t width = bx::uint32_min(srcWidth, dstWidth);
  2692. uint32_t height = bx::uint32_min(srcHeight, dstHeight);
  2693. uint32_t depth = bx::uint32_min(srcDepth, dstDepth);
  2694. #if BX_PLATFORM_OSX
  2695. bool readBack = !!(dst.m_flags & BGFX_TEXTURE_READ_BACK);
  2696. #endif // BX_PLATFORM_OSX
  2697. if (MTLTextureType3D == src.m_ptr.textureType() )
  2698. {
  2699. m_blitCommandEncoder.copyFromTexture(
  2700. src.m_ptr
  2701. , 0
  2702. , 0
  2703. , MTLOriginMake(blit.m_srcX, blit.m_srcY, blit.m_srcZ)
  2704. , MTLSizeMake(width, height, bx::uint32_imax(depth, 1) )
  2705. , dst.m_ptr
  2706. , 0
  2707. , 0
  2708. , MTLOriginMake(blit.m_dstX, blit.m_dstY, blit.m_dstZ)
  2709. );
  2710. #if BX_PLATFORM_OSX
  2711. if (m_macOS11Runtime
  2712. && readBack)
  2713. {
  2714. m_blitCommandEncoder.synchronizeResource(dst.m_ptr);
  2715. }
  2716. #endif // BX_PLATFORM_OSX
  2717. }
  2718. else
  2719. {
  2720. m_blitCommandEncoder.copyFromTexture(
  2721. src.m_ptr
  2722. , blit.m_srcZ
  2723. , blit.m_srcMip
  2724. , MTLOriginMake(blit.m_srcX, blit.m_srcY, 0)
  2725. , MTLSizeMake(width, height, 1)
  2726. , dst.m_ptr
  2727. , blit.m_dstZ
  2728. , blit.m_dstMip
  2729. , MTLOriginMake(blit.m_dstX, blit.m_dstY, 0)
  2730. );
  2731. #if BX_PLATFORM_OSX
  2732. if (m_macOS11Runtime
  2733. && readBack)
  2734. {
  2735. m_blitCommandEncoder.synchronizeTexture(dst.m_ptr, 0, blit.m_dstMip);
  2736. }
  2737. #endif // BX_PLATFORM_OSX
  2738. }
  2739. }
  2740. if (0 != m_blitCommandEncoder)
  2741. {
  2742. m_blitCommandEncoder.endEncoding();
  2743. m_blitCommandEncoder = 0;
  2744. }
  2745. }
  2746. void RendererContextMtl::submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter)
  2747. {
  2748. m_cmd.finish(false);
  2749. if (m_commandBuffer == NULL)
  2750. {
  2751. m_commandBuffer = m_cmd.alloc();
  2752. }
  2753. int64_t timeBegin = bx::getHPCounter();
  2754. int64_t captureElapsed = 0;
  2755. m_gpuTimer.addHandlers(m_commandBuffer);
  2756. if (m_blitCommandEncoder)
  2757. {
  2758. m_blitCommandEncoder.endEncoding();
  2759. m_blitCommandEncoder = 0;
  2760. }
  2761. updateResolution(_render->m_resolution);
  2762. if (m_saveScreenshot
  2763. || NULL != m_capture)
  2764. {
  2765. if (m_screenshotTarget)
  2766. {
  2767. if (m_screenshotTarget.width() != m_resolution.width
  2768. || m_screenshotTarget.height() != m_resolution.height)
  2769. {
  2770. MTL_RELEASE(m_screenshotTarget);
  2771. }
  2772. }
  2773. if (NULL == m_screenshotTarget)
  2774. {
  2775. m_textureDescriptor.textureType = MTLTextureType2D;
  2776. m_textureDescriptor.pixelFormat = m_metalLayer.pixelFormat;
  2777. m_textureDescriptor.width = m_resolution.width;
  2778. m_textureDescriptor.height = m_resolution.height;
  2779. m_textureDescriptor.depth = 1;
  2780. m_textureDescriptor.mipmapLevelCount = 1;
  2781. m_textureDescriptor.sampleCount = 1;
  2782. m_textureDescriptor.arrayLength = 1;
  2783. if (m_iOS9Runtime
  2784. || m_macOS11Runtime)
  2785. {
  2786. m_textureDescriptor.cpuCacheMode = MTLCPUCacheModeDefaultCache;
  2787. m_textureDescriptor.storageMode = BX_ENABLED(BX_PLATFORM_IOS)
  2788. ? (MTLStorageMode)0 // MTLStorageModeShared
  2789. : (MTLStorageMode)1 // MTLStorageModeManaged
  2790. ;
  2791. m_textureDescriptor.usage = 0
  2792. | MTLTextureUsageRenderTarget
  2793. | MTLTextureUsageShaderRead
  2794. ;
  2795. }
  2796. m_screenshotTarget = m_device.newTextureWithDescriptor(m_textureDescriptor);
  2797. }
  2798. m_saveScreenshot = false;
  2799. }
  2800. else
  2801. {
  2802. MTL_RELEASE(m_screenshotTarget);
  2803. }
  2804. m_uniformBuffer = m_uniformBuffers[m_bufferIndex];
  2805. m_bufferIndex = (m_bufferIndex + 1) % MTL_MAX_FRAMES_IN_FLIGHT;
  2806. m_uniformBufferVertexOffset = 0;
  2807. m_uniformBufferFragmentOffset = 0;
  2808. if (0 < _render->m_iboffset)
  2809. {
  2810. TransientIndexBuffer* ib = _render->m_transientIb;
  2811. m_indexBuffers[ib->handle.idx].update(0, _render->m_iboffset, ib->data, true);
  2812. }
  2813. if (0 < _render->m_vboffset)
  2814. {
  2815. TransientVertexBuffer* vb = _render->m_transientVb;
  2816. m_vertexBuffers[vb->handle.idx].update(0, _render->m_vboffset, vb->data, true);
  2817. }
  2818. _render->sort();
  2819. RenderDraw currentState;
  2820. currentState.clear();
  2821. currentState.m_stateFlags = BGFX_STATE_NONE;
  2822. currentState.m_stencil = packStencil(BGFX_STENCIL_NONE, BGFX_STENCIL_NONE);
  2823. RenderBind currentBind;
  2824. currentBind.clear();
  2825. const bool hmdEnabled = false;
  2826. static ViewState viewState;
  2827. viewState.reset(_render, hmdEnabled);
  2828. uint32_t blendFactor = 0;
  2829. bool wireframe = !!(_render->m_debug&BGFX_DEBUG_WIREFRAME);
  2830. uint16_t programIdx = kInvalidHandle;
  2831. SortKey key;
  2832. uint16_t view = UINT16_MAX;
  2833. FrameBufferHandle fbh = { BGFX_CONFIG_MAX_FRAME_BUFFERS };
  2834. BlitState bs(_render);
  2835. const uint64_t primType = 0;
  2836. uint8_t primIndex = uint8_t(primType>>BGFX_STATE_PT_SHIFT);
  2837. PrimInfo prim = s_primInfo[primIndex];
  2838. ProgramMtl* currentProgram = NULL;
  2839. RenderCommandEncoder rce;
  2840. bool wasCompute = false;
  2841. bool viewHasScissor = false;
  2842. Rect viewScissorRect;
  2843. viewScissorRect.clear();
  2844. uint32_t statsNumPrimsSubmitted[BX_COUNTOF(s_primInfo)] = {};
  2845. uint32_t statsNumPrimsRendered[BX_COUNTOF(s_primInfo)] = {};
  2846. uint32_t statsNumInstances[BX_COUNTOF(s_primInfo)] = {};
  2847. uint32_t statsNumDrawIndirect[BX_COUNTOF(s_primInfo)] = {};
  2848. uint32_t statsNumIndices = 0;
  2849. uint32_t statsKeyType[2] = {};
  2850. m_occlusionQuery.resolve(_render);
  2851. if (0 == (_render->m_debug&BGFX_DEBUG_IFH) )
  2852. {
  2853. bool viewRestart = false;
  2854. uint8_t eye = 0;
  2855. uint8_t restartState = 0;
  2856. viewState.m_rect = _render->m_view[0].m_rect;
  2857. int32_t numItems = _render->m_numRenderItems;
  2858. for (int32_t item = 0, restartItem = numItems; item < numItems || restartItem < numItems;)
  2859. {
  2860. const uint64_t encodedKey = _render->m_sortKeys[item];
  2861. const bool isCompute = key.decode(encodedKey, _render->m_viewRemap);
  2862. statsKeyType[isCompute]++;
  2863. const bool viewChanged = 0
  2864. || key.m_view != view
  2865. || item == numItems
  2866. ;
  2867. const uint32_t itemIdx = _render->m_sortValues[item];
  2868. const RenderItem& renderItem = _render->m_renderItem[itemIdx];
  2869. const RenderBind& renderBind = _render->m_renderItemBind[itemIdx];
  2870. ++item;
  2871. if (viewChanged)
  2872. {
  2873. if (1 == restartState)
  2874. {
  2875. restartState = 2;
  2876. item = restartItem;
  2877. restartItem = numItems;
  2878. view = UINT16_MAX;
  2879. continue;
  2880. }
  2881. view = key.m_view;
  2882. programIdx = kInvalidHandle;
  2883. viewRestart = BGFX_VIEW_STEREO == (_render->m_view[view].m_flags & BGFX_VIEW_STEREO);
  2884. viewRestart &= hmdEnabled;
  2885. if (viewRestart)
  2886. {
  2887. if (0 == restartState)
  2888. {
  2889. restartState = 1;
  2890. restartItem = item - 1;
  2891. }
  2892. eye = (restartState - 1) & 1;
  2893. restartState &= 1;
  2894. }
  2895. else
  2896. {
  2897. eye = 0;
  2898. }
  2899. viewState.m_rect = _render->m_view[view].m_rect;
  2900. if (viewRestart)
  2901. {
  2902. viewState.m_rect.m_x = eye * (viewState.m_rect.m_width+1)/2;
  2903. viewState.m_rect.m_width /= 2;
  2904. }
  2905. submitBlit(bs, view);
  2906. const Rect& scissorRect = _render->m_view[view].m_scissor;
  2907. viewHasScissor = !scissorRect.isZero();
  2908. viewScissorRect = viewHasScissor ? scissorRect : viewState.m_rect;
  2909. Clear& clr = _render->m_view[view].m_clear;
  2910. Rect viewRect = viewState.m_rect;
  2911. bool clearWithRenderPass = false;
  2912. if (NULL == m_renderCommandEncoder
  2913. || fbh.idx != _render->m_view[view].m_fbh.idx)
  2914. {
  2915. if (0 != m_renderCommandEncoder)
  2916. {
  2917. m_renderCommandEncoder.endEncoding();
  2918. }
  2919. RenderPassDescriptor renderPassDescriptor = newRenderPassDescriptor();
  2920. renderPassDescriptor.visibilityResultBuffer = m_occlusionQuery.m_buffer;
  2921. fbh = _render->m_view[view].m_fbh;
  2922. uint32_t width = m_resolution.width;
  2923. uint32_t height = m_resolution.height;
  2924. if (isValid(fbh) )
  2925. {
  2926. FrameBufferMtl& frameBuffer = m_frameBuffers[fbh.idx];
  2927. width = frameBuffer.m_width;
  2928. height = frameBuffer.m_height;
  2929. }
  2930. clearWithRenderPass = true
  2931. && 0 == viewRect.m_x
  2932. && 0 == viewRect.m_y
  2933. && width == viewRect.m_width
  2934. && height == viewRect.m_height
  2935. ;
  2936. setFrameBuffer(renderPassDescriptor, fbh);
  2937. if (clearWithRenderPass)
  2938. {
  2939. for (uint32_t ii = 0; ii < g_caps.limits.maxFBAttachments; ++ii)
  2940. {
  2941. MTLRenderPassColorAttachmentDescriptor* desc = renderPassDescriptor.colorAttachments[ii];
  2942. if (desc.texture != NULL)
  2943. {
  2944. if (0 != (BGFX_CLEAR_COLOR & clr.m_flags) )
  2945. {
  2946. if (0 != (BGFX_CLEAR_COLOR_USE_PALETTE & clr.m_flags) )
  2947. {
  2948. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, clr.m_index[ii]);
  2949. const float* rgba = _render->m_colorPalette[index];
  2950. const float rr = rgba[0];
  2951. const float gg = rgba[1];
  2952. const float bb = rgba[2];
  2953. const float aa = rgba[3];
  2954. desc.clearColor = MTLClearColorMake(rr, gg, bb, aa);
  2955. }
  2956. else
  2957. {
  2958. float rr = clr.m_index[0]*1.0f/255.0f;
  2959. float gg = clr.m_index[1]*1.0f/255.0f;
  2960. float bb = clr.m_index[2]*1.0f/255.0f;
  2961. float aa = clr.m_index[3]*1.0f/255.0f;
  2962. desc.clearColor = MTLClearColorMake(rr, gg, bb, aa);
  2963. }
  2964. desc.loadAction = MTLLoadActionClear;
  2965. }
  2966. else
  2967. {
  2968. desc.loadAction = MTLLoadActionLoad;
  2969. }
  2970. desc.storeAction = desc.texture.sampleCount > 1 ? MTLStoreActionMultisampleResolve : MTLStoreActionStore;
  2971. }
  2972. }
  2973. RenderPassDepthAttachmentDescriptor depthAttachment = renderPassDescriptor.depthAttachment;
  2974. if (NULL != depthAttachment.texture)
  2975. {
  2976. depthAttachment.clearDepth = clr.m_depth;
  2977. depthAttachment.loadAction = 0 != (BGFX_CLEAR_DEPTH & clr.m_flags)
  2978. ? MTLLoadActionClear
  2979. : MTLLoadActionLoad
  2980. ;
  2981. depthAttachment.storeAction = NULL != m_backBufferColorMSAA
  2982. ? MTLStoreActionDontCare
  2983. : MTLStoreActionStore
  2984. ;
  2985. }
  2986. RenderPassStencilAttachmentDescriptor stencilAttachment = renderPassDescriptor.stencilAttachment;
  2987. if (NULL != stencilAttachment.texture)
  2988. {
  2989. stencilAttachment.clearStencil = clr.m_stencil;
  2990. stencilAttachment.loadAction = 0 != (BGFX_CLEAR_STENCIL & clr.m_flags)
  2991. ? MTLLoadActionClear
  2992. : MTLLoadActionLoad
  2993. ;
  2994. stencilAttachment.storeAction = NULL != m_backBufferColorMSAA
  2995. ? MTLStoreActionDontCare
  2996. : MTLStoreActionStore
  2997. ;
  2998. }
  2999. }
  3000. else
  3001. {
  3002. for (uint32_t ii = 0; ii < g_caps.limits.maxFBAttachments; ++ii)
  3003. {
  3004. MTLRenderPassColorAttachmentDescriptor* desc = renderPassDescriptor.colorAttachments[ii];
  3005. if (desc.texture != NULL)
  3006. {
  3007. desc.loadAction = MTLLoadActionLoad;
  3008. }
  3009. }
  3010. RenderPassDepthAttachmentDescriptor depthAttachment = renderPassDescriptor.depthAttachment;
  3011. if (NULL != depthAttachment.texture)
  3012. {
  3013. depthAttachment.loadAction = MTLLoadActionLoad;
  3014. depthAttachment.storeAction = MTLStoreActionStore;
  3015. }
  3016. RenderPassStencilAttachmentDescriptor stencilAttachment = renderPassDescriptor.stencilAttachment;
  3017. if (NULL != stencilAttachment.texture)
  3018. {
  3019. stencilAttachment.loadAction = MTLLoadActionLoad;
  3020. stencilAttachment.storeAction = MTLStoreActionStore;
  3021. }
  3022. }
  3023. rce = m_commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor);
  3024. m_renderCommandEncoder = rce;
  3025. m_renderCommandEncoderFrameBufferHandle = fbh;
  3026. MTL_RELEASE(renderPassDescriptor);
  3027. }
  3028. rce.setTriangleFillMode(wireframe ? MTLTriangleFillModeLines : MTLTriangleFillModeFill);
  3029. if (BX_ENABLED(BGFX_CONFIG_DEBUG_MTL) )
  3030. {
  3031. if (item != 1)
  3032. {
  3033. rce.popDebugGroup();
  3034. }
  3035. rce.pushDebugGroup(s_viewName[view]);
  3036. }
  3037. MTLViewport vp;
  3038. vp.originX = viewState.m_rect.m_x;
  3039. vp.originY = viewState.m_rect.m_y;
  3040. vp.width = viewState.m_rect.m_width;
  3041. vp.height = viewState.m_rect.m_height;
  3042. vp.znear = 0.0f;
  3043. vp.zfar = 1.0f;
  3044. rce.setViewport(vp);
  3045. if (BGFX_CLEAR_NONE != (clr.m_flags & BGFX_CLEAR_MASK)
  3046. && !clearWithRenderPass)
  3047. {
  3048. clearQuad(_clearQuad, viewState.m_rect, clr, _render->m_colorPalette);
  3049. }
  3050. }
  3051. bool resetState = viewChanged || wasCompute;
  3052. if (wasCompute)
  3053. {
  3054. wasCompute = false;
  3055. programIdx = kInvalidHandle;
  3056. currentProgram = NULL;
  3057. //invalidateCompute();
  3058. }
  3059. const RenderDraw& draw = renderItem.draw;
  3060. const bool hasOcclusionQuery = 0 != (draw.m_stateFlags & BGFX_STATE_INTERNAL_OCCLUSION_QUERY);
  3061. {
  3062. const bool occluded = true
  3063. && isValid(draw.m_occlusionQuery)
  3064. && !hasOcclusionQuery
  3065. && !isVisible(_render, draw.m_occlusionQuery, 0 != (draw.m_submitFlags&BGFX_SUBMIT_INTERNAL_OCCLUSION_VISIBLE) )
  3066. ;
  3067. if (occluded
  3068. || _render->m_frameCache.isZeroArea(viewScissorRect, draw.m_scissor) )
  3069. {
  3070. if (resetState)
  3071. {
  3072. currentState.clear();
  3073. currentState.m_scissor = !draw.m_scissor;
  3074. currentBind.clear();
  3075. }
  3076. continue;
  3077. }
  3078. }
  3079. const uint64_t newFlags = draw.m_stateFlags;
  3080. uint64_t changedFlags = currentState.m_stateFlags ^ draw.m_stateFlags;
  3081. currentState.m_stateFlags = newFlags;
  3082. const uint64_t newStencil = draw.m_stencil;
  3083. uint64_t changedStencil = currentState.m_stencil ^ draw.m_stencil;
  3084. currentState.m_stencil = newStencil;
  3085. if (resetState)
  3086. {
  3087. currentState.clear();
  3088. currentState.m_scissor = !draw.m_scissor;
  3089. changedFlags = BGFX_STATE_MASK;
  3090. changedStencil = packStencil(BGFX_STENCIL_MASK, BGFX_STENCIL_MASK);
  3091. currentState.m_stateFlags = newFlags;
  3092. currentState.m_stencil = newStencil;
  3093. currentBind.clear();
  3094. programIdx = kInvalidHandle;
  3095. setDepthStencilState(newFlags, packStencil(BGFX_STENCIL_DEFAULT, BGFX_STENCIL_DEFAULT) );
  3096. const uint64_t pt = newFlags&BGFX_STATE_PT_MASK;
  3097. primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT);
  3098. }
  3099. if (prim.m_type != s_primInfo[primIndex].m_type)
  3100. {
  3101. prim = s_primInfo[primIndex];
  3102. }
  3103. uint16_t scissor = draw.m_scissor;
  3104. if (currentState.m_scissor != scissor)
  3105. {
  3106. currentState.m_scissor = scissor;
  3107. MTLScissorRect rc;
  3108. if (UINT16_MAX == scissor)
  3109. {
  3110. if (viewHasScissor)
  3111. {
  3112. rc.x = viewScissorRect.m_x;
  3113. rc.y = viewScissorRect.m_y;
  3114. rc.width = viewScissorRect.m_width;
  3115. rc.height = viewScissorRect.m_height;
  3116. }
  3117. else
  3118. { // can't disable: set to view rect
  3119. rc.x = viewState.m_rect.m_x;
  3120. rc.y = viewState.m_rect.m_y;
  3121. rc.width = viewState.m_rect.m_width;
  3122. rc.height = viewState.m_rect.m_height;
  3123. }
  3124. }
  3125. else
  3126. {
  3127. Rect scissorRect;
  3128. scissorRect.setIntersect(viewScissorRect, _render->m_frameCache.m_rectCache.m_cache[scissor]);
  3129. rc.x = scissorRect.m_x;
  3130. rc.y = scissorRect.m_y;
  3131. rc.width = scissorRect.m_width;
  3132. rc.height = scissorRect.m_height;
  3133. }
  3134. rce.setScissorRect(rc);
  3135. }
  3136. if ( (0
  3137. | BGFX_STATE_WRITE_Z
  3138. | BGFX_STATE_DEPTH_TEST_MASK
  3139. ) & changedFlags
  3140. || 0 != changedStencil)
  3141. {
  3142. setDepthStencilState(newFlags,newStencil);
  3143. }
  3144. if ( (0
  3145. | BGFX_STATE_CULL_MASK
  3146. | BGFX_STATE_ALPHA_REF_MASK
  3147. | BGFX_STATE_PT_MASK
  3148. ) & changedFlags)
  3149. {
  3150. if (BGFX_STATE_CULL_MASK & changedFlags)
  3151. {
  3152. const uint64_t pt = newFlags&BGFX_STATE_CULL_MASK;
  3153. const uint8_t cullIndex = uint8_t(pt>>BGFX_STATE_CULL_SHIFT);
  3154. rce.setCullMode(s_cullMode[cullIndex]);
  3155. }
  3156. if (BGFX_STATE_ALPHA_REF_MASK & changedFlags)
  3157. {
  3158. uint32_t ref = (newFlags&BGFX_STATE_ALPHA_REF_MASK)>>BGFX_STATE_ALPHA_REF_SHIFT;
  3159. viewState.m_alphaRef = ref/255.0f;
  3160. }
  3161. const uint64_t pt = newFlags&BGFX_STATE_PT_MASK;
  3162. primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT);
  3163. if (prim.m_type != s_primInfo[primIndex].m_type)
  3164. {
  3165. prim = s_primInfo[primIndex];
  3166. }
  3167. }
  3168. if (blendFactor != draw.m_rgba
  3169. && !(newFlags & BGFX_STATE_BLEND_INDEPENDENT) )
  3170. {
  3171. const uint32_t rgba = draw.m_rgba;
  3172. float rr = ( (rgba>>24) )/255.0f;
  3173. float gg = ( (rgba>>16)&0xff)/255.0f;
  3174. float bb = ( (rgba>> 8)&0xff)/255.0f;
  3175. float aa = ( (rgba )&0xff)/255.0f;
  3176. rce.setBlendColor(rr,gg,bb,aa);
  3177. blendFactor = draw.m_rgba;
  3178. }
  3179. bool programChanged = false;
  3180. bool constantsChanged = draw.m_uniformBegin < draw.m_uniformEnd;
  3181. rendererUpdateUniforms(this, _render->m_uniformBuffer[draw.m_uniformIdx], draw.m_uniformBegin, draw.m_uniformEnd);
  3182. bool vertexStreamChanged = hasVertexStreamChanged(currentState, draw);
  3183. if (key.m_program != programIdx
  3184. || vertexStreamChanged
  3185. || (0
  3186. | BGFX_STATE_BLEND_MASK
  3187. | BGFX_STATE_BLEND_EQUATION_MASK
  3188. | BGFX_STATE_WRITE_RGB
  3189. | BGFX_STATE_WRITE_A
  3190. | BGFX_STATE_BLEND_INDEPENDENT
  3191. | BGFX_STATE_MSAA
  3192. | BGFX_STATE_BLEND_ALPHA_TO_COVERAGE
  3193. ) & changedFlags
  3194. || ( (blendFactor != draw.m_rgba) && !!(newFlags & BGFX_STATE_BLEND_INDEPENDENT) ) )
  3195. {
  3196. programIdx = key.m_program;
  3197. currentState.m_streamMask = draw.m_streamMask;
  3198. currentState.m_instanceDataBuffer.idx = draw.m_instanceDataBuffer.idx;
  3199. currentState.m_instanceDataOffset = draw.m_instanceDataOffset;
  3200. currentState.m_instanceDataStride = draw.m_instanceDataStride;
  3201. const VertexDecl* decls[BGFX_CONFIG_MAX_VERTEX_STREAMS];
  3202. uint32_t numVertices = draw.m_numVertices;
  3203. uint8_t numStreams = 0;
  3204. for (uint32_t idx = 0, streamMask = draw.m_streamMask, ntz = bx::uint32_cnttz(streamMask)
  3205. ; 0 != streamMask
  3206. ; streamMask >>= 1, idx += 1, ntz = bx::uint32_cnttz(streamMask), ++numStreams
  3207. )
  3208. {
  3209. streamMask >>= ntz;
  3210. idx += ntz;
  3211. currentState.m_stream[idx].m_decl = draw.m_stream[idx].m_decl;
  3212. currentState.m_stream[idx].m_handle = draw.m_stream[idx].m_handle;
  3213. currentState.m_stream[idx].m_startVertex = draw.m_stream[idx].m_startVertex;
  3214. const uint16_t handle = draw.m_stream[idx].m_handle.idx;
  3215. const VertexBufferMtl& vb = m_vertexBuffers[handle];
  3216. const uint16_t decl = !isValid(vb.m_decl) ? draw.m_stream[idx].m_decl.idx : vb.m_decl.idx;
  3217. const VertexDecl& vertexDecl = m_vertexDecls[decl];
  3218. const uint32_t stride = vertexDecl.m_stride;
  3219. decls[numStreams] = &vertexDecl;
  3220. numVertices = bx::uint32_min(UINT32_MAX == draw.m_numVertices
  3221. ? vb.m_size/stride
  3222. : draw.m_numVertices
  3223. , numVertices
  3224. );
  3225. const uint32_t offset = draw.m_stream[idx].m_startVertex * stride;
  3226. rce.setVertexBuffer(vb.getBuffer(), offset, idx+1);
  3227. }
  3228. currentState.m_numVertices = numVertices;
  3229. if (kInvalidHandle == programIdx)
  3230. {
  3231. currentProgram = NULL;
  3232. continue;
  3233. }
  3234. else
  3235. {
  3236. ProgramMtl& program = m_program[programIdx];
  3237. currentProgram = &program;
  3238. RenderPipelineState pso = NULL;
  3239. if (0 < numStreams)
  3240. {
  3241. pso = getPipelineState(
  3242. newFlags
  3243. , draw.m_rgba
  3244. , fbh
  3245. , numStreams
  3246. , decls
  3247. , programIdx
  3248. , draw.m_instanceDataStride/16
  3249. );
  3250. }
  3251. if (NULL == pso)
  3252. {
  3253. currentProgram = NULL;
  3254. programIdx = kInvalidHandle;
  3255. continue;
  3256. }
  3257. rce.setRenderPipelineState(pso);
  3258. }
  3259. if (isValid(draw.m_instanceDataBuffer) )
  3260. {
  3261. const VertexBufferMtl& inst = m_vertexBuffers[draw.m_instanceDataBuffer.idx];
  3262. rce.setVertexBuffer(inst.getBuffer(), draw.m_instanceDataOffset, numStreams+1);
  3263. }
  3264. programChanged =
  3265. constantsChanged = true;
  3266. }
  3267. if (kInvalidHandle != programIdx)
  3268. {
  3269. ProgramMtl& program = m_program[programIdx];
  3270. uint32_t vertexUniformBufferSize = program.m_vshConstantBufferSize;
  3271. uint32_t fragmentUniformBufferSize = program.m_fshConstantBufferSize;
  3272. if (0 != vertexUniformBufferSize)
  3273. {
  3274. m_uniformBufferVertexOffset = BX_ALIGN_MASK(m_uniformBufferVertexOffset, program.m_vshConstantBufferAlignmentMask);
  3275. rce.setVertexBuffer(m_uniformBuffer, m_uniformBufferVertexOffset, 0);
  3276. }
  3277. m_uniformBufferFragmentOffset = m_uniformBufferVertexOffset + vertexUniformBufferSize;
  3278. if (0 != fragmentUniformBufferSize)
  3279. {
  3280. m_uniformBufferFragmentOffset = BX_ALIGN_MASK(m_uniformBufferFragmentOffset, program.m_fshConstantBufferAlignmentMask);
  3281. rce.setFragmentBuffer(m_uniformBuffer, m_uniformBufferFragmentOffset, 0);
  3282. }
  3283. if (constantsChanged)
  3284. {
  3285. UniformBuffer* vcb = program.m_vshConstantBuffer;
  3286. if (NULL != vcb)
  3287. {
  3288. commit(*vcb);
  3289. }
  3290. UniformBuffer* fcb = program.m_fshConstantBuffer;
  3291. if (NULL != fcb)
  3292. {
  3293. commit(*fcb);
  3294. }
  3295. }
  3296. viewState.setPredefined<4>(this, view, eye, program, _render, draw);
  3297. m_uniformBufferFragmentOffset += fragmentUniformBufferSize;
  3298. m_uniformBufferVertexOffset = m_uniformBufferFragmentOffset;
  3299. }
  3300. if (kInvalidHandle != programIdx)
  3301. {
  3302. ProgramMtl& program = m_program[programIdx];
  3303. for (uint32_t sampler = 0; sampler < program.m_samplerCount; ++sampler)
  3304. {
  3305. ProgramMtl::SamplerInfo& samplerInfo = program.m_samplers[sampler];
  3306. UniformHandle handle = samplerInfo.m_uniform;
  3307. int stage = *((int*)m_uniforms[handle.idx]);
  3308. const Binding& bind = renderBind.m_bind[stage];
  3309. Binding& current = currentBind.m_bind[stage];
  3310. if (current.m_idx != bind.m_idx
  3311. || current.m_un.m_draw.m_textureFlags != bind.m_un.m_draw.m_textureFlags
  3312. || programChanged)
  3313. {
  3314. if (kInvalidHandle != bind.m_idx)
  3315. {
  3316. TextureMtl& texture = m_textures[bind.m_idx];
  3317. texture.commit(samplerInfo.m_index
  3318. , !samplerInfo.m_fragment
  3319. , samplerInfo.m_fragment
  3320. , bind.m_un.m_draw.m_textureFlags
  3321. );
  3322. }
  3323. }
  3324. current = bind;
  3325. }
  3326. }
  3327. if (0 != currentState.m_streamMask)
  3328. {
  3329. uint32_t numVertices = draw.m_numVertices;
  3330. if (UINT32_MAX == numVertices)
  3331. {
  3332. const VertexBufferMtl& vb = m_vertexBuffers[currentState.m_stream[0].m_handle.idx];
  3333. uint16_t decl = !isValid(vb.m_decl) ? draw.m_stream[0].m_decl.idx : vb.m_decl.idx;
  3334. const VertexDecl& vertexDecl = m_vertexDecls[decl];
  3335. numVertices = vb.m_size/vertexDecl.m_stride;
  3336. }
  3337. uint32_t numIndices = 0;
  3338. uint32_t numPrimsSubmitted = 0;
  3339. uint32_t numInstances = 0;
  3340. uint32_t numPrimsRendered = 0;
  3341. uint32_t numDrawIndirect = 0;
  3342. if (hasOcclusionQuery)
  3343. {
  3344. m_occlusionQuery.begin(rce, _render, draw.m_occlusionQuery);
  3345. }
  3346. if (isValid(draw.m_indirectBuffer) )
  3347. {
  3348. }
  3349. else
  3350. {
  3351. if (isValid(draw.m_indexBuffer) )
  3352. {
  3353. const IndexBufferMtl& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  3354. MTLIndexType indexType = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32;
  3355. if (UINT32_MAX == draw.m_numIndices)
  3356. {
  3357. const uint32_t indexSize = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
  3358. numIndices = ib.m_size/indexSize;
  3359. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  3360. numInstances = draw.m_numInstances;
  3361. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  3362. rce.drawIndexedPrimitives(prim.m_type, numIndices, indexType, ib.getBuffer(), 0, draw.m_numInstances);
  3363. }
  3364. else if (prim.m_min <= draw.m_numIndices)
  3365. {
  3366. const uint32_t indexSize = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32) ? 2 : 4;
  3367. numIndices = draw.m_numIndices;
  3368. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  3369. numInstances = draw.m_numInstances;
  3370. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  3371. rce.drawIndexedPrimitives(prim.m_type, numIndices, indexType, ib.getBuffer(), draw.m_startIndex * indexSize,numInstances);
  3372. }
  3373. }
  3374. else
  3375. {
  3376. numPrimsSubmitted = numVertices/prim.m_div - prim.m_sub;
  3377. numInstances = draw.m_numInstances;
  3378. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  3379. rce.drawPrimitives(prim.m_type, 0, draw.m_numVertices, draw.m_numInstances);
  3380. }
  3381. }
  3382. if (hasOcclusionQuery)
  3383. {
  3384. m_occlusionQuery.end(rce);
  3385. }
  3386. statsNumPrimsSubmitted[primIndex] += numPrimsSubmitted;
  3387. statsNumPrimsRendered[primIndex] += numPrimsRendered;
  3388. statsNumInstances[primIndex] += numInstances;
  3389. statsNumDrawIndirect[primIndex] += numDrawIndirect;
  3390. statsNumIndices += numIndices;
  3391. }
  3392. }
  3393. if (wasCompute)
  3394. {
  3395. //invalidateCompute();
  3396. }
  3397. submitBlit(bs, BGFX_CONFIG_MAX_VIEWS);
  3398. if (0 < _render->m_numRenderItems)
  3399. {
  3400. captureElapsed = -bx::getHPCounter();
  3401. capture();
  3402. rce = m_renderCommandEncoder;
  3403. captureElapsed += bx::getHPCounter();
  3404. }
  3405. }
  3406. if (BX_ENABLED(BGFX_CONFIG_DEBUG_MTL) )
  3407. {
  3408. if (0 < _render->m_numRenderItems)
  3409. {
  3410. rce.popDebugGroup();
  3411. }
  3412. }
  3413. int64_t timeEnd = bx::getHPCounter();
  3414. int64_t frameTime = timeEnd - timeBegin;
  3415. static int64_t min = frameTime;
  3416. static int64_t max = frameTime;
  3417. min = bx::min<int64_t>(min, frameTime);
  3418. max = bx::max<int64_t>(max, frameTime);
  3419. static uint32_t maxGpuLatency = 0;
  3420. static double maxGpuElapsed = 0.0f;
  3421. double elapsedGpuMs = 0.0;
  3422. do
  3423. {
  3424. double toGpuMs = 1000.0 / double(m_gpuTimer.m_frequency);
  3425. elapsedGpuMs = m_gpuTimer.m_elapsed * toGpuMs;
  3426. maxGpuElapsed = elapsedGpuMs > maxGpuElapsed ? elapsedGpuMs : maxGpuElapsed;
  3427. }
  3428. while (m_gpuTimer.get() );
  3429. maxGpuLatency = bx::uint32_imax(maxGpuLatency, m_gpuTimer.m_control.available()-1);
  3430. const int64_t timerFreq = bx::getHPFrequency();
  3431. Stats& perfStats = _render->m_perfStats;
  3432. perfStats.cpuTimeBegin = timeBegin;
  3433. perfStats.cpuTimeEnd = timeEnd;
  3434. perfStats.cpuTimerFreq = timerFreq;
  3435. perfStats.gpuTimeBegin = m_gpuTimer.m_begin;
  3436. perfStats.gpuTimeEnd = m_gpuTimer.m_end;
  3437. perfStats.gpuTimerFreq = m_gpuTimer.m_frequency;
  3438. perfStats.numDraw = statsKeyType[0];
  3439. perfStats.numCompute = statsKeyType[1];
  3440. perfStats.maxGpuLatency = maxGpuLatency;
  3441. bx::memCopy(perfStats.numPrims, statsNumPrimsRendered, sizeof(perfStats.numPrims) );
  3442. perfStats.gpuMemoryMax = -INT64_MAX;
  3443. perfStats.gpuMemoryUsed = -INT64_MAX;
  3444. rce.setTriangleFillMode(MTLTriangleFillModeFill);
  3445. if (_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) )
  3446. {
  3447. rce.pushDebugGroup("debugstats");
  3448. TextVideoMem& tvm = m_textVideoMem;
  3449. static int64_t next = timeEnd;
  3450. if (timeEnd >= next)
  3451. {
  3452. next = timeEnd + timerFreq;
  3453. double freq = double(timerFreq);
  3454. double toMs = 1000.0/freq;
  3455. tvm.clear();
  3456. uint16_t pos = 0;
  3457. tvm.printf(0, pos++, BGFX_CONFIG_DEBUG ? 0x8c : 0x8f
  3458. , " %s / " BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME " "
  3459. , getRendererName()
  3460. );
  3461. pos = 10;
  3462. tvm.printf(10, pos++, 0x8b, " Frame: %7.3f, % 7.3f \x1f, % 7.3f \x1e [ms] / % 6.2f FPS "
  3463. , double(frameTime)*toMs
  3464. , double(min)*toMs
  3465. , double(max)*toMs
  3466. , freq/frameTime
  3467. );
  3468. const uint32_t msaa = (m_resolution.reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  3469. tvm.printf(10, pos++, 0x8b, " Reset flags: [%c] vsync, [%c] MSAAx%d, [%c] MaxAnisotropy "
  3470. , !!(m_resolution.reset&BGFX_RESET_VSYNC) ? '\xfe' : ' '
  3471. , 0 != msaa ? '\xfe' : ' '
  3472. , 1<<msaa
  3473. , !!(m_resolution.reset&BGFX_RESET_MAXANISOTROPY) ? '\xfe' : ' '
  3474. );
  3475. double elapsedCpuMs = double(frameTime)*toMs;
  3476. tvm.printf(10, pos++, 0x8b, " Submitted: %4d (draw %4d, compute %4d) / CPU %3.4f [ms] %c GPU %3.4f [ms] (latency %d)"
  3477. , _render->m_numRenderItems
  3478. , statsKeyType[0]
  3479. , statsKeyType[1]
  3480. , elapsedCpuMs
  3481. , elapsedCpuMs > maxGpuElapsed ? '>' : '<'
  3482. , maxGpuElapsed
  3483. , maxGpuLatency
  3484. );
  3485. maxGpuLatency = 0;
  3486. maxGpuElapsed = 0.0;
  3487. for (uint32_t ii = 0; ii < Topology::Count; ++ii)
  3488. {
  3489. tvm.printf(10, pos++, 0x8b, " %10s: %7d (#inst: %5d), submitted: %7d"
  3490. , getName(Topology::Enum(ii) )
  3491. , statsNumPrimsRendered[ii]
  3492. , statsNumInstances[ii]
  3493. , statsNumPrimsSubmitted[ii]
  3494. );
  3495. }
  3496. tvm.printf(10, pos++, 0x8b, " Indices: %7d ", statsNumIndices);
  3497. // tvm.printf(10, pos++, 0x8b, " Uniform size: %7d, Max: %7d ", _render->m_uniformEnd, _render->m_uniformMax);
  3498. tvm.printf(10, pos++, 0x8b, " DVB size: %7d ", _render->m_vboffset);
  3499. tvm.printf(10, pos++, 0x8b, " DIB size: %7d ", _render->m_iboffset);
  3500. pos++;
  3501. double captureMs = double(captureElapsed)*toMs;
  3502. tvm.printf(10, pos++, 0x8b, " Capture: %3.4f [ms]", captureMs);
  3503. uint8_t attr[2] = { 0x8c, 0x8a };
  3504. uint8_t attrIndex = _render->m_waitSubmit < _render->m_waitRender;
  3505. tvm.printf(10, pos++, attr[attrIndex &1], " Submit wait: %3.4f [ms]", _render->m_waitSubmit*toMs);
  3506. tvm.printf(10, pos++, attr[(attrIndex+1)&1], " Render wait: %3.4f [ms]", _render->m_waitRender*toMs);
  3507. min = frameTime;
  3508. max = frameTime;
  3509. }
  3510. blit(this, _textVideoMemBlitter, tvm);
  3511. rce = m_renderCommandEncoder;
  3512. rce.popDebugGroup();
  3513. }
  3514. else if (_render->m_debug & BGFX_DEBUG_TEXT)
  3515. {
  3516. rce.pushDebugGroup("debugtext");
  3517. blit(this, _textVideoMemBlitter, _render->m_textVideoMem);
  3518. rce = m_renderCommandEncoder;
  3519. rce.popDebugGroup();
  3520. }
  3521. rce.endEncoding();
  3522. m_renderCommandEncoder = 0;
  3523. m_renderCommandEncoderFrameBufferHandle.idx = kInvalidHandle;
  3524. if (m_screenshotTarget)
  3525. {
  3526. RenderPassDescriptor renderPassDescriptor = newRenderPassDescriptor();
  3527. renderPassDescriptor.colorAttachments[0].texture = currentDrawable().texture;
  3528. renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
  3529. rce = m_commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor);
  3530. rce.setCullMode(MTLCullModeNone);
  3531. rce.setRenderPipelineState(m_screenshotBlitRenderPipelineState);
  3532. rce.setFragmentSamplerState(getSamplerState(BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT), 0);
  3533. rce.setFragmentTexture(m_screenshotTarget, 0);
  3534. rce.drawPrimitives(MTLPrimitiveTypeTriangle, 0, 3, 1);
  3535. rce.endEncoding();
  3536. }
  3537. }
  3538. } /* namespace mtl */ } // namespace bgfx
  3539. #else
  3540. namespace bgfx { namespace mtl
  3541. {
  3542. RendererContextI* rendererCreate(const Init& _init)
  3543. {
  3544. BX_UNUSED(_init);
  3545. return NULL;
  3546. }
  3547. void rendererDestroy()
  3548. {
  3549. }
  3550. } /* namespace mtl */ } // namespace bgfx
  3551. #endif // BGFX_CONFIG_RENDERER_METAL