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

WebGPUPipelines: Improve reuse programs. #26305

Merged
merged 4 commits into from
Jun 21, 2023
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
1 change: 0 additions & 1 deletion examples/jsm/nodes/display/ColorSpaceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class ColorSpaceNode extends TempNode {
super( 'vec4' );

this.method = method;

this.node = node;

}
Expand Down
36 changes: 22 additions & 14 deletions examples/jsm/renderers/common/Pipelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Pipelines extends DataMap {

// release previous cache

this._releasePipeline( computeNode );
const previousPipeline = this._releasePipeline( computeNode );

// get shader

Expand All @@ -45,6 +45,8 @@ class Pipelines extends DataMap {

if ( stageCompute === undefined ) {

if ( previousPipeline ) this._releaseProgram( previousPipeline.computeShader );

stageCompute = new ProgrammableStage( nodeBuilder.computeShader, 'compute' );
this.programs.compute.set( nodeBuilder.computeShader, stageCompute );

Expand Down Expand Up @@ -81,7 +83,7 @@ class Pipelines extends DataMap {

// release previous cache

this._releasePipeline( renderObject );
const previousPipeline = this._releasePipeline( renderObject );

// get shader

Expand All @@ -93,6 +95,8 @@ class Pipelines extends DataMap {

if ( stageVertex === undefined ) {

if ( previousPipeline ) this._releaseProgram( previousPipeline.vertexProgram );

stageVertex = new ProgrammableStage( nodeBuilder.vertexShader, 'vertex' );
this.programs.vertex.set( nodeBuilder.vertexShader, stageVertex );

Expand All @@ -104,6 +108,8 @@ class Pipelines extends DataMap {

if ( stageFragment === undefined ) {

if ( previousPipeline ) this._releaseProgram( previousPipeline.fragmentShader );

stageFragment = new ProgrammableStage( nodeBuilder.fragmentShader, 'fragment' );
this.programs.fragment.set( nodeBuilder.fragmentShader, stageFragment );

Expand Down Expand Up @@ -133,7 +139,18 @@ class Pipelines extends DataMap {

delete( object ) {

this._releasePipeline( object );
const pipeline = this._releasePipeline( object );

if ( pipeline.isComputePipeline ) {

this._releaseProgram( pipeline.computeProgram );

} else {

this._releaseProgram( pipeline.vertexProgram );
this._releaseProgram( pipeline.fragmentProgram );

}

super.delete( object );

Expand Down Expand Up @@ -236,19 +253,10 @@ class Pipelines extends DataMap {

this.caches.delete( pipeline.cacheKey );

if ( pipeline.isComputePipeline ) {

this._releaseProgram( pipeline.computeProgram );

} else {

this._releaseProgram( pipeline.vertexProgram );
this._releaseProgram( pipeline.fragmentProgram );

}

}

return pipeline;

}

_releaseProgram( program ) {
Expand Down
16 changes: 7 additions & 9 deletions examples/jsm/renderers/common/RenderObject.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { EventDispatcher } from 'three';

let id = 0;

export default class RenderObject extends EventDispatcher {
export default class RenderObject {

constructor( nodes, geometries, renderer, object, material, scene, camera, lightsNode ) {

super();

this._nodes = nodes;
this._geometries = geometries;

Expand All @@ -29,15 +25,15 @@ export default class RenderObject extends EventDispatcher {
this._materialVersion = - 1;
this._materialCacheKey = '';

const onDispose = () => {
this.onDispose = null;

this.material.removeEventListener( 'dispose', onDispose );
this.onMaterialDispose = () => {

this.dispose();

};

this.material.addEventListener( 'dispose', onDispose );
this.material.addEventListener( 'dispose', this.onMaterialDispose );

}

Expand Down Expand Up @@ -108,7 +104,9 @@ export default class RenderObject extends EventDispatcher {

dispose() {

this.dispatchEvent( { type: 'dispose' } );
this.material.removeEventListener( 'dispose', this.onMaterialDispose );

this.onDispose();

}

Expand Down
30 changes: 12 additions & 18 deletions examples/jsm/renderers/common/RenderObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ class RenderObjects extends ChainMap {

if ( renderObject === undefined ) {

renderObject = new RenderObject( this.nodes, this.geometries, this.renderer, object, material, scene, camera, lightsNode );

this._initRenderObject( renderObject );
renderObject = this.createRenderObject( this.nodes, this.geometries, this.renderer, object, material, scene, camera, lightsNode );

this.set( chainArray, renderObject );

Expand Down Expand Up @@ -59,29 +57,25 @@ class RenderObjects extends ChainMap {

}

_initRenderObject( renderObject ) {

const data = this.dataMap.get( renderObject );

if ( data.initialized !== true ) {
createRenderObject( nodes, geometries, renderer, object, material, scene, camera, lightsNode ) {

data.initialized = true;
data.cacheKey = renderObject.getCacheKey();
const renderObject = new RenderObject( nodes, geometries, renderer, object, material, scene, camera, lightsNode );

const onDispose = () => {
const data = this.dataMap.get( renderObject );
data.cacheKey = renderObject.getCacheKey();

renderObject.removeEventListener( 'dispose', onDispose );
renderObject.onDispose = () => {

this.pipelines.delete( renderObject );
this.nodes.delete( renderObject );
this.dataMap.delete( renderObject );

this.delete( renderObject.getChainArray() );
this.pipelines.delete( renderObject );
this.nodes.delete( renderObject );

};
this.delete( renderObject.getChainArray() );

renderObject.addEventListener( 'dispose', onDispose );
};

}
return renderObject;

}

Expand Down
4 changes: 3 additions & 1 deletion examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NoColorSpace } from 'three';

import UniformsGroup from '../../common/UniformsGroup.js';
import {
FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
Expand Down Expand Up @@ -133,7 +135,7 @@ class WGSLNodeBuilder extends NodeBuilder {

needsColorSpaceToLinear( texture ) {

return texture.isVideoTexture === true;
return texture.isVideoTexture === true && texture.colorSpace !== NoColorSpace;

}

Expand Down