danijar
danijar

Reputation: 34185

Is it valid to draw with an empty shader program?

In my game there is a render module that handles shaders, framebuffers and drawing. Now I want to encapsulate the logic of these three tasks separately. The idea is that I split up the render module in three modules. I do this for decreasing code complexity and easily implementing live shader reload, but isn't important for my question.

The drawing module would create empty shader objects with glCreateProgram() and globally store them along with the path to the source file. The shader module would check for them and create the actual shader by loading the source file, compiling and linking.

But in this concept, it could occur a case where the render module already wants to draw but the shader module hadn't create the actual shader. So my question is, is it valid to draw with an empty shader program? It is completely acceptable for me that the screen would be black when this happens. Creating the shaders should be ready very soon, so the delay might be unnoticeable.

Is it valid to draw with an empty shader program? How can I implement the idea of elsewhere loaded shaders otherwise?

Upvotes: 1

Views: 784

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 473537

How do you define "valid" and "empty"?

If the program object's last link was not successful (or if it had no last link), then calling glUseProgram on it is a GL_INVALID_OPERATION error. This also means that glUseProgram will fail, so the current program will not change. So all glUniform calls will refer to that program and not the new one; if that program is zero, you get more GL_INVALID_OPERATION errors.

If there is no current program (ie, the program is 0), then attempting to render will produce undefined behavior.

Is undefined behavior "valid" for your needs? If you're not going to show the user that frame (by not calling swap buffers), then what you render won't matter. Is having all of those errors in the OpenGL error queue "valid" for you?

Upvotes: 1

filmor
filmor

Reputation: 32222

Two relevant lines in the documentation:

All operations that can be performed on a shader object are valid whether or not the shader object is attached to a program object. It is permissible to attach a shader object to a program object before source code has been loaded into the shader object or before the shader object has been compiled.

If program is zero, then the current rendering state refers to an invalid program object and the results of shader execution are undefined. However, this is not an error. If program does not contain shader objects of type GL_FRAGMENT_SHADER, an executable will be installed on the vertex, and possibly geometry processors, but the results of fragment shader execution will be undefined.

So it seems it is possible to do that, but you might not get the same result on all machines.

Upvotes: 1

Related Questions