graphicsdriver/opengl: Implement shaders for browsers

Fixes #1166
This commit is contained in:
Hajime Hoshi 2020-08-01 18:19:06 +09:00
parent bab05874cd
commit 4bd01fd038
2 changed files with 32 additions and 8 deletions

View File

@ -26,6 +26,20 @@ import (
. "github.com/hajimehoshi/ebiten/internal/shader"
)
func normalize(str string) string {
ls := strings.Split(strings.TrimSpace(str), "\n")
var start int
for i, l := range ls {
if !strings.HasPrefix(l, "#endif") {
continue
}
start = i + 1
break
}
ls = ls[start:]
return strings.TrimSpace(strings.Join(ls, "\n"))
}
func TestCompile(t *testing.T) {
if runtime.GOOS == "js" {
t.Skip("file open might not be implemented in this environment")
@ -107,11 +121,11 @@ func TestCompile(t *testing.T) {
return
}
vs, fs := s.Glsl()
if got, want := vs, string(tc.VS); got != want {
if got, want := normalize(vs), normalize(string(tc.VS)); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if tc.FS != nil {
if got, want := fs, string(tc.FS); got != want {
if got, want := normalize(fs), normalize(string(tc.FS)); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}

View File

@ -92,7 +92,7 @@ func (p *Program) Glsl() (vertexShader, fragmentShader string) {
vslines = append(vslines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i))))
}
if len(p.Funcs) > 0 {
if len(vslines) > 0 {
if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
vslines = append(vslines, "")
}
}
@ -100,14 +100,14 @@ func (p *Program) Glsl() (vertexShader, fragmentShader string) {
vslines = append(vslines, p.glslFunc(&f, true)...)
}
for _, f := range p.Funcs {
if len(vslines) > 0 {
if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
vslines = append(vslines, "")
}
vslines = append(vslines, p.glslFunc(&f, false)...)
}
if len(p.VertexFunc.Block.Stmts) > 0 {
if len(vslines) > 0 {
if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
vslines = append(vslines, "")
}
vslines = append(vslines, "void main(void) {")
@ -119,6 +119,16 @@ func (p *Program) Glsl() (vertexShader, fragmentShader string) {
// Fragment func
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 {
fslines = append(fslines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&t, fmt.Sprintf("U%d", i))))
}
@ -128,21 +138,21 @@ func (p *Program) Glsl() (vertexShader, fragmentShader string) {
for i, t := range p.Varyings {
fslines = append(fslines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i))))
}
if len(fslines) > 0 {
if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
fslines = append(fslines, "")
}
for _, f := range p.Funcs {
fslines = append(fslines, p.glslFunc(&f, true)...)
}
for _, f := range p.Funcs {
if len(fslines) > 0 {
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(fslines) > 0 {
if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
fslines = append(fslines, "")
}
fslines = append(fslines, "void main(void) {")