Strange bug when linking GLSL shaders twice

Discussion created by lvezzaro on Nov 4, 2011

What I'm going to describe is a very strange bug, that took me a day to find out in our software.

What we are doing is putting all the shader code in a single file, and using preprocessor definitions to compile only parts of the shader (e.g. #define VERTEX_SHADER is pre-pendended to the shader code to compile only the vertex shader part of the file).

This worked well until I wanted to add geometry shader support. Since those are an optional part of the pipeline and since a different geometry shader is needed for different primitives, I couldn't afford to modify all the existing shader files to add pass-through geometry shader code.

So what I did is try to first link the shader programs by including the geometry shader, even if it has no main(). Then if the link fails, I detach the geometry shader from the program object and link again.

To my dismay, shaders that worked before, suddenly stopped working when using this approach.

I was able to reproduce the issue in a simple program. On my machine (ATI Firepro V7800), I only see a blue background. With an NVIDIA Quadro it works just fine.

You can find the program here (if a page shows up instead, click the first link on the page):

Either avoiding to attach the empty geometry shader (by commenting line 150 in flat.cpp) to the program or changing the line:

float c = 1.0;


const float c = 1.0;

in the shader code will also display the white triangle, in addition to the background.

To build the code on Windows, you'll need Visual Studio 2008 or later (external libraries are included).
To build on Linux you'll need Scons and development libraries of SDL and glew.

I encountered this problem in both Linux and Windows, I'm using the latest driver.

Please let me know if you are able to reproduce it, and hopefully fix this.

Best regards,

Luca Vezzaro