shaderir: Bug fix: Pass tests

This commit is contained in:
Hajime Hoshi 2020-08-01 18:19:06 +09:00
parent 4bd01fd038
commit fa60e31997
3 changed files with 69 additions and 54 deletions

View File

@ -24,20 +24,14 @@ import (
"testing" "testing"
. "github.com/hajimehoshi/ebiten/internal/shader" . "github.com/hajimehoshi/ebiten/internal/shader"
"github.com/hajimehoshi/ebiten/internal/shaderir"
) )
func normalize(str string) string { func normalize(str string) string {
ls := strings.Split(strings.TrimSpace(str), "\n") if strings.HasPrefix(str, shaderir.GlslFragmentPrelude) {
var start int str = str[len(shaderir.GlslFragmentPrelude):]
for i, l := range ls {
if !strings.HasPrefix(l, "#endif") {
continue
}
start = i + 1
break
} }
ls = ls[start:] return strings.TrimSpace(str)
return strings.TrimSpace(strings.Join(ls, "\n"))
} }
func TestCompile(t *testing.T) { func TestCompile(t *testing.T) {

View File

@ -21,6 +21,15 @@ import (
"strings" "strings"
) )
const GlslFragmentPrelude = `#if defined(GL_ES)
precision highp float;
#else
#define lowp
#define mediump
#define highp
#endif
`
func isValidSwizzling(s string) bool { func isValidSwizzling(s string) bool {
if len(s) < 1 || 4 < len(s) { if len(s) < 1 || 4 < len(s) {
return false return false
@ -95,15 +104,15 @@ func (p *Program) Glsl() (vertexShader, fragmentShader string) {
if len(vslines) > 0 && vslines[len(vslines)-1] != "" { if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
vslines = append(vslines, "") vslines = append(vslines, "")
} }
} for _, f := range p.Funcs {
for _, f := range p.Funcs { vslines = append(vslines, p.glslFunc(&f, true)...)
vslines = append(vslines, p.glslFunc(&f, true)...) }
} for _, f := range p.Funcs {
for _, f := range p.Funcs { if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
if len(vslines) > 0 && vslines[len(vslines)-1] != "" { vslines = append(vslines, "")
vslines = append(vslines, "") }
vslines = append(vslines, p.glslFunc(&f, false)...)
} }
vslines = append(vslines, p.glslFunc(&f, false)...)
} }
if len(p.VertexFunc.Block.Stmts) > 0 { if len(p.VertexFunc.Block.Stmts) > 0 {
@ -119,16 +128,6 @@ func (p *Program) Glsl() (vertexShader, fragmentShader string) {
// Fragment func // Fragment func
var fslines []string var fslines []string
{ {
fslines = append(fslines,
"#if defined(GL_ES)",
"precision highp float;",
"#else",
"#define lowp",
"#define mediump",
"#define highp",
"#endif",
"")
for i, t := range p.Uniforms { for i, t := range p.Uniforms {
fslines = append(fslines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&t, fmt.Sprintf("U%d", i)))) fslines = append(fslines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&t, fmt.Sprintf("U%d", i))))
} }
@ -138,17 +137,19 @@ func (p *Program) Glsl() (vertexShader, fragmentShader string) {
for i, t := range p.Varyings { for i, t := range p.Varyings {
fslines = append(fslines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i)))) fslines = append(fslines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i))))
} }
if len(fslines) > 0 && fslines[len(fslines)-1] != "" { if len(p.Funcs) > 0 {
fslines = append(fslines, "")
}
for _, f := range p.Funcs {
fslines = append(fslines, p.glslFunc(&f, true)...)
}
for _, f := range p.Funcs {
if len(fslines) > 0 && fslines[len(fslines)-1] != "" { if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
fslines = append(fslines, "") fslines = append(fslines, "")
} }
fslines = append(fslines, p.glslFunc(&f, false)...) for _, f := range p.Funcs {
fslines = append(fslines, p.glslFunc(&f, true)...)
}
for _, f := range p.Funcs {
if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
fslines = append(fslines, "")
}
fslines = append(fslines, p.glslFunc(&f, false)...)
}
} }
if len(p.FragmentFunc.Block.Stmts) > 0 { if len(p.FragmentFunc.Block.Stmts) > 0 {
@ -171,10 +172,13 @@ func (p *Program) Glsl() (vertexShader, fragmentShader string) {
} }
vslines = append(stlines, vslines...) vslines = append(stlines, vslines...)
tmp := make([]string, len(stlines)) tmp := make([]string, len(stlines))
copy(tmp, stlines) copy(tmp, stlines)
fslines = append(tmp, fslines...) fslines = append(tmp, fslines...)
fslines = append(strings.Split(GlslFragmentPrelude, "\n"), fslines...)
return strings.Join(vslines, "\n") + "\n", strings.Join(fslines, "\n") + "\n" return strings.Join(vslines, "\n") + "\n", strings.Join(fslines, "\n") + "\n"
} }

