Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my GLSL shader fail compilation with no error message?

I'm building a game using OpenGL and C++. I'm using GLFW and GLAD. I'm currently in the process of setting up simple shaders, but I'm completely roadblocked by a compilation problem. In a nutshell, shader compilation fails with no error message.

Here's my vertex shader (it's meant to draw 2D images and text):

#version 330 core

layout (location = 0) in vec2 vPosition;
layout (location = 1) in vec2 vTexCoords;
layout (location = 2) in vec4 vColor;

out vec4 fColor;
out vec2 fTexCoords;

uniform mat4 mvp;

void main()
{
    vec4 position = mvp * vec4(vPosition, 0, 1);
    position.y *= -1;

    gl_Position = position;
    fColor = vColor;
    fTexCoords = vTexCoords;
}

And here's the relevant code to create the shader, load the shader source, compile the shader, and check for errors.

GLuint shaderId = glCreateShader(GL_VERTEX_SHADER);

std::string source = FileUtilities::ReadAllText(Paths::Shaders + filename);

GLchar const* file = source.c_str();
GLint length = static_cast<GLint>(source.size());

glShaderSource(shaderId, 0, &file, &length);
glCompileShader(shaderId);

GLint status;

glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);

if (status == GL_FALSE)
{
    GLint logSize = 0;

    glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logSize);

    std::vector<GLchar> message = std::vector<GLchar>(logSize);

    glGetShaderInfoLog(shaderId, logSize, nullptr, &message[0]);
    glDeleteShader(shaderId);

    std::cout << std::string(message.begin(), message.end());
}

Using that code, logSize is returned as 1, meaning that I'm unable to access the error message provided by GL. From what I can tell, the message doesn't exist at all. I've already seen the question posted here, in which the issue was a missing call to glCompileShader. As you can see, my code does call that function.

In attempting to solve this problem, I've already confirmed a few things.

  • My shader source (a string) is being read correctly. The source is a single string that, as far as I can tell, exactly matches the actual shader file.
  • There are no casting issues from the string source to GLchar const* or GLint (the variables file and length). Both look correct.
  • If I artificially inflate the value of logSize (to, say, 1000), the resulting message is nothing but zeroes. No error message exists.
  • I am calling glfwInit() and related functions before reaching this point in the code. Querying glGetString(GL_VERSION) does correctly return the target version (3.3.0).

Does anyone know how to fix this? As I said, my progress is completely blocked since I can't render anything (2D or 3D) without working shaders.

Thank you!

like image 617
Grimelios Avatar asked Oct 20 '25 04:10

Grimelios


1 Answers

The problem is that you never upload any shader source to the shader.

The second parameter in this line:

glShaderSource(shaderId, 0, &file, &length);

tells OpenGL to load 0 code strings to the shader (nothing). Change this to 1, and it should work.

like image 56
BDL Avatar answered Oct 21 '25 17:10

BDL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!