webgpuEngine.ts 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029
  1. import { Logger } from "../Misc/logger";
  2. import { Nullable, DataArray, IndicesArray, FloatArray, Immutable } from "../types";
  3. import { Color4 } from "../Maths/math";
  4. import { Engine } from "../Engines/engine";
  5. import { InstancingAttributeInfo } from "../Engines/instancingAttributeInfo";
  6. import { RenderTargetCreationOptions } from "../Materials/Textures/renderTargetCreationOptions";
  7. import { InternalTexture, InternalTextureSource } from "../Materials/Textures/internalTexture";
  8. import { IEffectCreationOptions, Effect } from "../Materials/effect";
  9. import { EffectFallbacks } from "../Materials/effectFallbacks";
  10. import { _TimeToken } from "../Instrumentation/timeToken";
  11. import { Constants } from "./constants";
  12. import * as WebGPUConstants from './WebGPU/webgpuConstants';
  13. import { VertexBuffer } from "../Meshes/buffer";
  14. import { WebGPUPipelineContext, IWebGPURenderPipelineStageDescriptor } from './WebGPU/webgpuPipelineContext';
  15. import { IPipelineContext } from './IPipelineContext';
  16. import { DataBuffer } from '../Meshes/dataBuffer';
  17. import { WebGPUDataBuffer } from '../Meshes/WebGPU/webgpuDataBuffer';
  18. import { BaseTexture } from "../Materials/Textures/baseTexture";
  19. import { IShaderProcessor } from "./Processors/iShaderProcessor";
  20. import { WebGPUShaderProcessor } from "./WebGPU/webgpuShaderProcessors";
  21. import { ShaderProcessingContext } from "./Processors/shaderProcessingOptions";
  22. import { WebGPUShaderProcessingContext } from "./WebGPU/webgpuShaderProcessingContext";
  23. import { Tools } from "../Misc/tools";
  24. import { WebGPUTextureHelper } from './WebGPU/webgpuTextureHelper';
  25. import { ISceneLike } from './thinEngine';
  26. import { Scene } from '../scene';
  27. import { WebGPUBufferManager } from './WebGPU/webgpuBufferManager';
  28. import { DepthTextureCreationOptions } from './depthTextureCreationOptions';
  29. import { HardwareTextureWrapper } from '../Materials/Textures/hardwareTextureWrapper';
  30. import { WebGPUHardwareTexture } from './WebGPU/webgpuHardwareTexture';
  31. import { IColor4Like } from '../Maths/math.like';
  32. import { IWebRequest } from '../Misc/interfaces/iWebRequest';
  33. import { UniformBuffer } from '../Materials/uniformBuffer';
  34. import { WebGPURenderPassWrapper } from './WebGPU/webgpuRenderPassWrapper';
  35. import { IMultiRenderTargetOptions } from '../Materials/Textures/multiRenderTarget';
  36. import { WebGPUCacheSampler } from "./WebGPU/webgpuCacheSampler";
  37. import { WebGPUShaderManager } from "./WebGPU/webgpuShaderManager";
  38. import { WebGPUCacheRenderPipeline } from "./WebGPU/webgpuCacheRenderPipeline";
  39. import "../Shaders/clearQuad.vertex";
  40. import "../Shaders/clearQuad.fragment";
  41. declare type VideoTexture = import("../Materials/Textures/videoTexture").VideoTexture;
  42. declare type RenderTargetTexture = import("../Materials/Textures/renderTargetTexture").RenderTargetTexture;
  43. // TODO WEBGPU remove when not needed anymore
  44. function assert(condition: any, msg?: string): asserts condition {
  45. if (!condition) {
  46. throw new Error(msg);
  47. }
  48. }
  49. /**
  50. * Options to load the associated Glslang library
  51. */
  52. export interface GlslangOptions {
  53. /**
  54. * Defines an existing instance of Glslang (usefull in modules who do not access the global instance).
  55. */
  56. glslang?: any;
  57. /**
  58. * Defines the URL of the glslang JS File.
  59. */
  60. jsPath?: string;
  61. /**
  62. * Defines the URL of the glslang WASM File.
  63. */
  64. wasmPath?: string;
  65. }
  66. /**
  67. * Options to create the WebGPU engine
  68. */
  69. export interface WebGPUEngineOptions extends GPURequestAdapterOptions {
  70. /**
  71. * If delta time between frames should be constant
  72. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  73. */
  74. deterministicLockstep?: boolean;
  75. /**
  76. * Maximum about of steps between frames (Default: 4)
  77. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  78. */
  79. lockstepMaxSteps?: number;
  80. /**
  81. * Defines the seconds between each deterministic lock step
  82. */
  83. timeStep?: number;
  84. /**
  85. * Defines that engine should ignore modifying touch action attribute and style
  86. * If not handle, you might need to set it up on your side for expected touch devices behavior.
  87. */
  88. doNotHandleTouchAction?: boolean;
  89. /**
  90. * Defines if webaudio should be initialized as well
  91. * @see http://doc.babylonjs.com/how_to/playing_sounds_and_music
  92. */
  93. audioEngine?: boolean;
  94. /**
  95. * Defines the category of adapter to use.
  96. * Is it the discrete or integrated device.
  97. */
  98. powerPreference?: GPUPowerPreference;
  99. /**
  100. * Defines the device descriptor used to create a device.
  101. */
  102. deviceDescriptor?: GPUDeviceDescriptor;
  103. /**
  104. * Defines the requested Swap Chain Format.
  105. */
  106. swapChainFormat?: GPUTextureFormat;
  107. /**
  108. * Defines wether MSAA is enabled on the canvas.
  109. */
  110. antialiasing?: boolean;
  111. /**
  112. * Defines wether the stencil buffer should be enabled.
  113. */
  114. stencil?: boolean;
  115. /**
  116. * Defines wether we should generate debug markers in the gpu command lists (can be seen with PIX for eg)
  117. */
  118. enableGPUDebugMarkers?: boolean;
  119. /**
  120. * Options to load the associated Glslang library
  121. */
  122. glslangOptions?: GlslangOptions;
  123. }
  124. /**
  125. * The web GPU engine class provides support for WebGPU version of babylon.js.
  126. */
  127. export class WebGPUEngine extends Engine {
  128. // Default glslang options.
  129. private static readonly _glslangDefaultOptions: GlslangOptions = {
  130. jsPath: "https://preview.babylonjs.com/glslang/glslang.js",
  131. wasmPath: "https://preview.babylonjs.com/glslang/glslang.wasm"
  132. };
  133. // Page Life cycle and constants
  134. private readonly _uploadEncoderDescriptor = { label: "upload" };
  135. private readonly _renderEncoderDescriptor = { label: "render" };
  136. private readonly _renderTargetEncoderDescriptor = { label: "renderTarget" };
  137. private readonly _clearDepthValue = 1;
  138. private readonly _clearReverseDepthValue = 0;
  139. private readonly _clearStencilValue = 0;
  140. private readonly _defaultSampleCount = 4; // Only supported value for now.
  141. // Engine Life Cycle
  142. private _canvas: HTMLCanvasElement;
  143. private _options: WebGPUEngineOptions;
  144. private _glslang: any = null;
  145. private _adapter: GPUAdapter;
  146. private _adapterSupportedExtensions: GPUExtensionName[];
  147. private _device: GPUDevice;
  148. private _deviceEnabledExtensions: GPUExtensionName[];
  149. private _context: GPUCanvasContext;
  150. private _swapChain: GPUSwapChain;
  151. private _swapChainTexture: GPUTexture;
  152. private _mainPassSampleCount: number;
  153. private _textureHelper: WebGPUTextureHelper;
  154. private _bufferManager: WebGPUBufferManager;
  155. private _shaderManager: WebGPUShaderManager;
  156. private _cacheSampler: WebGPUCacheSampler;
  157. private _cacheRenderPipeline: WebGPUCacheRenderPipeline;
  158. private _emptyVertexBuffer: VertexBuffer;
  159. private _lastCachedWrapU: number;
  160. private _lastCachedWrapV: number;
  161. private _lastCachedWrapR: number;
  162. private _mrtAttachments: number[];
  163. private _counters: {
  164. numBindGroupsCreation: number;
  165. } = {
  166. numBindGroupsCreation: 0,
  167. };
  168. // Some of the internal state might change during the render pass.
  169. // This happens mainly during clear for the state
  170. // And when the frame starts to swap the target texture from the swap chain
  171. private _mainTexture: GPUTexture;
  172. private _depthTexture: GPUTexture;
  173. private _mainTextureExtends: GPUExtent3D;
  174. private _depthTextureFormat: GPUTextureFormat | undefined;
  175. private _colorFormat: GPUTextureFormat;
  176. // Frame Life Cycle (recreated each frame)
  177. private _uploadEncoder: GPUCommandEncoder;
  178. private _renderEncoder: GPUCommandEncoder;
  179. private _renderTargetEncoder: GPUCommandEncoder;
  180. private _commandBuffers: GPUCommandBuffer[] = [null as any, null as any, null as any];
  181. // Frame Buffer Life Cycle (recreated for each render target pass)
  182. private _currentRenderPass: Nullable<GPURenderPassEncoder> = null;
  183. private _mainRenderPassWrapper: WebGPURenderPassWrapper = new WebGPURenderPassWrapper();
  184. private _rttRenderPassWrapper: WebGPURenderPassWrapper = new WebGPURenderPassWrapper();
  185. private _pendingDebugCommands: Array<[string, Nullable<string>]> = [];
  186. // DrawCall Life Cycle
  187. // Effect is on the parent class
  188. // protected _currentEffect: Nullable<Effect> = null;
  189. private _currentVertexBuffers: Nullable<{ [key: string]: Nullable<VertexBuffer> }> = null;
  190. private _currentIndexBuffer: Nullable<DataBuffer> = null;
  191. private __colorWrite = true;
  192. private _uniformsBuffers: { [name: string]: WebGPUDataBuffer } = {};
  193. private _forceEnableEffect = false;
  194. // TODO WEBGPU remove those variables when code stabilized
  195. /** @hidden */
  196. public dbgShowShaderCode = false;
  197. /** @hidden */
  198. public dbgSanityChecks = false;
  199. /** @hidden */
  200. public dbgGenerateLogs = false;
  201. /** @hidden */
  202. public dbgVerboseLogsForFirstFrames = false;
  203. /** @hidden */
  204. public dbgVerboseLogsNumFrames = 10;
  205. /** @hidden */
  206. public dbgShowWarningsNotImplemented = false;
  207. /**
  208. * Sets this to true to disable the cache for the samplers. You should do it only for testing purpose!
  209. */
  210. public get disableCacheSamplers(): boolean {
  211. return this._cacheSampler ? this._cacheSampler.disabled : false;
  212. }
  213. public set disableCacheSamplers(disable: boolean) {
  214. if (this._cacheSampler) {
  215. this._cacheSampler.disabled = disable;
  216. }
  217. }
  218. /**
  219. * Sets this to true to disable the cache for the render pipelines. You should do it only for testing purpose!
  220. */
  221. public get disableCacheRenderPipelines(): boolean {
  222. return this._cacheRenderPipeline ? this._cacheRenderPipeline.disabled : false;
  223. }
  224. public set disableCacheRenderPipelines(disable: boolean) {
  225. if (this._cacheRenderPipeline) {
  226. this._cacheRenderPipeline.disabled = disable;
  227. }
  228. }
  229. /**
  230. * Gets a boolean indicating if the engine can be instanciated (ie. if a WebGPU context can be found)
  231. * @returns true if the engine can be created
  232. */
  233. public static get IsSupported(): boolean {
  234. return !!navigator.gpu;
  235. }
  236. /**
  237. * Gets a boolean indicating that the engine supports uniform buffers
  238. */
  239. public get supportsUniformBuffers(): boolean {
  240. return true;
  241. }
  242. /** Gets the supported extensions by the WebGPU adapter */
  243. public get supportedExtensions(): Immutable<GPUExtensionName[]> {
  244. return this._adapterSupportedExtensions;
  245. }
  246. /** Gets the currently enabled extensions on the WebGPU device */
  247. public get enabledExtensions(): Immutable<GPUExtensionName[]> {
  248. return this._deviceEnabledExtensions;
  249. }
  250. /**
  251. * Returns the name of the engine
  252. */
  253. public get name(): string {
  254. return "WebGPU";
  255. }
  256. /**
  257. * Returns a string describing the current engine
  258. */
  259. public get description(): string {
  260. let description = this.name + this.version;
  261. return description;
  262. }
  263. /**
  264. * Returns the version of the engine
  265. */
  266. public get version(): number {
  267. return 1;
  268. }
  269. /**
  270. * Create a new instance of the gpu engine asynchronously
  271. * @param canvas Defines the canvas to use to display the result
  272. * @param options Defines the options passed to the engine to create the GPU context dependencies
  273. * @returns a promise that resolves with the created engine
  274. */
  275. public static CreateAsync(canvas: HTMLCanvasElement, options: WebGPUEngineOptions = {}): Promise<WebGPUEngine> {
  276. const engine = new WebGPUEngine(canvas, options);
  277. return new Promise((resolve) => {
  278. engine.initAsync(options.glslangOptions).then(() => resolve(engine));
  279. });
  280. }
  281. /**
  282. * Create a new instance of the gpu engine.
  283. * @param canvas Defines the canvas to use to display the result
  284. * @param options Defines the options passed to the engine to create the GPU context dependencies
  285. */
  286. public constructor(canvas: HTMLCanvasElement, options: WebGPUEngineOptions = {}) {
  287. super(null);
  288. options.deviceDescriptor = options.deviceDescriptor || { };
  289. options.swapChainFormat = options.swapChainFormat || WebGPUConstants.TextureFormat.BGRA8Unorm;
  290. options.antialiasing = options.antialiasing === undefined ? true : options.antialiasing;
  291. options.stencil = options.stencil ?? true;
  292. options.enableGPUDebugMarkers = options.enableGPUDebugMarkers ?? false;
  293. Logger.Log(`Babylon.js v${Engine.Version} - ${this.description} engine`);
  294. if (!navigator.gpu) {
  295. Logger.Error("WebGPU is not supported by your browser.");
  296. return;
  297. }
  298. this._isWebGPU = true;
  299. this._shaderPlatformName = "WEBGPU";
  300. if (options.deterministicLockstep === undefined) {
  301. options.deterministicLockstep = false;
  302. }
  303. if (options.lockstepMaxSteps === undefined) {
  304. options.lockstepMaxSteps = 4;
  305. }
  306. if (options.audioEngine === undefined) {
  307. options.audioEngine = true;
  308. }
  309. this._deterministicLockstep = options.deterministicLockstep;
  310. this._lockstepMaxSteps = options.lockstepMaxSteps;
  311. this._timeStep = options.timeStep || 1 / 60;
  312. this._doNotHandleContextLost = false;
  313. this._canvas = canvas;
  314. this._options = options;
  315. this.premultipliedAlpha = false;
  316. this._hardwareScalingLevel = 1;
  317. this._mainPassSampleCount = options.antialiasing ? this._defaultSampleCount : 1;
  318. this._isStencilEnable = options.stencil;
  319. this._depthCullingState.depthTest = true;
  320. this._depthCullingState.depthFunc = Constants.LEQUAL;
  321. this._depthCullingState.depthMask = true;
  322. this._sharedInit(canvas, !!options.doNotHandleTouchAction, options.audioEngine);
  323. this._shaderProcessor = this._getShaderProcessor();
  324. // TODO. WEBGPU. Use real way to do it.
  325. this._canvas.style.transform = "scaleY(-1)";
  326. }
  327. //------------------------------------------------------------------------------
  328. // Initialization
  329. //------------------------------------------------------------------------------
  330. /**
  331. * Initializes the WebGPU context and dependencies.
  332. * @param glslangOptions Defines the GLSLang compiler options if necessary
  333. * @returns a promise notifying the readiness of the engine.
  334. */
  335. public initAsync(glslangOptions?: GlslangOptions): Promise<void> {
  336. return this._initGlslang(glslangOptions ?? this._options?.glslangOptions)
  337. .then((glslang: any) => {
  338. this._glslang = glslang;
  339. return navigator.gpu!.requestAdapter(this._options);
  340. })
  341. .then((adapter: GPUAdapter | null) => {
  342. this._adapter = adapter!;
  343. this._adapterSupportedExtensions = this._adapter.extensions.slice(0);
  344. const deviceDescriptor = this._options.deviceDescriptor;
  345. if (deviceDescriptor?.extensions) {
  346. const requestedExtensions = deviceDescriptor.extensions;
  347. const validExtensions = [];
  348. const iterator = requestedExtensions[Symbol.iterator]();
  349. while (true) {
  350. const { done, value : extension } = iterator.next();
  351. if (done) {
  352. break;
  353. }
  354. if (this._adapterSupportedExtensions.indexOf(extension) >= 0) {
  355. validExtensions.push(extension);
  356. }
  357. }
  358. deviceDescriptor.extensions = validExtensions;
  359. }
  360. return this._adapter.requestDevice(this._options.deviceDescriptor);
  361. })
  362. .then((device: GPUDevice | null) => {
  363. this._device = device!;
  364. this._deviceEnabledExtensions = this._device.extensions.slice(0);
  365. })
  366. .then(() => {
  367. this._bufferManager = new WebGPUBufferManager(this._device);
  368. this._textureHelper = new WebGPUTextureHelper(this._device, this._glslang, this._bufferManager);
  369. this._shaderManager = new WebGPUShaderManager(this._device);
  370. this._cacheSampler = new WebGPUCacheSampler(this._device);
  371. if (this.dbgVerboseLogsForFirstFrames) {
  372. if ((this as any)._count === undefined) {
  373. (this as any)._count = 0;
  374. console.log("%c frame #" + (this as any)._count + " - begin", "background: #ffff00");
  375. }
  376. }
  377. this._uploadEncoder = this._device.createCommandEncoder(this._uploadEncoderDescriptor);
  378. this._renderEncoder = this._device.createCommandEncoder(this._renderEncoderDescriptor);
  379. this._renderTargetEncoder = this._device.createCommandEncoder(this._renderTargetEncoderDescriptor);
  380. this._emptyVertexBuffer = new VertexBuffer(this, [0], "", false, false, 1, false, 0, 1);
  381. this._cacheRenderPipeline = new WebGPUCacheRenderPipeline(this._device, this._emptyVertexBuffer);
  382. this._cacheRenderPipeline.setDepthCompare(this._depthCullingState.depthFunc);
  383. //this._cacheRenderPipeline.disabled = true;
  384. this._textureHelper.setCommandEncoder(this._uploadEncoder);
  385. this._initializeLimits();
  386. this._initializeContextAndSwapChain();
  387. this._initializeMainAttachments();
  388. this.resize();
  389. })
  390. .catch((e: any) => {
  391. Logger.Error("Can not create WebGPU Device and/or context.");
  392. Logger.Error(e);
  393. if (console.trace) {
  394. console.trace();
  395. }
  396. });
  397. }
  398. private _initGlslang(glslangOptions?: GlslangOptions): Promise<any> {
  399. glslangOptions = glslangOptions || { };
  400. glslangOptions = {
  401. ...WebGPUEngine._glslangDefaultOptions,
  402. ...glslangOptions
  403. };
  404. if (glslangOptions.glslang) {
  405. return Promise.resolve(glslangOptions.glslang);
  406. }
  407. if ((window as any).glslang) {
  408. return (window as any).glslang(glslangOptions!.wasmPath);
  409. }
  410. if (glslangOptions.jsPath && glslangOptions.wasmPath) {
  411. return Tools.LoadScriptAsync(glslangOptions.jsPath)
  412. .then(() => {
  413. return (window as any).glslang(glslangOptions!.wasmPath);
  414. });
  415. }
  416. return Promise.reject("gslang is not available.");
  417. }
  418. private _initializeLimits(): void {
  419. // Init caps
  420. // TODO WEBGPU Real Capability check once limits will be working.
  421. this._caps = {
  422. maxTexturesImageUnits: 16,
  423. maxVertexTextureImageUnits: 16,
  424. maxCombinedTexturesImageUnits: 32,
  425. maxTextureSize: 2048,
  426. maxCubemapTextureSize: 2048,
  427. maxRenderTextureSize: 2048,
  428. maxVertexAttribs: 16,
  429. maxVaryingVectors: 16,
  430. maxFragmentUniformVectors: 1024,
  431. maxVertexUniformVectors: 1024,
  432. standardDerivatives: true,
  433. astc: null,
  434. s3tc: (this._deviceEnabledExtensions.indexOf(WebGPUConstants.ExtensionName.TextureCompressionBC) >= 0 ? true : undefined) as any,
  435. pvrtc: null,
  436. etc1: null,
  437. etc2: null,
  438. bptc: this._deviceEnabledExtensions.indexOf(WebGPUConstants.ExtensionName.TextureCompressionBC) >= 0 ? true : undefined,
  439. maxAnisotropy: 16, // TODO WEBGPU: Retrieve this smartly
  440. uintIndices: true,
  441. fragmentDepthSupported: true,
  442. highPrecisionShaderSupported: true,
  443. colorBufferFloat: true,
  444. textureFloat: true,
  445. textureFloatLinearFiltering: true,
  446. textureFloatRender: true,
  447. textureHalfFloat: true,
  448. textureHalfFloatLinearFiltering: true,
  449. textureHalfFloatRender: true,
  450. textureLOD: true,
  451. drawBuffersExtension: true,
  452. depthTextureExtension: true,
  453. vertexArrayObject: false,
  454. instancedArrays: true,
  455. timerQuery: undefined,
  456. canUseTimestampForTimerQuery: false,
  457. multiview: false,
  458. oculusMultiview: false,
  459. parallelShaderCompile: undefined,
  460. blendMinMax: true,
  461. maxMSAASamples: 4,
  462. canUseGLInstanceID: true,
  463. };
  464. this._caps.parallelShaderCompile = null as any;
  465. this._features = {
  466. forceBitmapOverHTMLImageElement: true,
  467. supportRenderAndCopyToLodForFloatTextures: true,
  468. supportDepthStencilTexture: true,
  469. supportShadowSamplers: true,
  470. uniformBufferHardCheckMatrix: true,
  471. allowTexturePrefiltering: true,
  472. trackUbosInFrame: true,
  473. supportCSM: true,
  474. basisNeedsPOT: false,
  475. support3DTextures: false, // TODO WEBGPU change to true when Chrome supports 3D textures
  476. needTypeSuffixInShaderConstants: true,
  477. supportMSAA: true,
  478. supportSSAO2: true,
  479. supportExtendedTextureFormats: true,
  480. supportSwitchCaseInShader: true,
  481. supportSyncTextureRead: false,
  482. _collectUbosUpdatedInFrame: true,
  483. };
  484. }
  485. private _initializeContextAndSwapChain(): void {
  486. this._context = this._canvas.getContext('gpupresent') as unknown as GPUCanvasContext;
  487. this._swapChain = this._context.configureSwapChain({
  488. device: this._device,
  489. format: this._options.swapChainFormat!,
  490. usage: WebGPUConstants.TextureUsage.OutputAttachment | WebGPUConstants.TextureUsage.CopySrc,
  491. });
  492. this._colorFormat = this._options.swapChainFormat!;
  493. this._mainRenderPassWrapper.colorAttachmentGPUTextures = [new WebGPUHardwareTexture()];
  494. this._mainRenderPassWrapper.colorAttachmentGPUTextures[0].format = this._colorFormat;
  495. if (this.dbgGenerateLogs) {
  496. this._context.getSwapChainPreferredFormat(this._device).then((format) => {
  497. console.log("Swap chain preferred format:", format);
  498. });
  499. }
  500. }
  501. // Set default values as WebGL with depth and stencil attachment for the broadest Compat.
  502. private _initializeMainAttachments(): void {
  503. this._mainTextureExtends = {
  504. width: this.getRenderWidth(),
  505. height: this.getRenderHeight(),
  506. depth: 1
  507. };
  508. let mainColorAttachments: GPURenderPassColorAttachmentDescriptor[];
  509. if (this._options.antialiasing) {
  510. const mainTextureDescriptor: GPUTextureDescriptor = {
  511. size: this._mainTextureExtends,
  512. mipLevelCount: 1,
  513. sampleCount: this._mainPassSampleCount,
  514. dimension: WebGPUConstants.TextureDimension.E2d,
  515. format: this._options.swapChainFormat!,
  516. usage: WebGPUConstants.TextureUsage.OutputAttachment,
  517. };
  518. if (this._mainTexture) {
  519. this._mainTexture.destroy();
  520. }
  521. this._mainTexture = this._device.createTexture(mainTextureDescriptor);
  522. mainColorAttachments = [{
  523. attachment: this._mainTexture.createView(),
  524. loadValue: new Color4(0, 0, 0, 1),
  525. storeOp: WebGPUConstants.StoreOp.Store
  526. }];
  527. }
  528. else {
  529. mainColorAttachments = [{
  530. attachment: undefined as any,
  531. loadValue: new Color4(0, 0, 0, 1),
  532. storeOp: WebGPUConstants.StoreOp.Store
  533. }];
  534. }
  535. this._mainRenderPassWrapper.depthTextureFormat = this.isStencilEnable ? WebGPUConstants.TextureFormat.Depth24PlusStencil8 : WebGPUConstants.TextureFormat.Depth32Float;
  536. this._setDepthTextureFormat(this._mainRenderPassWrapper);
  537. const depthTextureDescriptor: GPUTextureDescriptor = {
  538. size: this._mainTextureExtends,
  539. mipLevelCount: 1,
  540. sampleCount: this._mainPassSampleCount,
  541. dimension: WebGPUConstants.TextureDimension.E2d,
  542. format: this._mainRenderPassWrapper.depthTextureFormat,
  543. usage: WebGPUConstants.TextureUsage.OutputAttachment
  544. };
  545. if (this._depthTexture) {
  546. this._depthTexture.destroy();
  547. }
  548. this._depthTexture = this._device.createTexture(depthTextureDescriptor);
  549. const mainDepthAttachment: GPURenderPassDepthStencilAttachmentDescriptor = {
  550. attachment: this._depthTexture.createView(),
  551. depthLoadValue: this._clearDepthValue,
  552. depthStoreOp: WebGPUConstants.StoreOp.Store,
  553. stencilLoadValue: this._clearStencilValue,
  554. stencilStoreOp: WebGPUConstants.StoreOp.Store,
  555. };
  556. this._mainRenderPassWrapper.renderPassDescriptor = {
  557. colorAttachments: mainColorAttachments,
  558. depthStencilAttachment: mainDepthAttachment
  559. };
  560. if (this._mainRenderPassWrapper.renderPass !== null) {
  561. this._endMainRenderPass();
  562. }
  563. }
  564. /**
  565. * Force a specific size of the canvas
  566. * @param width defines the new canvas' width
  567. * @param height defines the new canvas' height
  568. * @param forceSetSize true to force setting the sizes of the underlying canvas
  569. * @returns true if the size was changed
  570. */
  571. public setSize(width: number, height: number, forceSetSize = false): boolean {
  572. if (!super.setSize(width, height, forceSetSize)) {
  573. return false;
  574. }
  575. if (this.dbgVerboseLogsForFirstFrames) {
  576. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  577. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  578. console.log("frame #" + (this as any)._count + " - setSize called -", width, height);
  579. }
  580. }
  581. this._initializeMainAttachments();
  582. return true;
  583. }
  584. /**
  585. * Gets a shader processor implementation fitting with the current engine type.
  586. * @returns The shader processor implementation.
  587. */
  588. protected _getShaderProcessor(): Nullable<IShaderProcessor> {
  589. return new WebGPUShaderProcessor();
  590. }
  591. /** @hidden */
  592. public _getShaderProcessingContext(): Nullable<ShaderProcessingContext> {
  593. return new WebGPUShaderProcessingContext();
  594. }
  595. //------------------------------------------------------------------------------
  596. // Static Pipeline WebGPU States
  597. //------------------------------------------------------------------------------
  598. /**
  599. * Force the entire cache to be cleared
  600. * You should not have to use this function unless your engine needs to share the WebGPU context with another engine
  601. * @param bruteForce defines a boolean to force clearing ALL caches (including stencil, detoh and alpha states)
  602. */
  603. public wipeCaches(bruteForce?: boolean): void {
  604. if (this.preventCacheWipeBetweenFrames && !bruteForce) {
  605. return;
  606. }
  607. //this._currentEffect = null; // can't reset _currentEffect, else some crashes can occur (for eg in ProceduralTexture which calls bindFrameBuffer (which calls wipeCaches) after having called enableEffect and before drawing into the texture)
  608. // _forceEnableEffect = true assumes the role of _currentEffect = null
  609. this._forceEnableEffect = true;
  610. this._currentIndexBuffer = null;
  611. this._currentVertexBuffers = null;
  612. this._cacheRenderPipeline.setBuffers(null, null);
  613. if (bruteForce) {
  614. this._currentProgram = null;
  615. this._stencilState.reset();
  616. this._cacheRenderPipeline.resetStencilState();
  617. this._depthCullingState.reset();
  618. this._depthCullingState.depthFunc = Constants.LEQUAL;
  619. this._cacheRenderPipeline.resetDepthCullingState();
  620. this._cacheRenderPipeline.setDepthCompare(Constants.LEQUAL);
  621. this._alphaState.reset();
  622. this._alphaMode = Constants.ALPHA_ADD;
  623. this._alphaEquation = Constants.ALPHA_DISABLE;
  624. this._cacheRenderPipeline.setAlphaBlendFactors(this._alphaState._blendFunctionParameters, this._alphaState._blendEquationParameters);
  625. this._cacheRenderPipeline.setAlphaBlendEnabled(false);
  626. this.setColorWrite(true);
  627. }
  628. this._cachedVertexBuffers = null;
  629. this._cachedIndexBuffer = null;
  630. this._cachedEffectForVertexBuffers = null;
  631. }
  632. /**
  633. * Enable or disable color writing
  634. * @param enable defines the state to set
  635. */
  636. public setColorWrite(enable: boolean): void {
  637. this.__colorWrite = enable;
  638. this._cacheRenderPipeline.setWriteMask(enable ? 0xF : 0);
  639. }
  640. /**
  641. * Gets a boolean indicating if color writing is enabled
  642. * @returns the current color writing state
  643. */
  644. public getColorWrite(): boolean {
  645. return this.__colorWrite;
  646. }
  647. //------------------------------------------------------------------------------
  648. // Dynamic WebGPU States
  649. //------------------------------------------------------------------------------
  650. private _viewportsCurrent: Array<{ x: number, y: number, w: number, h: number }> = [{ x: 0, y: 0, w: 0, h: 0 }, { x: 0, y: 0, w: 0, h: 0 }];
  651. private _resetCurrentViewport(index: number) {
  652. this._viewportsCurrent[index].x = 0;
  653. this._viewportsCurrent[index].y = 0;
  654. this._viewportsCurrent[index].w = 0;
  655. this._viewportsCurrent[index].h = 0;
  656. }
  657. private _applyViewport(renderPass: GPURenderPassEncoder): void {
  658. const index = renderPass === this._mainRenderPassWrapper.renderPass ? 0 : 1;
  659. const x = this._viewportCached.x,
  660. y = this._viewportCached.y,
  661. w = this._viewportCached.z,
  662. h = this._viewportCached.w;
  663. if (this._viewportsCurrent[index].x !== x || this._viewportsCurrent[index].y !== y ||
  664. this._viewportsCurrent[index].w !== w || this._viewportsCurrent[index].h !== h)
  665. {
  666. this._viewportsCurrent[index].x = x;
  667. this._viewportsCurrent[index].y = y;
  668. this._viewportsCurrent[index].w = w;
  669. this._viewportsCurrent[index].h = h;
  670. renderPass.setViewport(x, y, w, h, 0, 1);
  671. if (this.dbgVerboseLogsForFirstFrames) {
  672. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  673. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  674. console.log("frame #" + (this as any)._count + " - viewport applied - (", x, y, w, h, ") current pass is main pass=" + (renderPass === this._mainRenderPassWrapper.renderPass));
  675. }
  676. }
  677. }
  678. }
  679. /** @hidden */
  680. public _viewport(x: number, y: number, width: number, height: number): void {
  681. this._viewportCached.x = x;
  682. this._viewportCached.y = y;
  683. this._viewportCached.z = width;
  684. this._viewportCached.w = height;
  685. }
  686. private _scissorsCurrent: Array<{ x: number, y: number, w: number, h: number }> = [{ x: 0, y: 0, w: 0, h: 0 }, { x: 0, y: 0, w: 0, h: 0 }];
  687. protected _scissorCached = { x: 0, y: 0, z: 0, w: 0 };
  688. private _resetCurrentScissor(index: number) {
  689. this._scissorsCurrent[index].x = 0;
  690. this._scissorsCurrent[index].y = 0;
  691. this._scissorsCurrent[index].w = 0;
  692. this._scissorsCurrent[index].h = 0;
  693. }
  694. private _applyScissor(renderPass: GPURenderPassEncoder): void {
  695. const index = renderPass === this._mainRenderPassWrapper.renderPass ? 0 : 1;
  696. const x = this._scissorCached.x,
  697. y = this._scissorCached.y,
  698. w = this._scissorCached.z,
  699. h = this._scissorCached.w;
  700. if (this._scissorsCurrent[index].x !== x || this._scissorsCurrent[index].y !== y ||
  701. this._scissorsCurrent[index].w !== w || this._scissorsCurrent[index].h !== h)
  702. {
  703. this._scissorsCurrent[index].x = x;
  704. this._scissorsCurrent[index].y = y;
  705. this._scissorsCurrent[index].w = w;
  706. this._scissorsCurrent[index].h = h;
  707. renderPass.setScissorRect(x, y, w, h);
  708. if (this.dbgVerboseLogsForFirstFrames) {
  709. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  710. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  711. console.log("frame #" + (this as any)._count + " - scissor applied - (", x, y, w, h, ") current pass is main pass=" + (renderPass === this._mainRenderPassWrapper.renderPass));
  712. }
  713. }
  714. }
  715. }
  716. private _scissorIsActive() {
  717. return this._scissorCached.x !== 0 ||
  718. this._scissorCached.y !== 0 ||
  719. (this._scissorCached.z !== this.getRenderWidth() && this._scissorCached.z !== 0) ||
  720. (this._scissorCached.w !== this.getRenderHeight() && this._scissorCached.w !== 0);
  721. }
  722. public enableScissor(x: number, y: number, width: number, height: number): void {
  723. this._scissorCached.x = x;
  724. this._scissorCached.y = y;
  725. this._scissorCached.z = width;
  726. this._scissorCached.w = height;
  727. }
  728. public disableScissor() {
  729. this._scissorCached.x = 0;
  730. this._scissorCached.y = 0;
  731. this._scissorCached.z = this.getRenderWidth();
  732. this._scissorCached.w = this.getRenderHeight();
  733. }
  734. /**
  735. * Clear the current render buffer or the current render target (if any is set up)
  736. * @param color defines the color to use
  737. * @param backBuffer defines if the back buffer must be cleared
  738. * @param depth defines if the depth buffer must be cleared
  739. * @param stencil defines if the stencil buffer must be cleared
  740. */
  741. public clear(color: Nullable<IColor4Like>, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
  742. // Some PGs are using color3...
  743. if (color && color.a === undefined) {
  744. color.a = 1;
  745. }
  746. if (this.dbgVerboseLogsForFirstFrames) {
  747. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  748. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  749. console.log("frame #" + (this as any)._count + " - clear called - backBuffer=", backBuffer, " depth=", depth, " stencil=", stencil);
  750. }
  751. }
  752. // We need to recreate the render pass so that the new parameters for clear color / depth / stencil are taken into account
  753. if (this._currentRenderTarget) {
  754. if (this._currentRenderPass) {
  755. this._endRenderTargetRenderPass();
  756. }
  757. this._startRenderTargetRenderPass(this._currentRenderTarget!, backBuffer ? color : null, backBuffer ? color : null, depth, stencil);
  758. } else {
  759. this._startMainRenderPass(true, backBuffer ? color : null, depth, stencil);
  760. }
  761. }
  762. /**
  763. * Clears a list of attachments
  764. * @param attachments list of the attachments
  765. * @param colorMain clear color for the main attachment (the first one)
  766. * @param colorOthers clear color for the other attachments
  767. * @param clearDepth true to clear the depth buffer. Used only for the first attachment
  768. * @param clearStencil true to clear the stencil buffer. Used only for the first attachment
  769. */
  770. public clearAttachments(attachments: number[], colorMain: Nullable<IColor4Like>, colorOthers: Nullable<IColor4Like>, clearDepth: boolean, clearStencil: boolean): void {
  771. if (attachments.length === 0 || !this._currentRenderTarget) {
  772. return;
  773. }
  774. if (this._currentRenderPass) {
  775. this._endRenderTargetRenderPass();
  776. }
  777. this._startRenderTargetRenderPass(this._currentRenderTarget!, colorMain, colorOthers, clearDepth, clearStencil);
  778. }
  779. //------------------------------------------------------------------------------
  780. // Vertex/Index Buffers
  781. //------------------------------------------------------------------------------
  782. /**
  783. * Creates a vertex buffer
  784. * @param data the data for the vertex buffer
  785. * @returns the new buffer
  786. */
  787. public createVertexBuffer(data: DataArray): DataBuffer {
  788. let view: ArrayBufferView;
  789. if (data instanceof Array) {
  790. view = new Float32Array(data);
  791. }
  792. else if (data instanceof ArrayBuffer) {
  793. view = new Uint8Array(data);
  794. }
  795. else {
  796. view = data;
  797. }
  798. const dataBuffer = this._bufferManager.createBuffer(view, WebGPUConstants.BufferUsage.Vertex | WebGPUConstants.BufferUsage.CopyDst);
  799. return dataBuffer;
  800. }
  801. /**
  802. * Creates a vertex buffer
  803. * @param data the data for the dynamic vertex buffer
  804. * @returns the new buffer
  805. */
  806. public createDynamicVertexBuffer(data: DataArray): DataBuffer {
  807. return this.createVertexBuffer(data);
  808. }
  809. /**
  810. * Updates a vertex buffer.
  811. * @param vertexBuffer the vertex buffer to update
  812. * @param data the data used to update the vertex buffer
  813. * @param byteOffset the byte offset of the data
  814. * @param byteLength the byte length of the data
  815. */
  816. public updateDynamicVertexBuffer(vertexBuffer: DataBuffer, data: DataArray, byteOffset?: number, byteLength?: number): void {
  817. const dataBuffer = vertexBuffer as WebGPUDataBuffer;
  818. if (byteOffset === undefined) {
  819. byteOffset = 0;
  820. }
  821. let view: ArrayBufferView;
  822. if (byteLength === undefined) {
  823. if (data instanceof Array) {
  824. view = new Float32Array(data);
  825. }
  826. else if (data instanceof ArrayBuffer) {
  827. view = new Uint8Array(data);
  828. }
  829. else {
  830. view = data;
  831. }
  832. byteLength = view.byteLength;
  833. } else {
  834. if (data instanceof Array) {
  835. view = new Float32Array(data);
  836. }
  837. else if (data instanceof ArrayBuffer) {
  838. view = new Uint8Array(data);
  839. }
  840. else {
  841. view = data;
  842. }
  843. }
  844. this._bufferManager.setSubData(dataBuffer, byteOffset, view, 0, byteLength);
  845. }
  846. /**
  847. * Creates a new index buffer
  848. * @param indices defines the content of the index buffer
  849. * @param updatable defines if the index buffer must be updatable - not used in WebGPU
  850. * @returns a new buffer
  851. */
  852. public createIndexBuffer(indices: IndicesArray, updatable?: boolean): DataBuffer {
  853. let is32Bits = true;
  854. let view: ArrayBufferView;
  855. if (indices instanceof Uint32Array || indices instanceof Int32Array) {
  856. view = indices;
  857. }
  858. else if (indices instanceof Uint16Array) {
  859. view = indices;
  860. is32Bits = false;
  861. }
  862. else {
  863. if (indices.length > 65535) {
  864. view = new Uint32Array(indices);
  865. }
  866. else {
  867. view = new Uint16Array(indices);
  868. is32Bits = false;
  869. }
  870. }
  871. const dataBuffer = this._bufferManager.createBuffer(view, WebGPUConstants.BufferUsage.Index | WebGPUConstants.BufferUsage.CopyDst);
  872. dataBuffer.is32Bits = is32Bits;
  873. return dataBuffer;
  874. }
  875. /**
  876. * Update an index buffer
  877. * @param indexBuffer defines the target index buffer
  878. * @param indices defines the data to update
  879. * @param offset defines the offset in the target index buffer where update should start
  880. */
  881. public updateDynamicIndexBuffer(indexBuffer: DataBuffer, indices: IndicesArray, offset: number = 0): void {
  882. const gpuBuffer = indexBuffer as WebGPUDataBuffer;
  883. var view: ArrayBufferView;
  884. if (indices instanceof Uint16Array) {
  885. if (indexBuffer.is32Bits) {
  886. view = Uint32Array.from(indices);
  887. }
  888. else {
  889. view = indices;
  890. }
  891. }
  892. else if (indices instanceof Uint32Array) {
  893. if (indexBuffer.is32Bits) {
  894. view = indices;
  895. }
  896. else {
  897. view = Uint16Array.from(indices);
  898. }
  899. }
  900. else {
  901. if (indexBuffer.is32Bits) {
  902. view = new Uint32Array(indices);
  903. }
  904. else {
  905. view = new Uint16Array(indices);
  906. }
  907. }
  908. this._bufferManager.setSubData(gpuBuffer, offset, view);
  909. }
  910. /** @hidden */
  911. public bindBuffersDirectly(vertexBuffer: DataBuffer, indexBuffer: DataBuffer, vertexDeclaration: number[], vertexStrideSize: number, effect: Effect): void {
  912. throw "Not implemented on WebGPU so far.";
  913. }
  914. /** @hidden */
  915. public updateAndBindInstancesBuffer(instancesBuffer: DataBuffer, data: Float32Array, offsetLocations: number[] | InstancingAttributeInfo[]): void {
  916. throw "Not implemented on WebGPU so far.";
  917. }
  918. /**
  919. * Bind a list of vertex buffers with the engine
  920. * @param vertexBuffers defines the list of vertex buffers to bind
  921. * @param indexBuffer defines the index buffer to bind
  922. * @param effect defines the effect associated with the vertex buffers
  923. */
  924. public bindBuffers(vertexBuffers: { [key: string]: Nullable<VertexBuffer> }, indexBuffer: Nullable<DataBuffer>, effect: Effect): void {
  925. this._currentIndexBuffer = indexBuffer;
  926. this._currentVertexBuffers = vertexBuffers;
  927. this._cacheRenderPipeline.setBuffers(vertexBuffers, indexBuffer);
  928. }
  929. /** @hidden */
  930. public _releaseBuffer(buffer: DataBuffer): boolean {
  931. return this._bufferManager.releaseBuffer(buffer);
  932. }
  933. //------------------------------------------------------------------------------
  934. // UBO
  935. //------------------------------------------------------------------------------
  936. public createUniformBuffer(elements: FloatArray): DataBuffer {
  937. let view: Float32Array;
  938. if (elements instanceof Array) {
  939. view = new Float32Array(elements);
  940. }
  941. else {
  942. view = elements;
  943. }
  944. const dataBuffer = this._bufferManager.createBuffer(view, WebGPUConstants.BufferUsage.Uniform | WebGPUConstants.BufferUsage.CopyDst);
  945. return dataBuffer;
  946. }
  947. public createDynamicUniformBuffer(elements: FloatArray): DataBuffer {
  948. return this.createUniformBuffer(elements);
  949. }
  950. public updateUniformBuffer(uniformBuffer: DataBuffer, elements: FloatArray, offset?: number, count?: number): void {
  951. if (offset === undefined) {
  952. offset = 0;
  953. }
  954. const dataBuffer = uniformBuffer as WebGPUDataBuffer;
  955. let view: Float32Array;
  956. if (count === undefined) {
  957. if (elements instanceof Float32Array) {
  958. view = elements;
  959. } else {
  960. view = new Float32Array(elements);
  961. }
  962. count = view.byteLength;
  963. } else {
  964. if (elements instanceof Float32Array) {
  965. view = elements;
  966. } else {
  967. view = new Float32Array(elements);
  968. }
  969. }
  970. this._bufferManager.setSubData(dataBuffer, offset, view, 0, count);
  971. }
  972. public bindUniformBufferBase(buffer: DataBuffer, location: number, name: string): void {
  973. this._uniformsBuffers[name] = buffer as WebGPUDataBuffer;
  974. }
  975. //------------------------------------------------------------------------------
  976. // Effects
  977. //------------------------------------------------------------------------------
  978. /**
  979. * Create a new effect (used to store vertex/fragment shaders)
  980. * @param baseName defines the base name of the effect (The name of file without .fragment.fx or .vertex.fx)
  981. * @param attributesNamesOrOptions defines either a list of attribute names or an IEffectCreationOptions object
  982. * @param uniformsNamesOrEngine defines either a list of uniform names or the engine to use
  983. * @param samplers defines an array of string used to represent textures
  984. * @param defines defines the string containing the defines to use to compile the shaders
  985. * @param fallbacks defines the list of potential fallbacks to use if shader conmpilation fails
  986. * @param onCompiled defines a function to call when the effect creation is successful
  987. * @param onError defines a function to call when the effect creation has failed
  988. * @param indexParameters defines an object containing the index values to use to compile shaders (like the maximum number of simultaneous lights)
  989. * @returns the new Effect
  990. */
  991. public createEffect(baseName: any, attributesNamesOrOptions: string[] | IEffectCreationOptions, uniformsNamesOrEngine: string[] | Engine, samplers?: string[], defines?: string, fallbacks?: EffectFallbacks,
  992. onCompiled?: Nullable<(effect: Effect) => void>, onError?: Nullable<(effect: Effect, errors: string) => void>, indexParameters?: any): Effect {
  993. const vertex = baseName.vertexElement || baseName.vertex || baseName.vertexToken || baseName.vertexSource || baseName;
  994. const fragment = baseName.fragmentElement || baseName.fragment || baseName.fragmentToken || baseName.fragmentSource || baseName;
  995. const name = vertex + "+" + fragment + "@" + (defines ? defines : (<IEffectCreationOptions>attributesNamesOrOptions).defines);
  996. if (this._compiledEffects[name]) {
  997. var compiledEffect = <Effect>this._compiledEffects[name];
  998. if (onCompiled && compiledEffect.isReady()) {
  999. onCompiled(compiledEffect);
  1000. }
  1001. return compiledEffect;
  1002. }
  1003. var effect = new Effect(baseName, attributesNamesOrOptions, uniformsNamesOrEngine, samplers, this, defines, fallbacks, onCompiled, onError, indexParameters, name);
  1004. this._compiledEffects[name] = effect;
  1005. return effect;
  1006. }
  1007. private _compileRawShaderToSpirV(source: string, type: string): Uint32Array {
  1008. return this._glslang.compileGLSL(source, type);
  1009. }
  1010. private _compileShaderToSpirV(source: string, type: string, defines: Nullable<string>, shaderVersion: string): Uint32Array {
  1011. return this._compileRawShaderToSpirV(shaderVersion + (defines ? defines + "\n" : "") + source, type);
  1012. }
  1013. private _createPipelineStageDescriptor(vertexShader: Uint32Array, fragmentShader: Uint32Array): IWebGPURenderPipelineStageDescriptor {
  1014. return {
  1015. vertexStage: {
  1016. module: this._device.createShaderModule({
  1017. code: vertexShader,
  1018. }),
  1019. entryPoint: "main",
  1020. },
  1021. fragmentStage: {
  1022. module: this._device.createShaderModule({
  1023. code: fragmentShader,
  1024. }),
  1025. entryPoint: "main"
  1026. }
  1027. };
  1028. }
  1029. private _compileRawPipelineStageDescriptor(vertexCode: string, fragmentCode: string): IWebGPURenderPipelineStageDescriptor {
  1030. var vertexShader = this._compileRawShaderToSpirV(vertexCode, "vertex");
  1031. var fragmentShader = this._compileRawShaderToSpirV(fragmentCode, "fragment");
  1032. return this._createPipelineStageDescriptor(vertexShader, fragmentShader);
  1033. }
  1034. private _compilePipelineStageDescriptor(vertexCode: string, fragmentCode: string, defines: Nullable<string>): IWebGPURenderPipelineStageDescriptor {
  1035. this.onBeforeShaderCompilationObservable.notifyObservers(this);
  1036. var shaderVersion = "#version 450\n";
  1037. var vertexShader = this._compileShaderToSpirV(vertexCode, "vertex", defines, shaderVersion);
  1038. var fragmentShader = this._compileShaderToSpirV(fragmentCode, "fragment", defines, shaderVersion);
  1039. let program = this._createPipelineStageDescriptor(vertexShader, fragmentShader);
  1040. this.onAfterShaderCompilationObservable.notifyObservers(this);
  1041. return program;
  1042. }
  1043. /** @hidden */
  1044. public createRawShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): WebGLProgram {
  1045. throw "Not available on WebGPU";
  1046. }
  1047. /** @hidden */
  1048. public createShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, defines: Nullable<string>, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): WebGLProgram {
  1049. throw "Not available on WebGPU";
  1050. }
  1051. /**
  1052. * Creates a new pipeline context
  1053. * @param shaderProcessingContext defines the shader processing context used during the processing if available
  1054. * @returns the new pipeline
  1055. */
  1056. public createPipelineContext(shaderProcessingContext: Nullable<ShaderProcessingContext>): IPipelineContext {
  1057. return new WebGPUPipelineContext(shaderProcessingContext! as WebGPUShaderProcessingContext, this);
  1058. }
  1059. /** @hidden */
  1060. public _preparePipelineContext(pipelineContext: IPipelineContext, vertexSourceCode: string, fragmentSourceCode: string, createAsRaw: boolean, rawVertexSourceCode: string, rawFragmentSourceCode: string,
  1061. rebuildRebind: any,
  1062. defines: Nullable<string>,
  1063. transformFeedbackVaryings: Nullable<string[]>,
  1064. key: string) {
  1065. const webGpuContext = pipelineContext as WebGPUPipelineContext;
  1066. if (this.dbgShowShaderCode) {
  1067. console.log(defines);
  1068. console.log(vertexSourceCode);
  1069. console.log(fragmentSourceCode);
  1070. }
  1071. webGpuContext.sources = {
  1072. fragment: fragmentSourceCode,
  1073. vertex: vertexSourceCode,
  1074. rawVertex: rawVertexSourceCode,
  1075. rawFragment: rawFragmentSourceCode,
  1076. };
  1077. if (createAsRaw) {
  1078. webGpuContext.stages = this._compileRawPipelineStageDescriptor(vertexSourceCode, fragmentSourceCode);
  1079. }
  1080. else {
  1081. webGpuContext.stages = this._compilePipelineStageDescriptor(vertexSourceCode, fragmentSourceCode, defines);
  1082. }
  1083. }
  1084. /**
  1085. * Gets the list of active attributes for a given WebGPU program
  1086. * @param pipelineContext defines the pipeline context to use
  1087. * @param attributesNames defines the list of attribute names to get
  1088. * @returns an array of indices indicating the offset of each attribute
  1089. */
  1090. public getAttributes(pipelineContext: IPipelineContext, attributesNames: string[]): number[] {
  1091. const results = new Array(attributesNames.length);
  1092. const gpuPipelineContext = (pipelineContext as WebGPUPipelineContext);
  1093. // TODO WEBGPU. Hard coded for WebGPU until an introspection lib is available.
  1094. // Should be done at processing time, not need to double the work in here.
  1095. for (let i = 0; i < attributesNames.length; i++) {
  1096. const attributeName = attributesNames[i];
  1097. const attributeLocation = gpuPipelineContext.shaderProcessingContext.availableAttributes[attributeName];
  1098. if (attributeLocation === undefined) {
  1099. continue;
  1100. }
  1101. results[i] = attributeLocation;
  1102. }
  1103. return results;
  1104. }
  1105. /**
  1106. * Activates an effect, mkaing it the current one (ie. the one used for rendering)
  1107. * @param effect defines the effect to activate
  1108. */
  1109. public enableEffect(effect: Nullable<Effect>): void {
  1110. if (!effect || effect === this._currentEffect && !this._forceEnableEffect) {
  1111. return;
  1112. }
  1113. this._currentEffect = effect;
  1114. this._forceEnableEffect = false;
  1115. if (effect.onBind) {
  1116. effect.onBind(effect);
  1117. }
  1118. if (effect._onBindObservable) {
  1119. effect._onBindObservable.notifyObservers(effect);
  1120. }
  1121. }
  1122. /** @hidden */
  1123. public _releaseEffect(effect: Effect): void {
  1124. // Effect gets garbage collected without explicit destroy in WebGPU.
  1125. }
  1126. /**
  1127. * Force the engine to release all cached effects. This means that next effect compilation will have to be done completely even if a similar effect was already compiled
  1128. */
  1129. public releaseEffects() {
  1130. // Effect gets garbage collected without explicit destroy in WebGPU.
  1131. }
  1132. public _deletePipelineContext(pipelineContext: IPipelineContext): void {
  1133. const webgpuPipelineContext = pipelineContext as WebGPUPipelineContext;
  1134. if (webgpuPipelineContext) {
  1135. pipelineContext.dispose();
  1136. }
  1137. }
  1138. //------------------------------------------------------------------------------
  1139. // Textures
  1140. //------------------------------------------------------------------------------
  1141. /**
  1142. * Gets a boolean indicating that only power of 2 textures are supported
  1143. * Please note that you can still use non power of 2 textures but in this case the engine will forcefully convert them
  1144. */
  1145. public get needPOTTextures(): boolean {
  1146. return false;
  1147. }
  1148. /** @hidden */
  1149. public _createHardwareTexture(): HardwareTextureWrapper {
  1150. return new WebGPUHardwareTexture();
  1151. }
  1152. /** @hidden */
  1153. public _releaseTexture(texture: InternalTexture): void {
  1154. const index = this._internalTexturesCache.indexOf(texture);
  1155. if (index !== -1) {
  1156. this._internalTexturesCache.splice(index, 1);
  1157. }
  1158. this._textureHelper.releaseTexture(texture);
  1159. }
  1160. /** @hidden */
  1161. public _getRGBABufferInternalSizedFormat(type: number, format?: number): number {
  1162. return Constants.TEXTUREFORMAT_RGBA;
  1163. }
  1164. public updateTextureComparisonFunction(texture: InternalTexture, comparisonFunction: number): void {
  1165. texture._comparisonFunction = comparisonFunction;
  1166. }
  1167. /**
  1168. * Usually called from Texture.ts.
  1169. * Passed information to create a hardware texture
  1170. * @param url defines a value which contains one of the following:
  1171. * * A conventional http URL, e.g. 'http://...' or 'file://...'
  1172. * * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
  1173. * * An indicator that data being passed using the buffer parameter, e.g. 'data:mytexture.jpg'
  1174. * @param noMipmap defines a boolean indicating that no mipmaps shall be generated. Ignored for compressed textures. They must be in the file
  1175. * @param invertY when true, image is flipped when loaded. You probably want true. Certain compressed textures may invert this if their default is inverted (eg. ktx)
  1176. * @param scene needed for loading to the correct scene
  1177. * @param samplingMode mode with should be used sample / access the texture (Default: Texture.TRILINEAR_SAMPLINGMODE)
  1178. * @param onLoad optional callback to be called upon successful completion
  1179. * @param onError optional callback to be called upon failure
  1180. * @param buffer a source of a file previously fetched as either a base64 string, an ArrayBuffer (compressed or image format), HTMLImageElement (image format), or a Blob
  1181. * @param fallback an internal argument in case the function must be called again, due to etc1 not having alpha capabilities
  1182. * @param format internal format. Default: RGB when extension is '.jpg' else RGBA. Ignored for compressed textures
  1183. * @param forcedExtension defines the extension to use to pick the right loader
  1184. * @param mimeType defines an optional mime type
  1185. * @param loaderOptions options to be passed to the loader
  1186. * @returns a InternalTexture for assignment back into BABYLON.Texture
  1187. */
  1188. public createTexture(url: Nullable<string>, noMipmap: boolean, invertY: boolean, scene: Nullable<ISceneLike>, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
  1189. onLoad: Nullable<() => void> = null, onError: Nullable<(message: string, exception: any) => void> = null,
  1190. buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob | ImageBitmap> = null, fallback: Nullable<InternalTexture> = null, format: Nullable<number> = null,
  1191. forcedExtension: Nullable<string> = null, mimeType?: string, loaderOptions?: any): InternalTexture {
  1192. return this._createTextureBase(
  1193. url, noMipmap, invertY, scene, samplingMode, onLoad, onError,
  1194. (texture: InternalTexture, extension: string, scene: Nullable<ISceneLike>, img: HTMLImageElement | ImageBitmap | { width: number, height: number }, invertY: boolean, noMipmap: boolean, isCompressed: boolean,
  1195. processFunction: (width: number, height: number, img: HTMLImageElement | ImageBitmap | { width: number, height: number }, extension: string, texture: InternalTexture, continuationCallback: () => void) => boolean, samplingMode: number) => {
  1196. const imageBitmap = img as (ImageBitmap | { width: number, height: number}); // we will never get an HTMLImageElement in WebGPU
  1197. texture.baseWidth = imageBitmap.width;
  1198. texture.baseHeight = imageBitmap.height;
  1199. texture.width = imageBitmap.width;
  1200. texture.height = imageBitmap.height;
  1201. texture.format = format ?? -1;
  1202. processFunction(texture.width, texture.height, imageBitmap, extension, texture, () => {});
  1203. if (!texture._hardwareTexture?.underlyingResource) { // the texture could have been created before reaching this point so don't recreate it if already existing
  1204. const gpuTextureWrapper = this._textureHelper.createGPUTextureForInternalTexture(texture, imageBitmap.width, imageBitmap.height);
  1205. if (WebGPUTextureHelper.IsImageBitmap(imageBitmap)) {
  1206. this._textureHelper.updateTexture(imageBitmap, gpuTextureWrapper.underlyingResource!, imageBitmap.width, imageBitmap.height, texture.depth, gpuTextureWrapper.format, 0, 0, invertY, false, 0, 0, this._uploadEncoder);
  1207. if (!noMipmap && !isCompressed) {
  1208. this._generateMipmaps(texture, this._uploadEncoder);
  1209. }
  1210. }
  1211. } else if (!noMipmap && !isCompressed) {
  1212. this._generateMipmaps(texture, this._uploadEncoder);
  1213. }
  1214. if (scene) {
  1215. scene._removePendingData(texture);
  1216. }
  1217. texture.isReady = true;
  1218. texture.onLoadedObservable.notifyObservers(texture);
  1219. texture.onLoadedObservable.clear();
  1220. },
  1221. () => false,
  1222. buffer, fallback, format, forcedExtension, mimeType, loaderOptions
  1223. );
  1224. }
  1225. /** @hidden */
  1226. public _setCubeMapTextureParams(texture: InternalTexture, loadMipmap: boolean) {
  1227. texture.samplingMode = loadMipmap ? Engine.TEXTURE_TRILINEAR_SAMPLINGMODE : Engine.TEXTURE_BILINEAR_SAMPLINGMODE;
  1228. texture._cachedWrapU = Constants.TEXTURE_CLAMP_ADDRESSMODE;
  1229. texture._cachedWrapV = Constants.TEXTURE_CLAMP_ADDRESSMODE;
  1230. }
  1231. /**
  1232. * Creates a cube texture
  1233. * @param rootUrl defines the url where the files to load is located
  1234. * @param scene defines the current scene
  1235. * @param files defines the list of files to load (1 per face)
  1236. * @param noMipmap defines a boolean indicating that no mipmaps shall be generated (false by default)
  1237. * @param onLoad defines an optional callback raised when the texture is loaded
  1238. * @param onError defines an optional callback raised if there is an issue to load the texture
  1239. * @param format defines the format of the data
  1240. * @param forcedExtension defines the extension to use to pick the right loader
  1241. * @param createPolynomials if a polynomial sphere should be created for the cube texture
  1242. * @param lodScale defines the scale applied to environment texture. This manages the range of LOD level used for IBL according to the roughness
  1243. * @param lodOffset defines the offset applied to environment texture. This manages first LOD level used for IBL according to the roughness
  1244. * @param fallback defines texture to use while falling back when (compressed) texture file not found.
  1245. * @param loaderOptions options to be passed to the loader
  1246. * @returns the cube texture as an InternalTexture
  1247. */
  1248. public createCubeTexture(rootUrl: string, scene: Nullable<Scene>, files: Nullable<string[]>, noMipmap?: boolean, onLoad: Nullable<(data?: any) => void> = null,
  1249. onError: Nullable<(message?: string, exception?: any) => void> = null, format?: number, forcedExtension: any = null, createPolynomials: boolean = false, lodScale: number = 0, lodOffset: number = 0, fallback: Nullable<InternalTexture> = null): InternalTexture {
  1250. return this.createCubeTextureBase(
  1251. rootUrl, scene, files, !!noMipmap, onLoad, onError, format, forcedExtension, createPolynomials, lodScale, lodOffset, fallback,
  1252. null,
  1253. (texture: InternalTexture, imgs: HTMLImageElement[] | ImageBitmap[]) => {
  1254. const imageBitmaps = imgs as ImageBitmap[]; // we will always get an ImageBitmap array in WebGPU
  1255. const width = imageBitmaps[0].width;
  1256. const height = width;
  1257. this._setCubeMapTextureParams(texture, !noMipmap);
  1258. texture.format = format ?? -1;
  1259. const gpuTextureWrapper = this._textureHelper.createGPUTextureForInternalTexture(texture, width, height);
  1260. this._textureHelper.updateCubeTextures(imageBitmaps, gpuTextureWrapper.underlyingResource!, width, height, gpuTextureWrapper.format, false, false, 0, 0, this._uploadEncoder);
  1261. if (!noMipmap) {
  1262. this._generateMipmaps(texture, this._uploadEncoder);
  1263. }
  1264. texture.isReady = true;
  1265. texture.onLoadedObservable.notifyObservers(texture);
  1266. texture.onLoadedObservable.clear();
  1267. if (onLoad) {
  1268. onLoad();
  1269. }
  1270. }
  1271. );
  1272. }
  1273. /**
  1274. * Creates a raw texture
  1275. * @param data defines the data to store in the texture
  1276. * @param width defines the width of the texture
  1277. * @param height defines the height of the texture
  1278. * @param format defines the format of the data
  1279. * @param generateMipMaps defines if the engine should generate the mip levels
  1280. * @param invertY defines if data must be stored with Y axis inverted
  1281. * @param samplingMode defines the required sampling mode (Texture.NEAREST_SAMPLINGMODE by default)
  1282. * @param compression defines the compression used (null by default)
  1283. * @param type defines the type fo the data (Engine.TEXTURETYPE_UNSIGNED_INT by default)
  1284. * @returns the raw texture inside an InternalTexture
  1285. */
  1286. public createRawTexture(data: Nullable<ArrayBufferView>, width: number, height: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number,
  1287. compression: Nullable<string> = null, type: number = Constants.TEXTURETYPE_UNSIGNED_INT): InternalTexture
  1288. {
  1289. const texture = new InternalTexture(this, InternalTextureSource.Raw);
  1290. texture.baseWidth = width;
  1291. texture.baseHeight = height;
  1292. texture.width = width;
  1293. texture.height = height;
  1294. texture.format = format;
  1295. texture.generateMipMaps = generateMipMaps;
  1296. texture.samplingMode = samplingMode;
  1297. texture.invertY = invertY;
  1298. texture._compression = compression;
  1299. texture.type = type;
  1300. if (!this._doNotHandleContextLost) {
  1301. texture._bufferView = data;
  1302. }
  1303. this._textureHelper.createGPUTextureForInternalTexture(texture, width, height);
  1304. this.updateRawTexture(texture, data, format, invertY, compression, type);
  1305. this._internalTexturesCache.push(texture);
  1306. return texture;
  1307. }
  1308. /**
  1309. * Creates a new raw cube texture
  1310. * @param data defines the array of data to use to create each face
  1311. * @param size defines the size of the textures
  1312. * @param format defines the format of the data
  1313. * @param type defines the type of the data (like Engine.TEXTURETYPE_UNSIGNED_INT)
  1314. * @param generateMipMaps defines if the engine should generate the mip levels
  1315. * @param invertY defines if data must be stored with Y axis inverted
  1316. * @param samplingMode defines the required sampling mode (like Texture.NEAREST_SAMPLINGMODE)
  1317. * @param compression defines the compression used (null by default)
  1318. * @returns the cube texture as an InternalTexture
  1319. */
  1320. public createRawCubeTexture(data: Nullable<ArrayBufferView[]>, size: number, format: number, type: number,
  1321. generateMipMaps: boolean, invertY: boolean, samplingMode: number,
  1322. compression: Nullable<string> = null): InternalTexture
  1323. {
  1324. const texture = new InternalTexture(this, InternalTextureSource.CubeRaw);
  1325. texture.isCube = true;
  1326. texture.format = format === Constants.TEXTUREFORMAT_RGB ? Constants.TEXTUREFORMAT_RGBA : format;
  1327. texture.type = type;
  1328. texture.generateMipMaps = generateMipMaps;
  1329. texture.width = size;
  1330. texture.height = size;
  1331. texture.samplingMode = samplingMode;
  1332. if (!this._doNotHandleContextLost) {
  1333. texture._bufferViewArray = data;
  1334. }
  1335. this._textureHelper.createGPUTextureForInternalTexture(texture);
  1336. if (data) {
  1337. this.updateRawCubeTexture(texture, data, format, type, invertY, compression);
  1338. }
  1339. return texture;
  1340. }
  1341. /**
  1342. * Creates a new raw cube texture from a specified url
  1343. * @param url defines the url where the data is located
  1344. * @param scene defines the current scene
  1345. * @param size defines the size of the textures
  1346. * @param format defines the format of the data
  1347. * @param type defines the type fo the data (like Engine.TEXTURETYPE_UNSIGNED_INT)
  1348. * @param noMipmap defines if the engine should avoid generating the mip levels
  1349. * @param callback defines a callback used to extract texture data from loaded data
  1350. * @param mipmapGenerator defines to provide an optional tool to generate mip levels
  1351. * @param onLoad defines a callback called when texture is loaded
  1352. * @param onError defines a callback called if there is an error
  1353. * @param samplingMode defines the required sampling mode (like Texture.NEAREST_SAMPLINGMODE)
  1354. * @param invertY defines if data must be stored with Y axis inverted
  1355. * @returns the cube texture as an InternalTexture
  1356. */
  1357. public createRawCubeTextureFromUrl(url: string, scene: Nullable<Scene>, size: number, format: number, type: number, noMipmap: boolean,
  1358. callback: (ArrayBuffer: ArrayBuffer) => Nullable<ArrayBufferView[]>,
  1359. mipmapGenerator: Nullable<((faces: ArrayBufferView[]) => ArrayBufferView[][])>,
  1360. onLoad: Nullable<() => void> = null,
  1361. onError: Nullable<(message?: string, exception?: any) => void> = null,
  1362. samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
  1363. invertY: boolean = false): InternalTexture
  1364. {
  1365. const texture = this.createRawCubeTexture(null, size, format, type, !noMipmap, invertY, samplingMode, null);
  1366. scene?._addPendingData(texture);
  1367. texture.url = url;
  1368. this._internalTexturesCache.push(texture);
  1369. const onerror = (request?: IWebRequest, exception?: any) => {
  1370. scene?._removePendingData(texture);
  1371. if (onError && request) {
  1372. onError(request.status + " " + request.statusText, exception);
  1373. }
  1374. };
  1375. const internalCallback = (data: any) => {
  1376. const width = texture.width;
  1377. const faceDataArrays = callback(data);
  1378. if (!faceDataArrays) {
  1379. return;
  1380. }
  1381. const faces = [0, 2, 4, 1, 3, 5];
  1382. if (mipmapGenerator) {
  1383. const needConversion = format === Constants.TEXTUREFORMAT_RGB;
  1384. const mipData = mipmapGenerator(faceDataArrays);
  1385. const gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1386. const faces = [0, 1, 2, 3, 4, 5];
  1387. for (let level = 0; level < mipData.length; level++) {
  1388. const mipSize = width >> level;
  1389. const allFaces = [];
  1390. for (let faceIndex = 0; faceIndex < 6; faceIndex++) {
  1391. let mipFaceData = mipData[level][faces[faceIndex]];
  1392. if (needConversion) {
  1393. mipFaceData = _convertRGBtoRGBATextureData(mipFaceData, mipSize, mipSize, type);
  1394. }
  1395. allFaces.push(new Uint8Array(mipFaceData.buffer, mipFaceData.byteOffset, mipFaceData.byteLength));
  1396. }
  1397. this._textureHelper.updateCubeTextures(allFaces, gpuTextureWrapper.underlyingResource!, mipSize, mipSize, gpuTextureWrapper.format, invertY, false, 0, 0, this._uploadEncoder);
  1398. }
  1399. }
  1400. else {
  1401. const allFaces = [];
  1402. for (let faceIndex = 0; faceIndex < 6; faceIndex++) {
  1403. allFaces.push(faceDataArrays[faces[faceIndex]]);
  1404. }
  1405. this.updateRawCubeTexture(texture, allFaces, format, type, invertY);
  1406. }
  1407. texture.isReady = true;
  1408. scene?._removePendingData(texture);
  1409. if (onLoad) {
  1410. onLoad();
  1411. }
  1412. };
  1413. this._loadFile(url, (data) => {
  1414. internalCallback(data);
  1415. }, undefined, scene?.offlineProvider, true, onerror);
  1416. return texture;
  1417. }
  1418. /**
  1419. * Creates a new raw 2D array texture
  1420. * @param data defines the data used to create the texture
  1421. * @param width defines the width of the texture
  1422. * @param height defines the height of the texture
  1423. * @param depth defines the number of layers of the texture
  1424. * @param format defines the format of the texture
  1425. * @param generateMipMaps defines if the engine must generate mip levels
  1426. * @param invertY defines if data must be stored with Y axis inverted
  1427. * @param samplingMode defines the required sampling mode (like Texture.NEAREST_SAMPLINGMODE)
  1428. * @param compression defines the compressed used (can be null)
  1429. * @param textureType defines the compressed used (can be null)
  1430. * @returns a new raw 2D array texture (stored in an InternalTexture)
  1431. */
  1432. public createRawTexture2DArray(data: Nullable<ArrayBufferView>, width: number, height: number, depth: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number,
  1433. compression: Nullable<string> = null, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT): InternalTexture
  1434. {
  1435. var source = InternalTextureSource.Raw2DArray;
  1436. var texture = new InternalTexture(this, source);
  1437. if (this.dbgShowWarningsNotImplemented) {
  1438. console.warn("createRawTexture2DArray not implemented yet in WebGPU");
  1439. }
  1440. return texture;
  1441. }
  1442. /**
  1443. * Creates a new raw 3D texture
  1444. * @param data defines the data used to create the texture
  1445. * @param width defines the width of the texture
  1446. * @param height defines the height of the texture
  1447. * @param depth defines the depth of the texture
  1448. * @param format defines the format of the texture
  1449. * @param generateMipMaps defines if the engine must generate mip levels
  1450. * @param invertY defines if data must be stored with Y axis inverted
  1451. * @param samplingMode defines the required sampling mode (like Texture.NEAREST_SAMPLINGMODE)
  1452. * @param compression defines the compressed used (can be null)
  1453. * @param textureType defines the compressed used (can be null)
  1454. * @returns a new raw 3D texture (stored in an InternalTexture)
  1455. */
  1456. public createRawTexture3D(data: Nullable<ArrayBufferView>, width: number, height: number, depth: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number,
  1457. compression: Nullable<string> = null, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT): InternalTexture
  1458. {
  1459. const source = InternalTextureSource.Raw3D;
  1460. const texture = new InternalTexture(this, source);
  1461. texture.baseWidth = width;
  1462. texture.baseHeight = height;
  1463. texture.baseDepth = depth;
  1464. texture.width = width;
  1465. texture.height = height;
  1466. texture.depth = depth;
  1467. texture.format = format;
  1468. texture.type = textureType;
  1469. texture.generateMipMaps = generateMipMaps;
  1470. texture.samplingMode = samplingMode;
  1471. texture.is3D = true;
  1472. if (!this._doNotHandleContextLost) {
  1473. texture._bufferView = data;
  1474. }
  1475. this._textureHelper.createGPUTextureForInternalTexture(texture, width, height);
  1476. this.updateRawTexture3D(texture, data, format, invertY, compression, textureType);
  1477. this._internalTexturesCache.push(texture);
  1478. return texture;
  1479. }
  1480. public generateMipMapsForCubemap(texture: InternalTexture, unbind = true) {
  1481. if (texture.generateMipMaps) {
  1482. let gpuTexture = texture._hardwareTexture?.underlyingResource;
  1483. if (!gpuTexture) {
  1484. this._textureHelper.createGPUTextureForInternalTexture(texture);
  1485. }
  1486. this._generateMipmaps(texture, texture.source === InternalTextureSource.RenderTarget || texture.source === InternalTextureSource.MultiRenderTarget ? this._renderTargetEncoder : undefined);
  1487. }
  1488. }
  1489. /**
  1490. * Update the sampling mode of a given texture
  1491. * @param samplingMode defines the required sampling mode
  1492. * @param texture defines the texture to update
  1493. * @param generateMipMaps defines whether to generate mipmaps for the texture
  1494. */
  1495. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture, generateMipMaps: boolean = false): void {
  1496. if (generateMipMaps) {
  1497. texture.generateMipMaps = true;
  1498. this._generateMipmaps(texture);
  1499. }
  1500. texture.samplingMode = samplingMode;
  1501. }
  1502. /**
  1503. * Update the sampling mode of a given texture
  1504. * @param texture defines the texture to update
  1505. * @param wrapU defines the texture wrap mode of the u coordinates
  1506. * @param wrapV defines the texture wrap mode of the v coordinates
  1507. * @param wrapR defines the texture wrap mode of the r coordinates
  1508. */
  1509. public updateTextureWrappingMode(texture: InternalTexture, wrapU: Nullable<number>, wrapV: Nullable<number> = null, wrapR: Nullable<number> = null): void {
  1510. if (wrapU !== null) {
  1511. texture._cachedWrapU = wrapU;
  1512. this._lastCachedWrapU = wrapU;
  1513. }
  1514. if (wrapV !== null) {
  1515. texture._cachedWrapV = wrapV;
  1516. this._lastCachedWrapV = wrapV;
  1517. }
  1518. if ((texture.is2DArray || texture.is3D) && (wrapR !== null)) {
  1519. texture._cachedWrapR = wrapR;
  1520. this._lastCachedWrapR = wrapR;
  1521. }
  1522. }
  1523. /**
  1524. * Update the dimensions of a texture
  1525. * @param texture texture to update
  1526. * @param width new width of the texture
  1527. * @param height new height of the texture
  1528. * @param depth new depth of the texture
  1529. */
  1530. public updateTextureDimensions(texture: InternalTexture, width: number, height: number, depth: number = 1): void {
  1531. if (!texture._hardwareTexture) {
  1532. // the gpu texture is not created yet, so when it is it will be created with the right dimensions
  1533. return;
  1534. }
  1535. if (texture.width === width && texture.height === height && texture.depth === depth) {
  1536. return;
  1537. }
  1538. texture._hardwareTexture.release(); // don't defer the releasing! Else we will release at the end of this frame the gpu texture we are about to create in the next line...
  1539. this._textureHelper.createGPUTextureForInternalTexture(texture, width, height, depth);
  1540. }
  1541. private _setInternalTexture(name: string, internalTexture: Nullable<InternalTexture>, baseName?: string, textureIndex = 0): void {
  1542. baseName = baseName ?? name;
  1543. if (this._currentEffect) {
  1544. const webgpuPipelineContext = this._currentEffect._pipelineContext as WebGPUPipelineContext;
  1545. if (webgpuPipelineContext.textures[name]) {
  1546. if (webgpuPipelineContext.textures[name]!.texture !== internalTexture) {
  1547. webgpuPipelineContext.bindGroupsCache = {}; // the bind groups need to be rebuilt (at least the bind group owning this texture, but it's easier to just have them all rebuilt)
  1548. }
  1549. webgpuPipelineContext.textures[name]!.texture = internalTexture!;
  1550. }
  1551. else {
  1552. const availableSampler = webgpuPipelineContext.shaderProcessingContext.availableSamplers[baseName];
  1553. if (availableSampler) {
  1554. webgpuPipelineContext.samplers[baseName] = {
  1555. samplerBinding: availableSampler.sampler.bindingIndex,
  1556. firstTextureName: name,
  1557. };
  1558. webgpuPipelineContext.textures[name] = {
  1559. textureBinding: availableSampler.textures[textureIndex].bindingIndex,
  1560. texture: internalTexture!,
  1561. };
  1562. }
  1563. }
  1564. }
  1565. }
  1566. /**
  1567. * Sets a texture to the according uniform.
  1568. * @param channel The texture channel
  1569. * @param unused unused parameter
  1570. * @param texture The texture to apply
  1571. * @param name The name of the uniform in the effect
  1572. */
  1573. public setTexture(channel: number, unused: Nullable<WebGLUniformLocation>, texture: Nullable<BaseTexture>, name: string): void {
  1574. this._setTexture(channel, texture, false, false, name, name);
  1575. }
  1576. /**
  1577. * Sets an array of texture to the WebGPU context
  1578. * @param channel defines the channel where the texture array must be set
  1579. * @param unused unused parameter
  1580. * @param textures defines the array of textures to bind
  1581. * @param name name of the channel
  1582. */
  1583. public setTextureArray(channel: number, unused: Nullable<WebGLUniformLocation>, textures: BaseTexture[], name: string): void {
  1584. for (var index = 0; index < textures.length; index++) {
  1585. this._setTexture(-1, textures[index], true, false, name + index.toString(), name, index);
  1586. }
  1587. }
  1588. protected _setTexture(channel: number, texture: Nullable<BaseTexture>, isPartOfTextureArray = false, depthStencilTexture = false, name = "", baseName?: string, textureIndex = 0): boolean {
  1589. // name == baseName for a texture that is not part of a texture array
  1590. // Else, name is something like 'myTexture0' / 'myTexture1' / ... and baseName is 'myTexture'
  1591. // baseName is used to look up the sampler in the WebGPUPipelineContext.samplers map
  1592. // name is used to look up the texture in the WebGPUPipelineContext.textures map
  1593. baseName = baseName ?? name;
  1594. if (this._currentEffect) {
  1595. const webgpuPipelineContext = this._currentEffect._pipelineContext as WebGPUPipelineContext;
  1596. if (!texture) {
  1597. if (webgpuPipelineContext.textures[name] && webgpuPipelineContext.textures[name]!.texture) {
  1598. webgpuPipelineContext.bindGroupsCache = {}; // the bind groups need to be rebuilt (at least the bind group owning this texture, but it's easier to just have them all rebuilt)
  1599. }
  1600. webgpuPipelineContext.textures[name] = null;
  1601. return false;
  1602. }
  1603. // Video
  1604. if ((<VideoTexture>texture).video) {
  1605. (<VideoTexture>texture).update();
  1606. } else if (texture.delayLoadState === Constants.DELAYLOADSTATE_NOTLOADED) { // Delay loading
  1607. texture.delayLoad();
  1608. return false;
  1609. }
  1610. let internalTexture: Nullable<InternalTexture> = null;
  1611. if (depthStencilTexture) {
  1612. internalTexture = (<RenderTargetTexture>texture).depthStencilTexture!;
  1613. }
  1614. else if (texture.isReady()) {
  1615. internalTexture = <InternalTexture>texture.getInternalTexture();
  1616. }
  1617. else if (texture.isCube) {
  1618. internalTexture = this.emptyCubeTexture;
  1619. }
  1620. else if (texture.is3D) {
  1621. internalTexture = this.emptyTexture3D;
  1622. }
  1623. else if (texture.is2DArray) {
  1624. internalTexture = this.emptyTexture2DArray;
  1625. }
  1626. else {
  1627. internalTexture = this.emptyTexture;
  1628. }
  1629. if (internalTexture && !internalTexture.isMultiview) {
  1630. // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
  1631. if (internalTexture.isCube && internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  1632. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  1633. const textureWrapMode = (texture.coordinatesMode !== Constants.TEXTURE_CUBIC_MODE && texture.coordinatesMode !== Constants.TEXTURE_SKYBOX_MODE) ? Constants.TEXTURE_WRAP_ADDRESSMODE : Constants.TEXTURE_CLAMP_ADDRESSMODE;
  1634. texture.wrapU = textureWrapMode;
  1635. texture.wrapV = textureWrapMode;
  1636. }
  1637. internalTexture._cachedWrapU = texture.wrapU;
  1638. if (this._lastCachedWrapU !== texture.wrapU) {
  1639. this._lastCachedWrapU = texture.wrapU;
  1640. }
  1641. internalTexture._cachedWrapV = texture.wrapV;
  1642. if (this._lastCachedWrapV !== texture.wrapV) {
  1643. this._lastCachedWrapV = texture.wrapV;
  1644. }
  1645. internalTexture._cachedWrapR = texture.wrapR;
  1646. if (internalTexture.is3D && this._lastCachedWrapR !== texture.wrapR) {
  1647. this._lastCachedWrapR = texture.wrapR;
  1648. }
  1649. this._setAnisotropicLevel(0, internalTexture, texture.anisotropicFilteringLevel);
  1650. }
  1651. this._setInternalTexture(name, internalTexture, baseName, textureIndex);
  1652. } else {
  1653. if (this.dbgVerboseLogsForFirstFrames) {
  1654. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  1655. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  1656. console.log("frame #" + (this as any)._count + " - _setTexture called with a null _currentEffect! texture=", texture);
  1657. }
  1658. }
  1659. }
  1660. return true;
  1661. }
  1662. /** @hidden */
  1663. public _setAnisotropicLevel(target: number, internalTexture: InternalTexture, anisotropicFilteringLevel: number) {
  1664. if (internalTexture._cachedAnisotropicFilteringLevel !== anisotropicFilteringLevel) {
  1665. internalTexture._cachedAnisotropicFilteringLevel = Math.min(anisotropicFilteringLevel, this._caps.maxAnisotropy);
  1666. }
  1667. }
  1668. /** @hidden */
  1669. public _bindTexture(channel: number, texture: InternalTexture, name: string): void {
  1670. if (channel === undefined) {
  1671. return;
  1672. }
  1673. if (texture) {
  1674. if (this._lastCachedWrapU !== null) {
  1675. texture._cachedWrapU = this._lastCachedWrapU;
  1676. }
  1677. if (this._lastCachedWrapV !== null) {
  1678. texture._cachedWrapV = this._lastCachedWrapV;
  1679. }
  1680. if (this._lastCachedWrapR !== null) {
  1681. texture._cachedWrapR = this._lastCachedWrapR;
  1682. }
  1683. }
  1684. this._setInternalTexture(name, texture);
  1685. }
  1686. private _generateMipmaps(texture: InternalTexture, commandEncoder?: GPUCommandEncoder) {
  1687. const gpuTexture = texture._hardwareTexture?.underlyingResource;
  1688. if (!gpuTexture) {
  1689. return;
  1690. }
  1691. // try as much as possible to use the command encoder corresponding to the current pass.
  1692. // If not possible (because the pass is started - generateMipmaps itself creates a pass and it's not allowed to have a pass inside a pass), use _uploadEncoder
  1693. commandEncoder = commandEncoder ?? (this._currentRenderTarget && !this._currentRenderPass ? this._renderTargetEncoder : !this._currentRenderPass ? this._renderEncoder : this._uploadEncoder);
  1694. const format = (texture._hardwareTexture as WebGPUHardwareTexture).format;
  1695. const mipmapCount = WebGPUTextureHelper.ComputeNumMipmapLevels(texture.width, texture.height);
  1696. if (this.dbgVerboseLogsForFirstFrames) {
  1697. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  1698. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  1699. console.log("frame #" + (this as any)._count + " - generate mipmaps called - width=", texture.width, "height=", texture.height, "isCube=", texture.isCube);
  1700. }
  1701. }
  1702. if (texture.isCube) {
  1703. this._textureHelper.generateCubeMipmaps(gpuTexture, format, mipmapCount, commandEncoder);
  1704. } else {
  1705. this._textureHelper.generateMipmaps(gpuTexture, format, mipmapCount, 0, commandEncoder);
  1706. }
  1707. }
  1708. /**
  1709. * Update the content of a texture
  1710. * @param texture defines the texture to update
  1711. * @param canvas defines the source containing the data
  1712. * @param invertY defines if data must be stored with Y axis inverted
  1713. * @param premulAlpha defines if alpha is stored as premultiplied
  1714. * @param format defines the format of the data
  1715. * @param forceBindTexture if the texture should be forced to be bound eg. after a graphics context loss (Default: false)
  1716. */
  1717. public updateDynamicTexture(texture: Nullable<InternalTexture>, canvas: HTMLCanvasElement | OffscreenCanvas, invertY: boolean, premulAlpha: boolean = false, format?: number, forceBindTexture?: boolean): void {
  1718. if (!texture) {
  1719. return;
  1720. }
  1721. const width = canvas.width, height = canvas.height;
  1722. let gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1723. if (!texture._hardwareTexture?.underlyingResource) {
  1724. gpuTextureWrapper = this._textureHelper.createGPUTextureForInternalTexture(texture, width, height);
  1725. }
  1726. createImageBitmap(canvas).then((bitmap) => {
  1727. this._textureHelper.updateTexture(bitmap, gpuTextureWrapper.underlyingResource!, width, height, texture.depth, gpuTextureWrapper.format, 0, 0, invertY, premulAlpha, 0, 0, this._uploadEncoder);
  1728. if (texture.generateMipMaps) {
  1729. this._generateMipmaps(texture, this._uploadEncoder);
  1730. }
  1731. texture.isReady = true;
  1732. });
  1733. }
  1734. /**
  1735. * Update a portion of an internal texture
  1736. * @param texture defines the texture to update
  1737. * @param imageData defines the data to store into the texture
  1738. * @param xOffset defines the x coordinates of the update rectangle
  1739. * @param yOffset defines the y coordinates of the update rectangle
  1740. * @param width defines the width of the update rectangle
  1741. * @param height defines the height of the update rectangle
  1742. * @param faceIndex defines the face index if texture is a cube (0 by default)
  1743. * @param lod defines the lod level to update (0 by default)
  1744. */
  1745. public updateTextureData(texture: InternalTexture, imageData: ArrayBufferView, xOffset: number, yOffset: number, width: number, height: number, faceIndex: number = 0, lod: number = 0): void {
  1746. let gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1747. if (!texture._hardwareTexture?.underlyingResource) {
  1748. gpuTextureWrapper = this._textureHelper.createGPUTextureForInternalTexture(texture);
  1749. }
  1750. const data = new Uint8Array(imageData.buffer, imageData.byteOffset, imageData.byteLength);
  1751. this._textureHelper.updateTexture(data, gpuTextureWrapper.underlyingResource!, width, height, texture.depth, gpuTextureWrapper.format, faceIndex, lod, texture.invertY, false, xOffset, yOffset, this._uploadEncoder);
  1752. }
  1753. /**
  1754. * Update a video texture
  1755. * @param texture defines the texture to update
  1756. * @param video defines the video element to use
  1757. * @param invertY defines if data must be stored with Y axis inverted
  1758. */
  1759. public updateVideoTexture(texture: Nullable<InternalTexture>, video: HTMLVideoElement, invertY: boolean): void {
  1760. if (!texture || texture._isDisabled) {
  1761. return;
  1762. }
  1763. if (this._videoTextureSupported === undefined) {
  1764. this._videoTextureSupported = true;
  1765. }
  1766. let gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1767. if (!texture._hardwareTexture?.underlyingResource) {
  1768. gpuTextureWrapper = this._textureHelper.createGPUTextureForInternalTexture(texture);
  1769. }
  1770. createImageBitmap(video).then((bitmap) => {
  1771. this._textureHelper.updateTexture(bitmap, gpuTextureWrapper.underlyingResource!, texture.width, texture.height, texture.depth, gpuTextureWrapper.format, 0, 0, !invertY, false, 0, 0, this._uploadEncoder);
  1772. if (texture.generateMipMaps) {
  1773. this._generateMipmaps(texture, this._uploadEncoder);
  1774. }
  1775. texture.isReady = true;
  1776. }).catch((msg) => {
  1777. // Sometimes createImageBitmap(video) fails with "Failed to execute 'createImageBitmap' on 'Window': The provided element's player has no current data."
  1778. // Just keep going on
  1779. texture.isReady = true;
  1780. });
  1781. }
  1782. /** @hidden */
  1783. public _uploadCompressedDataToTextureDirectly(texture: InternalTexture, internalFormat: number, width: number, height: number, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0) {
  1784. let gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1785. if (!texture._hardwareTexture?.underlyingResource) {
  1786. texture.format = internalFormat;
  1787. gpuTextureWrapper = this._textureHelper.createGPUTextureForInternalTexture(texture, width, height);
  1788. }
  1789. const data = new Uint8Array(imageData.buffer, imageData.byteOffset, imageData.byteLength);
  1790. this._textureHelper.updateTexture(data, gpuTextureWrapper.underlyingResource!, width, height, texture.depth, gpuTextureWrapper.format, faceIndex, lod, false, false, 0, 0, this._uploadEncoder);
  1791. }
  1792. /** @hidden */
  1793. public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0, babylonInternalFormat?: number, useTextureWidthAndHeight = false): void {
  1794. // TODO WEBPU babylonInternalFormat not handled.
  1795. // Note that it is used only by BasisTools.LoadTextureFromTranscodeResult when transcoding could not be done, and in that case the texture format used (TEXTURETYPE_UNSIGNED_SHORT_5_6_5) is not compatible with WebGPU...
  1796. const lodMaxWidth = Math.round(Math.log(texture.width) * Math.LOG2E);
  1797. const lodMaxHeight = Math.round(Math.log(texture.height) * Math.LOG2E);
  1798. const width = useTextureWidthAndHeight ? texture.width : Math.pow(2, Math.max(lodMaxWidth - lod, 0));
  1799. const height = useTextureWidthAndHeight ? texture.height : Math.pow(2, Math.max(lodMaxHeight - lod, 0));
  1800. let gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1801. if (!texture._hardwareTexture?.underlyingResource) {
  1802. gpuTextureWrapper = this._textureHelper.createGPUTextureForInternalTexture(texture, width, height);
  1803. }
  1804. const data = new Uint8Array(imageData.buffer, imageData.byteOffset, imageData.byteLength);
  1805. this._textureHelper.updateTexture(data, gpuTextureWrapper.underlyingResource!, width, height, texture.depth, gpuTextureWrapper.format, faceIndex, lod, texture.invertY, false, 0, 0, this._uploadEncoder);
  1806. }
  1807. /** @hidden */
  1808. public _uploadArrayBufferViewToTexture(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  1809. this._uploadDataToTextureDirectly(texture, imageData, faceIndex, lod);
  1810. }
  1811. /** @hidden */
  1812. public _uploadImageToTexture(texture: InternalTexture, image: HTMLImageElement | ImageBitmap, faceIndex: number = 0, lod: number = 0) {
  1813. let gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1814. if (!texture._hardwareTexture?.underlyingResource) {
  1815. gpuTextureWrapper = this._textureHelper.createGPUTextureForInternalTexture(texture);
  1816. }
  1817. const bitmap = image as ImageBitmap; // in WebGPU we will always get an ImageBitmap, not an HTMLImageElement
  1818. const width = Math.ceil(texture.width / (1 << lod));
  1819. const height = Math.ceil(texture.height / (1 << lod));
  1820. this._textureHelper.updateTexture(bitmap, gpuTextureWrapper.underlyingResource!, width, height, texture.depth, gpuTextureWrapper.format, faceIndex, lod, texture.invertY, false, 0, 0, this._uploadEncoder);
  1821. }
  1822. /**
  1823. * Update a raw texture
  1824. * @param texture defines the texture to update
  1825. * @param bufferView defines the data to store in the texture
  1826. * @param format defines the format of the data
  1827. * @param invertY defines if data must be stored with Y axis inverted
  1828. * @param compression defines the compression used (null by default)
  1829. * @param type defines the type fo the data (Engine.TEXTURETYPE_UNSIGNED_INT by default)
  1830. */
  1831. public updateRawTexture(texture: Nullable<InternalTexture>, bufferView: Nullable<ArrayBufferView>, format: number, invertY: boolean, compression: Nullable<string> = null, type: number = Constants.TEXTURETYPE_UNSIGNED_INT): void {
  1832. if (!texture) {
  1833. return;
  1834. }
  1835. if (!this._doNotHandleContextLost) {
  1836. texture._bufferView = bufferView;
  1837. texture.invertY = invertY;
  1838. texture._compression = compression;
  1839. }
  1840. if (bufferView) {
  1841. const gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1842. const needConversion = format === Constants.TEXTUREFORMAT_RGB;
  1843. if (needConversion) {
  1844. bufferView = _convertRGBtoRGBATextureData(bufferView, texture.width, texture.height, type);
  1845. }
  1846. const data = new Uint8Array(bufferView.buffer, bufferView.byteOffset, bufferView.byteLength);
  1847. this._textureHelper.updateTexture(data, gpuTextureWrapper.underlyingResource!, texture.width, texture.height, texture.depth, gpuTextureWrapper.format, 0, 0, invertY, false, 0, 0, this._uploadEncoder);
  1848. if (texture.generateMipMaps) {
  1849. this._generateMipmaps(texture, this._uploadEncoder);
  1850. }
  1851. }
  1852. texture.isReady = true;
  1853. }
  1854. /**
  1855. * Update a raw cube texture
  1856. * @param texture defines the texture to udpdate
  1857. * @param bufferView defines the data to store
  1858. * @param format defines the data format
  1859. * @param type defines the type fo the data (Engine.TEXTURETYPE_UNSIGNED_INT by default)
  1860. * @param invertY defines if data must be stored with Y axis inverted
  1861. * @param compression defines the compression used (null by default)
  1862. * @param level defines which level of the texture to update
  1863. */
  1864. public updateRawCubeTexture(texture: InternalTexture, bufferView: ArrayBufferView[], format: number, type: number, invertY: boolean, compression: Nullable<string> = null, level: number = 0): void {
  1865. texture._bufferViewArray = bufferView;
  1866. texture.invertY = invertY;
  1867. texture._compression = compression;
  1868. const gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1869. const needConversion = format === Constants.TEXTUREFORMAT_RGB;
  1870. const data = [];
  1871. for (let i = 0; i < bufferView.length; ++i) {
  1872. let faceData = bufferView[i];
  1873. if (needConversion) {
  1874. faceData = _convertRGBtoRGBATextureData(bufferView[i], texture.width, texture.height, type);
  1875. }
  1876. data.push(new Uint8Array(faceData.buffer, faceData.byteOffset, faceData.byteLength));
  1877. }
  1878. this._textureHelper.updateCubeTextures(data, gpuTextureWrapper.underlyingResource!, texture.width, texture.height, gpuTextureWrapper.format, invertY, false, 0, 0, this._uploadEncoder);
  1879. if (texture.generateMipMaps) {
  1880. this._generateMipmaps(texture, this._uploadEncoder);
  1881. }
  1882. texture.isReady = true;
  1883. }
  1884. /**
  1885. * Update a raw 2D array texture
  1886. * @param texture defines the texture to update
  1887. * @param bufferView defines the data to store
  1888. * @param format defines the data format
  1889. * @param invertY defines if data must be stored with Y axis inverted
  1890. * @param compression defines the used compression (can be null)
  1891. * @param textureType defines the texture Type (Engine.TEXTURETYPE_UNSIGNED_INT, Engine.TEXTURETYPE_FLOAT...)
  1892. */
  1893. public updateRawTexture2DArray(texture: InternalTexture, bufferView: Nullable<ArrayBufferView>, format: number, invertY: boolean, compression: Nullable<string> = null, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT): void {
  1894. if (this.dbgShowWarningsNotImplemented) {
  1895. console.warn("updateRawTexture2DArray not implemented yet in WebGPU");
  1896. }
  1897. }
  1898. /**
  1899. * Update a raw 3D texture
  1900. * @param texture defines the texture to update
  1901. * @param bufferView defines the data to store
  1902. * @param format defines the data format
  1903. * @param invertY defines if data must be stored with Y axis inverted
  1904. * @param compression defines the used compression (can be null)
  1905. * @param textureType defines the texture Type (Engine.TEXTURETYPE_UNSIGNED_INT, Engine.TEXTURETYPE_FLOAT...)
  1906. */
  1907. public updateRawTexture3D(texture: InternalTexture, bufferView: Nullable<ArrayBufferView>, format: number, invertY: boolean, compression: Nullable<string> = null, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT): void {
  1908. if (!this._doNotHandleContextLost) {
  1909. texture._bufferView = bufferView;
  1910. texture.format = format;
  1911. texture.invertY = invertY;
  1912. texture._compression = compression;
  1913. }
  1914. if (bufferView) {
  1915. const gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1916. const needConversion = format === Constants.TEXTUREFORMAT_RGB;
  1917. if (needConversion) {
  1918. bufferView = _convertRGBtoRGBATextureData(bufferView, texture.width, texture.height, textureType);
  1919. }
  1920. const data = new Uint8Array(bufferView.buffer, bufferView.byteOffset, bufferView.byteLength);
  1921. this._textureHelper.updateTexture(data, gpuTextureWrapper.underlyingResource!, texture.width, texture.height, texture.depth, gpuTextureWrapper.format, 0, 0, invertY, false, 0, 0, this._uploadEncoder);
  1922. if (texture.generateMipMaps) {
  1923. this._generateMipmaps(texture, this._uploadEncoder);
  1924. }
  1925. }
  1926. texture.isReady = true;
  1927. }
  1928. /**
  1929. * Reads pixels from the current frame buffer. Please note that this function can be slow
  1930. * @param x defines the x coordinate of the rectangle where pixels must be read
  1931. * @param y defines the y coordinate of the rectangle where pixels must be read
  1932. * @param width defines the width of the rectangle where pixels must be read
  1933. * @param height defines the height of the rectangle where pixels must be read
  1934. * @param hasAlpha defines whether the output should have alpha or not (defaults to true)
  1935. * @param flushRenderer true to flush the renderer from the pending commands before reading the pixels
  1936. * @returns a ArrayBufferView promise (Uint8Array) containing RGBA colors
  1937. */
  1938. public readPixels(x: number, y: number, width: number, height: number, hasAlpha = true, flushRenderer = true): Promise<ArrayBufferView> {
  1939. const renderPassWrapper = this._rttRenderPassWrapper.renderPass ? this._rttRenderPassWrapper : this._mainRenderPassWrapper;
  1940. const gpuTexture = renderPassWrapper.colorAttachmentGPUTextures![0].underlyingResource;
  1941. const gpuTextureFormat = renderPassWrapper.colorAttachmentGPUTextures![0].format;
  1942. if (!gpuTexture) {
  1943. // we are calling readPixels before startMainRenderPass has been called and no RTT is bound, so swapChainTexture is not setup yet!
  1944. return Promise.resolve(new Uint8Array(0));
  1945. }
  1946. if (flushRenderer) {
  1947. this.flushFramebuffer();
  1948. }
  1949. return this._textureHelper.readPixels(gpuTexture, x, y, width, height, gpuTextureFormat);
  1950. }
  1951. /** @hidden */
  1952. public _readTexturePixels(texture: InternalTexture, width: number, height: number, faceIndex = -1, level = 0, buffer: Nullable<ArrayBufferView> = null, flushRenderer = true): Promise<ArrayBufferView> {
  1953. let gpuTextureWrapper = texture._hardwareTexture as WebGPUHardwareTexture;
  1954. if (flushRenderer) {
  1955. this.flushFramebuffer();
  1956. }
  1957. return this._textureHelper.readPixels(gpuTextureWrapper.underlyingResource!, 0, 0, width, height, gpuTextureWrapper.format, faceIndex, level, buffer);
  1958. }
  1959. /** @hidden */
  1960. public _readTexturePixelsSync(texture: InternalTexture, width: number, height: number, faceIndex = -1, level = 0, buffer: Nullable<ArrayBufferView> = null, flushRenderer = true): ArrayBufferView {
  1961. throw "_readTexturePixelsSync is unsupported in WebGPU!";
  1962. }
  1963. //------------------------------------------------------------------------------
  1964. // Render Target Textures
  1965. //------------------------------------------------------------------------------
  1966. /**
  1967. * Creates a new render target texture
  1968. * @param size defines the size of the texture
  1969. * @param options defines the options used to create the texture
  1970. * @returns a new render target texture stored in an InternalTexture
  1971. */
  1972. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  1973. let fullOptions = new RenderTargetCreationOptions();
  1974. if (options !== undefined && typeof options === "object") {
  1975. fullOptions.generateMipMaps = options.generateMipMaps;
  1976. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  1977. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  1978. fullOptions.type = options.type === undefined ? Constants.TEXTURETYPE_UNSIGNED_INT : options.type;
  1979. fullOptions.samplingMode = options.samplingMode === undefined ? Constants.TEXTURE_TRILINEAR_SAMPLINGMODE : options.samplingMode;
  1980. fullOptions.format = options.format === undefined ? Constants.TEXTUREFORMAT_RGBA : options.format;
  1981. fullOptions.samples = options.samples ?? 1;
  1982. } else {
  1983. fullOptions.generateMipMaps = <boolean>options;
  1984. fullOptions.generateDepthBuffer = true;
  1985. fullOptions.generateStencilBuffer = false;
  1986. fullOptions.type = Constants.TEXTURETYPE_UNSIGNED_INT;
  1987. fullOptions.samplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
  1988. fullOptions.format = Constants.TEXTUREFORMAT_RGBA;
  1989. fullOptions.samples = 1;
  1990. }
  1991. const texture = new InternalTexture(this, InternalTextureSource.RenderTarget);
  1992. const width = size.width || size;
  1993. const height = size.height || size;
  1994. const layers = size.layers || 0;
  1995. texture._depthStencilBuffer = {};
  1996. texture._framebuffer = {};
  1997. texture.baseWidth = width;
  1998. texture.baseHeight = height;
  1999. texture.width = width;
  2000. texture.height = height;
  2001. texture.depth = layers;
  2002. texture.isReady = true;
  2003. texture.samples = fullOptions.samples;
  2004. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  2005. texture.samplingMode = fullOptions.samplingMode;
  2006. texture.type = fullOptions.type;
  2007. texture.format = fullOptions.format;
  2008. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  2009. texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  2010. texture.is2DArray = layers > 0;
  2011. this._internalTexturesCache.push(texture);
  2012. if (texture._generateDepthBuffer || texture._generateStencilBuffer) {
  2013. texture._depthStencilTexture = this.createDepthStencilTexture({ width, height, layers }, {
  2014. bilinearFiltering:
  2015. fullOptions.samplingMode === undefined ||
  2016. fullOptions.samplingMode === Constants.TEXTURE_BILINEAR_SAMPLINGMODE || fullOptions.samplingMode === Constants.TEXTURE_LINEAR_LINEAR ||
  2017. fullOptions.samplingMode === Constants.TEXTURE_TRILINEAR_SAMPLINGMODE || fullOptions.samplingMode === Constants.TEXTURE_LINEAR_LINEAR_MIPLINEAR ||
  2018. fullOptions.samplingMode === Constants.TEXTURE_NEAREST_LINEAR_MIPNEAREST || fullOptions.samplingMode === Constants.TEXTURE_NEAREST_LINEAR_MIPLINEAR ||
  2019. fullOptions.samplingMode === Constants.TEXTURE_NEAREST_LINEAR || fullOptions.samplingMode === Constants.TEXTURE_LINEAR_LINEAR_MIPNEAREST,
  2020. comparisonFunction: 0,
  2021. generateStencil: texture._generateStencilBuffer,
  2022. isCube: texture.isCube,
  2023. samples: texture.samples,
  2024. });
  2025. }
  2026. if (options !== undefined && typeof options === "object" && options.createMipMaps && !fullOptions.generateMipMaps) {
  2027. texture.generateMipMaps = true;
  2028. }
  2029. this._textureHelper.createGPUTextureForInternalTexture(texture);
  2030. if (options !== undefined && typeof options === "object" && options.createMipMaps && !fullOptions.generateMipMaps) {
  2031. texture.generateMipMaps = false;
  2032. }
  2033. return texture;
  2034. }
  2035. /**
  2036. * Create a multi render target texture
  2037. * @param size defines the size of the texture
  2038. * @param options defines the creation options
  2039. * @returns the cube texture as an InternalTexture
  2040. */
  2041. public createMultipleRenderTarget(size: any, options: IMultiRenderTargetOptions): InternalTexture[] {
  2042. let generateMipMaps = false;
  2043. let generateDepthBuffer = true;
  2044. let generateStencilBuffer = false;
  2045. let generateDepthTexture = false;
  2046. let textureCount = 1;
  2047. let defaultType = Constants.TEXTURETYPE_UNSIGNED_INT;
  2048. let defaultSamplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
  2049. let types = new Array<number>();
  2050. let samplingModes = new Array<number>();
  2051. if (options !== undefined) {
  2052. generateMipMaps = options.generateMipMaps === undefined ? false : options.generateMipMaps;
  2053. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  2054. generateStencilBuffer = options.generateStencilBuffer === undefined ? false : options.generateStencilBuffer;
  2055. generateDepthTexture = options.generateDepthTexture === undefined ? false : options.generateDepthTexture;
  2056. textureCount = options.textureCount || 1;
  2057. if (options.types) {
  2058. types = options.types;
  2059. }
  2060. if (options.samplingModes) {
  2061. samplingModes = options.samplingModes;
  2062. }
  2063. }
  2064. const width = size.width || size;
  2065. const height = size.height || size;
  2066. let depthStencilTexture = null;
  2067. if (generateDepthBuffer || generateStencilBuffer || generateDepthTexture) {
  2068. depthStencilTexture = this.createDepthStencilTexture({ width, height }, {
  2069. bilinearFiltering: false,
  2070. comparisonFunction: 0,
  2071. generateStencil: generateStencilBuffer,
  2072. isCube: false,
  2073. samples: 1,
  2074. });
  2075. }
  2076. const textures = [];
  2077. const attachments = [];
  2078. for (let i = 0; i < textureCount; i++) {
  2079. let samplingMode = samplingModes[i] || defaultSamplingMode;
  2080. let type = types[i] || defaultType;
  2081. if (type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloatLinearFiltering) {
  2082. // if floating point linear (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
  2083. samplingMode = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
  2084. }
  2085. else if (type === Constants.TEXTURETYPE_HALF_FLOAT && !this._caps.textureHalfFloatLinearFiltering) {
  2086. // if floating point linear (HALF_FLOAT) then force to NEAREST_SAMPLINGMODE
  2087. samplingMode = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
  2088. }
  2089. if (type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloat) {
  2090. type = Constants.TEXTURETYPE_UNSIGNED_INT;
  2091. Logger.Warn("Float textures are not supported. Render target forced to TEXTURETYPE_UNSIGNED_BYTE type");
  2092. }
  2093. const texture = new InternalTexture(this, InternalTextureSource.MultiRenderTarget);
  2094. textures.push(texture);
  2095. attachments.push(i + 1);
  2096. texture._depthStencilTexture = i === 0 ? depthStencilTexture : null;
  2097. texture._framebuffer = {};
  2098. texture._depthStencilBuffer = {};
  2099. texture.baseWidth = width;
  2100. texture.baseHeight = height;
  2101. texture.width = width;
  2102. texture.height = height;
  2103. texture.isReady = true;
  2104. texture.samples = 1;
  2105. texture.generateMipMaps = generateMipMaps;
  2106. texture.samplingMode = samplingMode;
  2107. texture.type = type;
  2108. texture._generateDepthBuffer = generateDepthBuffer;
  2109. texture._generateStencilBuffer = generateStencilBuffer ? true : false;
  2110. texture._attachments = attachments;
  2111. texture._textureArray = textures;
  2112. this._internalTexturesCache.push(texture);
  2113. this._textureHelper.createGPUTextureForInternalTexture(texture);
  2114. }
  2115. if (depthStencilTexture) {
  2116. textures.push(depthStencilTexture);
  2117. this._internalTexturesCache.push(depthStencilTexture);
  2118. }
  2119. return textures;
  2120. }
  2121. /**
  2122. * Creates a new render target cube texture
  2123. * @param size defines the size of the texture
  2124. * @param options defines the options used to create the texture
  2125. * @returns a new render target cube texture stored in an InternalTexture
  2126. */
  2127. public createRenderTargetCubeTexture(size: number, options?: Partial<RenderTargetCreationOptions>): InternalTexture {
  2128. let fullOptions = {
  2129. generateMipMaps: true,
  2130. generateDepthBuffer: true,
  2131. generateStencilBuffer: false,
  2132. type: Constants.TEXTURETYPE_UNSIGNED_INT,
  2133. samplingMode: Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
  2134. format: Constants.TEXTUREFORMAT_RGBA,
  2135. samples: 1,
  2136. ...options
  2137. };
  2138. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && fullOptions.generateStencilBuffer;
  2139. const texture = new InternalTexture(this, InternalTextureSource.RenderTarget);
  2140. texture.width = size;
  2141. texture.height = size;
  2142. texture.depth = 0;
  2143. texture.isReady = true;
  2144. texture.isCube = true;
  2145. texture.samples = fullOptions.samples;
  2146. texture.generateMipMaps = fullOptions.generateMipMaps;
  2147. texture.samplingMode = fullOptions.samplingMode;
  2148. texture.type = fullOptions.type;
  2149. texture.format = fullOptions.format;
  2150. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  2151. texture._generateStencilBuffer = fullOptions.generateStencilBuffer;
  2152. this._internalTexturesCache.push(texture);
  2153. if (texture._generateDepthBuffer || texture._generateStencilBuffer) {
  2154. texture._depthStencilTexture = this.createDepthStencilTexture({ width: texture.width, height: texture.height, layers: texture.depth }, {
  2155. bilinearFiltering:
  2156. fullOptions.samplingMode === undefined ||
  2157. fullOptions.samplingMode === Constants.TEXTURE_BILINEAR_SAMPLINGMODE || fullOptions.samplingMode === Constants.TEXTURE_LINEAR_LINEAR ||
  2158. fullOptions.samplingMode === Constants.TEXTURE_TRILINEAR_SAMPLINGMODE || fullOptions.samplingMode === Constants.TEXTURE_LINEAR_LINEAR_MIPLINEAR ||
  2159. fullOptions.samplingMode === Constants.TEXTURE_NEAREST_LINEAR_MIPNEAREST || fullOptions.samplingMode === Constants.TEXTURE_NEAREST_LINEAR_MIPLINEAR ||
  2160. fullOptions.samplingMode === Constants.TEXTURE_NEAREST_LINEAR || fullOptions.samplingMode === Constants.TEXTURE_LINEAR_LINEAR_MIPNEAREST,
  2161. comparisonFunction: 0,
  2162. generateStencil: texture._generateStencilBuffer,
  2163. isCube: texture.isCube,
  2164. samples: texture.samples,
  2165. });
  2166. }
  2167. if (options && options.createMipMaps && !fullOptions.generateMipMaps) {
  2168. texture.generateMipMaps = true;
  2169. }
  2170. this._textureHelper.createGPUTextureForInternalTexture(texture);
  2171. if (options && options.createMipMaps && !fullOptions.generateMipMaps) {
  2172. texture.generateMipMaps = false;
  2173. }
  2174. return texture;
  2175. }
  2176. /** @hidden */
  2177. public _setupDepthStencilTexture(internalTexture: InternalTexture, size: number | { width: number, height: number, layers?: number }, generateStencil: boolean, bilinearFiltering: boolean, comparisonFunction: number, samples = 1): void {
  2178. const width = (<{ width: number, height: number, layers?: number }>size).width || <number>size;
  2179. const height = (<{ width: number, height: number, layers?: number }>size).height || <number>size;
  2180. const layers = (<{ width: number, height: number, layers?: number }>size).layers || 0;
  2181. internalTexture.baseWidth = width;
  2182. internalTexture.baseHeight = height;
  2183. internalTexture.width = width;
  2184. internalTexture.height = height;
  2185. internalTexture.is2DArray = layers > 0;
  2186. internalTexture.depth = layers;
  2187. internalTexture.isReady = true;
  2188. internalTexture.samples = samples;
  2189. internalTexture.generateMipMaps = false;
  2190. internalTexture._generateDepthBuffer = true;
  2191. internalTexture._generateStencilBuffer = generateStencil;
  2192. internalTexture.samplingMode = bilinearFiltering ? Constants.TEXTURE_BILINEAR_SAMPLINGMODE : Constants.TEXTURE_NEAREST_SAMPLINGMODE;
  2193. internalTexture.type = Constants.TEXTURETYPE_UNSIGNED_INT;
  2194. internalTexture._comparisonFunction = comparisonFunction;
  2195. }
  2196. /** @hidden */
  2197. public _createDepthStencilTexture(size: number | { width: number, height: number, layers?: number }, options: DepthTextureCreationOptions): InternalTexture {
  2198. const internalTexture = new InternalTexture(this, InternalTextureSource.Depth);
  2199. const internalOptions = {
  2200. bilinearFiltering: false,
  2201. comparisonFunction: 0,
  2202. generateStencil: false,
  2203. samples: 1,
  2204. ...options
  2205. };
  2206. // TODO WEBGPU allow to choose the format?
  2207. internalTexture.format = internalOptions.generateStencil ? Constants.TEXTUREFORMAT_DEPTH24_STENCIL8 : Constants.TEXTUREFORMAT_DEPTH32_FLOAT;
  2208. this._setupDepthStencilTexture(internalTexture, size, internalOptions.generateStencil, internalOptions.bilinearFiltering, internalOptions.comparisonFunction, internalOptions.samples);
  2209. this._textureHelper.createGPUTextureForInternalTexture(internalTexture);
  2210. return internalTexture;
  2211. }
  2212. /** @hidden */
  2213. public _createDepthStencilCubeTexture(size: number, options: DepthTextureCreationOptions): InternalTexture {
  2214. const internalTexture = new InternalTexture(this, InternalTextureSource.Depth);
  2215. internalTexture.isCube = true;
  2216. const internalOptions = {
  2217. bilinearFiltering: false,
  2218. comparisonFunction: 0,
  2219. generateStencil: false,
  2220. samples: 1,
  2221. ...options
  2222. };
  2223. // TODO WEBGPU allow to choose the format?
  2224. internalTexture.format = internalOptions.generateStencil ? Constants.TEXTUREFORMAT_DEPTH24_STENCIL8 : Constants.TEXTUREFORMAT_DEPTH32_FLOAT;
  2225. this._setupDepthStencilTexture(internalTexture, size, internalOptions.generateStencil, internalOptions.bilinearFiltering, internalOptions.comparisonFunction, internalOptions.samples);
  2226. this._textureHelper.createGPUTextureForInternalTexture(internalTexture);
  2227. return internalTexture;
  2228. }
  2229. public updateRenderTargetTextureSampleCount(texture: Nullable<InternalTexture>, samples: number): number {
  2230. if (!texture || texture.samples === samples) {
  2231. return samples;
  2232. }
  2233. samples = Math.min(samples, this.getCaps().maxMSAASamples);
  2234. this._textureHelper.createMSAATexture(texture, samples);
  2235. if (texture._depthStencilTexture) {
  2236. this._textureHelper.createMSAATexture(texture._depthStencilTexture, samples);
  2237. texture._depthStencilTexture.samples = samples;
  2238. }
  2239. texture.samples = samples;
  2240. return samples;
  2241. }
  2242. /**
  2243. * Update the sample count for a given multiple render target texture
  2244. * @param textures defines the textures to update
  2245. * @param samples defines the sample count to set
  2246. * @returns the effective sample count (could be 0 if multisample render targets are not supported)
  2247. */
  2248. public updateMultipleRenderTargetTextureSampleCount(textures: Nullable<InternalTexture[]>, samples: number): number {
  2249. if (!textures || textures[0].samples === samples) {
  2250. return samples;
  2251. }
  2252. samples = Math.min(samples, this.getCaps().maxMSAASamples);
  2253. // Note that the last texture of textures is the depth texture (if the depth texture has been generated by the MRT class) and so the MSAA texture
  2254. // will be recreated for this texture too. As a consequence, there's no need to explicitely recreate the MSAA texture for textures[0]._depthStencilTexture
  2255. for (let i = 0; i < textures.length; ++i) {
  2256. const texture = textures[i];
  2257. this._textureHelper.createMSAATexture(texture, samples);
  2258. texture.samples = samples;
  2259. }
  2260. return samples;
  2261. }
  2262. //------------------------------------------------------------------------------
  2263. // Render Commands
  2264. //------------------------------------------------------------------------------
  2265. /**
  2266. * Begin a new frame
  2267. */
  2268. public beginFrame(): void {
  2269. super.beginFrame();
  2270. }
  2271. /**
  2272. * End the current frame
  2273. */
  2274. public endFrame() {
  2275. this._endMainRenderPass();
  2276. this.flushFramebuffer();
  2277. if (this.dbgVerboseLogsForFirstFrames) {
  2278. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  2279. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  2280. console.log("frame #" + (this as any)._count + " - counters - numBindGroupsCreation=", this._counters.numBindGroupsCreation);
  2281. }
  2282. }
  2283. this._textureHelper.destroyDeferredTextures();
  2284. this._bufferManager.destroyDeferredBuffers();
  2285. if (this._features._collectUbosUpdatedInFrame) {
  2286. if (this.dbgVerboseLogsForFirstFrames) {
  2287. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  2288. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  2289. const list: Array<string> = [];
  2290. for (const name in UniformBuffer._updatedUbosInFrame) {
  2291. list.push(name + ":" + UniformBuffer._updatedUbosInFrame[name]);
  2292. }
  2293. console.log("frame #" + (this as any)._count + " - updated ubos -", list.join(", "));
  2294. }
  2295. }
  2296. UniformBuffer._updatedUbosInFrame = {};
  2297. }
  2298. this._counters.numBindGroupsCreation = 0;
  2299. this._cacheRenderPipeline.endFrame();
  2300. this._pendingDebugCommands.length = 0;
  2301. super.endFrame();
  2302. if (this.dbgVerboseLogsForFirstFrames) {
  2303. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  2304. if ((this as any)._count < this.dbgVerboseLogsNumFrames) {
  2305. console.log("%c frame #" + (this as any)._count + " - end", "background: #ffff00");
  2306. }
  2307. if ((this as any)._count < this.dbgVerboseLogsNumFrames) {
  2308. (this as any)._count++;
  2309. if ((this as any)._count !== this.dbgVerboseLogsNumFrames) {
  2310. console.log("%c frame #" + (this as any)._count + " - begin", "background: #ffff00");
  2311. }
  2312. }
  2313. }
  2314. }
  2315. /**
  2316. * Force a WebGPU flush (ie. a flush of all waiting commands)
  2317. */
  2318. public flushFramebuffer(): void {
  2319. // we need to end the current render pass (main or rtt) if any as we are not allowed to submit the command buffers when being in a pass
  2320. let currentPassType = 0; // 0 if no pass, 1 for rtt, 2 for main pass
  2321. if (this._currentRenderPass) {
  2322. if (this._currentRenderTarget) {
  2323. if (this._currentRenderPass) {
  2324. currentPassType = 1;
  2325. this._endRenderTargetRenderPass();
  2326. }
  2327. } else {
  2328. currentPassType = 2;
  2329. this._endMainRenderPass();
  2330. }
  2331. }
  2332. this._commandBuffers[0] = this._uploadEncoder.finish();
  2333. this._commandBuffers[1] = this._renderTargetEncoder.finish();
  2334. this._commandBuffers[2] = this._renderEncoder.finish();
  2335. this._device.defaultQueue.submit(this._commandBuffers);
  2336. this._uploadEncoder = this._device.createCommandEncoder(this._uploadEncoderDescriptor);
  2337. this._renderEncoder = this._device.createCommandEncoder(this._renderEncoderDescriptor);
  2338. this._renderTargetEncoder = this._device.createCommandEncoder(this._renderTargetEncoderDescriptor);
  2339. this._textureHelper.setCommandEncoder(this._uploadEncoder);
  2340. // restart the render pass
  2341. if (currentPassType === 1) {
  2342. this._startRenderTargetRenderPass(this._currentRenderTarget!, null, null, false, false);
  2343. } else if (currentPassType === 2) {
  2344. this._startMainRenderPass(false);
  2345. }
  2346. }
  2347. //------------------------------------------------------------------------------
  2348. // Render Pass
  2349. //------------------------------------------------------------------------------
  2350. private _startRenderTargetRenderPass(internalTexture: InternalTexture, clearColorMain: Nullable<IColor4Like>, clearColorOtherAttachments: Nullable<IColor4Like>, clearDepth: boolean, clearStencil: boolean) {
  2351. const gpuWrapper = internalTexture._hardwareTexture as WebGPUHardwareTexture;
  2352. const gpuTexture = gpuWrapper.underlyingResource!;
  2353. const depthStencilTexture = internalTexture._depthStencilTexture;
  2354. const gpuDepthStencilWrapper = depthStencilTexture?._hardwareTexture as Nullable<WebGPUHardwareTexture>;
  2355. const gpuDepthStencilTexture = gpuDepthStencilWrapper?.underlyingResource as Nullable<GPUTexture>;
  2356. const gpuDepthStencilMSAATexture = gpuDepthStencilWrapper?.msaaTexture;
  2357. const depthTextureView = gpuDepthStencilTexture?.createView(this._rttRenderPassWrapper.depthAttachmentViewDescriptor!);
  2358. const depthMSAATextureView = gpuDepthStencilMSAATexture?.createView(this._rttRenderPassWrapper.depthAttachmentViewDescriptor!);
  2359. const colorAttachments: GPURenderPassColorAttachmentDescriptor[] = [];
  2360. if (internalTexture._attachments && internalTexture._textureArray) {
  2361. // multi render targets
  2362. for (let i = 0; i < internalTexture._attachments.length; ++i) {
  2363. const index = internalTexture._attachments[i];
  2364. const mrtTexture = internalTexture._textureArray[index - 1];
  2365. const gpuMRTWrapper = mrtTexture?._hardwareTexture as Nullable<WebGPUHardwareTexture>;
  2366. const gpuMRTTexture = gpuMRTWrapper?.underlyingResource;
  2367. if (gpuMRTWrapper && gpuMRTTexture) {
  2368. const viewDescriptor = {
  2369. ...this._rttRenderPassWrapper.colorAttachmentViewDescriptor,
  2370. format: gpuMRTWrapper.format,
  2371. };
  2372. const gpuMSAATexture = gpuMRTWrapper.msaaTexture;
  2373. const colorTextureView = gpuMRTTexture.createView(viewDescriptor);
  2374. const colorMSAATextureView = gpuMSAATexture?.createView(viewDescriptor);
  2375. const clearColor = i === 0 ? (clearColorMain ? clearColorMain : WebGPUConstants.LoadOp.Load) : (clearColorOtherAttachments ? clearColorOtherAttachments : WebGPUConstants.LoadOp.Load);
  2376. colorAttachments.push({
  2377. attachment: colorMSAATextureView ? colorMSAATextureView : colorTextureView,
  2378. resolveTarget: gpuMSAATexture ? colorTextureView : undefined,
  2379. loadValue: clearColor,
  2380. storeOp: WebGPUConstants.StoreOp.Store,
  2381. });
  2382. }
  2383. }
  2384. this._mrtAttachments = internalTexture._attachments;
  2385. this._cacheRenderPipeline.setMRTAttachments(this._mrtAttachments, internalTexture._textureArray);
  2386. } else {
  2387. // single render target
  2388. const gpuMSAATexture = gpuWrapper.msaaTexture;
  2389. const colorTextureView = gpuTexture.createView(this._rttRenderPassWrapper.colorAttachmentViewDescriptor!);
  2390. const colorMSAATextureView = gpuMSAATexture?.createView(this._rttRenderPassWrapper.colorAttachmentViewDescriptor!);
  2391. colorAttachments.push({
  2392. attachment: colorMSAATextureView ? colorMSAATextureView : colorTextureView,
  2393. resolveTarget: gpuMSAATexture ? colorTextureView : undefined,
  2394. loadValue: clearColorMain !== null ? clearColorMain : WebGPUConstants.LoadOp.Load,
  2395. storeOp: WebGPUConstants.StoreOp.Store,
  2396. });
  2397. }
  2398. this._debugPushGroup("render target pass", 1);
  2399. this._rttRenderPassWrapper.renderPassDescriptor = {
  2400. colorAttachments,
  2401. depthStencilAttachment: depthStencilTexture && gpuDepthStencilTexture ? {
  2402. attachment: depthMSAATextureView ? depthMSAATextureView : depthTextureView!,
  2403. depthLoadValue: clearDepth && depthStencilTexture._generateDepthBuffer ? this._clearDepthValue : WebGPUConstants.LoadOp.Load,
  2404. depthStoreOp: WebGPUConstants.StoreOp.Store,
  2405. stencilLoadValue: clearStencil && depthStencilTexture._generateStencilBuffer ? this._clearStencilValue : WebGPUConstants.LoadOp.Load,
  2406. stencilStoreOp: WebGPUConstants.StoreOp.Store,
  2407. } : undefined
  2408. };
  2409. this._rttRenderPassWrapper.renderPass = this._renderTargetEncoder.beginRenderPass(this._rttRenderPassWrapper.renderPassDescriptor);
  2410. if (this.dbgVerboseLogsForFirstFrames) {
  2411. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  2412. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  2413. console.log("frame #" + (this as any)._count + " - render target begin pass - internalTexture.uniqueId=", internalTexture.uniqueId, this._rttRenderPassWrapper.renderPassDescriptor);
  2414. }
  2415. }
  2416. this._currentRenderPass = this._rttRenderPassWrapper.renderPass;
  2417. this._debugFlushPendingCommands();
  2418. this._resetCurrentViewport(1);
  2419. this._resetCurrentScissor(1);
  2420. }
  2421. private _endRenderTargetRenderPass() {
  2422. if (this._currentRenderPass) {
  2423. this._currentRenderPass.endPass();
  2424. if (this.dbgVerboseLogsForFirstFrames) {
  2425. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  2426. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  2427. console.log("frame #" + (this as any)._count + " - render target end pass - internalTexture.uniqueId=", this._currentRenderTarget?.uniqueId);
  2428. }
  2429. }
  2430. this._debugPopGroup(1);
  2431. this._resetCurrentViewport(1);
  2432. this._resetCurrentScissor(1);
  2433. this._currentRenderPass = null;
  2434. this._rttRenderPassWrapper.reset();
  2435. }
  2436. }
  2437. private _getCurrentRenderPass(): GPURenderPassEncoder {
  2438. if (this._currentRenderTarget && !this._currentRenderPass) {
  2439. // delayed creation of the render target pass, but we now need to create it as we are requested the render pass
  2440. this._startRenderTargetRenderPass(this._currentRenderTarget, null, null, false, false);
  2441. } else if (!this._currentRenderPass) {
  2442. this._startMainRenderPass(false);
  2443. }
  2444. return this._currentRenderPass!;
  2445. }
  2446. private _startMainRenderPass(setClearStates: boolean, clearColor?: Nullable<IColor4Like>, clearDepth?: boolean, clearStencil?: boolean): void {
  2447. if (this._mainRenderPassWrapper.renderPass) {
  2448. this._endMainRenderPass();
  2449. }
  2450. if (this.useReverseDepthBuffer) {
  2451. this.setDepthFunctionToGreater();
  2452. }
  2453. const scissorIsActive = this._scissorIsActive();
  2454. if (setClearStates) {
  2455. const colorClearValue = scissorIsActive ? WebGPUConstants.LoadOp.Load : clearColor ? clearColor : WebGPUConstants.LoadOp.Load;
  2456. const depthClearValue = scissorIsActive ? WebGPUConstants.LoadOp.Load : clearDepth ? (this.useReverseDepthBuffer ? this._clearReverseDepthValue : this._clearDepthValue) : WebGPUConstants.LoadOp.Load;
  2457. const stencilClearValue = scissorIsActive ? WebGPUConstants.LoadOp.Load : clearStencil ? this._clearStencilValue : WebGPUConstants.LoadOp.Load;
  2458. (this._mainRenderPassWrapper.renderPassDescriptor!.colorAttachments as GPURenderPassColorAttachmentDescriptor[])[0].loadValue = colorClearValue;
  2459. this._mainRenderPassWrapper.renderPassDescriptor!.depthStencilAttachment!.depthLoadValue = depthClearValue;
  2460. this._mainRenderPassWrapper.renderPassDescriptor!.depthStencilAttachment!.stencilLoadValue = stencilClearValue;
  2461. }
  2462. this._swapChainTexture = this._swapChain.getCurrentTexture();
  2463. this._mainRenderPassWrapper.colorAttachmentGPUTextures![0].set(this._swapChainTexture);
  2464. // Resolve in case of MSAA
  2465. if (this._options.antialiasing) {
  2466. (this._mainRenderPassWrapper.renderPassDescriptor!.colorAttachments as GPURenderPassColorAttachmentDescriptor[])[0].resolveTarget = this._swapChainTexture.createView();
  2467. }
  2468. else {
  2469. (this._mainRenderPassWrapper.renderPassDescriptor!.colorAttachments as GPURenderPassColorAttachmentDescriptor[])[0].attachment = this._swapChainTexture.createView();
  2470. }
  2471. if (this.dbgVerboseLogsForFirstFrames) {
  2472. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  2473. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  2474. console.log("frame #" + (this as any)._count + " - main begin pass - texture width=" + (this._mainTextureExtends as any).width, " height=" + (this._mainTextureExtends as any).height, this._mainRenderPassWrapper.renderPassDescriptor);
  2475. }
  2476. }
  2477. this._debugPushGroup("main pass", 0);
  2478. this._currentRenderPass = this._renderEncoder.beginRenderPass(this._mainRenderPassWrapper.renderPassDescriptor!);
  2479. this._mainRenderPassWrapper.renderPass = this._currentRenderPass;
  2480. this._debugFlushPendingCommands();
  2481. this._resetCurrentViewport(0);
  2482. this._resetCurrentScissor(0);
  2483. if (setClearStates && scissorIsActive) {
  2484. this._applyScissor(this._currentRenderPass);
  2485. // TODO WEBGPU cache things, move this code somewhere else
  2486. const pipeline = this._device.createRenderPipeline({
  2487. sampleCount: this._currentRenderTarget ? this._currentRenderTarget.samples : this._mainPassSampleCount,
  2488. primitiveTopology: WebGPUConstants.PrimitiveTopology.TriangleStrip,
  2489. vertexState: {
  2490. indexFormat: WebGPUConstants.IndexFormat.Uint16
  2491. },
  2492. depthStencilState: this._depthTextureFormat === undefined ? undefined : {
  2493. depthWriteEnabled: clearDepth,
  2494. depthCompare: WebGPUConstants.CompareFunction.Always,
  2495. format: this._depthTextureFormat,
  2496. stencilFront: {
  2497. compare: clearStencil ? WebGPUConstants.CompareFunction.Always : WebGPUConstants.CompareFunction.Never,
  2498. passOp: clearStencil ? WebGPUConstants.StencilOperation.Replace : WebGPUConstants.StencilOperation.Keep,
  2499. },
  2500. stencilReadMask: 0xFF,
  2501. stencilWriteMask: clearStencil ? 0xFF : 0,
  2502. },
  2503. colorStates: [{
  2504. format: this._colorFormat,
  2505. writeMask: clearColor ? WebGPUConstants.ColorWrite.All : 0,
  2506. }],
  2507. ...this._shaderManager.getCompiledShaders("clearQuad"),
  2508. });
  2509. const bindGroupLayout = pipeline.getBindGroupLayout(0);
  2510. const buffer = this._bufferManager.createBuffer(4 * 4, WebGPUConstants.BufferUsage.CopyDst | WebGPUConstants.BufferUsage.Uniform) as WebGPUDataBuffer;
  2511. if (clearColor) {
  2512. const data = new Float32Array([clearColor.r, clearColor.g, clearColor.b, clearColor.a]);
  2513. this._bufferManager.setSubData(buffer, 0, data);
  2514. }
  2515. const bindGroup = this._device.createBindGroup({
  2516. layout: bindGroupLayout,
  2517. entries: [{
  2518. binding: 0,
  2519. resource: {
  2520. buffer: buffer.underlyingResource,
  2521. },
  2522. }],
  2523. });
  2524. this._currentRenderPass.setStencilReference(this._clearStencilValue);
  2525. this._currentRenderPass.setPipeline(pipeline);
  2526. this._currentRenderPass.setBindGroup(0, bindGroup);
  2527. this._currentRenderPass.draw(4, 1, 0, 0);
  2528. if (this._stencilState.stencilTest) {
  2529. this._getCurrentRenderPass().setStencilReference(this._stencilState.stencilFuncRef);
  2530. }
  2531. this._bufferManager.releaseBuffer(buffer);
  2532. }
  2533. }
  2534. private _endMainRenderPass(): void {
  2535. if (this._mainRenderPassWrapper.renderPass !== null) {
  2536. this._mainRenderPassWrapper.renderPass.endPass();
  2537. if (this.dbgVerboseLogsForFirstFrames) {
  2538. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  2539. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  2540. console.log("frame #" + (this as any)._count + " - main end pass");
  2541. }
  2542. }
  2543. this._debugPopGroup(0);
  2544. this._resetCurrentViewport(0);
  2545. this._resetCurrentScissor(0);
  2546. if (this._mainRenderPassWrapper.renderPass === this._currentRenderPass) {
  2547. this._currentRenderPass = null;
  2548. }
  2549. this._mainRenderPassWrapper.reset(false);
  2550. }
  2551. }
  2552. /**
  2553. * Restores the WebGPU state to only draw on the main color attachment
  2554. */
  2555. public restoreSingleAttachment(): void {
  2556. // nothing to do, this is done automatically in the unBindFramebuffer function
  2557. }
  2558. /**
  2559. * Creates a layout object to draw/clear on specific textures in a MRT
  2560. * @param textureStatus textureStatus[i] indicates if the i-th is active
  2561. * @returns A layout to be fed to the engine, calling `bindAttachments`.
  2562. */
  2563. public buildTextureLayout(textureStatus: boolean[]): number[] {
  2564. const result = [];
  2565. for (let i = 0; i < textureStatus.length; i++) {
  2566. if (textureStatus[i]) {
  2567. result.push(i + 1);
  2568. } else {
  2569. result.push(0);
  2570. }
  2571. }
  2572. return result;
  2573. }
  2574. /**
  2575. * Select a subsets of attachments to draw to.
  2576. * @param attachments index of attachments
  2577. */
  2578. public bindAttachments(attachments: number[]): void {
  2579. // nothing to do, this is done automatically in the _startRenderTargetRenderPass function
  2580. }
  2581. /**
  2582. * Binds the frame buffer to the specified texture.
  2583. * @param texture The texture to render to or null for the default canvas
  2584. * @param faceIndex The face of the texture to render to in case of cube texture
  2585. * @param requiredWidth The width of the target to render to
  2586. * @param requiredHeight The height of the target to render to
  2587. * @param forceFullscreenViewport Forces the viewport to be the entire texture/screen if true
  2588. * @param lodLevel defines the lod level to bind to the frame buffer
  2589. * @param layer defines the 2d array index to bind to frame buffer to
  2590. */
  2591. public bindFramebuffer(texture: InternalTexture, faceIndex: number = 0, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean, lodLevel = 0, layer = 0): void {
  2592. const hardwareTexture = texture._hardwareTexture as Nullable<WebGPUHardwareTexture>;
  2593. const gpuTexture = hardwareTexture?.underlyingResource as Nullable<GPUTexture>;
  2594. if (!hardwareTexture || !gpuTexture) {
  2595. if (this.dbgSanityChecks) {
  2596. console.error("bindFramebuffer: Trying to bind a texture that does not have a hardware texture or that has a webgpu texture empty!", texture, hardwareTexture, gpuTexture);
  2597. }
  2598. return;
  2599. }
  2600. if (this._currentRenderTarget) {
  2601. this.unBindFramebuffer(this._currentRenderTarget);
  2602. }
  2603. this._currentRenderTarget = texture;
  2604. this._rttRenderPassWrapper.colorAttachmentGPUTextures[0] = hardwareTexture;
  2605. this._rttRenderPassWrapper.depthTextureFormat = this._currentRenderTarget._depthStencilTexture ? WebGPUTextureHelper.GetWebGPUTextureFormat(-1, this._currentRenderTarget._depthStencilTexture.format) : undefined;
  2606. this._setDepthTextureFormat(this._rttRenderPassWrapper);
  2607. this._setColorFormat(this._rttRenderPassWrapper);
  2608. this._rttRenderPassWrapper.colorAttachmentViewDescriptor = {
  2609. format: this._colorFormat,
  2610. dimension: WebGPUConstants.TextureViewDimension.E2d,
  2611. mipLevelCount: 1,
  2612. baseArrayLayer: texture.isCube ? layer * 6 + faceIndex : layer,
  2613. baseMipLevel: lodLevel,
  2614. arrayLayerCount: 1,
  2615. aspect: WebGPUConstants.TextureAspect.All
  2616. };
  2617. this._rttRenderPassWrapper.depthAttachmentViewDescriptor = {
  2618. format: this._depthTextureFormat,
  2619. dimension: WebGPUConstants.TextureViewDimension.E2d,
  2620. mipLevelCount: 1,
  2621. baseArrayLayer: texture.isCube ? layer * 6 + faceIndex : layer,
  2622. baseMipLevel: 0,
  2623. arrayLayerCount: 1,
  2624. aspect: WebGPUConstants.TextureAspect.All
  2625. };
  2626. if (this.dbgVerboseLogsForFirstFrames) {
  2627. if ((this as any)._count === undefined) { (this as any)._count = 0; }
  2628. if (!(this as any)._count || (this as any)._count < this.dbgVerboseLogsNumFrames) {
  2629. console.log("frame #" + (this as any)._count + " - bindFramebuffer called - face=", faceIndex, "lodLevel=", lodLevel, "layer=", layer, this._rttRenderPassWrapper.colorAttachmentViewDescriptor, this._rttRenderPassWrapper.depthAttachmentViewDescriptor);
  2630. }
  2631. }
  2632. this._currentRenderPass = null; // lazy creation of the render pass, hoping the render pass will be created by a call to clear()...
  2633. if (this._cachedViewport && !forceFullscreenViewport) {
  2634. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  2635. } else {
  2636. if (!requiredWidth) {
  2637. requiredWidth = texture.width;
  2638. if (lodLevel) {
  2639. requiredWidth = requiredWidth / Math.pow(2, lodLevel);
  2640. }
  2641. }
  2642. if (!requiredHeight) {
  2643. requiredHeight = texture.height;
  2644. if (lodLevel) {
  2645. requiredHeight = requiredHeight / Math.pow(2, lodLevel);
  2646. }
  2647. }
  2648. this._viewport(0, 0, requiredWidth, requiredHeight);
  2649. }
  2650. this.wipeCaches();
  2651. }
  2652. /**
  2653. * Unbind the current render target texture from the WebGPU context
  2654. * @param texture defines the render target texture to unbind
  2655. * @param disableGenerateMipMaps defines a boolean indicating that mipmaps must not be generated
  2656. * @param onBeforeUnbind defines a function which will be called before the effective unbind
  2657. */
  2658. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  2659. // TODO WEBGPU remove the assert debugging code
  2660. assert(this._currentRenderTarget === null || (this._currentRenderTarget !== null && texture === this._currentRenderTarget), "unBindFramebuffer - the texture we wan't to unbind is not the same than the currentRenderTarget! texture=" + texture + ", this._currentRenderTarget=" + this._currentRenderTarget);
  2661. if (onBeforeUnbind) {
  2662. onBeforeUnbind();
  2663. }
  2664. if (this._currentRenderPass && this._currentRenderPass !== this._mainRenderPassWrapper.renderPass) {
  2665. this._endRenderTargetRenderPass();
  2666. }
  2667. if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {
  2668. this._generateMipmaps(texture);
  2669. }
  2670. this._currentRenderTarget = null;
  2671. this._mrtAttachments = [];
  2672. this._cacheRenderPipeline.setMRTAttachments(this._mrtAttachments, []);
  2673. this._currentRenderPass = this._mainRenderPassWrapper.renderPass;
  2674. this._setDepthTextureFormat(this._mainRenderPassWrapper);
  2675. this._setColorFormat(this._mainRenderPassWrapper);
  2676. }
  2677. /**
  2678. * Unbind a list of render target textures from the WebGPU context
  2679. * @param textures defines the render target textures to unbind
  2680. * @param disableGenerateMipMaps defines a boolean indicating that mipmaps must not be generated
  2681. * @param onBeforeUnbind defines a function which will be called before the effective unbind
  2682. */
  2683. public unBindMultiColorAttachmentFramebuffer(textures: InternalTexture[], disableGenerateMipMaps: boolean = false, onBeforeUnbind?: () => void): void {
  2684. if (onBeforeUnbind) {
  2685. onBeforeUnbind();
  2686. }
  2687. const attachments = textures[0]._attachments!;
  2688. const count = attachments.length;
  2689. if (this._currentRenderPass && this._currentRenderPass !== this._mainRenderPassWrapper.renderPass) {
  2690. this._endRenderTargetRenderPass();
  2691. }
  2692. for (let i = 0; i < count; i++) {
  2693. const texture = textures[i];
  2694. if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {
  2695. this._generateMipmaps(texture);
  2696. }
  2697. }
  2698. this._currentRenderTarget = null;
  2699. this._mrtAttachments = [];
  2700. this._cacheRenderPipeline.setMRTAttachments(this._mrtAttachments, []);
  2701. this._currentRenderPass = this._mainRenderPassWrapper.renderPass;
  2702. this._setDepthTextureFormat(this._mainRenderPassWrapper);
  2703. this._setColorFormat(this._mainRenderPassWrapper);
  2704. }
  2705. /**
  2706. * Unbind the current render target and bind the default framebuffer
  2707. */
  2708. public restoreDefaultFramebuffer(): void {
  2709. if (this._currentRenderTarget) {
  2710. this.unBindFramebuffer(this._currentRenderTarget);
  2711. } else {
  2712. this._currentRenderPass = this._mainRenderPassWrapper.renderPass;
  2713. this._setDepthTextureFormat(this._mainRenderPassWrapper);
  2714. this._setColorFormat(this._mainRenderPassWrapper);
  2715. }
  2716. if (this._currentRenderPass) {
  2717. if (this._cachedViewport) {
  2718. this.setViewport(this._cachedViewport);
  2719. }
  2720. }
  2721. this.wipeCaches();
  2722. }
  2723. //------------------------------------------------------------------------------
  2724. // Render
  2725. //------------------------------------------------------------------------------
  2726. public setZOffset(value: number): void {
  2727. if (value !== this._depthCullingState.zOffset) {
  2728. this._depthCullingState.zOffset = value;
  2729. }
  2730. }
  2731. private _setColorFormat(wrapper: WebGPURenderPassWrapper): void {
  2732. const format = wrapper.colorAttachmentGPUTextures[0].format;
  2733. this._cacheRenderPipeline.setColorFormat(format);
  2734. if (this._colorFormat === format) {
  2735. return;
  2736. }
  2737. this._colorFormat = format;
  2738. }
  2739. private _setDepthTextureFormat(wrapper: WebGPURenderPassWrapper): void {
  2740. this._cacheRenderPipeline.setDepthStencilFormat(wrapper.depthTextureFormat);
  2741. if (this._depthTextureFormat === wrapper.depthTextureFormat) {
  2742. return;
  2743. }
  2744. this._depthTextureFormat = wrapper.depthTextureFormat;
  2745. }
  2746. public setDepthBuffer(enable: boolean): void {
  2747. if (this._depthCullingState.depthTest !== enable) {
  2748. this._depthCullingState.depthTest = enable;
  2749. }
  2750. }
  2751. public setDepthWrite(enable: boolean): void {
  2752. if (this._depthCullingState.depthMask !== enable) {
  2753. this._depthCullingState.depthMask = enable;
  2754. }
  2755. }
  2756. public setStencilBuffer(enable: boolean): void {
  2757. if (this._stencilState.stencilTest !== enable) {
  2758. this._stencilState.stencilTest = enable;
  2759. }
  2760. }
  2761. public setStencilMask(mask: number): void {
  2762. if (this._stencilState.stencilMask !== mask) {
  2763. this._stencilState.stencilMask = mask;
  2764. }
  2765. }
  2766. public setStencilFunction(stencilFunc: number) {
  2767. if (this._stencilState.stencilFunc !== stencilFunc) {
  2768. this._stencilState.stencilFunc = stencilFunc;
  2769. }
  2770. }
  2771. public setStencilFunctionReference(reference: number) {
  2772. if (this._stencilState.stencilFuncRef !== reference) {
  2773. this._stencilState.stencilFuncRef = reference;
  2774. }
  2775. }
  2776. public setStencilFunctionMask(mask: number) {
  2777. if (this._stencilState.stencilFuncMask !== mask) {
  2778. this._stencilState.stencilFuncMask = mask;
  2779. }
  2780. }
  2781. public setStencilOperationFail(operation: number): void {
  2782. if (this._stencilState.stencilOpStencilFail !== operation) {
  2783. this._stencilState.stencilOpStencilFail = operation;
  2784. }
  2785. }
  2786. public setStencilOperationDepthFail(operation: number): void {
  2787. if (this._stencilState.stencilOpDepthFail !== operation) {
  2788. this._stencilState.stencilOpDepthFail = operation;
  2789. }
  2790. }
  2791. public setStencilOperationPass(operation: number): void {
  2792. if (this._stencilState.stencilOpStencilDepthPass !== operation) {
  2793. this._stencilState.stencilOpStencilDepthPass = operation;
  2794. }
  2795. }
  2796. public setDitheringState(value: boolean): void {
  2797. // Does not exist in WebGPU
  2798. }
  2799. public setRasterizerState(value: boolean): void {
  2800. // Does not exist in WebGPU
  2801. }
  2802. public setDepthFunction(depthFunc: number) {
  2803. if (this._depthCullingState.depthFunc !== depthFunc) {
  2804. this._depthCullingState.depthFunc = depthFunc;
  2805. }
  2806. }
  2807. public setDepthFunctionToGreater(): void {
  2808. if (this._depthCullingState.depthFunc !== Constants.GREATER) {
  2809. this._depthCullingState.depthFunc = Constants.GREATER;
  2810. }
  2811. }
  2812. public setDepthFunctionToGreaterOrEqual(): void {
  2813. if (this._depthCullingState.depthFunc !== Constants.GEQUAL) {
  2814. this._depthCullingState.depthFunc = Constants.GEQUAL;
  2815. }
  2816. }
  2817. public setDepthFunctionToLess(): void {
  2818. if (this._depthCullingState.depthFunc !== Constants.LESS) {
  2819. this._depthCullingState.depthFunc = Constants.LESS;
  2820. }
  2821. }
  2822. public setDepthFunctionToLessOrEqual(): void {
  2823. if (this._depthCullingState.depthFunc !== Constants.LEQUAL) {
  2824. this._depthCullingState.depthFunc = Constants.LEQUAL;
  2825. }
  2826. }
  2827. /**
  2828. * Set various states to the context
  2829. * @param culling defines backface culling state
  2830. * @param zOffset defines the value to apply to zOffset (0 by default)
  2831. * @param force defines if states must be applied even if cache is up to date
  2832. * @param reverseSide defines if culling must be reversed (CCW instead of CW and CW instead of CCW)
  2833. */
  2834. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  2835. // Culling
  2836. if (this._depthCullingState.cull !== culling || force) {
  2837. this._depthCullingState.cull = culling;
  2838. }
  2839. // Cull face
  2840. // var cullFace = this.cullBackFaces ? this._gl.BACK : this._gl.FRONT;
  2841. var cullFace = this.cullBackFaces ? 1 : 2;
  2842. if (this._depthCullingState.cullFace !== cullFace || force) {
  2843. this._depthCullingState.cullFace = cullFace;
  2844. }
  2845. // Z offset
  2846. this.setZOffset(zOffset);
  2847. // Front face
  2848. // var frontFace = reverseSide ? this._gl.CW : this._gl.CCW;
  2849. var frontFace = reverseSide ? 1 : 2;
  2850. if (this._depthCullingState.frontFace !== frontFace || force) {
  2851. this._depthCullingState.frontFace = frontFace;
  2852. }
  2853. }
  2854. /**
  2855. * Sets the current alpha mode
  2856. * @param mode defines the mode to use (one of the Engine.ALPHA_XXX)
  2857. * @param noDepthWriteChange defines if depth writing state should remains unchanged (false by default)
  2858. * @see http://doc.babylonjs.com/resources/transparency_and_how_meshes_are_rendered
  2859. */
  2860. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  2861. if (this._alphaMode === mode) {
  2862. return;
  2863. }
  2864. switch (mode) {
  2865. case Engine.ALPHA_DISABLE:
  2866. this._alphaState.alphaBlend = false;
  2867. break;
  2868. case Engine.ALPHA_PREMULTIPLIED:
  2869. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  2870. this._alphaState.setAlphaBlendFunctionParameters(1, 0x0303, 1, 1);
  2871. this._alphaState.alphaBlend = true;
  2872. break;
  2873. case Engine.ALPHA_PREMULTIPLIED_PORTERDUFF:
  2874. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE_MINUS_SRC_ALPHA);
  2875. this._alphaState.setAlphaBlendFunctionParameters(1, 0x0303, 1, 0x0303);
  2876. this._alphaState.alphaBlend = true;
  2877. break;
  2878. case Engine.ALPHA_COMBINE:
  2879. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  2880. this._alphaState.setAlphaBlendFunctionParameters(0x0302, 0x0303, 1, 1);
  2881. this._alphaState.alphaBlend = true;
  2882. break;
  2883. case Engine.ALPHA_ONEONE:
  2884. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  2885. this._alphaState.setAlphaBlendFunctionParameters(1, 1, 0, 1);
  2886. this._alphaState.alphaBlend = true;
  2887. break;
  2888. case Engine.ALPHA_ADD:
  2889. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.SRC_ALPHA, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  2890. this._alphaState.setAlphaBlendFunctionParameters(0x0302, 1, 0, 1);
  2891. this._alphaState.alphaBlend = true;
  2892. break;
  2893. case Engine.ALPHA_SUBTRACT:
  2894. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.ZERO, this._gl.ONE_MINUS_SRC_COLOR, this._gl.ONE, this._gl.ONE);
  2895. this._alphaState.setAlphaBlendFunctionParameters(0, 0x0301, 1, 1);
  2896. this._alphaState.alphaBlend = true;
  2897. break;
  2898. case Engine.ALPHA_MULTIPLY:
  2899. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.DST_COLOR, this._gl.ZERO, this._gl.ONE, this._gl.ONE);
  2900. this._alphaState.setAlphaBlendFunctionParameters(0x0306, 0, 1, 1);
  2901. this._alphaState.alphaBlend = true;
  2902. break;
  2903. case Engine.ALPHA_MAXIMIZED:
  2904. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_COLOR, this._gl.ONE, this._gl.ONE);
  2905. this._alphaState.setAlphaBlendFunctionParameters(0x0302, 0x0301, 1, 1);
  2906. this._alphaState.alphaBlend = true;
  2907. break;
  2908. case Engine.ALPHA_INTERPOLATE:
  2909. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.CONSTANT_COLOR, this._gl.ONE_MINUS_CONSTANT_COLOR, this._gl.CONSTANT_ALPHA, this._gl.ONE_MINUS_CONSTANT_ALPHA);
  2910. this._alphaState.setAlphaBlendFunctionParameters(0x8001, 0x8002, 0x8003, 0x8004);
  2911. this._alphaState.alphaBlend = true;
  2912. break;
  2913. case Engine.ALPHA_SCREENMODE:
  2914. // this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE_MINUS_SRC_COLOR, this._gl.ONE, this._gl.ONE_MINUS_SRC_ALPHA);
  2915. this._alphaState.setAlphaBlendFunctionParameters(1, 0x0301, 1, 0x0303);
  2916. this._alphaState.alphaBlend = true;
  2917. break;
  2918. }
  2919. if (!noDepthWriteChange) {
  2920. this.setDepthWrite(mode === Engine.ALPHA_DISABLE);
  2921. this._cacheRenderPipeline.setDepthWriteEnabled(mode === Engine.ALPHA_DISABLE);
  2922. }
  2923. this._alphaMode = mode;
  2924. this._cacheRenderPipeline.setAlphaBlendEnabled(this._alphaState.alphaBlend);
  2925. this._cacheRenderPipeline.setAlphaBlendFactors(this._alphaState._blendFunctionParameters, this._alphaState._blendEquationParameters);
  2926. }
  2927. /**
  2928. * Sets the current alpha equation
  2929. * @param equation defines the equation to use (one of the Engine.ALPHA_EQUATION_XXX)
  2930. */
  2931. public setAlphaEquation(equation: number): void {
  2932. super.setAlphaEquation(equation);
  2933. this._cacheRenderPipeline.setAlphaBlendFactors(this._alphaState._blendFunctionParameters, this._alphaState._blendEquationParameters);
  2934. }
  2935. private _getBindGroupsToRender(): GPUBindGroup[] {
  2936. const webgpuPipelineContext = this._currentEffect!._pipelineContext as WebGPUPipelineContext;
  2937. if (webgpuPipelineContext.uniformBuffer) {
  2938. this.bindUniformBufferBase(webgpuPipelineContext.uniformBuffer.getBuffer()!, 0, "LeftOver");
  2939. webgpuPipelineContext.uniformBuffer.update();
  2940. }
  2941. let bufferKey = "";
  2942. for (let i = 0; i < webgpuPipelineContext.shaderProcessingContext.uniformBufferNames.length; ++i) {
  2943. const bufferName = webgpuPipelineContext.shaderProcessingContext.uniformBufferNames[i];
  2944. const dataBuffer = this._uniformsBuffers[bufferName];
  2945. if (dataBuffer) {
  2946. bufferKey += dataBuffer.uniqueId + "_";
  2947. }
  2948. }
  2949. let bindGroups: GPUBindGroup[] = webgpuPipelineContext.bindGroupsCache[bufferKey];
  2950. if (bindGroups) {
  2951. return bindGroups;
  2952. }
  2953. bindGroups = [];
  2954. webgpuPipelineContext.bindGroupsCache[bufferKey] = bindGroups;
  2955. this._counters.numBindGroupsCreation++;
  2956. const bindGroupLayouts = webgpuPipelineContext.bindGroupLayouts;
  2957. for (let i = 0; i < webgpuPipelineContext.shaderProcessingContext.orderedUBOsAndSamplers.length; i++) {
  2958. const setDefinition = webgpuPipelineContext.shaderProcessingContext.orderedUBOsAndSamplers[i];
  2959. if (setDefinition === undefined) {
  2960. let groupLayout = bindGroupLayouts[i];
  2961. bindGroups[i] = this._device.createBindGroup({
  2962. layout: groupLayout,
  2963. entries: [],
  2964. });
  2965. continue;
  2966. }
  2967. const entries: GPUBindGroupEntry[] = [];
  2968. for (let j = 0; j < setDefinition.length; j++) {
  2969. const bindingDefinition = webgpuPipelineContext.shaderProcessingContext.orderedUBOsAndSamplers[i][j];
  2970. if (bindingDefinition === undefined) {
  2971. continue;
  2972. }
  2973. if (bindingDefinition.isSampler) {
  2974. const bindingInfo = webgpuPipelineContext.samplers[bindingDefinition.name];
  2975. if (bindingInfo) {
  2976. const texture = webgpuPipelineContext.textures[bindingInfo.firstTextureName]?.texture;
  2977. if (!texture) {
  2978. Logger.Error(`Could not create the gpu sampler "${bindingDefinition.name}" because no texture can be looked up for the name "${bindingInfo.firstTextureName}". bindingInfo=${JSON.stringify(bindingInfo)}, webgpuPipelineContext.textures=${webgpuPipelineContext.textures}`, 50);
  2979. continue;
  2980. }
  2981. entries.push({
  2982. binding: bindingInfo.samplerBinding,
  2983. resource: this._cacheSampler.getSampler(texture),
  2984. });
  2985. } else {
  2986. Logger.Error(`Sampler "${bindingDefinition.name}" could not be bound. bindingDefinition=${JSON.stringify(bindingDefinition)}, webgpuPipelineContext.samplers=${JSON.stringify(webgpuPipelineContext.samplers)}`, 50);
  2987. }
  2988. } else if (bindingDefinition.isTexture) {
  2989. const bindingInfo = webgpuPipelineContext.textures[bindingDefinition.name];
  2990. if (bindingInfo) {
  2991. if (this.dbgSanityChecks && bindingInfo.texture === null) {
  2992. Logger.Error(`Trying to bind a null texture! bindingDefinition=${JSON.stringify(bindingDefinition)}, bindingInfo=${JSON.stringify(bindingInfo, (key: string, value: any) => key === 'texture' ? '<no dump>' : value)}`, 50);
  2993. continue;
  2994. }
  2995. const hardwareTexture = bindingInfo.texture._hardwareTexture as WebGPUHardwareTexture;
  2996. if (this.dbgSanityChecks && !hardwareTexture.view) {
  2997. Logger.Error(`Trying to bind a null gpu texture! bindingDefinition=${JSON.stringify(bindingDefinition)}, bindingInfo=${JSON.stringify(bindingInfo, (key: string, value: any) => key === 'texture' ? '<no dump>' : value)}, isReady=${bindingInfo.texture.isReady}`, 50);
  2998. continue;
  2999. }
  3000. entries.push({
  3001. binding: bindingInfo.textureBinding,
  3002. resource: hardwareTexture.view!,
  3003. });
  3004. } else {
  3005. Logger.Error(`Texture "${bindingDefinition.name}" could not be bound. bindingDefinition=${JSON.stringify(bindingDefinition)}, webgpuPipelineContext.textures=${JSON.stringify(webgpuPipelineContext.textures, (key: string, value: any) => key === 'texture' ? '<no dump>' : value)}`, 50);
  3006. }
  3007. } else {
  3008. const dataBuffer = this._uniformsBuffers[bindingDefinition.name];
  3009. if (dataBuffer) {
  3010. const webgpuBuffer = dataBuffer.underlyingResource as GPUBuffer;
  3011. entries.push({
  3012. binding: j,
  3013. resource: {
  3014. buffer: webgpuBuffer,
  3015. offset: 0,
  3016. size: dataBuffer.capacity,
  3017. },
  3018. });
  3019. } else {
  3020. Logger.Error(`UBO "${bindingDefinition.name}. bindingDefinition=${JSON.stringify(bindingDefinition)}, _uniformsBuffers=${JSON.stringify(this._uniformsBuffers)}`, 50);
  3021. }
  3022. }
  3023. }
  3024. if (entries.length > 0) {
  3025. let groupLayout = bindGroupLayouts[i];
  3026. bindGroups[i] = this._device.createBindGroup({
  3027. layout: groupLayout,
  3028. entries,
  3029. });
  3030. }
  3031. }
  3032. return bindGroups;
  3033. }
  3034. private _bindVertexInputs(): void {
  3035. const renderPass = this._bundleEncoder || this._getCurrentRenderPass();
  3036. if (this._currentIndexBuffer) {
  3037. renderPass.setIndexBuffer(this._currentIndexBuffer.underlyingResource, this._currentIndexBuffer!.is32Bits ? WebGPUConstants.IndexFormat.Uint32 : WebGPUConstants.IndexFormat.Uint16, 0);
  3038. }
  3039. const effect = this._currentEffect!;
  3040. const attributes = effect.getAttributesNames();
  3041. let bufferIdx = 0;
  3042. for (var index = 0; index < attributes.length; index++) {
  3043. const order = effect.getAttributeLocation(index);
  3044. if (order >= 0) {
  3045. let vertexBuffer = this._currentVertexBuffers![attributes[index]];
  3046. if (!vertexBuffer) {
  3047. // In WebGL it's valid to not bind a vertex buffer to an attribute, but it's not valid in WebGPU
  3048. // So we must bind a dummy buffer when we are not given one for a specific attribute
  3049. vertexBuffer = this._emptyVertexBuffer;
  3050. }
  3051. const buffer = vertexBuffer.getBuffer();
  3052. if (buffer) {
  3053. renderPass.setVertexBuffer(bufferIdx++, buffer.underlyingResource, vertexBuffer.byteOffset);
  3054. }
  3055. }
  3056. }
  3057. }
  3058. private _setRenderBindGroups(bindGroups: GPUBindGroup[]): void {
  3059. // TODO WEBGPU. Only set groups if changes happened.
  3060. const renderPass = this._bundleEncoder || this._getCurrentRenderPass();
  3061. for (let i = 0; i < bindGroups.length; i++) {
  3062. renderPass.setBindGroup(i, bindGroups[i]);
  3063. }
  3064. }
  3065. private _setRenderPipeline(fillMode: number): void {
  3066. const renderPass = this._bundleEncoder || this._getCurrentRenderPass();
  3067. this._cacheRenderPipeline.setDepthCullingState(
  3068. !!this._depthCullingState.cull,
  3069. this._depthCullingState.frontFace ?? 2,
  3070. this._depthCullingState.cullFace ?? 1,
  3071. this._depthCullingState.zOffset,
  3072. this._depthCullingState.depthTest,
  3073. this._depthCullingState.depthMask,
  3074. this._depthCullingState.depthFunc
  3075. );
  3076. this._cacheRenderPipeline.setStencilState(
  3077. this._stencilState.stencilTest,
  3078. this._stencilState.stencilFunc,
  3079. this._stencilState.stencilOpDepthFail,
  3080. this._stencilState.stencilOpStencilDepthPass,
  3081. this._stencilState.stencilOpStencilFail,
  3082. this._stencilState.stencilFuncMask,
  3083. this._stencilState.stencilMask
  3084. );
  3085. const pipeline = this._cacheRenderPipeline.getRenderPipeline(fillMode, this._currentEffect!, this._currentRenderTarget ? this._currentRenderTarget.samples : this._mainPassSampleCount);
  3086. renderPass.setPipeline(pipeline);
  3087. this._bindVertexInputs();
  3088. const bindGroups = this._getBindGroupsToRender();
  3089. this._setRenderBindGroups(bindGroups);
  3090. // TODO WEBGPU add dirty mechanism as for _alphaState._blendConstants
  3091. if (this._stencilState.stencilTest && renderPass !== this._bundleEncoder) {
  3092. this._getCurrentRenderPass().setStencilReference(this._stencilState.stencilFuncRef);
  3093. }
  3094. // TODO WebGPU add back the dirty mechanism, but we need to distinguish between the main render pass and the RTT pass (if any)
  3095. if (this._alphaState.alphaBlend /* && this._alphaState._isBlendConstantsDirty*/ && renderPass !== this._bundleEncoder) {
  3096. this._getCurrentRenderPass().setBlendColor(this._alphaState._blendConstants as any);
  3097. }
  3098. if (renderPass !== this._bundleEncoder) {
  3099. this._applyViewport(renderPass as GPURenderPassEncoder);
  3100. this._applyScissor(renderPass as GPURenderPassEncoder);
  3101. }
  3102. }
  3103. /**
  3104. * Draw a list of indexed primitives
  3105. * @param fillMode defines the primitive to use
  3106. * @param indexStart defines the starting index
  3107. * @param indexCount defines the number of index to draw
  3108. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  3109. */
  3110. public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount: number = 1): void {
  3111. const renderPass = this._bundleEncoder || this._getCurrentRenderPass();
  3112. this._setRenderPipeline(fillMode);
  3113. renderPass.drawIndexed(indexCount, instancesCount || 1, indexStart, 0, 0);
  3114. this._reportDrawCall();
  3115. }
  3116. /**
  3117. * Draw a list of unindexed primitives
  3118. * @param fillMode defines the primitive to use
  3119. * @param verticesStart defines the index of first vertex to draw
  3120. * @param verticesCount defines the count of vertices to draw
  3121. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  3122. */
  3123. public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount: number = 1): void {
  3124. const renderPass = this._bundleEncoder || this._getCurrentRenderPass();
  3125. this._currentIndexBuffer = null;
  3126. this._setRenderPipeline(fillMode);
  3127. renderPass.draw(verticesCount, instancesCount || 1, verticesStart, 0);
  3128. this._reportDrawCall();
  3129. }
  3130. //------------------------------------------------------------------------------
  3131. // Render Bundle
  3132. //------------------------------------------------------------------------------
  3133. private _bundleEncoder: Nullable<GPURenderBundleEncoder>;
  3134. /**
  3135. * Start recording all the gpu calls into a bundle.
  3136. */
  3137. public startRecordBundle(): void {
  3138. // TODO. WebGPU. options should be dynamic.
  3139. this._bundleEncoder = this._device.createRenderBundleEncoder({
  3140. colorFormats: [ WebGPUConstants.TextureFormat.BGRA8Unorm ],
  3141. depthStencilFormat: WebGPUConstants.TextureFormat.Depth24PlusStencil8,
  3142. sampleCount: this._mainPassSampleCount,
  3143. });
  3144. }
  3145. /**
  3146. * Stops recording the bundle.
  3147. * @returns the recorded bundle
  3148. */
  3149. public stopRecordBundle(): GPURenderBundle {
  3150. const bundle = this._bundleEncoder!.finish();
  3151. this._bundleEncoder = null;
  3152. return bundle;
  3153. }
  3154. /**
  3155. * Execute the previously recorded bundle.
  3156. * @param bundles defines the bundle to replay
  3157. */
  3158. public executeBundles(bundles: GPURenderBundle[]): void {
  3159. const renderPass = this._getCurrentRenderPass();
  3160. renderPass.executeBundles(bundles);
  3161. }
  3162. //------------------------------------------------------------------------------
  3163. // Dispose
  3164. //------------------------------------------------------------------------------
  3165. /**
  3166. * Dispose and release all associated resources
  3167. */
  3168. public dispose(): void {
  3169. if (this._mainTexture) {
  3170. this._mainTexture.destroy();
  3171. }
  3172. if (this._depthTexture) {
  3173. this._depthTexture.destroy();
  3174. }
  3175. super.dispose();
  3176. }
  3177. //------------------------------------------------------------------------------
  3178. // Misc
  3179. //------------------------------------------------------------------------------
  3180. /**
  3181. * Gets the current render width
  3182. * @param useScreen defines if screen size must be used (or the current render target if any)
  3183. * @returns a number defining the current render width
  3184. */
  3185. public getRenderWidth(useScreen = false): number {
  3186. if (!useScreen && this._currentRenderTarget) {
  3187. return this._currentRenderTarget.width;
  3188. }
  3189. return this._canvas.width;
  3190. }
  3191. /**
  3192. * Gets the current render height
  3193. * @param useScreen defines if screen size must be used (or the current render target if any)
  3194. * @returns a number defining the current render height
  3195. */
  3196. public getRenderHeight(useScreen = false): number {
  3197. if (!useScreen && this._currentRenderTarget) {
  3198. return this._currentRenderTarget.height;
  3199. }
  3200. return this._canvas.height;
  3201. }
  3202. /**
  3203. * Gets the HTML canvas attached with the current WebGPU context
  3204. * @returns a HTML canvas
  3205. */
  3206. public getRenderingCanvas(): Nullable<HTMLCanvasElement> {
  3207. return this._canvas;
  3208. }
  3209. /** @hidden */
  3210. public _debugPushGroup(groupName: string, targetObject?: number): void {
  3211. if (!this._options.enableGPUDebugMarkers) {
  3212. return;
  3213. }
  3214. if (targetObject === 0 || targetObject === 1) {
  3215. const encoder = targetObject === 0 ? this._renderEncoder : this._renderTargetEncoder;
  3216. encoder.pushDebugGroup(groupName);
  3217. } else if (this._currentRenderPass) {
  3218. this._currentRenderPass.pushDebugGroup(groupName);
  3219. } else {
  3220. this._pendingDebugCommands.push(["push", groupName]);
  3221. }
  3222. }
  3223. /** @hidden */
  3224. public _debugPopGroup(targetObject?: number): void {
  3225. if (!this._options.enableGPUDebugMarkers) {
  3226. return;
  3227. }
  3228. if (targetObject === 0 || targetObject === 1) {
  3229. const encoder = targetObject === 0 ? this._renderEncoder : this._renderTargetEncoder;
  3230. encoder.popDebugGroup();
  3231. } else if (this._currentRenderPass) {
  3232. this._currentRenderPass.popDebugGroup();
  3233. } else {
  3234. this._pendingDebugCommands.push(["pop", null]);
  3235. }
  3236. }
  3237. /** @hidden */
  3238. public _debugInsertMarker(text: string, targetObject?: number): void {
  3239. if (!this._options.enableGPUDebugMarkers) {
  3240. return;
  3241. }
  3242. if (targetObject === 0 || targetObject === 1) {
  3243. const encoder = targetObject === 0 ? this._renderEncoder : this._renderTargetEncoder;
  3244. encoder.insertDebugMarker(text);
  3245. } else if (this._currentRenderPass) {
  3246. this._currentRenderPass.insertDebugMarker(text);
  3247. } else {
  3248. this._pendingDebugCommands.push(["insert", text]);
  3249. }
  3250. }
  3251. private _debugFlushPendingCommands(): void {
  3252. for (let i = 0; i < this._pendingDebugCommands.length; ++i) {
  3253. const [name, param] = this._pendingDebugCommands[i];
  3254. switch (name) {
  3255. case "push":
  3256. this._debugPushGroup(param!);
  3257. break;
  3258. case "pop":
  3259. this._debugPopGroup();
  3260. break;
  3261. case "insert":
  3262. this._debugInsertMarker(param!);
  3263. break;
  3264. }
  3265. }
  3266. this._pendingDebugCommands.length = 0;
  3267. }
  3268. //------------------------------------------------------------------------------
  3269. // Errors
  3270. //------------------------------------------------------------------------------
  3271. /**
  3272. * Get the current error code of the WebGPU context
  3273. * @returns the error code
  3274. */
  3275. public getError(): number {
  3276. // TODO WEBGPU. from the webgpu errors.
  3277. return 0;
  3278. }
  3279. //------------------------------------------------------------------------------
  3280. // Unused WebGPU
  3281. //------------------------------------------------------------------------------
  3282. /** @hidden */
  3283. public bindSamplers(effect: Effect): void { }
  3284. /** @hidden */
  3285. public _bindTextureDirectly(target: number, texture: InternalTexture, forTextureDataUpdate = false, force = false): boolean {
  3286. return false;
  3287. }
  3288. /** @hidden */
  3289. public _releaseFramebufferObjects(texture: InternalTexture): void { }
  3290. /** @hidden */
  3291. public applyStates() { }
  3292. /**
  3293. * Gets a boolean indicating if all created effects are ready
  3294. * @returns always true - No parallel shader compilation
  3295. */
  3296. public areAllEffectsReady(): boolean {
  3297. return true;
  3298. }
  3299. /** @hidden */
  3300. public _executeWhenRenderingStateIsCompiled(pipelineContext: IPipelineContext, action: () => void) {
  3301. // No parallel shader compilation.
  3302. // No Async, so direct launch
  3303. action();
  3304. }
  3305. /** @hidden */
  3306. public _isRenderingStateCompiled(pipelineContext: IPipelineContext): boolean {
  3307. // No parallel shader compilation.
  3308. return true;
  3309. }
  3310. /** @hidden */
  3311. public _getUnpackAlignement(): number {
  3312. return 1;
  3313. }
  3314. /** @hidden */
  3315. public _unpackFlipY(value: boolean) { }
  3316. // TODO WEBGPU. All of the below should go once engine split with baseEngine.
  3317. /** @hidden */
  3318. public _getSamplingParameters(samplingMode: number, generateMipMaps: boolean): { min: number; mag: number } {
  3319. throw "_getSamplingParameters is not available in WebGPU";
  3320. }
  3321. /** @hidden */
  3322. public bindUniformBlock(pipelineContext: IPipelineContext, blockName: string, index: number): void {
  3323. }
  3324. /** @hidden */
  3325. public getUniforms(pipelineContext: IPipelineContext, uniformsNames: string[]): Nullable<WebGLUniformLocation>[] {
  3326. return [];
  3327. }
  3328. /** @hidden */
  3329. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  3330. return false;
  3331. }
  3332. /** @hidden */
  3333. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  3334. return false;
  3335. }
  3336. /** @hidden */
  3337. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  3338. return false;
  3339. }
  3340. /** @hidden */
  3341. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): boolean {
  3342. return false;
  3343. }
  3344. /** @hidden */
  3345. public setArray(uniform: WebGLUniformLocation, array: number[]): boolean {
  3346. return false;
  3347. }
  3348. /** @hidden */
  3349. public setArray2(uniform: WebGLUniformLocation, array: number[]): boolean {
  3350. return false;
  3351. }
  3352. /** @hidden */
  3353. public setArray3(uniform: WebGLUniformLocation, array: number[]): boolean {
  3354. return false;
  3355. }
  3356. /** @hidden */
  3357. public setArray4(uniform: WebGLUniformLocation, array: number[]): boolean {
  3358. return false;
  3359. }
  3360. /** @hidden */
  3361. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): boolean {
  3362. return false;
  3363. }
  3364. /** @hidden */
  3365. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): boolean {
  3366. return false;
  3367. }
  3368. /** @hidden */
  3369. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): boolean {
  3370. return false;
  3371. }
  3372. /** @hidden */
  3373. public setFloat(uniform: WebGLUniformLocation, value: number): boolean {
  3374. return false;
  3375. }
  3376. /** @hidden */
  3377. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): boolean {
  3378. return false;
  3379. }
  3380. /** @hidden */
  3381. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): boolean {
  3382. return false;
  3383. }
  3384. /** @hidden */
  3385. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): boolean {
  3386. return false;
  3387. }
  3388. }
  3389. /** @hidden */
  3390. function _convertRGBtoRGBATextureData(rgbData: any, width: number, height: number, textureType: number): ArrayBufferView {
  3391. // Create new RGBA data container.
  3392. var rgbaData: any;
  3393. if (textureType === Constants.TEXTURETYPE_FLOAT) {
  3394. rgbaData = new Float32Array(width * height * 4);
  3395. }
  3396. else {
  3397. rgbaData = new Uint32Array(width * height * 4);
  3398. }
  3399. // Convert each pixel.
  3400. for (let x = 0; x < width; x++) {
  3401. for (let y = 0; y < height; y++) {
  3402. let index = (y * width + x) * 3;
  3403. let newIndex = (y * width + x) * 4;
  3404. // Map Old Value to new value.
  3405. rgbaData[newIndex + 0] = rgbData[index + 0];
  3406. rgbaData[newIndex + 1] = rgbData[index + 1];
  3407. rgbaData[newIndex + 2] = rgbData[index + 2];
  3408. // Add fully opaque alpha channel.
  3409. rgbaData[newIndex + 3] = 1;
  3410. }
  3411. }
  3412. return rgbaData;
  3413. }