예제 #1
0
파일: Function.cs 프로젝트: bostich83/axiom
        /// <summary>
        ///   Get parameter by a given content and type from the given param list.
        /// </summary>
        /// <param name="paramList"> The parameters list to look in </param>
        /// <param name="content"> The content of the paramter to search in the list </param>
        /// <param name="type"> The type of the parameter to search in the list </param>
        /// <returns> null if no matching paramter found </returns>
        public static Parameter GetParameterByContent(List <Parameter> paramList, Parameter.ContentType content,
                                                      Graphics.GpuProgramParameters.GpuConstantType type)
        {
            if (content != Parameter.ContentType.Unknown)
            {
                for (int it = 0; it < paramList.Count; it++)
                {
                    if (paramList[it].Content == content && paramList[it].Type == type)
                    {
                        return(paramList[it]);
                    }
                }
            }

            return(null);
        }
예제 #2
0
파일: Function.cs 프로젝트: bostich83/axiom
        /// <summary>
        ///   Resolve input paramteter of this function
        /// </summary>
        /// <param name="semantic"> The desired parameter semantic </param>
        /// <param name="index"> The index of the desired parameter </param>
        /// <param name="content"> The Content of the parameter </param>
        /// <param name="type"> The type of the desired parameter </param>
        /// <returns> paramter instance in case of that resolve operation succeed </returns>
        internal Parameter ResolveLocalParameter(Parameter.SemanticType semantic, int index,
                                                 Parameter.ContentType content,
                                                 Graphics.GpuProgramParameters.GpuConstantType type)
        {
            Parameter param = Function.GetParameterByContent(this.localParameters, content, type);

            if (param != null)
            {
                return(param);
            }

            param = new Parameter(type, "lLocalParam_" + this.localParameters.Count.ToString(), semantic, index, content, 0);
            AddParameter(this.localParameters, param);

            return(param);
        }
