/*
 * Copyright (C) 2005 Oliver Hunt <ojh16@student.canterbury.ac.nz>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

//TODO: We currently ignore the input kernel lengths
kernel vec4 convolve3x3(sampler image,  float divisor, float bias,
			vec3 m0, vec3 m1, vec3 m2)
{
    vec4 colour = vec4(0.0, 0.0, 0.0, 0.0);
    vec2 pos= samplerCoord(image);
    colour =  sample(image, pos + vec2(-1.0, -1.0)) *m0.x;
    colour += sample(image, pos + vec2(-1.0,  0.0)) *m0.y;
    colour += sample(image, pos + vec2(-1.0,  1.0)) *m0.z;
    colour += sample(image, pos + vec2( 0.0, -1.0)) *m1.x;
    colour += sample(image, pos) * m1.y;
    colour += sample(image, pos + vec2( 0.0,  1.0))*m1.z;
    colour += sample(image, pos + vec2( 1.0, -1.0))*m2.x;
    colour += sample(image, pos + vec2( 1.0,  0.0))*m2.y;
    colour += sample(image, pos + vec2( 1.0,  1.0))*m2.z;
    return colour / divisor + bias;
}

kernel vec4 mergeNormals(sampler Nx, sampler Ny, sampler src, float surfaceScale)
{
    vec3 N = vec3(surfaceScale * sample(Nx, samplerCoord(Nx)).a, -surfaceScale * sample(Ny, samplerCoord(Ny)).a, 1.0);
    N = normalize(N);
    return vec4(N.x, N.y, N.z, sample(src, samplerCoord(src)).a);
}
