summaryrefslogtreecommitdiff
path: root/opencl/box-blur.cl
blob: bb85bcb2eeec5d68f5db59d4d96a19a5e535f7a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
__kernel void kernel_blur_hor (__global const float4     *in,
                               __global       float4     *aux,
                               int width, int radius)
{
  const int in_index = get_global_id(0) * (width + 2 * radius)
                       + (radius + get_global_id (1));

  const int aux_index = get_global_id(0) * width + get_global_id (1);
  int i;
  float4 mean;

  mean = (float4)(0.0f);

  if (get_global_id(1) < width)
    {
      for (i=-radius; i <= radius; i++)
        {
          mean += in[in_index + i];
        }
      aux[aux_index] = mean / (float)(2 * radius + 1);
    }
}

__kernel void kernel_blur_ver (__global const float4     *aux,
                               __global       float4     *out,
                               int width, int radius)
{
  const int out_index = get_global_id(0) * width + get_global_id (1);
  int i;
  float4 mean;

  mean = (float4)(0.0f);
  int aux_index = get_global_id(0) * width + get_global_id (1);

  if (get_global_id(1) < width)
    {
      for (i=-radius; i <= radius; i++)
        {
          mean += aux[aux_index];
          aux_index += width;
        }
      out[out_index] = mean / (float)(2 * radius + 1);
    }
}

__kernel
__attribute__((reqd_work_group_size(256,1,1)))
void kernel_box_blur_fast (__global const float4 *in,
                           __global       float4 *out,
                           __local        float4 *column_sum,
                                    const int     width,
                                    const int     height,
                                    const int     radius,
                                    const int     size)
{
  const int local_id0 = get_local_id(0);
  const int twice_radius = 2 * radius;
  const int in_width = twice_radius + width;
  const int in_height = twice_radius + height;
  const float4 area = (float4)( (twice_radius + 1) * (twice_radius + 1) );
  int column_index_start,column_index_end;
  int y = get_global_id(1) * size;
  const int out_x = get_group_id(0)
                    * ( get_local_size(0) - twice_radius ) + local_id0 - radius;
  const int in_x = out_x + radius;
  int tmp_size = size;
  int tmp_index = 0;
  float4 tmp_sum = (float4)0.0f;
  float4 total_sum = (float4)0.0f;

  if (in_x < in_width)
    {
      column_index_start = y;
      column_index_end = y + twice_radius;

      for (int i = 0; i < twice_radius + 1; ++i)
        {
          tmp_sum += in[(y + i) * in_width + in_x];
        }

      column_sum[local_id0] = tmp_sum;
    }

  barrier(CLK_LOCAL_MEM_FENCE);

  while (1)
    {
      if (out_x < width)
        {
          if (local_id0 >= radius && local_id0 < get_local_size(0) - radius)
            {
                total_sum = (float4)0.0f;

                for (int i = 0; i < twice_radius + 1; ++i)
                  {
                    total_sum += column_sum[local_id0 - radius + i];
                  }

                out[y * width + out_x] = total_sum / area;
            }
        }

    if (--tmp_size == 0 || y == height - 1)
      break;

    barrier(CLK_LOCAL_MEM_FENCE);

    ++y;

    if (in_x < in_width)
      {
        tmp_sum = column_sum[local_id0];
        tmp_sum -= in[(column_index_start)   * in_width + in_x];
        tmp_sum += in[(column_index_end + 1) * in_width + in_x];
        ++column_index_start;
        ++column_index_end;
        column_sum[local_id0] = tmp_sum;
      }

    barrier(CLK_LOCAL_MEM_FENCE);
  }
}