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

WebGPURenderer: Uniform Group #27134

Merged
merged 2 commits into from
Nov 8, 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: 1 addition & 0 deletions examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export { default as ParameterNode, parameter } from './core/ParameterNode.js';
export { default as PropertyNode, property, output, diffuseColor, roughness, metalness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, specularColor, shininess, dashSize, gapSize, pointWidth } from './core/PropertyNode.js';
export { default as StackNode, stack } from './core/StackNode.js';
export { default as TempNode } from './core/TempNode.js';
export { default as UniformGroupNode, uniformGroup, objectGroup, renderGroup, frameGroup } from './core/UniformGroupNode.js';
export { default as UniformNode, uniform } from './core/UniformNode.js';
export { default as VaryingNode, varying } from './core/VaryingNode.js';
export { default as OutputStructNode, outputStruct } from './core/OutputStructNode.js';
Expand Down
10 changes: 10 additions & 0 deletions examples/jsm/nodes/accessors/CameraNode.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import Object3DNode from './Object3DNode.js';
import { addNodeClass } from '../core/Node.js';
import { label } from '../core/ContextNode.js';
import { NodeUpdateType } from '../core/constants.js';
//import { sharedUniformGroup } from '../core/UniformGroupNode.js';
import { nodeImmutable } from '../shadernode/ShaderNode.js';

//const cameraGroup = sharedUniformGroup( 'camera' );

