Sign in to follow this  
winston123

pixel shadow for pixel changing

Recommended Posts

winston123    122
hi, all: i am using D3D to load an image to display as a texture, is shown on the screen fine.. and this image is changing, so the display is changing too.. now i want to do some process to the pixels on the texture..for example, average the 8 neighbors as the new pixel there....... if i get the surface then lockrect it, get the pixel out and do the math, put it back,, it is quite slow... took a lot of CPUs.. now i look it into the ps (pixel shadow), i think maybe it can do without any cpu usage, actually it's GPU programming.. i just read some thing about the HLSL, but i don't know how to do the function above.. Is there anybody out there can kindly tell me , how to write this HLSL file (*.psh), so i can function as i said above (average the 8 neighbors).. thank you very much! winston..

Share this post


Link to post
Share on other sites
SilverCoder    325
hi,

here is my simple blur hlsl shader.
you need 2 rendertargets.

PS == pixel shader

first take your image as input and BlurHorizontal into the first render target ( rt0 )
then take rt0 as input and BlurVertical into the second render target ( rt1 )

then you have the blurred image in rt1.


struct PS_IN
{
float2 TexCoord: TEXCOORD0;
};

float4 BlurHorizontal_PS(
PS_IN IN,

uniform sampler2D scene: register( s0 )
) : COLOR0
{
float4 sceneColor = 0;

for( int i = -4; i <= 4; i++ )
{
sceneColor += tex2D( scene, float2( IN.TexCoord.x + ( i * 0.004f ), IN.TexCoord.y ) );
}
sceneColor = sceneColor / 9;

return sceneColor;
}

float4 BlurVertical_PS(
PS_IN IN,

uniform sampler2D scene: register( s0 )
) : COLOR0
{
float4 sceneColor = 0;

for( int i = -4; i <= 4; i++ )
{
sceneColor += tex2D( scene, float2( IN.TexCoord.x, IN.TexCoord.y + ( i * 0.004f ) ) );
}
sceneColor = sceneColor / 9;

return sceneColor;
}

Share this post


Link to post
Share on other sites
winston123    122
Thank you very much, Silvercoder..

i am just beginning to learn this PS thing..(sorry,even spelling it wrong..Pixel Shader, not Shadow..:( )

so, according to your method, actually, i can use this to do other graphic filtering.

suppose the filter matrix is
1 -2 1
-2 4 -2
1 -2 1

that means, i want the pixel m(x,y) is changed to

m(x,y) =

n(x-1,y-1) -2*n(x-1,y) + n(x-1,y+1)
-2*n(x,y-1) +4*n(x,y) + n(x,y+1)
+n(x+1,y-1) -2*n(x+1,y) + n(x+1,y+1);

n(x,y) is the pixel value of the original texture.

the way to do it, is also to do horizonal and vertical shader seperately as you shown?

is there other way to do it one time?

thank you very much!
winston.

Share this post


Link to post
Share on other sites
SilverCoder    325
hmmm... it should be possible to do it in one pass.
simply compare the output of a shader which do it in one pass and another one doing it in 2 passes.

one important thing is that your input texture is not the target texture you render to.
this will end in an incorrect output because you are writing to the texture while you are reading.

Share this post


Link to post
Share on other sites
MJP    19754
Hey there. First off, I'd like to officially welcome you to the world of shader programming. It's a nice place to be! :D

Second, I'd suggest you look into the PostProcess sample found in the SDK. Not only does it have a blur shader to produce the effect you're trying to implement, but it also has a bunch of other shaders that are typically applied to the entire screen as a post-process. It's a good starting point if you're looking to get into some of the typical post-processing shader effects like bloom, depth of field, edge-glowing, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this