Post processing shaders

This is a discussion on Post processing shaders within the Game Programming forums, part of the General Programming Boards category; Here is a first screenshot of my first post-processing shader. This is a very simple bloom shader so it's not ...

  1. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586

    Post processing shaders

    Here is a first screenshot of my first post-processing shader. This is a very simple bloom shader so it's not much to look at.

    I'm working on several more to add to the game.
    Last edited by VirtualAce; 03-12-2011 at 10:41 AM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Here is another try.

    1. Downsample to 64x64
    2. Blur
    3. Additive blend and saturate with original scene

    Defects:
    1. Edges of screen
    2. Filter sucks.

    Last edited by VirtualAce; 03-12-2011 at 10:41 AM.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Here is a really goofy post process. The screen aligned quad is offset just a hair from the original.

    Very odd.
    Last edited by VirtualAce; 03-12-2011 at 10:41 AM.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Final pic for the day.

    This shader has the edges fixed. I figured out what was going on in the shader.
    Last edited by VirtualAce; 03-12-2011 at 10:41 AM.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Looks most impressive Bubba, well done
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    For reference, what do these images look like with the post-processing deactivated?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    Pfft. If I didn't suck at memory managment (read: if my engine weren't crashing all the time lately), I'd have been doing this long ago.

    Seriously though, looks very nice.
    Last edited by psychopath; 12-11-2006 at 04:15 PM.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    No post processing:
    Last edited by VirtualAce; 03-12-2011 at 10:41 AM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Veery nice!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    The effect file code with the shaders in them.

    Code:
    //Glare vertex shader
    float4x4 matWorldViewProj;
    float fBlendFactor;
    
    sampler SourceTex1;
    sampler SourceTex2;
    sampler SourceTex3;
    sampler SourceTex4;
    
    struct VS_OUTPUT
    {
    	
      float4 Pos : POSITION;
      float2 UV0 : TEXCOORD0;
      float2 UV1 : TEXCOORD1;
      float2 UV2 : TEXCOORD2;
      float2 UV3 : TEXCOORD3;
    };
    
    
    VS_OUTPUT VSBlurMain(float4 Pos : POSITION,float2 Tex : TEXCOORD0)
    {
    	
      VS_OUTPUT Out=(VS_OUTPUT)0;
    	
      float2 vecUp=(-0.04,0.00);
      float2 vecDn=(0.04,0.00);
      float2 vecLt=(-0.02,0.0);
      float2 vecRt=(0.02,0.0);
          
      Out.Pos=mul(matWorldViewProj,Pos);
    
      Out.UV0=Tex.xy+vecUp;
      Out.UV1=Tex.xy+vecDn;
      Out.UV2=Tex.xy+vecLt;
      Out.UV3=Tex.xy+vecRt;
    		
      return Out;
    }
    
    float4 PSBlurMain(VS_OUTPUT In) : COLOR
    {
    	
      float4 Color0=tex2D(SourceTex1,In.UV0);
      float4 Color1=tex2D(SourceTex1,In.UV1);
      float4 Color2=tex2D(SourceTex1,In.UV2);
      float4 Color3=tex2D(SourceTex1,In.UV3);
      float4 vCol1=lerp(Color0,Color3,.50);
      float4 vCol2=lerp(Color1,Color2,.50);
    	
      return saturate(lerp(vCol1,vCol2,.50));
    }                 
    
    VS_OUTPUT VSABMain(float4 Pos : POSITION,float2 Tex : TEXCOORD0,
                       float2 Tex2 : TEXCOORD1)
    {
    	
      VS_OUTPUT Out=(VS_OUTPUT)0;
     
      Out.Pos=mul(matWorldViewProj,Pos);
      Out.UV0=Tex;
      Out.UV1=Tex;
    		
      return Out;
    }
    
    float4 PSABMain(VS_OUTPUT In) : COLOR
    {
    	
      float4 Color0=tex2D(SourceTex1,In.UV0);
      float4 Color1=tex2D(SourceTex2,In.UV1);
    		
      return ((Color1*0.40f)+Color0)*1.55f;
    }
    
    technique Blur
    {
      pass P0
      {
        Lighting=false;
        VertexShader=compile vs_3_0 VSBlurMain();
        PixelShader=compile ps_3_0 PSBlurMain();
      }
    }
    
    technique AddBlend
    {
      pass P0
      {
        Lighting=false;
        VertexShader=compile vs_3_0 VSABMain();
        PixelShader=compile ps_3_0 PSABMain();
      }
    }
    The source code that utilizes this effect file:
    Code:
    ...
      HRESULT hr=0; 
        
      //Get primary back buffer surface
      IDirect3DSurface9 *pSourceSurf=NULL;
      hr=m_pDevice->GetRenderTarget(0,&pSourceSurf);
      
      if (FAILED(hr))
      {
        ::MessageBox(0,"Cannot get render surface","Cannot get render surface",0);
      }
        
      //Get surface of down sample texture (currently blank)
      IDirect3DSurface9 *pTargSurf=NULL;
      hr=m_pScene->GetSurfaceLevel(0,&pTargSurf);
          
      //Copy texture from original scene render to 1/64 down sample texture
      m_pDevice->EndScene();
      
      //Copy rendered scene into m_pScene
      hr=m_pDevice->StretchRect(pSourceSurf,NULL,
                                pTargSurf,NULL,D3DTEXF_LINEAR);
      
      hr=m_pDownSample164->GetSurfaceLevel(0,&pTargSurf);
      
      //Downsample rendered scene into m_pDownSample164
      hr=m_pDevice->StretchRect(pSourceSurf,NULL,
                                pTargSurf,NULL,D3DTEXF_NONE);
      
      if (FAILED(hr)) ::MessageBox(0,"","Stretch rect failed",0);
      
      m_pDevice->BeginScene();
      
      //Blur the texture by drawing a quad on Blur164 using the
      //downsample164 texture
       
      UINT iPass=0,cPasses=0;
      	
      //Set render target to down sample render target
      IDirect3DSurface9 *pBlur164Surf=NULL;
      m_pBlur164->GetSurfaceLevel(0,&pBlur164Surf);
    	
      //Set render target to blur surface
      m_pDevice->SetRenderTarget(0,pBlur164Surf);
    	
      //Our down sampled texture to blur
      m_pDevice->SetTexture(0,m_pDownSample164);
      m_pDevice->SetSamplerState(0,D3DSAMP_ADDRESSU,D3DTADDRESS_CLAMP);
      m_pDevice->SetSamplerState(0,D3DSAMP_ADDRESSV,D3DTADDRESS_CLAMP);
    	
      m_pDevice->SetSamplerState(1,D3DSAMP_ADDRESSU,D3DTADDRESS_CLAMP);
      m_pDevice->SetSamplerState(1,D3DSAMP_ADDRESSV,D3DTADDRESS_CLAMP);
    	
      m_pDevice->SetSamplerState(2,D3DSAMP_ADDRESSU,D3DTADDRESS_CLAMP);
      m_pDevice->SetSamplerState(2,D3DSAMP_ADDRESSV,D3DTADDRESS_CLAMP);
    
      m_pDevice->SetSamplerState(3,D3DSAMP_ADDRESSU,D3DTADDRESS_CLAMP);
      m_pDevice->SetSamplerState(3,D3DSAMP_ADDRESSV,D3DTADDRESS_CLAMP);
    	
    		
      //Blur it by drawing a quad on blur surface
      //Use blur technique from effect file
      m_pDevice->SetVertexDeclaration(VertexTLT1::pDecl);
    
      m_pGlareEffect->SetTechnique("Blur");
      m_pGlareEffect->Begin(&cPasses, 0);
      for(iPass = 0; iPass < cPasses; iPass++)
      {
        m_pGlareEffect->BeginPass(iPass);
        RenderScreenAlignedQuad(0,0,m_pGlareEffect);
        m_pGlareEffect->EndPass();
      }
      m_pGlareEffect->End();
    		
      //Set back to primary buffer
    	
      m_pDevice->SetRenderTarget(0,pSourceSurf);
    		
      m_pDevice->SetVertexDeclaration(VertexTLT1::pDecl);
      m_pGlareEffect->SetTechnique("AddBlend");
      m_pGlareEffect->SetFloat("fBlendFactor",0.05f);
    	
      m_pDevice->SetTexture(0,m_pBlur164);
      m_pDevice->SetTexture(1,m_pScene);
    	
      m_pGlareEffect->Begin(&cPasses, 0);
      for(iPass = 0; iPass < cPasses; iPass++)
      {
        m_pGlareEffect->BeginPass(iPass);
        RenderScreenAlignedQuad(0,0,m_pGlareEffect);
        m_pGlareEffect->EndPass();
      }
    	
      m_pGlareEffect->End();
      m_pDevice->EndScene();  
      
    }
    All surfaces are members of the application class and can be used for any shader effects. I couldn't find a better way to do it.
    Last edited by VirtualAce; 12-12-2006 at 04:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sub Array Processing
    By GCNDoug in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2007, 03:41 PM
  2. Using a lot of processing time!
    By nickname_changed in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2003, 03:44 AM
  3. Post your Best Text Adventure
    By Joe100 in forum Game Programming
    Replies: 3
    Last Post: 08-15-2003, 05:47 PM
  4. Auto POST
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-07-2003, 10:42 AM
  5. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21