1

I am using indexed rendering and a geometry shader. If I pass gl_VertexID to the geometry shader, it works fine as long as I do not emit any vertices; if I emit one or more vertices, gl_VertexID (passed as any name) is zero. Why?

Using the shaders below, the geometry shader will put the correct indices into my feedback buffer, if and only if I comment out both EmitVertex calls. What am I missing?

(I can work around it, but it is bugging the hell out of me!)

VERTEX SHADER

#version 440

in vec4 position;

out VSOUT{
  vec4 gl_Position;
  int index;
} vsout;

uniform mat4 gl_ModelViewMatrix;

void main(){
    gl_Position = gl_ModelViewMatrix * position;
    vsout.index = gl_VertexID;
    vsout.gl_Position = gl_Position;
}

GEOMETRY SHADER

#version 440
#extension GL_ARB_shader_storage_buffer_object : enable

layout (lines) in;
layout (line_strip) out;

in VSOUT{
   vec4 gl_Position;
  int index;
} vdata[];

layout (std430, binding=0) buffer FeedbackBuffer{
    vec2 fb[];
};

void main(){
  int i = vdata[0].index;
  int j = vdata[1].index;

  fb[gl_PrimitiveIDIn][0] = vdata[0].index;
  fb[gl_PrimitiveIDIn][1] = vdata[1].index;

  gl_Position = gl_in[0].gl_Position;
  EmitVertex();
  gl_Position = gl_in[1].gl_Position;
  EmitVertex();
}

FRAGMENT SHADER

#version 430

out vec4 outputColor;

void main(){
    outputColor = vec4(.5,.5,.5,.5);
}
Mick
  • 11
  • 4

1 Answers1

0

So this looks like an nVidia implementation thing. If I run these shaders on a GeForce GTX580, behaviour is as described above. Using an AMD FirePro V5900, it behaves as I'd expect, with the correct values in the feedback buffer whether or not I emit vertices.

Mick
  • 11
  • 4
  • 1
    I think your problem may be that what you expect is incorrect? Per-vertex outputs become undefined after emitting a vertex in the geometry shader stage. You cannot expect anything if you do not write the values explicitly, or you are expecting undefined behavior. Write each per-vertex output after every emit, then you can expect stuff :) – Andon M. Coleman Oct 27 '16 at 10:48
  • Yep - I thought about that, but neither the index nor the feedback buffer are per-vertex outputs. The buffer works whether or not I call emitVertex (I can verify it by writing other values to it). Index is a per-vertex **input**, and it's this that seems to be broken by emitVertex. – Mick Oct 27 '16 at 14:30
  • Capturing vertex streams for transform feedback is vertex output. The GS runs after the VS, not the other way around. You're also not guaranteed coherency across all of the vertices spit out by a geometry shader unless you write the values for every vertex generated. – Andon M. Coleman Oct 28 '16 at 07:07
  • I understand the flow is VS-GS-FS. The feedback buffer is just a debugging/demonstration tool. Originally, I used two VertexIDs from an incoming line primitive to perform a lookup to determine a colour, then I emitted two vertices of that colour in a for loop, so all parameters were defined for each. Would you expect this to work? – Mick Oct 28 '16 at 11:03