Skip to content

Commit ae1269c

Browse files
authored
WebGPURenderer: Introduce Quad cache per RendeTarget texture (#33228)
1 parent 1816e94 commit ae1269c

2 files changed

Lines changed: 46 additions & 26 deletions

File tree

src/renderers/common/Renderer.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,14 @@ class Renderer {
418418
this._background = null;
419419

420420
/**
421+
* Cache for the fullscreen quad.
421422
* This fullscreen quad is used for internal render passes
422423
* like the tone mapping and color space output pass.
423424
*
424425
* @private
425-
* @type {QuadMesh}
426+
* @type {Map<Texture,QuadMesh>}
426427
*/
427-
this._quad = new QuadMesh( new NodeMaterial() );
428-
this._quad.name = 'Output Color Transform';
429-
this._quad.material.name = 'outputColorTransform';
428+
this._quadCache = new Map();
430429

431430
/**
432431
* A reference to the current render context.
@@ -1788,12 +1787,52 @@ class Renderer {
17881787
*/
17891788
_renderOutput( renderTarget ) {
17901789

1791-
const quad = this._quad;
1790+
const cacheKey = this._nodes.getOutputCacheKey();
17921791

1793-
if ( this._nodes.hasOutputChange( renderTarget.texture ) ) {
1792+
let quadData = this._quadCache.get( renderTarget.texture );
1793+
let quad;
1794+
1795+
if ( quadData === undefined ) {
1796+
1797+
quad = new QuadMesh( new NodeMaterial() );
1798+
quad.name = 'Output Color Transform';
1799+
quad.material.name = 'outputColorTransform';
17941800

17951801
quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
1796-
quad.material.needsUpdate = true;
1802+
1803+
quadData = {
1804+
quad,
1805+
cacheKey
1806+
};
1807+
1808+
this._quadCache.set( renderTarget.texture, quadData );
1809+
1810+
// dispose logic
1811+
1812+
const dispose = () => {
1813+
1814+
quad.material.dispose();
1815+
1816+
this._quadCache.delete( renderTarget.texture );
1817+
1818+
renderTarget.texture.removeEventListener( 'dispose', dispose );
1819+
1820+
};
1821+
1822+
renderTarget.texture.addEventListener( 'dispose', dispose );
1823+
1824+
} else {
1825+
1826+
quad = quadData.quad;
1827+
1828+
if ( quadData.cacheKey !== cacheKey ) {
1829+
1830+
quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
1831+
quad.material.needsUpdate = true;
1832+
1833+
quadData.cacheKey = cacheKey;
1834+
1835+
}
17971836

17981837
}
17991838

src/renderers/common/nodes/NodeManager.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { CubeUVReflectionMapping, EquirectangularReflectionMapping, Equirectangu
1111
import { hashArray } from '../../../nodes/core/NodeUtils.js';
1212
import { error } from '../../../utils.js';
1313

14-
const _outputNodeMap = new WeakMap();
1514
const _chainKeys = [];
1615
const _cacheKeyValues = [];
1716

@@ -854,21 +853,6 @@ class NodeManager extends DataMap {
854853

855854
}
856855

857-
/**
858-
* Checks if the output configuration (tone mapping and color space) for
859-
* the given target has changed.
860-
*
861-
* @param {Texture} outputTarget - The output target.
862-
* @return {boolean} Whether the output configuration has changed or not.
863-
*/
864-
hasOutputChange( outputTarget ) {
865-
866-
const cacheKey = _outputNodeMap.get( outputTarget );
867-
868-
return cacheKey !== this.getOutputCacheKey();
869-
870-
}
871-
872856
/**
873857
* Returns a node that represents the output configuration (tone mapping and
874858
* color space) for the current target.
@@ -879,14 +863,11 @@ class NodeManager extends DataMap {
879863
getOutputNode( outputTarget ) {
880864

881865
const renderer = this.renderer;
882-
const cacheKey = this.getOutputCacheKey();
883866

884867
const output = outputTarget.isArrayTexture ?
885868
texture( outputTarget, screenUV ).depth( builtin( 'gl_ViewID_OVR' ) ).renderOutput( renderer.toneMapping, renderer.currentColorSpace ) :
886869
texture( outputTarget, screenUV ).renderOutput( renderer.toneMapping, renderer.currentColorSpace );
887870

888-
_outputNodeMap.set( outputTarget, cacheKey );
889-
890871
return output;
891872

892873
}

0 commit comments

Comments
 (0)