예제 #3
0
        protected override bool ResolveParameters(ProgramSet programSet)
        {
            Program  vsProgram = programSet.CpuVertexProgram;
            Program  psProgram = programSet.CpuFragmentProgram;
            Function vsMain    = vsProgram.EntryPointFunction;
            Function psMain    = psProgram.EntryPointFunction;

            //Get input position parameter
            this.vsInPos = Function.GetParameterBySemantic(vsMain.InputParameters, Parameter.SemanticType.Position, 0);
            if (this.vsInPos == null)
            {
                return(false);
            }

            //Get output position parameter
            this.vsOutPos = Function.GetParameterBySemantic(vsMain.OutputParameters, Parameter.SemanticType.Position, 0);
            if (this.vsOutPos == null)
            {
                return(false);
            }

            //Resolve vertex shader output depth.
            this.vsOutDepth = vsMain.ResolveOutputParameter(Parameter.SemanticType.TextureCoordinates, -1,
                                                            Parameter.ContentType.DepthViewSpace,
                                                            GpuProgramParameters.GpuConstantType.Float1);
            if (this.vsOutDepth == null)
            {
                return(false);
            }

            //Resolve input depth parameter.
            this.psInDepth = psMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates, this.vsOutDepth.Index,
                                                          this.vsOutDepth.Content, GpuProgramParameters.GpuConstantType.Float1);
            if (this.psInDepth == null)
            {
                return(false);
            }

            //Get in/local specular paramter
            this.psSpecular = Function.GetParameterBySemantic(psMain.InputParameters, Parameter.SemanticType.Color, 1);
            if (this.psSpecular == null)
            {
                this.psSpecular = Function.GetParameterBySemantic(psMain.LocalParameters, Parameter.SemanticType.Color, 1);
                if (this.psSpecular == null)
                {
                    return(false);
                }
            }
            //Resolve computed local shadow color parameter.
            this.psLocalShadowFactor = psMain.ResolveLocalParameter(Parameter.SemanticType.Unknown, 0, "lShadowFactor",
                                                                    GpuProgramParameters.GpuConstantType.Float1);
            if (this.psLocalShadowFactor == null)
            {
                return(false);
            }

            //Resolve computed local shadow color parameter
            this.psSplitPoints = psProgram.ResolveParameter(GpuProgramParameters.GpuConstantType.Float4, -1,
                                                            GpuProgramParameters.GpuParamVariability.Global,
                                                            "pssm_split_points");
            if (this.psSplitPoints == null)
            {
                return(false);
            }

            //Get derived scene color
            this.psDerivedSceneColor =
                psProgram.ResolveAutoParameterInt(GpuProgramParameters.AutoConstantType.DerivedSceneColor, 0);
            if (this.psDerivedSceneColor == null)
            {
                return(false);
            }

            int lightIndex = 0;

            foreach (var it in this.shadowTextureParamsList)
            {
                it.WorldViewProjMatrix = vsProgram.ResolveParameter(GpuProgramParameters.GpuConstantType.Matrix_4X4, -1,
                                                                    GpuProgramParameters.GpuParamVariability.PerObject,
                                                                    "world_texture_view_proj");
                if (it.WorldViewProjMatrix == null)
                {
                    return(false);
                }

                Parameter.ContentType lightSpace = Parameter.ContentType.PositionLightSpace0;

                switch (lightIndex)
                {
                case 1:
                    lightSpace = Parameter.ContentType.PositionLightSpace1;
                    break;

                case 2:
                    lightSpace = Parameter.ContentType.PositionLightSpace2;
                    break;

                case 3:
                    lightSpace = Parameter.ContentType.PositionLightSpace3;
                    break;

                case 4:
                    lightSpace = Parameter.ContentType.PositionLightSpace4;
                    break;

                case 5:
                    lightSpace = Parameter.ContentType.PositionLightSpace5;
                    break;

                case 6:
                    lightSpace = Parameter.ContentType.PositionLightSpace6;
                    break;

                case 7:
                    lightSpace = Parameter.ContentType.PositionLightSpace7;
                    break;
                }

                it.VSOutLightPosition = vsMain.ResolveOutputParameter(Parameter.SemanticType.TextureCoordinates, -1,
                                                                      lightSpace,
                                                                      GpuProgramParameters.GpuConstantType.Float4);
                if (it.VSOutLightPosition == null)
                {
                    return(false);
                }

                it.PSInLightPosition = psMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates,
                                                                    it.VSOutLightPosition.Index,
                                                                    it.VSOutLightPosition.Content,
                                                                    GpuProgramParameters.GpuConstantType.Float4);
                if (it.PSInLightPosition == null)
                {
                    return(false);
                }

                it.TextureSampler = psProgram.ResolveParameter(GpuProgramParameters.GpuConstantType.Sampler2D,
                                                               it.TextureSamplerIndex,
                                                               GpuProgramParameters.GpuParamVariability.Global,
                                                               "shadow_map");
                if (it.TextureSampler == null)
                {
                    return(false);
                }

                it.InvTextureSize = psProgram.ResolveParameter(GpuProgramParameters.GpuConstantType.Float4, -1,
                                                               GpuProgramParameters.GpuParamVariability.Global,
                                                               "inv_shadow_texture_size");
                if (it.InvTextureSize == null)
                {
                    return(false);
                }

                lightIndex++;
            }

            return(true);
        }
예제 #4
0
        private void WriteInputParameters(StreamWriter stream, Function function, GpuProgramType gpuType)
        {
            var inParams = function.InputParameters;

            foreach (var param in inParams)
            {
                Parameter.ContentType paramContent = param.Content;
                string paramName = param.Name;

                if (gpuType == GpuProgramType.Fragment)
                {
                    // push fragment inputs they all could be written (in glsl you can not write
                    // input params in the fragment program)
                    this.fragInputParams.Add(paramName);

                    // In the vertex and fragment program the variable names must match.
                    // Unfortunately now the input params are prefixed with an 'i' and output params with 'o'.
                    // Thats why we are using a map for name mapping (we rename the params which are used in function atoms).
                    paramName.Remove(0);
                    paramName.Insert(0, "o");

                    stream.Write("varying\t");
                    stream.Write(this.gpuConstTypeMap[param.Type]);
                    stream.Write("\t");
                    stream.Write(paramName);
                    stream.WriteLine(";");
                }
                else if (gpuType == GpuProgramType.Vertex &&
                         this.contentToPerVertexAttributes.ContainsKey(paramContent))
                {
                    // Due the fact that glsl does not have register like cg we have to rename the params
                    // according their content.
                    this.inputToGLStatesMap.Add(paramName, this.contentToPerVertexAttributes[paramContent]);
                    stream.Write("attribute\t");

                    //All uv texcoords passed by Axiom are vec4
                    if (paramContent == Parameter.ContentType.TextureCoordinate0 ||
                        paramContent == Parameter.ContentType.TextureCoordinate1 ||
                        paramContent == Parameter.ContentType.TextureCoordinate2 ||
                        paramContent == Parameter.ContentType.TextureCoordinate3 ||
                        paramContent == Parameter.ContentType.TextureCoordinate4 ||
                        paramContent == Parameter.ContentType.TextureCoordinate5 ||
                        paramContent == Parameter.ContentType.TextureCoordinate6 ||
                        paramContent == Parameter.ContentType.TextureCoordinate7)
                    {
                        stream.Write("vec4");
                    }
                    else
                    {
                        stream.Write(this.gpuConstTypeMap[param.Type]);
                    }
                    stream.Write("\t");
                    stream.Write(this.contentToPerVertexAttributes[paramContent]);
                    stream.WriteLine(";");
                }
                else
                {
                    stream.Write("uniform \t");
                    stream.Write(this.gpuConstTypeMap[param.Type]);
                    stream.Write("\t");
                    stream.Write(paramName);
                    stream.WriteLine(";");
                }
            }
        }
