   simple blur in directx and hlsl
  Hi! My first post :D:D OKI'm trying to write a simple blur effect in my little application. This is a code of pixelshader (it's for study purpose so I use a shader from Microsoft DiretcX SDK - PostProces)texture g_txSrcColor;sampler2D g_samSrcColor =sampler_state{    Texture = <g_txSrcColor>;    AddressU = Clamp;    AddressV = Clamp;    MinFilter = Point;    MagFilter = Linear;    MipFilter = Linear;};static const int g_cKernelSize = 13;float2 PixelKernel[g_cKernelSize] ={    { 0, -6 },    { 0, -5 },    { 0, -4 },    { 0, -3 },    { 0, -2 },    { 0, -1 },    { 0,  0 },    { 0,  1 },    { 0,  2 },    { 0,  3 },    { 0,  4 },    { 0,  5 },    { 0,  6 },};float2 TexelKernel[g_cKernelSize]<    string ConvertPixelsToTexels = "PixelKernel";>;static const float BlurWeights[g_cKernelSize] = {    0.002216,    0.008764,    0.026995,    0.064759,    0.120985,    0.176033,    0.199471,    0.176033,    0.120985,    0.064759,    0.026995,    0.008764,    0.002216,};//-----------------------------------------------------------------------------// Pixel Shader: HorizontalBlur// Desc: Blurs the image vertically//-----------------------------------------------------------------------------float4 PostProcessPS( float2 Tex : TEXCOORD0 ) : COLOR0{    float4 Color = 0;    for (int i = 0; i < g_cKernelSize; i++)    {  Color += tex2D( g_samSrcColor, Tex + TexelKernel.xy ) * BlurWeights;    }    return Color;}//-----------------------------------------------------------------------------// Technique: PostProcess// Desc: Performs post-processing effect that converts a colored image to//  black and white.//-----------------------------------------------------------------------------technique PostProcess{    pass p0    {   VertexShader = null;   PixelShader = compile ps_2_0 PostProcessPS();   ZEnable = false;    }}It's shorter version, then I have my rendering function, but I don't know what to do:1.  Should I render all my scen to second renderTarget2. Change RenderTarget3. Render before RenderTarget with blure effect ?this is code:d3d::gd3dDevice->SetRenderTarget(0, gdRenderSurface);d3d::gd3dDevice->BeginScene();// rysowanie skyboxa.//d3d::gd3dDevice->SetRenderState(D3DRS_WRAP0, 0);gSkybox->draw( gCamera->pos() );d3d::chm->PreRender();d3d::chm->Render3();d3d::chm->PostRender();d3d::gd3dDevice->EndScene();d3d::gd3dDevice->SetRenderTarget(0, gdBackBuffer);d3d::gd3dDevice->Clear(0L, 0, D3DCLEAR_ZBUFFER, 0x88888888, 1.0f, 0L);d3d::gd3dDevice->BeginScene();g_pEffect->SetTechnique("PostProcess");g_pEffect->SetTexture("g_txSrcColor", gdTextureRender); UINT cPasses, iPass;   g_pEffect->Begin(&cPasses, 0);for (iPass = 0; iPass < cPasses; iPass++)   {  g_pEffect->BeginPass(iPass);  g_pEffect->EndPass();   }g_pEffect->End();d3d::gd3dDevice->EndScene();d3d::gd3dDevice->Present(0, 0, 0, 0);I know it's wrong but I don't know where ;/ I think I do not understand the basic principles of the blure   
Quote:Hi!My first post :D:D OKWelcome [smile]Your shader code looks reasonable to me, and if it doesn't work as expected you could enhance your learning by debugging it with PIX for Windows.As for the actual rendering, you will need two render targets (often referred to as a "ping pong" setup or something similar). The first render target contains the original unblurred image. You then render these results (now completely 2D and no further rendering of individual meshes) into the second render target (which could just be the screen/backbuffer) with your blurring pixel shader applied. The results in this second result will be blurred.hthJack   ;  Hi, so @jollyjeffers my rendering function more and less is correct, yes? With one exception, after bluring the previous renderTargetg_pEffect->Begin(&cPasses, 0);//...g_pEffect->End();  I must render quad with this blured texture, is that correct?Quote:Your shader code looks reasonable to me, and if...Yes, ofcourse :) because this is the SDK's version :D but a little bit shorter[Edited by - newtong on September 4, 2008 6:54:12 AM];Quote:Original post by newtongso @jollyjeffers my rendering function more and less is correct, yes?I'm not sure I follow all of your code, but it should be something along the lines of:IDirect3DSurface9 old_back_buffer = GetBackBuffer()SetRenderTarget(source_texture)RenderComplete3DScene()SetRenderTarget(old_back_buffer)for_each(pass in blur technique)    Configure pass    SetTexture(source_texture)    RenderFullScreenQuadHope that helps. The above works ok for your single horizontal blur but you'd need a second texture if you want to render a second vertical blur rather than render straight to the back buffer.hthJack   ;  Finally it's works :D:D Now I rendering my world to RT and then Rendering it on quade, blure effect still doesn't work but I suppose I know the problem.I've two questions:first:Can I use PixelShaders without any PixelShaders. I mean i don't even read from file or declaring any PixeShader (is tahta a reason why PixelShader want to work?)second:Is there any function that render quad on fullscreen? Or I must write my own function?Thx for your time, with best regardsNewToN   ;  Hi again!I still have some kind of bug ;/ but I don't know where and why. I'm rednering my scene to First RenderTarget then change RenderTarget and rendering quad with texture from first RT. It works perfectly! Problem appears when I trying to blur texture rendered on the quad - nothing happening, blur doesn't work at all. But when I change blur for other effect e.g. inverse (negative) everything works..I think that my blur effect had some 'bud days' and wont work :/ This is a code, any suggestions?function rendering scenevoid d3d::RenderScene(){   d3d::gd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&gOldBackBuff);   d3d::gd3dDevice->SetRenderTarget(0, gTextureRender);   d3d::gd3dDevice->Clear(0L, 0, D3DCLEAR_TARGET, 0x88888888, 1.0f, 0L);   d3d::gd3dDevice->BeginScene(); //rendering all world gSkybox->draw( gCamera->pos() );   d3d::gd3dDevice->EndScene();   //RT to texture   gdTextureRender->GetSurfaceLevel(0, &gTextureRender);   d3d::gd3dDevice->SetRenderTarget(0, gOldBackBuff);   d3d::gd3dDevice->Clear(0L, 0, D3DCLEAR_TARGET, 0x88888888, 1.0f, 0L);    d3d::gd3dDevice->BeginScene();gSkybox->draw( gCamera->pos() );UINT cPasses, iPass;int eff = 6; //blur vertical i horizontalg_pEffect[eff]->Begin(&cPasses, 0);for (iPass = 0; iPass < cPasses; iPass++)//orginalne{    g_pEffect[eff]->BeginPass(iPass);    g_pEffect[eff]->SetTexture("g_txSrcColor", gdTextureRender );    g_pEffect[eff]->CommitChanges();    g_pEffect[eff]->Begin(&cPasses, 0);  //rendering quad with my world as texture    d3d::gd3dDevice->SetStreamSource(0, quadVB, 0, sizeof(D3DVERTEX));    d3d::gd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);    g_pEffect[eff]->EndPass();    d3d::gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);    d3d::gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);    d3d::gd3dDevice->SetRenderState(D3DRS_DESTBLEND,  D3DBLEND_INVSRCALPHA);}g_pEffect[eff]->End();d3d::gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);d3d::gd3dDevice->EndScene();d3d::gd3dDevice->Present(0, 0, 0, 0);} and this is a shade code:texture g_txSrcColor;sampler2D g_samSrcColor =sampler_state{    Texture = <g_txSrcColor>;    AddressU = Clamp;    AddressV = Clamp;    MinFilter = Point;    MagFilter = Linear;    MipFilter = Linear;};static const int g_cKernelSize = 13;float2 PixelKernel[g_cKernelSize] ={    { 0, -6 },    { 0, -5 },    { 0, -4 },    { 0, -3 },    { 0, -2 },    { 0, -1 },    { 0,  0 },    { 0,  1 },    { 0,  2 },    { 0,  3 },    { 0,  4 },    { 0,  5 },    { 0,  6 },};float2 TexelKernel[g_cKernelSize]<    string ConvertPixelsToTexels = "PixelKernel";>;float2 PixelKernelH[g_cKernelSize] ={    { -6, 0 },    { -5, 0 },    { -4, 0 },    { -3, 0 },    { -2, 0 },    { -1, 0 },    {  0, 0 },    {  1, 0 },    {  2, 0 },    {  3, 0 },    {  4, 0 },    {  5, 0 },    {  6, 0 },};float2 TexelKernelH[g_cKernelSize]<    string ConvertPixelsToTexels = "PixelKernelH";>;static const float BlurWeights[g_cKernelSize] = {    //dla testow zmieniałem już nawet wagi i jedyne   //co uzyskałem to rozjasnienie tesktury a nie rozmycie ;/0.02216,//0.002216,    0.008764,    0.026995,    0.094759,//0.064759,    0.120985,    0.176033,    0.199471,    0.176033,    0.120985,    0.094759,//0.064759,    0.026995,    0.008764,    0.02216,//0.002216,};//-----------------------------------------------------------------------------// Pixel Shader: HorizontalBlur// Desc: Blurs the image horizontally//-----------------------------------------------------------------------------float4 PostProcessPS( float2 Tex : TEXCOORD0 ) : COLOR0{    float4 Color = 0;    for (int i = 0; i < g_cKernelSize; i++)    {  Color += tex2D( g_samSrcColor, Tex + TexelKernel.xy ) * BlurWeights;    }    return Color;}float4 PostProcessPS_H( float2 Tex : TEXCOORD0 ) : COLOR0{    float4 Color = 0;    for (int i = 0; i < g_cKernelSize; i++)    {  Color += tex2D( g_samSrcColor, Tex + TexelKernelH.xy ) * BlurWeights;    }   return Color;}technique PostProcess{    pass p0    {   VertexShader = null;   PixelShader = compile ps_3_0 PostProcessPS();   ZEnable = false;    }pass p1    {  VertexShader = null;  PixelShader = compile ps_3_0 PostProcessPS_H();   ZEnable = false;    }}Shader compile correctly, without any errors. Rest of the code too complie without errors. What's wrong ?   