shaderir: Add swizzling

This commit is contained in:
Hajime Hoshi 2020-05-17 02:24:35 +09:00
parent 7648271eff
commit 7e274050a3
3 changed files with 52 additions and 4 deletions

View File

@ -19,6 +19,40 @@ import (
"strings"
)
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
}
}
case strings.IndexByte(rgba, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(rgba, c) == -1 {
return false
}
}
case strings.IndexByte(strq, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(strq, c) == -1 {
return false
}
}
}
return false
}
func (p *Program) structName(t *Type) string {
if t.Main != Struct {
panic("shaderir: the given type at structName must be a struct")
@ -190,6 +224,11 @@ func (p *Program) glslBlock(b *Block, level int, localVarIndex int) []string {
}
case BuiltinFuncExpr:
return string(e.BuiltinFunc)
case SwizzlingExpr:
if isValidSwizzling(e.Swizzling) {
return fmt.Sprintf("?(unexpected swizzling: %s)", e.Swizzling)
}
return e.Swizzling
case Ident:
return e.Ident
case Unary:

View File

@ -98,6 +98,13 @@ func builtinFuncExpr(f BuiltinFunc) Expr {
}
}
func swizzlingExpr(swizzling string) Expr {
return Expr{
Type: SwizzlingExpr,
Swizzling: swizzling,
}
}
func identExpr(ident string) Expr {
return Expr{
Type: Ident,
@ -455,7 +462,7 @@ varying vec3 V0;`,
{Main: Vec4},
},
OutParams: []Type{
{Main: Float},
{Main: Vec2},
},
Block: block(
nil,
@ -463,15 +470,15 @@ varying vec3 V0;`,
varNameExpr(Local, 1),
fieldSelectorExpr(
varNameExpr(Local, 0),
identExpr("x"),
swizzlingExpr("xz"),
),
),
),
},
},
},
Glsl: `void F0(in vec4 l0, out float l1) {
l1 = (l0).x;
Glsl: `void F0(in vec4 l0, out vec2 l1) {
l1 = (l0).xz;
}`,
},
{

View File

@ -89,6 +89,7 @@ type Expr struct {
Int int32
Float float32
BuiltinFunc BuiltinFunc
Swizzling string
Ident string
Op Op
}
@ -100,6 +101,7 @@ const (
FloatExpr
VarName
BuiltinFuncExpr
SwizzlingExpr
Ident
Unary
Binary