summaryrefslogtreecommitdiffstats
path: root/src/kernels/stacked-forwardproject.cl
blob: c37eb536ab6b76ecf9c83df17493064553e91ee6 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright (C) 2011-2013 Karlsruhe Institute of Technology
 *
 * This file is part of Ufo.
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

 constant sampler_t volumeSampler = CLK_NORMALIZED_COORDS_FALSE |
                                    CLK_ADDRESS_CLAMP |
                                    CLK_FILTER_NEAREST;

 kernel void
 interleave_single ( global float *slices,
                     write_only image2d_array_t interleaved_slices)
 {
     const int idx = get_global_id(0);
     const int idy = get_global_id(1);
     const int idz = get_global_id(2);
     const int sizex = get_global_size(0);
     const int sizey = get_global_size(1);

     int slice_offset = idz*2;

     float x = slices[idx + idy * sizex + (slice_offset) * sizex * sizey];
     float y = slices[idx + idy * sizex + (slice_offset+1) * sizex * sizey];

     write_imagef(interleaved_slices, (int4)(idx, idy, idz, 0),(float4)(x,y,0.0f,0.0f));
 }

 kernel void
   texture_single (
          read_only image2d_array_t slices,
          global float2 *reconstructed_buffer,
          float axis_pos,
          float angle_step,
          unsigned long size){

        const int idx = get_global_id(0);
        const int idy = get_global_id(1);
        const int idz = get_global_id(2);

        float angle = idy * angle_step;
        float r = fmin (axis_pos, size - axis_pos);
        float d = idx - axis_pos + 0.5f;
        float l = sqrt(8.0f*r*r - 4.0f*d*d);

        float2 D = (float2) (cos(angle), -sin(angle));

        D = normalize(D);

        float2 N = (float2) (D.y, -D.x);

        float2 sample = d * D - l/2.0f * N + ((float2) (axis_pos, axis_pos));

        float2 sum = {0.0f,0.0f};

        for (int i = 0; i < l; i++) {
            sum += read_imagef(slices, volumeSampler, (float4)((float2)sample,idz,0.0f)).xy;
            sample += N;
        }

        reconstructed_buffer[idx + idy*size + idz*size*size] = sum;
  }


 kernel void
 uninterleave_single (global float2 *reconstructed_buffer,
                 global float *output)
 {
     const int idx = get_global_id(0);
     const int idy = get_global_id(1);
     const int idz = get_global_id(2);
     const int sizex = get_global_size(0);
     const int sizey = get_global_size(1);
     int output_offset = idz*2;

     output[idx + idy*sizex + (output_offset)*sizex*sizey] = reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].x;
     output[idx + idy*sizex + (output_offset+1)*sizex*sizey] = reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].y;
 }

  kernel void
  interleave_half ( global float *slices,
                    write_only image2d_array_t interleaved_slices)
  {
      const int idx = get_global_id(0);
      const int idy = get_global_id(1);
      const int idz = get_global_id(2);

      const int sizex = get_global_size(0);
      const int sizey = get_global_size(1);

      int slice_offset = idz*4;

      float4 b = {slices[idx + idy * sizex + (slice_offset) * sizex * sizey],
                  slices[idx + idy * sizex + (slice_offset+1) * sizex * sizey],
                  slices[idx + idy * sizex + (slice_offset+2) * sizex * sizey],
                  slices[idx + idy * sizex + (slice_offset+3) * sizex * sizey]};

      write_imagef(interleaved_slices, (int4)(idx, idy, idz, 0),(float4)(b));
  }

