Exemplo n.º 1
0
 private bool checkAttribute(ShaderAttribute attribute)
 {
     if (attribute.location == -1)
     {
         Logger.LogError(Logger.ErrorState.Limited, "Location of attribute " + attribute.name + " is invalid");
         return(false);
     }
     return(true);
 }
Exemplo n.º 2
0
        private bool bufferData(List <ShaderAttribute> attributes)
        {
            VertexAmount = positions.Count;
            if (hasPositionData)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, positionBufferHandle);

                GL.BufferData(BufferTarget.ArrayBuffer, VertexAmount * MeshData.getPositionSizeBytes()
                              , positions.ToArray(), BufferUsageHint.StaticDraw);

                ShaderAttribute aPosition = getAttribute(ShaderAttributeName.Position, attributes);
                if (checkAttribute(aPosition))
                {
                    enableAttribute(aPosition);
                }

                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            }

            if (hasTexCoordData)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, texCoordBufferHandle);

                GL.BufferData(BufferTarget.ArrayBuffer, VertexAmount * MeshData.getTexCoordSizeBytes()
                              , texCoords.ToArray(), BufferUsageHint.StaticDraw);

                ShaderAttribute aTexCoord = getAttribute(ShaderAttributeName.TexCoord, attributes);
                if (checkAttribute(aTexCoord))
                {
                    enableAttribute(aTexCoord);
                }
            }

            if (hasNormalData)
            {
                GL.BindBuffer(BufferTarget.ArrayBuffer, normalBufferHandle);

                GL.BufferData(BufferTarget.ArrayBuffer, VertexAmount * MeshData.getNormalSizeBytes()
                              , normals.ToArray(), BufferUsageHint.StaticDraw);

                ShaderAttribute aNormal = getAttribute(ShaderAttributeName.Normal, attributes);
                if (checkAttribute(aNormal))
                {
                    enableAttribute(aNormal);
                }
            }

            if (hasIndexData)
            {
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferHandle);
                GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Count * BytesPerFloat, indices.ToArray(), BufferUsageHint.StaticDraw);
            }

            Error.checkGLError("MeshData.bufferData " + sourceFileName);

            return(true);
        }
Exemplo n.º 3
0
        private void enableAttribute(ShaderAttribute attr)
        {
            GL.VertexAttribPointer(index: attr.location, size: attr.elementSize
                                   , type: VertexAttribPointerType.Float
                                   , normalized: false, stride: 0, offset: 0);

            GL.EnableVertexAttribArray(attr.location);

            Error.checkGLError("MeshData.enableAttribute " + attr.name + " location:" + attr.location + " size: " + attr.elementSize);
        }
Exemplo n.º 4
0
        public static ShaderAttribute getAttribute(ShaderAttributeName name, ShaderProgram shaderProgram)
        {
            foreach (ShaderAttribute attr in shaderProgram.attributes)
            {
                if (attr.name == name)
                {
                    return(attr);
                }
            }
            Logger.LogError(Logger.ErrorState.Critical, "Did not get attribute " + ShaderUniformManager.GetSingleton().GetAttributeName(name));
            ShaderAttribute invalid = new ShaderAttribute(ShaderAttributeName.InvalidAttributeName, ShaderDataType.InvalidType);

            return(invalid);
        }
Exemplo n.º 5
0
        public ShaderAttribute CreateShaderAttribute(string nameString, ActiveAttribType type, int location, int sizeElements)
        {
            ShaderAttribute returnValue;

            if (supportedAttributes.ContainsKey(nameString))
            {
                ShaderAttribute supported  = supportedAttributes[nameString];
                ShaderDataType  attribType = AttribTypeToDataType(type);
                if (supported.dataType == attribType)
                {
                    /*
                     * Logger.LogInfoLinePart("Created attribute", System.ConsoleColor.Gray);
                     * Logger.LogInfoLinePart(" (", System.ConsoleColor.Gray);
                     * Logger.LogInfoLinePart("" + location, System.ConsoleColor.Red);
                     * Logger.LogInfoLinePart(") ", System.ConsoleColor.Gray);
                     * Logger.LogInfoLinePart(GetDataTypeString(attribType), System.ConsoleColor.Cyan);
                     * Logger.LogInfoLinePart(" " + nameString, System.ConsoleColor.Gray);
                     * Logger.LogInfoLineEnd();
                     */
                    returnValue = new ShaderAttribute(supported.name, supported.dataType, location, sizeElements);
                }
                else
                {
                    Logger.LogError(Logger.ErrorState.Critical, "Shader data type mismatch with supported uniform type: "
                                    + " Expected: " + GetDataTypeString(supported.dataType)
                                    + " Got: " + GetDataTypeString(attribType));
                    returnValue = new ShaderAttribute(ShaderAttributeName.InvalidAttributeName, ShaderDataType.InvalidType);
                }
            }
            else if (nameString.StartsWith("uc"))
            {
                // Custom attribute is ok
                returnValue = new ShaderAttribute(ShaderAttributeName.CustomAttribute, AttribTypeToDataType(type), location, sizeElements);
            }
            else
            {
                Logger.LogError(Logger.ErrorState.Critical, "Unsupported shader attribute: " + nameString + ", of type:" + GetDataTypeString(AttribTypeToDataType(type)));
                returnValue = new ShaderAttribute(ShaderAttributeName.InvalidAttributeName, ShaderDataType.InvalidType);
            }

            return(returnValue);
        }
Exemplo n.º 6
0
        private ShaderAttribute getAttribute(ShaderAttributeName name, List <ShaderAttribute> attributes)
        {
            ShaderAttribute result = new ShaderAttribute();

            result.location = -1;

            foreach (ShaderAttribute a in attributes)
            {
                if (a.name.Equals(name))
                {
                    if (checkAttribute(a))
                    {
                        result = a;
                    }
                }
            }

            if (result.location == -1)
            {
                Logger.LogError(Logger.ErrorState.Limited, "Did not get attribute ");
            }
            return(result);
        }
Exemplo n.º 7
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);
            }
        }