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"
|
|
|
|
)
|
|
|
|
|
2020-05-16 19:24:35 +02:00
|
|
|
func isValidSwizzling(s string) bool {
|
|
|
|
if len(s) < 1 || 4 < len(s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
xyzw = "xyzw"
|
|
|
|
rgba = "rgba"
|
|
|
|
strq = "strq"
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.IndexByte(xyzw, s[0]) >= 0:
|
|
|
|
for _, c := range s {
|
|
|
|
if strings.IndexRune(xyzw, c) == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 19:25:37 +02:00
|
|
|
return true
|
2020-05-16 19:24:35 +02:00
|
|
|
case strings.IndexByte(rgba, s[0]) >= 0:
|
|
|
|
for _, c := range s {
|
|
|
|
if strings.IndexRune(rgba, c) == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 19:25:37 +02:00
|
|
|
return true
|
2020-05-16 19:24:35 +02:00
|
|
|
case strings.IndexByte(strq, s[0]) >= 0:
|
|
|
|
for _, c := range s {
|
|
|
|
if strings.IndexRune(strq, c) == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 19:25:37 +02:00
|
|
|
return true
|
2020-05-16 19:24:35 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:19:42 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-21 16:58:08 +02:00
|
|
|
func (p *Program) Glsl() (vertexShader, fragmentShader string) {
|
2020-05-11 17:19:42 +02:00
|
|
|
p.structNames = map[string]string{}
|
|
|
|
p.structTypes = nil
|
|
|
|
|
2020-05-16 15:18:58 +02:00
|
|
|
// Vertex func
|
2020-05-21 16:58:08 +02:00
|
|
|
var vslines []string
|
2020-05-23 12:06:44 +02:00
|
|
|
{
|
|
|
|
for i, t := range p.Uniforms {
|
|
|
|
vslines = append(vslines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&t, fmt.Sprintf("U%d", i))))
|
|
|
|
}
|
|
|
|
for i, t := range p.Attributes {
|
|
|
|
vslines = append(vslines, fmt.Sprintf("attribute %s;", p.glslVarDecl(&t, fmt.Sprintf("A%d", i))))
|
|
|
|
}
|
|
|
|
for i, t := range p.Varyings {
|
|
|
|
vslines = append(vslines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i))))
|
|
|
|
}
|
2020-06-02 15:45:44 +02:00
|
|
|
if len(vslines) > 0 && len(p.Funcs) > 0 {
|
|
|
|
vslines = append(vslines, "")
|
|
|
|
}
|
2020-05-23 12:06:44 +02:00
|
|
|
for _, f := range p.Funcs {
|
|
|
|
vslines = append(vslines, p.glslFunc(&f)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.VertexFunc.Block.Stmts) > 0 {
|
|
|
|
vslines = append(vslines, "")
|
|
|
|
vslines = append(vslines, "void main(void) {")
|
|
|
|
vslines = append(vslines, p.glslBlock(&p.VertexFunc.Block, 0, 0)...)
|
|
|
|
vslines = append(vslines, "}")
|
|
|
|
}
|
2020-05-16 15:18:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:07:24 +02:00
|
|
|
// Fragment func
|
2020-05-21 16:58:08 +02:00
|
|
|
var fslines []string
|
2020-05-23 12:06:44 +02:00
|
|
|
{
|
|
|
|
for i, t := range p.Uniforms {
|
|
|
|
fslines = append(fslines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&t, fmt.Sprintf("U%d", i))))
|
|
|
|
}
|
|
|
|
for i, t := range p.Varyings {
|
|
|
|
fslines = append(fslines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i))))
|
|
|
|
}
|
|
|
|
for _, f := range p.Funcs {
|
|
|
|
fslines = append(fslines, p.glslFunc(&f)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.FragmentFunc.Block.Stmts) > 0 {
|
|
|
|
fslines = append(fslines, "")
|
|
|
|
fslines = append(fslines, "void main(void) {")
|
|
|
|
fslines = append(fslines, p.glslBlock(&p.FragmentFunc.Block, 0, 0)...)
|
|
|
|
fslines = append(fslines, "}")
|
|
|
|
}
|
2020-05-16 16:07:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 12:06:44 +02:00
|
|
|
var stlines []string
|
2020-05-11 17:19:42 +02:00
|
|
|
for i, t := range p.structTypes {
|
2020-05-23 12:06:44 +02:00
|
|
|
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-23 12:06:44 +02:00
|
|
|
stlines = append(stlines, fmt.Sprintf("\t%s;", p.glslVarDecl(&st, fmt.Sprintf("M%d", j))))
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
2020-05-23 12:06:44 +02:00
|
|
|
stlines = append(stlines, "};")
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 12:06:44 +02:00
|
|
|
vslines = append(stlines, vslines...)
|
|
|
|
tmp := make([]string, len(stlines))
|
|
|
|
copy(tmp, stlines)
|
2020-05-21 16:58:08 +02:00
|
|
|
fslines = append(tmp, fslines...)
|
|
|
|
|
|
|
|
return strings.Join(vslines, "\n") + "\n", strings.Join(fslines, "\n") + "\n"
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 13:16:04 +02:00
|
|
|
func (p *Program) glslType(t *Type) string {
|
|
|
|
switch t.Main {
|
|
|
|
case None:
|
|
|
|
return "void"
|
|
|
|
case Array:
|
|
|
|
panic("not implemented")
|
|
|
|
case Struct:
|
|
|
|
return p.structName(t)
|
|
|
|
default:
|
|
|
|
return t.Main.Glsl()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:19:42 +02:00
|
|
|
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 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 16:57:03 +02:00
|
|
|
func (p *Program) glslVarInit(t *Type) string {
|
|
|
|
switch t.Main {
|
|
|
|
case None:
|
|
|
|
return "?(none)"
|
|
|
|
case Array:
|
|
|
|
panic("not implemented")
|
|
|
|
case Struct:
|
|
|
|
panic("not implemented")
|
|
|
|
case Bool:
|
|
|
|
return "false"
|
|
|
|
case Int:
|
|
|
|
return "0"
|
|
|
|
case Float:
|
|
|
|
return "0.0"
|
|
|
|
case Vec2:
|
|
|
|
return "vec2(0.0)"
|
|
|
|
case Vec3:
|
|
|
|
return "vec3(0.0)"
|
|
|
|
case Vec4:
|
|
|
|
return "vec4(0.0)"
|
|
|
|
case Mat2:
|
|
|
|
return "mat2(0.0)"
|
|
|
|
case Mat3:
|
|
|
|
return "mat3(0.0)"
|
|
|
|
case Mat4:
|
|
|
|
return "mat4(0.0)"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("?(unexpected type: %s)", p.glslType(t)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 16:32:32 +02:00
|
|
|
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
|
2020-05-16 20:00:57 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%s F%d(%s) {", p.glslType(&f.Return), f.Index, argsstr))
|
2020-05-16 15:18:58 +02:00
|
|
|
lines = append(lines, p.glslBlock(&f.Block, 0, idx)...)
|
2020-05-13 17:46:36 +02:00
|
|
|
lines = append(lines, "}")
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2020-05-16 15:18:58 +02:00
|
|
|
func (p *Program) glslBlock(b *Block, 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 {
|
2020-05-31 16:57:03 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, p.glslVarDecl(&t, fmt.Sprintf("l%d", localVarIndex)), p.glslVarInit(&t)))
|
2020-05-13 20:52:36 +02:00
|
|
|
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:
|
2020-06-02 14:45:33 +02:00
|
|
|
// TODO: Cast to int if the context requries integers.
|
|
|
|
return fmt.Sprintf("%d.0", e.Int)
|
2020-05-15 20:40:33 +02:00
|
|
|
case FloatExpr:
|
|
|
|
return fmt.Sprintf("%.9e", e.Float)
|
2020-05-16 21:28:03 +02:00
|
|
|
case UniformVariable:
|
|
|
|
return fmt.Sprintf("U%d", e.Index)
|
|
|
|
case LocalVariable:
|
|
|
|
idx := e.Index
|
|
|
|
switch b {
|
|
|
|
case &p.VertexFunc.Block:
|
|
|
|
na := len(p.Attributes)
|
|
|
|
nv := len(p.Varyings)
|
|
|
|
switch {
|
|
|
|
case idx < na:
|
|
|
|
return fmt.Sprintf("A%d", idx)
|
2020-06-02 17:46:52 +02:00
|
|
|
case idx == na:
|
2020-05-16 21:28:03 +02:00
|
|
|
return "gl_Position"
|
2020-06-02 17:46:52 +02:00
|
|
|
case idx < na+nv+1:
|
|
|
|
return fmt.Sprintf("V%d", idx-na-1)
|
2020-05-16 16:07:24 +02:00
|
|
|
default:
|
2020-05-16 21:28:03 +02:00
|
|
|
return fmt.Sprintf("l%d", idx-(na+nv+1))
|
|
|
|
}
|
|
|
|
case &p.FragmentFunc.Block:
|
|
|
|
nv := len(p.Varyings)
|
|
|
|
switch {
|
2020-06-02 19:01:50 +02:00
|
|
|
case idx == 0:
|
2020-05-16 21:28:03 +02:00
|
|
|
return "gl_FragCoord"
|
2020-06-02 19:01:50 +02:00
|
|
|
case idx < nv+1:
|
|
|
|
return fmt.Sprintf("V%d", idx-1)
|
2020-05-23 11:18:22 +02:00
|
|
|
case idx == nv+1:
|
|
|
|
return "gl_FragColor"
|
2020-05-16 21:28:03 +02:00
|
|
|
default:
|
2020-05-23 11:18:22 +02:00
|
|
|
return fmt.Sprintf("l%d", idx-(nv+2))
|
2020-05-16 15:18:58 +02:00
|
|
|
}
|
2020-05-14 16:47:15 +02:00
|
|
|
default:
|
2020-05-16 21:28:03 +02:00
|
|
|
return fmt.Sprintf("l%d", idx)
|
2020-05-14 16:47:15 +02:00
|
|
|
}
|
2020-05-16 21:28:03 +02:00
|
|
|
case StructMember:
|
|
|
|
return fmt.Sprintf("M%d", e.Index)
|
2020-05-16 17:25:31 +02:00
|
|
|
case BuiltinFuncExpr:
|
|
|
|
return string(e.BuiltinFunc)
|
2020-05-16 19:24:35 +02:00
|
|
|
case SwizzlingExpr:
|
2020-05-16 20:00:57 +02:00
|
|
|
if !isValidSwizzling(e.Swizzling) {
|
|
|
|
return fmt.Sprintf("?(unexpected swizzling: %s)", e.Swizzling)
|
2020-05-16 19:24:35 +02:00
|
|
|
}
|
2020-05-16 20:00:57 +02:00
|
|
|
return e.Swizzling
|
|
|
|
case FunctionExpr:
|
|
|
|
return fmt.Sprintf("F%d", e.Index)
|
2020-05-13 18:45:33 +02:00
|
|
|
case Unary:
|
2020-05-16 10:22:17 +02:00
|
|
|
var op string
|
|
|
|
switch e.Op {
|
2020-06-02 15:45:44 +02:00
|
|
|
case Add, Sub, NotOp:
|
2020-05-16 10:22:17 +02:00
|
|
|
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
|
2020-05-16 17:25:31 +02:00
|
|
|
for _, exp := range e.Exprs[1:] {
|
2020-05-16 12:10:12 +02:00
|
|
|
args = append(args, glslExpr(&exp))
|
|
|
|
}
|
2020-05-23 11:06:50 +02:00
|
|
|
// Using parentheses at the callee is illegal.
|
|
|
|
return fmt.Sprintf("%s(%s)", glslExpr(&e.Exprs[0]), 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-16 15:18:58 +02:00
|
|
|
lines = append(lines, p.glslBlock(&s.Blocks[0], 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-16 15:18:58 +02:00
|
|
|
lines = append(lines, p.glslBlock(&s.Blocks[0], level+1, localVarIndex)...)
|
2020-05-14 20:12:23 +02:00
|
|
|
if len(s.Blocks) > 1 {
|
2020-05-14 19:10:07 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%s} else {", idt))
|
2020-05-16 15:18:58 +02:00
|
|
|
lines = append(lines, p.glslBlock(&s.Blocks[1], 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 {
|
2020-05-16 08:51:54 +02:00
|
|
|
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:
|
2020-05-16 08:51:54 +02:00
|
|
|
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
|
|
|
}
|
2020-05-16 08:51:54 +02:00
|
|
|
var op string
|
|
|
|
switch s.ForOp {
|
2020-05-16 17:25:31 +02:00
|
|
|
case LessThanOp, LessThanEqualOp, GreaterThanOp, GreaterThanEqualOp, EqualOp, NotEqualOp:
|
2020-05-16 08:51:54 +02:00
|
|
|
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-16 15:18:58 +02:00
|
|
|
lines = append(lines, p.glslBlock(&s.Blocks[0], level+1, localVarIndex)...)
|
2020-05-15 20:10:03 +02:00
|
|
|
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:
|
2020-05-16 13:16:04 +02:00
|
|
|
if len(s.Exprs) == 0 {
|
|
|
|
lines = append(lines, idt+"return;")
|
|
|
|
} else {
|
|
|
|
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, glslExpr(&s.Exprs[0])))
|
|
|
|
}
|
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
|
|
|
}
|