-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathstrands_node.js
More file actions
170 lines (155 loc) · 5.73 KB
/
strands_node.js
File metadata and controls
170 lines (155 loc) · 5.73 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
import { swizzleTrap, primitiveConstructorNode, variableNode } from './ir_builders';
import { BaseType, NodeType, OpCode } from './ir_types';
import { getNodeDataFromID, createNodeData, getOrCreateNode } from './ir_dag';
import { recordInBasicBlock } from './ir_cfg';
export class StrandsNode {
constructor(id, dimension, strandsContext) {
this.id = id;
this.strandsContext = strandsContext;
this.dimension = dimension;
this.structProperties = null;
this.isStrandsNode = true;
// Store original identifier for varying variables
const dag = this.strandsContext.dag;
const nodeData = getNodeDataFromID(dag, this.id);
if (nodeData && nodeData.identifier) {
this._originalIdentifier = nodeData.identifier;
}
if (nodeData) {
this._originalBaseType = nodeData.baseType;
this._originalDimension = nodeData.dimension;
}
}
withStructProperties(properties) {
this.structProperties = properties;
return this;
}
copy() {
return createStrandsNode(this.id, this.dimension, this.strandsContext);
}
typeInfo() {
return {
baseType: this._originalBaseType || BaseType.FLOAT,
dimension: this.dimension
};
}
bridge(value) {
const { dag, cfg } = this.strandsContext;
const orig = getNodeDataFromID(dag, this.id);
const baseType = orig?.baseType ?? BaseType.FLOAT;
let newValueID;
if (value?.isStrandsNode) {
newValueID = value.id;
} else {
const newVal = primitiveConstructorNode(
this.strandsContext,
{ baseType, dimension: this.dimension },
value
);
newValueID = newVal.id;
}
// For varying variables, we need both assignment generation AND a way to reference by identifier
if (this._originalIdentifier) {
// Create a variable node for the target (the varying variable)
const { id: targetVarID } = variableNode(
this.strandsContext,
{ baseType: this._originalBaseType, dimension: this._originalDimension },
this._originalIdentifier
);
// Create assignment node for GLSL generation
const assignmentNode = createNodeData({
nodeType: NodeType.ASSIGNMENT,
dependsOn: [targetVarID, newValueID],
phiBlocks: []
});
const assignmentID = getOrCreateNode(dag, assignmentNode);
recordInBasicBlock(cfg, cfg.currentBlock, assignmentID);
// Simply update this node to be a variable node with the identifier
// This ensures it always generates the variable name in expressions
const variableNodeData = createNodeData({
nodeType: NodeType.VARIABLE,
baseType: this._originalBaseType,
dimension: this._originalDimension,
identifier: this._originalIdentifier
});
const variableID = getOrCreateNode(dag, variableNodeData);
this.id = variableID; // Point to the variable node for expression generation
} else {
this.id = newValueID; // For non-varying variables, just update to new value
}
return this;
}
bridgeSwizzle(swizzlePattern, value) {
const { dag, cfg } = this.strandsContext;
const orig = getNodeDataFromID(dag, this.id);
const baseType = orig?.baseType ?? BaseType.FLOAT;
let newValueID;
if (value?.isStrandsNode) {
newValueID = value.id;
} else {
const newVal = primitiveConstructorNode(
this.strandsContext,
{ baseType, dimension: this.dimension },
value
);
newValueID = newVal.id;
}
// For varying variables, create swizzle assignment
if (this._originalIdentifier) {
// Create a variable node for the target with swizzle
const { id: targetVarID } = variableNode(
this.strandsContext,
{ baseType: this._originalBaseType, dimension: this._originalDimension },
this._originalIdentifier
);
// Create a swizzle node for the target (myVarying.xyz)
const swizzleNode = createNodeData({
nodeType: NodeType.OPERATION,
opCode: OpCode.Unary.SWIZZLE,
baseType: this._originalBaseType,
dimension: swizzlePattern.length, // xyz = 3, xy = 2, etc.
swizzle: swizzlePattern,
dependsOn: [targetVarID]
});
const swizzleID = getOrCreateNode(dag, swizzleNode);
// Create assignment node: myVarying.xyz = value
const assignmentNode = createNodeData({
nodeType: NodeType.ASSIGNMENT,
dependsOn: [swizzleID, newValueID],
phiBlocks: []
});
const assignmentID = getOrCreateNode(dag, assignmentNode);
recordInBasicBlock(cfg, cfg.currentBlock, assignmentID);
// Simply update this node to be a variable node with the identifier
// This ensures it always generates the variable name in expressions
const variableNodeData = createNodeData({
nodeType: NodeType.VARIABLE,
baseType: this._originalBaseType,
dimension: this._originalDimension,
identifier: this._originalIdentifier
});
const variableID = getOrCreateNode(dag, variableNodeData);
this.id = variableID; // Point to the variable node, not the assignment node
} else {
this.id = newValueID; // For non-varying variables, just update to new value
}
return this;
}
getValue() {
if (this._originalIdentifier) {
const { id, dimension } = variableNode(
this.strandsContext,
{ baseType: this._originalBaseType, dimension: this._originalDimension },
this._originalIdentifier
);
return createStrandsNode(id, dimension, this.strandsContext);
}
return this;
}
}
export function createStrandsNode(id, dimension, strandsContext, onRebind) {
return new Proxy(
new StrandsNode(id, dimension, strandsContext),
swizzleTrap(id, dimension, strandsContext, onRebind)
);
}