Skip to content
Draft
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
4 changes: 2 additions & 2 deletions src/materials/nodes/Line2NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class Line2NodeMaterial extends NodeMaterial {

if ( ! useDash ) {

if ( useAlphaToCoverage && renderer.samples > 1 ) {
if ( useAlphaToCoverage && renderer.currentSamples > 0 ) {

const dnorm = norm.fwidth();
alpha.assign( smoothstep( dnorm.negate().add( 0.5 ), dnorm.add( 0.5 ), norm ).oneMinus() );
Expand All @@ -395,7 +395,7 @@ class Line2NodeMaterial extends NodeMaterial {

// round endcaps

if ( useAlphaToCoverage && renderer.samples > 1 ) {
if ( useAlphaToCoverage && renderer.currentSamples > 0 ) {

const a = vUv.x;
const b = vUv.y.greaterThan( 0.0 ).select( vUv.y.sub( 1.0 ), vUv.y.add( 1.0 ) );
Expand Down
2 changes: 1 addition & 1 deletion src/materials/nodes/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ class NodeMaterial extends Material {

if ( unionPlanes.length > 0 || intersectionPlanes.length > 0 ) {

const samples = builder.renderer.samples;
const samples = builder.renderer.currentSamples;

if ( this.alphaToCoverage && samples > 1 ) {

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/shapes/Shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const shapeCircle = Fn( ( [ coord = uv() ], { renderer, material } ) => {

let alpha;

if ( material.alphaToCoverage && renderer.samples > 1 ) {
if ( material.alphaToCoverage && renderer.currentSamples > 0 ) {

const dlen = float( len2.fwidth() ).toVar();

Expand Down
40 changes: 39 additions & 1 deletion src/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Renderer {
* @type {number}
* @default 0
*/
this.samples = samples || ( antialias === true ) ? 4 : 0;
this._samples = samples || ( antialias === true ) ? 4 : 0;

/**
* Whether the renderer should automatically clear the current rendering target
Expand Down Expand Up @@ -2144,6 +2144,44 @@ class Renderer {

}

/**
* Returns `true` if a framebuffer target is needed to perform tone mapping or color space conversion.
* If this is the case, the renderer allocates an internal render target for that purpose.
*
*/
get needsFrameBufferTarget() {

const useToneMapping = this.currentToneMapping !== NoToneMapping;
const useColorSpace = this.currentColorSpace !== ColorManagement.workingColorSpace;

return useToneMapping || useColorSpace;

}

get samples() {

return this._samples;

}

get currentSamples() {

let samples = this._samples;

if ( this._renderTarget !== null ) {

samples = this._renderTarget.samples;

} else if ( this.needsFrameBufferTarget ) {

samples = 0;

}

return samples;

}

/**
* The current tone mapping of the renderer. When not producing screen output,
* the tone mapping is always `NoToneMapping`.
Expand Down
1 change: 1 addition & 0 deletions src/renderers/common/Textures.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Textures extends DataMap {
depthTexture.image.width = mipWidth;
depthTexture.image.height = mipHeight;
depthTexture.image.depth = size.depth;
depthTexture.renderTarget = renderTarget;
depthTexture.isArrayTexture = renderTarget.multiview === true && size.depth > 1;

depthTextureMips[ activeMipmapLevel ] = depthTexture;
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/common/XRManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ class XRManager extends EventDispatcher {
// fallback to XRWebGLLayer

const layerInit = {
antialias: renderer.samples > 0,
antialias: renderer.currentSamples > 0,
alpha: true,
depth: renderer.depth,
stencil: renderer.stencil,
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/webgl-fallback/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class WebGLBackend extends Backend {
const parameters = this.parameters;

const contextAttributes = {
antialias: renderer.samples > 0,
antialias: renderer.currentSamples > 0,
alpha: true, // always true for performance reasons
depth: renderer.depth,
stencil: renderer.stencil
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/webgl-fallback/utils/WebGLState.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ class WebGLState {

this.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );

material.alphaToCoverage === true && this.backend.renderer.samples > 1
material.alphaToCoverage === true && this.backend.renderer.currentSamples > 1
? this.enable( gl.SAMPLE_ALPHA_TO_COVERAGE )
: this.disable( gl.SAMPLE_ALPHA_TO_COVERAGE );

Expand Down
15 changes: 3 additions & 12 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ class WebGPUBackend extends Backend {
*/
this.context = null;

/**
* A reference to the color attachment of the default framebuffer.
*
* @type {?GPUTexture}
* @default null
*/
this.colorBuffer = null;

/**
* A reference to the default render pass descriptor.
*
Expand Down Expand Up @@ -328,9 +320,9 @@ class WebGPUBackend extends Backend {

const colorAttachment = descriptor.colorAttachments[ 0 ];

if ( this.renderer.samples > 0 ) {
if ( this.renderer.currentSamples > 0 ) {

colorAttachment.view = this.colorBuffer.createView();
colorAttachment.view = this.textureUtils.getColorBuffer().createView();

} else {

Expand All @@ -344,7 +336,7 @@ class WebGPUBackend extends Backend {

const colorAttachment = descriptor.colorAttachments[ 0 ];

if ( this.renderer.samples > 0 ) {
if ( this.renderer.currentSamples > 0 ) {

colorAttachment.resolveTarget = this.context.getCurrentTexture().createView();

Expand Down Expand Up @@ -2192,7 +2184,6 @@ class WebGPUBackend extends Backend {
*/
updateSize() {

this.colorBuffer = this.textureUtils.getColorBuffer();
this.defaultRenderPassdescriptor = null;

}
Expand Down
80 changes: 59 additions & 21 deletions src/renderers/webgpu/utils/WebGPUTextureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,22 @@ class WebGPUTextureUtils {
*/
this.defaultVideoFrame = null;

/**
* Represents the color attachment of the default framebuffer.
*
* @type {?GPUTexture}
* @default null
*/
this.colorBuffer = null;

/**
* Represents the depth attachment of the default framebuffer.
*
* @type {DepthTexture}
*/
this.depthTexture = new DepthTexture();
this.depthTexture.name = 'depthBuffer';
this.frameBufferData = {
color: {
buffer: null, // TODO: Move to Texture
width: 0,
height: 0,
samples: 0
},
depth: {
texture: new DepthTexture(),
width: 0,
height: 0,
samples: 0,
depth: false,
stencil: false
}
};

}

Expand Down Expand Up @@ -359,24 +360,44 @@ class WebGPUTextureUtils {
*/
getColorBuffer() {

if ( this.colorBuffer ) this.colorBuffer.destroy();

const backend = this.backend;
const { width, height } = backend.getDrawingBufferSize();
const samples = backend.renderer.currentSamples;

this.colorBuffer = backend.device.createTexture( {
const frameBufferColor = this.frameBufferData.color;

if ( frameBufferColor.width === width && frameBufferColor.height === height && frameBufferColor.samples === samples ) {

return frameBufferColor.buffer;

}

// recreate

let colorBuffer = frameBufferColor.buffer;

if ( colorBuffer ) colorBuffer.destroy();

colorBuffer = backend.device.createTexture( {
label: 'colorBuffer',
size: {
width: width,
height: height,
depthOrArrayLayers: 1
},
sampleCount: backend.utils.getSampleCount( backend.renderer.samples ),
sampleCount: backend.utils.getSampleCount( backend.renderer.currentSamples ),
format: backend.utils.getPreferredCanvasFormat(),
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC
} );

return this.colorBuffer;
//

frameBufferColor.buffer = colorBuffer;
frameBufferColor.width = width;
frameBufferColor.height = height;
frameBufferColor.samples = samples;

return colorBuffer;

}

Expand All @@ -392,8 +413,23 @@ class WebGPUTextureUtils {

const backend = this.backend;
const { width, height } = backend.getDrawingBufferSize();
const samples = backend.renderer.currentSamples;

const frameBufferDepth = this.frameBufferData.depth;
const depthTexture = frameBufferDepth.texture;

if ( depthTexture.width === width &&
depthTexture.height === height &&
depthTexture.samples === samples &&
depthTexture.depth === depth &&
depthTexture.stencil === stencil ) {

return backend.get( depthTexture ).texture;

}

//

const depthTexture = this.depthTexture;
const depthTextureGPU = backend.get( depthTexture ).texture;

let format, type;
Expand Down Expand Up @@ -422,6 +458,8 @@ class WebGPUTextureUtils {

}

// recreate

depthTexture.name = 'depthBuffer';
depthTexture.format = format;
depthTexture.type = type;
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/webgpu/utils/WebGPUUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class WebGPUUtils {
const renderer = this.backend.renderer;
const renderTarget = renderer.getRenderTarget();

samples = renderTarget ? renderTarget.samples : renderer.samples;
samples = renderTarget ? renderTarget.samples : renderer.currentSamples;

} else if ( texture.renderTarget ) {

Expand Down Expand Up @@ -186,7 +186,7 @@ class WebGPUUtils {

}

return this.getSampleCount( this.backend.renderer.samples );
return this.getSampleCount( this.backend.renderer.currentSamples );

}

Expand Down
Loading