123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428 |
- /*// debugger tools
- import 'https://greggman.github.io/webgpu-avoid-redundant-state-setting/webgpu-check-redundant-state-setting.js';
- //*/
- import { WebGPUCoordinateSystem } from 'three';
- import { GPUFeatureName, GPUTextureFormat, GPULoadOp, GPUStoreOp, GPUIndexFormat, GPUTextureViewDimension } from './utils/WebGPUConstants.js';
- import WGSLNodeBuilder from './nodes/WGSLNodeBuilder.js';
- import Backend from '../common/Backend.js';
- import WebGPUUtils from './utils/WebGPUUtils.js';
- import WebGPUAttributeUtils from './utils/WebGPUAttributeUtils.js';
- import WebGPUBindingUtils from './utils/WebGPUBindingUtils.js';
- import WebGPUPipelineUtils from './utils/WebGPUPipelineUtils.js';
- import WebGPUTextureUtils from './utils/WebGPUTextureUtils.js';
- //
- class WebGPUBackend extends Backend {
- constructor( parameters = {} ) {
- super( parameters );
- this.isWebGPUBackend = true;
- // some parameters require default values other than "undefined"
- this.parameters.alpha = ( parameters.alpha === undefined ) ? true : parameters.alpha;
- this.parameters.antialias = ( parameters.antialias === true );
- if ( this.parameters.antialias === true ) {
- this.parameters.sampleCount = ( parameters.sampleCount === undefined ) ? 4 : parameters.sampleCount;
- } else {
- this.parameters.sampleCount = 1;
- }
- this.parameters.requiredLimits = ( parameters.requiredLimits === undefined ) ? {} : parameters.requiredLimits;
- this.trackTimestamp = ( parameters.trackTimestamp === true );
- this.device = null;
- this.context = null;
- this.colorBuffer = null;
- this.defaultRenderPassdescriptor = null;
- this.utils = new WebGPUUtils( this );
- this.attributeUtils = new WebGPUAttributeUtils( this );
- this.bindingUtils = new WebGPUBindingUtils( this );
- this.pipelineUtils = new WebGPUPipelineUtils( this );
- this.textureUtils = new WebGPUTextureUtils( this );
- this.occludedResolveCache = new Map();
- }
- async init( renderer ) {
- await super.init( renderer );
- //
- const parameters = this.parameters;
- // create the device if it is not passed with parameters
- let device;
- if ( parameters.device === undefined ) {
- const adapterOptions = {
- powerPreference: parameters.powerPreference
- };
- const adapter = await navigator.gpu.requestAdapter( adapterOptions );
- if ( adapter === null ) {
- throw new Error( 'WebGPUBackend: Unable to create WebGPU adapter.' );
- }
- // feature support
- const features = Object.values( GPUFeatureName );
- const supportedFeatures = [];
- for ( const name of features ) {
- if ( adapter.features.has( name ) ) {
- supportedFeatures.push( name );
- }
- }
- const deviceDescriptor = {
- requiredFeatures: supportedFeatures,
- requiredLimits: parameters.requiredLimits
- };
- device = await adapter.requestDevice( deviceDescriptor );
- } else {
- device = parameters.device;
- }
- const context = ( parameters.context !== undefined ) ? parameters.context : renderer.domElement.getContext( 'webgpu' );
- this.device = device;
- this.context = context;
- const alphaMode = parameters.alpha ? 'premultiplied' : 'opaque';
- this.context.configure( {
- device: this.device,
- format: GPUTextureFormat.BGRA8Unorm,
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
- alphaMode: alphaMode
- } );
- this.updateSize();
- }
- get coordinateSystem() {
- return WebGPUCoordinateSystem;
- }
- async getArrayBufferAsync( attribute ) {
- return await this.attributeUtils.getArrayBufferAsync( attribute );
- }
- getContext() {
- return this.context;
- }
- _getDefaultRenderPassDescriptor() {
- let descriptor = this.defaultRenderPassdescriptor;
- const antialias = this.parameters.antialias;
- if ( descriptor === null ) {
- const renderer = this.renderer;
- descriptor = {
- colorAttachments: [ {
- view: null
- } ],
- depthStencilAttachment: {
- view: this.textureUtils.getDepthBuffer( renderer.depth, renderer.stencil ).createView()
- }
- };
- const colorAttachment = descriptor.colorAttachments[ 0 ];
- if ( antialias === true ) {
- colorAttachment.view = this.colorBuffer.createView();
- } else {
- colorAttachment.resolveTarget = undefined;
- }
- this.defaultRenderPassdescriptor = descriptor;
- }
- const colorAttachment = descriptor.colorAttachments[ 0 ];
- if ( antialias === true ) {
- colorAttachment.resolveTarget = this.context.getCurrentTexture().createView();
- } else {
- colorAttachment.view = this.context.getCurrentTexture().createView();
- }
- return descriptor;
- }
- _getRenderPassDescriptor( renderContext ) {
- const renderTarget = renderContext.renderTarget;
- const renderTargetData = this.get( renderTarget );
- let descriptors = renderTargetData.descriptors;
- if ( descriptors === undefined ) {
- descriptors = [];
- renderTargetData.descriptors = descriptors;
- }
- if ( renderTargetData.width !== renderTarget.width ||
- renderTargetData.height !== renderTarget.height ||
- renderTargetData.activeMipmapLevel !== renderTarget.activeMipmapLevel ||
- renderTargetData.samples !== renderTarget.samples
- ) {
- descriptors.length = 0;
- }
- let descriptor = descriptors[ renderContext.activeCubeFace ];
- if ( descriptor === undefined ) {
- const textures = renderContext.textures;
- const colorAttachments = [];
- for ( let i = 0; i < textures.length; i ++ ) {
- const textureData = this.get( textures[ i ] );
- const textureView = textureData.texture.createView( {
- baseMipLevel: renderContext.activeMipmapLevel,
- mipLevelCount: 1,
- baseArrayLayer: renderContext.activeCubeFace,
- dimension: GPUTextureViewDimension.TwoD
- } );
- let view, resolveTarget;
- if ( textureData.msaaTexture !== undefined ) {
- view = textureData.msaaTexture.createView();
- resolveTarget = textureView;
- } else {
- view = textureView;
- resolveTarget = undefined;
- }
- colorAttachments.push( {
- view,
- resolveTarget,
- loadOp: GPULoadOp.Load,
- storeOp: GPUStoreOp.Store
- } );
- }
- const depthTextureData = this.get( renderContext.depthTexture );
- const depthStencilAttachment = {
- view: depthTextureData.texture.createView(),
- };
- descriptor = {
- colorAttachments,
- depthStencilAttachment
- };
- descriptors[ renderContext.activeCubeFace ] = descriptor;
- renderTargetData.width = renderTarget.width;
- renderTargetData.height = renderTarget.height;
- renderTargetData.samples = renderTarget.samples;
- renderTargetData.activeMipmapLevel = renderTarget.activeMipmapLevel;
- }
- return descriptor;
- }
- beginRender( renderContext ) {
- const renderContextData = this.get( renderContext );
- const device = this.device;
- const occlusionQueryCount = renderContext.occlusionQueryCount;
- let occlusionQuerySet;
- if ( occlusionQueryCount > 0 ) {
- if ( renderContextData.currentOcclusionQuerySet ) renderContextData.currentOcclusionQuerySet.destroy();
- if ( renderContextData.currentOcclusionQueryBuffer ) renderContextData.currentOcclusionQueryBuffer.destroy();
- // Get a reference to the array of objects with queries. The renderContextData property
- // can be changed by another render pass before the buffer.mapAsyc() completes.
- renderContextData.currentOcclusionQuerySet = renderContextData.occlusionQuerySet;
- renderContextData.currentOcclusionQueryBuffer = renderContextData.occlusionQueryBuffer;
- renderContextData.currentOcclusionQueryObjects = renderContextData.occlusionQueryObjects;
- //
- occlusionQuerySet = device.createQuerySet( { type: 'occlusion', count: occlusionQueryCount } );
- renderContextData.occlusionQuerySet = occlusionQuerySet;
- renderContextData.occlusionQueryIndex = 0;
- renderContextData.occlusionQueryObjects = new Array( occlusionQueryCount );
- renderContextData.lastOcclusionObject = null;
- }
- let descriptor;
- if ( renderContext.textures === null ) {
- descriptor = this._getDefaultRenderPassDescriptor();
- } else {
- descriptor = this._getRenderPassDescriptor( renderContext );
- }
- this.initTimestampQuery( renderContext, descriptor );
- descriptor.occlusionQuerySet = occlusionQuerySet;
- const depthStencilAttachment = descriptor.depthStencilAttachment;
- if ( renderContext.textures !== null ) {
- const colorAttachments = descriptor.colorAttachments;
- for ( let i = 0; i < colorAttachments.length; i ++ ) {
- const colorAttachment = colorAttachments[ i ];
- if ( renderContext.clearColor ) {
- colorAttachment.clearValue = renderContext.clearColorValue;
- colorAttachment.loadOp = GPULoadOp.Clear;
- colorAttachment.storeOp = GPUStoreOp.Store;
- } else {
- colorAttachment.loadOp = GPULoadOp.Load;
- colorAttachment.storeOp = GPUStoreOp.Store;
- }
- }
- } else {
- const colorAttachment = descriptor.colorAttachments[ 0 ];
- if ( renderContext.clearColor ) {
- colorAttachment.clearValue = renderContext.clearColorValue;
- colorAttachment.loadOp = GPULoadOp.Clear;
- colorAttachment.storeOp = GPUStoreOp.Store;
- } else {
- colorAttachment.loadOp = GPULoadOp.Load;
- colorAttachment.storeOp = GPUStoreOp.Store;
- }
- }
- //
- if ( renderContext.depth ) {
- if ( renderContext.clearDepth ) {
- depthStencilAttachment.depthClearValue = renderContext.clearDepthValue;
- depthStencilAttachment.depthLoadOp = GPULoadOp.Clear;
- depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
- } else {
- depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
- depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
- }
- }
- if ( renderContext.stencil ) {
- if ( renderContext.clearStencil ) {
- depthStencilAttachment.stencilClearValue = renderContext.clearStencilValue;
- depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear;
- depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
- } else {
- depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
- depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
- }
- }
- //
- const encoder = device.createCommandEncoder( { label: 'renderContext_' + renderContext.id } );
- const currentPass = encoder.beginRenderPass( descriptor );
- //
- renderContextData.descriptor = descriptor;
- renderContextData.encoder = encoder;
- renderContextData.currentPass = currentPass;
- renderContextData.currentSets = { attributes: {} };
- //
- if ( renderContext.viewport ) {
- this.updateViewport( renderContext );
- }
- if ( renderContext.scissor ) {
- const { x, y, width, height } = renderContext.scissorValue;
- currentPass.setScissorRect( x, renderContext.height - height - y, width, height );
- }
- }
- finishRender( renderContext ) {
- const renderContextData = this.get( renderContext );
- const occlusionQueryCount = renderContext.occlusionQueryCount;
- if ( renderContextData.renderBundles !== undefined && renderContextData.renderBundles.length > 0 ) {
- renderContextData.registerBundlesPhase = false;
- renderContextData.currentPass.executeBundles( renderContextData.renderBundles );
- }
- if ( occlusionQueryCount > renderContextData.occlusionQueryIndex ) {
- renderContextData.currentPass.endOcclusionQuery();
- }
- renderContextData.currentPass.end();
- if ( occlusionQueryCount > 0 ) {
- const bufferSize = occlusionQueryCount * 8; // 8 byte entries for query results
- //
- let queryResolveBuffer = this.occludedResolveCache.get( bufferSize );
- if ( queryResolveBuffer === undefined ) {
- queryResolveBuffer = this.device.createBuffer(
- {
- size: bufferSize,
- usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC
- }
- );
- this.occludedResolveCache.set( bufferSize, queryResolveBuffer );
- }
- //
- const readBuffer = this.device.createBuffer(
- {
- size: bufferSize,
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
- }
- );
- // two buffers required here - WebGPU doesn't allow usage of QUERY_RESOLVE & MAP_READ to be combined
- renderContextData.encoder.resolveQuerySet( renderContextData.occlusionQuerySet, 0, occlusionQueryCount, queryResolveBuffer, 0 );
- renderContextData.encoder.copyBufferToBuffer( queryResolveBuffer, 0, readBuffer, 0, bufferSize );
- renderContextData.occlusionQueryBuffer = readBuffer;
- //
- this.resolveOccludedAsync( renderContext );
- }
- this.prepareTimestampBuffer( renderContext, renderContextData.encoder );
- this.device.queue.submit( [ renderContextData.encoder.finish() ] );
- //
- if ( renderContext.textures !== null ) {
- const textures = renderContext.textures;
- for ( let i = 0; i < textures.length; i ++ ) {
- const texture = textures[ i ];
- if ( texture.generateMipmaps === true ) {
- this.textureUtils.generateMipmaps( texture );
- }
- }
- }
- }
- isOccluded( renderContext, object ) {
- const renderContextData = this.get( renderContext );
- return renderContextData.occluded && renderContextData.occluded.has( object );
- }
- async resolveOccludedAsync( renderContext ) {
- const renderContextData = this.get( renderContext );
- // handle occlusion query results
- const { currentOcclusionQueryBuffer, currentOcclusionQueryObjects } = renderContextData;
- if ( currentOcclusionQueryBuffer && currentOcclusionQueryObjects ) {
- const occluded = new WeakSet();
- renderContextData.currentOcclusionQueryObjects = null;
- renderContextData.currentOcclusionQueryBuffer = null;
- await currentOcclusionQueryBuffer.mapAsync( GPUMapMode.READ );
- const buffer = currentOcclusionQueryBuffer.getMappedRange();
- const results = new BigUint64Array( buffer );
- for ( let i = 0; i < currentOcclusionQueryObjects.length; i ++ ) {
- if ( results[ i ] !== 0n ) {
- occluded.add( currentOcclusionQueryObjects[ i ] );
- }
- }
- currentOcclusionQueryBuffer.destroy();
- renderContextData.occluded = occluded;
- }
- }
- updateViewport( renderContext ) {
- const { currentPass } = this.get( renderContext );
- const { x, y, width, height, minDepth, maxDepth } = renderContext.viewportValue;
- currentPass.setViewport( x, renderContext.height - height - y, width, height, minDepth, maxDepth );
- }
- clear( color, depth, stencil, renderTargetData = null ) {
- const device = this.device;
- const renderer = this.renderer;
- let colorAttachments = [];
- let depthStencilAttachment;
- let clearValue;
- let supportsDepth;
- let supportsStencil;
- if ( color ) {
- const clearColor = this.getClearColor();
- clearValue = { r: clearColor.r, g: clearColor.g, b: clearColor.b, a: clearColor.a };
- }
- if ( renderTargetData === null ) {
- supportsDepth = renderer.depth;
- supportsStencil = renderer.stencil;
- const descriptor = this._getDefaultRenderPassDescriptor();
- if ( color ) {
- colorAttachments = descriptor.colorAttachments;
- const colorAttachment = colorAttachments[ 0 ];
- colorAttachment.clearValue = clearValue;
- colorAttachment.loadOp = GPULoadOp.Clear;
- colorAttachment.storeOp = GPUStoreOp.Store;
- }
- if ( supportsDepth || supportsStencil ) {
- depthStencilAttachment = descriptor.depthStencilAttachment;
- }
- } else {
- supportsDepth = renderTargetData.depth;
- supportsStencil = renderTargetData.stencil;
- if ( color ) {
- for ( const texture of renderTargetData.textures ) {
- const textureData = this.get( texture );
- const textureView = textureData.texture.createView();
- let view, resolveTarget;
- if ( textureData.msaaTexture !== undefined ) {
- view = textureData.msaaTexture.createView();
- resolveTarget = textureView;
- } else {
- view = textureView;
- resolveTarget = undefined;
- }
- colorAttachments.push( {
- view,
- resolveTarget,
- clearValue,
- loadOp: GPULoadOp.Clear,
- storeOp: GPUStoreOp.Store
- } );
- }
- }
- if ( supportsDepth || supportsStencil ) {
- const depthTextureData = this.get( renderTargetData.depthTexture );
- depthStencilAttachment = {
- view: depthTextureData.texture.createView()
- };
- }
- }
- //
- if ( supportsDepth ) {
- if ( depth ) {
- depthStencilAttachment.depthLoadOp = GPULoadOp.Clear;
- depthStencilAttachment.depthClearValue = renderer.getClearDepth();
- depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
- } else {
- depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
- depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
- }
- }
- //
- if ( supportsStencil ) {
- if ( stencil ) {
- depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear;
- depthStencilAttachment.stencilClearValue = renderer.getClearStencil();
- depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
- } else {
- depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
- depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
- }
- }
- //
- const encoder = device.createCommandEncoder( {} );
- const currentPass = encoder.beginRenderPass( {
- colorAttachments,
- depthStencilAttachment
- } );
- currentPass.end();
- device.queue.submit( [ encoder.finish() ] );
- }
- // compute
- beginCompute( computeGroup ) {
- const groupGPU = this.get( computeGroup );
- const descriptor = {};
- this.initTimestampQuery( computeGroup, descriptor );
- groupGPU.cmdEncoderGPU = this.device.createCommandEncoder();
- groupGPU.passEncoderGPU = groupGPU.cmdEncoderGPU.beginComputePass( descriptor );
- }
- compute( computeGroup, computeNode, bindings, pipeline ) {
- const { passEncoderGPU } = this.get( computeGroup );
- // pipeline
- const pipelineGPU = this.get( pipeline ).pipeline;
- passEncoderGPU.setPipeline( pipelineGPU );
- // bind groups
- for ( let i = 0, l = bindings.length; i < l; i ++ ) {
- const bindGroup = bindings[ i ];
- const bindingsData = this.get( bindGroup );
- passEncoderGPU.setBindGroup( i, bindingsData.group );
- }
- passEncoderGPU.dispatchWorkgroups( computeNode.dispatchCount );
- }
- finishCompute( computeGroup ) {
- const groupData = this.get( computeGroup );
- groupData.passEncoderGPU.end();
- this.prepareTimestampBuffer( computeGroup, groupData.cmdEncoderGPU );
- this.device.queue.submit( [ groupData.cmdEncoderGPU.finish() ] );
- }
- // render object
- draw( renderObject, info ) {
- const { object, geometry, context, pipeline } = renderObject;
- const bindings = renderObject.getBindings();
- const contextData = this.get( context );
- const pipelineGPU = this.get( pipeline ).pipeline;
- const currentSets = contextData.currentSets;
- const renderObjectData = this.get( renderObject );
- const { bundleEncoder, renderBundle, lastPipelineGPU } = renderObjectData;
- const renderContextData = this.get( context );
- if ( renderContextData.registerBundlesPhase === true && bundleEncoder !== undefined && lastPipelineGPU === pipelineGPU ) {
- renderContextData.renderBundles.push( renderBundle );
- return;
- }
- const passEncoderGPU = this.renderer._currentRenderBundle ? this.createBundleEncoder( context, renderObject ) : contextData.currentPass;
- // pipeline
- if ( currentSets.pipeline !== pipelineGPU ) {
- passEncoderGPU.setPipeline( pipelineGPU );
- currentSets.pipeline = pipelineGPU;
- }
- // bind groups
- for ( let i = 0, l = bindings.length; i < l; i ++ ) {
- const bindGroup = bindings[ i ];
- const bindingsData = this.get( bindGroup );
- passEncoderGPU.setBindGroup( i, bindingsData.group );
- }
- // attributes
- const index = renderObject.getIndex();
- const hasIndex = ( index !== null );
- // index
- if ( hasIndex === true ) {
- if ( currentSets.index !== index ) {
- const buffer = this.get( index ).buffer;
- const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
- passEncoderGPU.setIndexBuffer( buffer, indexFormat );
- currentSets.index = index;
- }
- }
- // vertex buffers
- const vertexBuffers = renderObject.getVertexBuffers();
- for ( let i = 0, l = vertexBuffers.length; i < l; i ++ ) {
- const vertexBuffer = vertexBuffers[ i ];
- if ( currentSets.attributes[ i ] !== vertexBuffer ) {
- const buffer = this.get( vertexBuffer ).buffer;
- passEncoderGPU.setVertexBuffer( i, buffer );
- currentSets.attributes[ i ] = vertexBuffer;
- }
- }
- // occlusion queries - handle multiple consecutive draw calls for an object
- if ( contextData.occlusionQuerySet !== undefined ) {
- const lastObject = contextData.lastOcclusionObject;
- if ( lastObject !== object ) {
- if ( lastObject !== null && lastObject.occlusionTest === true ) {
- passEncoderGPU.endOcclusionQuery();
- contextData.occlusionQueryIndex ++;
- }
- if ( object.occlusionTest === true ) {
- passEncoderGPU.beginOcclusionQuery( contextData.occlusionQueryIndex );
- contextData.occlusionQueryObjects[ contextData.occlusionQueryIndex ] = object;
- }
- contextData.lastOcclusionObject = object;
- }
- }
- // draw
- const drawRange = renderObject.drawRange;
- const firstVertex = drawRange.start;
- const instanceCount = this.getInstanceCount( renderObject );
- if ( instanceCount === 0 ) return;
- if ( object.isBatchedMesh === true ) {
- const starts = object._multiDrawStarts;
- const counts = object._multiDrawCounts;
- const drawCount = object._multiDrawCount;
- const bytesPerElement = index.bytesPerElement || 1;
- for ( let i = 0; i < drawCount; i ++ ) {
- passEncoderGPU.drawIndexed( counts[ i ] / bytesPerElement, 1, starts[ i ] / 4, 0, i );
- }
- } else if ( hasIndex === true ) {
- const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
- passEncoderGPU.drawIndexed( indexCount, instanceCount, firstVertex, 0, 0 );
- info.update( object, indexCount, instanceCount );
- } else {
- const positionAttribute = geometry.attributes.position;
- const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
- passEncoderGPU.draw( vertexCount, instanceCount, firstVertex, 0 );
- info.update( object, vertexCount, instanceCount );
- }
-
- if ( this.renderer._currentRenderBundle ) {
- const renderBundle = passEncoderGPU.finish();
- renderObjectData.lastPipelineGPU = pipelineGPU;
- renderObjectData.renderBundle = renderBundle;
- renderObjectData.bundleEncoder = passEncoderGPU;
- }
- }
- // cache key
- needsRenderUpdate( renderObject ) {
- const data = this.get( renderObject );
- const { object, material } = renderObject;
- const utils = this.utils;
- const sampleCount = utils.getSampleCount( renderObject.context );
- const colorSpace = utils.getCurrentColorSpace( renderObject.context );
- const colorFormat = utils.getCurrentColorFormat( renderObject.context );
- const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderObject.context );
- const primitiveTopology = utils.getPrimitiveTopology( object, material );
- let needsUpdate = false;
- if ( data.material !== material || data.materialVersion !== material.version ||
- data.transparent !== material.transparent || data.blending !== material.blending || data.premultipliedAlpha !== material.premultipliedAlpha ||
- data.blendSrc !== material.blendSrc || data.blendDst !== material.blendDst || data.blendEquation !== material.blendEquation ||
- data.blendSrcAlpha !== material.blendSrcAlpha || data.blendDstAlpha !== material.blendDstAlpha || data.blendEquationAlpha !== material.blendEquationAlpha ||
- data.colorWrite !== material.colorWrite || data.depthWrite !== material.depthWrite || data.depthTest !== material.depthTest || data.depthFunc !== material.depthFunc ||
- data.stencilWrite !== material.stencilWrite || data.stencilFunc !== material.stencilFunc ||
- data.stencilFail !== material.stencilFail || data.stencilZFail !== material.stencilZFail || data.stencilZPass !== material.stencilZPass ||
- data.stencilFuncMask !== material.stencilFuncMask || data.stencilWriteMask !== material.stencilWriteMask ||
- data.side !== material.side || data.alphaToCoverage !== material.alphaToCoverage ||
- data.sampleCount !== sampleCount || data.colorSpace !== colorSpace ||
- data.colorFormat !== colorFormat || data.depthStencilFormat !== depthStencilFormat ||
- data.primitiveTopology !== primitiveTopology ||
- data.clippingContextVersion !== renderObject.clippingContextVersion
- ) {
- data.material = material; data.materialVersion = material.version;
- data.transparent = material.transparent; data.blending = material.blending; data.premultipliedAlpha = material.premultipliedAlpha;
- data.blendSrc = material.blendSrc; data.blendDst = material.blendDst; data.blendEquation = material.blendEquation;
- data.blendSrcAlpha = material.blendSrcAlpha; data.blendDstAlpha = material.blendDstAlpha; data.blendEquationAlpha = material.blendEquationAlpha;
- data.colorWrite = material.colorWrite;
- data.depthWrite = material.depthWrite; data.depthTest = material.depthTest; data.depthFunc = material.depthFunc;
- data.stencilWrite = material.stencilWrite; data.stencilFunc = material.stencilFunc;
- data.stencilFail = material.stencilFail; data.stencilZFail = material.stencilZFail; data.stencilZPass = material.stencilZPass;
- data.stencilFuncMask = material.stencilFuncMask; data.stencilWriteMask = material.stencilWriteMask;
- data.side = material.side; data.alphaToCoverage = material.alphaToCoverage;
- data.sampleCount = sampleCount;
- data.colorSpace = colorSpace;
- data.colorFormat = colorFormat;
- data.depthStencilFormat = depthStencilFormat;
- data.primitiveTopology = primitiveTopology;
- data.clippingContextVersion = renderObject.clippingContextVersion;
- needsUpdate = true;
- }
- return needsUpdate;
- }
- getRenderCacheKey( renderObject ) {
- const { object, material } = renderObject;
- const utils = this.utils;
- const renderContext = renderObject.context;
- return [
- material.transparent, material.blending, material.premultipliedAlpha,
- material.blendSrc, material.blendDst, material.blendEquation,
- material.blendSrcAlpha, material.blendDstAlpha, material.blendEquationAlpha,
- material.colorWrite,
- material.depthWrite, material.depthTest, material.depthFunc,
- material.stencilWrite, material.stencilFunc,
- material.stencilFail, material.stencilZFail, material.stencilZPass,
- material.stencilFuncMask, material.stencilWriteMask,
- material.side,
- utils.getSampleCount( renderContext ),
- utils.getCurrentColorSpace( renderContext ), utils.getCurrentColorFormat( renderContext ), utils.getCurrentDepthStencilFormat( renderContext ),
- utils.getPrimitiveTopology( object, material ),
- renderObject.clippingContextVersion
- ].join();
- }
- // textures
- createSampler( texture ) {
- this.textureUtils.createSampler( texture );
- }
- destroySampler( texture ) {
- this.textureUtils.destroySampler( texture );
- }
- createDefaultTexture( texture ) {
- this.textureUtils.createDefaultTexture( texture );
- }
- createTexture( texture, options ) {
- this.textureUtils.createTexture( texture, options );
- }
- updateTexture( texture, options ) {
- this.textureUtils.updateTexture( texture, options );
- }
- generateMipmaps( texture ) {
- this.textureUtils.generateMipmaps( texture );
- }
- destroyTexture( texture ) {
- this.textureUtils.destroyTexture( texture );
- }
- copyTextureToBuffer( texture, x, y, width, height ) {
- return this.textureUtils.copyTextureToBuffer( texture, x, y, width, height );
- }
- initTimestampQuery( renderContext, descriptor ) {
- if ( ! this.hasFeature( GPUFeatureName.TimestampQuery ) || ! this.trackTimestamp ) return;
- const renderContextData = this.get( renderContext );
- if ( ! renderContextData.timeStampQuerySet ) {
- // Create a GPUQuerySet which holds 2 timestamp query results: one for the
- // beginning and one for the end of compute pass execution.
- const timeStampQuerySet = this.device.createQuerySet( { type: 'timestamp', count: 2 } );
- const timestampWrites = {
- querySet: timeStampQuerySet,
- beginningOfPassWriteIndex: 0, // Write timestamp in index 0 when pass begins.
- endOfPassWriteIndex: 1, // Write timestamp in index 1 when pass ends.
- };
- Object.assign( descriptor, {
- timestampWrites,
- } );
- renderContextData.timeStampQuerySet = timeStampQuerySet;
- }
- }
- // timestamp utils
- prepareTimestampBuffer( renderContext, encoder ) {
- if ( ! this.hasFeature( GPUFeatureName.TimestampQuery ) || ! this.trackTimestamp ) return;
- const renderContextData = this.get( renderContext );
- const size = 2 * BigInt64Array.BYTES_PER_ELEMENT;
- if ( renderContextData.currentTimestampQueryBuffers === undefined ) {
- renderContextData.currentTimestampQueryBuffers = {
- resolveBuffer: this.device.createBuffer({
- label: 'timestamp resolve buffer',
- size: size,
- usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
- }),
- resultBuffer: this.device.createBuffer({
- label: 'timestamp result buffer',
- size: size,
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
- }),
- isMappingPending: false,
- }
- }
- const { resolveBuffer, resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers
- if ( isMappingPending === true ) return;
- encoder.resolveQuerySet( renderContextData.timeStampQuerySet, 0, 2, resolveBuffer, 0 );
- encoder.copyBufferToBuffer( resolveBuffer, 0, resultBuffer, 0, size );
- }
- async resolveTimestampAsync( renderContext, type = 'render' ) {
- if ( ! this.hasFeature( GPUFeatureName.TimestampQuery ) || ! this.trackTimestamp ) return;
-
- const renderContextData = this.get(renderContext);
- if ( renderContextData.currentTimestampQueryBuffers === undefined ) return;
- const { resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers;
- if ( isMappingPending === true ) return;
- renderContextData.currentTimestampQueryBuffers.isMappingPending = true;
- resultBuffer.mapAsync( GPUMapMode.READ ).then( () => {
- const times = new BigUint64Array( resultBuffer.getMappedRange() );
- const duration = Number( times[ 1 ] - times[ 0 ] ) / 1000000;
- this.renderer.info.updateTimestamp( type, duration );
- resultBuffer.unmap();
- renderContextData.currentTimestampQueryBuffers.isMappingPending = false;
- })
- }
-
- // node builder
- createNodeBuilder( object, renderer ) {
- return new WGSLNodeBuilder( object, renderer );
- }
- // program
- createProgram( program ) {
- const programGPU = this.get( program );
- programGPU.module = {
- module: this.device.createShaderModule( { code: program.code, label: program.stage } ),
- entryPoint: 'main'
- };
- }
- destroyProgram( program ) {
- this.delete( program );
- }
- // pipelines
- createRenderPipeline( renderObject, promises ) {
- this.pipelineUtils.createRenderPipeline( renderObject, promises );
- }
- createComputePipeline( computePipeline, bindings ) {
- this.pipelineUtils.createComputePipeline( computePipeline, bindings );
- }
- createBundleEncoder( renderContext, renderObject ) {
- return this.pipelineUtils.createBundleEncoder( renderContext, renderObject );
- }
- // bindings
- createBindings( bindGroup ) {
- this.bindingUtils.createBindings( bindGroup );
- }
- updateBindings( bindGroup ) {
- this.bindingUtils.createBindings( bindGroup );
- }
- updateBinding( binding ) {
- this.bindingUtils.updateBinding( binding );
- }
- // attributes
- createIndexAttribute( attribute ) {
- this.attributeUtils.createAttribute( attribute, GPUBufferUsage.INDEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST );
- }
- createAttribute( attribute ) {
- this.attributeUtils.createAttribute( attribute, GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST );
- }
- createStorageAttribute( attribute ) {
- this.attributeUtils.createAttribute( attribute, GPUBufferUsage.STORAGE | GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST );
- }
- updateAttribute( attribute ) {
- this.attributeUtils.updateAttribute( attribute );
- }
- destroyAttribute( attribute ) {
- this.attributeUtils.destroyAttribute( attribute );
- }
- // canvas
- updateSize() {
- this.colorBuffer = this.textureUtils.getColorBuffer();
- this.defaultRenderPassdescriptor = null;
- }
- // utils public
- getMaxAnisotropy() {
- return 16;
- }
- hasFeature( name ) {
- return this.device.features.has( name );
- }
- copyTextureToTexture( srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0 ) {
- let dstX = 0;
- let dstY = 0;
- if ( dstPosition !== null ) {
- dstX = dstPosition.x;
- dstY = dstPosition.y;
- }
-
- const encoder = this.device.createCommandEncoder( { label: 'copyTextureToTexture_' + srcTexture.id + '_' + dstTexture.id } );
- const sourceGPU = this.get( srcTexture ).texture;
- const destinationGPU = this.get( dstTexture ).texture;
- encoder.copyTextureToTexture(
- {
- texture: sourceGPU,
- mipLevel: level,
- origin: { x: 0, y: 0, z: 0 }
- },
- {
- texture: destinationGPU,
- mipLevel: level,
- origin: { x: dstX, y: dstY, z: 0 }
- },
- [
- srcTexture.image.width,
- srcTexture.image.height
- ]
- );
- this.device.queue.submit( [ encoder.finish() ] );
- }
- copyFramebufferToTexture( texture, renderContext ) {
- const renderContextData = this.get( renderContext );
- const { encoder, descriptor } = renderContextData;
- let sourceGPU = null;
- if ( renderContext.renderTarget ) {
- if ( texture.isDepthTexture ) {
- sourceGPU = this.get( renderContext.depthTexture ).texture;
- } else {
- sourceGPU = this.get( renderContext.textures[ 0 ] ).texture;
- }
- } else {
- if ( texture.isDepthTexture ) {
- sourceGPU = this.textureUtils.getDepthBuffer( renderContext.depth, renderContext.stencil );
- } else {
- sourceGPU = this.context.getCurrentTexture();
- }
- }
- const destinationGPU = this.get( texture ).texture;
- if ( sourceGPU.format !== destinationGPU.format ) {
- console.error( 'WebGPUBackend: copyFramebufferToTexture: Source and destination formats do not match.', sourceGPU.format, destinationGPU.format );
- return;
- }
- renderContextData.currentPass.end();
- encoder.copyTextureToTexture(
- {
- texture: sourceGPU,
- origin: { x: 0, y: 0, z: 0 }
- },
- {
- texture: destinationGPU
- },
- [
- texture.image.width,
- texture.image.height
- ]
- );
- if ( texture.generateMipmaps ) this.textureUtils.generateMipmaps( texture );
- descriptor.colorAttachments[ 0 ].loadOp = GPULoadOp.Load;
- if ( renderContext.depth ) descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
- if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
- renderContextData.currentPass = encoder.beginRenderPass( descriptor );
- renderContextData.currentSets = { attributes: {} };
- }
- }
- export default WebGPUBackend;
|