HLSL, Program pixel shader with different Texture2D downscaling algorithms
- by Kaminari
I'm trying to port some image interpolation algorithms into HLSL code, for now i got:
float2 texSize;
float scale;
int method;
sampler TextureSampler : register(s0);
float4 PixelShader(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
float2 newTexSize = texSize * scale;
float4 tex2;
if(texCoord[0] * texSize[0] > newTexSize[0] ||
texCoord[1] * texSize[1] > newTexSize[1])
{
tex2 = float4( 0, 0, 0, 0 );
} else {
if (method == 0) {
tex2 = tex2D(TextureSampler, float2(texCoord[0]/scale, texCoord[1]/scale));
} else {
float2 step = float2(1/texSize[0], 1/texSize[1]);
float4 px1 = tex2D(TextureSampler, float2(texCoord[0]/scale-step[0], texCoord[1]/scale-step[1]));
float4 px2 = tex2D(TextureSampler, float2(texCoord[0]/scale , texCoord[1]/scale-step[1]));
float4 px3 = tex2D(TextureSampler, float2(texCoord[0]/scale+step[0], texCoord[1]/scale-step[1]));
float4 px4 = tex2D(TextureSampler, float2(texCoord[0]/scale-step[0], texCoord[1]/scale ));
float4 px5 = tex2D(TextureSampler, float2(texCoord[0]/scale+step[0], texCoord[1]/scale ));
float4 px6 = tex2D(TextureSampler, float2(texCoord[0]/scale-step[0], texCoord[1]/scale+step[1]));
float4 px7 = tex2D(TextureSampler, float2(texCoord[0]/scale , texCoord[1]/scale+step[1]));
float4 px8 = tex2D(TextureSampler, float2(texCoord[0]/scale+step[0], texCoord[1]/scale+step[1]));
tex2 = (px1+px2+px3+px4+px5+px6+px7+px8)/8;
tex2.a = 1;
}
}
return tex2;
}
technique Resample
{
pass Pass1
{
PixelShader = compile ps_2_0 PixelShader();
}
}
The problem is that programming pixel shader requires different approach because we don't have the control of current position, only the 'inner' part of actual loop through pixels.
I've been googling for about whole day and found none open source library with scaling algoriths used in loop. Is there such library from wich i could port some methods?
I found http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx but I really don't understand His approach to the problem, and porting it will be a pain in the ...
Wikipedia tells a matematic approach. So my question is: Where can I find easy-to-port graphic open source library wich includes simple scaling algorithms? Of course if such library even exists :)