Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
64 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Assemble Pixel Shader

After checking for pixel shader support, setting the proper textures with SetTexture() and after writing a pixel shader and setting the needed constant values, the pixel shader has to be assembled. This is needed, because Direct3D uses pixel shaders as byte-code.

Assembling the shader is helpful in finding bugs earlier in the development cycle.

At the time of this writing there are three different ways to compile a pixel shader:

Pre-Compiled Shaders

Use the pixel shader in a separate ASCII file for example test.psh and compile it with a pixel shader assembler (Microsoft Pixel Shader Assembler or NVASM) to produce a byte-code file which could be named test.pso. This way, not every person will be able to read and modify your source.

On the Fly Compiled Shaders

Write the pixel shader in a separate ASCII file or as a char string into your *.cpp file and compile it "on the fly" while the application starts up with the D3DXAssembleShader*() functions.

Shaders in Effect Files

Write the pixel shader source in an effect file and open this effect file when the application starts up. The pixel shader will be compiled by reading the effects file with D3DXCreateEffectFromFile(). It is also possible to pre-compile an effects file. This way most of the handling of pixel shaders is simplified and handled by the effects file functions.

The pre-compiled shader should be the preferred way of compiling shaders, since compilation happens during development of the code i.e. at the same time that the *.cpp files are compiled.

Creating a Pixel Shader

The CreatePixelShader() function is used to create and validate a pixel shader.

HRESULT CreatePixelShader(
    CONST DWORD* pFunction,
    DWORD* pHandle
);

This function takes the pointer to the pixel shader byte-code in pFunction and returns a handle to the pixel shader in pHandle. A typical piece of source might look like this:

TCHAR Shad[255];
LPD3DXBUFFER pCode = NULL;

DXUtil_FindMediaFile(Shad,_T("environment.psh"));
if(FAILED(D3DXAssembleShaderFromFile(Shad,0,NULL, &pCode,NULL) ) )
  return E_FAIL;
if( FAILED(m_pd3dDevice->CreatePixelShader((DWORD*)pCode->GetBufferPointer(), 
  &m_dwPixShader) ) )
return E_FAIL;

DXUtil_FindMediaFile() helps you finding the ASCII file. D3DXAssembleShaderFromFile() compiles it before CreatePixelShader() returns the handle in m_dwPixShader.

The pointer pCode to the ID3DXBuffer interface is used to store the object code and to return a pointer to this object code with GetBufferPointer().

Set Pixel Shader

You set a pixel shader for a specific amount of vertices by using the SetPixelShader() function before the DrawPrimitive*() call for these vertices:

m_pd3dDevice->SetPixelShader(m_dwPixShader);

The only parameter that has to be provided is the handle of the pixel shader created by CreatePixelShader(). The pixel shader is executed for every pixel that is covered by the vertices in the DrawPrimitve*() call.

Free Pixel Shader resources

While the game shuts down or before a device change, the resources taken by the pixel shader has to be freed. This must be done by calling DeletePixelShader() with the pixel shader handle like this:

if(m_dwPixShader)
  m_pd3dDevice->DeletePixelShader(m_dwPixShader);

Summarize

We have walked step by step through a vertex shader creation process. Let's summarize what was shown so far:

  • First, the pixel shader support of end-user hardware has to be checked with the caps bit PixelShaderVersion.
  • All textures have to be set with SetTexture(), like with the multitexturing unit.
  • The constant values for a pixel shader have to be set afterwards in the application code with the SetPixelShaderConstant() or in the pixel shader code with def.
  • There are texture address and arithmetic instructions. The scope of the texture address instruction in ps.1.1 - ps.1.3 enfolds loading texture data and changing of texture data. The scope of the texture address instructions in ps.1.4 enfolds only the loading of texture data. Changing of texture data is done in ps.1.4 and in ps.1.1 - ps.1.3 with the arithmetic instructions.
  • After a pixel shader is written into a source file, it has to be compiled.
  • To get a handle to a pixel shader it has to be created with CreatePixelShader().
  • To use a pixel shader it has to be set with a call to SetPixelShader().
  • At the end of a pixel shader driven application, the resources occupied by the pixel shader must be freed with a call to DeletePixelShader().

What happens next?

In the next part of this introduction named "Programming Pixel Shaders", we will start with a first basic pixel shader program and discuss a few basic algorithms and the way how to implement them with pixel shaders.

References

[Bendel] Steffen Bendel, "Hallo World - Font Smoothing with Pixel Shaders", ShaderX, Wordware Inc., pp. ?? - ??, 2002, ISBN 1-55622-041-3

[Beaudoin/Guardado] Philippe Beaudoin , Juan Guardado, "Non-integer Power Function on the Pixel Shader", ShaderX, Wordware Inc., pp. ?? - ??, 2002, ISBN 1-55622-041-3

