8

I'm relatively new to graphics programming, and I've just been reading some books and have been scanning through tutorials, so please pardon me if this seems a silly question.

I've got the basics of directx11 up and running, and now i'm looking to have some fun. so naturally I've been reading heavily into the shader pipeline, and i'm already fascinated. The idea of writing a simple, minuscule piece of code that has to be efficient enough to run maybe tens of thousands of times every 60th of a second without wasting resources has me in a hurry to grasp the concept before continuing on and possibly making a mess of things. What i'm having trouble with is grasping what the pixel shader is actually doing.

Vertex shaders are simple to understand, you organize the vertices of an object in uniform data structures that relate information about it, like position and texture coordinates, and then pass each vertex into the shader to be converted from 3d to 2d by way of trasformation matrices. As long as i understand it, i can work out how to code it.

But i don't get pixel shaders. What i do get is that the output of the vertex shader is the input of the pixel shader. So wouldn't that just be handing the pixel shader the 2d coordinates of the polygon's vertices? What i've come to understand is that the pixel shader receives individual pixels and performs calculations on them to determine things like color and lighting. But if that's true, then which pixels? the whole screen or just the pixels that lie within the transformed 2d polygon?

or have i misunderstood something entirely?

2 Answers 2

10

Vertex shaders are simple to understand, you organize the vertices of an object in uniform data structures that relate information about it, like position and texture coordinates, and then pass each vertex into the shader to be converted from 3d to 2d by way of trasformation matrices.

After this, primitives (triangles or multiples of triangles) are generated and clipped (in Direct3D 11, it is actually a little more complicated thanks to transform feedback, geometry shaders, tesselation, you name it... but whatever it is, in the end you have triangles).

Now, fragments are "generated", i.e. a single triangle is divided into little cells with a regular grid, the output attributes of the vertex shader are interpolated according to each grid cell's relative position to the three vertices, and a "task" is set up for each little grid cell. Each of these cells is a "fragment" (if multisampling is used, several fragments may be present for one pixel1).

Finally, a little program is executed over all these "tasks", this is the pixel shader (or fragment shader).

It takes the interpolated vertex attributes, and optionally reads uniform values or textures, and produces one output (it can optionally produce several outputs, too). This output of the pixel shader refers to one fragment, and is then either discarded (for example due to depth test) or blended with the frame buffer. Usually, many instances of the same pixel shader run in parallel at the same time. This is because it is more silicon efficient and power efficient to have a GPU run like this. One pixel shader does not know about any of the others running at the same time.
Pixel shaders commonly run in a group (also called "warp" or "wavefront"), and all pixel shaders within one group execute the exact same instruction at the same time (on different data). Again, this allows to build more powerful chips that user less energy, and cheaper.




1Note that in this case, the fragment shader still only runs once for every "cell". Multisampling only decides whether or not it stores the calculated value in one of the higher resolution extra "slots" (subsamples) according to the (higher resolution) depth test. For most pixels on the screen, all subsamples are the same. However, on edges, only some subsamples will be filled by close-up geometry whereas some will keep their value from further away "background" geometry. When the multisampled image is resolved (that is, converted to a "normal" image), the graphics card generates a "mix" (in the easiest case, simply the arithmetic mean) of these subsamples, which results in everything except edges coming out the same as usual, and edges being "smoothed".

6
  • okay, i think i understand now. Because i'm a visual learner though, i made a diagram of what i think you've described, so please tell me if i'm missing anything. postimage.org/image/9oy2nroqr Jun 26, 2012 at 5:55
  • 1
    One mistake: post-vertexshader coordinates are not pixel coordinates, but normalized. For Direct3D this means they're between 0.0 and 1.0 (-1.0 and 1.0 in OpenGL). This is not very intuitive at first, because the screen is usually not square, but it "just works" (no worries, nothing special for you to do!) and it's more efficient for the hardware to do calculations (e.g. clipping) in this space. The rest looks good (geom shader, tesselation, and clipping are referred to as "stuff that happens in between").
    – Damon
    Jun 26, 2012 at 10:11
  • ah gotcha, i prefer normalized coordinates actually, as everything would be on the same scale no matter your chosen screen resolution, (opengl seems a bit odd though, -1 to 1). So the geometry shader and tesselation stages happen in between? i would've figured they come first as the new vertices could then be fed into the vertex shader and converted to screen coords. are the new vertices that are created automatically turned 2d? Jun 26, 2012 at 10:28
  • woops, just realized i'm quickly turning this one question into a small forum of questions and i've already taken enough of your time.i think i've got a pretty good grasp on how pixel shaders operate so i should be good. Thanks for the help! Gonna go have some fun now :P Jun 26, 2012 at 10:42
  • 2
    See here (or here for the OpenGL 4.x equivalent, nicer image, slightly different names but same thing). It makes sense to run tesselation after the vertex shader for several practical and efficiency reasons (how would you for example know how much to tesselate without knowing the on-screen area covered by a patch?). You don't simply tesselate every triple of vertices into a thousand triangles. You do it according to how much screen area they actually cover.
    – Damon
    Jun 26, 2012 at 10:44
2

Your understanding of pixel shaders is correct in that it "receives individual pixels and performs calculations on them to determine things like color and lighting."

The pixels the shader receives are the individual ones calculated during the rasterization of the transformed 2d polygon (the triangle to be specific). So whereas the vertex shader processes the 3 points of the triangle, the pixel shader processes the pixels, one at a time, that "fill in" the triangle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.