Sign in to follow this  
rohith291991

problem with linear blur implementation!!

Recommended Posts

im trying to implement a kind of "diagonal blur" of the rendered scene.. What im doing is an initial pass p0 where i output the entire frame into a texture.. then i start another pass (after setting render target back to original) and i use a loop with a texture sampler to blur the image.. however all the pixels are set to black..(the first pass was intended to output a depth map which it did quite well) here is the HLSL code:
float4x4 xViewProjection;
float xMaxView=60.0f;
texture firstFrame;
struct VertexToPixel
{
 float4 Position :POSITION;
 float4 Position3D :TEXCOORD0;

};

struct PixelToFrame
{
 float4 Color :COLOR0;

};


VertexToPixel VS1(float4 inPos :POSITION)
{
   VertexToPixel Output=(VertexToPixel)0;
  Output.Position=mul(inPos,xViewProjection);
  Output.Position3D=Output.Position;
  return Output; 

}


PixelToFrame PS1(VertexToPixel inVS)
{
    PixelToFrame Output=(PixelToFrame)0;
    Output.Color=1.0f*sin(xTime);
 return Output;
}

sampler MySampler=sampler_state{texture=firstFrame;magfilter=LINEAR;minfilter=LINEAR;mipfilter=LINEAR;AddressU=clamp;AddressV=clamp;};
struct VToP2
{
 float2 TexPosition :TEXCOORD0;
 float4 Position :POSITION;
 
};

VToP2 VS2(float4 inPos :POSITION) 
{VToP2 Output=(VToP2)0;
 Output.TexPosition=mul(inPos,xViewProjection);
 Output.TexPosition.x=Output.TexPosition.x/2+0.5f;
 Output.TexPosition.y=Output.TexPosition.y/2+0.5f; 
Output.Position=mul(inPos,xViewProjection);
 return Output;
}

float4 PS2( VToP2 inPS2) :COLOR0
{  
   float4 Color=0;
 for(int i=0;i<10;i++)
{
  Color+=tex2D(MySampler,inPS2.TexPosition+0.001*i);
}
Color=Color/10;
return Color;
}
technique  Simplest
{
pass p0
{
VertexShader=compile vs_2_0 VS1();
PixelShader=compile ps_2_0 PS1();
}
pass p1
{
VertexShader=compile vs_2_0 VS2();
PixelShader=compile ps_2_0 PS2();

}
}

here is the related c++ code:
LPDIRECT3DTEXTURE9 RenderTexture;
LPDIRECT3DSURFACE9 pSurface;
LPDIRECT3DSURFACE9 oldSurface;

D3DXCreateTexture(d3ddev,640,480,0,D3DUSAGE_RENDERTARGET,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&RenderTexture);
	RenderTexture->GetSurfaceLevel(0,&pSurface);
    d3ddev->GetRenderTarget(0,&oldSurface);

d3ddev->BeginScene();
     //Rendering Scene
	d3ddev->SetVertexDeclaration(vdecl);
	d3ddev->SetStreamSource(0,buffer,0,sizeof(VERTEX));
	effect->SetMatrix("xViewProjection",&(matView*matProjection));
effect->SetFloatArray("xLightPos",FLOAT*)&D3DXVECTOR4(4.0f,5.0f,10.0f,0.0f),4);
	D3DXHANDLE hTechnique;
	hTechnique=effect->GetTechniqueByName("Simplest");
	effect->SetTechnique(hTechnique);
	UINT passes;
	effect->Begin(&passes,0);
	for(UINT i=0;i<passes;i++)
	{
	  effect->BeginPass(i);
	  //Render here....
	  if(i==0)
	  {
		  d3ddev->SetRenderState(D3DRS_ZENABLE,FALSE); 
		  d3ddev->SetRenderTarget(0,pSurface);
	  }
	  else
	  {   effect->SetTexture("firstFrame",texture);
		  d3ddev->SetRenderTarget(0,oldSurface);
	  }
	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2); 

	 // Teapot->DrawSubset(0);
	  effect->EndPass();

	}
	effect->End();


all i get is a black cube!! and i tried this by setting the texture "firstFrame" to a texture loaded from a file but got compeletely unexpected results.. in short please tell me what im doing wrong!! (btw im new to this sampler stuff if someone could explain how they work it would be great!! for example how do you access any pixel of your choice in a texture using a sampler???) (BTW my actual backbuffer size is 1024x768 pixels and i tried to create a new texture as a render target with this resolution but when i ran the program it said something like depthstencil buffer must be at least as big as the render target or something like that... which is why i changed the res to 640x480.. what could be the problem??? do i have to specify a stencil buffer again?? if so how to do so??

Share this post


Link to post
Share on other sites
There are a bunch of post-processing shaders and samples available so it may be worth cross-referencing your code against those. In my experience the only real change between them is your computation of tex2D() offsets - the general mechanism for multi-pass/post-process rendering is the same throughout.

Both the PostProcess and HDRDemo/HDRPipeline sample in the DXSDK will demonstrate the technique.

I can't see anything immediately obvious with your code, but static analysis of these things tends to be quite futile. Fire up your shader in something like FXComposer or better yet, do a frame capture with PIX and debug a pixel - see what actually happened. These days its pretty much possible to debug your HLSL in the same way as your regular code [cool]


hth
Jack

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