shaderir: Implement fragment function

This commit is contained in:
Hajime Hoshi 2020-05-16 23:07:24 +09:00
parent ddaed674dd
commit 66e76597d8
2 changed files with 95 additions and 3 deletions

View File

@ -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:

View File

@ -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;
}`,
},
}