예제 #1
0
        private string GenerateSetup(MetaOperator metaOp, MetaOperatorPart codeMetaOpPart)
        {
            var lines                 = new List <string>();
            var metaInputOpParts      = metaOp.GetOrderedInputs();
            var imageMetaInputOpParts = (from p in metaInputOpParts
                                         where p.OpPart.Type == FunctionType.Image
                                         select p).ToList();

            foreach (var input in imageMetaInputOpParts)
            {
                var inputName = Utilities.AdjustOpPartNameForCode(input.Name);
                lines.Add(string.Format("using (var {0}View = new ShaderResourceView(context.D3DDevice, {0}))",
                                        inputName));
            }

            lines.Add("{");
            lines.Add("    _effect.GetVariableByName(\"RenderTargetSize\").AsVector().Set(new Vector2(_usedViewport.Width, _usedViewport.Height));");

            var inputGroups = from input in metaInputOpParts
                              group input by input.Name.Split(new[] { '.' })[0] into g
                              select new { Name = g.Key, Inputs = g.ToArray() };

            foreach (var group in inputGroups)
            {
                var inputGroupName = Utilities.AdjustOpPartNameForCode(group.Name);

                switch (group.Inputs[0].OpPart.Type)
                {
                case FunctionType.Float:
                    switch (group.Inputs.Length)
                    {
                    case 1:
                        lines.Add(string.Format("    _effect.GetVariableByName(\"{0}\").AsScalar().Set({0});",
                                                inputGroupName));
                        break;

                    case 2:
                    case 3:
                    case 4:
                        string vectorParams = Utilities.AdjustOpPartNameForCode(group.Inputs[0].Name);
                        for (int i = 1; i < group.Inputs.Length; ++i)
                        {
                            vectorParams += ", " + Utilities.AdjustOpPartNameForCode(group.Inputs[i].Name);
                        }
                        lines.Add(string.Format("    _effect.GetVariableByName(\"{0}\").AsVector().Set(new Vector{1}({2}));",
                                                inputGroupName, group.Inputs.Length, vectorParams));
                        break;
                    }
                    break;

                case FunctionType.Image:
                    lines.Add(string.Format("    _effect.GetVariableByName(\"{0}\").AsShaderResource().SetResource({0}View);", inputGroupName));
                    break;

                default:
                    break;
                }
            }
            return(string.Join("\n", lines));
        }
예제 #2
0
        private string GenerateOutputIds(MetaOperator metaOp, MetaOperatorPart codeMetaOpPart)
        {
            var lines   = new List <string>();
            var outputs = metaOp.GetOrderedOutputs().ToList();

            outputs.Reverse();
            if (outputs.Count > 0)
            {
                var outputName = Utilities.AdjustOpPartNameForCode(outputs[0].Name);
                lines.Insert(0, string.Format("    {0} = {1}", outputName, outputs.Count - 1));
            }
            for (int i = 1; i < outputs.Count; ++i)
            {
                var outputName = Utilities.AdjustOpPartNameForCode(outputs[i].Name);
                lines.Insert(0, string.Format("    {0} = {1},", outputName, outputs.Count - 1 - i));
            }
            lines.Insert(0, "{");
            lines.Insert(0, "private enum OutputId");

            lines.Add("}");
            return(string.Join("\n", lines));
        }
예제 #3
0
        private string GenerateParameters(MetaOperator metaOp, MetaOperatorPart codeMetaOpPart)
        {
            var inputGroups = from input in metaOp.GetOrderedInputs()
                              group input by input.Name.Split(new[] { '.' })[0] into g
                              select new { Name = g.Key, Inputs = g.ToArray() };

            var lines = new List <string>();

            foreach (var group in inputGroups)
            {
                var inputGroupName = Utilities.AdjustOpPartNameForCode(group.Name);

                switch (group.Inputs[0].OpPart.Type)
                {
                case FunctionType.Float:
                    switch (group.Inputs.Length)
                    {
                    case 1:
                        lines.Add(string.Format("float {0};", inputGroupName));
                        break;

                    case 2:
                    case 3:
                    case 4:
                        lines.Add(string.Format("float{0} {1};", group.Inputs.Length, inputGroupName));
                        break;
                    }
                    break;

                case FunctionType.Image:
                    lines.Add(string.Format("Texture2D {0};", inputGroupName));
                    break;

                default:
                    break;
                }
            }
            return(string.Join("\n", lines));
        }
