Sdk postprocess

From Valve Developer Community
Revision as of 06:15, 18 April 2011 by DaFox (talk | contribs)
Jump to: navigation, search

sdk_postprocess is a shader provided with the original Source SDK, it has since been replaced by the current set of SDK_Shaders.

This shader applies an overdone sharpen filter to the image.

alt text

sdk_postprocess_ps20.fxc

#include "common_ps_fxc.h"

sampler BaseTextureSampler	: register( s0 );
const HALF sharpenFactor : register( c0 );

struct PS_INPUT
{
	HALF2 vBaseTexCoord0 : TEXCOORD0;
	HALF2 vBaseTexCoord1 : TEXCOORD1;
	HALF2 vBaseTexCoord2 : TEXCOORD2;
	HALF2 vBaseTexCoord3 : TEXCOORD3;
	HALF2 vBaseTexCoord4 : TEXCOORD4;
};

HALF4 main( PS_INPUT i ) : COLOR
{
	HALF4 mainColor = tex2D( BaseTextureSampler, i.vBaseTexCoord1 );

	HALF4 baseColor1 = tex2D( BaseTextureSampler, i.vBaseTexCoord1 );
	HALF4 baseColor2 = tex2D( BaseTextureSampler, i.vBaseTexCoord2 );
	HALF4 baseColor3 = tex2D( BaseTextureSampler, i.vBaseTexCoord3 );
	HALF4 baseColor4 = tex2D( BaseTextureSampler, i.vBaseTexCoord4 );

	HALF4 g = mainColor - (baseColor1 + baseColor2 + baseColor3 + baseColor4) * 0.25;
	return mainColor + g * sharpenFactor;
}

sdk_postprocess_vs20.fxc

#include "common_vs_fxc.h"

struct VS_INPUT
{
	float3 vPos						: POSITION;
	float2 vBaseTexCoord			: TEXCOORD0;
};

struct VS_OUTPUT
{
    float4 vProjPos					: POSITION;	
	float2 vBaseTexCoord0			: TEXCOORD0;
	float2 vBaseTexCoord1			: TEXCOORD1;
	float2 vBaseTexCoord2			: TEXCOORD2;
	float2 vBaseTexCoord3			: TEXCOORD3;
	float2 vBaseTexCoord4			: TEXCOORD4;
};

const float cBlurAmount :  register( SHADER_SPECIFIC_CONST_0 );

VS_OUTPUT main( const VS_INPUT v )
{
	VS_OUTPUT o = ( VS_OUTPUT )0;

	// Project the point.
	float4 vProjPos = mul( float4( v.vPos, 1 ), cModelViewProj );
	o.vProjPos = vProjPos;

	// Pass texture coordinates through to the pixel shader.
	float adj = cBlurAmount;
	
	o.vBaseTexCoord0 = v.vBaseTexCoord;
	o.vBaseTexCoord1 = v.vBaseTexCoord + float2( -adj, adj );
	o.vBaseTexCoord2 = v.vBaseTexCoord + float2( -adj, -adj );
	o.vBaseTexCoord3 = v.vBaseTexCoord + float2( adj, -adj );
	o.vBaseTexCoord4 = v.vBaseTexCoord + float2( adj, adj );

	return o;
}

sdk_postprocess.cpp

//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Just about as simple as a shader gets. Specify a vertex
//          and pixel shader, bind textures, and that's it.
//
// $Header: $
// $NoKeywords: $
//=============================================================================//

#include "BaseVSShader.h"
#include "convar.h"

/*

NOTE: this shader needs to be called from the client DLL. You could insert this code at the end of CViewRender::RenderView.

if ( g_pMaterialSystemHardwareConfig->GetDXSupportLevel() >= 80 )
{
		// Get whatever material references your postprocess shader.
		IMaterial *pMaterial = materials->FindMaterial( "sdk/sdk_postprocess", TEXTURE_GROUP_CLIENT_EFFECTS, true );
		if ( pMaterial )
		{
				// This copies the contents of the framebuffer (drawn during RenderView) into a texture that your shader can use.
				UpdateScreenEffectTexture( 0 );

				materials->MatrixMode( MATERIAL_PROJECTION );
				materials->PushMatrix();
				materials->LoadIdentity();	

				materials->MatrixMode( MATERIAL_VIEW );
				materials->PushMatrix();
				materials->LoadIdentity();	

				materials->DrawScreenSpaceQuad( pMaterial );

				materials->MatrixMode( MATERIAL_PROJECTION );
				materials->PopMatrix();
				materials->MatrixMode( MATERIAL_VIEW );
				materials->PopMatrix();
		}
}

*/


BEGIN_VS_SHADER( SDK_PostProcess, "Help for SDK_PostProcess" )

	BEGIN_SHADER_PARAMS
		SHADER_PARAM( FILTERSIZE, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
		SHADER_PARAM( SHARPENFACTOR, SHADER_PARAM_TYPE_FLOAT, "0.0", "contrast 0 == normal 1 == color*color" )
	END_SHADER_PARAMS

	// Set up anything that is necessary to make decisions in SHADER_FALLBACK.
	SHADER_INIT_PARAMS()
	{
	}

	SHADER_FALLBACK
	{
		return 0;
	}

	bool NeedsFullFrameBufferTexture( IMaterialVar **params ) const
	{
		return true;
	}

	SHADER_INIT
	{
	}

	SHADER_DRAW
	{
		SHADOW_STATE
		{
			// Enable the texture for base texture and lightmap.
			pShaderShadow->EnableTexture( SHADER_TEXTURE_STAGE0, true );
			pShaderShadow->EnableDepthWrites( false );

			int fmt = VERTEX_POSITION;
			pShaderShadow->VertexShaderVertexFormat( fmt, 1, 0, 0, 0 );

			pShaderShadow->SetVertexShader( "sdk_postprocess_vs20", 0 );
			pShaderShadow->SetPixelShader( "sdk_postprocess_ps20" );

			// Optional, do some blending..
			//pShaderShadow->EnableBlending( true );
			//pShaderShadow->BlendFunc( SHADER_BLEND_DST_COLOR, SHADER_BLEND_SRC_COLOR );

			DefaultFog();
		}
		DYNAMIC_STATE
		{
			// Tell it how wide the blur is.
			int width, height;
			pShaderAPI->GetBackBufferDimensions( width, height );

			float v[4] = {0,0,0,0};
			v[0] = params[FILTERSIZE]->GetFloatValue() / width;
			pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, v, 1 );

			float factor[4];
			factor[0] = params[SHARPENFACTOR]->GetFloatValue();
			factor[1] = factor[2] = factor[3] = factor[0];
			pShaderAPI->SetPixelShaderConstant( 0, factor );

			pShaderAPI->BindFBTexture( SHADER_TEXTURE_STAGE0 );
			pShaderAPI->SetVertexShaderIndex( 0 );
		}
		Draw();
	}
END_SHADER