View File

@ -159,7 +159,7 @@ func TestOutput(t *testing.T) {
Name: "Empty", Name: "Empty",
Program: Program{}, Program: Program{},
GlslVS: ``, GlslVS: ``,
GlslFS: ``, GlslFS: GlslFragmentPrelude,
}, },
{ {
Name: "Uniform", Name: "Uniform",
@ -169,7 +169,8 @@ func TestOutput(t *testing.T) {
}, },
}, },
GlslVS: `uniform float U0;`, GlslVS: `uniform float U0;`,
GlslFS: `uniform float U0;`, GlslFS: GlslFragmentPrelude + `
uniform float U0;`,
}, },
{ {
Name: "UniformStruct", Name: "UniformStruct",
@ -187,7 +188,8 @@ func TestOutput(t *testing.T) {
float M0; float M0;
}; };
uniform S0 U0;`, uniform S0 U0;`,
GlslFS: `struct S0 { GlslFS: GlslFragmentPrelude + `
struct S0 {
float M0; float M0;
}; };
uniform S0 U0;`, uniform S0 U0;`,
@ -208,7 +210,8 @@ uniform S0 U0;`,
GlslVS: `uniform float U0; GlslVS: `uniform float U0;
attribute vec2 A0; attribute vec2 A0;
varying vec3 V0;`, varying vec3 V0;`,
GlslFS: `uniform float U0; GlslFS: GlslFragmentPrelude + `
uniform float U0;
varying vec3 V0;`, varying vec3 V0;`,
}, },
{ {
@ -224,7 +227,8 @@ varying vec3 V0;`,
void F0(void) { void F0(void) {
}`, }`,
GlslFS: `void F0(void); GlslFS: GlslFragmentPrelude + `
void F0(void);
void F0(void) { void F0(void) {
}`, }`,
@ -250,7 +254,8 @@ void F0(void) {
void F0(in float l0, in vec2 l1, in vec4 l2, out mat4 l3) { void F0(in float l0, in vec2 l1, in vec4 l2, out mat4 l3) {
}`, }`,
GlslFS: `void F0(in float l0, in vec2 l1, in vec4 l2, out mat4 l3); GlslFS: GlslFragmentPrelude + `
void F0(in float l0, in vec2 l1, in vec4 l2, out mat4 l3);
void F0(in float l0, in vec2 l1, in vec4 l2, out mat4 l3) { void F0(in float l0, in vec2 l1, in vec4 l2, out mat4 l3) {
}`, }`,
@ -279,7 +284,8 @@ void F0(in float l0, in vec2 l1, in vec4 l2, out mat4 l3) {
float F0(in float l0) { float F0(in float l0) {
return l0; return l0;
}`, }`,
GlslFS: `float F0(in float l0); GlslFS: GlslFragmentPrelude + `
float F0(in float l0);
float F0(in float l0) { float F0(in float l0) {
return l0; return l0;
@ -310,7 +316,8 @@ void F0(in float l0, out float l1) {
mat4 l2 = mat4(0); mat4 l2 = mat4(0);
mat4 l3 = mat4(0); mat4 l3 = mat4(0);
}`, }`,
GlslFS: `void F0(in float l0, out float l1); GlslFS: GlslFragmentPrelude + `
void F0(in float l0, out float l1);
void F0(in float l0, out float l1) { void F0(in float l0, out float l1) {
mat4 l2 = mat4(0); mat4 l2 = mat4(0);
@ -356,7 +363,8 @@ void F0(in float l0, out float l1) {
mat4 l5 = mat4(0); mat4 l5 = mat4(0);
} }
}`, }`,
GlslFS: `void F0(in float l0, out float l1); GlslFS: GlslFragmentPrelude + `
void F0(in float l0, out float l1);
void F0(in float l0, out float l1) { void F0(in float l0, out float l1) {
mat4 l2 = mat4(0); mat4 l2 = mat4(0);
@ -399,7 +407,8 @@ void F0(in float l0, out float l1) {
void F0(in float l0, in float l1, out float l2) { void F0(in float l0, in float l1, out float l2) {
l2 = (l0) + (l1); l2 = (l0) + (l1);
}`, }`,
GlslFS: `void F0(in float l0, in float l1, out float l2); GlslFS: GlslFragmentPrelude + `
void F0(in float l0, in float l1, out float l2);
void F0(in float l0, in float l1, out float l2) { void F0(in float l0, in float l1, out float l2) {
l2 = (l0) + (l1); l2 = (l0) + (l1);
@ -438,7 +447,8 @@ void F0(in float l0, in float l1, out float l2) {
void F0(in bool l0, in float l1, in float l2, out float l3) { void F0(in bool l0, in float l1, in float l2, out float l3) {
l3 = (l0) ? (l1) : (l2); l3 = (l0) ? (l1) : (l2);
}`, }`,
GlslFS: `void F0(in bool l0, in float l1, in float l2, out float l3); GlslFS: GlslFragmentPrelude + `
void F0(in bool l0, in float l1, in float l2, out float l3);
void F0(in bool l0, in float l1, in float l2, out float l3) { void F0(in bool l0, in float l1, in float l2, out float l3) {
l3 = (l0) ? (l1) : (l2); l3 = (l0) ? (l1) : (l2);
@ -482,7 +492,8 @@ void F0(in float l0, in float l1, out vec2 l2) {
F1(); F1();
l2 = F2(l0, l1); l2 = F2(l0, l1);
}`, }`,
GlslFS: `void F0(in float l0, in float l1, out vec2 l2); GlslFS: GlslFragmentPrelude + `
void F0(in float l0, in float l1, out vec2 l2);
void F0(in float l0, in float l1, out vec2 l2) { void F0(in float l0, in float l1, out vec2 l2) {
F1(); F1();
@ -521,7 +532,8 @@ void F0(in float l0, in float l1, out vec2 l2) {
void F0(in float l0, in float l1, out float l2) { void F0(in float l0, in float l1, out float l2) {
l2 = min(l0, l1); l2 = min(l0, l1);
}`, }`,
GlslFS: `void F0(in float l0, in float l1, out float l2); GlslFS: GlslFragmentPrelude + `
void F0(in float l0, in float l1, out float l2);
void F0(in float l0, in float l1, out float l2) { void F0(in float l0, in float l1, out float l2) {
l2 = min(l0, l1); l2 = min(l0, l1);
@ -557,7 +569,8 @@ void F0(in float l0, in float l1, out float l2) {
void F0(in vec4 l0, out vec2 l1) { void F0(in vec4 l0, out vec2 l1) {
l1 = (l0).xz; l1 = (l0).xz;
}`, }`,
GlslFS: `void F0(in vec4 l0, out vec2 l1); GlslFS: GlslFragmentPrelude + `
void F0(in vec4 l0, out vec2 l1);
void F0(in vec4 l0, out vec2 l1) { void F0(in vec4 l0, out vec2 l1) {
l1 = (l0).xz; l1 = (l0).xz;
@ -612,7 +625,8 @@ void F0(in float l0, in float l1, out float l2) {
l2 = l1; l2 = l1;
} }
}`, }`,
GlslFS: `void F0(in float l0, in float l1, out float l2); GlslFS: GlslFragmentPrelude + `
void F0(in float l0, in float l1, out float l2);
void F0(in float l0, in float l1, out float l2) { void F0(in float l0, in float l1, out float l2) {
if ((l0) == (0.0)) { if ((l0) == (0.0)) {
@ -662,7 +676,8 @@ void F0(in float l0, in float l1, out float l2) {
l2 = l0; l2 = l0;
} }
}`, }`,
GlslFS: `void F0(in float l0, in float l1, out float l2); GlslFS: GlslFragmentPrelude + `
void F0(in float l0, in float l1, out float l2);
void F0(in float l0, in float l1, out float l2) { void F0(in float l0, in float l1, out float l2) {
for (int l3 = 0; l3 < 100; l3++) { for (int l3 = 0; l3 < 100; l3++) {
@ -715,7 +730,8 @@ void main(void) {
V0 = A1; V0 = A1;
V1 = A2; V1 = A2;
}`, }`,
GlslFS: `uniform float U0; GlslFS: GlslFragmentPrelude + `
uniform float U0;
varying float V0; varying float V0;
varying vec2 V1;`, varying vec2 V1;`,
}, },
@ -784,7 +800,8 @@ void main(void) {
V0 = A1; V0 = A1;
V1 = A2; V1 = A2;
}`, }`,
GlslFS: `uniform float U0; GlslFS: GlslFragmentPrelude + `
uniform float U0;
varying float V0; varying float V0;
varying vec2 V1; varying vec2 V1;