#include "filter.cuh"

#include <cstdint>

/**********************************************Filter Methods***************************************************/

void launch_medKernel(const dim3& blocks, const dim3& threads, char *out, const char *in, size_t dimy, size_t dimx, size_t dimz, size_t pitch) {
    medKernel<<<blocks, threads>>>(out, in, dimy, dimx, dimz, pitch);
}

__global__ void medKernel(char *out, const char *in, size_t dimy, size_t dimx, size_t dimz, size_t pitch) {
    int row = threadIdx.y;
    int col = threadIdx.x;

    __shared__ char im[blockSizeRow + 2][blockSizeCol + 2][3];

    if ((threadIdx.y + blockIdx.y * blockDim.y < dimy - 2) && (threadIdx.x + blockIdx.x * blockDim.x < dimx - 2)) {

        im[row + 1][col + 1][0] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch);
        im[row + 1][col + 1][1] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy);
        im[row + 1][col + 1][2] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);

        if (threadIdx.x == 0) {
            im[row + 1][col][0] = *(in + (blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch);
            im[row + 1][col][1] = *(in + (blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy);
            im[row + 1][col][2] = *(in + (blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);
        }

        if ((threadIdx.x == blockDim.x - 1) || (threadIdx.x + blockIdx.x * blockDim.x == dimx - 3)) {
            im[row + 1][col + 2][0] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch);
            im[row + 1][col + 2][1] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy);
            im[row + 1][col + 2][2] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);
        }

        if (threadIdx.y == 0) {
            im[row][col + 1][0] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch);
            im[row][col + 1][1] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy);
            im[row][col + 1][2] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);
        }

        if ((threadIdx.y == blockDim.y - 1) || (threadIdx.y + blockIdx.y * blockDim.y == dimy - 3)) {
            im[row + 2][col + 1][0] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch);
            im[row + 2][col + 1][1] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy);
            im[row + 2][col + 1][2] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);
        }

        if (threadIdx.x == 0 && threadIdx.y == 0) {
            im[row][col][0] = *(in + (blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch);
            im[row][col][1] = *(in + (blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy);
            im[row][col][2] = *(in + (blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);
        }

        if ((threadIdx.x == blockDim.x - 1 || (threadIdx.x + blockIdx.x * blockDim.x == dimx - 3)) && threadIdx.y == 0) {
            im[row][col + 2][0] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch);
            im[row][col + 2][1] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy);
            im[row][col + 2][2] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);
        }

        if (threadIdx.x == 0 && (threadIdx.y == blockDim.y - 1 || (threadIdx.y + blockIdx.y * blockDim.y == dimy - 3))) {
            im[row + 2][col][0] = *(in + (blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch);
            im[row + 2][col][1] = *(in + (blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy);
            im[row + 2][col][2] = *(in + (blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);
        }

        if ((threadIdx.x == blockDim.x - 1 || (threadIdx.x + blockIdx.x * blockDim.x == dimx - 3)) && (threadIdx.y == blockDim.y - 1 || (threadIdx.y + blockIdx.y * blockDim.y == dimy - 3))) {
            im[row + 2][col + 2][0] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch);
            im[row + 2][col + 2][1] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy);
            im[row + 2][col + 2][2] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * 2);
        }


    }
    //
    __syncthreads();

    if ((threadIdx.y + blockIdx.y * blockDim.y < dimy - 2) && (threadIdx.x + blockIdx.x * blockDim.x < dimx - 2)) {
        char a0 = im[row][col][0];
        char a1 = im[row + 1][col][0];
        char a2 = im[row + 2][col][0];
        char a3 = im[row][col + 1][0];
        char a4 = im[row + 1][col + 1][0];
        char a5 = im[row + 2][col + 1][0];
        char a6 = im[row][col + 2][0];
        char a7 = im[row + 1][col + 2][0];
        char a8 = im[row + 2][col + 2][0];
        char a9 = im[row][col][1];
        char a10 = im[row + 1][col][1];
        char a11 = im[row + 2][col][1];
        char a12 = im[row][col + 1][1];
        char a13 = im[row + 1][col + 1][1];
        char a14 = im[row + 2][col + 1][1];

        minmax15(&a0, &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row][col + 2][1];
        minmax14(&a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row + 1][col + 2][1];
        minmax13(&a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row + 2][col + 2][1];
        minmax12(&a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row][col][2];
        minmax11(&a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row + 1][col][2];
        minmax10(&a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row + 2][col][2];
        minmax9(&a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row][col + 1][2];
        minmax8(&a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row + 1][col + 1][2];
        minmax7(&a8, &a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row + 2][col + 1][2];
        minmax6(&a9, &a10, &a11, &a12, &a13, &a14);
        a14 = im[row][col + 2][2];
        minmax5(&a10, &a11, &a12, &a13, &a14);
        a14 = im[row + 1][col + 2][2];
        minmax4(&a11, &a12, &a13, &a14);
        a14 = im[row + 2][col + 2][2];
        minmax3(&a12, &a13, &a14);

        *(out + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch) = a13;

    }

    for (int i = 1; i < dimz - 1; i++) {
        int frame = (i - 1) % 3;

        if ((threadIdx.y + blockIdx.y * blockDim.y < dimy - 2) && (threadIdx.x + blockIdx.x * blockDim.x < dimx - 2)) {

            im[row + 1][col + 1][frame] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));

            if (threadIdx.x == 0) {

                im[row + 1][col][frame] = *(in + (blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));
            }

            if ((threadIdx.x == blockDim.x - 1) || (threadIdx.x + blockIdx.x * blockDim.x == dimx - 3)) {

                im[row + 1][col + 2][frame] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));

            }

            if (threadIdx.y == 0) {

                im[row][col + 1][frame] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));

            }

            if ((threadIdx.y == blockDim.y - 1) || (threadIdx.y + blockIdx.y * blockDim.y == dimy - 3)) {

                im[row + 2][col + 1][frame] = *(in + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));

            }

            if (threadIdx.x == 0 && threadIdx.y == 0) {

                im[row][col][frame] = *(in + (blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));

            }

            if ((threadIdx.x == blockDim.x - 1 || (threadIdx.x + blockIdx.x * blockDim.x == dimx - 3)) && threadIdx.y == 0) {

                im[row][col + 2][frame] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));

            }

            if (threadIdx.x == 0 && (threadIdx.y == blockDim.y - 1 || (threadIdx.y + blockIdx.y * blockDim.y == dimy - 3))) {

                im[row + 2][col][frame] = *(in + (blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));

            }

            if ((threadIdx.x == blockDim.x - 1 || (threadIdx.x + blockIdx.x * blockDim.x == dimx - 3)) && (threadIdx.y == blockDim.y - 1 || (threadIdx.y + blockIdx.y * blockDim.y == dimy - 3))) {

                im[row + 2][col + 2][frame] = *(in + (2 + threadIdx.x + blockIdx.x * blockDim.x) + (2 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch * dimy * (1 + i));

            }


        }

        __syncthreads();

        if ((threadIdx.y + blockIdx.y * blockDim.y < dimy - 2) && (threadIdx.x + blockIdx.x * blockDim.x < dimx - 2)) {
            char a0 = im[row][col][0];
            char a1 = im[row + 1][col][0];
            char a2 = im[row + 2][col][0];
            char a3 = im[row][col + 1][0];
            char a4 = im[row + 1][col + 1][0];
            char a5 = im[row + 2][col + 1][0];
            char a6 = im[row][col + 2][0];
            char a7 = im[row + 1][col + 2][0];
            char a8 = im[row + 2][col + 2][0];
            char a9 = im[row][col][1];
            char a10 = im[row + 1][col][1];
            char a11 = im[row + 2][col][1];
            char a12 = im[row][col + 1][1];
            char a13 = im[row + 1][col + 1][1];
            char a14 = im[row + 2][col + 1][1];

            minmax15(&a0, &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row][col + 2][1];
            minmax14(&a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row + 1][col + 2][1];
            minmax13(&a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row + 2][col + 2][1];
            minmax12(&a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row][col][2];
            minmax11(&a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row + 1][col][2];
            minmax10(&a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row + 2][col][2];
            minmax9(&a6, &a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row][col + 1][2];
            minmax8(&a7, &a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row + 1][col + 1][2];
            minmax7(&a8, &a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row + 2][col + 1][2];
            minmax6(&a9, &a10, &a11, &a12, &a13, &a14);
            a14 = im[row][col + 2][2];
            minmax5(&a10, &a11, &a12, &a13, &a14);
            a14 = im[row + 1][col + 2][2];
            minmax4(&a11, &a12, &a13, &a14);
            a14 = im[row + 2][col + 2][2];
            minmax3(&a12, &a13, &a14);

            *(out + (1 + threadIdx.x + blockIdx.x * blockDim.x) + (1 + threadIdx.y + blockIdx.y * blockDim.y) * pitch + pitch*dimy*i) = a13;

            __syncthreads();

        }
    }
}



