Example #1
0
        public ShaderProgram(string nameParam, params Shader[] shaders)
        {
            handle = GL.CreateProgram();

            foreach (var shader in shaders)
            {
                GL.AttachShader(handle, shader.Handle);
            }

            GL.LinkProgram(handle);

            // Check link errors
            int successValue = 0;

            GL.GetProgram(handle, GetProgramParameterName.LinkStatus, out successValue);
            if (successValue == 0)
            {
                Logger.LogError(Logger.ErrorState.Limited, "Shader link failed: " + GL.GetProgramInfoLog(handle));
            }

            programName = nameParam;
            Logger.LogInfoLinePart("Creating Shader Program: ", ConsoleColor.Gray);
            Logger.LogInfoLinePart(programName, ConsoleColor.Cyan);
            Logger.LogInfoLineEnd();

            // Load Uniforms
            ///////////////////////////////////
            int uniformAmount = -1;

            GL.GetProgram(handle, GetProgramParameterName.ActiveUniforms, out uniformAmount);
            Error.checkGLError("Shader()");
            ShaderUniformManager uniManager = ShaderUniformManager.GetSingleton();

            Logger.LogInfo("Program linked.");

            uniforms = new List <ShaderUniform>(uniformAmount);

            int               maxShaderNameSize = 100;
            StringBuilder     shaderName        = new StringBuilder(maxShaderNameSize);
            int               writtenLength;
            int               uniformSize;
            ActiveUniformType type;

            Logger.LogInfo("Uniforms (" + uniformAmount + ") >>>");
            for (int i = 0; i < uniformAmount; i++)
            {
                GL.GetActiveUniform(this.handle, i, maxShaderNameSize, out writtenLength, out uniformSize, out type, shaderName);

                string         uniformName = shaderName.ToString();
                ShaderSizeInfo info        = uniManager.GetTypeSizeInfo(type);

                ShaderUniform uniform = uniManager.CreateShaderUniform(uniformName, type, GetUniformLocation(this.handle, uniformName));

                Logger.LogInfoLinePart(" " + i, ConsoleColor.White);
                Logger.LogInfoLinePart("\t" + uniManager.GetDataTypeString(uniManager.UniformTypeToDataType(type)), ConsoleColor.Cyan);
                Logger.LogInfoLinePart("\t" + uniformName + " (" + info.sizeElements + ")", ConsoleColor.White);
                if (uniform.IsValid())
                {
                    Logger.LogInfoLinePart("\t\t [", ConsoleColor.Gray);
                    Logger.LogInfoLinePart("OK", ConsoleColor.Green);
                    Logger.LogInfoLinePart("]", ConsoleColor.Gray);
                    uniforms.Add(uniform);
                }
                Logger.LogInfoLineEnd();
            }

            int attributeAmount = -1;

            GL.GetProgram(handle, GetProgramParameterName.ActiveAttributes, out attributeAmount);
            ActiveAttribType attribType;
            int attrSize = -1;

            Logger.LogInfo("Attributes (" + attributeAmount + ") >>>");

            attributes = new List <ShaderAttribute>(attributeAmount);

            for (int i = 0; i < attributeAmount; i++)
            {
                GL.GetActiveAttrib(this.handle, i, maxShaderNameSize, out writtenLength, out attrSize, out attribType, shaderName);

                string          attribName = shaderName.ToString();
                int             location   = GetAttributeLocation(handle, attribName);
                ShaderSizeInfo  info       = uniManager.GetTypeSizeInfo(attribType);
                ShaderAttribute attribute  = uniManager.CreateShaderAttribute(attribName, attribType, GetAttributeLocation(this.handle, attribName), info.sizeElements);

                Logger.LogInfoLinePart(" " + i, ConsoleColor.White);
                Logger.LogInfoLinePart("\t" + uniManager.GetDataTypeString(uniManager.AttribTypeToDataType(attribType)), ConsoleColor.Cyan);
                Logger.LogInfoLinePart("\t" + attribName + " (" + info.sizeElements + ")", ConsoleColor.White);
                Logger.LogInfoLinePart("\t " + location, ConsoleColor.Red);
                if (attribute.IsValid())
                {
                    Logger.LogInfoLinePart("\t\t [", ConsoleColor.Gray);
                    Logger.LogInfoLinePart("OK", ConsoleColor.Green);
                    Logger.LogInfoLinePart("]", ConsoleColor.Gray);
                    attributes.Add(attribute);
                }
                Logger.LogInfoLineEnd();
            }

            foreach (var shader in shaders)
            {
                GL.DeleteShader(shader.Handle);
            }
        }