diff --git a/internal/shaderir/glsl.go b/internal/shaderir/glsl.go index 399150c40..9ef4f6f3b 100644 --- a/internal/shaderir/glsl.go +++ b/internal/shaderir/glsl.go @@ -58,6 +58,13 @@ func (p *Program) Glsl() string { lines = append(lines, "}") } + // Fragment func + if len(p.FragmentFunc.Block.Stmts) > 0 { + lines = append(lines, "void main(void) {") + lines = append(lines, p.glslBlock(&p.FragmentFunc.Block, 0, 0)...) + lines = append(lines, "}") + } + var stLines []string for i, t := range p.structTypes { stLines = append(stLines, fmt.Sprintf("struct S%d {", i)) @@ -151,7 +158,8 @@ func (p *Program) glslBlock(b *Block, level int, localVarIndex int) []string { return fmt.Sprintf("U%d", e.Variable.Index) case Local: idx := e.Variable.Index - if b == &p.VertexFunc.Block { + switch b { + case &p.VertexFunc.Block: na := len(p.Attributes) nv := len(p.Varyings) switch { @@ -162,9 +170,19 @@ func (p *Program) glslBlock(b *Block, level int, localVarIndex int) []string { case idx == na+nv: return "gl_Position" default: - return fmt.Sprintf("l%d", idx-(na+nv)) + return fmt.Sprintf("l%d", idx-(na+nv+1)) } - } else { + case &p.FragmentFunc.Block: + nv := len(p.Varyings) + switch { + case idx < nv: + return fmt.Sprintf("V%d", idx) + case idx == nv: + return "gl_FragCoord" + default: + return fmt.Sprintf("l%d", idx-(nv+1)) + } + default: return fmt.Sprintf("l%d", idx) } default: diff --git a/internal/shaderir/ir_test.go b/internal/shaderir/ir_test.go index 422707d05..226911871 100644 --- a/internal/shaderir/ir_test.go +++ b/internal/shaderir/ir_test.go @@ -566,6 +566,80 @@ void main(void) { gl_Position = A0; V0 = A1; V1 = A2; +}`, + }, + { + Name: "FragmentFunc", + Program: Program{ + Uniforms: []Type{ + {Main: Float}, + }, + Attributes: []Type{ + {Main: Vec4}, + {Main: Float}, + {Main: Vec2}, + }, + Varyings: []Type{ + {Main: Float}, + {Main: Vec2}, + }, + VertexFunc: VertexFunc{ + Block: block( + nil, + assignStmt( + varNameExpr(Local, 5), + varNameExpr(Local, 0), + ), + assignStmt( + varNameExpr(Local, 3), + varNameExpr(Local, 1), + ), + assignStmt( + varNameExpr(Local, 4), + varNameExpr(Local, 2), + ), + ), + }, + FragmentFunc: FragmentFunc{ + Block: block( + []Type{ + {Main: Vec2}, + {Main: Vec4}, + {Main: Float}, + }, + assignStmt( + varNameExpr(Local, 5), + varNameExpr(Local, 0), + ), + assignStmt( + varNameExpr(Local, 3), + varNameExpr(Local, 1), + ), + assignStmt( + varNameExpr(Local, 4), + varNameExpr(Local, 2), + ), + ), + }, + }, + Glsl: `uniform float U0; +attribute vec4 A0; +attribute float A1; +attribute vec2 A2; +varying float V0; +varying vec2 V1; +void main(void) { + gl_Position = A0; + V0 = A1; + V1 = A2; +} +void main(void) { + vec2 l0; + vec4 l1; + float l2; + l2 = V0; + l0 = V1; + l1 = gl_FragCoord; }`, }, }