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"
|
2020-06-20 18:22:30 +02:00
|
|
|
"go/constant"
|
2020-07-12 15:22:22 +02:00
|
|
|
"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
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2020-05-11 17:19:42 +02:00
|
|
|
)
|
|
|
|
|
2020-10-26 02:33:11 +01:00
|
|
|
type GLSLVersion int
|
|
|
|
|
|
|
|
const (
|
|
|
|
GLSLVersionDefault GLSLVersion = iota
|
2020-11-21 11:40:07 +01:00
|
|
|
GLSLVersionES100
|
2020-10-26 02:33:11 +01:00
|
|
|
GLSLVersionES300
|
|
|
|
)
|
|
|
|
|
2022-01-11 17:41:06 +01:00
|
|
|
// utilFunctions is GLSL utility functions for old GLSL versions.
|
|
|
|
const utilFunctions = `int modInt(int x, int y) {
|
|
|
|
return x - y*(x/y);
|
2022-11-20 16:49:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ivec2 modInt(ivec2 x, int y) {
|
|
|
|
return x - y*(x/y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ivec3 modInt(ivec3 x, int y) {
|
|
|
|
return x - y*(x/y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ivec4 modInt(ivec4 x, int y) {
|
|
|
|
return x - y*(x/y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ivec2 modInt(ivec2 x, ivec2 y) {
|
|
|
|
return x - y*(x/y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ivec3 modInt(ivec3 x, ivec3 y) {
|
|
|
|
return x - y*(x/y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ivec4 modInt(ivec4 x, ivec4 y) {
|
|
|
|
return x - y*(x/y);
|
2022-01-11 17:41:06 +01:00
|
|
|
}`
|
|
|
|
|
2020-10-26 02:33:11 +01:00
|
|
|
func VertexPrelude(version GLSLVersion) string {
|
2022-01-11 17:41:06 +01:00
|
|
|
switch version {
|
|
|
|
case GLSLVersionDefault:
|
|
|
|
return utilFunctions
|
|
|
|
case GLSLVersionES100:
|
|
|
|
return utilFunctions
|
|
|
|
case GLSLVersionES300:
|
2020-10-26 02:33:11 +01:00
|
|
|
return `#version 300 es`
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func FragmentPrelude(version GLSLVersion) string {
|
|
|
|
var prefix string
|
|
|
|
switch version {
|
2020-11-21 11:40:07 +01:00
|
|
|
case GLSLVersionES100:
|
2020-10-26 02:33:11 +01:00
|
|
|
prefix = `#extension GL_OES_standard_derivatives : enable` + "\n\n"
|
|
|
|
case GLSLVersionES300:
|
|
|
|
prefix = `#version 300 es` + "\n\n"
|
|
|
|
}
|
2022-01-11 17:41:06 +01:00
|
|
|
prelude := prefix + `#if defined(GL_ES)
|
2020-08-01 11:19:06 +02:00
|
|
|
precision highp float;
|
2022-11-12 10:38:15 +01:00
|
|
|
precision highp int;
|
2020-08-01 11:19:06 +02:00
|
|
|
#else
|
|
|
|
#define lowp
|
|
|
|
#define mediump
|
|
|
|
#define highp
|
2020-08-01 17:09:12 +02:00
|
|
|
#endif`
|
2022-01-11 17:41:06 +01:00
|
|
|
if version == GLSLVersionDefault || version == GLSLVersionES100 {
|
|
|
|
prelude += "\n\n" + utilFunctions
|
|
|
|
}
|
|
|
|
return prelude
|
2020-10-26 02:33:11 +01:00
|
|
|
}
|
2020-08-01 11:19:06 +02:00
|
|
|
|
2020-08-03 16:21:09 +02:00
|
|
|
type compileContext struct {
|
2020-10-26 02:33:11 +01:00
|
|
|
version GLSLVersion
|
2020-08-03 16:21:09 +02:00
|
|
|
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-10-26 02:33:11 +01:00
|
|
|
func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentShader string) {
|
2022-08-16 18:01:23 +02:00
|
|
|
p = adjustProgram(p)
|
|
|
|
|
2020-08-03 16:21:09 +02:00
|
|
|
c := &compileContext{
|
2020-10-26 02:33:11 +01:00
|
|
|
version: version,
|
2020-08-03 16:21:09 +02:00
|
|
|
structNames: map[string]string{},
|
|
|
|
}
|
2020-05-11 17:19:42 +02:00
|
|
|
|
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
|
|
|
{
|
2020-10-26 02:33:11 +01:00
|
|
|
vslines = append(vslines, strings.Split(VertexPrelude(version), "\n")...)
|
2022-01-11 17:56:09 +01:00
|
|
|
vslines = append(vslines, "", "{{.Structs}}")
|
2022-12-24 07:55:05 +01:00
|
|
|
if len(p.Uniforms) > 0 || p.TextureCount > 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 {
|
2022-03-08 17:43:36 +01:00
|
|
|
vslines = append(vslines, fmt.Sprintf("uniform %s;", c.varDecl(p, &t, fmt.Sprintf("U%d", i))))
|
2020-08-01 17:09:12 +02:00
|
|
|
}
|
2022-12-24 07:55:05 +01:00
|
|
|
for i := 0; i < p.TextureCount; i++ {
|
2020-08-01 17:09:12 +02:00
|
|
|
vslines = append(vslines, fmt.Sprintf("uniform sampler2D T%d;", i))
|
|
|
|
}
|
|
|
|
for i, t := range p.Attributes {
|
2020-10-26 02:33:11 +01:00
|
|
|
keyword := "attribute"
|
|
|
|
if version == GLSLVersionES300 {
|
|
|
|
keyword = "in"
|
|
|
|
}
|
2022-03-08 17:43:36 +01:00
|
|
|
vslines = append(vslines, fmt.Sprintf("%s %s;", keyword, c.varDecl(p, &t, fmt.Sprintf("A%d", i))))
|
2020-08-01 17:09:12 +02:00
|
|
|
}
|
|
|
|
for i, t := range p.Varyings {
|
2020-10-26 02:33:11 +01:00
|
|
|
keyword := "varying"
|
|
|
|
if version == GLSLVersionES300 {
|
|
|
|
keyword = "out"
|
|
|
|
}
|
2022-03-08 17:43:36 +01:00
|
|
|
vslines = append(vslines, fmt.Sprintf("%s %s;", keyword, c.varDecl(p, &t, fmt.Sprintf("V%d", i))))
|
2020-08-01 17:09:12 +02:00
|
|
|
}
|
2020-05-23 12:06:44 +02:00
|
|
|
}
|
2021-07-09 11:11:56 +02:00
|
|
|
|
|
|
|
var funcs []*shaderir.Func
|
|
|
|
if p.VertexFunc.Block != nil {
|
2022-11-03 10:48:59 +01:00
|
|
|
funcs = p.ReachableFuncsFromBlock(p.VertexFunc.Block)
|
2021-07-09 11:11:56 +02:00
|
|
|
} else {
|
|
|
|
// When a vertex entry point is not defined, allow to put all the functions. This is useful for testing.
|
|
|
|
funcs = make([]*shaderir.Func, 0, len(p.Funcs))
|
2020-08-01 11:19:06 +02:00
|
|
|
for _, f := range p.Funcs {
|
2021-07-09 11:11:56 +02:00
|
|
|
f := f
|
|
|
|
funcs = append(funcs, &f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(funcs) > 0 {
|
|
|
|
vslines = append(vslines, "")
|
|
|
|
for _, f := range funcs {
|
2022-03-08 17:43:36 +01:00
|
|
|
vslines = append(vslines, c.function(p, f, true)...)
|
2021-07-09 11:11:56 +02:00
|
|
|
}
|
|
|
|
for _, f := range funcs {
|
2020-08-01 11:19:06 +02:00
|
|
|
if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
|
|
|
|
vslines = append(vslines, "")
|
|
|
|
}
|
2022-03-08 17:43:36 +01:00
|
|
|
vslines = append(vslines, c.function(p, f, false)...)
|
2020-06-07 18:41:47 +02:00
|
|
|
}
|
2020-05-23 12:06:44 +02:00
|
|
|
}
|
|
|
|
|
2021-08-15 08:46:24 +02:00
|
|
|
// Add a dummy function to just touch uniform array variable's elements (#1754).
|
|
|
|
// Without this, the first elements of a uniform array might not be initialized correctly on some environments.
|
|
|
|
var touchedUniforms []string
|
|
|
|
for i, t := range p.Uniforms {
|
|
|
|
if t.Main != shaderir.Array {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if t.Length <= 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
str := fmt.Sprintf("U%d[%d]", i, t.Length-1)
|
|
|
|
switch t.Sub[0].Main {
|
2022-11-20 07:36:07 +01:00
|
|
|
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.IVec2, shaderir.IVec3, shaderir.IVec4:
|
2021-08-15 08:46:24 +02:00
|
|
|
str += ".x"
|
|
|
|
case shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
|
|
|
|
str += "[0][0]"
|
|
|
|
}
|
|
|
|
str = "float(" + str + ")"
|
|
|
|
touchedUniforms = append(touchedUniforms, str)
|
|
|
|
}
|
|
|
|
|
|
|
|
var touchUniformsFunc []string
|
|
|
|
if len(touchedUniforms) > 0 {
|
|
|
|
touchUniformsFunc = append(touchUniformsFunc, "float touchUniforms() {")
|
|
|
|
touchUniformsFunc = append(touchUniformsFunc, fmt.Sprintf("\treturn %s;", strings.Join(touchedUniforms, " + ")))
|
|
|
|
touchUniformsFunc = append(touchUniformsFunc, "}")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-09 08:55:59 +02:00
|
|
|
if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 {
|
2021-08-15 08:46:24 +02:00
|
|
|
if len(touchUniformsFunc) > 0 {
|
|
|
|
vslines = append(vslines, "")
|
|
|
|
vslines = append(vslines, touchUniformsFunc...)
|
|
|
|
}
|
2020-08-05 18:38:06 +02:00
|
|
|
vslines = append(vslines, "")
|
2020-05-23 12:06:44 +02:00
|
|
|
vslines = append(vslines, "void main(void) {")
|
2021-08-15 08:46:24 +02:00
|
|
|
if len(touchUniformsFunc) > 0 {
|
|
|
|
vslines = append(vslines, "\ttouchUniforms();")
|
|
|
|
}
|
2022-03-08 17:43:36 +01:00
|
|
|
vslines = append(vslines, c.block(p, p.VertexFunc.Block, p.VertexFunc.Block, 0)...)
|
2020-05-23 12:06:44 +02:00
|
|
|
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
|
|
|
{
|
2020-10-26 02:33:11 +01:00
|
|
|
fslines = append(fslines, strings.Split(FragmentPrelude(version), "\n")...)
|
2020-08-05 18:38:06 +02:00
|
|
|
fslines = append(fslines, "", "{{.Structs}}")
|
2022-12-24 07:55:05 +01:00
|
|
|
if len(p.Uniforms) > 0 || p.TextureCount > 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 {
|
2022-03-08 17:43:36 +01:00
|
|
|
fslines = append(fslines, fmt.Sprintf("uniform %s;", c.varDecl(p, &t, fmt.Sprintf("U%d", i))))
|
2020-08-01 17:09:12 +02:00
|
|
|
}
|
2022-12-24 07:55:05 +01:00
|
|
|
for i := 0; i < p.TextureCount; i++ {
|
2020-08-01 17:09:12 +02:00
|
|
|
fslines = append(fslines, fmt.Sprintf("uniform sampler2D T%d;", i))
|
|
|
|
}
|
|
|
|
for i, t := range p.Varyings {
|
2020-10-26 02:33:11 +01:00
|
|
|
keyword := "varying"
|
|
|
|
if version == GLSLVersionES300 {
|
|
|
|
keyword = "in"
|
|
|
|
}
|
2022-03-08 17:43:36 +01:00
|
|
|
fslines = append(fslines, fmt.Sprintf("%s %s;", keyword, c.varDecl(p, &t, fmt.Sprintf("V%d", i))))
|
2020-08-01 17:09:12 +02:00
|
|
|
}
|
2020-05-23 12:06:44 +02:00
|
|
|
}
|
2020-10-26 02:33:11 +01:00
|
|
|
if version == GLSLVersionES300 {
|
|
|
|
fslines = append(fslines, "out vec4 fragColor;")
|
|
|
|
}
|
|
|
|
|
2021-07-09 11:11:56 +02:00
|
|
|
var funcs []*shaderir.Func
|
|
|
|
if p.VertexFunc.Block != nil {
|
2022-11-03 10:48:59 +01:00
|
|
|
funcs = p.ReachableFuncsFromBlock(p.FragmentFunc.Block)
|
2021-07-09 11:11:56 +02:00
|
|
|
} else {
|
|
|
|
// When a fragment entry point is not defined, allow to put all the functions. This is useful for testing.
|
|
|
|
funcs = make([]*shaderir.Func, 0, len(p.Funcs))
|
2020-08-01 11:19:06 +02:00
|
|
|
for _, f := range p.Funcs {
|
2021-07-09 11:11:56 +02:00
|
|
|
f := f
|
|
|
|
funcs = append(funcs, &f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(funcs) > 0 {
|
|
|
|
fslines = append(fslines, "")
|
|
|
|
for _, f := range funcs {
|
2022-03-08 17:43:36 +01:00
|
|
|
fslines = append(fslines, c.function(p, f, true)...)
|
2021-07-09 11:11:56 +02:00
|
|
|
}
|
|
|
|
for _, f := range funcs {
|
2020-08-01 11:19:06 +02:00
|
|
|
if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
|
|
|
|
fslines = append(fslines, "")
|
|
|
|
}
|
2022-03-08 17:43:36 +01:00
|
|
|
fslines = append(fslines, c.function(p, f, false)...)
|
2020-08-01 11:19:06 +02:00
|
|
|
}
|
2020-05-23 12:06:44 +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, "")
|
2020-05-23 12:06:44 +02:00
|
|
|
fslines = append(fslines, "void main(void) {")
|
2022-03-08 17:43:36 +01:00
|
|
|
fslines = append(fslines, c.block(p, p.FragmentFunc.Block, p.FragmentFunc.Block, 0)...)
|
2020-05-23 12:06:44 +02:00
|
|
|
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 {
|
2020-08-08 12:08:07 +02:00
|
|
|
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 {
|
2022-03-08 17:43:36 +01:00
|
|
|
stlines = append(stlines, fmt.Sprintf("\t%s;", c.varDecl(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-05-21 16:58:08 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
func (c *compileContext) typ(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:
|
2020-08-01 09:20:49 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
func (c *compileContext) varDecl(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)
|
2020-08-01 09:20:49 +02:00
|
|
|
return fmt.Sprintf("%s %s%s", t0, varname, t1)
|
2020-05-12 16:32:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
func (c *compileContext) varInit(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:
|
2022-03-08 17:43:36 +01:00
|
|
|
init := c.varInit(p, &t.Sub[0])
|
2020-07-29 15:43:49 +02:00
|
|
|
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)
|
2020-08-01 09:20:49 +02:00
|
|
|
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"
|
2022-11-20 07:36:07 +01:00
|
|
|
case shaderir.Float, shaderir.Vec2, shaderir.Vec3, shaderir.Vec4,
|
|
|
|
shaderir.IVec2, shaderir.IVec3, shaderir.IVec4,
|
|
|
|
shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
|
2020-08-06 18:53:02 +02:00
|
|
|
return fmt.Sprintf("%s(0)", basicTypeString(t.Main))
|
2020-05-31 16:57:03 +02:00
|
|
|
default:
|
2022-03-08 17:43:36 +01:00
|
|
|
t0, t1 := c.typ(p, t)
|
2020-08-01 09:20:49 +02:00
|
|
|
panic(fmt.Sprintf("?(unexpected type: %s%s)", t0, t1))
|
2020-05-31 16:57:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
func (c *compileContext) function(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 {
|
2022-03-08 17:43:36 +01:00
|
|
|
args = append(args, "in "+c.varDecl(p, &t, fmt.Sprintf("l%d", idx)))
|
2020-05-13 16:31:17 +02:00
|
|
|
idx++
|
|
|
|
}
|
|
|
|
for _, t := range f.OutParams {
|
2022-03-08 17:43:36 +01:00
|
|
|
args = append(args, "out "+c.varDecl(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, ", ")
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
t0, t1 := c.typ(p, &f.Return)
|
2020-08-01 09:20:49 +02:00
|
|
|
sig := fmt.Sprintf("%s%s F%d(%s)", t0, t1, f.Index, argsstr)
|
2020-07-27 16:51:05 +02:00
|
|
|
|
2020-05-13 17:46:36 +02:00
|
|
|
var lines []string
|
2020-07-27 16:51:05 +02:00
|
|
|
if prototype {
|
|
|
|
lines = append(lines, fmt.Sprintf("%s;", sig))
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("%s {", sig))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, f.Block, f.Block, 0)...)
|
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 {
|
2020-07-12 15:22:22 +02:00
|
|
|
switch t {
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.ConstTypeNone:
|
2020-07-12 15:22:22 +02:00
|
|
|
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:
|
2020-07-12 15:22:22 +02:00
|
|
|
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)
|
2020-08-12 17:01:14 +02:00
|
|
|
return fmt.Sprintf("%.10e", x)
|
2020-07-12 15:22:22 +02:00
|
|
|
}
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.ConstTypeInt:
|
2020-07-12 15:22:22 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:03:15 +01:00
|
|
|
func (c *compileContext) localVariableName(p *shaderir.Program, topBlock *shaderir.Block, idx int) string {
|
2020-07-13 15:49:11 +02:00
|
|
|
switch topBlock {
|
2020-08-09 08:55:59 +02:00
|
|
|
case p.VertexFunc.Block:
|
2020-07-13 15:49:11 +02:00
|
|
|
na := len(p.Attributes)
|
|
|
|
nv := len(p.Varyings)
|
|
|
|
switch {
|
|
|
|
case idx < na:
|
2020-09-09 18:38:54 +02:00
|
|
|
return fmt.Sprintf("A%d", idx)
|
2020-07-13 15:49:11 +02:00
|
|
|
case idx == na:
|
2020-09-09 18:38:54 +02:00
|
|
|
return "gl_Position"
|
2020-07-13 15:49:11 +02:00
|
|
|
case idx < na+nv+1:
|
2020-09-09 18:38:54 +02:00
|
|
|
return fmt.Sprintf("V%d", idx-na-1)
|
2020-07-13 15:49:11 +02:00
|
|
|
default:
|
2020-09-09 18:38:54 +02:00
|
|
|
return fmt.Sprintf("l%d", idx-(na+nv+1))
|
2020-07-13 15:49:11 +02:00
|
|
|
}
|
2020-08-09 08:55:59 +02:00
|
|
|
case p.FragmentFunc.Block:
|
2020-07-13 15:49:11 +02:00
|
|
|
nv := len(p.Varyings)
|
|
|
|
switch {
|
|
|
|
case idx == 0:
|
2020-09-09 18:38:54 +02:00
|
|
|
return "gl_FragCoord"
|
2020-07-13 15:49:11 +02:00
|
|
|
case idx < nv+1:
|
2020-09-09 18:38:54 +02:00
|
|
|
return fmt.Sprintf("V%d", idx-1)
|
2020-07-13 15:49:11 +02:00
|
|
|
default:
|
2022-08-16 18:01:23 +02:00
|
|
|
return fmt.Sprintf("l%d", idx-(nv+1))
|
2020-07-13 15:49:11 +02:00
|
|
|
}
|
|
|
|
default:
|
2020-09-09 18:38:54 +02:00
|
|
|
return fmt.Sprintf("l%d", idx)
|
2020-07-13 15:49:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 19:11:21 +02:00
|
|
|
func (c *compileContext) initVariable(p *shaderir.Program, topBlock, block *shaderir.Block, index int, decl bool, level int) []string {
|
|
|
|
idt := strings.Repeat("\t", level+1)
|
2022-03-10 18:03:15 +01:00
|
|
|
name := c.localVariableName(p, topBlock, index)
|
2020-09-09 19:11:21 +02:00
|
|
|
t := p.LocalVariableType(topBlock, block, index)
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
switch t.Main {
|
|
|
|
case shaderir.Array:
|
|
|
|
if decl {
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s;", idt, c.varDecl(p, &t, name)))
|
2020-09-09 19:11:21 +02:00
|
|
|
}
|
2022-03-08 17:43:36 +01:00
|
|
|
init := c.varInit(p, &t.Sub[0])
|
2020-09-09 19:11:21 +02:00
|
|
|
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:
|
|
|
|
if decl {
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, c.varDecl(p, &t, name), c.varInit(p, &t)))
|
2020-09-09 19:11:21 +02:00
|
|
|
} else {
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, name, c.varInit(p, &t)))
|
2020-09-09 19:11:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Block, level int) []string {
|
2020-08-09 08:55:59 +02:00
|
|
|
if block == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:46:36 +02:00
|
|
|
var lines []string
|
2020-09-07 19:42:31 +02:00
|
|
|
for i := range block.LocalVars {
|
2020-09-09 19:11:21 +02:00
|
|
|
lines = append(lines, c.initVariable(p, topBlock, block, block.LocalVarIndexOffset+i, true, level)...)
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
2020-05-13 18:45:33 +02:00
|
|
|
|
2022-03-11 07:18:38 +01:00
|
|
|
var expr func(e *shaderir.Expr) string
|
|
|
|
expr = 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:
|
2020-07-12 15:22:22 +02:00
|
|
|
return constantToNumberLiteral(e.ConstType, e.Const)
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.UniformVariable:
|
2020-05-16 21:28:03 +02:00
|
|
|
return fmt.Sprintf("U%d", e.Index)
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.TextureVariable:
|
2020-07-05 20:36:15 +02:00
|
|
|
return fmt.Sprintf("T%d", e.Index)
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.LocalVariable:
|
2022-03-10 18:03:15 +01:00
|
|
|
return c.localVariableName(p, topBlock, e.Index)
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.StructMember:
|
2020-05-16 21:28:03 +02:00
|
|
|
return fmt.Sprintf("M%d", e.Index)
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.BuiltinFuncExpr:
|
2020-10-26 02:33:11 +01:00
|
|
|
return c.builtinFuncString(e.BuiltinFunc)
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.SwizzlingExpr:
|
2020-08-08 12:08:07 +02:00
|
|
|
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:
|
2022-03-13 08:20:01 +01:00
|
|
|
op = opString(e.Op)
|
2020-05-16 10:22:17 +02:00
|
|
|
default:
|
2022-03-13 08:20:01 +01:00
|
|
|
op = fmt.Sprintf("?(unexpected op: %d)", e.Op)
|
2020-05-16 10:22:17 +02:00
|
|
|
}
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("%s(%s)", op, expr(&e.Exprs[0]))
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.Binary:
|
2022-01-11 17:41:06 +01:00
|
|
|
if e.Op == shaderir.ModOp && (c.version == GLSLVersionDefault || c.version == GLSLVersionES100) {
|
|
|
|
// '%' is not defined.
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("modInt((%s), (%s))", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
|
2022-01-11 17:41:06 +01:00
|
|
|
}
|
2022-03-13 08:20:01 +01:00
|
|
|
return fmt.Sprintf("(%s) %s (%s)", expr(&e.Exprs[0]), opString(e.Op), expr(&e.Exprs[1]))
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.Selection:
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("(%s) ? (%s) : (%s)", expr(&e.Exprs[0]), expr(&e.Exprs[1]), expr(&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:] {
|
2022-03-11 07:18:38 +01:00
|
|
|
args = append(args, expr(&exp))
|
2020-05-16 12:10:12 +02:00
|
|
|
}
|
2020-05-23 11:06:50 +02:00
|
|
|
// Using parentheses at the callee is illegal.
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("%s(%s)", expr(&e.Exprs[0]), strings.Join(args, ", "))
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.FieldSelector:
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("(%s).%s", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.Index:
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("(%s)[%s]", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
|
2020-05-13 18:45:33 +02:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("?(unexpected expr: %d)", e.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 19:11:21 +02:00
|
|
|
idt := strings.Repeat("\t", level+1)
|
2020-07-05 10:47:19 +02:00
|
|
|
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:
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s;", idt, expr(&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+"{")
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
2020-05-13 18:45:33 +02:00
|
|
|
lines = append(lines, idt+"}")
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.Assign:
|
2020-08-09 11:55:39 +02:00
|
|
|
lhs := s.Exprs[0]
|
|
|
|
rhs := s.Exprs[1]
|
|
|
|
if lhs.Type == shaderir.LocalVariable {
|
2020-09-09 18:38:54 +02:00
|
|
|
if t := p.LocalVariableType(topBlock, block, lhs.Index); t.Main == shaderir.Array {
|
2020-08-09 11:55:39 +02:00
|
|
|
for i := 0; i < t.Length; i++ {
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%[1]s%[2]s[%[3]d] = %[4]s[%[3]d];", idt, expr(&lhs), i, expr(&rhs)))
|
2020-08-09 11:55:39 +02:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, expr(&lhs), expr(&rhs)))
|
2020-09-06 17:13:46 +02:00
|
|
|
case shaderir.Init:
|
|
|
|
lines = append(lines, c.initVariable(p, topBlock, block, s.InitIndex, false, level)...)
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.If:
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%sif (%s) {", idt, expr(&s.Exprs[0])))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
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))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, topBlock, s.Blocks[1], level+1)...)
|
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
|
2020-07-12 15:22:22 +02:00
|
|
|
switch s.ForVarType.Main {
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.Int:
|
|
|
|
ct = shaderir.ConstTypeInt
|
|
|
|
case shaderir.Float:
|
|
|
|
ct = shaderir.ConstTypeFloat
|
2020-07-12 15:22:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-10 18:03:15 +01:00
|
|
|
v := c.localVariableName(p, topBlock, s.ForVarIndex)
|
2020-05-15 20:10:03 +02:00
|
|
|
var delta string
|
2020-07-12 15:22:22 +02:00
|
|
|
switch val, _ := constant.Float64Val(s.ForDelta); val {
|
2020-05-16 08:51:54 +02:00
|
|
|
case 0:
|
2020-07-12 15:22:22 +02:00
|
|
|
delta = fmt.Sprintf("?(unexpected delta: %v)", s.ForDelta)
|
2020-05-15 20:10:03 +02:00
|
|
|
case 1:
|
2020-07-13 15:49:11 +02:00
|
|
|
delta = fmt.Sprintf("%s++", v)
|
2020-05-15 20:10:03 +02:00
|
|
|
case -1:
|
2020-07-13 15:49:11 +02:00
|
|
|
delta = fmt.Sprintf("%s--", v)
|
2020-05-15 20:10:03 +02:00
|
|
|
default:
|
2020-07-12 15:22:22 +02:00
|
|
|
d := s.ForDelta
|
|
|
|
if val > 0 {
|
2020-07-13 15:49:11 +02:00
|
|
|
delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(ct, d))
|
2020-05-16 08:51:54 +02:00
|
|
|
} else {
|
2020-07-12 15:22:22 +02:00
|
|
|
d = constant.UnaryOp(token.SUB, d, 0)
|
2020-07-13 15:49:11 +02:00
|
|
|
delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(ct, d))
|
2020-05-16 08:51:54 +02:00
|
|
|
}
|
2020-05-15 20:10:03 +02:00
|
|
|
}
|
2020-05-16 08:51:54 +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:
|
2022-03-13 08:20:01 +01:00
|
|
|
op = opString(s.ForOp)
|
2020-05-16 08:51:54 +02:00
|
|
|
default:
|
2022-03-13 08:20:01 +01:00
|
|
|
op = fmt.Sprintf("?(unexpected op: %d)", s.ForOp)
|
2020-05-16 08:51:54 +02:00
|
|
|
}
|
2020-07-12 15:22:22 +02:00
|
|
|
|
2020-07-28 17:57:47 +02:00
|
|
|
t := s.ForVarType
|
2020-07-12 15:22:22 +02:00
|
|
|
init := constantToNumberLiteral(ct, s.ForInit)
|
|
|
|
end := constantToNumberLiteral(ct, s.ForEnd)
|
2020-08-03 16:21:09 +02:00
|
|
|
t0, t1 := typeString(&t)
|
2020-08-01 09:20:49 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%sfor (%s %s%s = %s; %s %s %s; %s) {", idt, t0, v, t1, init, v, op, end, delta))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
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:
|
2022-08-16 18:01:23 +02:00
|
|
|
switch {
|
|
|
|
case topBlock == p.FragmentFunc.Block:
|
|
|
|
token := "gl_FragColor"
|
|
|
|
if c.version == GLSLVersionES300 {
|
|
|
|
token = "fragColor"
|
|
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, token, expr(&s.Exprs[0])))
|
|
|
|
// The 'return' statement is not required so far, as the fragment entrypoint has only one sentence so far. See adjustProgram implementation.
|
|
|
|
case len(s.Exprs) == 0:
|
2020-05-16 13:16:04 +02:00
|
|
|
lines = append(lines, idt+"return;")
|
2022-08-16 18:01:23 +02:00
|
|
|
default:
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, expr(&s.Exprs[0])))
|
2020-05-16 13:16:04 +02:00
|
|
|
}
|
2020-08-03 16:21:09 +02:00
|
|
|
case shaderir.Discard:
|
2022-08-16 18:01:23 +02:00
|
|
|
// 'discard' is invoked only in the fragment shader entry point.
|
|
|
|
lines = append(lines, idt+"discard;", idt+"return vec4(0.0);")
|
2020-05-13 18:45:33 +02:00
|
|
|
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
|
|
|
}
|
2022-08-16 18:01:23 +02:00
|
|
|
|
|
|
|
func adjustProgram(p *shaderir.Program) *shaderir.Program {
|
|
|
|
if p.FragmentFunc.Block == nil {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shallow-clone the program in order not to modify p itself.
|
|
|
|
newP := *p
|
|
|
|
|
|
|
|
// Create a new slice not to affect the original p.
|
|
|
|
newP.Funcs = make([]shaderir.Func, len(p.Funcs))
|
|
|
|
copy(newP.Funcs, p.Funcs)
|
|
|
|
|
|
|
|
// Create a new function whose body is the same is the fragment shader's entry point.
|
|
|
|
// The entry point will call this.
|
|
|
|
// This indirect call is needed for these issues:
|
|
|
|
// - Assignment to gl_FragColor doesn't work (#2245)
|
|
|
|
// - There are some odd compilers that don't work with early returns and gl_FragColor (#2247)
|
|
|
|
|
|
|
|
// Determine a unique index of the new function.
|
|
|
|
var funcIdx int
|
|
|
|
for _, f := range newP.Funcs {
|
|
|
|
if funcIdx <= f.Index {
|
|
|
|
funcIdx = f.Index + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For parameters of a fragment func, see the comment in internal/shaderir/program.go.
|
|
|
|
inParams := make([]shaderir.Type, 1+len(newP.Varyings))
|
|
|
|
inParams[0] = shaderir.Type{
|
|
|
|
Main: shaderir.Vec4, // gl_FragCoord
|
|
|
|
}
|
|
|
|
copy(inParams[1:], newP.Varyings)
|
|
|
|
|
|
|
|
newP.Funcs = append(newP.Funcs, shaderir.Func{
|
|
|
|
Index: funcIdx,
|
|
|
|
InParams: inParams,
|
2022-08-16 18:01:23 +02:00
|
|
|
OutParams: nil,
|
|
|
|
Return: shaderir.Type{
|
|
|
|
Main: shaderir.Vec4,
|
|
|
|
},
|
|
|
|
Block: newP.FragmentFunc.Block,
|
2022-08-16 18:01:23 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Create an AST to call the new function.
|
|
|
|
call := []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.FunctionExpr,
|
|
|
|
Index: funcIdx,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i := 0; i < 1+len(newP.Varyings); i++ {
|
|
|
|
call = append(call, shaderir.Expr{
|
|
|
|
Type: shaderir.LocalVariable,
|
|
|
|
Index: i,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the entry point with just calling the new function.
|
|
|
|
stmts := []shaderir.Stmt{
|
|
|
|
{
|
2022-08-16 18:01:23 +02:00
|
|
|
// Return: This will be replaced with assignment to gl_FragColor.
|
|
|
|
Type: shaderir.Return,
|
2022-08-16 18:01:23 +02:00
|
|
|
Exprs: []shaderir.Expr{
|
2022-08-16 18:01:23 +02:00
|
|
|
// The function call
|
2022-08-16 18:01:23 +02:00
|
|
|
{
|
|
|
|
Type: shaderir.Call,
|
|
|
|
Exprs: call,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
newP.FragmentFunc = shaderir.FragmentFunc{
|
|
|
|
Block: &shaderir.Block{
|
2022-08-16 18:01:23 +02:00
|
|
|
LocalVars: nil,
|
2022-08-16 18:01:23 +02:00
|
|
|
LocalVarIndexOffset: 1 + len(newP.Varyings) + 1,
|
|
|
|
Stmts: stmts,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &newP
|
|
|
|
}
|