| 3 | import { NodeType, StatementType, structType, TypeInfoFromGLSLName } from './ir_types'; |
| 4 | |
| 5 | export function generateShaderCode(strandsContext) { |
| 6 | const { |
| 7 | cfg, |
| 8 | backend, |
| 9 | vertexDeclarations, |
| 10 | fragmentDeclarations, |
| 11 | computeDeclarations |
| 12 | } = strandsContext; |
| 13 | |
| 14 | const hooksObj = { |
| 15 | uniforms: {}, |
| 16 | storageUniforms: {}, |
| 17 | varyingVariables: [], |
| 18 | }; |
| 19 | |
| 20 | for (const {name, typeInfo, defaultValue} of strandsContext.uniforms) { |
| 21 | if (typeInfo.baseType === 'storage') { |
| 22 | if (defaultValue !== null && defaultValue !== undefined) { |
| 23 | hooksObj.storageUniforms[name] = defaultValue; |
| 24 | } |
| 25 | } else { |
| 26 | const key = backend.generateHookUniformKey(name, typeInfo); |
| 27 | if (key !== null) { |
| 28 | hooksObj.uniforms[key] = defaultValue; |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Add texture bindings to declarations for WebGPU backend |
| 34 | if (backend.addTextureBindingsToDeclarations) { |
| 35 | backend.addTextureBindingsToDeclarations(strandsContext); |
| 36 | } |
| 37 | |
| 38 | // Add storage buffer bindings to declarations for WebGPU backend |
| 39 | if (backend.addStorageBufferBindingsToDeclarations) { |
| 40 | backend.addStorageBufferBindingsToDeclarations(strandsContext); |
| 41 | } |
| 42 | |
| 43 | for (const { hookType, rootNodeID, entryBlockID, shaderContext } of strandsContext.hooks) { |
| 44 | const generationContext = { |
| 45 | indent: 1, |
| 46 | codeLines: [], |
| 47 | write(line) { |
| 48 | this.codeLines.push(' '.repeat(this.indent) + line); |
| 49 | }, |
| 50 | tempNames: {}, |
| 51 | declarations: [], |
| 52 | nextTempID: 0, |
| 53 | visitedNodes: new Set(), |
| 54 | shaderContext, // 'vertex' or 'fragment' |
| 55 | strandsContext, // For shared variable tracking |
| 56 | }; |
| 57 | |
| 58 | const blocks = sortCFG(cfg.outgoingEdges, entryBlockID); |
| 59 | for (const blockID of blocks) { |
| 60 | backend.generateBlock(blockID, strandsContext, generationContext); |
| 61 | } |
| 62 | |