jstoecker

04-16-2013, 01:00 AM

I'm trying to get a fairly straightforward geometry shader to work in OS X (10.8.3), but I've run into a very strange problem that is causing artifacts on screen. This same program works just fine using a Windows computer with an Nvidia graphics card. Here is a video of the problem, where you can see random triangles appearing and disappearing.

web.cs.miami.edu/home/jstoecker/video/geometry_shader_osx.mp4

I have narrowed down the problem to transferring vertex attributes between the geometry and fragment shader. It doesn't matter if I use built-in attributes (gl_Vertex, gl_Normal) or generic vertex attributes.

// VERTEX SHADER----------------------------------------------------------------

#version 120

uniform mat4 modelView;

uniform mat4 projection;

varying vec3 vert_normal_eye;

void main()

{

vert_normal_eye = (modelView * vec4(gl_Normal, 0.0)).xyz;

gl_Position = projection * modelView * gl_Vertex;

}

// GEOMETRY SHADER ------------------------------------------------------------

#version 120

#extension GL_EXT_gpu_shader4 : enable

#extension GL_EXT_geometry_shader4 : enable

varying in vec3 vert_normal_eye[3];

varying out vec3 normal_eye;

void main()

{

for (int i = 0; i < 3; i++) {

gl_Position = gl_PositionIn[i];

normal_eye = vert_normal_eye[i];

EmitVertex();

}

}

// FRAGMENT SHADER ------------------------------------------------------------

#version 120

const vec3 light = vec3(0.0, 0.0, -1.0);

varying vec3 normal_eye;

void main()

{

vec3 n = normalize(normal_eye);

float diffuse = max(max(dot(n, light), dot(-n, light)), 0.35);

gl_FragColor = vec4(diffuse, diffuse, diffuse, 0.2);

}

If I use gl_FragColor = vec4(1.0, 1.0, 1.0, 0.2) in the fragment shader and remove all lines containing normal_eye the problem goes away. Note that I don't really need a geometry shader for this example, but it's only to illustrate the problem in the smallest example I can provide.

web.cs.miami.edu/home/jstoecker/video/geometry_shader_osx.mp4

I have narrowed down the problem to transferring vertex attributes between the geometry and fragment shader. It doesn't matter if I use built-in attributes (gl_Vertex, gl_Normal) or generic vertex attributes.

// VERTEX SHADER----------------------------------------------------------------

#version 120

uniform mat4 modelView;

uniform mat4 projection;

varying vec3 vert_normal_eye;

void main()

{

vert_normal_eye = (modelView * vec4(gl_Normal, 0.0)).xyz;

gl_Position = projection * modelView * gl_Vertex;

}

// GEOMETRY SHADER ------------------------------------------------------------

#version 120

#extension GL_EXT_gpu_shader4 : enable

#extension GL_EXT_geometry_shader4 : enable

varying in vec3 vert_normal_eye[3];

varying out vec3 normal_eye;

void main()

{

for (int i = 0; i < 3; i++) {

gl_Position = gl_PositionIn[i];

normal_eye = vert_normal_eye[i];

EmitVertex();

}

}

// FRAGMENT SHADER ------------------------------------------------------------

#version 120

const vec3 light = vec3(0.0, 0.0, -1.0);

varying vec3 normal_eye;

void main()

{

vec3 n = normalize(normal_eye);

float diffuse = max(max(dot(n, light), dot(-n, light)), 0.35);

gl_FragColor = vec4(diffuse, diffuse, diffuse, 0.2);

}

If I use gl_FragColor = vec4(1.0, 1.0, 1.0, 0.2) in the fragment shader and remove all lines containing normal_eye the problem goes away. Note that I don't really need a geometry shader for this example, but it's only to illustrate the problem in the smallest example I can provide.