//////////////////////////////////////////////////////////////////
//Row convolution
//////////////////////////////////////////////////////////////////

void launch_convolutionRows(const dim3& blocks, const dim3& threads, char* output, const char* input, size_t imageW, size_t imageH, size_t pitch) {
    convolutionRows<<<blocks, threads>>>(output, input, imageW, imageH, pitch);
}

__global__ void convolutionRows(char* output, const char* input, size_t imageW, size_t imageH, size_t pitch) {

    __shared__ float sData[ROWS_Y][(COLS_X*PIX_PER_THREAD) + (2 * KERNEL_RADIUS)];


    const int x = blockIdx.x * (COLS_X*PIX_PER_THREAD) + threadIdx.x;
    const int y = blockIdx.y * blockDim.y + threadIdx.y;

    //check to see if we are guaranteed to be within the image bounds
    if ((y < imageH) && (x + (COLS_X * PIX_PER_THREAD) < imageW)) {


        //load left overlap
        if (threadIdx.x == 0) {
            for (int n = KERNEL_RADIUS; n >= 1; n--) {
                sData[threadIdx.y][threadIdx.x + (-n + KERNEL_RADIUS)] = ((x - n) > 0) ? input[y * pitch + x - n] : 0;
            }
        }



        //load PIX_PER_THREAD values
#pragma unroll
        for (int i = 0; i < PIX_PER_THREAD; i++) {
            sData[threadIdx.y][threadIdx.x + (COLS_X * i) + KERNEL_RADIUS] = input[y * pitch + x + (COLS_X * i)];
        }


        //load in right overlap
        for (int n = 1; n <= KERNEL_RADIUS; n++) {
            sData[threadIdx.y][(COLS_X * PIX_PER_THREAD) + KERNEL_RADIUS + n - 1] = (blockIdx.x * (COLS_X*PIX_PER_THREAD) + (COLS_X * PIX_PER_THREAD) + n < imageW) ? input[y * pitch + blockIdx.x * (COLS_X*PIX_PER_THREAD) + (COLS_X * PIX_PER_THREAD) + (n - 1)] : 0;
        }


        __syncthreads();

        //do the convolution
        //#pragma unroll
        for (int i = 0; i < PIX_PER_THREAD; i++) {
            float sum = 0;
            //#pragma unroll
            for (int n = -KERNEL_RADIUS; n <= KERNEL_RADIUS; n++) {
                sum += gaussK[KERNEL_RADIUS - n] * (float)sData[threadIdx.y][threadIdx.x + i * COLS_X + n + KERNEL_RADIUS];
            }

            output[y * pitch + x + (COLS_X * i)] = sum;
        }

        //we are not guaranteed to be within the x image bounds
    }
    else if (y < imageH) {

        //load left overlap (should evaluate to input[y*pitch+x-1] unless image is less than COLS_X*PIX_PER_THREAD wide)
        if (threadIdx.x == 0) {
            for (int n = KERNEL_RADIUS; n >= 1; n--) {
                sData[threadIdx.y][threadIdx.x + (-n + KERNEL_RADIUS)] = ((x - n) > 0) ? input[y * pitch + x - n] : 0;
            }
        }

        //load as many values as allowed fill the rest with zeros
        //#pragma unroll
        for (int i = 0; i < PIX_PER_THREAD; i++) {
            if (x + (COLS_X * i) < imageW) {
                sData[threadIdx.y][threadIdx.x + (COLS_X * i) + KERNEL_RADIUS] = input[y * pitch + x + (COLS_X * i)];
            }
            else {
                //load in zeros for the rest of the shared memory
                sData[threadIdx.y][threadIdx.x + (COLS_X * i) + KERNEL_RADIUS] = 0;
            }
        }
        //load in right overlap
        for (int n = 1; n <= KERNEL_RADIUS; n++) {
            sData[threadIdx.y][(COLS_X * PIX_PER_THREAD) + KERNEL_RADIUS + n - 1] = (blockIdx.x * (COLS_X*PIX_PER_THREAD) + (COLS_X * PIX_PER_THREAD) + n < imageW) ? input[y * pitch + blockIdx.x * (COLS_X*PIX_PER_THREAD) + (COLS_X * PIX_PER_THREAD) + (n - 1)] : 0;
        }

        __syncthreads();

        //do the convolution
        //#pragma unroll
        for (int i = 0; i < PIX_PER_THREAD; i++) {
            float sum = 0;
            if (x + (COLS_X * i) < imageW) {

                //#pragma unroll
                for (int n = -KERNEL_RADIUS; n <= KERNEL_RADIUS; n++) {
                    sum += gaussK[KERNEL_RADIUS - n] * (float)sData[threadIdx.y][threadIdx.x + i * COLS_X + n + KERNEL_RADIUS];
                }

                output[y * pitch + x + (COLS_X * i)] = sum;
            }
            else {
                break;
            }
        }
    }
}


