size of a pixel
Hello all,
I have one strange question about size of a pixel use in shader to cacl offset of filter, or half pixel offset (D3D).
At firts I was thinking like everybody that size of a pixel was:
float2 invScreenDim(1 / RenderTargetSizeX, 1 / RenderTargetSizeY)
Then I can use this value in my shader for case like the following:
(from http://diaryofagraphicsprogrammer.blogspot.com/2008/09/calculating-screen-space-texture.html)
Float4 vPos = float4(0.5 * (float2(p.x + p.w, p.w – p.y) + p.w * invScreenDim.xy), pos.zw)
Here "p.w * invScreenDim.xy * 0.5" allow to apply the half pixel offset.
However, when looking at some 3D Engine, I found few which define size of
a pixel as (2 / RenderTargetSizeX, 2 / RenderTargetSizeY).
For sample in Nebula3 engine (http://flohofwoe.blogspot.com/):
frameposteffect.cc
(...)
// compute a half pixel offset
SizeT w = this->renderTarget->GetWidth();
SizeT h = this->renderTarget->GetHeight();
Math::float4 pixelSize(2.0f / float(w), 2.0f / float(h), 0.0f, 0.0f);
Math::float4 halfPixelSize = pixelSize * 0.5f;
(...)
and in a shader:
float3 sampleOffsetWeights[13] = {
{ -1.5, 0.5, 0.024882 },
{ -0.5, -0.5, 0.067638 },
(...)
float4 psMain(const vsInOut psIn) : COLOR
{
float4 sample = float4(0.0, 0.0, 0.0, 0.0);
int i;
for (i = 0; i < 13; i++)
{
sample += sampleOffsetWeights.z * tex2D(SourceBufferSampler, psIn.uv0 + sampleOffsetWeights.xy * pixelSize.xy);
}
return sample;
}
So, is someone can explain me why a pixel is (2 / RenderTargetSize), or in which case it must be 2 ? Cause Nebula3 is not the only case where I see this.
Thanks for any explanation.
Seb
Screen coordinates range from -1 to 1, not 0 to 1, meaning the width of the screen in device-independent units is 2, not 1.
After some though, I still be disagree with something.
For the following code:
Float4 vPos = float4(0.5 * (float2(p.x + p.w, p.w – p.y) + p.w * invScreenDim.xy), pos.zw)
I am OK to set pixel size as 2 / RenderTarget.
In fact we are in screen coordinate [-1..1] transform to [0..w] then we divide by W in pixel shader to get [0..1] which allow to get texture coordinate.
So the value of invScreenDim.xy on W. Engel blog are wrong and on Gamedev thread too (http://www.gamedev.net/community/forums/topic.asp?topic_id=506573) ?
but for the nebula3 case, where we just sample in a texture without conversion between screen coordinate and texture coordinate (which have same size as a screen) which is [0..1], I still don't understand. If we have a texture of 1280x720, then 1280 x 2 / RenderTargetSizeX => 2
I think I miss something.
Any help.
Thanks
For the following code:
Float4 vPos = float4(0.5 * (float2(p.x + p.w, p.w – p.y) + p.w * invScreenDim.xy), pos.zw)
I am OK to set pixel size as 2 / RenderTarget.
In fact we are in screen coordinate [-1..1] transform to [0..w] then we divide by W in pixel shader to get [0..1] which allow to get texture coordinate.
So the value of invScreenDim.xy on W. Engel blog are wrong and on Gamedev thread too (http://www.gamedev.net/community/forums/topic.asp?topic_id=506573) ?
but for the nebula3 case, where we just sample in a texture without conversion between screen coordinate and texture coordinate (which have same size as a screen) which is [0..1], I still don't understand. If we have a texture of 1280x720, then 1280 x 2 / RenderTargetSizeX => 2
I think I miss something.
Any help.
Thanks
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement