What is a vertex shader? What is a pixel shader?

Started by
4 comments, last by jollyjeffers 18 years ago
- Primary question: * Can someone give an short and exact definition of vertex shader and pixel shader? - And some secondary questions in case the answer to the first one doesn't explain these things: * What are the differences? Which of them is used for what? Why use them? * Do they involve only color choices etc. or can they affect things such as locations of objects? If so, are they preferable over normal matrix operations to use for translating world coordinates etc.? * What are the basic primitive operations a vertex shader can do? What are the basic primitive operations a vertex shader can do? * Where does HLSL come into the picture (is it used both for vertex and pixel shaders?)? HLSL like other languages I assume must be standardized by someone, who? Can I find a full documentation about the language on their homepage?
Advertisement
(From Wiki):

Vertex Shader:

Vertex shaders are applied for each vertex and run on a programmable vertex processor. Vertex shaders define a method to compute vector space transformations and other linearizable computations.

Fragment (Pixel Shader):

Those kind of shaders are used to compute properties which, most of the time, are recognized as pixel colors.

Pixel shaders are applied for each pixel. They are run on a pixel processor, which usually features much more processing power than its vertex-oriented counterpart.

HLSL is DirectX language for writing shaders, you used to have to write shaders out in asm style code, which is harder and more prone to errors, HLSL gives programmers a C like language to write shaders which is easier, more readable and better to understand.
Quote:Original post by all_names_taken
- Primary question:
* Can someone give an short and exact definition of vertex shader and pixel shader?

- And some secondary questions in case the answer to the first one doesn't explain these things:
* What are the differences? Which of them is used for what? Why use them?
* Do they involve only color choices etc. or can they affect things such as locations of objects? If so, are they preferable over normal matrix operations to use for translating world coordinates etc.?
* What are the basic primitive operations a vertex shader can do? What are the basic primitive operations a vertex shader can do?
* Where does HLSL come into the picture (is it used both for vertex and pixel shaders?)? HLSL like other languages I assume must be standardized by someone, who? Can I find a full documentation about the language on their homepage?

A vertex or pixel shader is a small program that runs on the graphics card.
A vertex shader operates on every vertex getting passed in. The application specifies vertices in 3D world space, and the vertex shader transforms them into 2D clip space. Things you can do in a vertex shader include vertex blending for animated meshes. You move the vertex based on some values (blending weights in this case)
A pixel shader operates on every pixel that is drawn to the screen. It takes a bunch of information like the currently set textures for things, vertex colour, etc and outputs a single ARGB value. Things you can do in a pixel shader include things like procedural fire effects and ripples.

Using vertex and pixel shaders gives you more control over what you're drawing, although not all graphics hardware supports them. Shader language looks a bit like assembly. There's two versions of each, one for OpenGL and one for DirectX. HLSL is a bit like C, and is only availaible for DirectX. I don't know about the OpenGL shader language at all, but the DirectX one can be found in the DirectX SDK. The OpenGL shader language is standardized buy the ARB, and the DirectX one by Microsoft.

Vertex and pixel shaders can only reference the one thing they're operating on. A vertex shader can't create vertices (But the geometry shader comming in D3D10 can), and it can't reference any vertex except the one that's getting transformed by the shader, and similarly for the pixel shader.
Quote:Original post by Evil Steve
The application specifies vertices in 3D world space, and the vertex shader transforms them into 2D clip space.


That's not accurate. Input vertices don't have to be in 3D worldspace - IIRC they don't even need to have positional data attached, and if they do it /certainly/ doesn't have to be 3D or in worldspace; and the output is in 3D clipspace, not 2D.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Ok thanks, looks like it's worth learning about it after all, even though it seems difficult to learn...
Quote:Original post by all_names_taken
looks like it's worth learning about it after all
Going forwards it definitely is better to learn shaders rather than the fixed function. It might not be important right this second, but Direct3D 10 is completely shader-based - no more fixed function. If you start to get your head around shaders now you'll find it much easier to pick up D3D10 as/when necessary.

Quote:Original post by all_names_taken
even though it seems difficult to learn...
The initial hurdle might be higher, but there is no shortage of good tools, books and resources around. Once you've got the basics up-and-running the rest falls nicely into place. I'd also go as far as saying that, at the end of it, you'll have a much better understanding of how computer graphics works.

I'd highly recommend looking into "RenderMonkey" (ATI) or "FXComposer" (Nvidia) - they give you a nice sandbox to play with. You can start chucking HLSL expressions at it and you can quickly see the results (good or bad!).

Have fun!
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement