Skip to content

Commit

Permalink
WebGPURenderer: Only use HalfFloatType for the framebuffer target i…
Browse files Browse the repository at this point in the history
…f necessary.
  • Loading branch information
Mugen87 committed Jan 24, 2025
1 parent 0d34438 commit 2b6771e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/renderers/common/Backend.js
Original file line number Diff line number Diff line change
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 false;

}

/**
* Sets a dictionary for the given object into the
* internal data structure.
Expand Down
23 changes: 20 additions & 3 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 @@ -453,6 +453,15 @@ class Renderer {
*/
this._frameBufferTarget = null;

/**
* Used to identify the framebuffer target.
*
* @private
* @type {Number?}
* @default null
*/
this._frameBufferTargetKey = null;

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

/**
Expand Down Expand Up @@ -1123,12 +1132,19 @@ class Renderer {

let frameBufferTarget = this._frameBufferTarget;

if ( frameBufferTarget === null ) {
if ( frameBufferTarget === null || this._frameBufferTargetKey !== currentToneMapping ) {

if ( frameBufferTarget !== null ) frameBufferTarget.dispose();

// to improve performance we only want to use HalfFloatType if necessary
// HalfFloatType is required when a) tone mapping is configured or b) the backend specifically requests it

const type = ( currentToneMapping !== NoToneMapping || this.backend.needsHalfFloatFrameBufferTarget() ) ? HalfFloatType : UnsignedByteType;

frameBufferTarget = new RenderTarget( width, height, {
depthBuffer: depth,
stencilBuffer: stencil,
type: HalfFloatType, // FloatType
type: type,
format: RGBAFormat,
colorSpace: LinearSRGBColorSpace,
generateMipmaps: false,
Expand All @@ -1140,6 +1156,7 @@ class Renderer {
frameBufferTarget.isPostProcessingRenderTarget = true;

this._frameBufferTarget = frameBufferTarget;
this._frameBufferTargetKey = currentToneMapping;

}

Expand Down
13 changes: 12 additions & 1 deletion src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import WebGPUBindingUtils from './utils/WebGPUBindingUtils.js';
import WebGPUPipelineUtils from './utils/WebGPUPipelineUtils.js';
import WebGPUTextureUtils from './utils/WebGPUTextureUtils.js';

import { WebGPUCoordinateSystem } from '../../constants.js';
import { HalfFloatType, WebGPUCoordinateSystem } from '../../constants.js';
import WebGPUTimestampQueryPool from './utils/WebGPUTimestampQueryPool.js';

/**
Expand Down Expand Up @@ -279,6 +279,17 @@ class WebGPUBackend extends 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 );

}

/**
* Returns the default render pass descriptor.
*
Expand Down

0 comments on commit 2b6771e

Please sign in to comment.