kernel void
texture_half (
        read_only image2d_array_t slices,
        global float4 *reconstructed_buffer,
        float axis_pos,
        float angle_step,
        unsigned long size){

      const int idx = get_global_id(0);
      const int idy = get_global_id(1);
      const int idz = get_global_id(2);

      float angle = idy * angle_step;
      float r = fmin (axis_pos, size - axis_pos);
      float d = idx - axis_pos + 0.5f;
      float l = sqrt(8.0f*r*r - 4.0f*d*d);

      float2 D = (float2) (cos(angle), -sin(angle));

      D = normalize(D);

      float2 N = (float2) (D.y, -D.x);

      float2 sample = d * D - l/2.0f * N + ((float2) (axis_pos, axis_pos));

      float4 sum = {0.0f,0.0f,0.0f,0.0f};

      for (int i = 0; i < l; i++) {
          sum += read_imagef(slices, volumeSampler, (float4)((float2)sample,idz,0.0f));
          sample += N;
      }

      reconstructed_buffer[idx + idy*size + idz*size*size] = sum;
}

 kernel void
 uninterleave_half (global float4 *reconstructed_buffer,
                 global float *output)
 {
     const int idx = get_global_id(0);
     const int idy = get_global_id(1);
     const int idz = get_global_id(2);

     const int sizex = get_global_size(0);
     const int sizey = get_global_size(1);

     int output_offset = idz*4;

     output[idx + idy*sizex + (output_offset)*sizex*sizey] = reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].x;
     output[idx + idy*sizex + (output_offset+1)*sizex*sizey] = reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].y;
     output[idx + idy*sizex + (output_offset+2)*sizex*sizey] = reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].z;
     output[idx + idy*sizex + (output_offset+3)*sizex*sizey] = reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].w;
 }

 union converter {
     uint2 storage;
     uchar8 a;
 };

  kernel void
  interleave_uint ( global float *slices,
                    write_only image2d_array_t interleaved_slices,
                    const float min,
                    const float max)
  {
    const int idx = get_global_id(0);
    const int idy = get_global_id(1);
    const int idz = get_global_id(2);

    const int sizex = get_global_size(0);
    const int sizey = get_global_size(1);

    int slice_offset = idz*8;

    const float scale = 255.0f / (max - min);

    union converter il;
    il.a.s0 = (slices[idx + idy * sizex + (slice_offset) * sizex * sizey] - min)*scale;
    il.a.s1 = (slices[idx + idy * sizex + (slice_offset+1) * sizex * sizey] - min)*scale;
    il.a.s2 = (slices[idx + idy * sizex + (slice_offset+2) * sizex * sizey] - min)*scale;
    il.a.s3 = (slices[idx + idy * sizex + (slice_offset+3) * sizex * sizey] - min)*scale;
    il.a.s4 = (slices[idx + idy * sizex + (slice_offset+4) * sizex * sizey] - min)*scale;
    il.a.s5 = (slices[idx + idy * sizex + (slice_offset+5) * sizex * sizey] - min)*scale;
    il.a.s6 = (slices[idx + idy * sizex + (slice_offset+6) * sizex * sizey] - min)*scale;
    il.a.s7 = (slices[idx + idy * sizex + (slice_offset+7) * sizex * sizey] - min)*scale;

    write_imageui(interleaved_slices, (int4)(idx, idy, idz, 0),(uint4)((uint)il.storage.x,(uint)il.storage.y,0,0));
  }

kernel void
texture_uint (
        read_only image2d_array_t slices,
        global uint8 *reconstructed_buffer,
        float axis_pos,
        float angle_step,
        unsigned long size){

      const int idx = get_global_id(0);
      const int idy = get_global_id(1);
      const int idz = get_global_id(2);

      float angle = idy * angle_step;
      float r = fmin (axis_pos, size - axis_pos);
      float d = idx - axis_pos + 0.5f;
      float l = sqrt(8.0f*r*r - 4.0f*d*d);

      float2 D = (float2) (cos(angle), -sin(angle));

      D = normalize(D);

      float2 N = (float2) (D.y, -D.x);

      float2 sample = d * D - l/2.0f * N + ((float2) (axis_pos, axis_pos));

      uint8 sum = {0,0,0,0,0,0,0,0};

      union converter tex;
      for (int i = 0; i < l; i++) {
          tex.storage = read_imageui(slices, volumeSampler, (float4)((float2)sample,idz,0.0f)).xy;

         sum.s0 += (uint)tex.a.s0;
         sum.s1 += (uint)tex.a.s1;
         sum.s2 += (uint)tex.a.s2;
         sum.s3 += (uint)tex.a.s3;
         sum.s4 += (uint)tex.a.s4;
         sum.s5 += (uint)tex.a.s5;
         sum.s6 += (uint)tex.a.s6;
         sum.s7 += (uint)tex.a.s7;

         sample += N;
      }

      reconstructed_buffer[idx + idy*size + idz*size*size] = sum;
}

 kernel void
 uninterleave_uint (global uint8 *reconstructed_buffer,
                    global float *output,
                    const float min,
                    const float max)
 {
     const int idx = get_global_id(0);
     const int idy = get_global_id(1);
     const int idz = get_global_id(2);

     const int sizex = get_global_size(0);
     const int sizey = get_global_size(1);

     int output_offset = idz*8;
     float scale = (max-min)/255.0f;

     output[idx + idy*sizex + (output_offset)*sizex*sizey] = (reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].s0)*scale+min;
     output[idx + idy*sizex + (output_offset+1)*sizex*sizey] = (reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].s1)*scale+min;
     output[idx + idy*sizex + (output_offset+2)*sizex*sizey] = (reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].s2)*scale+min;
     output[idx + idy*sizex + (output_offset+3)*sizex*sizey] = (reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].s3)*scale+min;
     output[idx + idy*sizex + (output_offset+4)*sizex*sizey] = (reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].s4)*scale+min;
     output[idx + idy*sizex + (output_offset+5)*sizex*sizey] = (reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].s5)*scale+min;
     output[idx + idy*sizex + (output_offset+6)*sizex*sizey] = (reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].s6)*scale+min;
     output[idx + idy*sizex + (output_offset+7)*sizex*sizey] = (reconstructed_buffer[idx + idy*sizex + idz*sizex*sizey].s7)*scale+min;
 }