Beispiel #1
0
        public static string ArgAssignedToOutputTest()
        {
            using (HlslProgram program = new HlslProgram())
            {
                Type vsData = CreateVSType(program);

                UserDefinedFunction udf = new UserDefinedFunction("vs_main");
                Value argValue = udf.AddArgument(vsData);

                DeclExpr output = new DeclExpr(vsData, argValue);
                udf.AddExpr(output);
                udf.AddExpr(new ReturnExpr(output));

                program.AddFunction(udf);
                return program.EmitRawShaderCode();
            }
        }
Beispiel #2
0
        public static string SimpleFunctionCallTest()
        {
            using (HlslProgram program = new HlslProgram())
            {
                Type vsData = CreateVSType(program);
                Type f1 = TypeRegistry.GetFloatType();
                Type f4 = TypeRegistry.GetVectorType(f1, 4);

                DeclExpr wvpMatrixDecl = new DeclExpr(TypeRegistry.GetMatrixType(f4, 4), "WorldViewProjection");
                program.AddGlobal(wvpMatrixDecl);

                UserDefinedFunction udf = new UserDefinedFunction("vs_main");
                Value argValue = udf.AddArgument(vsData);

                DeclExpr output = new DeclExpr(vsData);
                udf.AddExpr(output);

                // Initialize the position element -- multiply input position by WVP matrix.
                Function fn = program.GetFunctionByName("mul");
                CallExpr wvpMul = new CallExpr(fn, new Expr[] { new StructMemberExpr(argValue, "position"), wvpMatrixDecl });
                udf.AddExpr(new AssignmentExpr(new StructMemberExpr(output.Value, "position").Value, wvpMul.Value));

                // Initialize the rest of the struct to zero.
                StructMemberExpr[] otherMembers = new StructMemberExpr[] {
                    new StructMemberExpr(output.Value, 1),
                    new StructMemberExpr(output.Value, 2),
                    new StructMemberExpr(output.Value, 3),
                    new StructMemberExpr(output.Value, 4),
                };

                udf.AddExpr(new CommentExpr("Ensuring that a valid comment is being emitted."));
                udf.AddExpr(new CommentExpr("Even one that consists of multiple lines."));
                udf.AddExpr(new CommentExpr(string.Format("Or embedded newlines.{0}Like this!", Environment.NewLine)));
                udf.AddExpr(new CommentExpr("Or this.\n(not a proper newline.)"));

                foreach (StructMemberExpr SME in otherMembers)
                    udf.AddExpr(new AssignmentExpr(SME.Value, new LiteralExpr(SME.Value.ValueType).Value));

                udf.AddExpr(new ReturnExpr(output));

                program.AddFunction(udf);
                return program.EmitRawShaderCode();
            }
        }
Beispiel #3
0
        public static string SimpleStructMemberTest()
        {
            using (HlslProgram program = new HlslProgram())
            {
                Type vsData = CreateVSType(program);

                UserDefinedFunction udf = new UserDefinedFunction("vs_main");
                Value argValue = udf.AddArgument(vsData);

                DeclExpr output = new DeclExpr(vsData);
                udf.AddExpr(output);
                udf.AddExpr(new AssignmentExpr(
                    new StructMemberExpr(output.Value, "position").Value,
                    new StructMemberExpr(argValue, "position").Value));

                StructMemberExpr[] otherMembers = new StructMemberExpr[] {
                    new StructMemberExpr(output.Value, 1),
                    new StructMemberExpr(output.Value, 2),
                    new StructMemberExpr(output.Value, 3),
                    new StructMemberExpr(output.Value, 4),
                };

                foreach (StructMemberExpr SME in otherMembers)
                    udf.AddExpr(new AssignmentExpr(SME.Value, new LiteralExpr(SME.Value.ValueType).Value));

                udf.AddExpr(new ReturnExpr(output));

                program.AddFunction(udf);
                return program.EmitRawShaderCode();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Construct a ForExpr with an attribute and an unroll depth.
        /// </summary>
        /// <param name="initializer">Initializer expression.</param>
        /// <param name="test">Test expression, must evaluate to a scalar boolean.</param>
        /// <param name="update">Update expression.</param>
        /// <param name="attributes">Loop attribute.</param>
        /// <param name="unrollDepth">Depth with which to unroll the loop, used with LoopAttributes.UNROLL</param>
        public ForExpr(DeclExpr initializer, Expr test, Expr update, LoopAttributes attributes, int unrollDepth)
        {
            if (!test.HasValue())
                throw new HlslDomException("Test expression doesn't return a value!");

            if (!(test.Value.ValueType is BoolType))
                throw new HlslDomException("Test expression does not return a boolean value!");

            Initializer = initializer;
            Test = test;
            Update = update;
            Attributes = attributes;
            UnrollDepth = unrollDepth;
        }
Beispiel #5
0
 /// <summary>
 /// Construct a ForExpr with an attribute.
 /// </summary>
 /// <param name="initializer">Initializer expression.</param>
 /// <param name="test">Test expression, must evaluate to a scalar boolean.</param>
 /// <param name="update">Update expression.</param>
 /// <param name="attributes">Loop attribute.</param>
 public ForExpr(DeclExpr initializer, Expr test, Expr update, LoopAttributes attributes)
     : this(initializer, test, update, attributes, 0)
 {
     if (attributes == LoopAttributes.UNROLL)
         throw new HlslDomException("Unroll attribute specified without an unroll depth!");
 }
Beispiel #6
0
 /// <summary>
 /// Construct a ForExpr.
 /// </summary>
 /// <param name="initializer">Initializer expression.</param>
 /// <param name="test">Test expression, must evaluate to a scalar boolean.</param>
 /// <param name="update">Update expression.</param>
 public ForExpr(DeclExpr initializer, Expr test, Expr update)
     : this(initializer, test, update, (int)LoopAttributes.NO_ATTRIBUTE, 0)
 {
 }