Exemplo n.º 1
0
        public UniformParameter(GpuProgramParameters.AutoConstantType autoConstantType, Real autoConstantData, int size,
                                GpuProgramParameters.GpuConstantType type)
            : base(
                Parameter.AutoParameters[autoConstantType].Type, Parameter.AutoParameters[autoConstantType].Name,
                SemanticType.Unknown, -1, ContentType.Unknown, size)
        {
            AutoShaderParameter parameterDef = Parameter.AutoParameters[autoConstantType];

            _name = parameterDef.Name;
            if (autoConstantData != 0.0)
            {
                _name += autoConstantData.ToString();
                //replace possible illegal point character in name
                _name = _name.Replace('.', '_');
            }
            _type     = type;
            _semantic = SemanticType.Unknown;
            _index    = -1;
            _content  = Parameter.ContentType.Unknown;
            this.isAutoConstantReal   = true;
            this.isAutoConstantInt    = false;
            this.autoConstantType     = autoConstantType;
            this.autoConstantRealData = autoConstantData;
            this.variability          = (int)GpuProgramParameters.GpuParamVariability.Global;
            this._params       = null;
            this.physicalIndex = -1;
            _size = size;
        }
Exemplo n.º 2
0
 public UniformParameter(GpuProgramParameters.GpuConstantType type, string name, SemanticType semantic,
                         int index, ContentType content, int variability, int size)
     : base(type, name, semantic, index, content, size)
 {
     this.isAutoConstantInt   = false;
     this.isAutoConstantReal  = false;
     this.autoConstantIntData = 0;
     this.variability         = variability;
     this._params             = null;
     this.physicalIndex       = -1;
 }
Exemplo n.º 3
0
            protected static bool getConstantType(AbstractNode i, out GpuProgramParameters.GpuConstantType op)
            {
                op = GpuProgramParameters.GpuConstantType.Unknown;
                string val;

                getString(i, out val);

                if (val.Contains("float"))
                {
                    var count = 1;
                    if (val.Length == 6)
                    {
                        count = int.Parse(val.Substring(5));
                    }
                    else if (val.Length > 6)
                    {
                        return(false);
                    }

                    if (count > 4 || count == 0)
                    {
                        return(false);
                    }

                    op = GpuProgramParameters.GpuConstantType.Float1 + count - 1;
                }
                else if (val.Contains("int"))
                {
                    var count = 1;
                    if (val.Length == 4)
                    {
                        count = int.Parse(val.Substring(3));
                    }
                    else if (val.Length > 4)
                    {
                        return(false);
                    }

                    if (count > 4 || count == 0)
                    {
                        return(false);
                    }

                    op = GpuProgramParameters.GpuConstantType.Int1 + count - 1;
                }
                else if (val.Contains("matrix"))
                {
                    int count1, count2;

                    if (val.Length == 9)
                    {
                        count1 = int.Parse(val.Substring(6, 1));
                        count2 = int.Parse(val.Substring(8, 1));
                    }
                    else
                    {
                        return(false);
                    }

                    if (count1 > 4 || count1 < 2 || count2 > 4 || count2 < 2)
                    {
                        return(false);
                    }

                    switch (count1)
                    {
                    case 2:
                        op = GpuProgramParameters.GpuConstantType.Matrix_2X2 + count2 - 2;
                        break;

                    case 3:
                        op = GpuProgramParameters.GpuConstantType.Matrix_3X2 + count2 - 2;
                        break;

                    case 4:
                        op = GpuProgramParameters.GpuConstantType.Matrix_4X2 + count2 - 2;
                        break;
                    }
                    ;
                }

                return(true);
            }
