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: Support MSAA for texture render targets. #26451

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/jsm/renderers/common/RenderContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RenderContext {
this.texture = null;
this.depthTexture = null;
this.activeCubeFace = 0;
this.sampleCount = 1;

}

Expand Down
2 changes: 2 additions & 0 deletions examples/jsm/renderers/common/RenderContexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class RenderContexts {

}

if ( renderTarget !== null ) renderState.sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;

return renderState;

}
Expand Down
22 changes: 17 additions & 5 deletions examples/jsm/renderers/common/Textures.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Textures extends DataMap {
updateRenderTarget( renderTarget ) {

const renderTargetData = this.get( renderTarget );
const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;

const texture = renderTarget.texture;
const size = this.getSize( texture );
Expand Down Expand Up @@ -48,8 +49,19 @@ class Textures extends DataMap {
renderTargetData.texture = texture;
renderTargetData.depthTexture = depthTexture;

this.updateTexture( texture );
this.updateTexture( depthTexture );
if ( renderTargetData.sampleCount !== sampleCount ) {

texture.needsUpdate = true;
depthTexture.needsUpdate = true;

renderTargetData.sampleCount = sampleCount;

}

const options = { sampleCount };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use { samples } name to preserve the property name used in RenderTarget.


this.updateTexture( texture, options );
this.updateTexture( depthTexture, options );

// dispose handler

Expand All @@ -74,7 +86,7 @@ class Textures extends DataMap {

}

updateTexture( texture ) {
updateTexture( texture, options = {} ) {

const textureData = this.get( texture );
if ( textureData.initialized === true && textureData.version === texture.version ) return;
Expand All @@ -96,7 +108,7 @@ class Textures extends DataMap {
if ( isRenderTarget ) {

backend.createSampler( texture );
backend.createTexture( texture );
backend.createTexture( texture, options );

} else {

Expand All @@ -120,7 +132,7 @@ class Textures extends DataMap {

if ( textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true ) {

backend.createTexture( texture );
backend.createTexture( texture, options );

textureData.isDefaultTexture = false;

Expand Down
20 changes: 15 additions & 5 deletions examples/jsm/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,25 @@
const textureData = this.get( renderContext.texture );
const depthTextureData = this.get( renderContext.depthTexture );

// @TODO: Support RenderTarget with antialiasing.

colorAttachment.view = textureData.texture.createView( {
const view = textureData.texture.createView( {
baseMipLevel: 0,
mipLevelCount: 1,
baseArrayLayer: renderContext.activeCubeFace,
dimension: GPUTextureViewDimension.TwoD
} );

if ( textureData.msaaTexture !== undefined ) {

colorAttachment.view = textureData.msaaTexture.createView();
colorAttachment.resolveTarget = view;
``
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
} else {

colorAttachment.view = view;
colorAttachment.resolveTarget = undefined;

}

depthStencilAttachment.view = depthTextureData.texture.createView();

if ( renderContext.stencil && renderContext.depthTexture.format === DepthFormat ) {
Expand Down Expand Up @@ -555,9 +565,9 @@

}

createTexture( texture ) {
createTexture( texture, options ) {

this.textureUtils.createTexture( texture );
this.textureUtils.createTexture( texture, options );

}

Expand Down
18 changes: 16 additions & 2 deletions examples/jsm/renderers/webgpu/utils/WebGPUTextureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ class WebGPUTextureUtils {
const dimension = this._getDimension( texture );
const mipLevelCount = this._getMipLevelCount( texture, width, height, needsMipmaps );
const format = texture.internalFormat || this._getFormat( texture );
//const sampleCount = texture.isRenderTargetTexture || texture.isDepthTexture ? backend.utils.getSampleCount( renderContext ) : 1;

const sampleCount = options.sampleCount !== undefined ? options.sampleCount : 1;
const primarySampleCount = texture.isRenderTargetTexture ? 1 : sampleCount;

let usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC;

Expand All @@ -122,7 +123,7 @@ class WebGPUTextureUtils {
depthOrArrayLayers: depth,
},
mipLevelCount: mipLevelCount,
sampleCount: sampleCount,
sampleCount: primarySampleCount,
dimension: dimension,
format: format,
usage: usage
Expand Down Expand Up @@ -156,6 +157,17 @@ class WebGPUTextureUtils {

}

if ( texture.isRenderTargetTexture && sampleCount > 1 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can do just primarySampleCount > 1.


const msaaTextureDescriptorGPU = Object.assign( {}, textureDescriptorGPU );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think { ...textureDescriptorGPU } looks a bit clearer that it is just a clone.


msaaTextureDescriptorGPU.label = msaaTextureDescriptorGPU.label + '-msaa';
msaaTextureDescriptorGPU.sampleCount = sampleCount;

textureData.msaaTexture = backend.device.createTexture( msaaTextureDescriptorGPU );

}

textureData.initialized = true;

textureData.needsMipmaps = needsMipmaps;
Expand All @@ -170,6 +182,8 @@ class WebGPUTextureUtils {

textureData.texture.destroy();

if ( textureData.msaaTexture !== undefined ) textureData.msaaTexture.destroy();

backend.delete( texture );

}
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/webgpu/utils/WebGPUUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class WebGPUUtils {

if ( renderContext.texture !== null ) {

return 1;
return renderContext.sampleCount;

}

Expand Down