ebiten/internal/shaderir/glsl.go

232 lines
6.8 KiB
Go
Raw Normal View History

2020-05-11 17:19:42 +02:00
// Copyright 2020 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shaderir
import (
"fmt"
"strings"
)
func (p *Program) structName(t *Type) string {
2020-05-12 16:32:32 +02:00
if t.Main != Struct {
2020-05-11 17:19:42 +02:00
panic("shaderir: the given type at structName must be a struct")
}
s := t.serialize()
if n, ok := p.structNames[s]; ok {
return n
}
n := fmt.Sprintf("S%d", len(p.structNames))
p.structNames[s] = n
p.structTypes = append(p.structTypes, *t)
return n
}
func (p *Program) Glsl() string {
p.structNames = map[string]string{}
p.structTypes = nil
var lines []string
2020-05-14 16:47:15 +02:00
for i, t := range p.Uniforms {
lines = append(lines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&t, fmt.Sprintf("U%d", i))))
2020-05-11 17:19:42 +02:00
}
2020-05-14 16:47:15 +02:00
for i, t := range p.Attributes {
lines = append(lines, fmt.Sprintf("attribute %s;", p.glslVarDecl(&t, fmt.Sprintf("A%d", i))))
2020-05-12 16:32:32 +02:00
}
2020-05-14 16:47:15 +02:00
for i, t := range p.Varyings {
lines = append(lines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i))))
2020-05-12 16:32:32 +02:00
}
for _, f := range p.Funcs {
lines = append(lines, p.glslFunc(&f)...)
}
2020-05-11 17:19:42 +02:00
var stLines []string
for i, t := range p.structTypes {
stLines = append(stLines, fmt.Sprintf("struct S%d {", i))
2020-05-12 16:32:32 +02:00
for j, st := range t.Sub {
2020-05-11 17:19:42 +02:00
stLines = append(stLines, fmt.Sprintf("\t%s;", p.glslVarDecl(&st, fmt.Sprintf("M%d", j))))
}
stLines = append(stLines, "};")
}
lines = append(stLines, lines...)
return strings.Join(lines, "\n") + "\n"
}
func (p *Program) glslVarDecl(t *Type, varname string) string {
2020-05-12 16:32:32 +02:00
switch t.Main {
2020-05-11 17:19:42 +02:00
case None:
return "?(none)"
case Image2D:
panic("not implemented")
case Array:
2020-05-12 16:32:32 +02:00
panic("not implemented")
2020-05-11 17:19:42 +02:00
case Struct:
return fmt.Sprintf("%s %s", p.structName(t), varname)
default:
2020-05-12 16:32:32 +02:00
return fmt.Sprintf("%s %s", t.Main.Glsl(), varname)
}
}
func (p *Program) glslFunc(f *Func) []string {
2020-05-13 16:31:17 +02:00
var args []string
var idx int
for _, t := range f.InParams {
args = append(args, "in "+p.glslVarDecl(&t, fmt.Sprintf("l%d", idx)))
idx++
}
for _, t := range f.InOutParams {
args = append(args, "inout "+p.glslVarDecl(&t, fmt.Sprintf("l%d", idx)))
idx++
}
for _, t := range f.OutParams {
args = append(args, "out "+p.glslVarDecl(&t, fmt.Sprintf("l%d", idx)))
idx++
}
argsstr := "void"
if len(args) > 0 {
argsstr = strings.Join(args, ", ")
}
2020-05-13 17:46:36 +02:00
var lines []string
lines = append(lines, fmt.Sprintf("void %s(%s) {", f.Name, argsstr))
lines = append(lines, p.glslBlock(&f.Block, f, 0, idx)...)
2020-05-13 17:46:36 +02:00
lines = append(lines, "}")
return lines
}
func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []string {
2020-05-13 17:46:36 +02:00
idt := strings.Repeat("\t", level+1)
var lines []string
for _, t := range b.LocalVars {
lines = append(lines, fmt.Sprintf("%s%s;", idt, p.glslVarDecl(&t, fmt.Sprintf("l%d", localVarIndex))))
localVarIndex++
2020-05-11 17:19:42 +02:00
}
2020-05-13 18:45:33 +02:00
var glslExpr func(e *Expr) string
glslExpr = func(e *Expr) string {
switch e.Type {
2020-05-15 20:40:33 +02:00
case IntExpr:
return fmt.Sprintf("%d", e.Int)
case FloatExpr:
return fmt.Sprintf("%.9e", e.Float)
2020-05-14 16:47:15 +02:00
case VarName:
switch e.Variable.Type {
case Uniform:
return fmt.Sprintf("U%d", e.Variable.Index)
case Attribute:
return fmt.Sprintf("A%d", e.Variable.Index)
case Varying:
return fmt.Sprintf("V%d", e.Variable.Index)
case Local:
return fmt.Sprintf("l%d", e.Variable.Index)
default:
return fmt.Sprintf("?(unexpected variable type: %d)", e.Variable.Type)
}
2020-05-13 18:45:33 +02:00
case Ident:
2020-05-14 19:10:07 +02:00
return e.Ident
2020-05-13 18:45:33 +02:00
case Unary:
2020-05-16 10:22:17 +02:00
var op string
switch e.Op {
case Add, Sub, Neg:
op = string(e.Op)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(e.Op))
}
return fmt.Sprintf("%s(%s)", op, glslExpr(&e.Exprs[0]))
2020-05-13 18:45:33 +02:00
case Binary:
return fmt.Sprintf("(%s) %s (%s)", glslExpr(&e.Exprs[0]), e.Op, glslExpr(&e.Exprs[1]))
2020-05-16 10:22:17 +02:00
case Selection:
return fmt.Sprintf("(%s) ? (%s) : (%s)", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]), glslExpr(&e.Exprs[2]))
2020-05-13 18:45:33 +02:00
case Call:
2020-05-16 12:10:12 +02:00
var args []string
for _, exp := range e.Exprs {
args = append(args, glslExpr(&exp))
}
return fmt.Sprintf("%s(%s)", e.Ident, strings.Join(args, ", "))
2020-05-16 10:22:17 +02:00
case FieldSelector:
2020-05-13 18:45:33 +02:00
return fmt.Sprintf("(%s).%s", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]))
case Index:
return fmt.Sprintf("(%s)[%s]", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]))
default:
return fmt.Sprintf("?(unexpected expr: %d)", e.Type)
}
}
for _, s := range b.Stmts {
switch s.Type {
case ExprStmt:
2020-05-13 20:14:39 +02:00
lines = append(lines, fmt.Sprintf("%s%s;", idt, glslExpr(&s.Exprs[0])))
2020-05-13 18:45:33 +02:00
case BlockStmt:
lines = append(lines, idt+"{")
2020-05-14 20:12:23 +02:00
lines = append(lines, p.glslBlock(&s.Blocks[0], f, level+1, localVarIndex)...)
2020-05-13 18:45:33 +02:00
lines = append(lines, idt+"}")
case Assign:
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, glslExpr(&s.Exprs[0]), glslExpr(&s.Exprs[1])))
case If:
2020-05-14 19:10:07 +02:00
lines = append(lines, fmt.Sprintf("%sif (%s) {", idt, glslExpr(&s.Exprs[0])))
2020-05-14 20:12:23 +02:00
lines = append(lines, p.glslBlock(&s.Blocks[0], f, level+1, localVarIndex)...)
if len(s.Blocks) > 1 {
2020-05-14 19:10:07 +02:00
lines = append(lines, fmt.Sprintf("%s} else {", idt))
2020-05-14 20:12:23 +02:00
lines = append(lines, p.glslBlock(&s.Blocks[1], f, level+1, localVarIndex)...)
2020-05-14 19:10:07 +02:00
}
lines = append(lines, fmt.Sprintf("%s}", idt))
2020-05-13 18:45:33 +02:00
case For:
2020-05-15 20:10:03 +02:00
v := localVarIndex
localVarIndex++
var delta string
switch s.ForDelta {
case 0:
delta = fmt.Sprintf("?(unexpected delta: %d)", s.ForDelta)
2020-05-15 20:10:03 +02:00
case 1:
delta = fmt.Sprintf("l%d++", v)
case -1:
delta = fmt.Sprintf("l%d--", v)
default:
if s.ForDelta > 0 {
delta = fmt.Sprintf("l%d += %d", v, s.ForDelta)
} else {
delta = fmt.Sprintf("l%d -= %d", v, -s.ForDelta)
}
2020-05-15 20:10:03 +02:00
}
var op string
switch s.ForOp {
case LessThan, LessEqual, GreaterThan, GreaterEqual, Equal, NotEqual:
op = string(s.ForOp)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(s.ForOp))
}
lines = append(lines, fmt.Sprintf("%sfor (int l%d = %d; l%d %s %d; %s) {", idt, v, s.ForInit, v, op, s.ForEnd, delta))
2020-05-15 20:10:03 +02:00
lines = append(lines, p.glslBlock(&s.Blocks[0], f, level+1, localVarIndex)...)
lines = append(lines, fmt.Sprintf("%s}", idt))
2020-05-13 18:45:33 +02:00
case Continue:
lines = append(lines, idt+"continue;")
case Break:
lines = append(lines, idt+"break;")
2020-05-13 20:14:39 +02:00
case Return:
lines = append(lines, idt+"return;")
2020-05-13 18:45:33 +02:00
case Discard:
lines = append(lines, idt+"discard;")
default:
lines = append(lines, fmt.Sprintf("%s?(unexpected stmt: %d)", idt, s.Type))
}
}
2020-05-13 17:46:36 +02:00
return lines
2020-05-11 17:19:42 +02:00
}
2020-05-13 20:14:39 +02:00
// TODO: Distinguish regular functions, vertex functions and fragment functions. e.g., Treat gl_Position correctly.