-
-
Notifications
You must be signed in to change notification settings - Fork 36.3k
Expand file tree
/
Copy pathIndexNode.js
More file actions
173 lines (127 loc) · 4.16 KB
/
IndexNode.js
File metadata and controls
173 lines (127 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import Node from './Node.js';
import { nodeImmutable, varying } from '../tsl/TSLBase.js';
/**
* This class represents shader indices of different types. The following predefined node
* objects cover frequent use cases:
*
* - `vertexIndex`: The index of a vertex within a mesh.
* - `instanceIndex`: The index of either a mesh instance or an invocation of a compute shader.
* - `drawIndex`: The index of a draw call.
* - `invocationLocalIndex`: The index of a compute invocation within the scope of a workgroup load.
* - `invocationSubgroupIndex`: The index of a compute invocation within the scope of a subgroup.
* - `subgroupIndex`: The index of a compute invocation's subgroup within its workgroup.
*
* @augments Node
*/
class IndexNode extends Node {
static get type() {
return 'IndexNode';
}
/**
* Constructs a new index node.
*
* @param {('vertex'|'instance'|'subgroup'|'invocationLocal'|'invocationGlobal'|'invocationSubgroup'|'draw')} scope - The scope of the index node.
*/
constructor( scope ) {
super( 'uint' );
/**
* The scope of the index node.
*
* @type {string}
*/
this.scope = scope;
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isIndexNode = true;
}
generate( builder ) {
const nodeType = this.getNodeType( builder );
const scope = this.scope;
let propertyName;
if ( scope === IndexNode.VERTEX ) {
propertyName = builder.getVertexIndex();
} else if ( scope === IndexNode.INSTANCE ) {
propertyName = builder.getInstanceIndex();
} else if ( scope === IndexNode.DRAW ) {
propertyName = builder.getDrawIndex();
} else if ( scope === IndexNode.INVOCATION_LOCAL ) {
propertyName = builder.getInvocationLocalIndex();
} else if ( scope === IndexNode.INVOCATION_SUBGROUP ) {
propertyName = builder.getInvocationSubgroupIndex();
} else if ( scope === IndexNode.SUBGROUP ) {
propertyName = builder.getSubgroupIndex();
} else {
throw new Error( 'THREE.IndexNode: Unknown scope: ' + scope );
}
let output;
if ( builder.shaderStage === 'vertex' || builder.shaderStage === 'compute' ) {
output = propertyName;
} else {
const nodeVarying = varying( this );
output = nodeVarying.build( builder, nodeType );
}
return output;
}
}
IndexNode.VERTEX = 'vertex';
IndexNode.INSTANCE = 'instance';
IndexNode.SUBGROUP = 'subgroup';
IndexNode.INVOCATION_LOCAL = 'invocationLocal';
IndexNode.INVOCATION_SUBGROUP = 'invocationSubgroup';
IndexNode.DRAW = 'draw';
export default IndexNode;
/**
* TSL object that represents the index of a vertex within a mesh.
*
* @tsl
* @type {IndexNode}
*/
export const vertexIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.VERTEX );
/**
* TSL object that represents the global index of a compute shader invocation.
* This is the linearized index across all workgroups, equivalent to the global invocation ID.
*
* @tsl
* @type {IndexNode}
*/
export const invocationIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.INVOCATION_GLOBAL );
/**
* TSL object that represents the index of either a mesh instance or an invocation of a compute shader.
*
* @tsl
* @type {IndexNode}
*/
export const instanceIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.INSTANCE );
/**
* TSL object that represents the index of the subgroup the current compute invocation belongs to.
*
* @tsl
* @type {IndexNode}
*/
export const subgroupIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.SUBGROUP );
/**
* TSL object that represents the index of a compute invocation within the scope of a subgroup.
*
* @tsl
* @type {IndexNode}
*/
export const invocationSubgroupIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.INVOCATION_SUBGROUP );
/**
* TSL object that represents the index of a compute invocation within the scope of a workgroup load.
*
* @tsl
* @type {IndexNode}
*/
export const invocationLocalIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.INVOCATION_LOCAL );
/**
* TSL object that represents the index of a draw call.
*
* @tsl
* @type {IndexNode}
*/
export const drawIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.DRAW );