简体   繁体   中英

XNA RenderTarget half pixel offset

I have come across this great explanation regarding DX9 and accordingly, XNA's catch regarding the pixel offsetting when attempting to render to a target texture for the purpose of using Post Process pixel shader effects:

Since reading that article I did notice that the image produced by my Engine code is somewhat blurry, not 100% crisp, when using post process effects. I do have the code for skipping post process effects, so it can be ran on slower machines.

I have tried to implement the solution presented in the article and I noticed it did help to some extent, however the image is still not quit crisp, that is, I do not believe I am achieving 1:1 pixel mapping in my rendering code.

I am using the following Shader code in my post process shader effect:

PixelShaderStruct vertShader(VertexShaderStruct input)
{
    PixelShaderStruct output;
    input.pos.X -=  halfPixel.X;
    input.pos.Y -=  halfPixel.Y;
    output.position = float4(input.pos, 1);
    output.texCoord = input.texCoord;

    return output;
}

I am forwarding the following variable as halfPixel variable:

bloom.Parameters["halfPixel"].SetValue(new Vector2(0.5f / (float)Game1.maxX, 0.5f / (float)Game1.maxY));

MaxX and MaxY are correct integer numbers for the resolution currently used, at all times.

I am achieving following results with this code.

Without post process shaders: http://i690.photobucket.com/albums/vv263/Eudaimonium/3D%20Renderings/test2_zps1fb27bcf.png As crisp as it gets.

However, using the post process shaders (and consequently, using more than several render-to-target passes): http://i690.photobucket.com/albums/vv263/Eudaimonium/3D%20Renderings/test1_zps64a20018.png

As you can see by the white bounding box outlines, it appears the lower-left border pixels of edges are somewhat crisp, but blurred in up-right direction resulting in incorrect result.

I have made sure that MSAA is turned off at starting of the game and every resolution change.

I can't figure out what's wrong.

I think you missed the relevant paragraph in the article you linked:

Note, that when I say “subtract a half-pixel from position,” what I mean is “move the position of the quad one half-pixel towards the upper-left. To do this, you actually add (-1/width, +1/height). The reason for the signs (-, +) is that you're moving left and up. In post-projection space, -x is left, and +y is up. The reason that it's 1/width instead of 0.5/width is because, in clip space, the coordinates range from -1 to 1 (width 2), and not from 0 to 1 (width 1) like they do in textures, so you need to double the movement to account for that.

So change

input.pos.X -= halfPixel.X;
input.pos.Y -= halfPixel.Y;

to

input.pos.X -= halfPixel.X;
input.pos.Y += halfPixel.Y;

and use (1f / width, 1f / height) as your halfPixel value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM