39
\$\begingroup\$

What is a fragment in a fragment shader? Wikipedia says that:

In general, a fragment can be thought of as the data needed to shade the pixel, plus the data needed to test whether the fragment survives to become a pixel (depth, alpha, stencil, scissor, window ID, etc.)

So is it textures, vertices or something else?

\$\endgroup\$

4 Answers 4

32
\$\begingroup\$

In computer graphics, a fragment is the data necessary to generate a single pixel's worth of a drawing primitive in the frame buffer.

This data may include, but is not limited to:

  • raster position
  • depth
  • interpolated attributes (color, texture coordinates, etc.)
  • stencil
  • alpha
  • window ID

As a scene is drawn, drawing primitives are rasterized into fragments which are textured and combined with the existing frame buffer. How a fragment is combined with the data already in the frame buffer depends on various settings. In a typical case, a fragment may be discarded if it is farther away than the pixel that is already at that location (according to the depth buffer). If it is nearer than the existing pixel, it may replace what is already there, or, if alpha blending is in use, the pixel's color may be replaced with a mixture of the fragment's color and the pixel's existing color, as in the case of drawing a translucent object.

In general, a fragment can be thought of as the data needed to shade the pixel, plus the data needed to test whether the fragment survives to become a pixel (depth, alpha, stencil, scissor, window ID, etc.)

From wikipedia

\$\endgroup\$
0
7
\$\begingroup\$

A fragment is simply the data that is given from the contribution of the 3 vertices around the pixel you're shading.

In the Vertex Shader, you output some data per vertex (color, texture coords, ...). Then in the Fragment Shader, for each pixel you get a weighted average of those values, that you then use to build the final color(s) of the pixel.

\$\endgroup\$
1
  • \$\begingroup\$ The pixel and all the other data on invisible buffers as mentioned above. \$\endgroup\$
    – Weaver
    Mar 24, 2016 at 22:34
5
\$\begingroup\$

What is a fragment in a fragment shader

The only precise thing that can be said is defining the inputs and outputs of the fragment shader.

Since that has been done / is too broad, I will focus on the intuitive visual side of it.

A fragment is the raster output

The OpenGL wiki says:

A Fragment is a collection of values produced by the Rasterizer.

Which makes sense since the fragment shader comes after the rasterizer which produces its input.

And this is the visual representation of the rasterizer output:

Image source.

The input of the rasterizer is a 3D triangle and a point of view, and the output, is a set of pixels which might appear on the screen (if they don't get blocked by others).

Each of those those little squares (which are encoded as a single plixel), is a piece of the triangle, and fragment is just another name for piece.

More information at: https://stackoverflow.com/questions/17789575/what-are-shaders-in-opengl-and-what-do-we-need-them-for/36211337#36211337

\$\endgroup\$
2
  • \$\begingroup\$ Downvoters, please explain so I can learn and improve content ;-) \$\endgroup\$ Mar 24, 2016 at 21:57
  • \$\begingroup\$ The information in the top answer mixed with your visual explinations would be best. \$\endgroup\$
    – Weaver
    Mar 24, 2016 at 22:36
3
\$\begingroup\$

In the same way a vertex shader acts on vertices, a fragment (or pixel) shader will just act on pixels.

\$\endgroup\$
2
  • \$\begingroup\$ Sorry, but this is not correct without mentioning word "rastarisation" and interpolations. \$\endgroup\$
    – Notabene
    Feb 24, 2011 at 20:13
  • 1
    \$\begingroup\$ Even worse it says pixels - which are distinct from fragments, especially when multisampling AA render targets are involved, coverage to alpha, or any of number of other situations - I would down vote if I had the rep \$\endgroup\$
    – jheriko
    Jul 16, 2011 at 2:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .