Convolution shader for blur

I am trying to do a convolution shader but I cannot find the way on how to find the neighbours pixels value. the code is:

        sampler2D _MainTex;
		float filter[25] = {
						-4.0,	-1.0,	0.0,	-1.0,	-4.0,
						-1.0,	2.0,	3.0,	2.0,	-1.0,
						0.0,	3.0,	4.0,	3.0,	0.0,
						-1.0,	2.0,	3.0,	2.0,	-1.0,
						-4.0,	-1.0,	0.0,	-1.0,	-4.0
					};

		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o)
		{
			half4 c = half4( 1, 0, 0, 1);

			int x,y,i=0;
			int xx = 0, yy = 0;
			for (x=0;x<5;x++)
			{
				xx = x - 2;
				for (y=0;y<5;y++)
				{
					yy = y - 2;
					c.rgb += filter[i++] * tex2D (_MainTex, IN.uv_MainTex + float2(xx, yy)).rgb;
				}
			}
			
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}

This throws an error:

Program 'frag_surf', Temporary register limit of 12 exceeded; 26 registers needed to compile program at line 8

Any ideas on how to solve the error and how to access neighbours values?

I’m no shader guru but I think you want ‘multi tap’

http://www.yaldex.com/open-gl/ch19lev1sec7.html
http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter27.html

and such

Sounds like you need to reduce the size of that matrix! I’d suggest using a 3x3 matrix reflected to act like a 5x5.

sampler2D _MainTex;
float filter[9] = {
         4.0,    3.0,    0.0,
         3.0,    2.0,    -1.0,
         0.0,    -1.0,    -4.0
};

struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o)
{
    half4 c = half4( 1, 0, 0, 1);

    int x,y,i=0;
    int xx = 0, yy = 0;
    for (x=0;x<5;x++)
    {
        xx = x - 2;
        for (y=0;y<5;y++)
        {
            yy = y - 2;
            i = abs(xx) + 3*abs(yy);
            c.rgb += filter <em>* tex2D (_MainTex, IN.uv_MainTex + float2(xx, yy)).rgb;</em>

}
}

o.Albedo = c.rgb;
o.Alpha = c.a;
}