/// <summary>
        ///     Appends the files to the shader.
        /// </summary>
        /// <param name="shader"></param>
        internal bool Append(GenericShader shader)
        {
            foreach (ShaderFile file in Vertex)
            {
                if (!file.Compile(shader, ShaderType.VertexShader))
                {
                    return(false);
                }
            }

            if (Geometry != null)
            {
                foreach (ShaderFile file in Geometry)
                {
                    if (!file.Compile(shader, ShaderType.GeometryShader))
                    {
                        return(false);
                    }
                }
            }

            foreach (ShaderFile file in Fragment)
            {
                if (!file.Compile(shader, ShaderType.FragmentShader))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        internal bool Compile(GenericShader shader, ShaderType type)
        {
            if (_id < 0)
            {
                GenerateSource();

                _id = GL.CreateShader(type);
                if (Defines.Count > 0)
                {
                    string defineString = "";
                    foreach (string define in Defines)
                    {
                        defineString += "#define " + define + Environment.NewLine;
                    }

                    GL.ShaderSource(_id, 2, new string[] { defineString, _data }, new int[] { defineString.Length, _data.Length });
                }
                else
                {
                    GL.ShaderSource(_id, _data);
                }
                GL.CompileShader(_id);
            }

            GL.GetShader(_id, ShaderParameter.CompileStatus, out int compileStatus);
            if (compileStatus != 1)
            {
                GL.GetShader(_id, ShaderParameter.InfoLogLength, out int loglength);

                GLCustomActions.AtWarning?.Invoke($"Shader '{ToString()}' doesn't compile correctly.\nReason:" + GL.GetShaderInfoLog(_id));

                GL.DeleteShader(_id);
                return(false);
            }

            GL.AttachShader(shader, _id);


            for (var i = 0; i < GLSLExtensions.Count; i++)
            {
                if (!GLSLExtensions[i].Compile(shader, type))
                {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        ///     Removes the files form the shader.
        /// </summary>
        /// <param name="shader"></param>
        internal void Detach(GenericShader shader)
        {
            foreach (ShaderFile file in Vertex)
            {
                GL.DetachShader(shader, file);
            }

            if (Geometry != null)
            {
                foreach (ShaderFile file in Geometry)
                {
                    GL.DetachShader(shader, file);
                }
            }

            foreach (ShaderFile file in Fragment)
            {
                GL.DetachShader(shader, file);
            }

            GLDebugging.CheckGLErrors($"Error at detaching '{shader.GetType()}': %code%");
        }
Ejemplo n.º 4
0
        internal void Import(GenericShader shader)
        {
            GL.GetProgram(shader, GetProgramParameterName.ActiveUniforms, out var uniformCount);
            if (uniformCount < 1)
            {
                GLCustomActions.AtError("No uniforms has been found.");
            }

            var lastArrayKey = "";
            var array        = new UniformArray();
            var arrayFilled  = false;

            if (GLSettings.InfoEveryUniform)
            {
                GLCustomActions.AtInfo?.Invoke("Uniforms for: " + shader.GetType());
            }

            for (var i = 0; i < uniformCount; i++)
            {
                var key = GL.GetActiveUniform(shader, i, out _, out _);
                var loc = GL.GetUniformLocation(shader, key);
                if (GLSettings.InfoEveryUniform)
                {
                    GLCustomActions.AtInfo?.Invoke($"{key} - {loc}");
                }

                if (key.Contains("["))
                {
                    var keySplits = key.Split('[', ']');
                    if (keySplits[2] == "")
                    {
                        if (keySplits[1] == "0")
                        {
                            Add(keySplits[0], loc);
                        }

                        continue;
                    }

                    if (keySplits[0] != lastArrayKey)
                    {
                        if (arrayFilled)
                        {
                            Add(lastArrayKey, array);
                        }

                        array = new UniformArray
                        {
                            Location     = loc,
                            Name         = keySplits[0],
                            Parent       = this,
                            ParentShader = ParentShader
                        };

                        arrayFilled  = true;
                        lastArrayKey = keySplits[0];
                    }

                    if (keySplits[1] == "0")
                    {
                        array.UniformNames.Add(keySplits[2].Substring(1));
                    }
                }
                else
                {
                    Add(key, loc);
                }
            }

            if (arrayFilled)
            {
                Add(lastArrayKey, array);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 ///     This creates a new uniform manager, that get the location from the provided shader and with a parent.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="shader"></param>
 /// <param name="parent"></param>
 public Uniform(string name, GenericShader shader, UniformCollection parent) : this(GL.GetUniformLocation(shader, name), parent)
 {
 }
Ejemplo n.º 6
0
 /// <summary>
 ///     This creates a new uniform manager, that get the location from the provided shader and with a null parent.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="shader"></param>
 public Uniform(string name, GenericShader shader) : this(GL.GetUniformLocation(shader, name), null)
 {
 }