How to use a Pixel Shader CMT3317
Pixel shaders There is NO requirement to use a pixel shader for the coursework though you can if you want to You should get a general idea about shaders because examples you come across will use them These slides introduce you to how to use a pixel shader
Programming the Graphics Processing Unit (GPU) Massive increase in graphics performance in recent years Comes from increasing capabilities of graphics hardware And the ability to write programs that run on the GPU Baseline of XNA for 3D is that you have to use either a built in effect or write your own pixel and vertex shaders – little programs that run on the GPU written in the language HLSL
High Level Shading Language HLSL Is a C-like little language for writing pixel and vertex shaders (though you can write in assembly language instead) A pixel shader is applied to each pixel A vertex shader is applied to each vertex In directx 10 we also have geometry shaders that can even do some physics on the GPU – but we are stuck at direct 9c with the xbox360 For 2D graphics we can use pixel shaders to apply effects to our graphics e.g. blurring or making the image wavy.
Pixel shaders Because pixel shaders are virtually always used in conjunction with vertex shaders it is hard to find information just about pixel shaders But you can find lots of example shaders out there e.g. from Nvidia erlibrary/webpages/shader_library.html
In this example I got post_sepia.fx from the NVidia site and commented out the vertex shader part – probably this will not work with most examples
Pixel shaders Often referred to as post processing i.e. you generate the image first and then apply some effect to it You can modify every single pixel of an image The general way we will do this is –Draw to a texture in memory rather than to the screen –Then draw the texture to the screen modifying it in some way through a pixel shader program Actually we have a lot of steps to implement this simple concept first we will consider how to use and effect (shader) file in XNA
Shader terminology In HLSL a shader file is referred to as an effect An effect can have a number of techniques in it – these are different functions to support different graphics card capabilities A technique can have a number of passes. Usually there is only one pass but the output of one pass may be fed into or combined with the next pass. –e.g. pass 1 might blur the image, pass 2 could convert it to greyscale while a third pass could do an edge detection
Effects
XNA code to use an effect You need to add the effect file to your project – the content pipeline automatically compiles it for you In XNA you have to define an Effect and an EffectPass You need to load the effect file Then in draw –you begin the effect –You assign the EffectPass to the pass you want –You begin the pass –You draw your graphics –You then end the pass and end the effect
define an Effect and an EffectPass as class variables Load the effect file in LoadGraphicsContent()
Begin, draw and end. As this is 2D a spriteBatch is used for drawing
XNA code to draw to a texture and then draw to screen through a shader program You have to define a 2d render target You have to initialise it You draw to it You send it to the back buffer You clear it You use a spriteBatch to draw it You do the steps described previously for the effect to ensure that it is processed by your effect (shader program)
Define and initialise a renderTarget
Draw function becomes:
Use a pixel shader You are now ready to use any pixel shader that does not need to have variables passed to it In the next slides some pixel shaders are developed