Пример #1
0
        private string FindInVariables(Type vsType, Dictionary <string, InVariable> dict)
        {
            dict.Clear();
            uint nextLoc = 0;

            foreach (var item in vsType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                object[] attribute = item.GetCustomAttributes(typeof(InAttribute), false);
                if (attribute != null && attribute.Length > 0) // this is a 'in ...;' field.
                {
                    var      v = new InVariable(item);
                    object[] locationAttribute = item.GetCustomAttributes(typeof(LocationAttribute), false);
                    if (locationAttribute != null && locationAttribute.Length > 0) // (location = ..) in ...;
                    {
                        uint loc = (locationAttribute[0] as LocationAttribute).location;
                        if (loc < nextLoc)
                        {
                            return(string.Format("location error in {0}!", this.GetType().Name));
                        }
                        v.location = loc;
                        nextLoc    = loc + 1;
                    }
                    else
                    {
                        v.location = nextLoc++;
                    }
                    dict.Add(item.Name, v);
                }
            }

            return(string.Empty);
        }
Пример #2
0
        /// <summary>
        /// ex. Make sure vs.out and fs.in match.
        /// </summary>
        /// <returns></returns>
        private bool MakeSureVariablesMatch()
        {
            if (this.VertexShader != null && this.FragmentShader != null)
            {
                Dictionary <string, OutVariable> outDict = this.VertexShader.outVariableDict;
                Dictionary <string, InVariable>  inDict  = this.FragmentShader.inVariableDict;
                if (outDict.Count != inDict.Count)
                {
                    this.logInfo = string.Format("Variables number ({0} and {1}) not match!", outDict.Count, inDict.Count); return(false);
                }
                foreach (var outItem in outDict)
                {
                    InVariable inVar = null;
                    if (inDict.TryGetValue(outItem.Key, out inVar))
                    {
                        if (inVar.fieldInfo.FieldType != outItem.Value.fieldInfo.FieldType)
                        {
                            this.logInfo = string.Format("Variable [{0}] not the same type!", outItem.Key);
                            return(false);
                        }
                    }
                    else
                    {
                        this.logInfo = string.Format("No variable matches [{0}] in {1}", outItem.Key, this.VertexShader.ShaderType);
                        return(false);
                    }
                }
                var list = new List <InVariable>();
                foreach (var outItem in outDict)
                {
                    var inVar = inDict[outItem.Key];
                    list.Add(inVar);
                }
                inDict.Clear();
                foreach (var item in list)
                {
                    inDict.Add(item.fieldInfo.Name, item);
                }
            }

            return(true);
        }
Пример #3
0
        public int GetAttribLocation(string name)
        {
            int result = -1;

            if (this.InfoLog.Length > 0)
            {
                return(result);
            }
            Dictionary <string, InVariable> dict = this.inVariableDict;

            if (dict == null)
            {
                return(result);
            }
            InVariable v = null;

            if (dict.TryGetValue(name, out v))
            {
                result = (int)v.location;
            }

            return(result);
        }
Пример #4
0
        private unsafe void FragmentShaderStage(ShaderProgram program, List <Fragment> fragmentList)
        {
            FragmentShader fs = program.FragmentShader; if (fs == null)

            {
                return;
            }

            foreach (var fragment in fragmentList)
            {
                var instance = fs.CreateCodeInstance() as FragmentCodeBase; // an executable fragment shader.
                instance.gl_FragCoord = fragment.gl_FragCoord;              // setup fragmen coordinatein window/screen space.
                // setup "in SomeType varName;" vertex attributes.
                InVariable[] inVariables = fs.inVariableDict.Values.ToArray();
                if (inVariables.Length != fragment.attributes.Length)
                {
                    throw new Exception("This should not happen!");
                }
                for (int index = 0; index < inVariables.Length; index++)
                {
                    InVariable inVar     = inVariables[index];
                    PassBuffer attribute = fragment.attributes[index];
                    var        pointer   = attribute.Mapbuffer().ToPointer();
                    switch (attribute.elementType)
                    {
                    case PassType.Float: inVar.fieldInfo.SetValue(instance, ((float *)pointer)[0]); break;

                    case PassType.Vec2: inVar.fieldInfo.SetValue(instance, ((vec2 *)pointer)[0]); break;

                    case PassType.Vec3: inVar.fieldInfo.SetValue(instance, ((vec3 *)pointer)[0]); break;

                    case PassType.Vec4: inVar.fieldInfo.SetValue(instance, ((vec4 *)pointer)[0]); break;

                    case PassType.Mat2: inVar.fieldInfo.SetValue(instance, ((mat2 *)pointer)[0]); break;

                    case PassType.Mat3: inVar.fieldInfo.SetValue(instance, ((mat3 *)pointer)[0]); break;

                    case PassType.Mat4: inVar.fieldInfo.SetValue(instance, ((mat4 *)pointer)[0]); break;

                    default: throw new NotDealWithNewEnumItemException(typeof(PassType));
                    }
                    attribute.Unmapbuffer();
                }
                // setup "uniform SomeType varName;" in fragment shader.
                Dictionary <string, UniformValue> nameUniformDict = program.nameUniformDict;
                foreach (UniformVariable uniformVar in fs.UniformVariableDict.Values)
                {
                    string       name = uniformVar.fieldInfo.Name;
                    UniformValue obj  = null;
                    if (nameUniformDict.TryGetValue(name, out obj))
                    {
                        if (obj.value != null)
                        {
                            uniformVar.fieldInfo.SetValue(instance, obj.value);
                        }
                    }
                }

                instance.main();       // execute fragment shader code.
                fragment.discard = instance.discard;
                if (!instance.discard) // if this fragment is not discarded.
                {
                    OutVariable[] outVariables = fs.outVariableDict.Values.ToArray();
                    var           outBuffers   = new PassBuffer[outVariables.Length];
                    for (int index = 0; index < outVariables.Length; index++)
                    {
                        OutVariable outVar    = outVariables[index];
                        var         outBuffer = new PassBuffer(outVar.fieldInfo.FieldType.GetPassType(), 1);
                        var         pointer   = outBuffer.Mapbuffer().ToPointer();
                        switch (outBuffer.elementType)
                        {
                        case PassType.Float: ((float *)pointer)[0] = (float)outVar.fieldInfo.GetValue(instance); break;

                        case PassType.Vec2: ((vec2 *)pointer)[0] = (vec2)outVar.fieldInfo.GetValue(instance); break;

                        case PassType.Vec3: ((vec3 *)pointer)[0] = (vec3)outVar.fieldInfo.GetValue(instance); break;

                        case PassType.Vec4: ((vec4 *)pointer)[0] = (vec4)outVar.fieldInfo.GetValue(instance); break;

                        case PassType.Mat2: ((mat2 *)pointer)[0] = (mat2)outVar.fieldInfo.GetValue(instance); break;

                        case PassType.Mat3: ((mat3 *)pointer)[0] = (mat3)outVar.fieldInfo.GetValue(instance); break;

                        case PassType.Mat4: ((mat4 *)pointer)[0] = (mat4)outVar.fieldInfo.GetValue(instance); break;

                        default: throw new NotDealWithNewEnumItemException(typeof(PassType));
                        }
                        outBuffer.Unmapbuffer();
                        outBuffers[index] = outBuffer;
                    }
                    fragment.outVariables = outBuffers;
                }
            }
        }