giawa / opengl4csharp

OpenGL 4 Bindings (partially based on OpenTK) for C#
Other
232 stars 61 forks source link

Shader compile error "undefined variable in_position" #36

Closed TheRealNOIG closed 4 years ago

TheRealNOIG commented 4 years ago

Why would this code return error c1008: undefined variable \ "in_position\" when using genericVAOs This error doesn't seem to make sense. OpenGL dose not have a variable called in_position so why would the compiler be looking for it while creating the shader program. The only place I can find using githubs search that uses in_position is the VAO class. However, in my code I am creating my own VAO that inheritance from GenericVAO that is not looking for a variable named in_position.

        public Shader(string source, ShaderType type)
        {
            this.ShaderType = type;
            this.ShaderID = Gl.CreateShader(type);

            Gl.ShaderSource(ShaderID, source);
            Gl.CompileShader(ShaderID);

            //Check whether the shader compiled successfully.
            //If not then throw an error with the compile error.
            if (!Gl.GetShaderCompileStatus(ShaderID))
            {
                throw new Exception(ShaderLog);
            }
        }

Here is my VAO class but I dont know why this would effect the creation of the shaderprogram because the shader program is created first.

    public class SpriteVAO : GenericVAO
    {
        public SpriteVAO(ShaderProgram program, VBO<Vector2> vertex, VBO<uint> element)
            : base(program)
        {
            List<IGenericVBO> vbos = new List<IGenericVBO>();
            vbos.Add(new GenericVBO<Vector2>(vertex, "position"));
            vbos.Add(new GenericVBO<uint>(element));
            Init(vbos.ToArray());
        }

        public static SpriteVAO CreateQuad(ShaderProgram program, Vector3 size)
        {
            Vector2[] vertices = new Vector2[] { Vector2.Zero, new Vector2(size.X, 0),
                new Vector2(size.X, size.Y), new Vector2(0, size.Y) };
            uint[] indices = new uint[] { 0, 1, 2, 2, 3, 0 };

            return new SpriteVAO(program, new VBO<Vector2>(vertices), new VBO<uint>(indices, BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticRead));
        }
    }
giawa commented 4 years ago

Are you able to provide your shader source?

TheRealNOIG commented 4 years ago

I tested this in a environment that only has that code. Any working shader file (without a in vec3 in_position) gives this error. Here is a minimal viable shader for testing

#version 330

uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 transformation_matrix;

out vec3 color;

attribute vec3 position;

void main(void)
{
  gl_Position = projection_matrix * view_matrix * transformation_matrix * vec4(position, 1);
  color = vec3(position.x+0.5, position.z+0.5, position.y+0.5);
}
#version 330

in vec3 color;

void main(void)
{
  gl_FragColor = vec4(color, 1);
}
giawa commented 4 years ago

I took your code above and made a new project, here's my Program.cs: https://gist.github.com/giawa/c6bfcfa4d2924e86a5af0fcff28dce33

This runs just fine and gives the following:

temp

Is it possible there is something else using a standard VAO somewhere?

TheRealNOIG commented 4 years ago

So I just got back into work and this just got more confusing. So when I started up the app without changing anything from yesterday the error went away. So I tried testing it by changing position with other_position and the error came back saying no variable position but I already searched for that variable in my project and it has already been replaced everywhere in the code with other_position. I am even printing out the string before I pass it to the shaderprogram and it shows the updated string.

TheRealNOIG commented 4 years ago

So I think I found the issue. I am using Resources.resx to load in all my files and looking through the .Designer.cs file it shows that it has some of the old code for the txt files even though when I look at it in the resources.resx page it shows the new code. I will close out this issue and keep looking into why the resources.resx is cashing old txt files.

TheRealNOIG commented 4 years ago

BTW I wanted to note that it is not returning the old code but the summary of the get function has the old code in it which I think is messing with the shader compiler

Function called

        /// <summary>
        ///   Looks up a localized string similar to #version 330
        ///
        ///uniform mat4 projection_matrix;
        ///uniform mat4 view_matrix;
        ///uniform mat4 transformation_matrix;
        /// 
        ///out vec2 uv;
        ///
        ///in vec2 position;
        ///in vec2 in_uv;
        ///
        ///void main(void)
        ///{
        ///  gl_Position = projection_matrix * view_matrix * transformation_matrix * vec4(position, 0, 1);
        ///  uv = in_uv;
        ///}.
        /// </summary>
        public static string textured2DVertexShader {
            get {
                return ResourceManager.GetString("textured2DVertexShader", resourceCulture);
            }
        }

String returned

#version 330

uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 transformation_matrix;

out vec2 uv;

in vec2 other_position;
in vec2 in_uv;

void main(void)
{
  gl_Position = projection_matrix * view_matrix * transformation_matrix * vec4(other_position, 0, 1);
  uv = in_uv;
}
TheRealNOIG commented 4 years ago

Another note now it is completely fine with other_position but is now saying 'Shader did not contain 'in_uv'.' So I have no idea what is going on...

giawa commented 4 years ago

Does it appear to only happen when you load shaders from resources?

TheRealNOIG commented 4 years ago

No. After more testing The issue was still happening and I was completely unable to debug the issue. I did just create my own handler for shaders and shader program and everything is now working without issue.

Edit: Im just going to pull OpenGL.Platform out of my project and switch over to FreeGlut or GLFW to allow for x64 on Windows and Linux support. Luckily over time Ive already been replacing parts of OpenGL.Platform and the only part im using now is creating the SDL2 window and the GenericVBO class.

giawa commented 4 years ago

Okay, sounds good. I've had really good luck with both GLFW and SDL2. Worth noting, GenericVBO isn't part of OpenGL.Platform, and it part of the OpenGL namespace. Good luck!