Skip to content

Commit 8319d7c

Browse files
authored
Renderer: Introduce colorBufferType. (#30425)
1 parent 9e17a9a commit 8319d7c

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

examples/webgpu_xr_cubes.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696

9797
raycaster = new THREE.Raycaster();
9898

99-
renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: true } );
99+
renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: true, colorBufferType: THREE.UnsignedByteType } );
100100
renderer.setPixelRatio( window.devicePixelRatio );
101101
renderer.setSize( window.innerWidth, window.innerHeight );
102102
renderer.setAnimationLoop( animate );

src/nodes/display/PassNode.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,15 @@ class PassNode extends TempNode {
546546

547547
this.renderTarget.samples = this.options.samples === undefined ? renderer.samples : this.options.samples;
548548

549-
// Disable MSAA for WebGL backend for now
549+
// TODO: Disable MSAA for WebGL backend for now
550550
if ( renderer.backend.isWebGLBackend === true ) {
551551

552552
this.renderTarget.samples = 0;
553553

554554
}
555555

556+
this.renderTarget.texture.type = renderer.getColorBufferType();
557+
556558
return this.scope === PassNode.COLOR ? this.getTextureNode() : this.getLinearDepthNode();
557559

558560
}

src/renderers/common/Renderer.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class Renderer {
5555
* @param {Number} [parameters.samples=0] - When `antialias` is `true`, `4` samples are used by default. This parameter can set to any other integer value than 0
5656
* to overwrite the default.
5757
* @param {Function?} [parameters.getFallback=null] - This callback function can be used to provide a fallback backend, if the primary backend can't be targeted.
58+
* @param {Number} [parameters.colorBufferType=HalfFloatType] - Defines the type of color buffers. The default `HalfFloatType` is recommend for best
59+
* quality. To save memory and bandwidth, `UnsignedByteType` might be used. This will reduce rendering quality though.
5860
*/
5961
constructor( backend, parameters = {} ) {
6062

@@ -76,7 +78,8 @@ class Renderer {
7678
stencil = false,
7779
antialias = false,
7880
samples = 0,
79-
getFallback = null
81+
getFallback = null,
82+
colorBufferType = HalfFloatType
8083
} = parameters;
8184

8285
/**
@@ -577,6 +580,17 @@ class Renderer {
577580
*/
578581
this.onDeviceLost = this._onDeviceLost;
579582

583+
/**
584+
* Defines the type of color buffers. The default `HalfFloatType` is recommend for
585+
* best quality. To save memory and bandwidth, `UnsignedByteType` might be used.
586+
* This will reduce rendering quality though.
587+
*
588+
* @private
589+
* @type {Number}
590+
* @default HalfFloatType
591+
*/
592+
this._colorBufferType = colorBufferType;
593+
580594
/**
581595
* Whether the renderer has been initialized or not.
582596
*
@@ -974,6 +988,17 @@ class Renderer {
974988

975989
}
976990

991+
/**
992+
* Returns the color buffer type.
993+
*
994+
* @return {Number} The color buffer type.
995+
*/
996+
getColorBufferType() {
997+
998+
return this._colorBufferType;
999+
1000+
}
1001+
9771002
/**
9781003
* Default implementation of the device lost callback.
9791004
*
@@ -1128,7 +1153,7 @@ class Renderer {
11281153
frameBufferTarget = new RenderTarget( width, height, {
11291154
depthBuffer: depth,
11301155
stencilBuffer: stencil,
1131-
type: HalfFloatType, // FloatType
1156+
type: this._colorBufferType,
11321157
format: RGBAFormat,
11331158
colorSpace: LinearSRGBColorSpace,
11341159
generateMipmaps: false,
@@ -2026,6 +2051,8 @@ class Renderer {
20262051
this._renderContexts.dispose();
20272052
this._textures.dispose();
20282053

2054+
if ( this._frameBufferTarget !== null ) this._frameBufferTarget.dispose();
2055+
20292056
Object.values( this.backend.timestampQueryPool ).forEach( queryPool => {
20302057

20312058
if ( queryPool !== null ) queryPool.dispose();

src/renderers/webgpu/WebGPURenderer.Nodes.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import BasicNodeLibrary from './nodes/BasicNodeLibrary.js';
77
* This alternative version of {@link WebGPURenderer} only supports node materials.
88
* So classes like `MeshBasicMaterial` are not compatible.
99
*
10+
* @private
1011
* @augments module:Renderer~Renderer
1112
*/
1213
class WebGPURenderer extends Renderer {
@@ -20,10 +21,11 @@ class WebGPURenderer extends Renderer {
2021
* @param {Boolean} [parameters.depth=true] - Whether the default framebuffer should have a depth buffer or not.
2122
* @param {Boolean} [parameters.stencil=false] - Whether the default framebuffer should have a stencil buffer or not.
2223
* @param {Boolean} [parameters.antialias=false] - Whether MSAA as the default anti-aliasing should be enabled or not.
23-
* @param {Number} [parameters.samples=0] - When `antialias` is `true`, `4` samples are used by default. Set this parameter to any other integer value than 0
24-
* to overwrite the default.
25-
* @param {Boolean} [parameters.forceWebGL=false] - If set to `true`, the renderer uses it
26-
* WebGL 2 backend no matter if WebGPU is supported or not.
24+
* @param {Number} [parameters.samples=0] - When `antialias` is `true`, `4` samples are used by default. Set this parameter to any other integer value than 0 to overwrite the default.
25+
* @param {Boolean} [parameters.forceWebGL=false] - If set to `true`, the renderer uses it WebGL 2 backend no matter if WebGPU is supported or not.
26+
* @param {Number} [parameters.outputType=undefined] - Texture type for output to canvas. By default, device's preferred format is used; other formats may incur overhead.
27+
* @param {Number} [parameters.colorBufferType=HalfFloatType] - Defines the type of color buffers. The default `HalfFloatType` is recommend for best
28+
* quality. To save memory and bandwidth, `UnsignedByteType` might be used. This will reduce rendering quality though.
2729
*/
2830
constructor( parameters = {} ) {
2931

src/renderers/webgpu/WebGPURenderer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class WebGPURenderer extends Renderer {
3838
* @param {Number} [parameters.samples=0] - When `antialias` is `true`, `4` samples are used by default. Set this parameter to any other integer value than 0 to overwrite the default.
3939
* @param {Boolean} [parameters.forceWebGL=false] - If set to `true`, the renderer uses a WebGL 2 backend no matter if WebGPU is supported or not.
4040
* @param {Number} [parameters.outputType=undefined] - Texture type for output to canvas. By default, device's preferred format is used; other formats may incur overhead.
41+
* @param {Number} [parameters.colorBufferType=HalfFloatType] - Defines the type of color buffers. The default `HalfFloatType` is recommend for best
42+
* quality. To save memory and bandwidth, `UnsignedByteType` might be used. This will reduce rendering quality though.
4143
*/
4244
constructor( parameters = {} ) {
4345

0 commit comments

Comments
 (0)