ebiten/internal/shaderir/glsl/glsl.go

506 lines
15 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.
2020-08-03 16:21:09 +02:00
package glsl
2020-05-11 17:19:42 +02:00
import (
"fmt"
"go/constant"
"go/token"
2020-08-05 18:38:06 +02:00
"regexp"
2020-05-11 17:19:42 +02:00
"strings"
2020-08-03 16:21:09 +02:00
"github.com/hajimehoshi/ebiten/internal/shaderir"
2020-05-11 17:19:42 +02:00
)
2020-08-03 16:21:09 +02:00
const FragmentPrelude = `#if defined(GL_ES)
2020-08-01 11:19:06 +02:00
precision highp float;
#else
#define lowp
#define mediump
#define highp
2020-08-01 17:09:12 +02:00
#endif`
2020-08-01 11:19:06 +02:00
2020-08-03 16:21:09 +02:00
type compileContext struct {
structNames map[string]string
structTypes []shaderir.Type
}
func (c *compileContext) structName(p *shaderir.Program, t *shaderir.Type) string {
if t.Main != shaderir.Struct {
2020-08-04 18:50:41 +02:00
panic("glsl: the given type at structName must be a struct")
2020-05-11 17:19:42 +02:00
}
2020-08-03 16:21:09 +02:00
s := t.String()
if n, ok := c.structNames[s]; ok {
2020-05-11 17:19:42 +02:00
return n
}
2020-08-03 16:21:09 +02:00
n := fmt.Sprintf("S%d", len(c.structNames))
c.structNames[s] = n
c.structTypes = append(c.structTypes, *t)
2020-05-11 17:19:42 +02:00
return n
}
2020-08-03 16:21:09 +02:00
func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
c := &compileContext{
structNames: map[string]string{},
}
2020-05-11 17:19:42 +02:00
2020-05-16 15:18:58 +02:00
// Vertex func
var vslines []string
{
2020-08-05 18:38:06 +02:00
vslines = append(vslines, "{{.Structs}}")
2020-08-01 17:09:12 +02:00
if len(p.Uniforms) > 0 || p.TextureNum > 0 || len(p.Attributes) > 0 || len(p.Varyings) > 0 {
2020-08-05 18:38:06 +02:00
vslines = append(vslines, "")
2020-08-01 17:09:12 +02:00
for i, t := range p.Uniforms {
2020-08-03 16:21:09 +02:00
vslines = append(vslines, fmt.Sprintf("uniform %s;", c.glslVarDecl(p, &t, fmt.Sprintf("U%d", i))))
2020-08-01 17:09:12 +02:00
}
for i := 0; i < p.TextureNum; i++ {
vslines = append(vslines, fmt.Sprintf("uniform sampler2D T%d;", i))
}
for i, t := range p.Attributes {
2020-08-03 16:21:09 +02:00
vslines = append(vslines, fmt.Sprintf("attribute %s;", c.glslVarDecl(p, &t, fmt.Sprintf("A%d", i))))
2020-08-01 17:09:12 +02:00
}
for i, t := range p.Varyings {
2020-08-03 16:21:09 +02:00
vslines = append(vslines, fmt.Sprintf("varying %s;", c.glslVarDecl(p, &t, fmt.Sprintf("V%d", i))))
2020-08-01 17:09:12 +02:00
}
}
if len(p.Funcs) > 0 {
2020-08-05 18:38:06 +02:00
vslines = append(vslines, "")
2020-08-01 11:19:06 +02:00
for _, f := range p.Funcs {
2020-08-03 16:21:09 +02:00
vslines = append(vslines, c.glslFunc(p, &f, true)...)
2020-08-01 11:19:06 +02:00
}
for _, f := range p.Funcs {
if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
vslines = append(vslines, "")
}
2020-08-03 16:21:09 +02:00
vslines = append(vslines, c.glslFunc(p, &f, false)...)
}
}
2020-08-09 08:55:59 +02:00
if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 {
2020-08-05 18:38:06 +02:00
vslines = append(vslines, "")
vslines = append(vslines, "void main(void) {")
2020-08-09 08:55:59 +02:00
vslines = append(vslines, c.glslBlock(p, p.VertexFunc.Block, 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
var fslines []string
{
2020-08-05 18:38:06 +02:00
fslines = append(fslines, strings.Split(FragmentPrelude, "\n")...)
fslines = append(fslines, "", "{{.Structs}}")
2020-08-01 17:09:12 +02:00
if len(p.Uniforms) > 0 || p.TextureNum > 0 || len(p.Varyings) > 0 {
2020-08-05 18:38:06 +02:00
fslines = append(fslines, "")
2020-08-01 17:09:12 +02:00
for i, t := range p.Uniforms {
2020-08-03 16:21:09 +02:00
fslines = append(fslines, fmt.Sprintf("uniform %s;", c.glslVarDecl(p, &t, fmt.Sprintf("U%d", i))))
2020-08-01 17:09:12 +02:00
}
for i := 0; i < p.TextureNum; i++ {
fslines = append(fslines, fmt.Sprintf("uniform sampler2D T%d;", i))
}
for i, t := range p.Varyings {
2020-08-03 16:21:09 +02:00
fslines = append(fslines, fmt.Sprintf("varying %s;", c.glslVarDecl(p, &t, fmt.Sprintf("V%d", i))))
2020-08-01 17:09:12 +02:00
}
}
2020-08-01 11:19:06 +02:00
if len(p.Funcs) > 0 {
2020-08-05 18:38:06 +02:00
fslines = append(fslines, "")
2020-08-01 11:19:06 +02:00
for _, f := range p.Funcs {
2020-08-03 16:21:09 +02:00
fslines = append(fslines, c.glslFunc(p, &f, true)...)
2020-08-01 11:19:06 +02:00
}
for _, f := range p.Funcs {
if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
fslines = append(fslines, "")
}
2020-08-03 16:21:09 +02:00
fslines = append(fslines, c.glslFunc(p, &f, false)...)
2020-08-01 11:19:06 +02:00
}
}
2020-08-09 08:55:59 +02:00
if p.FragmentFunc.Block != nil && len(p.FragmentFunc.Block.Stmts) > 0 {
2020-08-05 18:38:06 +02:00
fslines = append(fslines, "")
fslines = append(fslines, "void main(void) {")
2020-08-09 08:55:59 +02:00
fslines = append(fslines, c.glslBlock(p, p.FragmentFunc.Block, p.FragmentFunc.Block, 0, 0)...)
fslines = append(fslines, "}")
}
2020-05-16 16:07:24 +02:00
}
2020-08-05 18:38:06 +02:00
vs := strings.Join(vslines, "\n")
fs := strings.Join(fslines, "\n")
2020-08-01 17:09:12 +02:00
// Struct types are determined after converting the program.
2020-08-03 16:21:09 +02:00
if len(c.structTypes) > 0 {
var stlines []string
2020-08-03 16:21:09 +02:00
for i, t := range c.structTypes {
2020-08-01 17:09:12 +02:00
stlines = append(stlines, fmt.Sprintf("struct S%d {", i))
for j, st := range t.Sub {
2020-08-03 16:21:09 +02:00
stlines = append(stlines, fmt.Sprintf("\t%s;", c.glslVarDecl(p, &st, fmt.Sprintf("M%d", j))))
2020-08-01 17:09:12 +02:00
}
stlines = append(stlines, "};")
2020-05-11 17:19:42 +02:00
}
2020-08-05 18:38:06 +02:00
st := strings.Join(stlines, "\n")
vs = strings.ReplaceAll(vs, "{{.Structs}}", st)
fs = strings.ReplaceAll(fs, "{{.Structs}}", st)
} else {
vs = strings.ReplaceAll(vs, "{{.Structs}}", "")
fs = strings.ReplaceAll(fs, "{{.Structs}}", "")
2020-08-01 17:09:12 +02:00
}
2020-08-01 11:19:06 +02:00
2020-08-05 18:38:06 +02:00
nls := regexp.MustCompile(`\n\n+`)
vs = nls.ReplaceAllString(vs, "\n\n")
fs = nls.ReplaceAllString(fs, "\n\n")
2020-08-05 18:38:06 +02:00
vs = strings.TrimSpace(vs) + "\n"
fs = strings.TrimSpace(fs) + "\n"
2020-08-01 11:19:06 +02:00
2020-08-05 18:38:06 +02:00
return vs, fs
2020-05-11 17:19:42 +02:00
}
2020-08-03 16:21:09 +02:00
func (c *compileContext) glslType(p *shaderir.Program, t *shaderir.Type) (string, string) {
2020-05-16 13:16:04 +02:00
switch t.Main {
2020-08-03 16:21:09 +02:00
case shaderir.None:
return "void", ""
2020-08-03 16:21:09 +02:00
case shaderir.Struct:
return c.structName(p, t), ""
2020-05-16 13:16:04 +02:00
default:
2020-08-03 16:21:09 +02:00
return typeString(t)
2020-05-16 13:16:04 +02:00
}
}
2020-08-03 16:21:09 +02:00
func (c *compileContext) glslVarDecl(p *shaderir.Program, t *shaderir.Type, varname string) string {
2020-05-12 16:32:32 +02:00
switch t.Main {
2020-08-03 16:21:09 +02:00
case shaderir.None:
2020-05-11 17:19:42 +02:00
return "?(none)"
2020-08-03 16:21:09 +02:00
case shaderir.Struct:
return fmt.Sprintf("%s %s", c.structName(p, t), varname)
2020-05-11 17:19:42 +02:00
default:
2020-08-03 16:21:09 +02:00
t0, t1 := typeString(t)
return fmt.Sprintf("%s %s%s", t0, varname, t1)
2020-05-12 16:32:32 +02:00
}
}
2020-08-03 16:21:09 +02:00
func (c *compileContext) glslVarInit(p *shaderir.Program, t *shaderir.Type) string {
2020-05-31 16:57:03 +02:00
switch t.Main {
2020-08-03 16:21:09 +02:00
case shaderir.None:
2020-05-31 16:57:03 +02:00
return "?(none)"
2020-08-03 16:21:09 +02:00
case shaderir.Array:
init := c.glslVarInit(p, &t.Sub[0])
es := make([]string, 0, t.Length)
for i := 0; i < t.Length; i++ {
es = append(es, init)
}
2020-08-03 16:21:09 +02:00
t0, t1 := typeString(t)
return fmt.Sprintf("%s%s(%s)", t0, t1, strings.Join(es, ", "))
2020-08-03 16:21:09 +02:00
case shaderir.Struct:
2020-05-31 16:57:03 +02:00
panic("not implemented")
2020-08-03 16:21:09 +02:00
case shaderir.Bool:
2020-05-31 16:57:03 +02:00
return "false"
2020-08-03 16:21:09 +02:00
case shaderir.Int:
2020-05-31 16:57:03 +02:00
return "0"
2020-08-06 18:53:02 +02:00
case shaderir.Float, shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
return fmt.Sprintf("%s(0)", basicTypeString(t.Main))
2020-05-31 16:57:03 +02:00
default:
2020-08-03 16:21:09 +02:00
t0, t1 := c.glslType(p, t)
panic(fmt.Sprintf("?(unexpected type: %s%s)", t0, t1))
2020-05-31 16:57:03 +02:00
}
}
2020-08-03 16:21:09 +02:00
func (c *compileContext) glslFunc(p *shaderir.Program, f *shaderir.Func, prototype bool) []string {
2020-05-13 16:31:17 +02:00
var args []string
var idx int
for _, t := range f.InParams {
2020-08-03 16:21:09 +02:00
args = append(args, "in "+c.glslVarDecl(p, &t, fmt.Sprintf("l%d", idx)))
2020-05-13 16:31:17 +02:00
idx++
}
for _, t := range f.OutParams {
2020-08-03 16:21:09 +02:00
args = append(args, "out "+c.glslVarDecl(p, &t, fmt.Sprintf("l%d", idx)))
2020-05-13 16:31:17 +02:00
idx++
}
argsstr := "void"
if len(args) > 0 {
argsstr = strings.Join(args, ", ")
}
2020-08-03 16:21:09 +02:00
t0, t1 := c.glslType(p, &f.Return)
sig := fmt.Sprintf("%s%s F%d(%s)", t0, t1, f.Index, argsstr)
2020-05-13 17:46:36 +02:00
var lines []string
if prototype {
lines = append(lines, fmt.Sprintf("%s;", sig))
return lines
}
lines = append(lines, fmt.Sprintf("%s {", sig))
2020-08-09 08:55:59 +02:00
lines = append(lines, c.glslBlock(p, f.Block, f.Block, 0, idx)...)
2020-05-13 17:46:36 +02:00
lines = append(lines, "}")
return lines
}
2020-08-03 16:21:09 +02:00
func constantToNumberLiteral(t shaderir.ConstType, v constant.Value) string {
switch t {
2020-08-03 16:21:09 +02:00
case shaderir.ConstTypeNone:
if v.Kind() == constant.Bool {
if constant.BoolVal(v) {
return "true"
}
return "false"
}
fallthrough
2020-08-03 16:21:09 +02:00
case shaderir.ConstTypeFloat:
if i := constant.ToInt(v); i.Kind() == constant.Int {
x, _ := constant.Int64Val(i)
return fmt.Sprintf("%d.0", x)
}
if i := constant.ToFloat(v); i.Kind() == constant.Float {
x, _ := constant.Float64Val(i)
return fmt.Sprintf("%.9e", x)
}
2020-08-03 16:21:09 +02:00
case shaderir.ConstTypeInt:
if i := constant.ToInt(v); i.Kind() == constant.Int {
x, _ := constant.Int64Val(i)
return fmt.Sprintf("%d", x)
}
}
return fmt.Sprintf("?(unexpected literal: %s)", v)
}
func descendantLocalVars(block, target *shaderir.Block) ([]shaderir.Type, bool) {
if block == target {
return block.LocalVars, true
}
var ts []shaderir.Type
for _, s := range block.Stmts {
for _, b := range s.Blocks {
if ts2, found := descendantLocalVars(b, target); found {
ts = append(ts, block.LocalVars...)
ts = append(ts, ts2...)
return ts, true
}
}
}
return nil, false
}
func localVariableType(p *shaderir.Program, topBlock, block *shaderir.Block, absidx int) shaderir.Type {
var ts []shaderir.Type
for _, f := range p.Funcs {
if f.Block == topBlock {
ts = append(f.InParams, f.OutParams...)
break
}
}
ts2, _ := descendantLocalVars(topBlock, block)
ts = append(ts, ts2...)
return ts[absidx]
}
func localVariable(p *shaderir.Program, topBlock, block *shaderir.Block, idx int) (string, shaderir.Type) {
switch topBlock {
2020-08-09 08:55:59 +02:00
case p.VertexFunc.Block:
na := len(p.Attributes)
nv := len(p.Varyings)
switch {
case idx < na:
return fmt.Sprintf("A%d", idx), p.Attributes[idx]
case idx == na:
return "gl_Position", shaderir.Type{Main: shaderir.Vec4}
case idx < na+nv+1:
return fmt.Sprintf("V%d", idx-na-1), p.Varyings[idx-na-1]
default:
return fmt.Sprintf("l%d", idx-(na+nv+1)), localVariableType(p, topBlock, block, idx-(na+nv+1))
}
2020-08-09 08:55:59 +02:00
case p.FragmentFunc.Block:
nv := len(p.Varyings)
switch {
case idx == 0:
return "gl_FragCoord", shaderir.Type{Main: shaderir.Vec4}
case idx < nv+1:
return fmt.Sprintf("V%d", idx-1), p.Varyings[idx-1]
case idx == nv+1:
return "gl_FragColor", shaderir.Type{Main: shaderir.Vec4}
default:
return fmt.Sprintf("l%d", idx-(nv+2)), localVariableType(p, topBlock, block, idx-(nv+2))
}
default:
return fmt.Sprintf("l%d", idx), localVariableType(p, topBlock, block, idx)
}
}
2020-08-03 16:21:09 +02:00
func (c *compileContext) glslBlock(p *shaderir.Program, topBlock, block *shaderir.Block, level int, localVarIndex int) []string {
2020-08-09 08:55:59 +02:00
if block == nil {
return nil
}
2020-05-13 17:46:36 +02:00
idt := strings.Repeat("\t", level+1)
var lines []string
for _, t := range block.LocalVars {
name := fmt.Sprintf("l%d", localVarIndex)
switch t.Main {
case shaderir.Array:
lines = append(lines, fmt.Sprintf("%s%s;", idt, c.glslVarDecl(p, &t, name)))
init := c.glslVarInit(p, &t.Sub[0])
for i := 0; i < t.Length; i++ {
lines = append(lines, fmt.Sprintf("%s%s[%d] = %s;", idt, name, i, init))
}
case shaderir.None:
// The type is None e.g., when the variable is a for-loop counter.
default:
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, c.glslVarDecl(p, &t, name), c.glslVarInit(p, &t)))
}
localVarIndex++
2020-05-11 17:19:42 +02:00
}
2020-05-13 18:45:33 +02:00
2020-08-03 16:21:09 +02:00
var glslExpr func(e *shaderir.Expr) string
glslExpr = func(e *shaderir.Expr) string {
2020-05-13 18:45:33 +02:00
switch e.Type {
2020-08-03 16:21:09 +02:00
case shaderir.NumberExpr:
return constantToNumberLiteral(e.ConstType, e.Const)
2020-08-03 16:21:09 +02:00
case shaderir.UniformVariable:
return fmt.Sprintf("U%d", e.Index)
2020-08-03 16:21:09 +02:00
case shaderir.TextureVariable:
return fmt.Sprintf("T%d", e.Index)
2020-08-03 16:21:09 +02:00
case shaderir.LocalVariable:
n, _ := localVariable(p, topBlock, block, e.Index)
return n
2020-08-03 16:21:09 +02:00
case shaderir.StructMember:
return fmt.Sprintf("M%d", e.Index)
2020-08-03 16:21:09 +02:00
case shaderir.BuiltinFuncExpr:
return builtinFuncString(e.BuiltinFunc)
case shaderir.SwizzlingExpr:
if !shaderir.IsValidSwizzling(e.Swizzling) {
2020-05-16 20:00:57 +02:00
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
2020-08-03 16:21:09 +02:00
case shaderir.FunctionExpr:
2020-05-16 20:00:57 +02:00
return fmt.Sprintf("F%d", e.Index)
2020-08-03 16:21:09 +02:00
case shaderir.Unary:
2020-05-16 10:22:17 +02:00
var op string
switch e.Op {
2020-08-03 16:21:09 +02:00
case shaderir.Add, shaderir.Sub, shaderir.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-08-03 16:21:09 +02:00
case shaderir.Binary:
2020-05-13 18:45:33 +02:00
return fmt.Sprintf("(%s) %s (%s)", glslExpr(&e.Exprs[0]), e.Op, glslExpr(&e.Exprs[1]))
2020-08-03 16:21:09 +02:00
case shaderir.Selection:
2020-05-16 10:22:17 +02:00
return fmt.Sprintf("(%s) ? (%s) : (%s)", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]), glslExpr(&e.Exprs[2]))
2020-08-03 16:21:09 +02:00
case shaderir.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))
}
// Using parentheses at the callee is illegal.
return fmt.Sprintf("%s(%s)", glslExpr(&e.Exprs[0]), strings.Join(args, ", "))
2020-08-03 16:21:09 +02:00
case shaderir.FieldSelector:
2020-05-13 18:45:33 +02:00
return fmt.Sprintf("(%s).%s", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]))
2020-08-03 16:21:09 +02:00
case shaderir.Index:
2020-05-13 18:45:33 +02:00
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 block.Stmts {
2020-05-13 18:45:33 +02:00
switch s.Type {
2020-08-03 16:21:09 +02:00
case shaderir.ExprStmt:
2020-05-13 20:14:39 +02:00
lines = append(lines, fmt.Sprintf("%s%s;", idt, glslExpr(&s.Exprs[0])))
2020-08-03 16:21:09 +02:00
case shaderir.BlockStmt:
2020-05-13 18:45:33 +02:00
lines = append(lines, idt+"{")
2020-08-09 08:55:59 +02:00
lines = append(lines, c.glslBlock(p, topBlock, s.Blocks[0], level+1, localVarIndex)...)
2020-05-13 18:45:33 +02:00
lines = append(lines, idt+"}")
2020-08-03 16:21:09 +02:00
case shaderir.Assign:
lhs := s.Exprs[0]
rhs := s.Exprs[1]
if lhs.Type == shaderir.LocalVariable {
if _, t := localVariable(p, topBlock, block, lhs.Index); t.Main == shaderir.Array {
for i := 0; i < t.Length; i++ {
lines = append(lines, fmt.Sprintf("%[1]s%[2]s[%[3]d] = %[4]s[%[3]d];", idt, glslExpr(&lhs), i, glslExpr(&rhs)))
}
continue
}
}
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, glslExpr(&lhs), glslExpr(&rhs)))
2020-08-03 16:21:09 +02:00
case shaderir.If:
2020-05-14 19:10:07 +02:00
lines = append(lines, fmt.Sprintf("%sif (%s) {", idt, glslExpr(&s.Exprs[0])))
2020-08-09 08:55:59 +02:00
lines = append(lines, c.glslBlock(p, topBlock, 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-08-09 08:55:59 +02:00
lines = append(lines, c.glslBlock(p, topBlock, s.Blocks[1], level+1, localVarIndex)...)
2020-05-14 19:10:07 +02:00
}
lines = append(lines, fmt.Sprintf("%s}", idt))
2020-08-03 16:21:09 +02:00
case shaderir.For:
var ct shaderir.ConstType
switch s.ForVarType.Main {
2020-08-03 16:21:09 +02:00
case shaderir.Int:
ct = shaderir.ConstTypeInt
case shaderir.Float:
ct = shaderir.ConstTypeFloat
}
v, _ := localVariable(p, topBlock, block, s.ForVarIndex)
2020-05-15 20:10:03 +02:00
var delta string
switch val, _ := constant.Float64Val(s.ForDelta); val {
case 0:
delta = fmt.Sprintf("?(unexpected delta: %v)", s.ForDelta)
2020-05-15 20:10:03 +02:00
case 1:
delta = fmt.Sprintf("%s++", v)
2020-05-15 20:10:03 +02:00
case -1:
delta = fmt.Sprintf("%s--", v)
2020-05-15 20:10:03 +02:00
default:
d := s.ForDelta
if val > 0 {
delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(ct, d))
} else {
d = constant.UnaryOp(token.SUB, d, 0)
delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(ct, d))
}
2020-05-15 20:10:03 +02:00
}
var op string
switch s.ForOp {
2020-08-03 16:21:09 +02:00
case shaderir.LessThanOp, shaderir.LessThanEqualOp, shaderir.GreaterThanOp, shaderir.GreaterThanEqualOp, shaderir.EqualOp, shaderir.NotEqualOp:
op = string(s.ForOp)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(s.ForOp))
}
t := s.ForVarType
init := constantToNumberLiteral(ct, s.ForInit)
end := constantToNumberLiteral(ct, s.ForEnd)
2020-08-03 16:21:09 +02:00
t0, t1 := typeString(&t)
lines = append(lines, fmt.Sprintf("%sfor (%s %s%s = %s; %s %s %s; %s) {", idt, t0, v, t1, init, v, op, end, delta))
lines = append(lines, c.glslBlock(p, topBlock, s.Blocks[0], level+1, localVarIndex)...)
2020-05-15 20:10:03 +02:00
lines = append(lines, fmt.Sprintf("%s}", idt))
2020-08-03 16:21:09 +02:00
case shaderir.Continue:
2020-05-13 18:45:33 +02:00
lines = append(lines, idt+"continue;")
2020-08-03 16:21:09 +02:00
case shaderir.Break:
2020-05-13 18:45:33 +02:00
lines = append(lines, idt+"break;")
2020-08-03 16:21:09 +02:00
case shaderir.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-08-03 16:21:09 +02:00
case shaderir.Discard:
2020-05-13 18:45:33 +02:00
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
}