|
1 | | -import { swizzleTrap } from './ir_builders'; |
| 1 | +import { swizzleTrap, primitiveConstructorNode, variableNode } from './ir_builders'; |
| 2 | +import { BaseType, NodeType, OpCode } from './ir_types'; |
| 3 | +import { getNodeDataFromID, createNodeData, getOrCreateNode } from './ir_dag'; |
| 4 | +import { recordInBasicBlock } from './ir_cfg'; |
2 | 5 | export class StrandsNode { |
3 | 6 | constructor(id, dimension, strandsContext) { |
4 | 7 | this.id = id; |
5 | 8 | this.strandsContext = strandsContext; |
6 | 9 | this.dimension = dimension; |
| 10 | + |
| 11 | + // Store original identifier for varying variables |
| 12 | + const dag = this.strandsContext.dag; |
| 13 | + const nodeData = getNodeDataFromID(dag, this.id); |
| 14 | + if (nodeData && nodeData.identifier) { |
| 15 | + this._originalIdentifier = nodeData.identifier; |
| 16 | + this._originalBaseType = nodeData.baseType; |
| 17 | + this._originalDimension = nodeData.dimension; |
| 18 | + } |
7 | 19 | } |
8 | 20 | copy() { |
9 | 21 | return createStrandsNode(this.id, this.dimension, this.strandsContext); |
10 | 22 | } |
| 23 | + bridge(value) { |
| 24 | + const { dag, cfg } = this.strandsContext; |
| 25 | + const orig = getNodeDataFromID(dag, this.id); |
| 26 | + const baseType = orig?.baseType ?? BaseType.FLOAT; |
| 27 | + |
| 28 | + let newValueID; |
| 29 | + if (value instanceof StrandsNode) { |
| 30 | + newValueID = value.id; |
| 31 | + } else { |
| 32 | + const newVal = primitiveConstructorNode( |
| 33 | + this.strandsContext, |
| 34 | + { baseType, dimension: this.dimension }, |
| 35 | + value |
| 36 | + ); |
| 37 | + newValueID = newVal.id; |
| 38 | + } |
| 39 | + |
| 40 | + // For varying variables, we need both assignment generation AND a way to reference by identifier |
| 41 | + if (this._originalIdentifier) { |
| 42 | + // Create a variable node for the target (the varying variable) |
| 43 | + const { id: targetVarID } = variableNode( |
| 44 | + this.strandsContext, |
| 45 | + { baseType: this._originalBaseType, dimension: this._originalDimension }, |
| 46 | + this._originalIdentifier |
| 47 | + ); |
| 48 | + |
| 49 | + // Create assignment node for GLSL generation |
| 50 | + const assignmentNode = createNodeData({ |
| 51 | + nodeType: NodeType.ASSIGNMENT, |
| 52 | + dependsOn: [targetVarID, newValueID], |
| 53 | + phiBlocks: [] |
| 54 | + }); |
| 55 | + const assignmentID = getOrCreateNode(dag, assignmentNode); |
| 56 | + recordInBasicBlock(cfg, cfg.currentBlock, assignmentID); |
| 57 | + |
| 58 | + // Track for global assignments processing |
| 59 | + this.strandsContext.globalAssignments.push(assignmentID); |
| 60 | + |
| 61 | + // Simply update this node to be a variable node with the identifier |
| 62 | + // This ensures it always generates the variable name in expressions |
| 63 | + const variableNodeData = createNodeData({ |
| 64 | + nodeType: NodeType.VARIABLE, |
| 65 | + baseType: this._originalBaseType, |
| 66 | + dimension: this._originalDimension, |
| 67 | + identifier: this._originalIdentifier |
| 68 | + }); |
| 69 | + const variableID = getOrCreateNode(dag, variableNodeData); |
| 70 | + |
| 71 | + this.id = variableID; // Point to the variable node for expression generation |
| 72 | + } else { |
| 73 | + this.id = newValueID; // For non-varying variables, just update to new value |
| 74 | + } |
| 75 | + |
| 76 | + return this; |
| 77 | + } |
| 78 | + bridgeSwizzle(swizzlePattern, value) { |
| 79 | + const { dag, cfg } = this.strandsContext; |
| 80 | + const orig = getNodeDataFromID(dag, this.id); |
| 81 | + const baseType = orig?.baseType ?? BaseType.FLOAT; |
| 82 | + |
| 83 | + let newValueID; |
| 84 | + if (value instanceof StrandsNode) { |
| 85 | + newValueID = value.id; |
| 86 | + } else { |
| 87 | + const newVal = primitiveConstructorNode( |
| 88 | + this.strandsContext, |
| 89 | + { baseType, dimension: this.dimension }, |
| 90 | + value |
| 91 | + ); |
| 92 | + newValueID = newVal.id; |
| 93 | + } |
| 94 | + |
| 95 | + // For varying variables, create swizzle assignment |
| 96 | + if (this._originalIdentifier) { |
| 97 | + // Create a variable node for the target with swizzle |
| 98 | + const { id: targetVarID } = variableNode( |
| 99 | + this.strandsContext, |
| 100 | + { baseType: this._originalBaseType, dimension: this._originalDimension }, |
| 101 | + this._originalIdentifier |
| 102 | + ); |
| 103 | + |
| 104 | + // Create a swizzle node for the target (myVarying.xyz) |
| 105 | + const swizzleNode = createNodeData({ |
| 106 | + nodeType: NodeType.OPERATION, |
| 107 | + opCode: OpCode.Unary.SWIZZLE, |
| 108 | + baseType: this._originalBaseType, |
| 109 | + dimension: swizzlePattern.length, // xyz = 3, xy = 2, etc. |
| 110 | + swizzle: swizzlePattern, |
| 111 | + dependsOn: [targetVarID] |
| 112 | + }); |
| 113 | + const swizzleID = getOrCreateNode(dag, swizzleNode); |
| 114 | + |
| 115 | + // Create assignment node: myVarying.xyz = value |
| 116 | + const assignmentNode = createNodeData({ |
| 117 | + nodeType: NodeType.ASSIGNMENT, |
| 118 | + dependsOn: [swizzleID, newValueID], |
| 119 | + phiBlocks: [] |
| 120 | + }); |
| 121 | + const assignmentID = getOrCreateNode(dag, assignmentNode); |
| 122 | + recordInBasicBlock(cfg, cfg.currentBlock, assignmentID); |
| 123 | + |
| 124 | + // Track for global assignments processing in the current hook context |
| 125 | + this.strandsContext.globalAssignments.push(assignmentID); |
| 126 | + |
| 127 | + // Simply update this node to be a variable node with the identifier |
| 128 | + // This ensures it always generates the variable name in expressions |
| 129 | + const variableNodeData = createNodeData({ |
| 130 | + nodeType: NodeType.VARIABLE, |
| 131 | + baseType: this._originalBaseType, |
| 132 | + dimension: this._originalDimension, |
| 133 | + identifier: this._originalIdentifier |
| 134 | + }); |
| 135 | + const variableID = getOrCreateNode(dag, variableNodeData); |
| 136 | + |
| 137 | + this.id = variableID; // Point to the variable node, not the assignment node |
| 138 | + } else { |
| 139 | + this.id = newValueID; // For non-varying variables, just update to new value |
| 140 | + } |
| 141 | + |
| 142 | + return this; |
| 143 | + } |
| 144 | + getValue() { |
| 145 | + if (this._originalIdentifier) { |
| 146 | + const { id, dimension } = variableNode( |
| 147 | + this.strandsContext, |
| 148 | + { baseType: this._originalBaseType, dimension: this._originalDimension }, |
| 149 | + this._originalIdentifier |
| 150 | + ); |
| 151 | + return createStrandsNode(id, dimension, this.strandsContext); |
| 152 | + } |
| 153 | + |
| 154 | + return this; |
| 155 | + } |
11 | 156 | } |
12 | 157 | export function createStrandsNode(id, dimension, strandsContext, onRebind) { |
13 | 158 | return new Proxy( |
|
0 commit comments