|
@@ -7,7 +7,7 @@ import { GPUFeatureName, GPUTextureFormat, GPULoadOp, GPUStoreOp, GPUIndexFormat
|
|
import WGSLNodeBuilder from './nodes/WGSLNodeBuilder.js';
|
|
import WGSLNodeBuilder from './nodes/WGSLNodeBuilder.js';
|
|
import Backend from '../common/Backend.js';
|
|
import Backend from '../common/Backend.js';
|
|
|
|
|
|
-import { DepthTexture, DepthFormat, DepthStencilFormat, UnsignedInt248Type, UnsignedIntType, WebGPUCoordinateSystem } from 'three';
|
|
|
|
|
|
+import { DepthFormat, WebGPUCoordinateSystem } from 'three';
|
|
|
|
|
|
import WebGPUUtils from './utils/WebGPUUtils.js';
|
|
import WebGPUUtils from './utils/WebGPUUtils.js';
|
|
import WebGPUAttributeUtils from './utils/WebGPUAttributeUtils.js';
|
|
import WebGPUAttributeUtils from './utils/WebGPUAttributeUtils.js';
|
|
@@ -56,9 +56,6 @@ class WebGPUBackend extends Backend {
|
|
this.context = null;
|
|
this.context = null;
|
|
this.colorBuffer = null;
|
|
this.colorBuffer = null;
|
|
|
|
|
|
- this.defaultDepthTexture = new DepthTexture();
|
|
|
|
- this.defaultDepthTexture.name = 'depthBuffer';
|
|
|
|
-
|
|
|
|
this.utils = new WebGPUUtils( this );
|
|
this.utils = new WebGPUUtils( this );
|
|
this.attributeUtils = new WebGPUAttributeUtils( this );
|
|
this.attributeUtils = new WebGPUAttributeUtils( this );
|
|
this.bindingUtils = new WebGPUBindingUtils( this );
|
|
this.bindingUtils = new WebGPUBindingUtils( this );
|
|
@@ -117,6 +114,13 @@ class WebGPUBackend extends Backend {
|
|
this.device = device;
|
|
this.device = device;
|
|
this.context = context;
|
|
this.context = context;
|
|
|
|
|
|
|
|
+ this.context.configure( {
|
|
|
|
+ device: this.device,
|
|
|
|
+ format: GPUTextureFormat.BGRA8Unorm,
|
|
|
|
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
|
|
|
|
+ alphaMode: 'premultiplied'
|
|
|
|
+ } );
|
|
|
|
+
|
|
this.updateSize();
|
|
this.updateSize();
|
|
|
|
|
|
}
|
|
}
|
|
@@ -218,7 +222,6 @@ class WebGPUBackend extends Backend {
|
|
resolveTarget,
|
|
resolveTarget,
|
|
loadOp: GPULoadOp.Load,
|
|
loadOp: GPULoadOp.Load,
|
|
storeOp: GPUStoreOp.Store
|
|
storeOp: GPUStoreOp.Store
|
|
-
|
|
|
|
} );
|
|
} );
|
|
|
|
|
|
}
|
|
}
|
|
@@ -247,7 +250,7 @@ class WebGPUBackend extends Backend {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- depthStencilAttachment.view = this._getDepthBufferGPU( renderContext ).createView();
|
|
|
|
|
|
+ depthStencilAttachment.view = this.textureUtils.getDepthBuffer( renderContext.depth, renderContext.stencil ).createView();
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -492,75 +495,166 @@ class WebGPUBackend extends Backend {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- clear( renderContext, color, depth, stencil ) {
|
|
|
|
|
|
+ clear( color, depth, stencil, renderTargetData = null ) {
|
|
|
|
|
|
const device = this.device;
|
|
const device = this.device;
|
|
- const renderContextData = this.get( renderContext );
|
|
|
|
|
|
+ const renderer = this.renderer;
|
|
|
|
|
|
- const { descriptor } = renderContextData;
|
|
|
|
|
|
+ const colorAttachments = [];
|
|
|
|
|
|
- depth = depth && renderContext.depth;
|
|
|
|
- stencil = stencil && renderContext.stencil;
|
|
|
|
|
|
+ let depthStencilAttachment;
|
|
|
|
+ let clearValue;
|
|
|
|
|
|
- const colorAttachment = descriptor.colorAttachments[ 0 ];
|
|
|
|
- const depthStencilAttachment = descriptor.depthStencilAttachment;
|
|
|
|
|
|
+ let supportsDepth;
|
|
|
|
+ let supportsStencil;
|
|
|
|
|
|
- const antialias = this.parameters.antialias;
|
|
|
|
|
|
+ if ( color ) {
|
|
|
|
|
|
- // @TODO: Include render target in clear operation.
|
|
|
|
- if ( antialias === true ) {
|
|
|
|
|
|
+ const clearColor = this.getClearColor();
|
|
|
|
|
|
- colorAttachment.view = this.colorBuffer.createView();
|
|
|
|
- colorAttachment.resolveTarget = this.context.getCurrentTexture().createView();
|
|
|
|
|
|
+ clearValue = { r: clearColor.r, g: clearColor.g, b: clearColor.b, a: clearColor.a };
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- colorAttachment.view = this.context.getCurrentTexture().createView();
|
|
|
|
- colorAttachment.resolveTarget = undefined;
|
|
|
|
|
|
+ if ( renderTargetData === null ) {
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ supportsDepth = renderer.depth;
|
|
|
|
+ supportsStencil = renderer.stencil;
|
|
|
|
|
|
- descriptor.depthStencilAttachment.view = this._getDepthBufferGPU( renderContext ).createView();
|
|
|
|
|
|
+ depth = depth && supportsDepth;
|
|
|
|
+ stencil = stencil && supportsStencil;
|
|
|
|
|
|
- if ( color ) {
|
|
|
|
|
|
+ if ( color ) {
|
|
|
|
|
|
- colorAttachment.loadOp = GPULoadOp.Clear;
|
|
|
|
- colorAttachment.clearValue = renderContext.clearColorValue;
|
|
|
|
|
|
+ const antialias = this.parameters.antialias;
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ const colorAttachment = {};
|
|
|
|
|
|
- colorAttachment.loadOp = GPULoadOp.Load;
|
|
|
|
|
|
+ if ( antialias === true ) {
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ colorAttachment.view = this.colorBuffer.createView();
|
|
|
|
+ colorAttachment.resolveTarget = this.context.getCurrentTexture().createView();
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ colorAttachment.view = this.context.getCurrentTexture().createView();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
- if ( depth ) {
|
|
|
|
|
|
+ colorAttachment.clearValue = clearValue;
|
|
|
|
+ colorAttachment.loadOp = GPULoadOp.Clear;
|
|
|
|
+ colorAttachment.storeOp = GPUStoreOp.Store;
|
|
|
|
|
|
- depthStencilAttachment.depthLoadOp = GPULoadOp.Clear;
|
|
|
|
- depthStencilAttachment.depthClearValue = renderContext.clearDepthValue;
|
|
|
|
|
|
+ colorAttachments.push( colorAttachment );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ( depth || stencil ) {
|
|
|
|
+
|
|
|
|
+ depthStencilAttachment = {
|
|
|
|
+ view: this.textureUtils.getDepthBuffer( renderer.depth, renderer.stencil ).createView()
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
} else {
|
|
} else {
|
|
|
|
|
|
- depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
|
|
|
|
|
|
+ supportsDepth = renderTargetData.depth;
|
|
|
|
+ supportsStencil = renderTargetData.stencil;
|
|
|
|
+
|
|
|
|
+ depth = depth && supportsDepth;
|
|
|
|
+ stencil = stencil && supportsStencil;
|
|
|
|
+
|
|
|
|
+ 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 ( depth || stencil ) {
|
|
|
|
+
|
|
|
|
+ const depthTextureData = this.get( renderTargetData.depthTexture );
|
|
|
|
+
|
|
|
|
+ depthStencilAttachment = {
|
|
|
|
+ view: depthTextureData.texture.createView()
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- if ( stencil ) {
|
|
|
|
|
|
+ //
|
|
|
|
+
|
|
|
|
+ if ( depthStencilAttachment !== undefined ) {
|
|
|
|
|
|
- depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear;
|
|
|
|
- depthStencilAttachment.stencilClearValue = renderContext.clearStencilValue;
|
|
|
|
|
|
+ if ( depth ) {
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ depthStencilAttachment.depthLoadOp = GPULoadOp.Clear;
|
|
|
|
+ depthStencilAttachment.depthClearValue = renderer.getClearDepth();
|
|
|
|
+ depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
|
|
|
|
+
|
|
|
|
+ } else if ( supportsDepth ) {
|
|
|
|
+
|
|
|
|
+ depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
|
|
|
|
+ depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+
|
|
|
|
+ if ( stencil ) {
|
|
|
|
+
|
|
|
|
+ depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear;
|
|
|
|
+ depthStencilAttachment.stencilClearValue = renderer.getClearStencil();
|
|
|
|
+ depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
|
|
|
|
+
|
|
|
|
+ } else if ( supportsStencil ) {
|
|
|
|
|
|
- depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
|
|
|
|
|
|
+ depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
|
|
|
|
+ depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- renderContextData.encoder = device.createCommandEncoder( {} );
|
|
|
|
- renderContextData.currentPass = renderContextData.encoder.beginRenderPass( descriptor );
|
|
|
|
|
|
+ //
|
|
|
|
|
|
- renderContextData.currentPass.end();
|
|
|
|
|
|
+ const encoder = device.createCommandEncoder( {} );
|
|
|
|
+ const currentPass = encoder.beginRenderPass( {
|
|
|
|
+ colorAttachments,
|
|
|
|
+ depthStencilAttachment
|
|
|
|
+ } );
|
|
|
|
+
|
|
|
|
+ currentPass.end();
|
|
|
|
|
|
- device.queue.submit( [ renderContextData.encoder.finish() ] );
|
|
|
|
|
|
+ device.queue.submit( [ encoder.finish() ] );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -956,9 +1050,8 @@ class WebGPUBackend extends Backend {
|
|
|
|
|
|
updateSize() {
|
|
updateSize() {
|
|
|
|
|
|
- this._configureContext();
|
|
|
|
- this._setupColorBuffer();
|
|
|
|
-
|
|
|
|
|
|
+ this.colorBuffer = this.textureUtils.getColorBuffer();
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
// utils public
|
|
// utils public
|
|
@@ -997,7 +1090,7 @@ class WebGPUBackend extends Backend {
|
|
|
|
|
|
} else if ( texture.isDepthTexture ) {
|
|
} else if ( texture.isDepthTexture ) {
|
|
|
|
|
|
- sourceGPU = this._getDepthBufferGPU( renderContext );
|
|
|
|
|
|
+ sourceGPU = this.textureUtils.getDepthBuffer( renderContext.depth, renderContext.stencil );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1030,85 +1123,6 @@ class WebGPUBackend extends Backend {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- // utils
|
|
|
|
-
|
|
|
|
- _getDepthBufferGPU( renderContext ) {
|
|
|
|
-
|
|
|
|
- const { width, height } = this.getDrawingBufferSize();
|
|
|
|
-
|
|
|
|
- const depthTexture = this.defaultDepthTexture;
|
|
|
|
- const depthTextureGPU = this.get( depthTexture ).texture;
|
|
|
|
-
|
|
|
|
- let format, type;
|
|
|
|
-
|
|
|
|
- if ( renderContext.stencil ) {
|
|
|
|
-
|
|
|
|
- format = DepthStencilFormat;
|
|
|
|
- type = UnsignedInt248Type;
|
|
|
|
-
|
|
|
|
- } else if ( renderContext.depth ) {
|
|
|
|
-
|
|
|
|
- format = DepthFormat;
|
|
|
|
- type = UnsignedIntType;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if ( depthTextureGPU !== undefined ) {
|
|
|
|
-
|
|
|
|
- if ( depthTexture.image.width === width && depthTexture.image.height === height && depthTexture.format === format && depthTexture.type === type ) {
|
|
|
|
-
|
|
|
|
- return depthTextureGPU;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- this.textureUtils.destroyTexture( depthTexture );
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- depthTexture.name = 'depthBuffer';
|
|
|
|
- depthTexture.format = format;
|
|
|
|
- depthTexture.type = type;
|
|
|
|
- depthTexture.image.width = width;
|
|
|
|
- depthTexture.image.height = height;
|
|
|
|
-
|
|
|
|
- this.textureUtils.createTexture( depthTexture, { sampleCount: this.parameters.sampleCount, width, height } );
|
|
|
|
-
|
|
|
|
- return this.get( depthTexture ).texture;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- _configureContext() {
|
|
|
|
-
|
|
|
|
- this.context.configure( {
|
|
|
|
- device: this.device,
|
|
|
|
- format: GPUTextureFormat.BGRA8Unorm,
|
|
|
|
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
|
|
|
|
- alphaMode: 'premultiplied'
|
|
|
|
- } );
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- _setupColorBuffer() {
|
|
|
|
-
|
|
|
|
- if ( this.colorBuffer ) this.colorBuffer.destroy();
|
|
|
|
-
|
|
|
|
- const { width, height } = this.getDrawingBufferSize();
|
|
|
|
- //const format = navigator.gpu.getPreferredCanvasFormat(); // @TODO: Move to WebGPUUtils
|
|
|
|
-
|
|
|
|
- this.colorBuffer = this.device.createTexture( {
|
|
|
|
- label: 'colorBuffer',
|
|
|
|
- size: {
|
|
|
|
- width: width,
|
|
|
|
- height: height,
|
|
|
|
- depthOrArrayLayers: 1
|
|
|
|
- },
|
|
|
|
- sampleCount: this.parameters.sampleCount,
|
|
|
|
- format: GPUTextureFormat.BGRA8Unorm,
|
|
|
|
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC
|
|
|
|
- } );
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
export default WebGPUBackend;
|
|
export default WebGPUBackend;
|