webgpuEngine.ts 196 KB

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