Postprocessing Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology2 Bloom effect - before http://creators.xna.com/en-us/sample/bloom3 Bloom effect - after http://creators.xna.com/en-us/sample/bloomMotion blur in Valve’s Portal - roll 4 http://www.valvesoftware.com/publications/2008/GDC2008_PostProcessingInTheOrangeBox.pdfMotion blur in Valve’s Portal - falling 5 http://www.valvesoftware.com/publications/2008/GDC2008_PostProcessingInTheOrangeBox.pdf6 Giant warning The code in these slides has not been tested. There may be bugs and/or misconceptions.7 Typical XNA setup code GraphicsDeviceManager graphics =! new GraphicsDeviceManager(this);!ContentManager content = ! new ContentManager(Services);!GraphicsDevice device =! graphics.GraphicsDevice;!Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 20088 Setup for postprocessing SpriteBatch mySpriteBatch;!RenderTarget2D myRenderTarget;!Texture2D beforeProc;!Effect ppEffect;!ppEffect = ! content.Load<Effect>(@”Content\Effects\CoolEffect”);!Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 20089 Creating the rendertarget myRenderTarget = ! new RenderTarget2D(device,! device.Viewport.Width,! device.Viewport.Height,! 1, // number of mipmap levels! device.DisplayMode.Format ! // a SurfaceFormat)!Vector2 offset = new Vector2(0,1 / device.Viewport.Height);!Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 200810 Creating the rendertarget (advanced) myRenderTarget = new RenderTarget2D(device,!device.Viewport.Width,!device.Viewport.Height,!1, ! ! ! ! ! // number of mipmap levels!device.DisplayMode.Format,! // a SurfaceFormat!sevice.PresentationParameters.MultiSampleType,!sevice.PresentationParameters.MultiSampleQuality)!Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 200811 SurfaceFormat enumeration Screenshot from msdn2.microsoft.com/en-us/library/microsoft.xna.framework.graphics.surfaceformat.aspx12 Direct3D/XNA SurfaceFormat conversions Screenshot from msdn2.microsoft.com/en-us/library/microsoft.xna.framework.graphics.surfaceformat.aspx13 Rendering the preprocessed scene device.SetRenderTarget(0,myRenderTarget);!// On Xbox 360, first argument must be set to zero!// since you only can set one Render Target on the 360!// PUT CODE TO DRAW STUFF HERE!// in XNA 1.0, needed to ResolveRenderTarget on Xbox 360!// to copy eDRAM contents to main RAM, but not on Windows!// in XNA 2.0, don’t need this line anymore on either platform!device.ResolveRenderTarget(0);!beforeProc = myRenderTarget.GetTexture();!// Set render target to the usual backbuffer !// in XNA 2.0, resolving happens automatically here!device.SetRenderTarget(0, null);!Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 2008 and http://blogs.msdn.com/shawnhar/archive/2007/11/21/rendertarget-changes-in-xna-game-studio-2-0.aspx14 Setting up the postprocessing effect myEffect.CurrentTechnique = !!!effect.Techniques[“BlurEffect”];!myEffect.Parameters[“offset”].SetValue(offset);!Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 200815 Drawing the processed scene device.Clear(Color.Black);!myEffect.Begin();!mySpriteBatch.Begin(SpriteBlendMode.None, !SpriteSortMode.Immediate, SaveStateMode.None);!EffectPass pass = effect.CurrentTechnique.Passes[0]!pass.Begin();!mySpriteBatch.Draw(beforeProc, Vector2.Zero, ! Color.White);!pass.End(); !mySpriteBatch.End();!myEffect.End();!Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 2008 This can be problematic – we’ll come back to this16 Just need a pixel shader Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 2008 // CoolEffect.fx!sampler textureSampler;!float2 offset;!float4 threewayBlurPS(texCoord : TEXCOORD0) : COLOR0 !{! float4 color =! (tex2D(textureSampler,texCoord)! + tex2D(textureSampler,texCoord + offset)! + tex2D(textureSampler,texCoord - offset)) / 3;! return color;!}!technique BlurEffect {!!pass P0 {!!!PixelShader = compile ps_2_0 threewayBlurPS();!!}!}!17 Behind the scenes Based on discussion on p. 277-281 of Chad Carter, “Microsoft XNA Unleashed,” 2008 sampler textureSampler!is sort of implicitly!sampler textureSampler : register(S0);!mySpriteBatch.Draw(beforeProc, Vector.Zero, ! Color.White);!was sort of doing this somewhere:!device.Textures[0] = beforeProc;!18 Multiple textures From msdn2.microsoft.com/en-us/library/microsoft.xna.framework.graphics.graphicsdevice.textures.aspx graphics.GraphicsDevice.Textures[0] = firstTexture;!graphics.GraphicsDevice.Textures[1] = secondTexture;!sampler firstSampler : register(s0);!sampler secondSampler : register(s1);!• In your C# code: • In your shader code:19 Rendertarget semantics on Windows • If not multisampling, a single area of video memory can be used for rendering or as a texture – In XNA 1.0: • Contents of rendertarget not lost in XNA 1.0 on Windows • Resolve is a no-op – Rendertarget cleared in XNA 2.0 to emulate Xbox 360 behavior • If multisampling, need large area to render into and small area to copy into – Resolve copies, downsampling as it goes – Contents of both buffers not lost (in XNA 1.0) From Shawn Hargreaves, “Rendertarget changes in XNA Game Studio 2.0,” http://blogs.msdn.com/shawnhar/archive/2007/11/21/ rendertarget-changes-in-xna-game-studio-2-0.aspx20 Rendertarget semantics on Xbox 360 • Xenos GPU renders into only one physical rendertarget: 10 MB eDRAM – Cannot texture from eDRAM – Cannot render into main 512M RAM • Must “resolve” to copy rendering in eDRAM back to main memory – Hardware designed to make this fast – Rendertarget in eDRAM is cleared From Shawn Hargreaves, “XNA rendertarget semantics,” blogs.msdn.com/shawnhar/archive/2007/02/04/xna-rendertarget-semantics.aspxWhat about SaveStateMode.None? • May mess up rendering of 3-D objects • SpriteBatch will change renderstates to things inappropriate for 3-D drawing 21 mySpriteBatch.Begin(SpriteBlendMode.None, !SpriteSortMode.Immediate, SaveStateMode.None);!From Shawn Hargreaves, “SpriteBatch and renderstates,”
View Full Document