Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPURenderer: Initial Antialiasing #20281

Merged
merged 1 commit into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/jsm/renderers/webgpu/WebGPURenderPipelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { FrontSide, BackSide, DoubleSide } from '../../../../build/three.module.

class WebGPURenderPipelines {

constructor( device, glslang, bindings ) {
constructor( device, glslang, bindings, sampleCount ) {

this.device = device;
this.glslang = glslang;
this.bindings = bindings;
this.sampleCount = sampleCount;

this.pipelines = new WeakMap();
this.shaderAttributes = new WeakMap();
Expand Down Expand Up @@ -161,7 +162,8 @@ class WebGPURenderPipelines {
vertexState: {
indexFormat: indexFormat,
vertexBuffers: vertexBuffers
}
},
sampleCount: this.sampleCount
} );

this.pipelines.set( object, pipeline );
Expand Down
73 changes: 67 additions & 6 deletions examples/jsm/renderers/webgpu/WebGPURenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class WebGPURenderer {
// public

this.domElement = ( parameters.canvas !== undefined ) ? parameters.canvas : document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
this.parameters = parameters;
this.parameters = Object.assign( {}, parameters );
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

this.autoClear = true;
this.autoClearColor = true;
Expand All @@ -45,6 +45,7 @@ class WebGPURenderer {
this._device = null;
this._context = null;
this._swapChain = null;
this._colorBuffer = null;
this._depthBuffer = null;

this._info = null;
Expand All @@ -71,6 +72,20 @@ class WebGPURenderer {

this._renderTarget = null;

// ensure some parameters

this.parameters.antialias = this.parameters.antialias === true;

if ( this.parameters.antialias ) {

this.parameters.sampleCount = this.parameters.sampleCount || 4;

} else {

this.parameters.sampleCount = 1;

}

}

init() {
Expand Down Expand Up @@ -112,17 +127,37 @@ class WebGPURenderer {

if ( renderTarget !== null ) {

// @TODO: Support RenderTarget with antialiasing.

if ( this.parameters.antialias ) {

console.error( 'WebGPURenderer: RenderTarget with antialiasing is not supported yet.' );
return;

}

const renderTargetProperties = this._properties.get( renderTarget );

colorAttachment.attachment = renderTargetProperties.colorTextureGPU.createView();
depthStencilAttachment.attachment = renderTargetProperties.depthTextureGPU.createView();


} else {

colorAttachment.attachment = this._swapChain.getCurrentTexture().createView();
depthStencilAttachment.attachment = this._depthBuffer.createView();
if ( this.parameters.antialias ) {

colorAttachment.attachment = this._colorBuffer.createView();
colorAttachment.resolveTarget = this._swapChain.getCurrentTexture().createView();
colorAttachment.storeOp = GPUStoreOp.Clear;

} else {

colorAttachment.attachment = this._swapChain.getCurrentTexture().createView();
colorAttachment.resolveTarget = undefined;
colorAttachment.storeOp = undefined;

}

depthStencilAttachment.attachment = this._depthBuffer.createView();

}

Expand Down Expand Up @@ -217,6 +252,7 @@ class WebGPURenderer {
this.domElement.width = Math.floor( width * pixelRatio );
this.domElement.height = Math.floor( height * pixelRatio );

this._setupColorBuffer();
this._setupDepthBuffer();

}
Expand All @@ -236,6 +272,7 @@ class WebGPURenderer {

}

this._setupColorBuffer();
this._setupDepthBuffer();

}
Expand Down Expand Up @@ -624,6 +661,29 @@ class WebGPURenderer {

}

_setupColorBuffer() {

const device = this._device;

if ( device ) {

if ( this._colorBuffer ) this._colorBuffer.destroy();

this._colorBuffer = this._device.createTexture({
size: {
width: this._width * this._pixelRatio,
height: this._height * this._pixelRatio,
depth: 1
},
sampleCount: this.parameters.sampleCount,
format: GPUTextureFormat.BRGA8Unorm,
usage: GPUTextureUsage.OUTPUT_ATTACHMENT
});

}

}

_setupDepthBuffer() {

const device = this._device;
Expand All @@ -638,14 +698,14 @@ class WebGPURenderer {
height: this._height * this._pixelRatio,
depth: 1
},
sampleCount: this.parameters.sampleCount,
format: GPUTextureFormat.Depth24PlusStencil8,
usage: GPUTextureUsage.OUTPUT_ATTACHMENT
} );

}

}

}

async function initWebGPU( scope ) {
Expand Down Expand Up @@ -687,7 +747,7 @@ async function initWebGPU( scope ) {
scope._textures = new WebGPUTextures( device, scope._properties, scope._info );
scope._bindings = new WebGPUBindings( device, scope._info, scope._properties, scope._textures );
scope._objects = new WebGPUObjects( scope._geometries, scope._info );
scope._renderPipelines = new WebGPURenderPipelines( device, compiler, scope._bindings );
scope._renderPipelines = new WebGPURenderPipelines( device, compiler, scope._bindings, scope.parameters.sampleCount );
scope._renderLists = new WebGPURenderLists();
scope._background = new WebGPUBackground( scope );

Expand All @@ -704,6 +764,7 @@ async function initWebGPU( scope ) {
}
};

scope._setupColorBuffer();
scope._setupDepthBuffer();

}
Expand Down