Skip to content

Commit 62623db

Browse files
authored
TSL: Fix performance of viewportTexture() (#31591)
* fix cache and reference * fix shared frame buffer * TexturesNode: Introduce `getBase()` * Update ViewportTextureNode.js
1 parent 8722ea5 commit 62623db

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

src/nodes/accessors/TextureNode.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ class TextureNode extends UniformNode {
548548

549549
const textureNode = this.clone();
550550
textureNode.uvNode = nodeObject( uvNode );
551-
textureNode.referenceNode = this.getSelf();
551+
textureNode.referenceNode = this.getBase();
552552

553553
return nodeObject( textureNode );
554554

@@ -576,7 +576,7 @@ class TextureNode extends UniformNode {
576576

577577
const textureNode = this.clone();
578578
textureNode.biasNode = nodeObject( amountNode ).mul( maxMipLevel( textureNode ) );
579-
textureNode.referenceNode = this.getSelf();
579+
textureNode.referenceNode = this.getBase();
580580

581581
const map = textureNode.value;
582582

@@ -602,7 +602,7 @@ class TextureNode extends UniformNode {
602602

603603
const textureNode = this.clone();
604604
textureNode.levelNode = nodeObject( levelNode );
605-
textureNode.referenceNode = this.getSelf();
605+
textureNode.referenceNode = this.getBase();
606606

607607
return nodeObject( textureNode );
608608

@@ -630,12 +630,22 @@ class TextureNode extends UniformNode {
630630

631631
const textureNode = this.clone();
632632
textureNode.biasNode = nodeObject( biasNode );
633-
textureNode.referenceNode = this.getSelf();
633+
textureNode.referenceNode = this.getBase();
634634

635635
return nodeObject( textureNode );
636636

637637
}
638638

639+
/**
640+
* Returns the base texture of this node.
641+
* @return {TextureNode} The base texture node.
642+
*/
643+
getBase() {
644+
645+
return this.referenceNode ? this.referenceNode.getBase() : this.getSelf();
646+
647+
}
648+
639649
/**
640650
* Samples the texture by executing a compare operation.
641651
*
@@ -646,7 +656,7 @@ class TextureNode extends UniformNode {
646656

647657
const textureNode = this.clone();
648658
textureNode.compareNode = nodeObject( compareNode );
649-
textureNode.referenceNode = this.getSelf();
659+
textureNode.referenceNode = this.getBase();
650660

651661
return nodeObject( textureNode );
652662

@@ -663,7 +673,7 @@ class TextureNode extends UniformNode {
663673

664674
const textureNode = this.clone();
665675
textureNode.gradNode = [ nodeObject( gradNodeX ), nodeObject( gradNodeY ) ];
666-
textureNode.referenceNode = this.getSelf();
676+
textureNode.referenceNode = this.getBase();
667677

668678
return nodeObject( textureNode );
669679

@@ -679,7 +689,7 @@ class TextureNode extends UniformNode {
679689

680690
const textureNode = this.clone();
681691
textureNode.depthNode = nodeObject( depthNode );
682-
textureNode.referenceNode = this.getSelf();
692+
textureNode.referenceNode = this.getBase();
683693

684694
return nodeObject( textureNode );
685695

@@ -779,7 +789,7 @@ export const texture = ( value = EmptyTexture, uvNode = null, levelNode = null,
779789
if ( value && value.isTextureNode === true ) {
780790

781791
textureNode = nodeObject( value.clone() );
782-
textureNode.referenceNode = value.getSelf(); // Ensure the reference is set to the original node
792+
textureNode.referenceNode = value.getBase(); // Ensure the reference is set to the original node
783793

784794
if ( uvNode !== null ) textureNode.uvNode = nodeObject( uvNode );
785795
if ( levelNode !== null ) textureNode.levelNode = nodeObject( levelNode );

src/nodes/display/ViewportSharedTextureNode.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class ViewportSharedTextureNode extends ViewportTextureNode {
3939

4040
}
4141

42+
getFrameBufferTexture() {
43+
44+
return _sharedFramebuffer;
45+
46+
}
47+
4248
updateReference() {
4349

4450
return this;

src/nodes/display/ViewportTextureNode.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,43 +80,66 @@ class ViewportTextureNode extends TextureNode {
8080
this.isOutputTextureNode = true;
8181

8282
/**
83-
* The `updateBeforeType` is set to `NodeUpdateType.RENDER` since the node renders the
84-
* scene once per render in its {@link ViewportTextureNode#updateBefore} method.
83+
* The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders the
84+
* scene once per frame in its {@link ViewportTextureNode#updateBefore} method.
8585
*
8686
* @type {string}
8787
* @default 'frame'
8888
*/
89-
this.updateBeforeType = NodeUpdateType.RENDER;
89+
this.updateBeforeType = NodeUpdateType.FRAME;
9090

9191
/**
9292
* The framebuffer texture for the current renderer context.
9393
*
9494
* @type {WeakMap<RenderTarget, FramebufferTexture>}
9595
* @private
9696
*/
97-
this._textures = new WeakMap();
97+
this._cacheTextures = new WeakMap();
9898

9999
}
100100

101101
getFrameBufferTexture( reference = null ) {
102102

103-
const defaultFramebuffer = this.referenceNode ? this.referenceNode.defaultFramebuffer : this.defaultFramebuffer;
103+
let defaultFramebuffer;
104+
let cacheTextures;
105+
106+
if ( this.referenceNode ) {
107+
108+
defaultFramebuffer = this.referenceNode.defaultFramebuffer;
109+
cacheTextures = this.referenceNode._cacheTextures;
110+
111+
} else {
112+
113+
defaultFramebuffer = this.defaultFramebuffer;
114+
cacheTextures = this._cacheTextures;
115+
116+
}
104117

105118
if ( reference === null ) {
106119

107120
return defaultFramebuffer;
108121

109122
}
110123

111-
if ( this._textures.has( reference ) === false ) {
124+
if ( cacheTextures.has( reference ) === false ) {
112125

113126
const framebufferTexture = defaultFramebuffer.clone();
114127

115-
this._textures.set( reference, framebufferTexture );
128+
cacheTextures.set( reference, framebufferTexture );
116129

117130
}
118131

119-
return this._textures.get( reference );
132+
return cacheTextures.get( reference );
133+
134+
}
135+
136+
updateReference( frame ) {
137+
138+
const renderTarget = frame.renderer.getRenderTarget();
139+
140+
this.value = this.getFrameBufferTexture( renderTarget );
141+
142+
return this.value;
120143

121144
}
122145

@@ -156,8 +179,6 @@ class ViewportTextureNode extends TextureNode {
156179

157180
framebufferTexture.generateMipmaps = currentGenerateMipmaps;
158181

159-
this.value = framebufferTexture;
160-
161182
}
162183

163184
clone() {

0 commit comments

Comments
 (0)