Commits

Michael Ludwig committed f907925

Add loop and function implementation shells.

  • Participants
  • Parent commits f376cc8

Comments (0)

Files changed (12)

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/FunctionBuilder.java

 
     public FunctionBuilder out(Type type, String name);
 
-    public Function invoke(Statement statement);
+    public Function invoke(Statement... body);
 }

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/ShaderBuilder.java

+package com.ferox.resource.shader;
+
+import com.ferox.resource.GlslShader;
+import com.ferox.resource.shader.simple_grammar.Constant;
+import com.ferox.resource.shader.simple_grammar.DoWhileLoop;
+import com.ferox.resource.shader.simple_grammar.ForLoop;
+import com.ferox.resource.shader.simple_grammar.FunctionDefinition;
+import com.ferox.resource.shader.simple_grammar.IfThenElse;
+import com.ferox.resource.shader.simple_grammar.Jump;
+import com.ferox.resource.shader.simple_grammar.Jump.JumpType;
+import com.ferox.resource.shader.simple_grammar.Variable;
+import com.ferox.resource.shader.simple_grammar.WhileLoop;
+
+public final class ShaderBuilder {
+    // FIXME define public static final Function definitions for builtin functions
+
+    private ShaderBuilder() {}
+
+    public static VertexShaderBuilder newVertexShader();
+
+    public static FragmentShaderBuilder newFragmentShader();
+
+    public static FragmentShaderBuilder newFragmentShader(VertexShader vertexStage);
+
+    public static GlslShader build(VertexShader vertexShader,
+                                   FragmentShader fragmentShader);
+
+    public static Type arrayOf(Type type, int len) {
+
+    }
+
+    public static Type arrayOf(Type type) {
+        return arrayOf(type, -1);
+    }
+
+    public static StructBuilder struct(String structName) {
+
+    }
+
+    public static Expression v(float v) {
+        return new Constant(v);
+    }
+
+    public static Expression v(int v) {
+        return new Constant(v);
+    }
+
+    public static Expression v(boolean v) {
+        return new Constant(v);
+    }
+
+    public static LValue v(String name) {
+        return new Variable(name);
+    }
+
+    public static FunctionBuilder function(Type type, String name) {
+        return new FunctionDefinition.Builder(type, name);
+    }
+
+    public static LValue declare(Type type, String name) {
+
+    }
+
+    public static IfBuilder if_(Expression condition) {
+        return new IfThenElse.Builder(condition);
+    }
+
+    public static DoWhileBuilder do_(Statement... body) {
+        return new DoWhileLoop.Builder(body);
+    }
+
+    public static WhileBuilder while_(Expression condition) {
+        return new WhileLoop.Builder(condition);
+    }
+
+    public static WhileBuilder for_(Expression init, Expression check,
+                                    Expression increment) {
+        return new ForLoop.Builder(init, check, increment);
+    }
+
+    public static Statement return_(Expression value) {
+        return new Jump(value);
+    }
+
+    public static Statement return_() {
+        return new Jump((Expression) null);
+    }
+
+    public static Statement break_() {
+        return new Jump(JumpType.BREAK);
+    }
+
+    public static Statement continue_() {
+        return new Jump(JumpType.CONTINUE);
+    }
+
+    public static Statement discard() {
+        return new Jump(JumpType.DISCARD);
+    }
+}

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/BuiltInFunction.java

+package com.ferox.resource.shader.simple_grammar;
+
+public class BuiltInFunction {
+
+}

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/ConditionalSelect.java

-package com.ferox.resource.shader.simple_grammar;
-
-public class ConditionalSelect implements Expression, RightAssociative {
-    private final Expression condition;
-    private final Expression onTrue;
-    private final Expression onFalse;
-
-    public ConditionalSelect(Expression condition, Expression onTrue,
-                                       Expression onFalse) {
-        this.condition = condition;
-        this.onTrue = onTrue;
-        this.onFalse = onFalse;
-    }
-}

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/DoWhileLoop.java

 package com.ferox.resource.shader.simple_grammar;
 