예제 #4
0
        private string GenerateParams(MetaOperator metaOp, MetaOperatorPart codeMetaOpPart)
        {
            var lines = new List <string>();
            int idx   = 0;

            var inputGroups = from input in metaOp.GetOrderedInputs()
                              group input by input.Name.Split(new[] { '.' })[0] into g
                              select new { Name = g.Key, Inputs = g.ToArray() };

            foreach (var group in inputGroups)
            {
                foreach (var input in group.Inputs)
                {
                    var inputName = Utilities.AdjustOpPartNameForCode(input.Name);

                    switch (input.OpPart.Type)
                    {
                    case FunctionType.Float:
                    {
                        var typeConverter = input.IsEnum ? "(int) " : string.Empty;
                        lines.Add(string.Format("var {0} = {1}inputs[(int)InputId.{0}].Eval(context).Value;", inputName, typeConverter));
                        break;
                    }

                    case FunctionType.Text:
                        lines.Add(string.Format("var {0} = inputs[(int)InputId.{0}].Eval(context).Text;", inputName));
                        break;

                    case FunctionType.Image:
                        lines.Add(string.Format("var {0} = inputs[(int)InputId.{0}].Eval(context).Image; // Needs to be checked for null!", inputName));
                        break;

                    case FunctionType.Dynamic:
                        lines.Add(string.Format("var {0} = inputs[(int)InputId.{0}].Eval(context).Dynamic;  // Needs to be checked for null!", inputName));
                        break;

                    case FunctionType.Mesh:
                        lines.Add(string.Format("var {0} = inputs[(int)InputId.{0}].Eval(context).Mesh;", inputName));
                        break;

                    case FunctionType.Generic:
                    case FunctionType.Scene:
                    default:
                        lines.Add(string.Format("var {0} = inputs[(int)InputId.{0}];", inputName));
                        break;
                    }
                    ++idx;
                }

                if (group.Inputs.Count() > 1)
                {
                    // real group -> generate specific data type object (e.g. Color4, Vector3, ...)
                    var    groupName           = Utilities.AdjustOpPartNameForCode(group.Name);
                    string type                = string.Empty;
                    var    firstInputName      = group.Inputs[0].Name;
                    var    extensionIndex      = firstInputName.IndexOf('.') + 1;
                    var    firstInputExtension = firstInputName.Substring(extensionIndex, firstInputName.Length - extensionIndex);
                    var    typeSize            = group.Inputs.Count().ToString();
                    if (firstInputExtension == "R")
                    {
                        type = "Color4";
                    }
                    else if (firstInputExtension == "X")
                    {
                        type = "Vector" + typeSize;
                    }
                    else if (firstInputExtension == "Width")
                    {
                        type = "Vector" + typeSize;
                    }
                    else
                    {
                        type = "Vector" + typeSize;
                    }

                    var groupLineBuilder = new StringBuilder(string.Format("var {0} = new {1}(", groupName, type));
                    foreach (var input in group.Inputs)
                    {
                        var inputName = Utilities.AdjustOpPartNameForCode(input.Name);
                        groupLineBuilder.AppendFormat("{0}, ", inputName);
                    }
                    groupLineBuilder.Remove(groupLineBuilder.Length - 2, 2); // remove last ", "
                    groupLineBuilder.Append(");");
                    lines.Add(groupLineBuilder.ToString());
                }
            }
            return(string.Join("\n", lines));
        }