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

TSL: Share context between RTT #28811

Merged
merged 1 commit into from
Jul 5, 2024
Merged
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
11 changes: 11 additions & 0 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ class NodeBuilder {

}

getSharedContext() {

const context = { ...this.context };

delete context.keywords;
delete context.material;

return this.context;

}

setCache( cache ) {

this.cache = cache;
Expand Down
3 changes: 2 additions & 1 deletion src/nodes/display/GaussianBlurNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ class GaussianBlurNode extends TempNode {
//

const material = this._material || ( this._material = builder.createNodeMaterial() );
material.fragmentNode = blur();
material.fragmentNode = blur().context( builder.getSharedContext() );
Copy link
Collaborator

@Mugen87 Mugen87 Jul 5, 2024

Choose a reason for hiding this comment

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

Do you mind explaining why .context( builder.getSharedContext() ) is now used in GaussianBlurNode (and not just RTTNode)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As it is a different render-pass, this shader is compiled in a different NodeBuilder and will probably not share the same context or the same settings defined in the renderer, any Node that uses the RenderToTexture approach can inherit the context defined in its essence if it is transferred with this approach.

Copy link
Collaborator

@Mugen87 Mugen87 Jul 5, 2024

Choose a reason for hiding this comment

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

So if this change would be missing in GaussianBlurNode, renderer settings could get out of sync and produce similar color errors like reported in #28781 (comment)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So if this change would be missing in GaussianBlurNode, renderer settings could get out of sync and produce similar color errors like reported in #28781 (comment)?

Yes, that's it, but only when a node uses context to obtain parameters like the renderOutput() case, in other situations it should be ok.

material.needsUpdate = true;

//

Expand Down
32 changes: 28 additions & 4 deletions src/nodes/utils/RTTNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { RenderTarget } from '../../core/RenderTarget.js';
import { Vector2 } from '../../math/Vector2.js';
import { HalfFloatType } from '../../constants.js';

const _quadMesh = new QuadMesh( new NodeMaterial() );
const _size = new Vector2();

class RTTNode extends TextureNode {
Expand All @@ -32,6 +31,9 @@ class RTTNode extends TextureNode {

this.updateMap = new WeakMap();

this._rttNode = null;
this._quadMesh = new QuadMesh( new NodeMaterial() );

this.updateBeforeType = NodeUpdateType.RENDER;

}
Expand All @@ -42,17 +44,37 @@ class RTTNode extends TextureNode {

}

setup( builder ) {

this._rttNode = this.node.context( builder.getSharedContext() );
this._quadMesh.material.needsUpdate = true;

return super.setup( builder );

}

setSize( width, height ) {

this.width = width;
this.height = height;

this.renderTarget.setSize( width, height );
const effectiveWidth = width * this.pixelRatio;
const effectiveHeight = height * this.pixelRatio;

this.renderTarget.setSize( effectiveWidth, effectiveHeight );

this.textureNeedsUpdate = true;

}

setPixelRatio( pixelRatio ) {

this.pixelRatio = pixelRatio;

this.setSize( this.width, this.height );

}

updateBefore( { renderer } ) {

if ( this.textureNeedsUpdate === false && this.autoUpdate === false ) return;
Expand All @@ -63,6 +85,8 @@ class RTTNode extends TextureNode {

if ( this.autoSize === true ) {

this.pixelRatio = renderer.getPixelRatio();

const size = renderer.getSize( _size );

this.setSize( size.width, size.height );
Expand All @@ -71,15 +95,15 @@ class RTTNode extends TextureNode {

//

_quadMesh.material.fragmentNode = this.node;
this._quadMesh.material.fragmentNode = this._rttNode;

//

const currentRenderTarget = renderer.getRenderTarget();

renderer.setRenderTarget( this.renderTarget );

_quadMesh.render( renderer );
this._quadMesh.render( renderer );

renderer.setRenderTarget( currentRenderTarget );

Expand Down