Sunday, September 8, 2019

antialiasing - How would I go about using FXAA in XNA?


This isn't so much a "give me teh codez" as it is a request for the "right" steps to go through.


I want to implement FXAA in XNA, but I'm a relative newb to the graphics side of game dev, so I'm not sure what the most efficient way to do this would be.


My first hunch is to render the scene into a texture, and then render that texture on a rectangle, with the FXAA code in a pixel shader.


However, I would actually be surprised if there was no more efficient way to do it.



Answer



You are almost right about it, but XNA has some built-in features to help you with all of that!


Render to Texture




My first hunch is to render the scene into a texture



Almost. You would start by rendering your scene into a RenderTarget2D (which actually inherits from Texture2D so it does qualify as rendering to a texture). Something like:


PresentationParameters pp = graphicsDevice.PresentationParameters;
RenderTarget2D renderTarget = new RenderTarget2D(graphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false, pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents);

graphicsDevice.SetRenderTarget(renderTarget);
RenderScene();
graphicsDevice.SetRenderTarget(null);


Apply Shader



and then render that texture on a rectangle, with the FXAA code in a pixel shader.



And for this you can just use the SpriteBatch class which already creates the quad for you. Load your shader into an Effect object and pass it to SpriteBatch.Begin(). Also, since the render target is already a texture, you can just use it directly! Something like:


Effect fxaa = content.Load("fxaa");

spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, fxaa);
spriteBatch.Draw(renderTarget, graphicsDevice.Viewport.Bounds, Color.White);

spriteBatch.End();

Making the Shader Work with SpriteBatch


As for writing a pixel shader that works with SpriteBatch, there's really not much to say (would be a different story if you were writing a vertex shader though). Check this sample to get you started. I'll copy one of the examples here:


sampler TextureSampler : register(s0);

float4 main(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
// Your pixel shader logic here
}


technique Desaturate
{
pass Pass1
{
PixelShader = compile ps_2_0 main();
}
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...