shader: Implement cap function

Fixes #1361
This commit is contained in:
Hajime Hoshi 2020-10-17 23:36:07 +09:00
parent 75158feccf
commit 185e367295
4 changed files with 20 additions and 3 deletions

View File

@ -220,13 +220,13 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
// For built-in functions, we can call this in this position. Return an expression for the function
// call.
if callee.Type == shaderir.BuiltinFuncExpr {
if callee.BuiltinFunc == shaderir.Len {
if callee.BuiltinFunc == shaderir.Len || callee.BuiltinFunc == shaderir.Cap {
if len(args) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("number of len's arguments must be 1 but %d", len(args)))
cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 1 but %d", callee.BuiltinFunc, len(args)))
return nil, nil, nil, false
}
if argts[0].Main != shaderir.Array {
cs.addError(e.Pos(), fmt.Sprintf("len takes an array but %s", argts[0].String()))
cs.addError(e.Pos(), fmt.Sprintf("%s takes an array but %s", callee.BuiltinFunc, argts[0].String()))
return nil, nil, nil, false
}
return []shaderir.Expr{

View File

@ -1,4 +1,5 @@
void F0(in vec2 l0, out int l1);
void F1(in vec2 l0, out int l1);
void F0(in vec2 l0, out int l1) {
int l2[2];
@ -7,3 +8,12 @@ void F0(in vec2 l0, out int l1) {
l1 = 2;
return;
}
void F1(in vec2 l0, out int l1) {
int l2[3];
l2[0] = 0;
l2[1] = 0;
l2[2] = 0;
l1 = 3;
return;
}

View File

@ -4,3 +4,8 @@ func Foo(foo vec2) int {
var a [2]int
return len(a)
}
func Bar(foo vec2) int {
var a [3]int
return cap(a)
}

View File

@ -203,6 +203,7 @@ type BuiltinFunc string
const (
Len BuiltinFunc = "len"
Cap BuiltinFunc = "cap"
BoolF BuiltinFunc = "bool"
IntF BuiltinFunc = "int"
FloatF BuiltinFunc = "float"
@ -257,6 +258,7 @@ const (
func ParseBuiltinFunc(str string) (BuiltinFunc, bool) {
switch BuiltinFunc(str) {
case Len,
Cap,
BoolF,
IntF,
FloatF,