+import com.ferox.resource.shader.DoWhileBuilder;
+import com.ferox.resource.shader.Environment;
+import com.ferox.resource.shader.Expression;
+import com.ferox.resource.shader.ShaderAccumulator;
+import com.ferox.resource.shader.Statement;
+
 public class DoWhileLoop implements Statement {
-    private final Statement body;
+    private final Statement[] body;
     private final Expression condition;
 
-    public DoWhileLoop(Statement body, Expression condition) {
+    public DoWhileLoop(Statement[] body, Expression condition) {
         this.condition = condition;
         this.body = body;
     }
+
+    @Override
+    public Environment validate(Environment environment) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void emit(ShaderAccumulator accumulator) {
+        // TODO Auto-generated method stub
+
+    }
+
+    public static class Builder implements DoWhileBuilder {
+        private final Statement[] body;
+
+        public Builder(Statement[] body) {
+            this.body = body;
+        }
+
+        @Override
+        public Statement while_(Expression condition) {
+            return new DoWhileLoop(body, condition);
+        }
+    }
 }

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/ForLoop.java

 package com.ferox.resource.shader.simple_grammar;
 
+import com.ferox.resource.shader.Environment;
+import com.ferox.resource.shader.Expression;
+import com.ferox.resource.shader.ShaderAccumulator;
+import com.ferox.resource.shader.Statement;
+import com.ferox.resource.shader.WhileBuilder;
+
 public class ForLoop implements Statement {
-    private final ForLoopInitializer initStatement;
-    private final WhileLoopCondition condition; // nullable for no condition
+    private final Expression initStatement;
+    private final Expression condition; // nullable for no condition
     private final Expression increment; // nullable for no increment
-    private final Statement body;
+    private final Statement[] body;
 
-    public ForLoop(ForLoopInitializer initStatement, WhileLoopCondition condition,
-                   Expression increment, Statement body) {
+    public ForLoop(Expression initStatement, Expression condition, Expression increment,
+                   Statement[] body) {
         this.initStatement = initStatement;
         this.condition = condition;
         this.increment = increment;
         this.body = body;
     }
+
+    @Override
+    public Environment validate(Environment environment) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void emit(ShaderAccumulator accumulator) {
+        // TODO Auto-generated method stub
+
+    }
+
+    public static class Builder implements WhileBuilder {
+        private final Expression initStatement;
+        private final Expression condition;
+        private final Expression increment;
+
+        public Builder(Expression initStatement, Expression condition,
+                       Expression increment) {
+            this.initStatement = initStatement;
+            this.condition = condition;
+            this.increment = increment;
+        }
+
+        @Override
+        public Statement do_(Statement... statement) {
+            return new ForLoop(initStatement, condition, increment, statement);
+        }
+    }
 }

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/FunctionCall.java

 package com.ferox.resource.shader.simple_grammar;
 
-public class FunctionCall implements Expression {
-    // this is a reasonable consolidation of the grammar for
-    // function calls with and without parameters
-    private final String identifier; // includes constructor calls for built-in types, etc
+import com.ferox.resource.shader.Environment;
+import com.ferox.resource.shader.Expression;
+import com.ferox.resource.shader.Function;
+import com.ferox.resource.shader.ShaderAccumulator;
+import com.ferox.resource.shader.Type;
+
+public class FunctionCall extends AbstractExpression {
+    private final Function function;
     private final Expression[] parameters;
 
-    public FunctionCall(String identifier, Expression... parameters) {
-        this.identifier = identifier;
+    public FunctionCall(Function function, Expression... parameters) {
+        this.function = function;
         this.parameters = parameters;
     }
+
+    @Override
+    public Type getType(Environment env) {
+        return function.getReturnType();
+    }
+
+    @Override
+    public Environment validate(Environment environment) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void emit(ShaderAccumulator accumulator) {
+        // TODO Auto-generated method stub
+
+    }
 }

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/FunctionDefinition.java

 package com.ferox.resource.shader.simple_grammar;
 
-public class FunctionDefinition implements ExternalDeclaration {
-    private final FunctionPrototype prototype;
-    private final CompoundStatement body;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
-    public FunctionDefinition(FunctionPrototype prototype, CompoundStatement body) {
-        this.prototype = prototype;
+import com.ferox.resource.shader.Expression;
+import com.ferox.resource.shader.Function;
+import com.ferox.resource.shader.FunctionBuilder;
+import com.ferox.resource.shader.Statement;
+import com.ferox.resource.shader.Type;
+import com.ferox.resource.shader.simple_grammar.Parameter.ParameterQualifier;
+
+public class FunctionDefinition implements Function {
+    private final String name;
+    private final Type returnType;
+
+    private final Parameter[] params;
+    private final Map<String, Type> paramMap;
+
+    private final Statement[] body;
+
+    public FunctionDefinition(String name, Type returnType, Parameter[] params,
+                              Statement[] body) {
+        this.name = name;
+        this.returnType = returnType;
+        this.params = params;
         this.body = body;
+
+        Map<String, Type> paramMap = new HashMap<String, Type>();
+        for (Parameter p : params) {
+            paramMap.put(p.getName(), p.getType());
+        }
+        this.paramMap = Collections.unmodifiableMap(paramMap);
+    }
+
+    @Override
+    public Expression call(Expression... parameters) {
+        return new FunctionCall(this, parameters);
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public Map<String, Type> getParameters() {
+        return paramMap;
+    }
+
+    @Override
+    public Type getReturnType() {
+        return returnType;
+    }
+
+    public static class Builder implements FunctionBuilder {
+        private final List<Parameter> params;
+        private final Type returnType;
+        private final String name;
+
+        public Builder(Type returnType, String name) {
+            this.name = name;
+            this.returnType = returnType;
+            params = new ArrayList<Parameter>();
+        }
+
+        @Override
+        public FunctionBuilder in(Type type, String name) {
+            params.add(new Parameter(ParameterQualifier.IN, type, name));
+            return this;
+        }
+
+        @Override
+        public FunctionBuilder inOut(Type type, String name) {
+            params.add(new Parameter(ParameterQualifier.INOUT, type, name));
+            return this;
+        }
+
+        @Override
+        public FunctionBuilder out(Type type, String name) {
+            params.add(new Parameter(ParameterQualifier.OUT, type, name));
+            return this;
+        }
+
+        @Override
+        public Function invoke(Statement... body) {
+            return new FunctionDefinition(name,
+                                          returnType,
+                                          params.toArray(new Parameter[params.size()]),
+                                          body);
+        }
     }
 }

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/FunctionPrototype.java

-package com.ferox.resource.shader.simple_grammar;
-
-import com.ferox.resource.shader.grammar.ParameterDeclaration;
-
-public class FunctionPrototype implements Declaration {
-    private final Type type;
-    private final ParameterDeclaration[] parameters;
-
-    public FunctionPrototype(Type type, ParameterDeclaration... parameters) {
-        this.type = type;
-        this.parameters = parameters;
-    }
-}

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/IfThenElse.java

 package com.ferox.resource.shader.simple_grammar;
 
+import com.ferox.resource.shader.Environment;
+import com.ferox.resource.shader.Expression;
+import com.ferox.resource.shader.IfBuilder;
+import com.ferox.resource.shader.ShaderAccumulator;
+import com.ferox.resource.shader.Statement;
+
 public class IfThenElse implements Statement {
     private final Expression condition;
-    private final Statement onTrue;
-    private final Statement onFalse; // null for no else branch
+    private final Statement[] onTrue;
+    private final Statement[] onFalse; // null for no else branch
 
-    public IfThenElse(Expression condition, Statement onTrue, Statement onFalse) {
+    public IfThenElse(Expression condition, Statement[] onTrue, Statement[] onFalse) {
         this.condition = condition;
         this.onTrue = onTrue;
         this.onFalse = onFalse;
     }
+
+    @Override
+    public Environment validate(Environment environment) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void emit(ShaderAccumulator accumulator) {
+        // TODO Auto-generated method stub
+
+    }
+
+    public static class Builder implements IfBuilder {
+        private final Expression condition;
+        private Statement[] onTrue;
+
+        public Builder(Expression condition) {
+            this.condition = condition;
+            onTrue = null;
+        }
+
+        @Override
+        public IfBuilder then(Statement... body) {
+            if (onTrue != null) {
+                throw new IllegalStateException("Already assigned body for true branch");
+            }
+            onTrue = body;
+            return this;
+        }
+
+        @Override
+        public Statement fi() {
+            if (onTrue == null) {
+                throw new IllegalStateException("Must specify a true branch first");
+            }
+            return new IfThenElse(condition, onTrue, null);
+        }
+
+        @Override
+        public Statement else_(Statement... body) {
+            if (onTrue == null) {
+                throw new IllegalStateException("Must specify a true branch first");
+            }
+            return new IfThenElse(condition, onTrue, body);
+        }
+    }
 }

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/Parameter.java

+package com.ferox.resource.shader.simple_grammar;
+
+import com.ferox.resource.shader.Type;
+
+public class Parameter {
+    public static enum ParameterQualifier {
+        IN,
+        OUT,
+        INOUT,
+        NONE
+    }
+
+    private final Type type;
+    private final String name;
+    private final ParameterQualifier qualifier;
+
+    public Parameter(ParameterQualifier qualifier, Type type, String name) {
+        this.qualifier = qualifier;
+        this.type = type;
+        this.name = name;
+    }
+
+    public ParameterQualifier getQualifier() {
+        return qualifier;
+    }
+
+    public Type getType() {
+        return type;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

File ferox-renderer/ferox-renderer-api/src/main/java/com/ferox/resource/shader/simple_grammar/WhileLoop.java

 package com.ferox.resource.shader.simple_grammar;
 
+import com.ferox.resource.shader.Environment;
+import com.ferox.resource.shader.Expression;
+import com.ferox.resource.shader.ShaderAccumulator;
+import com.ferox.resource.shader.Statement;
+import com.ferox.resource.shader.WhileBuilder;
+
 public class WhileLoop implements Statement {
-    private final WhileLoopCondition condition;
-    private final Statement body;
+    private final Expression condition;
+    private final Statement[] body;
 
-    public WhileLoop(WhileLoopCondition condition, Statement body) {
+    public WhileLoop(Expression condition, Statement[] body) {
         this.condition = condition;
         this.body = body;
     }
+
+    @Override
+    public Environment validate(Environment environment) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void emit(ShaderAccumulator accumulator) {
+        // TODO Auto-generated method stub
+
+    }
+
+    public static class Builder implements WhileBuilder {
+        private final Expression condition;
+
+        public Builder(Expression condition) {
+            this.condition = condition;
+        }
+
+        @Override
+        public Statement do_(Statement... statement) {
+            return new WhileLoop(condition, statement);
+        }
+    }
 }