Goz3rr's picture

Attribute goes missing

I have these helper classes:

public class ShaderProgram {
    private int id;
    public int ID {
        get {
            if(id == 0) id = GL.CreateProgram();
            return id;
        }
    }
 
    public void Link() {
        int status;
 
        GL.LinkProgram(ID);
        GL.GetProgram(ID, GetProgramParameterName.LinkStatus, out status);
        if(status != 1) throw new Exception(GL.GetProgramInfoLog(ID));
 
#if DEBUG
        GL.ValidateProgram(ID);
        GL.GetProgram(ID, GetProgramParameterName.ValidateStatus, out status);
        if(status != 1) throw new Exception(GL.GetProgramInfoLog(ID));
#endif
 
        int length;
        GL.GetProgram(ID, GetProgramParameterName.InfoLogLength, out length);
        if(length > 1) Console.WriteLine("ShaderProgram {0} info log not empty:\n\t{1}", ID, GL.GetProgramInfoLog(ID));
 
        int attributes;
        GL.GetProgram(ID, GetProgramParameterName.ActiveAttributes, out attributes);
        Console.WriteLine("Program {0} linked with {1} attributes:", ID, attributes);
        for(int i = 0; i < attributes; i++) {
            int size;
            ActiveAttribType type;
            string attrib = GL.GetActiveAttrib(ID, i, out size, out type);
            Console.WriteLine("\t{0}: {1} {2} {3}", i, size, type, attrib);
        }
    }
}
public class ShaderCompileException : Exception {
    public string SourceCode { get; set; }
 
    public ShaderCompileException(string message, string sourcecode) : base(message) {
        SourceCode = sourcecode;
    }
}
 
public class Shader {
    private int id;
    public int ID {
        get {
            if(id == 0) id = GL.CreateShader(Type);
            return id;
        }
    }
 
    public ShaderType Type { get; protected set; }
 
    public Shader(ShaderType type, string source) {
        Type = type;
        Load(source);
    }
 
    public void Load(string source) {
        GL.ShaderSource(ID, source);
        GL.CompileShader(ID);
 
        int status;
        GL.GetShader(ID, ShaderParameter.CompileStatus, out status);
        if(status != 1) throw new ShaderCompileException(GL.GetShaderInfoLog(ID), source);
 
        int length;
        GL.GetShader(ID, ShaderParameter.InfoLogLength, out length);
        if(length > 1) Console.WriteLine("{0} info log not empty:\n\t{1}", this, GL.GetShaderInfoLog(ID).TrimEnd('\n').Replace("\n", "\n\t"));
    }
 
    public void Attach(ShaderProgram program) {
        GL.AttachShader(program.ID, ID);
    }
}

And using the following code:

    Program = new ShaderProgram();
 
    Shader vertShader = new Shader(ShaderType.VertexShader, @"
#version 130
 
in vec2 in_pos;
in vec3 in_color;
 
out vec3 out_color;
 
void main() {
    out_color = in_color;
    gl_Position = vec4(in_pos, 0.0, 1.0);
}
");
 
    Shader fragShader = new Shader(ShaderType.FragmentShader, @"
#version 130
 
in vec3 in_color;
out vec4 out_color;
 
void main () {
    out_color = vec4(in_color, 1.0);
}
");
 
    vertShader.Attach(Program);
    fragShader.Attach(Program);
    Program.Link();

i get this output:

Program 3 linked with 1 attributes:
	0: 1 FloatVec2 in_pos

Where is the in_color attribute going?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Frassle's picture

Your attributes between the vertex and fragment stages don't match.
Your vertex shader declares an output attribute "vec3 out_color" but your fragment shaders input attribute is "vec3 in_color", the names don't match! So GL can't link them up, thus the out_color attribute in the vertex shader is not active. It then follows that as the in_color attribute is only used to set the out_color attribute it's not really used and so not active.

Goz3rr's picture

Oh never realized that, i though the name didn't matter it was just about if you have one out and one in they'd match automatically