CREATING AND USING PIXEL AND VERTEX SHADERS

In order to create a shader, you need to have the shader source in memory. You can do this in any number of ways. The easiest is to leave the shader in an ASCII file and to simply read the file and generate the binary shader tokens using an API function. If you are working on a commercial product, you could assemble the shader into a binary format and save the format either in a file or as part of a larger data set. Be aware that the tokens are just that, tokens, and that if someone was curious, he or she could easily parse the tokens back into assembly. Let's assume that you've got an ASCII shader program in memory, either read in from a file or typed in directly into a buffer. The steps you need to do to actually use it in your program are as follows:

  1. Create the shader and read it into memory.

  2. Translate (assemble) the shader into binary tokens.

  3. Create a shader from the tokens and get a handle (DX8) or interface (DX9).

  4. Select the shader.

  5. (Optional) Set any constant registers for that shader.

You'll probably create all of your shader token arrays at load time or precompile them and store them as data with your program. At run time, you'd convert the token array into a shader handle. When you need to render a new object with a particular shader, you'd load the vertex and/or pixel shader from its handle and any constant registers that need to be updated, plus any textures associated with that object before you make your render call. Shaders should be treated just as any other state variable. In terms of expense, changing shaders comes after setting textures, so you should render your objects by sorting first by textures, then by shaders.



Real-Time Shader Programming(c) Covering Directx 9. 0
Real-Time Shader Programming (The Morgan Kaufmann Series in Computer Graphics)
ISBN: 1558608532
EAN: 2147483647
Year: 2005
Pages: 104
Authors: Ron Fosner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net