Grimelios
Grimelios

Reputation: 361

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.

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!

Upvotes: 4

Views: 2579

Answers (1)

BDL
BDL

Reputation: 22175

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.

Upvotes: 7

Related Questions