Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

GLSL is fundamentally different from other shader solutions because the server (GPU driver) is responsible for shader compilation. Cg and HLSL are (afaik) generally compiled a priori and sent to the GPU in that way.

This causes some real-world practical issues:

  • many drivers provide buggy compilers
  • compilers differ in terms of strictness (one GPU can accept a program while another won't)
  • also we can't know how the assembler code will be optimised

What are the upsides of GLSL's current approach? Is it worth it?

share|improve this question

2 Answers 2

The advantage is that it's easy to write programs that dynamically generate GLSL code, for example you can dynamically generate new shaders when the lights affecting an object changes.

The disadvantage is that the compile time can become significant and that the compilers generally do less optimization (in order to keep compile times down).

share|improve this answer

This model pushes on code portability, decoupling the GLSL code from any specific platform (hardware and/or software). Because it is a royalty-free standard, every vendor could decide to support GLSL platform. Normally this support will result in a OS-specific OpenGL driver implementation.


many drivers provide buggy compilers

Probably there are many bugs in the drivers out there, but my experience says that the most of the times the bug is not a bug, but it is a misunderstanding of the GLSL specification. For example, this code run fine on NVIDIA platforms, while not on Intel platforms:

// We are in fragment shader

in vec2 ds_TexCoord[3];

uniform int ds_FrontMaterialAmbientTexCoord = -1;

uniform sampler2D ds_FrontMaterialAmbientTexture;

...

if (ds_FrontMaterialAmbientTexCoord >= 0) {
    fragmentMaterial.AmbientColor = texture(ds_FrontMaterialAmbientTexture, ds_TexCoord[ds_FrontMaterialAmbientTexCoord]);
}

The trick here is that the array cannot be accessed using directly an uniform variable. Indeed the code running on NVIDIA and Intel platforms is:

if (ds_FrontMaterialAmbientTexCoord >= 0) {
     int index = ds_FrontMaterialAmbientTexCoord;
    fragmentMaterial.AmbientColor = texture(ds_FrontMaterialAmbientTexture, ds_TexCoord[index]);
}

Once you got few tricks, writing portable GLSL code is just like writing C.


compilers differ in terms of strictness (one GPU can accept a program while another won't)

Use the strictest one. AMD and Intel have a strict interpretation of the GLSL specification, while NVIDIA don't: this is because the GLSL compiler is based on CG implementation, indeed being more permessive than what should be allowed.

The solution is simple: follow strctly the GLSL specification.


also we can't know how the assembler code will be optimised

If you can about optimization, write in ARB assembly (here some specification and hints). The driver will probably do the best it can for the current platform, will you do better?

Remember that the main goal is portability!

share|improve this answer
    
I still believe that DirectX's approach to driver compilation (one reference compiler to a standard bytecode) would be better for portability. Like you, I once thought that GLSL compilation is the easy part of drivers, but turns out it isn't. And in the end we're stuck with implementations like the Galaxy S' which allegedly can crash when it sees a #define. That's nowhere near portability. And desktop OpenGL isn't better. –  Kos Sep 1 '12 at 16:28
    
This is questionable. –  Luca Sep 1 '12 at 19:07
    
Yup, it is. DirectX drivers need to go through a complicated acceptance procedure (including a rigorous test suite), maybe OpenGL needs something like this too? Are there any decent OpenGL implementation test suites? –  Kos Sep 2 '12 at 9:50
    
I think so, even if I never developed an OpenGL driver. Nobody can expose the brand without a conformance test. I know that COLLADA have a conformance test suite, released by Khronos (the board defining OpenGL and GLSL specs). But I'd like to avoid DirectX vs OpenGL debacle, don't you? :) –  Luca Sep 2 '12 at 20:49
    
Yeah :), plus I'm in no position to actually participate in one. I'd call myself an OpenGL enthusiast but I feel kind of dissatisfied with the current state of affairs (and implementations), I want to get a better perspective. –  Kos Sep 3 '12 at 7:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.