//////////////////////////////////////////////////////////////////
//Column convolution
//////////////////////////////////////////////////////////////////

void launch_convolutionCols(const dim3& blocks, const dim3& threads, cudaSurfaceObject_t surf, char* input, size_t imageW, size_t imageH, size_t pitch, size_t zLoc) {
    convolutionCols<<<blocks, threads>>>(surf, input, imageW, imageH, pitch, zLoc);
}

__global__ void convolutionCols(cudaSurfaceObject_t surf, char* input, size_t imageW, size_t imageH, size_t pitch, size_t zLoc) {

    __shared__ float sData[COLS_Y*PIX_PER_THREAD + 2 * KERNEL_RADIUS][ROWS_X];

    //const uint32_t zLoc = z;// bscanLocations[z];

    const int x = blockIdx.x * blockDim.x + threadIdx.x;
    const int y = blockIdx.y * (COLS_Y*PIX_PER_THREAD) + threadIdx.y;

    //check to see if we are guaranteed to be within the image bounds
    if ((x < imageW) && (y + (COLS_Y * PIX_PER_THREAD) < imageH)) {


        //load top overlap
        if (threadIdx.y == 0) {
            for (int n = KERNEL_RADIUS; n >= 1; n--) {
                sData[threadIdx.y + (-n + KERNEL_RADIUS)][threadIdx.x] = ((y - n) > 0) ? input[(y - n) * pitch + x] : 0;
            }
        }
        //load PIX_PER_THREAD values
#pragma unroll
        for (int i = 0; i < PIX_PER_THREAD; i++) {
            sData[threadIdx.y + (COLS_Y * i) + KERNEL_RADIUS][threadIdx.x] = input[(y + (COLS_Y * i)) * pitch + x];
        }


        //load in bottom overlap
        for (int n = 1; n <= KERNEL_RADIUS; n++) {
            sData[(COLS_Y * PIX_PER_THREAD) + KERNEL_RADIUS + n - 1][threadIdx.x] = (blockIdx.y * (COLS_Y*PIX_PER_THREAD) + (COLS_Y * PIX_PER_THREAD) + n < imageH) ? input[(blockIdx.y * (COLS_Y*PIX_PER_THREAD) + (COLS_Y * PIX_PER_THREAD) + (n - 1)) * pitch + x] : 0;
        }


        __syncthreads();

        //do the convolution
#pragma unroll
        for (int i = 0; i < PIX_PER_THREAD; i++) {
            float sum = 0;
#pragma unroll
            for (int n = -KERNEL_RADIUS; n <= KERNEL_RADIUS; n++) {
                sum += gaussK[KERNEL_RADIUS - n] * (float)sData[threadIdx.y + i * COLS_Y + n + KERNEL_RADIUS][threadIdx.x];
            }

            //output[(y + (COLS_Y * i)) * pitch + x] = sum;
            //write out the the texture
            //float tempVol = 255 * (sum - 40) / (110 - 40); //works for logged values

            //if (tempVol < 0) {
            //    tempVol = 0.0;
            //}
            //if (tempVol > 255) {
            //    tempVol = 255.;
            //}

            ////why does writing out tempvol not work?
            //output[(y + (COLS_Y * i)) * pitch + x] = tempVol;
            surf3Dwrite((unsigned char)sum, surf, x, (y + (COLS_Y * i)), zLoc);
        }

        //we are not guaranteed to be within the x image bounds
    }
    else if (x < imageW) {

        //load left overlap (should evaluate to input[(y-1)*pitch+x] unless image is less than COLS_Y*PIX_PER_THREAD high)
        if (threadIdx.y == 0) {
            for (int n = KERNEL_RADIUS; n >= 1; n--) {
                sData[threadIdx.y + (-n + KERNEL_RADIUS)][threadIdx.x] = ((y - n) > 0) ? input[(y - n) * pitch + x] : 0;
            }
        }

        //load as many values as allowed fill the rest with zeros
#pragma unroll
        for (int i = 0; i < PIX_PER_THREAD; i++) {
            if (y + (COLS_Y * i)  < imageH) {
                sData[threadIdx.y + (COLS_Y * i) + KERNEL_RADIUS][threadIdx.x] = input[(y + (COLS_Y * i)) * pitch + x];
            }
            else {
                //load in zeros for the rest of the shared memory
                sData[threadIdx.y + (COLS_Y * i) + KERNEL_RADIUS][threadIdx.x] = 0;
            }
        }

        //load in bottom overlap

        for (int n = 1; n <= KERNEL_RADIUS; n++) {
            sData[(COLS_Y * PIX_PER_THREAD) + KERNEL_RADIUS + n - 1][threadIdx.x] = (blockIdx.y * (COLS_Y*PIX_PER_THREAD) + (COLS_Y * PIX_PER_THREAD) + n < imageH) ? input[(blockIdx.y * (COLS_Y*PIX_PER_THREAD) + (COLS_Y * PIX_PER_THREAD) + (n - 1)) * pitch + x] : 0;
        }

        __syncthreads();

        //do the convolution
#pragma unroll
        for (int i = 0; i < PIX_PER_THREAD; i++) {
            float sum = 0;
            if (y + (COLS_Y * i) < imageH) {

#pragma unroll
                for (int n = -KERNEL_RADIUS; n <= KERNEL_RADIUS; n++) {
                    sum += gaussK[KERNEL_RADIUS - n] * (float)sData[threadIdx.y + i * COLS_Y + n + KERNEL_RADIUS][threadIdx.x];
                }

                //write out to the texture
               /* float tempVol = 255 * (sum - 40) / (110 - 40);

                if (tempVol < 0) {
                    tempVol = 0.0;
                }
                if (tempVol > 255) {
                    tempVol = 255.;
                }*/
                //output[(y + (COLS_Y * i)) * pitch + x] = sum;
                surf3Dwrite((unsigned char)sum, surf, x, (y + (COLS_Y * i)), zLoc);

            }
            else {
                break;
            }
        }
    }
}

	.file	"tmpxft_00300ca1_00000000-6_filter.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2014:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2014:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z35__device_stub__Z9medKernelPcPKcmmmmPcPKcmmmm
	.type	_Z35__device_stub__Z9medKernelPcPKcmmmmPcPKcmmmm, @function
