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: Only use HalfFloatType for the framebuffer target if necessary. #30392

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions examples/webgpu_lights_custom.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.toneMapping = THREE.LinearToneMapping;
document.body.appendChild( renderer.domElement );

// controls
Expand Down
13 changes: 12 additions & 1 deletion src/renderers/common/Backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let _color4 = null;
import Color4 from './Color4.js';
import { Vector2 } from '../../math/Vector2.js';
import { createCanvasElement, warnOnce } from '../../utils.js';
import { REVISION } from '../../constants.js';
import { HalfFloatType, REVISION } from '../../constants.js';

/**
* Most of the rendering related logic is implemented in the
Expand Down Expand Up @@ -588,6 +588,17 @@ class Backend {

}

/**
* Returns `true` if the backend requires a framebuffer target with type `HalfFloat`.
*
* @return {Boolean} Whether the backend requires a framebuffer target with type `HalfFloat` or not.
*/
needsHalfFloatFrameBufferTarget() {

return ( this.parameters.outputType === HalfFloatType );

}

/**
* Sets a dictionary for the given object into the
* internal data structure.
Expand Down
31 changes: 22 additions & 9 deletions src/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Matrix4 } from '../../math/Matrix4.js';
import { Vector2 } from '../../math/Vector2.js';
import { Vector4 } from '../../math/Vector4.js';
import { RenderTarget } from '../../core/RenderTarget.js';
import { DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoToneMapping, LinearFilter, LinearSRGBColorSpace, HalfFloatType, RGBAFormat, PCFShadowMap } from '../../constants.js';
import { DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoToneMapping, LinearFilter, LinearSRGBColorSpace, HalfFloatType, RGBAFormat, PCFShadowMap, UnsignedByteType } from '../../constants.js';

/** @module Renderer **/

Expand Down Expand Up @@ -445,13 +445,13 @@ class Renderer {
this._transparentSort = null;

/**
* The framebuffer target.
* A map that holds a framebuffer target for each render
* target type.
*
* @private
* @type {RenderTarget?}
* @default null
* @type {Map<Number,RenderTarget>}
*/
this._frameBufferTarget = null;
this._frameBufferTargetMap = new Map();

const alphaClear = this.alpha === true ? 0 : 1;

Expand Down Expand Up @@ -1121,14 +1121,19 @@ class Renderer {
const { width, height } = this.getDrawingBufferSize( _drawingBufferSize );
const { depth, stencil } = this;

let frameBufferTarget = this._frameBufferTarget;
// to improve performance we only want to use HalfFloatType if necessary
// HalfFloatType is required when a) tone mapping is used or b) the backend specifically requests it

const type = ( useToneMapping || this.backend.needsHalfFloatFrameBufferTarget() ) ? HalfFloatType : UnsignedByteType;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Side note: When using UnsignedByteType, it is in general recommended to use a sRGB storage format for the framebuffer for better storage precision (see https://stackoverflow.com/a/47098507/5250847). However, these framebuffer formats are buggy in WebGL and do not consistently work across all platforms.


if ( frameBufferTarget === null ) {
let frameBufferTarget = this._frameBufferTargetMap.get( type );

if ( frameBufferTarget === undefined ) {

frameBufferTarget = new RenderTarget( width, height, {
depthBuffer: depth,
stencilBuffer: stencil,
type: HalfFloatType, // FloatType
type: type,
format: RGBAFormat,
colorSpace: LinearSRGBColorSpace,
generateMipmaps: false,
Expand All @@ -1139,7 +1144,7 @@ class Renderer {

frameBufferTarget.isPostProcessingRenderTarget = true;

this._frameBufferTarget = frameBufferTarget;
this._frameBufferTargetMap.set( type, frameBufferTarget );

}

Expand Down Expand Up @@ -2026,6 +2031,14 @@ class Renderer {
this._renderContexts.dispose();
this._textures.dispose();

for ( const renderTarget of this._frameBufferTargetMap.values() ) {

renderTarget.dispose();

}

this._frameBufferTargetMap.clear();

Object.values( this.backend.timestampQueryPool ).forEach( queryPool => {

if ( queryPool !== null ) queryPool.dispose();
Expand Down
Loading