예제 #5
0
        internal override void WriteSourceCode(System.IO.StreamWriter stream, Program program)
        {
            var gpuType = program.Type;

            if (gpuType == GpuProgramType.Geometry)
            {
                throw new Core.AxiomException("Geometry Programs not supported in GLSL ES writer");
            }

            this.fragInputParams.Clear();
            var functionList  = program.Functions;
            var parameterList = program.Parameters;

            // Write the current version (this forces the driver to fulfill the glsl es standard)
            stream.WriteLine("#version" + this.glslVersion.ToString());

            //Default precision declaration is required in fragment and vertex shaders.
            stream.WriteLine("precision highp float");
            stream.WriteLine("precision highp int");

            //Generate source code header
            WriteProgramTitle(stream, program);
            stream.WriteLine();

            //Embed depndencies.
            WriteProgramDependencies(stream, program);
            stream.WriteLine();

            //Generate global variable code.
            WriteUniformParametersTitle(stream, program);
            stream.WriteLine();

            //Write the uniforms
            foreach (var uniformParams in parameterList)
            {
                stream.Write("uniform\t");
                stream.Write(this.gpuConstTypeMap[uniformParams.Type]);
                stream.Write("\t");
                stream.Write(uniformParams.Name);
                if (uniformParams.IsArray)
                {
                    stream.Write("[" + uniformParams.Size.ToString() + "]");
                }
                stream.WriteLine(";");
            }
            stream.WriteLine();

            //Write program function(s)
            foreach (var curFunction in functionList)
            {
                WriteFunctionTitle(stream, curFunction);

                this.inputToGLStatesMap.Clear();

                WriteInputParameters(stream, curFunction, gpuType);
                WriteOutParameters(stream, curFunction, gpuType);

                stream.WriteLine("void main() {");

                if (gpuType == GpuProgramType.Fragment)
                {
                    stream.WriteLine("\tvec4 outputColor;");
                }
                else if (gpuType == GpuProgramType.Vertex)
                {
                    stream.WriteLine("\tvec4 outputPosition;");
                }

                //Write local paraemters
                var localParam = curFunction.LocalParameters;

                foreach (var itParam in localParam)
                {
                    stream.Write("\t");
                    WriteLocalParameter(stream, itParam);
                    stream.WriteLine(";");
                }
                stream.WriteLine();

                //sort function atoms
                curFunction.SortAtomInstances();

                var atomInstances = curFunction.AtomInstances;
                foreach (var itAtom in atomInstances)
                {
                    var funcInvoc    = (FunctionInvocation)itAtom;
                    int itOperand    = 0;
                    int itOperandEnd = funcInvoc.OperandList.Count;

                    var localOs = new StringBuilder();
                    localOs.Append("\t" + funcInvoc.FunctionName + "(");

                    int curIndLevel = 0;
                    while (itOperand != itOperandEnd)
                    {
                        Operand               op         = funcInvoc.OperandList[itOperand];
                        Operand.OpSemantic    opSemantic = op.Semantic;
                        string                paramName  = op.Parameter.Name;
                        Parameter.ContentType content    = op.Parameter.Content;

                        // Check if we write to a varying because the are only readable in fragment programs
                        if (opSemantic == Operand.OpSemantic.Out || opSemantic == Operand.OpSemantic.InOut)
                        {
                            bool isVarying = false;

                            if (gpuType == GpuProgramType.Fragment)
                            {
                                if (this.fragInputParams.Contains(paramName))
                                {
                                    //Declare the copy variable
                                    string newVar  = "local_" + paramName;
                                    string tempVar = paramName;
                                    isVarying = true;

                                    // We stored the original values in the mFragInputParams thats why we have to replace the first var with o
                                    // because all vertex output vars are prefixed with o in glsl the name has to match in the fragment program.
                                    tempVar.Remove(0);
                                    tempVar.Insert(0, "o");

                                    //Declare the copy variable and assign the original
                                    stream.WriteLine("\t" + this.gpuConstTypeMap[op.Parameter.Type] + " " + newVar + " = " +
                                                     tempVar + ";\n");

                                    //Fromnow on we replace it automatic
                                    this.inputToGLStatesMap.Add(paramName, newVar);

                                    this.fragInputParams.Remove(paramName);
                                }
                            }

                            if (!isVarying)
                            {
                                foreach (var param in parameterList)
                                {
                                    if (CompareUniformByName(param, paramName))
                                    {
                                        string newVar = "local_" + paramName;

                                        if (this.inputToGLStatesMap.ContainsKey(newVar) == false)
                                        {
                                            //Declare the copy variable and assign the original

                                            stream.WriteLine("\t" + this.gpuConstTypeMap[param.Type] + " " + newVar +
                                                             " = " + paramName + ";\n");

                                            //From now on we replace it automatic
                                            this.inputToGLStatesMap.Add(paramName, newVar);
                                        }
                                    }
                                }
                            }

                            string newParam;
                            if (this.inputToGLStatesMap.ContainsKey(paramName))
                            {
                                int mask = op.Mask;                                 //our swizzle mask

                                //Here we insert the renamed param name
                                newParam = this.inputToGLStatesMap[paramName];

                                if (mask != (int)Operand.OpMask.All)
                                {
                                    newParam += "." + Operand.GetMaskAsString(mask);
                                }
                                // Now that every texcoord is a vec4 (passed as vertex attributes) we
                                // have to swizzle them according the desired type.
                                else if (gpuType == GpuProgramType.Vertex &&
                                         content == Parameter.ContentType.TextureCoordinate0 ||
                                         content == Parameter.ContentType.TextureCoordinate1 ||
                                         content == Parameter.ContentType.TextureCoordinate2 ||
                                         content == Parameter.ContentType.TextureCoordinate3 ||
                                         content == Parameter.ContentType.TextureCoordinate4 ||
                                         content == Parameter.ContentType.TextureCoordinate5 ||
                                         content == Parameter.ContentType.TextureCoordinate6 ||
                                         content == Parameter.ContentType.TextureCoordinate7)
                                {
                                    //Now generate the swizzle mask according
                                    // the type.
                                    switch (op.Parameter.Type)
                                    {
                                    case GpuProgramParameters.GpuConstantType.Float1:
                                        newParam += ".x";
                                        break;

                                    case GpuProgramParameters.GpuConstantType.Float2:
                                        newParam += ".xy";
                                        break;

                                    case GpuProgramParameters.GpuConstantType.Float3:
                                        newParam += ".xyz";
                                        break;

                                    case GpuProgramParameters.GpuConstantType.Float4:
                                        newParam += ".xyzw";
                                        break;

                                    default:
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                newParam = op.ToString();
                            }

                            itOperand++;

                            //Prepare for the next operand
                            localOs.Append(newParam);

                            int opIndLevel = 0;
                            if (itOperand != itOperandEnd)
                            {
                                opIndLevel = funcInvoc.OperandList[itOperand].IndirectionLevel;
                            }

                            if (curIndLevel != 0)
                            {
                                localOs.Append(")");
                            }
                            if (curIndLevel < opIndLevel)
                            {
                                while (curIndLevel < opIndLevel)
                                {
                                    curIndLevel++;
                                    localOs.Append("[");
                                }
                            }
                            else
                            {
                                while (curIndLevel > opIndLevel)
                                {
                                    curIndLevel--;
                                    localOs.Append("]");
                                }
                                if (opIndLevel != 0)
                                {
                                    localOs.Append("][");
                                }
                                else if (itOperand != itOperandEnd)
                                {
                                    localOs.Append(", ");
                                }
                            }
                            if (curIndLevel != 0)
                            {
                                localOs.Append("int(");
                            }
                        }
                        //Write function call closer
                        localOs.AppendLine(");");
                        localOs.AppendLine();
                        stream.Write(localOs.ToString());
                    }
                }

                if (gpuType == GpuProgramType.Fragment)
                {
                    stream.WriteLine("\tgl_FragColor = outputColor;");
                }
                else if (gpuType == GpuProgramType.Vertex)
                {
                    stream.WriteLine("\tgl_Position = outputPosition;");
                }

                stream.WriteLine("}");
            }

            stream.WriteLine();
        }
예제 #6
0
        internal override void WriteSourceCode(System.IO.StreamWriter stream, Program program)
        {
            var gpuType = program.Type;

            if (gpuType == GpuProgramType.Geometry)
            {
                throw new Core.AxiomException("Geometry Program not supported iin GLSL writer");
            }

            this.fragInputParams.Clear();
            var functionList  = program.Functions;
            var parameterList = program.Parameters;

            // Write the current version (this force the driver to more fulfill the glsl standard)
            stream.WriteLine("#version " + this.glslVersion.ToString());

            //Generate source code header
            WriteProgramTitle(stream, program);
            stream.WriteLine();

            //Write forward declarations
            WriteForwardDeclarations(stream, program);
            stream.WriteLine();

            //Generate global variable code
            WriteUniformParametersTitle(stream, program);
            stream.WriteLine();

            //Write the uniforms
            foreach (var uniformParam in parameterList)
            {
                stream.Write("uniform\t");
                stream.Write(this.gpuConstTypeMap[uniformParam.Type]);
                stream.Write("\t");
                stream.Write(uniformParam.Name);
                if (uniformParam.IsArray)
                {
                    stream.Write("[" + uniformParam.Size.ToString() + "]");
                }
                stream.Write(";");
                stream.WriteLine();
            }
            stream.WriteLine();

            //Write program function(s)
            foreach (var curFunction in functionList)
            {
                WriteFunctionTitle(stream, curFunction);

                //Clear output mapping this map is used when we use
                //glsl built in types like gl_Color for example
                this.inputToGLStatesMap.Clear();

                //Write inout params and fill inputToGLStatesMap
                WriteInputParameters(stream, curFunction, gpuType);
                WriteOutParameters(stream, curFunction, gpuType);

                stream.Write("void main() {");
                stream.WriteLine();
                //Write local parameters
                var localParams = curFunction.LocalParameters;

                foreach (var itParam in localParams)
                {
                    stream.Write("\t");
                    WriteLocalParameter(stream, itParam);
                    stream.Write(";");
                    stream.WriteLine();
                }

                stream.WriteLine();
                //Sort function atoms
                curFunction.SortAtomInstances();
                var atomInstances = curFunction.AtomInstances;

                foreach (var itAtom in atomInstances)
                {
                    var funcInvoc = itAtom as FunctionInvocation;

                    var localOs = new StringBuilder();

                    //Write function name
                    localOs.Append("\t" + funcInvoc.FunctionAtomType + "(");
                    int curIndLevel = 0;

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

                    while (itOperand != itOperandEnd)
                    {
                        Operand               op         = funcInvoc.OperandList[itOperand];
                        Operand.OpSemantic    opSemantic = op.Semantic;
                        string                paramName  = op.Parameter.Name;
                        Parameter.ContentType content    = op.Parameter.Content;

                        if (opSemantic == Operand.OpSemantic.Out || opSemantic == Operand.OpSemantic.InOut)
                        {
                            bool isVarying = false;

                            // Check if we write to an varying because the are only readable in fragment programs
                            if (gpuType == GpuProgramType.Fragment)
                            {
                                if (this.fragInputParams.Contains(paramName))
                                {
                                    //Declare the copy variable
                                    string newVar  = "local_" + paramName;
                                    string tempVar = paramName;
                                    isVarying = true;

                                    // We stored the original values in the mFragInputParams thats why we have to replace the first var with o
                                    // because all vertex output vars are prefixed with o in glsl the name has to match in the fragment program.
                                    tempVar = tempVar.Remove(0);
                                    tempVar = tempVar.Insert(0, "o");

                                    //Declare the copy variable and assign the original
                                    stream.WriteLine("\t" + this.gpuConstTypeMap[op.Parameter.Type] + " " + newVar + " = " +
                                                     tempVar);

                                    //From now on we replace it automatic
                                    this.inputToGLStatesMap[paramName] = newVar;

                                    //Remove the param because now it is replaced automatic with the local variable
                                    //(which could be written).
                                    this.fragInputParams.Remove(paramName);
                                }
                            }

                            //If its not varying param check if a uniform is written
                            if (!isVarying)
                            {
                                foreach (var param in parameterList)
                                {
                                    if (GLSLProgramWriter.CompareUniformByName(param, paramName))
                                    {
                                        //Declare the copy variable
                                        string newVar = "local_" + paramName;

                                        //now we check if we already declared a uniform redirector var
                                        if (this.inputToGLStatesMap.ContainsKey(newVar) == false)
                                        {
                                            //Declare the copy variable and assign the original
                                            stream.WriteLine("\t" + this.gpuConstTypeMap[param.Type] + " " + newVar +
                                                             paramName + ";\n");

                                            //From now on we replace it automatic
                                            this.inputToGLStatesMap.Add(paramName, newVar);
                                        }
                                    }
                                }
                            }
                        }
                        if (this.inputToGLStatesMap.ContainsKey(paramName))
                        {
                            int mask = op.Mask; // our swizzle mask

                            //Here we insert the renamed param name
                            localOs.Append("." + Operand.GetMaskAsString(mask));

                            if (mask != (int)Operand.OpMask.All)
                            {
                                localOs.Append("." + Operand.GetMaskAsString(mask));
                            }

                            //Now that every texcoord is a vec4 (passed as vertex attributes)
                            //we have to swizzle them aoccording the desired type.
                            else if (gpuType == GpuProgramType.Vertex &&
                                     content == Parameter.ContentType.TextureCoordinate0 ||
                                     content == Parameter.ContentType.TextureCoordinate1 ||
                                     content == Parameter.ContentType.TextureCoordinate2 ||
                                     content == Parameter.ContentType.TextureCoordinate3 ||
                                     content == Parameter.ContentType.TextureCoordinate4 ||
                                     content == Parameter.ContentType.TextureCoordinate5 ||
                                     content == Parameter.ContentType.TextureCoordinate6 ||
                                     content == Parameter.ContentType.TextureCoordinate7)
                            {
                                //Now generate the swizzel mask according
                                //the type.
                                switch (op.Parameter.Type)
                                {
                                case GpuProgramParameters.GpuConstantType.Float1:
                                    localOs.Append(".x");
                                    break;

                                case GpuProgramParameters.GpuConstantType.Float2:
                                    localOs.Append(".xy");
                                    break;

                                case GpuProgramParameters.GpuConstantType.Float3:
                                    localOs.Append(".xyz");
                                    break;

                                case GpuProgramParameters.GpuConstantType.Float4:
                                    localOs.Append(".xyzw");
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                        else
                        {
                            localOs.Append(op.ToString());
                        }
                        itOperand++;

                        //Prepare for the next operand
                        int opIndLevel = 0;
                        if (itOperand != itOperandEnd)
                        {
                            opIndLevel = funcInvoc.OperandList[itOperand].IndirectionLevel;
                        }

                        if (curIndLevel < opIndLevel)
                        {
                            while (curIndLevel < opIndLevel)
                            {
                                curIndLevel++;
                                localOs.Append("[");
                            }
                        }
                        else
                        {
                            while (curIndLevel > opIndLevel)
                            {
                                curIndLevel--;
                                localOs.Append("]");
                            }
                            if (opIndLevel != 0)
                            {
                                localOs.Append("][");
                            }
                            else if (itOperand != itOperandEnd)
                            {
                                localOs.Append(", ");
                            }
                        }
                        if (curIndLevel != 0)
                        {
                            localOs.Append("int(");
                        }
                    }

                    //Write function call closer
                    localOs.AppendLine(");");
                    localOs.AppendLine();
                    stream.Write(localOs.ToString());
                }
                stream.WriteLine("}");
            }
            stream.WriteLine();
        }
예제 #7
0
        protected override bool ResolveParameters(Axiom.Components.RTShaderSystem.ProgramSet programSet)
        {
            Program vsProgram = programSet.CpuVertexProgram;
            Program psProgram = programSet.CpuFragmentProgram;

            Function vsMain = vsProgram.EntryPointFunction;
            Function psMain = psProgram.EntryPointFunction;

            //Resolve vertex shader output position in projective space.

            this.vsInPosition = vsMain.ResolveInputParameter(Parameter.SemanticType.Position, 0,
                                                             Parameter.ContentType.PositionObjectSpace,
                                                             Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.vsInPosition == null)
            {
                return(false);
            }

            this.vsOriginalOutPositionProjectiveSpace = vsMain.ResolveOutputParameter(Parameter.SemanticType.Position, 0,
                                                                                      Parameter.ContentType.
                                                                                      PositionProjectiveSpace,
                                                                                      Graphics.GpuProgramParameters.
                                                                                      GpuConstantType.
                                                                                      Float4);
            if (this.vsOriginalOutPositionProjectiveSpace == null)
            {
                return(false);
            }

            var positionProjectiveSpaceAsTexcoord = (Parameter.ContentType)(Parameter.ContentType.CustomContentBegin + 1);

            this.vsOutPositionProjectiveSpace = vsMain.ResolveOutputParameter(Parameter.SemanticType.TextureCoordinates, -1,
                                                                              positionProjectiveSpaceAsTexcoord,
                                                                              Graphics.GpuProgramParameters.GpuConstantType.
                                                                              Float4);
            if (this.vsOutPositionProjectiveSpace == null)
            {
                return(false);
            }

            //Resolve ps input position in projective space
            this.psInPositionProjectiveSpace = psMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates,
                                                                            this.vsOutPositionProjectiveSpace.Index,
                                                                            this.vsOutPositionProjectiveSpace.Content,
                                                                            Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.psInPositionProjectiveSpace == null)
            {
                return(false);
            }

            //Resolve vertex shader uniform monitors count
            this.vsInMonitorsCount = vsProgram.ResolveParameter(Graphics.GpuProgramParameters.GpuConstantType.Float2, -1,
                                                                Graphics.GpuProgramParameters.GpuParamVariability.Global,
                                                                "monitorsCount");
            if (this.vsInMonitorsCount == null)
            {
                return(false);
            }

            //Resolve pixel shader uniform monitors count
            this.psInMonitorsCount = psProgram.ResolveParameter(Graphics.GpuProgramParameters.GpuConstantType.Float2, -1,
                                                                Graphics.GpuProgramParameters.GpuParamVariability.Global,
                                                                "monitorsCount");
            if (this.psInMonitorsCount == null)
            {
                return(false);
            }

            //Resolve the current world & view matrices concatenated
            this.worldViewMatrix =
                vsProgram.ResolveAutoParameterInt(Graphics.GpuProgramParameters.AutoConstantType.WorldViewMatrix,
                                                  0);
            if (this.worldViewMatrix == null)
            {
                return(false);
            }

            //Resolve the current projection matrix
            this.projectionMatrix = vsProgram.ResolveAutoParameterInt(
                Graphics.GpuProgramParameters.AutoConstantType.ProjectionMatrix, 0);
            if (this.projectionMatrix == null)
            {
                return(false);
            }

            var monitorIndex = Parameter.ContentType.TextureCoordinate3;

            //Resolve vertex shader monitor index
            this.vsInMonitorIndex = vsMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates, 3, monitorIndex,
                                                                 Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.vsInMonitorIndex == null)
            {
                return(false);
            }

            Parameter.ContentType matrixR0 = Parameter.ContentType.TextureCoordinate4;
            Parameter.ContentType matrixR1 = Parameter.ContentType.TextureCoordinate5;
            Parameter.ContentType matrixR2 = Parameter.ContentType.TextureCoordinate6;
            Parameter.ContentType matrixR3 = Parameter.ContentType.TextureCoordinate7;

            //Resolve vertex shader viewport offset matrix
            this.vsInViewportOffsetMatrixR0 = vsMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates, 4,
                                                                           matrixR0,
                                                                           Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.vsInViewportOffsetMatrixR0 == null)
            {
                return(false);
            }
            this.vsInViewportOffsetMatrixR1 = vsMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates, 4,
                                                                           matrixR1,
                                                                           Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.vsInViewportOffsetMatrixR1 == null)
            {
                return(false);
            }
            this.vsInViewportOffsetMatrixR2 = vsMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates, 4,
                                                                           matrixR2,
                                                                           Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.vsInViewportOffsetMatrixR2 == null)
            {
                return(false);
            }
            this.vsInViewportOffsetMatrixR3 = vsMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates, 4,
                                                                           matrixR3,
                                                                           Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.vsInViewportOffsetMatrixR3 == null)
            {
                return(false);
            }

            this.vsOutMonitorIndex = vsMain.ResolveOutputParameter(Parameter.SemanticType.TextureCoordinates, -1, monitorIndex,
                                                                   Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.vsOutMonitorIndex == null)
            {
                return(false);
            }

            //Resolve ps input monitor index
            this.psInMonitorIndex = psMain.ResolveInputParameter(Parameter.SemanticType.TextureCoordinates,
                                                                 this.vsOutMonitorIndex.Index,
                                                                 this.vsOutMonitorIndex.Content,
                                                                 Graphics.GpuProgramParameters.GpuConstantType.Float4);
            if (this.psInMonitorIndex == null)
            {
                return(false);
            }

            return(true);
        }