[Brennan] Chris Brennan, "Per-Pixel Fresnel Term", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Brennan2] Chris Brennan, "Diffuse Cube Mapping", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Brennan3] Chris Brennan, "Accurate Environment Mapped Reflections and Refractions by Adjusting for Object Distance", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Card/Mitchell] Drew Card, Jason L. Mitchell, "Non-Photorealistic Rendering with Pixel and Vertex Shaders", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Calver] Dean Calver, Microsoft DirectX discussion forum, mail from Fri, 21 Sep 2001 10:00:55, http://discuss.microsoft.com/SCRIPTS/WA-MSD.EXE?A2=ind0109C&L=DIRECTXDEV&P=R25479

[Dietrich01] Sim Dietrich, "Guard Band Clipping in Direct3D", NVIDIA web-site

[Dietrich-DXDev] Sim Dietrich, Microsoft DirectX discussion forum, mail from Tue, 14 Aug 2001 20:36:02, http://discuss.microsoft.com/SCRIPTS/WA-MSD.EXE?A2=ind0108B&L=DIRECTXDEV&P=R13431

[Dominé01] Sébastien Dominé, "Alpha Test Tricks", NVIDIA web-site

[Hart] Evan Hart, "3D Textures and Pixel Shaders", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Isidoro/Brennan] John Isidoro and Chris Brennan, "Per-Pixel Strand Based Anisotropic Lighting", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Isidoro/Riguer] John Isidoro and Guennadi Riguer, "Texture Perturbation Effects", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Kraus] Martin Kraus, "TVolumetric Effects", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Mitchell] Jason L. Mitchell, "Image Processing with 1.4 Pixel Shaders in Direct3D", ShaderX, Wordware Inc. pp ?? - ??, 2002, ISBN 1-55622-041-3

[Moravánsky] Ádám Moravánszky, "Bump Mapped BRDF Rendering", ShaderX, Wordware Inc., pp ?? - ??, 2002, ISBN 1-55622-041-3

[Vlachos] Alex Vlachos, "Blending Textures for Terrain", ShaderX, Wordware Inc. pp ?? - ??, 2002, ISBN 1-55622-041-3

[Watt92] Alan Watt, Mark Watt, "Advanced Animation and Rendering Techniques", Addison Wesley, 1992, ISBN 1-55622-041-3

[Weiskopf] Daniel Weiskopf and Matthias Hopf, "Real-Time Simulation and Rendering of Particle Flows", ShaderX, Wordware Inc. pp ?? - ??, 2002, ISBN 1-55622-041-3

[Zecha] Oliver Zecha, "Procedural Textures", ShaderX, Wordware Inc. pp ?? - ??, 2002, ISBN 1-55622-041-3

Additional Resources

The best resource to accompany this article is the pixel shader assembler reference in the Direct3D 8.1 documentation at

DirectX Graphics->Reference->Pixel Shader Assembler Reference

A lot of valuable information on pixel shaders can be found at the web-sites of NVIDIA (developer.nvidia.com) and ATI (http://www.ati.com/developer/). I would like to name a few:

Author Article Published at
Philip Taylor Per-Pixel Lighting http://msdn.microsoft.com/directx
Miscancellous Meltdown Power Point Slids http://www.microsoft.com/mscorp/
corpevents/meltdown2001/presentations.asp
Sim Dietrich Intro To Pixel Shading in DX8 NVIDIA web-site (Courseware)
Sim Dietrich DX8 Pixel Shader Details NVIDIA web-site (Courseware)
Sim Dietrich DX8 Pixel Shaders NVIDIA web-site (Courseware)
Sim Dietrich AGDC Per-Pixel Shading NVIDIA web-site (Courseware)
Jason L. Mitchell 1.4 Pixel Shaders ATI web-site
Jason L. Mitchell Advanced Vertex and Pixel Shader Techniques ATI web-site
Alex Vlachos Preparing Sushi - How Hardware Guys Write a 3D Graphics Engine ATI web-site
Rich Direct3D 8.0 pipeline http://www.xmission.com/~legalize/book/
Dave Salvator 3D Pipeline Part I - III http://www.extremetech.com
and there <3D Graphics, Gaming & Audio> -> <Analysis & Tutorials>

Acknowledgements

I'd like to recognize a couple of individuals that were involved in proof-reading and improving this paper (in alphabetical order):

  • Jason L. Mitchell (ATI)
  • Ádám Moravánszky (Swiss Federal Institute of Technology)
  • Matthias Wloka (NVIDIA)

© 2000 - 2002 Wolfgang Engel, Frankenthal, Germany




Contents
  Introduction
  Pixel Shader Tools
  High Level View
  Instruction Modifiers
  Conclusion

  Printable version
  Discuss this article

The Series
  Fundamentals of Vertex Shaders
  Programming Vertex Shaders
  Fundamentals of Pixel Shaders
  Programming Pixel Shaders
  Diffuse & Specular Lighting with Pixel Shaders