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
#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 {
structNames map[string]string
structTypes []shaderir.Type
@ -175,8 +138,8 @@ func Compile(p *shaderir.Program) (vertexShader, fragmentShader string) {
fs := strings.Join(fslines, "\n")
// Struct types are determined after converting the program.
var stlines []string
if len(c.structTypes) > 0 {
var stlines []string
for i, t := range c.structTypes {
stlines = append(stlines, fmt.Sprintf("struct S%d {", i))
for j, st := range t.Sub {
@ -370,7 +333,7 @@ func (c *compileContext) glslBlock(p *shaderir.Program, topBlock, block *shaderi
case shaderir.BuiltinFuncExpr:
return builtinFuncString(e.BuiltinFunc)
case shaderir.SwizzlingExpr:
if !isValidSwizzling(e.Swizzling) {
if !shaderir.IsValidSwizzling(e.Swizzling) {
return fmt.Sprintf("?(unexpected swizzling: %s)", e.Swizzling)
}
return e.Swizzling

View File

@ -18,6 +18,7 @@ package shaderir
import (
"go/constant"
"go/token"
"strings"
)
type Program struct {
@ -292,3 +293,40 @@ func ParseBuiltinFunc(str string) (BuiltinFunc, bool) {
}
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
}