Thread: Pixel Shaders.

  1. #1
    Join Date
    Aug 2001

    Pixel Shaders.

    What exactly is a Pixel Shader? I've read that
    they're little programs that run over each pixel,
    but I dont' quite understand this. What do they
    do? What are they used for? How do they work? etc.

    Also, can these only be used through DirectX 8?
    Everything I've read about Pixel/Vertex Shaders
    is mentioned in articles talking about DirectX 8.
    What about OpenGL poeple? Are there special
    extension-things for them?

    Anyway, since I'm already here... What's a Vertex
    Shader? How are Vertex Shaders different from
    Pixel Shaders?

    I don't know much about DirectX or OpenGL.

    Staying away from General.

  2. #2
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Cairo, Egypt

    To get the best answers goto :
    1 - Microsoft's online SDK and see the section about the rendering pipleline, this will give you all what you need about pixel & vertex shaders.
    2 - Philipe taylor's Driving DirectX column on Microsoft's DirectX site
    3 - GDnet's Hardcore game programming column : Wofgang engel's writing an explosive series on vertex & pixel shaders.
    Prepare for a couple of sleepless nights, though.

    Anyway, the Direct3D8 fixed-function processing pipeline does things like the following for input vertices :

    1) World transform : Transform vertex from object space to world space
    2) Lighting & fogging ( as far as I recall, there place is here )
    3) View transform : Transform vertex from world space to camera ( view ) space
    4) Projection transform : Transform vertex from camera space to clipping space
    5) Clipping

    Fixed-Function means that these steps are taken in that fixed order, and that you have no control other than setting the parameters for such operations ( setting transforms, defining clipping planes,...etc )

    What vertex shaders offer you is replacing this fixed function pipeline, i.e. instead of going through the above steps, each vertex is input to your vertex shader ( which is written in a vertex assembly language, much similar to assembly ).

    Using a vertex shader, you're given a vertex in object space. There are constant registers that can be set to any value from the application ( using Direct3D API function from the app, before calling the vertex shader ).
    The vertex shader is supposed to do any thing it likes with the vertex, then emit a transformed vertex in clipping space.

    One can write a vertex shader that does the job of the fixed pipeline as follows :
    Set some of the constant registers to :
    world matrix, view matrix, projection matrix,ambient light value

    Then, you'd do for the input vertex ( using vertex assembly ofcourse, I'm presenting pseudo code here ):
    Multiply it by the world matrix
    Multiply the color component by the ambient color value
    Multiply it by the view matrix
    Multiply it by the projection matrix

    That's it. One can diverse various effects using vertex shaders ( although there's a current limitation on the number of instructions. But I don't recall how many instructions are allowed )
    Note that vertex shaders operate on single vertices, that means you can't do any per-polygon operations, you can't do culling or any such voodoo.

    There's a sample demonstrating using vertex shaders for shearing a cube, written by Slava Akhmechet ( )
    I don't recall his web site, so you'd have to either search on google with the keywords ( slava akhmechet mesh deformation ) or just mail the guy ( Excuse me sir, what's your website? )

    About pixel shaders :
    When a vertex has been emitted in screenspace, and the polygon containing it is ready to be rendered, pixel shaders come to play.
    Pixel shaders represent the rasterization part of the pipeline, using pixel shaders you can blend 2 textures together for a given pixel, make alpha effects, ...etc

    Philipe taylor has an amazing article about using pixel shaders for lighting.

    John carmack gave some feedback on them in a plan file on
    Webdog have closed recently, but last time they kept the plan files in searchable archives. So make a search with the keywords ( pixel vertex shaders John carmack ) and you'll find it

    As far as I recall, he admired vertex shaders, but didn't like pixel shaders and believed they were improperly implemented.

    About the GL thing : I believe GL has something called "Shader language" and it can do vertex shaders, don't worry ( Carmack's kicking stuff in his DOOMIII using GL, and he's using vertex & pixel shaders )

    Also, nVidia uses GL all the time for its shader samples.
    Muhammad Haggag

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing pixel colour
    By redruby147 in forum C Programming
    Replies: 11
    Last Post: 06-09-2009, 05:28 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Reading pixel color data from screen
    By JJFMJR in forum Windows Programming
    Replies: 8
    Last Post: 08-22-2008, 12:27 PM
  4. shaders: directx/opengl
    By Raven Arkadon in forum Game Programming
    Replies: 5
    Last Post: 08-24-2006, 09:19 AM
  5. 2 pixel shaders
    By Magos in forum Game Programming
    Replies: 8
    Last Post: 06-13-2005, 06:51 PM