2

This shader program works OK on Radeon 3450M, GT 550Ti, GT430 and others, but it fails on Intel HD4000. Any ideas what could be wrong, or suggestions to make the shader more Intel-friendly?

Vertex shader:

uniform mat4    uniform_Projection;
attribute vec4  attribute_Position;
attribute vec4  attribute_Color;
varying vec4    varying_Color;
vec4    varying_Color_Bak;

void main(void)
{
  varying_Color.x = clamp(abs((attribute_Position.x + 3.0f) / 5.0f), 0.1f, 1.0f);
  varying_Color.y = clamp(abs((attribute_Position.y + 3.0f) / 5.0f), 0.1f, 1.0f);
  varying_Color.z = 0.0f;//clamp(abs((attribute_Position.x + attribute_Position.y + 4.0f) / 7.0f), 0.1f, 1.0f);
  varying_Color.w = 0.9f;
  gl_Position = uniform_Projection * attribute_Position;
}

Fragment shader:

uniform mat4    uniform_Projection;
varying   vec4    varying_Color;
vec4    varying_Color_Bak;
void main (void)
{
   varying_Color_Bak = varying_Color;
   varying_Color = uniform_Projection * varying_Color;
   for (int i = 0; i < 3; i++) {
      varying_Color = sin(varying_Color);
      varying_Color = inversesqrt(abs(varying_Color));
      varying_Color = abs(log(varying_Color));
   }
   varying_Color = clamp(varying_Color, 0.1f, 1.0f);
   varying_Color = mix(varying_Color, varying_Color_Bak, 0.9f);
   gl_FragColor = sin(varying_Color);
}
6
  • 1
    It would help if you included any error codes the output of glGetProgramInfoLog (...), glGetShaderInfoLog (...), etc. It could be as simple as the shader compiler choking on the f suffix, since up until recently single-precision floating-point was the only precision, f actually generated parse errors in some stupider GLSL compilers. You're dealing with pre-GLSL 130 syntax, so I have to assume you are targeting a pretty old GLSL compiler - you might as well strip the precision suffix for portability. Oct 27, 2013 at 23:38
  • Thanks, I am adding some logging to the next version of my app, but I wanted to make sure I had already avoided the most obvious portability issues before issuing that next version. I'll try removing the 'f' suffixes.
    – Ags1
    Oct 28, 2013 at 17:09
  • @Ags1: Ideally there shouldn't be any issues. Adding the suffix is perfectly valid as per the GLSL spec. If the GLSL compiler complains about it, you should consider filing a bug report with Intel to help improve their drivers. I disagree with Andon that you're probably using an old compiler because an Ivy Bridge CPU needs recent drivers to operate and thus brings a fairly recent GLSL compiler. Also, I'm not aware of anything like that on Sandy Bridge CPUs - at least on Linux.
    – thokra
    Oct 30, 2013 at 10:52
  • @thokra: In OS X 10.6, the shader compiler does not understand the f suffix even though it is clearly defined in the GLSL 1.2 spec. under section 4.1.4. Likewise, I found on some Win32 systems using Intel GMA hardware that the compiler also generates a parse error. So it happens more frequently than you would think, but you are probably right the HD4000 GPU is much newer than the Intel hardware I was unfortunate enough to work with. It mostly stems from the fact that GLSL 1.2 added the suffix, and some compilers that are supposed to support 1.2 never caught on to this change :-\ Oct 30, 2013 at 16:38
  • @AndonM.Coleman: I don't disagree that there are GLSL compilers out there that mess up here and there. Indeed there are. However, Intel has made a huge leap forward starting with their HD GPUs. Forget the time of the GMAs ... just horrible. ;)
    – thokra
    Oct 30, 2013 at 16:41

2 Answers 2

4

The number one thing you can do to make this portable is add #version 120 to the top of the shader. As discussed in the comments, the optional f floating-point precision suffix is a GLSL 1.2 addition. In GLSL 1.1 the specification does not include this, and strictly conforming compilers will emit a parse error if they encounter this in a GLSL 1.1 shader.

The fundamental problem here is that if you do not include a #version directive at the top of your shader, compliant GLSL compilers are supposed to assume the shader is written against the GLSL 1.1 specification. Many compilers will relax these rules and you can use syntax that did not exist in GLSL 1.1 without so much as a warning, but others are very strict. Without a way to hint a GLSL compiler implementation to be more verbose in its warnings, you kind of have to familiarize yourself with the differences between each GLSL version on your own time. The ANGLE project provides a portable shader validator (which most WebGL implementations use), but it is targeted at OpenGL ES 2.0's version of GLSL and really would not help you here.

Everything else in your shader appears to be valid for GLSL 1.1. The takeaway message here is that the #version directive at the top of a GLSL shader is more important than you would think. That, and it is very difficult to diagnose shader issues if you do not read the output of glGetShaderInfoLog (...) and glGetProgramInfoLog (...) - each implementation has a different level of verbosity, but they generally all produce something useful upon compile / link failure ;)

3

Thanks all for the suggestions. It took a while to get the feedback from my testers, but I eventually got my program running on Intel HD4000 without breaking the program on other platforms.

I made hardly any changes to the vertex shader:

#version 120

uniform mat4    uniform_Projection;
attribute vec4  attribute_Position;
attribute vec4  attribute_Color;
varying vec4    varying_Color;

void main(void)
{
  varying_Color.x = clamp(abs((attribute_Position.x + 3.0) / 5.0), 0.1, 1.0);
  varying_Color.y = clamp(abs((attribute_Position.y + 3.0) / 5.0), 0.1, 1.0);
  varying_Color.z = 0.0;//clamp(abs((attribute_Position.x + attribute_Position.y + 4.0) / 7.0), 0.1, 1.0);
  varying_Color.w = 0.9;
  gl_Position = uniform_Projection * attribute_Position;
}

I removed 'f' modifiers, added a language version, and removed an unused variable declaration.

I made more changes to the fragment shader:

#version 120

uniform mat4    uniform_Projection;
varying   vec4    varying_Color;
void main (void)
{
   vec4 varying_Color_Bak;
   vec4 varying_Color_2;
   varying_Color_Bak = varying_Color;
   varying_Color_2 = uniform_Projection * varying_Color;
   for (int i = 0; i < 3; i++) {
      varying_Color_2 = sin(varying_Color_2);
      varying_Color_2 = inversesqrt(abs(varying_Color_2));
      varying_Color_2 = abs(log(varying_Color_2));
   }
   varying_Color_2 = clamp(varying_Color_2, 0.1, 1.0);
   varying_Color_2 = mix(varying_Color_2, varying_Color_Bak, 0.9);
   gl_FragColor = sin(varying_Color_2);
}

Aside from repeating all the changes from the vertex shader, I also copied the varying_Color value to a local variable. This resolved the error on HD4000, which was that the Intel chip did not allow a fragment shader to modify a varying.

I also found if I declared my local variables (varying_Color_bak and varying_Color_2) outside of main() I crashed on Linux with my NVidia GT430. But everything seems to run stably with the local variables declared inside main().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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