Вопрос

How come I can pass my geometry shader N (where N is a very high number, like 10000) vertices and render them like this:

for (int i = 0; i < gl_VerticesIn; ++i) {
    gl_Position = projectionmodelview_matrix_ * gl_PositionIn[i];
    EmitVertex();
}

...but I cant generate N vertices like this (when just passing a single vertex):

for (int i = 0; i < N; ++i) {
    float f = float(i);
    gl_Position = projectionmodelview_matrix_ * vec4(f, f, f, 1.0);
    EmitVertex();
}

How can OpenGL even differentiate between the two?

*Note: Ive modifier the code to access gl_PositionIn[0] etc, still the second one gives me an error.*

Это было полезно?

Решение

This is all based on a fundamental misunderstanding of what the GS is doing.

You do not "pass my geometry shader N vertices". gl_VerticesIn is not the number of vertices in a draw call. A Geometry Shader does not process every vertex in the draw call all at once. Geometry Shaders operate on primitives. And a draw call produces one or more primitives. gl_VerticesIn is the number of vertices in a primitive. And that is fixed for the particular primitive type that the GS operates on.

If you draw 30 vertices with GL_TRIANGLES as your primitive type, you produce 10 triangle primitives. So your GS will be called 10 times. If you draw 30 vertices with GL_TRIANGLE_STRIP as your primitive type, you will produce 28 triangle primitives. So your GS will be called 28 times.

A GS takes one primitive as input, and emits 0 or more primitives as output (that's why it ought to be called a "Primitive Shader", just like Vertex Shaders take and emit vertices and Fragment Shaders take and emit fragments). But the outputs are subject to various output size limitations. That's why your second GS chokes; you violate the output size restrictions.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top