Skip to content

Commit 7f4fc6f

Browse files
authored
Renderer: Document more modules. (#30233)
* Renderer: Document more modules. * Fix typos. * Renderer: Document more modules. * Fix typos.
1 parent 7f6e1fd commit 7f4fc6f

File tree

9 files changed

+734
-66
lines changed

9 files changed

+734
-66
lines changed

src/renderers/common/Backend.js

Lines changed: 304 additions & 54 deletions
Large diffs are not rendered by default.

src/renderers/common/BindGroup.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
let _id = 0;
22

33
/**
4-
* Represents a bind group.
4+
* A bind group represents a collection of bindings and thus a collection
5+
* or resources. Bind groups are assigned to pipelines to provide them
6+
* with the required resources (like uniform buffers or textures).
57
*
68
* @private
79
*/

src/renderers/common/Bindings.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ class Bindings extends DataMap {
217217

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

220-
if ( ! updated ) continue;
220+
// every uniforms group is a uniform buffer. So if no update is required,
221+
// we move one with the next binding. Otherwise the next if block will update the group.
222+
223+
if ( updated === false ) continue;
221224

222225
}
223226

src/renderers/common/nodes/NodeBuilderState.js

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,128 @@
11
import BindGroup from '../BindGroup.js';
22

3+
/**
4+
* This module represents the state of a node builder after it was
5+
* used to build the nodes for a render object. The state holds the
6+
* results of the build for further processing in the renderer.
7+
*
8+
* Render objects with identical cache keys share the same node builder state.
9+
*
10+
* @private
11+
*/
312
class NodeBuilderState {
413

14+
/**
15+
* Constructs a new node builder state.
16+
*
17+
* @param {String?} vertexShader - The native vertex shader code.
18+
* @param {String?} fragmentShader - The native fragment shader code.
19+
* @param {String?} computeShader - The native compute shader code.
20+
* @param {Array<NodeAttribute>} nodeAttributes - An array of node attributes.
21+
* @param {Array<BindGroup>} bindings - An array of bind groups.
22+
* @param {Array<Node>} updateNodes - An array of nodes that implement their `update()` method.
23+
* @param {Array<Node>} updateBeforeNodes - An array of nodes that implement their `updateBefore()` method.
24+
* @param {Array<Node>} updateAfterNodes - An array of nodes that implement their `updateAfter()` method.
25+
* @param {NodeMaterialObserver} monitor - A node material observer.
26+
* @param {Array<Object>} transforms - An array with transform attribute objects. Only relevant when using compute shaders with WebGL 2.
27+
*/
528
constructor( vertexShader, fragmentShader, computeShader, nodeAttributes, bindings, updateNodes, updateBeforeNodes, updateAfterNodes, monitor, transforms = [] ) {
629

30+
/**
31+
* The native vertex shader code.
32+
*
33+
* @type {String}
34+
*/
735
this.vertexShader = vertexShader;
36+
37+
/**
38+
* The native fragment shader code.
39+
*
40+
* @type {String}
41+
*/
842
this.fragmentShader = fragmentShader;
43+
44+
/**
45+
* The native compute shader code.
46+
*
47+
* @type {String}
48+
*/
949
this.computeShader = computeShader;
50+
51+
/**
52+
* An array with transform attribute objects.
53+
* Only relevant when using compute shaders with WebGL 2.
54+
*
55+
* @type {Array<Object>}
56+
*/
1057
this.transforms = transforms;
1158

59+
/**
60+
* An array of node attributes representing
61+
* the attributes of the shaders.
62+
*
63+
* @type {Array<NodeAttribute>}
64+
*/
1265
this.nodeAttributes = nodeAttributes;
66+
67+
/**
68+
* An array of bind groups representing the uniform or storage
69+
* buffers, texture or samplers of the shader.
70+
*
71+
* @type {Array<BindGroup>}
72+
*/
1373
this.bindings = bindings;
1474

75+
/**
76+
* An array of nodes that implement their `update()` method.
77+
*
78+
* @type {Array<Node>}
79+
*/
1580
this.updateNodes = updateNodes;
81+
82+
/**
83+
* An array of nodes that implement their `updateBefore()` method.
84+
*
85+
* @type {Array<Node>}
86+
*/
1687
this.updateBeforeNodes = updateBeforeNodes;
88+
89+
/**
90+
* An array of nodes that implement their `updateAfter()` method.
91+
*
92+
* @type {Array<Node>}
93+
*/
1794
this.updateAfterNodes = updateAfterNodes;
1895

96+
/**
97+
* A node material observer.
98+
*
99+
* @type {NodeMaterialObserver}
100+
*/
19101
this.monitor = monitor;
20102

103+
/**
104+
* How often this state is used by render objects.
105+
*
106+
* @type {Number}
107+
*/
21108
this.usedTimes = 0;
22109

23110
}
24111

112+
/**
113+
* This method is used to create a array of bind groups based
114+
* on the existing bind groups of this state. Shared groups are
115+
* not cloned.
116+
*
117+
* @return {Array<BindGroup>} A array of bind groups.
118+
*/
25119
createBindings() {
26120

27121
const bindings = [];
28122

29123
for ( const instanceGroup of this.bindings ) {
30124

31-
const shared = instanceGroup.bindings[ 0 ].groupNode.shared;
125+
const shared = instanceGroup.bindings[ 0 ].groupNode.shared; // TODO: Is it safe to always check the first binding in the group?
32126

33127
if ( shared !== true ) {
34128

src/renderers/common/nodes/NodeLibrary.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,52 @@
1+
/**
2+
* The purpose of a node library is to assign node implementations
3+
* to existing library features. In `WebGPURenderer` lights, materials
4+
* which are not based on `NodeMaterial` as well as tone mapping techniques
5+
* are implemented with node-based modules.
6+
*
7+
* @private
8+
*/
19
class NodeLibrary {
210

11+
/**
12+
* Constructs a new node library.
13+
*/
314
constructor() {
415

16+
/**
17+
* A weak map that maps lights to light nodes.
18+
*
19+
* @type {WeakMap<Light.constructor,AnalyticLightNode.constructor>}
20+
*/
521
this.lightNodes = new WeakMap();
22+
23+
/**
24+
* A map that maps materials to node materials.
25+
*
26+
* @type {WeakMap<String,NodeMaterial.constructor>}
27+
*/
628
this.materialNodes = new Map();
29+
30+
/**
31+
* A map that maps tone mapping techniques (constants)
32+
* to tone mapping node functions.
33+
*
34+
* @type {WeakMap<Number,Function>}
35+
*/
736
this.toneMappingNodes = new Map();
837

938
}
1039

40+
/**
41+
* Returns a matching node material instance for the given material object.
42+
*
43+
* This method also assigns/copies the properties of the given material object
44+
* to the node material. This is done to make sure the current material
45+
* configuration carries over to the node version.
46+
*
47+
* @param {Material} material - A material.
48+
* @return {NodeMaterial} The corresponding node material.
49+
*/
1150
fromMaterial( material ) {
1251

1352
if ( material.isNodeMaterial ) return material;
@@ -32,42 +71,85 @@ class NodeLibrary {
3271

3372
}
3473

74+
/**
75+
* Adds a tone mapping node function for a tone mapping technique (constant).
76+
*
77+
* @param {Function} toneMappingNode - The tone mapping node function.
78+
* @param {Number} toneMapping - The tone mapping.
79+
*/
3580
addToneMapping( toneMappingNode, toneMapping ) {
3681

3782
this.addType( toneMappingNode, toneMapping, this.toneMappingNodes );
3883

3984
}
4085

86+
/**
87+
* Returns a tone mapping node function for a tone mapping technique (constant).
88+
*
89+
* @param {Number} toneMapping - The tone mapping.
90+
* @return {Function?} The tone mapping node function. Returns `null` if no node function is found.
91+
*/
4192
getToneMappingFunction( toneMapping ) {
4293

4394
return this.toneMappingNodes.get( toneMapping ) || null;
4495

4596
}
4697

98+
/**
99+
* Returns a node material class definition for a material type.
100+
*
101+
* @param {Sring} materialType - The material type.
102+
* @return {NodeMaterial.constructor?} The node material class definition. Returns `null` if no node material is found.
103+
*/
47104
getMaterialNodeClass( materialType ) {
48105

49106
return this.materialNodes.get( materialType ) || null;
50107

51108
}
52109

110+
/**
111+
* Adds a node material class definition for a given material type.
112+
*
113+
* @param {NodeMaterial.constructor} materialNodeClass - The node material class definition.
114+
* @param {Sring} materialClassType - The material type.
115+
*/
53116
addMaterial( materialNodeClass, materialClassType ) {
54117

55118
this.addType( materialNodeClass, materialClassType, this.materialNodes );
56119

57120
}
58121

122+
/**
123+
* Returns a light node class definition for a light class definition.
124+
*
125+
* @param {Light.constructor} light - The light class definition.
126+
* @return {AnalyticLightNode.constructor?} The light node class definition. Returns `null` if no light node is found.
127+
*/
59128
getLightNodeClass( light ) {
60129

61130
return this.lightNodes.get( light ) || null;
62131

63132
}
64133

134+
/**
135+
* Adds a light node class definition for a given light class definition.
136+
*
137+
* @param {AnalyticLightNode.constructor} lightNodeClass - The light node class definition.
138+
* @param {Light.constructor} lightClass - The light class definition.
139+
*/
65140
addLight( lightNodeClass, lightClass ) {
66141

67142
this.addClass( lightNodeClass, lightClass, this.lightNodes );
68143

69144
}
70145

146+
/**
147+
* Adds a node class definition for the given type to the provided type library.
148+
*
149+
* @param {Any} nodeClass - The node class definition.
150+
* @param {String} type - The object type.
151+
* @param {Map} library - The type library.
152+
*/
71153
addType( nodeClass, type, library ) {
72154

73155
if ( library.has( type ) ) {
@@ -84,6 +166,13 @@ class NodeLibrary {
84166

85167
}
86168

169+
/**
170+
* Adds a node class definition for the given class definition to the provided type library.
171+
*
172+
* @param {Any} nodeClass - The node class definition.
173+
* @param {Any} baseClass - The class definition.
174+
* @param {WeakMap} library - The type library.
175+
*/
87176
addClass( nodeClass, baseClass, library ) {
88177

89178
if ( library.has( baseClass ) ) {

0 commit comments

Comments
 (0)