Ok here’s some background on GLSL.
'sampler2D’s are the TOP image(s) that are inputted into the node.
texture2D() is the function that samples the image at a given texture coordinate.
gl_TexCoord[0].st is the texture coordinate of the current pixel on the output image. It’ll be (0,0) for the bottom left pixel, (1,1) for the top right pixel.
To illustrate what gl_TexCoord[0].st is, output it as the color.
gl_FragColor = vec4(gl_TexCoord[0].s, gl_TexCoord[0].t, 0.0, 1.0);
Ok, so to sample neighbors, all you need to do is offset the texture coordinates.
The next concept to understand is ‘uniforms’. A uniform is a input to a shader, it’s value is the same for every pixel thats rendered.
I automatically set the values for some helful uniforms.
‘uResolution’ which is based on the resolution of the output image.
‘uInputRes1’ which is based on the resolution of the first input
‘uInputRes2’ which is based on the resolution of the 2nd input
etc.
Declare the ones you are interested in at the top of your shader like this
uniform vec4 uResolution;
uniform vec4 uInputRes1;
It’s 4 values are vec4( 1.0 / XRes, 1.0 / YRes, XRes, YRes);
The first two values are the ones that you are interested in. Offsetting a texture coordinate by 1.0 / XRes will move texture coordinate so they sample 1 pixel to the right.
So for example if you wanted to sample the pixel thats 2 pixels up and 1 pixel right of the current pixel, you’d do this
vec4 col1 = texture2D(sInput1, vec2(gl_TexCoord[0].s + uInputRes1.s, gl_TexCoord[0].t + (2.0 * uInputRes1.t)));
There is a sampling function called texture2DOffset() that encapsulates this more for you, but not all video cards support it so I won’t go into it right now.
To answer the 2nd question, hardware shaders don’t have memory, they have registers, which are quite a bit more limited. How limited depends on your video card. My Geforce 7900 GT has the ability to store 32 vec4s at a time. But the way nvidia hardware works the more registers you use, the slower the shader will run. So you should try and work in small batches if you can. Do some samples, do some math, repeat.
Hope that helps.