mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 03:02:49 +01:00
shaderir/glsl: Refactoring
This commit is contained in:
parent
6003d85f75
commit
6576a1da76
@ -18,6 +18,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go/constant"
|
"go/constant"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/shaderir"
|
"github.com/hajimehoshi/ebiten/internal/shaderir"
|
||||||
@ -95,10 +96,9 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
|
|||||||
// Vertex func
|
// Vertex func
|
||||||
var vslines []string
|
var vslines []string
|
||||||
{
|
{
|
||||||
|
vslines = append(vslines, "{{.Structs}}")
|
||||||
if len(p.Uniforms) > 0 || p.TextureNum > 0 || len(p.Attributes) > 0 || len(p.Varyings) > 0 {
|
if len(p.Uniforms) > 0 || p.TextureNum > 0 || len(p.Attributes) > 0 || len(p.Varyings) > 0 {
|
||||||
if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
|
vslines = append(vslines, "")
|
||||||
vslines = append(vslines, "")
|
|
||||||
}
|
|
||||||
for i, t := range p.Uniforms {
|
for i, t := range p.Uniforms {
|
||||||
vslines = append(vslines, fmt.Sprintf("uniform %s;", c.glslVarDecl(p, &t, fmt.Sprintf("U%d", i))))
|
vslines = append(vslines, fmt.Sprintf("uniform %s;", c.glslVarDecl(p, &t, fmt.Sprintf("U%d", i))))
|
||||||
}
|
}
|
||||||
@ -113,9 +113,7 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(p.Funcs) > 0 {
|
if len(p.Funcs) > 0 {
|
||||||
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, c.glslFunc(p, &f, true)...)
|
vslines = append(vslines, c.glslFunc(p, &f, true)...)
|
||||||
}
|
}
|
||||||
@ -128,9 +126,7 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(p.VertexFunc.Block.Stmts) > 0 {
|
if len(p.VertexFunc.Block.Stmts) > 0 {
|
||||||
if len(vslines) > 0 && vslines[len(vslines)-1] != "" {
|
vslines = append(vslines, "")
|
||||||
vslines = append(vslines, "")
|
|
||||||
}
|
|
||||||
vslines = append(vslines, "void main(void) {")
|
vslines = append(vslines, "void main(void) {")
|
||||||
vslines = append(vslines, c.glslBlock(p, &p.VertexFunc.Block, &p.VertexFunc.Block, 0, 0)...)
|
vslines = append(vslines, c.glslBlock(p, &p.VertexFunc.Block, &p.VertexFunc.Block, 0, 0)...)
|
||||||
vslines = append(vslines, "}")
|
vslines = append(vslines, "}")
|
||||||
@ -140,10 +136,10 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
|
|||||||
// Fragment func
|
// Fragment func
|
||||||
var fslines []string
|
var fslines []string
|
||||||
{
|
{
|
||||||
|
fslines = append(fslines, strings.Split(FragmentPrelude, "\n")...)
|
||||||
|
fslines = append(fslines, "", "{{.Structs}}")
|
||||||
if len(p.Uniforms) > 0 || p.TextureNum > 0 || len(p.Varyings) > 0 {
|
if len(p.Uniforms) > 0 || p.TextureNum > 0 || len(p.Varyings) > 0 {
|
||||||
if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
|
fslines = append(fslines, "")
|
||||||
fslines = append(fslines, "")
|
|
||||||
}
|
|
||||||
for i, t := range p.Uniforms {
|
for i, t := range p.Uniforms {
|
||||||
fslines = append(fslines, fmt.Sprintf("uniform %s;", c.glslVarDecl(p, &t, fmt.Sprintf("U%d", i))))
|
fslines = append(fslines, fmt.Sprintf("uniform %s;", c.glslVarDecl(p, &t, fmt.Sprintf("U%d", i))))
|
||||||
}
|
}
|
||||||
@ -155,9 +151,7 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(p.Funcs) > 0 {
|
if len(p.Funcs) > 0 {
|
||||||
if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
|
fslines = append(fslines, "")
|
||||||
fslines = append(fslines, "")
|
|
||||||
}
|
|
||||||
for _, f := range p.Funcs {
|
for _, f := range p.Funcs {
|
||||||
fslines = append(fslines, c.glslFunc(p, &f, true)...)
|
fslines = append(fslines, c.glslFunc(p, &f, true)...)
|
||||||
}
|
}
|
||||||
@ -170,17 +164,15 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(p.FragmentFunc.Block.Stmts) > 0 {
|
if len(p.FragmentFunc.Block.Stmts) > 0 {
|
||||||
if len(fslines) > 0 && fslines[len(fslines)-1] != "" {
|
fslines = append(fslines, "")
|
||||||
fslines = append(fslines, "")
|
|
||||||
}
|
|
||||||
fslines = append(fslines, "void main(void) {")
|
fslines = append(fslines, "void main(void) {")
|
||||||
fslines = append(fslines, c.glslBlock(p, &p.FragmentFunc.Block, &p.FragmentFunc.Block, 0, 0)...)
|
fslines = append(fslines, c.glslBlock(p, &p.FragmentFunc.Block, &p.FragmentFunc.Block, 0, 0)...)
|
||||||
fslines = append(fslines, "}")
|
fslines = append(fslines, "}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmpvslines []string
|
vs := strings.Join(vslines, "\n")
|
||||||
tmpfslines := strings.Split(FragmentPrelude, "\n")
|
fs := strings.Join(fslines, "\n")
|
||||||
|
|
||||||
// Struct types are determined after converting the program.
|
// Struct types are determined after converting the program.
|
||||||
var stlines []string
|
var stlines []string
|
||||||
@ -192,31 +184,22 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
|
|||||||
}
|
}
|
||||||
stlines = append(stlines, "};")
|
stlines = append(stlines, "};")
|
||||||
}
|
}
|
||||||
|
st := strings.Join(stlines, "\n")
|
||||||
if len(tmpvslines) > 0 {
|
vs = strings.ReplaceAll(vs, "{{.Structs}}", st)
|
||||||
tmpvslines = append(tmpvslines, "")
|
fs = strings.ReplaceAll(fs, "{{.Structs}}", st)
|
||||||
}
|
} else {
|
||||||
tmpvslines = append(stlines, tmpvslines...)
|
vs = strings.ReplaceAll(vs, "{{.Structs}}", "")
|
||||||
|
fs = strings.ReplaceAll(fs, "{{.Structs}}", "")
|
||||||
if len(tmpfslines) > 0 {
|
|
||||||
tmpfslines = append(tmpfslines, "")
|
|
||||||
}
|
|
||||||
copied := make([]string, len(stlines))
|
|
||||||
copy(copied, stlines)
|
|
||||||
tmpfslines = append(tmpfslines, copied...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tmpvslines) > 0 && len(vslines) > 0 {
|
nls := regexp.MustCompile(`\n\n+`)
|
||||||
tmpvslines = append(tmpvslines, "")
|
vs = nls.ReplaceAllString(vs, "\n\n")
|
||||||
}
|
fs = nls.ReplaceAllString(fs, "\n\n")
|
||||||
vslines = append(tmpvslines, vslines...)
|
|
||||||
|
|
||||||
if len(tmpfslines) > 0 && len(fslines) > 0 {
|
vs = strings.TrimSpace(vs) + "\n"
|
||||||
tmpfslines = append(tmpfslines, "")
|
fs = strings.TrimSpace(fs) + "\n"
|
||||||
}
|
|
||||||
fslines = append(tmpfslines, fslines...)
|
|
||||||
|
|
||||||
return strings.Join(vslines, "\n") + "\n", strings.Join(fslines, "\n") + "\n"
|
return vs, fs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *compileContext) glslType(p *shaderir.Program, t *shaderir.Type) (string, string) {
|
func (c *compileContext) glslType(p *shaderir.Program, t *shaderir.Type) (string, string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user