示例#1
0
            public static VariableStruct CreateVariableStruct(ref FileStruct.TSAreaVariable tsAreaVariable)
            {
                VariableStruct ss = new VariableStruct();

                ss.varName  = Utils.CharsToString(tsAreaVariable.varName);
                ss.varValue = tsAreaVariable.varValue;
                return(ss);
            }
        public void Compile()
        {
            SourceCode = "// Auto generated shader (HLSL) \n\n";
            
            // Create dictionaries
            dictonaryInputStruct = new Dictionary<Node, VariableStruct>();
            dictonaryOutputStruct = new Dictionary<Node, VariableStruct>();

            // Varyings struct definition
            SourceCode += "// Varying struct definition \n\n";

            varyingIdentifier = "IOVaryings";

            SourceCode += "struct " + varyingIdentifier + " {\n";
            SourceCode += "\t float4 position;\n";
            SourceCode += "\t float4 normal;\n";
            SourceCode += "\t float4 camera;\n";
            SourceCode += "};\n\n";

            // Add all input and output structs definitions
            SourceCode += "// Input and output struct definitions \n\n";

            foreach (Node node in fileState.Nodes)
            {
                VariableStruct inputStruct = new VariableStruct(node, Variable.VariableType.Input);
                dictonaryInputStruct[node] = inputStruct;

                SourceCode += inputStruct.HLSLDescription;

                VariableStruct outputStruct = new VariableStruct(node, Variable.VariableType.Output);
                dictonaryOutputStruct[node] = outputStruct;

                SourceCode += outputStruct.HLSLDescription;
            }

            // Find the output node
            outputNode = null;

            foreach (Node node in fileState.Nodes)
            {
                if (node.inode.IsOutputNode()) {
                    outputNode = node;
                    break;
                }
            }

            dictionaryGetIdentifiers = new Dictionary<Node, string>();

            if (outputNode == null)
                return;

            // Add all execute functions
            SourceCode += "// Execution functions \n\n";

            dictionaryExecuteIdentifiers = new Dictionary<Node, string>();
                 
            foreach (Node node in fileState.Nodes)
            {
                VariableStruct inputStruct = dictonaryInputStruct[node];
                VariableStruct outputStruct = dictonaryOutputStruct[node];

                string outputType = outputStruct.Identifier;

                if (node == outputNode)
                    outputType = inputStruct.Identifier;

                string executionIdentifier = "exec" + getUniqueID();

                dictionaryExecuteIdentifiers[node] = executionIdentifier;

                SourceCode += outputType + " " + executionIdentifier + "(" + varyingIdentifier + " varyings, " + inputStruct.Identifier + " input) {\n";

                SourceCode += "\t" + outputType + " output;\n";

                Dictionary<Variable, string> mergedDictionary = new Dictionary<Variable,string>();

                foreach (Variable variable in inputStruct.dictonaryVariableIdentifiers.Keys)
                    mergedDictionary[variable] = inputStruct.dictonaryVariableIdentifiers[variable];

                foreach (Variable variable in outputStruct.dictonaryVariableIdentifiers.Keys)
                    mergedDictionary[variable] = outputStruct.dictonaryVariableIdentifiers[variable];

                SourceCode += node.inode.GetSource(mergedDictionary);
                
                SourceCode += "\treturn output;\n";

                SourceCode += "}\n\n";
            }

            // Add all get functions
            SourceCode += "// Get functions \n\n";

            generateGetFunctions(outputNode);

            // Add the interface function
            
            SourceCode += "float4 getColor(float4 position, float4 normal, float4 camera) {\n";

            SourceCode += "\t" + varyingIdentifier + " varyings;\n";
            SourceCode += "\tvaryings.position = position;\n";
            SourceCode += "\tvaryings.normal = normal;\n";
            SourceCode += "\tvaryings.camera = camera;\n";

            SourceCode += "\t" + dictonaryInputStruct[outputNode].Identifier + " output;\n";
            SourceCode += "\toutput = " + dictionaryGetIdentifiers[outputNode] + "(varyings); \n\n";

            string outputVarName = dictonaryInputStruct[outputNode].dictonaryVariableIdentifiers[outputNode.Variables[0]];

            if (outputNode.Variables[0].InputType == Variable.InputTypes.Link && outputNode.Variables[0].GetLinks().Count == 1)
            {
                if (outputNode.Variables[0].GetLinks()[0].OutputVariable.IsFloat4())
                    SourceCode += "\tfloat4 color = output." + outputVarName + ";\n";
                else if (outputNode.Variables[0].GetLinks()[0].OutputVariable.IsFloat3())
                    SourceCode += "\tfloat4 color = float4(output." + outputVarName + ", 1);\n";
                else if (outputNode.Variables[0].GetLinks()[0].OutputVariable.IsFloat2())
                    SourceCode += "\tfloat4 color = float4(output." + outputVarName + ", 0, 1);\n";
                else
                    SourceCode += "\tfloat4 color = float4(output." + outputVarName + ", output." + outputVarName + ", output." + outputVarName + ", 1);\n";
            }
            else
            {
                SourceCode += "\tfloat4 color = output." + outputVarName + ";\n";
            }
             
            SourceCode += "\treturn color;\n";
            SourceCode += "}\n";
            
        }