Geometry Shader: Difference between revisions
No edit summary |
Major modification of the GS document. |
||
Line 1: | Line 1: | ||
A '''Geometry Shader''' (GS) is a [[Shader]] program that governs the processing of primitives. It happens after primitive assembly, as an additional optional step in that part of the [[Rendering Pipeline Overview|pipeline]]. A GS can create new primitives, unlike [[Vertex Shader|vertex shaders]], which are limited to a 1:1 input to output ratio. A GS can also do layered rendering; this means that the GS can specifically say that a primitive is to be rendered to a particular layer of the framebuffer. | {{infobox feature | ||
| version = 3.2 | |||
| arb_extension = ARB_geometry_shader4 | |||
}} | |||
A '''Geometry Shader''' (GS) is a [[Shader]] program written in [[GLSL]] that governs the processing of primitives. It happens after primitive assembly, as an additional optional step in that part of the [[Rendering Pipeline Overview|pipeline]]. A GS can create new primitives, unlike [[Vertex Shader|vertex shaders]], which are limited to a 1:1 input to output ratio. A GS can also do layered rendering; this means that the GS can specifically say that a primitive is to be rendered to a particular layer of the framebuffer. | |||
Unlike other shader stages, a geometry shader is optional and does not have to be used. | Unlike other shader stages, a geometry shader is optional and does not have to be used. | ||
{{note|While geometry shaders have had previous extensions like GL_EXT_geometry_shader4 and GL_ARB_geometry_shader4, these extensions expose the API and GLSL functionality in ''very'' different ways from the core feature. This page describes only the core feature.}} | |||
== Primitive in/out specification == | |||
Each geometry shader is designed to accept a specific [[Primitive]] type as input and to output a specific primitive type. The accepted input primitive type is defined in the shader: | |||
layout({{param|input_primitive}}) in; | |||
The {{param|input_primitive}} type ''must'' match the primitive type used with the rendering command that renders with this shader program. The valid values for {{param|input_primitive}}, along with the valid OpenGL [[Primitive|primitive types]], are: | |||
{| | |||
! GS input | |||
! OpenGL primitives | |||
! vertex count | |||
|- | |||
| points | |||
| {{enum|GL_POINTS}} | |||
| 1 | |||
|- | |||
| lines | |||
| {{enum|GL_LINES}}, {{enum|GL_LINE_STRIP}}, {{enum|GL_LINE_LIST}} | |||
| 2 | |||
|- | |||
| line_adjacency | |||
| {{enum|GL_LINES_ADJACENCY}}, {{enum|GL_LINE_STRIP_ADJACENCY}} | |||
| 4 | |||
|- | |||
| triangles | |||
| {{enum|GL_TRIANGLES}}, {{enum|GL_TRIANGLE_STRIP}}, {{enum|GL_TRIANGLE_FAN}} | |||
| 3 | |||
|- | |||
| triangles_adjacency | |||
| {{enum|GL_TRIANGLES_ADJACENCY}}, {{enum|GL_TRIANGLE_STRIP_ADJACENCY}} | |||
| 6 | |||
|} | |||
The vertex count is the number of vertices that the GS receives per-input primitive. | |||
The output primitive type is defined as follows: | |||
layout({{param|output_primitive}}, max_vertex = {{param|vert_count}}) out; | |||
The {{param|output_primitive}} may be one of the following: | |||
* points | |||
* line_strip | |||
* triangle_strip | |||
These work exactly the same way their counterpart OpenGL rendering modes do. To output individual triangles or lines, simply use {{code|EndPrimitive}} (see below) between each triangle/line. | |||
There must be a {{code|max_vertex}} declaration for the output, and | |||
=== Instancing === | |||
{{infobox feature | |||
| name = GS Instancing | |||
| version = 4.0 | |||
| core_extension = [http://www.opengl.org/registry/specs/ARB/gpu_shader5.txt ARB_gpu_shader5] | |||
}} | |||
The GS can also be instanced. This causes the GS to execute multiple times for the same primitive. This is useful for layered rendering and outputs to multiple streams (see below). | |||
To use instancing, there must be an input layout qualifier: | |||
layout(invocations = {{param|num_instances}}) in; | |||
The value of {{param|num_instances}} is a compile-time constant, and must not be larger than {{enum|MAX_GEOMETRY_SHADER_INVOCATIONS}}. The built-in value {{code|gl_InvocationID}} specifies the particular instance of this shader. | |||
The output primitives from instances are ordered by the {{code|gl_InvocationID}}. So 2 primitives written from 3 instances will create a primitive stream of: (prim0, inst0), (prim0, inst1), (prim0, inst2), (prim1, inst0), ... | |||
== Input values == | |||
== Output values == | |||
=== Output streams === | |||
{{stub}} | {{stub}} | ||
[[Category:Shaders]] | [[Category:Shaders]] |
Revision as of 03:48, 19 August 2012
Core in version | 3.2 | |
---|---|---|
ARB extension | ARB_geometry_shader4 |
A Geometry Shader (GS) is a Shader program written in GLSL that governs the processing of primitives. It happens after primitive assembly, as an additional optional step in that part of the pipeline. A GS can create new primitives, unlike vertex shaders, which are limited to a 1:1 input to output ratio. A GS can also do layered rendering; this means that the GS can specifically say that a primitive is to be rendered to a particular layer of the framebuffer.
Unlike other shader stages, a geometry shader is optional and does not have to be used.
Primitive in/out specification
Each geometry shader is designed to accept a specific Primitive type as input and to output a specific primitive type. The accepted input primitive type is defined in the shader:
layout(input_primitive) in;
The input_primitive type must match the primitive type used with the rendering command that renders with this shader program. The valid values for input_primitive, along with the valid OpenGL primitive types, are:
GS input | OpenGL primitives | vertex count |
---|---|---|
points | GL_POINTS | 1 |
lines | GL_LINES, GL_LINE_STRIP, GL_LINE_LIST | 2 |
line_adjacency | GL_LINES_ADJACENCY, GL_LINE_STRIP_ADJACENCY | 4 |
triangles | GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN | 3 |
triangles_adjacency | GL_TRIANGLES_ADJACENCY, GL_TRIANGLE_STRIP_ADJACENCY | 6 |
The vertex count is the number of vertices that the GS receives per-input primitive.
The output primitive type is defined as follows:
layout(output_primitive, max_vertex = vert_count) out;
The output_primitive may be one of the following:
- points
- line_strip
- triangle_strip
These work exactly the same way their counterpart OpenGL rendering modes do. To output individual triangles or lines, simply use EndPrimitive (see below) between each triangle/line.
There must be a max_vertex declaration for the output, and
Instancing
Core in version | 4.0 | |
---|---|---|
Core ARB extension | ARB_gpu_shader5 |
The GS can also be instanced. This causes the GS to execute multiple times for the same primitive. This is useful for layered rendering and outputs to multiple streams (see below).
To use instancing, there must be an input layout qualifier:
layout(invocations = num_instances) in;
The value of num_instances is a compile-time constant, and must not be larger than MAX_GEOMETRY_SHADER_INVOCATIONS. The built-in value gl_InvocationID specifies the particular instance of this shader.
The output primitives from instances are ordered by the gl_InvocationID. So 2 primitives written from 3 instances will create a primitive stream of: (prim0, inst0), (prim0, inst1), (prim0, inst2), (prim1, inst0), ...
Input values
Output values
Output streams
This article is a stub. You can help the OpenGL Wiki by expanding it. |