I am using XNA and have corrected my half pixel issue by offsetting my position coordinates using the following code:
position.x = position.x - g_ClipSpaceHalfPixel.x;
position.y = position.y + g_ClipSpaceHalfPixel.y;
position.z = position.z;
position.w = 1.0f;
g_ClipSpaceHalfPixel is calculated by dividing 1.0f by the target rendertarget width and height.
My downscaling pixel shader looks like so:
float4 SWScalingPS( in float2 texCoord : TEXCOORD0 ) : COLOR0
{
float4 fSampleSum = 0.0f;
for(int iSample = 0; iSample < 16; iSample++)
{
fSampleSum += tex2D(PointSampler, texCoord + g_SampleOffsets[iSample]);
}
// Divide the sum to complete the average
return (fSampleSum / 16);
}
My offset generation code looks like the below. Also, i pass in the source render target dimensions as the parameters.
private Vector2[] Get4x4Offsets( int iWidth, int iHeight )
{
// Use the multiplicative inverse instead of dividing each
// offset by the Width and Height.
float fU = 1.0f / iWidth;
float fV = 1.0f / iHeight;
Vector2[] offsets = new Vector2[16];
int index = 0;
for( int y = 0; y < 4; ++y )
{
for( int x = 0; x < 4; ++x )
{
offsets[index].X = ( x - 1.5f) * fU;
offsets[index].Y = ( y - 1.5f ) * fV;
++index;
}
}
return offsets;
}
Because my texels and pixels are aligned, I still need to apply a .5 offset to get the texel center, right? I figured that is the case and that is why I am using x -1.5f since I am using Point sampling and not Linear sampling. However, I have also tried it using x - 1.0f and that did not seem to make any difference at all.
Any help would be much appreciated. Thanks in advanced.