PDA

View Full Version : Shader loading function causes invalid operation error on glUseProgram



Ceannfaolaidh
08-12-2013, 09:25 AM
I've been trying to modify a SDL-GL framework I'm writing to include this shader loader:


GLuint loadShaderFromFile(const char* shaderFileName, GLenum shaderType)
{
GLuint shaderId = 0;

FILE* shaderFile = NULL;
shaderFile = fopen(shaderFileName, "rb");
if(shaderFile == NULL)
{
genericError("Failed to open shader file.");
}

long shaderFileSize = -1;
fseek(shaderFile, 0, SEEK_END);
shaderFileSize = ftell(shaderFile);
if(shaderFileSize == -1)
{
genericError("Failed to determine shader file size.");
}
rewind(shaderFile);

GLchar* shaderSourceBuffer = NULL;
shaderSourceBuffer = (char*)malloc(shaderFileSize + 1);
if(shaderSourceBuffer == NULL)
{
genericError("Failed to allocate shader source buffer.");
}

if(shaderFileSize != fread(shaderSourceBuffer, sizeof(char), shaderFileSize, shaderFile))
{
fprintf(stderr, "Error while copying shader source to buffer.\n");
free(shaderSourceBuffer);
exit(EXIT_FAILURE);
}

shaderSourceBuffer[shaderFileSize] = '\0';
const GLchar* shaderSource = shaderSourceBuffer;
free(shaderSourceBuffer);

shaderId = glCreateShader(shaderType);
if(shaderId == 0)
{
genericError("Failed to get unique ID for shader.");
}

int shaderCompiled = GL_FALSE;
glShaderSource(shaderId, 1, &shaderSource, NULL);
glCompileShader(shaderId);
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &shaderCompiled);
if(shaderCompiled = GL_FALSE)
{
genericError("Shader compilation failed.");
}
checkGLError("Shader compilation failed (OpenGL error).");

return shaderId;
}


genericError and checkGLError are functions that call exit(EXIT_FAILURE) if a required state isn't met or an OpenGL error has occurred and not been cleared, respectively. This function seems to run without a hitch. However, when used in this function:



void createShaders(void)
{
vertexShaderId = loadShaderFromFile("SimpleShader.vertex.glsl", GL_VERTEX_SHADER);
fragmentShaderId = loadShaderFromFile("SimpleShader.fragment.glsl", GL_FRAGMENT_SHADER);

programId = glCreateProgram();
if(glIsProgram(programId) == GL_FALSE)
{
genericError("Failed to acquire unique program id.");
}

glAttachShader(programId, vertexShaderId);
glAttachShader(programId, fragmentShaderId);
checkGLError("Failed to attach shaders to program.");

glLinkProgram(programId);
checkGLError("Failed to link program.");

glUseProgram(programId);
checkGLError("Failed to designate shader program.");
}


No OpenGL errors appear until I call glUseProgram(), at which point I get GL_INVALID_OPERATION. Every other check passes. As a matter of fact, with the exact same shaders hard-coded into the program, the entire program executes flawlessly. What is causing this error? If anyone can help, I would greatly appreciate it.

carsten neumann
08-12-2013, 11:34 AM
if(shaderCompiled = GL_FALSE)
{
genericError("Shader compilation failed.");
}


Typo, conditional should use '==' or simply if(!shaderCompiled) - I'm assuming the language is C/C++.
You should also check if your program successfully linked (similar to how you check for successful compiles), since failing to link does not produce an OpenGL error (in the glGetError sense) - and I suspect a link failure is the root cause for your problem.

Ceannfaolaidh
08-12-2013, 08:53 PM
Always the little things, isn't it? Thanks a ton for your help. That extra = helped me locate a minuscule error in my glsl code that was causing a compilation error.