_Z35__device_stub__Z9medKernelPcPKcmmmmPcPKcmmmm:
.LFB2036:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	64(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%r9, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z9medKernelPcPKcmmmm(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L2:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z35__device_stub__Z9medKernelPcPKcmmmmPcPKcmmmm, .-_Z35__device_stub__Z9medKernelPcPKcmmmmPcPKcmmmm
	.globl	_Z9medKernelPcPKcmmmm
	.type	_Z9medKernelPcPKcmmmm, @function
_Z9medKernelPcPKcmmmm:
.LFB2037:
	.cfi_startproc
	endbr64
	jmp	_Z35__device_stub__Z9medKernelPcPKcmmmmPcPKcmmmm
	.cfi_endproc
.LFE2037:
	.size	_Z9medKernelPcPKcmmmm, .-_Z9medKernelPcPKcmmmm
	.globl	_Z16launch_medKernelRK4dim3S1_PcPKcmmmm
	.type	_Z16launch_medKernelRK4dim3S1_PcPKcmmmm, @function
_Z16launch_medKernelRK4dim3S1_PcPKcmmmm:
.LFB2009:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %rax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%r9, %r13
	xorl	%r9d, %r9d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r8, %r12
	xorl	%r8d, %r8d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rcx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdx, %rbx
	pushq	%r11
	.cfi_def_cfa_offset 64
	movq	(%rsi), %rdx
	movl	8(%rsi), %ecx
	movq	(%rdi), %rdi
	movl	8(%rax), %esi
	movq	64(%rsp), %r14
	movq	72(%rsp), %r15
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L8
	popq	%r10
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	movq	%r15, %r9
	movq	%r14, %r8
	movq	%r13, %rcx
	movq	%r12, %rdx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	_Z35__device_stub__Z9medKernelPcPKcmmmmPcPKcmmmm
.L8:
	.cfi_restore_state
	popq	%rax
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2009:
	.size	_Z16launch_medKernelRK4dim3S1_PcPKcmmmm, .-_Z16launch_medKernelRK4dim3S1_PcPKcmmmm
	.globl	_Z41__device_stub__Z15convolutionRowsPcPKcmmmPcPKcmmm
	.type	_Z41__device_stub__Z15convolutionRowsPcPKcmmmPcPKcmmm, @function
_Z41__device_stub__Z15convolutionRowsPcPKcmmmPcPKcmmm:
.LFB2038:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L11
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z15convolutionRowsPcPKcmmm(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L11:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2038:
	.size	_Z41__device_stub__Z15convolutionRowsPcPKcmmmPcPKcmmm, .-_Z41__device_stub__Z15convolutionRowsPcPKcmmmPcPKcmmm
	.globl	_Z15convolutionRowsPcPKcmmm
	.type	_Z15convolutionRowsPcPKcmmm, @function
_Z15convolutionRowsPcPKcmmm:
.LFB2039:
	.cfi_startproc
	endbr64
	jmp	_Z41__device_stub__Z15convolutionRowsPcPKcmmmPcPKcmmm
	.cfi_endproc
.LFE2039:
	.size	_Z15convolutionRowsPcPKcmmm, .-_Z15convolutionRowsPcPKcmmm
	.globl	_Z22launch_convolutionRowsRK4dim3S1_PcPKcmmm
	.type	_Z22launch_convolutionRowsRK4dim3S1_PcPKcmmm, @function
_Z22launch_convolutionRowsRK4dim3S1_PcPKcmmm:
.LFB2010:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movq	%rdi, %rax
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%r9, %r13
	xorl	%r9d, %r9d
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%r8, %r12
	xorl	%r8d, %r8d
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movq	%rcx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movq	%rdx, %rbx
	movl	8(%rsi), %ecx
	movq	(%rsi), %rdx
	movq	(%rdi), %rdi
	movl	8(%rax), %esi
	movq	48(%rsp), %r14
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L16
	movq	%r14, %r8
	movq	%r13, %rcx
	movq	%r12, %rdx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	_Z41__device_stub__Z15convolutionRowsPcPKcmmmPcPKcmmm
.L16:
	.cfi_restore_state
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2010:
	.size	_Z22launch_convolutionRowsRK4dim3S1_PcPKcmmm, .-_Z22launch_convolutionRowsRK4dim3S1_PcPKcmmm
	.globl	_Z40__device_stub__Z15convolutionColsyPcmmmmyPcmmmm
	.type	_Z40__device_stub__Z15convolutionColsyPcmmmmyPcmmmm, @function
_Z40__device_stub__Z15convolutionColsyPcmmmmyPcmmmm:
.LFB2040:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	64(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%r9, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z15convolutionColsyPcmmmm(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L19:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z40__device_stub__Z15convolutionColsyPcmmmmyPcmmmm, .-_Z40__device_stub__Z15convolutionColsyPcmmmmyPcmmmm
	.globl	_Z15convolutionColsyPcmmmm
	.type	_Z15convolutionColsyPcmmmm, @function
_Z15convolutionColsyPcmmmm:
.LFB2041:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z15convolutionColsyPcmmmmyPcmmmm
	.cfi_endproc
.LFE2041:
	.size	_Z15convolutionColsyPcmmmm, .-_Z15convolutionColsyPcmmmm
	.globl	_Z22launch_convolutionColsRK4dim3S1_yPcmmmm
	.type	_Z22launch_convolutionColsRK4dim3S1_yPcmmmm, @function
_Z22launch_convolutionColsRK4dim3S1_yPcmmmm:
.LFB2011:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %rax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%r9, %r13
	xorl	%r9d, %r9d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r8, %r12
	xorl	%r8d, %r8d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rcx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdx, %rbx
	pushq	%r11
	.cfi_def_cfa_offset 64
	movq	(%rsi), %rdx
	movl	8(%rsi), %ecx
	movq	(%rdi), %rdi
	movl	8(%rax), %esi
	movq	64(%rsp), %r14
	movq	72(%rsp), %r15
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	popq	%r10
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	movq	%r15, %r9
	movq	%r14, %r8
	movq	%r13, %rcx
	movq	%r12, %rdx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	_Z40__device_stub__Z15convolutionColsyPcmmmmyPcmmmm
.L24:
	.cfi_restore_state
	popq	%rax
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2011:
	.size	_Z22launch_convolutionColsRK4dim3S1_yPcmmmm, .-_Z22launch_convolutionColsRK4dim3S1_yPcmmmm
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z15convolutionColsyPcmmmm"
.LC1:
	.string	"_Z15convolutionRowsPcPKcmmm"
.LC2:
	.string	"_Z9medKernelPcPKcmmmm"
.LC3:
	.string	"gaussK"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2043:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC0(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z15convolutionColsyPcmmmm(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC1(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z15convolutionRowsPcPKcmmm(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC2(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z9medKernelPcPKcmmmm(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC3(%rip), %rdx
	movl	$20, %r9d
	leaq	_ZL6gaussK(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2043:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata
	.align 16
	.type	_ZL6gaussK, @object
	.size	_ZL6gaussK, 20
_ZL6gaussK:
	.long	1038402296
	.long	1047670030
	.long	1050374518
	.long	1047670030
	.long	1038402296
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#pragma once
#include <hip/hip_runtime.h>

#ifndef RAYCAST_FILTER_CUH
#define RAYCAST_FILTER_CUH

#include <hip/hip_runtime_api.h>

//Median filter defines
__device__ inline void s(char* a, char* b) {
    char tmp;
    if (*a > *b) {
        tmp = *b;
        *b = *a;
        *a = tmp;
    }
}

#define min3(a, b, c)                   s(a,b); s(a,c);
#define max3(a, b, c)                   s(b,c); s(a,c);
#define min4(a, b, c, d)                s(a,b); s(c,d); s(a,c);
#define max4(a, b, c, d)                s(c,d); s(a,b); s(b,d);
#define min5(a, b, c, d, e)             s(a,b); s(c,d); min3(a, c, e);
#define max5(a, b, c, d, e)             s(a,b); s(c,d); max3(b, d, e);
#define min6(a, b, c, d, e, f)          s(a,b); s(c,d); s(e,f); min3(a, c, e);
#define max6(a, b, c, d, e, f)          s(a,b); s(c,d); s(e,f); max3(b, d, f);
#define min7(a, b, c, d, e, f, g)       s(a,b); s(c,d); s(e,f); min4(a, c, e, g);
#define max7(a, b, c, d, e, f, g)       s(a,b); s(c,d); s(e,f); max4(b, d, f, g);
#define min8(a, b, c, d, e, f, g, h)    s(a,b); s(c,d); s(e,f); s(g,h); min4(a, c, e, g);
#define max8(a, b, c, d, e, f, g, h)    s(a,b); s(c,d); s(e,f); s(g,h); max4(b, d, f, h);

#define minmax3(a, b, c)                                         max3(a,b,c); s(a,b);
#define minmax4(a, b, c, d)                                      s(a,b); s(c,d); s(a,c); s(b,d);
#define minmax5(a, b, c, d, e)                                   s(a,b); s(c,d); min3(a, c, e) max3(b, d, e);
#define minmax6(a, b, c, d, e, f)                                s(a,b); s(c,d); s(e,f); min3(a, c, e); max3(b, d, f);
#define minmax7(a, b, c, d, e, f, g)                             s(a,b); s(c,d); s(e,f); min4(a, c, e, g); max4(b, d, f, g);
#define minmax8(a, b, c, d, e, f, g, h)                          s(a,b); s(c,d); s(e,f); s(g,h); min4(a, c, e, g); max4(b, d, f, h);
#define minmax9(a, b, c, d, e, f, g, h, i)                       s(a,b); s(c,d); s(e,f); s(g,h); min5(a, c, e, g, i); max5(b, d, f, h, i);
#define minmax10(a, b, c, d, e, f, g, h, i, j)                   s(a,b); s(c,d); s(e,f); s(g,h); s(i,j); min5(a, c, e, g, i); max5(b, d, f, h, j);
#define minmax11(a, b, c, d, e, f, g, h, i, j, k)                s(a,b); s(c,d); s(e,f); s(g,h); s(i,j); min6(a, c, e, g, i, k); max6(b, d, f, h, j, k);
#define minmax12(a, b, c, d, e, f, g, h, i, j, k, l)             s(a,b); s(c,d); s(e,f); s(g,h); s(i,j); s(k,l); min6(a, c, e, g, i, k); max6(b, d, f, h, j, l);
#define minmax13(a, b, c, d, e, f, g, h, i, j, k, l, m)          s(a,b); s(c,d); s(e,f); s(g,h); s(i,j); s(k,l); min7(a, c, e, g, i, k, m); max7(b, d, f, h, j, l, m);
#define minmax14(a, b, c, d, e, f, g, h, i, j, k, l, m, n)       s(a,b); s(c,d); s(e,f); s(g,h); s(i,j); s(k,l); s(m,n); min7(a, c, e, g, i, k, m); max7(b, d, f, h, j, l, n);
#define minmax15(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)    s(a,b); s(c,d); s(e,f); s(g,h); s(i,j); s(k,l); s(m,n); min8(a, c, e, g, i, k, m, o); max8(b, d, f, h, j, l, n, o);


////////////////////Filtering methods////////////////////////////
//Performs 3d median filter on 16 bscans
__global__ void medKernel(char *out, const char *in, size_t dimy, size_t dimx, size_t dimz, size_t pitch);
const int blockSizeRow = 32;
const int blockSizeCol = 16;


//Filters using a gaussian kernel
#define ROWS_Y 8
#define COLS_X 32
#define ROWS_X 8
#define COLS_Y 32
#define PIX_PER_THREAD 8
#define KERNEL_RADIUS 2
//gauss filter defines
static __device__ const float gaussK[2 * KERNEL_RADIUS + 1] = { 0.1117f, 0.2365f, 0.3036f, 0.2365f, 0.1117f };
__global__ void convolutionRows(char* output, const char* input, size_t imageW, size_t imageH, size_t pitch);
__global__ void convolutionCols(hipSurfaceObject_t surf, char* input, size_t imageW, size_t imageH, size_t pitch, size_t zLoc);

void launch_medKernel(const dim3& blocks, const dim3& threads, char *out, const char *in, size_t dimy, size_t dimx, size_t dimz, size_t pitch);
void launch_convolutionRows(const dim3& blocks, const dim3& threads, char* output, const char* input, size_t imageW, size_t imageH, size_t pitch);
void launch_convolutionCols(const dim3& blocks, const dim3& threads, hipSurfaceObject_t surf, char* input, size_t imageW, size_t imageH, size_t pitch, size_t zLoc);

#endif

	.text
	.file	"filter.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
