shaderir: Export IsValidSwizzling

This is a preparation for compiling the IR to Metal.

Updates #1165
This commit is contained in:
Hajime Hoshi 2020-08-08 19:08:07 +09:00
parent 2b89710600
commit 18b3859e20
2 changed files with 40 additions and 39 deletions

View File

@ -32,43 +32,6 @@ precision highp float;
#define highp #define highp
#endif` #endif`
func isValidSwizzling(s string) bool {
if len(s) < 1 || 4 < len(s) {
return false
}
const (
xyzw = "xyzw"
rgba = "rgba"
strq = "strq"
)
switch {
case strings.IndexByte(xyzw, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(xyzw, c) == -1 {
return false
}
}
return true
case strings.IndexByte(rgba, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(rgba, c) == -1 {
return false
}
}
return true
case strings.IndexByte(strq, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(strq, c) == -1 {
return false
}
}
return true
}
return false
}
type compileContext struct { type compileContext struct {
structNames map[string]string structNames map[string]string
structTypes []shaderir.Type structTypes []shaderir.Type
@ -175,8 +138,8 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
fs := strings.Join(fslines, "\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
if len(c.structTypes) > 0 { if len(c.structTypes) > 0 {
var stlines []string
for i, t := range c.structTypes { for i, t := range c.structTypes {
stlines = append(stlines, fmt.Sprintf("struct S%d {", i)) stlines = append(stlines, fmt.Sprintf("struct S%d {", i))
for j, st := range t.Sub { for j, st := range t.Sub {
@ -370,7 +333,7 @@ func (c *compileContext) glslBlock(p *shaderir.Program, topBlock, block *shaderi
case shaderir.BuiltinFuncExpr: case shaderir.BuiltinFuncExpr:
return builtinFuncString(e.BuiltinFunc) return builtinFuncString(e.BuiltinFunc)
case shaderir.SwizzlingExpr: case shaderir.SwizzlingExpr:
if !isValidSwizzling(e.Swizzling) { if !shaderir.IsValidSwizzling(e.Swizzling) {
return fmt.Sprintf("?(unexpected swizzling: %s)", e.Swizzling) return fmt.Sprintf("?(unexpected swizzling: %s)", e.Swizzling)
} }
return e.Swizzling return e.Swizzling

View File

@ -18,6 +18,7 @@ package shaderir
import ( import (
"go/constant" "go/constant"
"go/token" "go/token"
"strings"
) )
type Program struct { type Program struct {
@ -292,3 +293,40 @@ func ParseBuiltinFunc(str string) (BuiltinFunc, bool) {
} }
return "", false return "", false
} }
func IsValidSwizzling(s string) bool {
if len(s) < 1 || 4 < len(s) {
return false
}
const (
xyzw = "xyzw"
rgba = "rgba"
strq = "strq"
)
switch {
case strings.IndexByte(xyzw, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(xyzw, c) == -1 {
return false
}
}
return true
case strings.IndexByte(rgba, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(rgba, c) == -1 {
return false
}
}
return true
case strings.IndexByte(strq, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(strq, c) == -1 {
return false
}
}
return true
}
return false
}