class CameraNode extends Object3DNode {

constructor( scope = CameraNode.POSITION ) {

super( scope );

this.updateType = NodeUpdateType.RENDER;

//this._uniformNode.groupNode = cameraGroup;

}

getNodeType( builder ) {
Expand All @@ -35,6 +43,8 @@ class CameraNode extends Object3DNode {
const uniformNode = this._uniformNode;
const scope = this.scope;

//cameraGroup.needsUpdate = true;

if ( scope === CameraNode.VIEW_MATRIX ) {

uniformNode.value = camera.matrixWorldInverse;
Expand Down
13 changes: 11 additions & 2 deletions examples/jsm/nodes/accessors/MaterialReferenceNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ReferenceNode from './ReferenceNode.js';
import { NodeUpdateType } from '../core/constants.js';
//import { renderGroup } from '../core/UniformGroupNode.js';
//import { NodeUpdateType } from '../core/constants.js';
import { addNodeClass } from '../core/Node.js';
import { nodeObject } from '../shadernode/ShaderNode.js';

Expand All @@ -11,10 +12,18 @@ class MaterialReferenceNode extends ReferenceNode {

this.material = material;

this.updateType = NodeUpdateType.RENDER;
//this.updateType = NodeUpdateType.RENDER;

}

/*setNodeType( node ) {

super.setNodeType( node );

this.node.groupNode = renderGroup;

}*/

updateReference( frame ) {

this.reference = this.material !== null ? this.material : frame.material;
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/accessors/ModelNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ModelNode extends Object3DNode {
export default ModelNode;

export const modelDirection = nodeImmutable( ModelNode, ModelNode.DIRECTION );
export const modelViewMatrix = nodeImmutable( ModelNode, ModelNode.VIEW_MATRIX ).temp( 'ModelViewMatrix' );
export const modelViewMatrix = nodeImmutable( ModelNode, ModelNode.VIEW_MATRIX ).label( 'modelViewMatrix' ).temp( 'ModelViewMatrix' );
export const modelNormalMatrix = nodeImmutable( ModelNode, ModelNode.NORMAL_MATRIX );
export const modelWorldMatrix = nodeImmutable( ModelNode, ModelNode.WORLD_MATRIX );
export const modelPosition = nodeImmutable( ModelNode, ModelNode.POSITION );
Expand Down
40 changes: 39 additions & 1 deletion examples/jsm/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { getCurrentStack, setCurrentStack } from '../shadernode/ShaderNode.js';
import { maxMipLevel } from '../utils/MaxMipLevelNode.js';

import CubeRenderTarget from '../../renderers/common/CubeRenderTarget.js';
import ChainMap from '../../renderers/common/ChainMap.js';

const uniformsGroupCache = new ChainMap();

const typeFromLength = new Map( [
[ 2, 'vec2' ],
Expand Down Expand Up @@ -128,6 +131,41 @@ class NodeBuilder {

}

_getSharedBindings( bindings ) {

const shared = [];

for ( const binding of bindings ) {

if ( binding.shared === true ) {

// nodes is the chainmap key
const nodes = binding.getNodes();

let sharedBinding = uniformsGroupCache.get( nodes );

if ( sharedBinding === undefined ) {

uniformsGroupCache.set( nodes, binding );

sharedBinding = binding;

}

shared.push( sharedBinding );

} else {

shared.push( binding );

}

}

return shared;

}

getBindings() {

let bindingsArray = this.bindingsArray;
Expand All @@ -136,7 +174,7 @@ class NodeBuilder {

const bindings = this.bindings;

this.bindingsArray = bindingsArray = ( this.material !== null ) ? [ ...bindings.vertex, ...bindings.fragment ] : bindings.compute;
this.bindingsArray = bindingsArray = this._getSharedBindings( ( this.material !== null ) ? [ ...bindings.vertex, ...bindings.fragment ] : bindings.compute );

}

Expand Down
18 changes: 10 additions & 8 deletions examples/jsm/nodes/core/NodeFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class NodeFrame {
const updateType = node.getUpdateBeforeType();
const reference = node.updateReference( this );

const { frameMap, renderMap } = this._getMaps( this.updateBeforeMap, reference );

if ( updateType === NodeUpdateType.FRAME ) {

const { frameMap } = this._getMaps( this.updateBeforeMap, reference );

if ( frameMap.get( node ) !== this.frameId ) {

frameMap.set( node, this.frameId );
Expand All @@ -61,10 +61,11 @@ class NodeFrame {

} else if ( updateType === NodeUpdateType.RENDER ) {

if ( renderMap.get( node ) !== this.renderId || frameMap.get( node ) !== this.frameId ) {
const { renderMap } = this._getMaps( this.updateBeforeMap, reference );

if ( renderMap.get( node ) !== this.renderId ) {

renderMap.set( node, this.renderId );
frameMap.set( node, this.frameId );

node.updateBefore( this );

Expand All @@ -83,10 +84,10 @@ class NodeFrame {
const updateType = node.getUpdateType();
const reference = node.updateReference( this );

const { frameMap, renderMap } = this._getMaps( this.updateMap, reference );

if ( updateType === NodeUpdateType.FRAME ) {

const { frameMap } = this._getMaps( this.updateMap, reference );

if ( frameMap.get( node ) !== this.frameId ) {

frameMap.set( node, this.frameId );
Expand All @@ -97,10 +98,11 @@ class NodeFrame {

} else if ( updateType === NodeUpdateType.RENDER ) {

if ( renderMap.get( node ) !== this.renderId || frameMap.get( node ) !== this.frameId ) {
const { renderMap } = this._getMaps( this.updateMap, reference );

if ( renderMap.get( node ) !== this.renderId ) {

renderMap.set( node, this.renderId );
frameMap.set( node, this.frameId );

node.update( this );

Expand Down
12 changes: 12 additions & 0 deletions examples/jsm/nodes/core/NodeUniform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ class NodeUniform {

}

get id() {

return this.node.id;

}

get groupNode() {

return this.node.groupNode;

}

}

export default NodeUniform;
13 changes: 13 additions & 0 deletions examples/jsm/nodes/core/UniformGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class UniformGroup {

constructor( name ) {

this.name = name;

this.isUniformGroup = true;

}

}

export default UniformGroup;
36 changes: 36 additions & 0 deletions examples/jsm/nodes/core/UniformGroupNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Node from './Node.js';
import { addNodeClass } from './Node.js';

class UniformGroupNode extends Node {
Copy link
Contributor

@LeviPesin LeviPesin Jan 11, 2024

Choose a reason for hiding this comment

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

Should this be a node, actually? Can't we unify this with the UniformGroup class (and maybe name it NodeUniformGroup)? /ping @sunag


constructor( name, shared = false ) {

super( 'string' );

this.name = name;
this.version = 0;

this.shared = shared;

this.isUniformGroup = true;

}

set needsUpdate( value ) {

if ( value === true ) this.version ++;

}

}

export const uniformGroup = ( name ) => new UniformGroupNode( name );
export const sharedUniformGroup = ( name ) => new UniformGroupNode( name, true );

export const frameGroup = sharedUniformGroup( 'frame' );
export const renderGroup = sharedUniformGroup( 'render' );
export const objectGroup = uniformGroup( 'object' );

export default UniformGroupNode;

addNodeClass( 'UniformGroupNode', UniformGroupNode );
17 changes: 17 additions & 0 deletions examples/jsm/nodes/core/UniformNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import InputNode from './InputNode.js';
import { objectGroup } from './UniformGroupNode.js';
import { addNodeClass } from './Node.js';
import { nodeObject, getConstNodeType } from '../shadernode/ShaderNode.js';

Expand All @@ -10,6 +11,22 @@ class UniformNode extends InputNode {

this.isUniformNode = true;

this.groupNode = objectGroup;

}

setGroup( group ) {

this.groupNode = group;

return this;

}

getGroup() {

return this.groupNode;

}

getUniformHash( builder ) {
Expand Down
1 change: 1 addition & 0 deletions examples/jsm/nodes/materials/MeshBasicNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MeshBasicNodeMaterial extends NodeMaterial {
this.isMeshBasicNodeMaterial = true;

this.lights = false;
//this.normals = false; @TODO: normals usage by context

this.setDefaultValues( defaultValues );

Expand Down
31 changes: 10 additions & 21 deletions examples/jsm/renderers/common/Bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Bindings extends DataMap {

this.pipelines.bindings = this; // assign bindings to pipelines

this.updateMap = new WeakMap();

}

getForRender( renderObject ) {
Expand Down Expand Up @@ -100,24 +98,25 @@ class Bindings extends DataMap {

const { backend } = this;

const updateMap = this.updateMap;
const callId = this.info.calls;

let needsBindingsUpdate = false;

// iterate over all bindings and check if buffer updates or a new binding group is required

for ( const binding of bindings ) {

const isUpdated = updateMap.get( binding ) === callId;
if ( binding.isNodeUniformsGroup ) {

const updated = this.nodes.updateGroup( binding );

if ( isUpdated ) continue;
if ( ! updated ) continue;

}

if ( binding.isUniformBuffer ) {

const needsUpdate = binding.update();
const updated = binding.update();

if ( needsUpdate ) {
if ( updated ) {

backend.updateBinding( binding );

Expand All @@ -127,18 +126,16 @@ class Bindings extends DataMap {

if ( binding.needsBindingsUpdate ) needsBindingsUpdate = true;

const needsUpdate = binding.update();
const updated = binding.update();

if ( needsUpdate ) {
if ( updated ) {

this.textures.updateTexture( binding.texture );

}

}

updateMap.set( binding, callId );

}

if ( needsBindingsUpdate === true ) {
Expand All @@ -151,14 +148,6 @@ class Bindings extends DataMap {

}

dispose() {

super.dispose();

this.updateMap = new WeakMap();

}

}

export default Bindings;
Loading