예제 #8
0
파일: Function.cs 프로젝트: bostich83/axiom
        /// <summary>
        ///   Resolve output paramteter of this function
        /// </summary>
        /// <param name="semantic"> The desired parameter semantic </param>
        /// <param name="index"> The index of the desired parameter </param>
        /// <param name="content"> The Content of the parameter </param>
        /// <param name="type"> The type of the desired parameter </param>
        /// <returns> paramter instance in case of that resolve operation succeed </returns>
        /// <remarks>
        ///   Pass -1 as index paraemter to craete a new pareamter with the desired semantic and type
        /// </remarks>
        public Parameter ResolveOutputParameter(Parameter.SemanticType semantic, int index,
                                                Parameter.ContentType content,
                                                Graphics.GpuProgramParameters.GpuConstantType type)
        {
            Parameter param = null;

            //Check if desired parameter already defined
            param = Function.GetParameterByContent(this.outputParameters, content, type);
            if (param != null)
            {
                return(param);
            }

            //case we have to create new parameter.
            if (index == -1)
            {
                index = 0;

                //find the next availabe index of the target semantic
                for (int it = 0; it < this.outputParameters.Count; it++)
                {
                    if (this.outputParameters[it].Semantic == semantic)
                    {
                        index++;
                    }
                }
            }
            else
            {
                //check if desired parameter already defined
                param = GetParameterBySemantic(this.outputParameters, semantic, index);
                if (param != null && param.Content == content)
                {
                    if (param.Type == type)
                    {
                        return(param);
                    }
                    else
                    {
                        throw new AxiomException("Cannot resolve parameter - semantic: " + semantic.ToString() +
                                                 " - index: " + index.ToString() + " due to type mimatch. Function <" +
                                                 Name + ">");
                    }
                }
            }

            //No parameter found -> create new one
            switch (semantic)
            {
            case Parameter.SemanticType.Unknown:
                break;

            case Parameter.SemanticType.Position:
                param = ParameterFactory.CreateOutPosition(index);
                break;

            case Parameter.SemanticType.BlendWeights:
            case Parameter.SemanticType.BlendIndicies:
                throw new AxiomException("Can not resolve parameter - semantic: " + semantic.ToString() +
                                         " - index: " + index.ToString() +
                                         " since support init is not implemented yet. Function <" + Name + ">");

            case Parameter.SemanticType.Normal:
                param = ParameterFactory.CreateOutNormal(index);
                break;

            case Parameter.SemanticType.Color:
                param = ParameterFactory.CreateOutColor(index);
                break;

            case Parameter.SemanticType.TextureCoordinates:
                param = ParameterFactory.CreateOutTexcoord(type, index, content);
                break;

            case Parameter.SemanticType.Binormal:
                param = ParameterFactory.CreateOutBiNormal(index);
                break;

            case Parameter.SemanticType.Tangent:
                param = ParameterFactory.CreateOutTangent(index);
                break;

            default:
                break;
            }

            if (param != null)
            {
                AddOutputParameter(param);
            }

            return(param);
        }