Exemplo n.º 4
0
        private void WriteProgramDependencies(StreamWriter stream, Program program)
        {
            for (int i = 0; i < program.DependencyCount; i++)
            {
                string curDependency = program.GetDependency(i);
                CacheDependencyFunctions(curDependency);
            }

            stream.WriteLine("//-----------------------------------------------------------------------------");
            stream.WriteLine("//                        PROGRAM DEPENDENCIES");
            stream.WriteLine();

            var      forwardDecl   = new List <FunctionInvocation>();
            var      functionList  = program.Functions;
            int      itFunction    = 0;
            Function curFunction   = functionList[0];
            var      atomInstances = curFunction.AtomInstances;
            int      itAtom        = 0;
            int      itAtomEnd     = atomInstances.Count;

            //Now iterate over all function atoms
            for ( ; itAtom != itAtomEnd; itAtom++)
            {
                //Skip non function invocation atom
                if (atomInstances[itAtom] is FunctionInvocation == false)
                {
                    continue;
                }

                var funcInvoc = atomInstances[itAtom] as FunctionInvocation;
                forwardDecl.Add(funcInvoc);

                // Now look into that function for other non-builtin functions and add them to the declaration list
                // Look for non-builtin functions
                // Do so by assuming that these functions do not have several variations.
                // Also, because GLSL is C based, functions must be defined before they are used
                // so we can make the assumption that we already have this function cached.
                //
                // If we find a function, look it up in the map and write it out
                DiscoverFunctionDependencies(funcInvoc, forwardDecl);
            }

            //Now remove duplicate declarations
            forwardDecl.Sort();
            forwardDecl = forwardDecl.Distinct(new FunctionInvocation.FunctionInvocationComparer()).ToList();

            for (int i = 0; i < program.DependencyCount; i++)
            {
                string curDependency = program.GetDependency(i);

                foreach (var key in this.definesMap.Keys)
                {
                    if (this.definesMap[key] == curDependency)
                    {
                        stream.Write(this.definesMap[key]);
                        stream.Write("\n");
                    }
                }
            }
            // Parse the source shader and write out only the needed functions
            foreach (var it in forwardDecl)
            {
                var invoc = new FunctionInvocation(string.Empty, 0, 0, string.Empty);

                string body = string.Empty;

                //find the function in the cache
                foreach (var key in this.functionCacheMap.Keys)
                {
                    if (!(it == key))
                    {
                        continue;
                    }

                    invoc = key;
                    body  = this.functionCacheMap[key];
                    break;
                }

                if (invoc.FunctionName.Length > 0)
                {
                    //Write out the funciton name from the cached FunctionInvocation
                    stream.Write(invoc.ReturnType);
                    stream.Write(" ");
                    stream.Write(invoc.FunctionName);
                    stream.Write("(");

                    int itOperand    = 0;
                    int itOperandEnd = invoc.OperandList.Count;

                    while (itOperand != itOperandEnd)
                    {
                        Operand            op         = invoc.OperandList[itOperand];
                        Operand.OpSemantic opSemantic = op.Semantic;
                        string             paramName  = op.Parameter.Name;
                        int opMask = op.Mask;
                        GpuProgramParameters.GpuConstantType gpuType = GpuProgramParameters.GpuConstantType.Unknown;

                        switch (opSemantic)
                        {
                        case Operand.OpSemantic.In:
                            stream.Write("in ");
                            break;

                        case Operand.OpSemantic.Out:
                            stream.Write("out ");
                            break;

                        case Operand.OpSemantic.InOut:
                            stream.Write("inout ");
                            break;

                        default:
                            break;
                        }

                        //Swizzle masks are onluy defined for types like vec2, vec3, vec4
                        if (opMask == (int)Operand.OpMask.All)
                        {
                            gpuType = op.Parameter.Type;
                        }
                        else
                        {
                            gpuType = Operand.GetGpuConstantType(opMask);
                        }

                        //We need a valid type otherwise glsl compilation will not work
                        if (gpuType == GpuProgramParameters.GpuConstantType.Unknown)
                        {
                            throw new Core.AxiomException("Cannot convert Operand.OpMask to GpuConstantType");
                        }

                        stream.Write(this.gpuConstTypeMap[gpuType] + " " + paramName);

                        itOperand++;

                        //Prepare for the next operand
                        if (itOperand != itOperandEnd)
                        {
                            stream.Write(", ");
                        }
                    }
                    stream.WriteLine();
                    stream.WriteLine("{");
                    stream.WriteLine(body);
                    stream.WriteLine("}");
                    stream.WriteLine();
                }
            }
        }
Exemplo n.º 5
0
        private FunctionInvocation CreateInvocationFromString(string input)
        {
            string             functionName, returnType;
            FunctionInvocation invoc = null;

            //Get the function name and return type
            var leftTokens  = input.Split('(');
            var leftTokens2 = leftTokens[0].Split(' ');

            leftTokens2[0] = leftTokens2[0].Trim();
            leftTokens2[1] = leftTokens2[1].Trim();
            returnType     = leftTokens2[0];
            functionName   = leftTokens2[1];


            invoc = new FunctionInvocation(functionName, 0, 0, returnType);

            string[] parameters;
            int      lparen_pos = -1;

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == '(')
                {
                    lparen_pos = i;
                    break;
                }
            }
            if (lparen_pos != -1)
            {
                string[] tokens = input.Split('(');
                parameters = tokens[1].Split(',');
            }
            else
            {
                parameters = input.Split(',');
            }
            for (int i = 0; i < parameters.Length; i++)
            {
                string itParam = parameters[i];
                itParam = itParam.Replace(")", string.Empty);
                itParam = itParam.Replace(",", string.Empty);
                string[] paramTokens = itParam.Split(' ');

                // There should be three parts for each token
                // 1. The operand type(in, out, inout)
                // 2. The type
                // 3. The name
                if (paramTokens.Length == 3)
                {
                    Operand.OpSemantic semantic = Operand.OpSemantic.In;
                    GpuProgramParameters.GpuConstantType gpuType = GpuProgramParameters.GpuConstantType.Unknown;

                    if (paramTokens[0] == "in")
                    {
                        semantic = Operand.OpSemantic.In;
                    }
                    else if (paramTokens[0] == "out")
                    {
                        semantic = Operand.OpSemantic.Out;
                    }
                    else if (paramTokens[0] == "inout")
                    {
                        semantic = Operand.OpSemantic.InOut;
                    }

                    //Find the internal type based on the string that we're given
                    foreach (var key in this.gpuConstTypeMap.Keys)
                    {
                        if (this.gpuConstTypeMap[key] == paramTokens[1])
                        {
                            gpuType = key;
                            break;
                        }
                    }

                    //We need a valid type otherwise glsl compilation will not work
                    if (gpuType == GpuProgramParameters.GpuConstantType.Unknown)
                    {
                        throw new Core.AxiomException("Cannot convert Operand.OpMask to GpuConstantType");
                    }
                    if (gpuType == GpuProgramParameters.GpuConstantType.Sampler1D)
                    {
                        gpuType = GpuProgramParameters.GpuConstantType.Sampler2D;
                    }

                    var p = new Parameter(gpuType, paramTokens[2], Parameter.SemanticType.Unknown, i,
                                          Parameter.ContentType.Unknown, 0);
                    invoc.PushOperand(p, semantic, (int)Operand.OpMask.All, 0);
                }
            }

            return(invoc);
        }