aboutsummaryrefslogtreecommitdiff
path: root/src/core/GLES_COMPUTE/cs_shaders/fill_border.cs
blob: 01a39866c7541a2d460ef7d0ebd1c37a91ce9eeb (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in;
#include "helpers.h"

#if defined(DATA_TYPE_FP32)
#ifdef FILL_IMAGE_BORDERS_REPLICATE
BUFFER_DECLARATION(buf, 1, float, restrict);
layout(std140) uniform shader_params
{
    TENSOR3D_PARAM_DECLARATION(buf);
    uint width;
    uint height;
    int  start_pos_x;
    int  start_pos_y;
};

/** Fill N pixel of the padding edge of a single channel image by replicating the closest valid pixel.
 *
 * @attention  The border size for top, bottom, left, right needs to be passed at the compile time.
 * e.g. BORDER_SIZE_TOP=0 BORDER_SIZE_BOTTOM=2 BORDER_SIZE_LEFT=0 BORDER_SIZE_RIGHT=2
 *
 * @param[in,out] buf_ptr                           Pointer to the source image. Supported data types: F32
 * @param[in]     buf_stride_x                      Stride of the source image in X dimension (in bytes)
 * @param[in]     buf_step_x                        buf_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     buf_stride_y                      Stride of the source image in Y dimension (in bytes)
 * @param[in]     buf_step_y                        buf_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     buf_stride_z                      Stride between images if batching images (in bytes)
 * @param[in]     buf_step_z                        buf_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     buf_offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in]     width                             Width of the valid region of the image
 * @param[in]     height                            Height of the valid region of the image
 * @param[in]     start_pos_x                       X coordinate indicating the start point of the valid region
 * @param[in]     start_pos_y                       Y coordinate indicating the start point of the valid region
 */
void main()
{
    Image buf = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(buf);

    // Update pointer to point to the starting point of the valid region
    buf.current_offset = uint(int(buf.current_offset) + ((start_pos_y * int(buf_stride_y) + start_pos_x * int(buf_stride_x)) >> 2));

    int total_width = BORDER_SIZE_LEFT + int(width) + BORDER_SIZE_RIGHT;
    int gid0        = int(gl_GlobalInvocationID.x);
    int gidH        = gid0 - total_width;
    int gidW        = gid0 - BORDER_SIZE_LEFT;

    if(gidH >= 0)
    {
        // Handle left border
        float left_val = LOAD4(buf, offset(buf, 0, gidH));
        for(int i = -BORDER_SIZE_LEFT; i < 0; ++i)
        {
            STORE4(buf, offset(buf, i, gidH), left_val);
        }
        // Handle right border
        float right_val = LOAD4(buf, offset(buf, int(width) - 1, gidH));
        for(int i = 0; i < BORDER_SIZE_RIGHT; ++i)
        {
            STORE4(buf, offset(buf, int(width) + i, gidH), right_val);
        }
    }
    else
    {
        // Get value for corners
        int val_idx = gidW;
        if(gidW < 0 || gidW > (int(width) - 1))
        {
            val_idx = gidW < 0 ? 0 : int(width) - 1;
        }

        // Handle top border
        float top_val = LOAD4(buf, offset(buf, val_idx, 0));
        for(int i = -BORDER_SIZE_TOP; i < 0; ++i)
        {
            STORE4(buf, offset(buf, gidW, i), top_val);
        }
        // Handle bottom border
        float bottom_val = LOAD4(buf, offset(buf, val_idx, int(height) - 1));
        for(int i = 0; i < BORDER_SIZE_BOTTOM; ++i)
        {
            STORE4(buf, offset(buf, gidW, int(height) + i), bottom_val);
        }
    }
}
#endif /* FILL_IMAGE_BORDERS_REPLICATE */

#ifdef FILL_IMAGE_BORDERS_CONSTANT
BUFFER_DECLARATION(buf, 1, float, writeonly);
layout(std140) uniform shader_params
{
    TENSOR3D_PARAM_DECLARATION(buf);
    uint  width;
    uint  height;
    int   start_pos_x;
    int   start_pos_y;
    float constant_value;
};

/** Fill N pixels of the padding edge of a single channel image with a constant value.
 *
 * @attention  The border size for top, bottom, left, right needs to be passed at the compile time.
 * e.g. BORDER_SIZE_TOP=0 BORDER_SIZE_BOTTOM=2 BORDER_SIZE_LEFT=0 BORDER_SIZE_RIGHT=2
 *
 * @param[out] buf_ptr                           Pointer to the source image. Supported data types: F32
 * @param[in]  buf_stride_x                      Stride of the source image in X dimension (in bytes)
 * @param[in]  buf_step_x                        buf_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  buf_stride_y                      Stride of the source image in Y dimension (in bytes)
 * @param[in]  buf_step_y                        buf_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  buf_offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in]  width                             Width of the valid region of the image
 * @param[in]  height                            Height of the valid region of the image
 * @param[in]  start_pos_x                       X coordinate indicating the start point of the valid region
 * @param[in]  start_pos_y                       Y coordinate indicating the start point of the valid region
 * @param[in]  constant_value                    Constant value to use to fill the edges
 */
void main()
{
    Image buf = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(buf);

    // Update pointer to point to the starting point of the valid region
    buf.current_offset = uint(int(buf.current_offset) + ((start_pos_y * int(buf_stride_y) + start_pos_x * int(buf_stride_x)) >> 2));

    int total_width = BORDER_SIZE_LEFT + int(width) + BORDER_SIZE_RIGHT;
    int gid0        = int(gl_GlobalInvocationID.x);
    int gidH        = gid0 - total_width;
    int gidW        = gid0 - BORDER_SIZE_LEFT;

    if(gidH >= 0)
    {
        // Handle left border
        for(int i = -BORDER_SIZE_LEFT; i < 0; ++i)
        {
            STORE1(buf, offset(buf, i, gidH), constant_value);
        }
        // Handle right border
        for(int i = 0; i < BORDER_SIZE_RIGHT; ++i)
        {
            STORE1(buf, offset(buf, int(width) + i, gidH), constant_value);
        }
    }
    else
    {
        // Handle top border
        for(int i = -BORDER_SIZE_TOP; i < 0; ++i)
        {
            STORE1(buf, offset(buf, gidW, i), constant_value);
        }
        // Handle bottom border
        for(int i = 0; i < BORDER_SIZE_BOTTOM; ++i)
        {
            STORE1(buf, offset(buf, gidW, int(height) + i), constant_value);
        }
    }
}
#endif /* FILL_IMAGE_BORDERS_CONSTANT */

#elif defined(DATA_TYPE_FP16)
precision mediump float;

#ifdef FILL_IMAGE_BORDERS_REPLICATE
BUFFER_DECLARATION(buf, 1, uint, restrict);
layout(std140) uniform shader_params
{
    TENSOR3D_PARAM_DECLARATION(buf);
    uint width;
    uint height;
    int  start_pos_x;
    int  start_pos_y;
};

void set_replicate(uint offset, int pos, uint replicate_value)
{
    uint packed_b;
    LOAD1(packed_b, buf, offset);

    vec2 b = unpackHalf2x16(packed_b);
    vec2 c = unpackHalf2x16(replicate_value);

    if(pos % 2 == 0)
    {
        b.x = c.y;
    }
    else
    {
        b.y = c.x;
    }

    packed_b = packHalf2x16(b);

    STORE1(buf, offset, packed_b);
}

/** Fill N pixel of the padding edge of a single channel image by replicating the closest valid pixel.
 *
 * @attention  The border size for top, bottom, left, right needs to be passed at the compile time.
 * e.g. BORDER_SIZE_TOP=0 BORDER_SIZE_BOTTOM=2 BORDER_SIZE_LEFT=0 BORDER_SIZE_RIGHT=2
 *
 * @param[in,out] buf_ptr                           Pointer to the source image. Supported data types: F16
 * @param[in]     buf_stride_x                      Stride of the source image in X dimension (in bytes)
 * @param[in]     buf_step_x                        buf_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     buf_stride_y                      Stride of the source image in Y dimension (in bytes)
 * @param[in]     buf_step_y                        buf_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     buf_stride_z                      Stride between images if batching images (in bytes)
 * @param[in]     buf_step_z                        buf_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     buf_offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in]     width                             Width of the valid region of the image
 * @param[in]     height                            Height of the valid region of the image
 * @param[in]     start_pos_x                       X coordinate indicating the start point of the valid region
 * @param[in]     start_pos_y                       Y coordinate indicating the start point of the valid region
 */
void main()
{
    Image buf = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP_FP16(buf);

    // Update pointer to point to the starting point of the valid region
    buf.current_offset = uint(buf.current_offset + uint(start_pos_y) * buf_stride_y + uint(start_pos_x) * buf_stride_x);

    int total_width = BORDER_SIZE_LEFT + int(width) + BORDER_SIZE_RIGHT;
    int gid0        = int(gl_GlobalInvocationID.x);
    int gidH        = gid0 - total_width;
    int gidW        = gid0 - BORDER_SIZE_LEFT;

    if(gidH >= 0)
    {
        // Handle left border
        uint left_val;
        LOAD1(left_val, buf, offset_fp16(buf, 0, gidH) >> uint(2));
        for(int i = -BORDER_SIZE_LEFT; i < 0; ++i)
        {
            uint offset = offset_fp16(buf, i, gidH) >> 2;
            int  pos    = i + BORDER_SIZE_LEFT;
            if(i == -1)
            {
                if(pos % 2 == 0)
                {
                    set_replicate(offset, pos, left_val);
                }
            }
            else
            {
                if(pos % 2 == 0)
                {
                    vec2 a = unpackHalf2x16(left_val);
                    uint b = packHalf2x16(a.xx);
                    STORE1(buf, offset, b);
                }
            }
        }
        // Handle right border
        uint right_val;
        LOAD1(right_val, buf, offset_fp16(buf, int(width) - 1, gidH) >> uint(2));
        for(int i = 0; i < BORDER_SIZE_RIGHT; ++i)
        {
            uint offset = offset_fp16(buf, int(width) + i, gidH) >> 2;
            int  pos    = i + BORDER_SIZE_LEFT + int(width);

            if(i == 0)
            {
                if(pos % 2 == 0)
                {
                    vec2 a = unpackHalf2x16(right_val);
                    uint b = packHalf2x16(a.yy);
                    STORE1(buf, offset, b);
                }
                else
                {
                    set_replicate(offset, pos, right_val);
                }
            }
            else
            {
                if(pos % 2 == 0)
                {
                    vec2 a = unpackHalf2x16(right_val);
                    uint b = packHalf2x16(a.yy);
                    STORE1(buf, offset, b);
                }
            }
        }
    }
    else
    {
        // Get value for corners
        int val_idx = gidW;
        if(gidW < 0 || (gidW > (int(width) - 1)))
        {
            val_idx = gidW < 0 ? 0 : (int(width) - 1);
        }

        // Handle top border
        uint top_val;
        LOAD1(top_val, buf, offset_fp16(buf, val_idx, 0) >> uint(2));
        for(int i = -BORDER_SIZE_TOP; i < 0; ++i)
        {
            uint offset = offset_fp16(buf, gidW, i) >> 2;

            if(gid0 % 2 == 0)
            {
                if(gidW == (int(width) - 1))
                {
                    vec2 a = unpackHalf2x16(top_val);
                    uint b = packHalf2x16(a.xx);
                    STORE1(buf, offset, b);
                }
                else
                {
                    if(gidW < 0)
                    {
                        vec2 a = unpackHalf2x16(top_val);
                        uint b;
                        if(BORDER_SIZE_LEFT % 2 == 0)
                        {
                            b = packHalf2x16(a.xx);
                        }
                        else
                        {
                            b = packHalf2x16(a.yy);
                        }
                        STORE1(buf, offset, b);
                    }
                    else if(gidW >= int(width))
                    {
                        vec2 a = unpackHalf2x16(top_val);
                        uint b;
                        if((BORDER_SIZE_LEFT + int(width)) % 2 == 0)
                        {
                            b = packHalf2x16(a.yy);
                        }
                        STORE1(buf, offset, b);
                    }
                    else
                    {
                        STORE1(buf, offset, top_val);
                    }
                }
            }
        }
        // Handle bottom border
        uint bottom_val;
        LOAD1(bottom_val, buf, offset_fp16(buf, val_idx, int(height) - 1) >> uint(2));
        for(int i = 0; i < BORDER_SIZE_BOTTOM; ++i)
        {
            uint offset = offset_fp16(buf, gidW, int(height) + i) >> 2;

            if(gid0 % 2 == 0)
            {
                if(gidW == (int(width) - 1))
                {
                    vec2 a = unpackHalf2x16(bottom_val);
                    uint b = packHalf2x16(a.xx);
                    STORE1(buf, offset, b);
                }
                else
                {
                    if(gidW < 0)
                    {
                        vec2 a = unpackHalf2x16(bottom_val);
                        uint b;
                        if(BORDER_SIZE_LEFT % 2 == 0)
                        {
                            b = packHalf2x16(a.xx);
                        }
                        else
                        {
                            b = packHalf2x16(a.yy);
                        }
                        STORE1(buf, offset, b);
                    }
                    else if(gidW >= int(width))
                    {
                        vec2 a = unpackHalf2x16(bottom_val);
                        uint b;
                        if((BORDER_SIZE_LEFT + int(width)) % 2 == 0)
                        {
                            b = packHalf2x16(a.yy);
                        }
                        STORE1(buf, offset, b);
                    }
                    else
                    {
                        STORE1(buf, offset, bottom_val);
                    }
                }
            }
        }
    }
}
#endif /* FILL_IMAGE_BORDERS_REPLICATE */

#ifdef FILL_IMAGE_BORDERS_CONSTANT
BUFFER_DECLARATION(buf, 1, uint, restrict);

layout(std140) uniform shader_params
{
    TENSOR3D_PARAM_DECLARATION(buf);
    uint  width;
    uint  height;
    int   start_pos_x;
    int   start_pos_y;
    float constant_value;
};

void set_constant(uint offset, int pos)
{
    uint packed_b;
    LOAD1(packed_b, buf, offset);

    vec2 b = unpackHalf2x16(packed_b);

    if(pos % 2 == 0)
    {
        b.x = constant_value;
    }
    else
    {
        b.y = constant_value;
    }

    packed_b = packHalf2x16(b);

    STORE1(buf, offset, packed_b);
}

/** Fill N pixels of the padding edge of a single channel image with a constant value.
 *
 * @attention  The border size for top, bottom, left, right needs to be passed at the compile time.
 * e.g. BORDER_SIZE_TOP=0 BORDER_SIZE_BOTTOM=2 BORDER_SIZE_LEFT=0 BORDER_SIZE_RIGHT=2
 *
 * @param[out] buf_ptr                           Pointer to the source image. Supported data types: F16
 * @param[in]  buf_stride_x                      Stride of the source image in X dimension (in bytes)
 * @param[in]  buf_step_x                        buf_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  buf_stride_y                      Stride of the source image in Y dimension (in bytes)
 * @param[in]  buf_step_y                        buf_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  buf_offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in]  width                             Width of the valid region of the image
 * @param[in]  height                            Height of the valid region of the image
 * @param[in]  start_pos_x                       X coordinate indicating the start point of the valid region
 * @param[in]  start_pos_y                       Y coordinate indicating the start point of the valid region
 * @param[in]  constant_value                    Constant value to use to fill the edges
 */
void main()
{
    Image buf = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP_FP16(buf);

    int total_width = BORDER_SIZE_LEFT + int(width) + BORDER_SIZE_RIGHT;
    int gid0        = int(gl_GlobalInvocationID.x);
    int gidH        = gid0 - total_width;
    int gidW        = gid0 - BORDER_SIZE_LEFT;

    // Update pointer to point to the starting point of the valid region
    buf.current_offset = uint(int(buf.current_offset) + ((start_pos_y * int(buf_stride_y) + start_pos_x * int(buf_stride_x))));

    vec2 b = vec2(constant_value, constant_value);

    uint packed_b = packHalf2x16(b);

    if(gidH >= 0)
    {
        // Handle left border
        for(int i = -BORDER_SIZE_LEFT; i < 0; ++i)
        {
            uint offset = offset_fp16(buf, i, gidH) >> 2;
            int  pos    = i + BORDER_SIZE_LEFT;

            if(i == -1)
            {
                if(pos % 2 == 0)
                {
                    set_constant(offset, pos);
                }
            }
            else
            {
                if(pos % 2 == 0)
                {
                    STORE1(buf, offset, packed_b);
                }
            }
        }
        // Handle right border
        for(int i = 0; i < BORDER_SIZE_RIGHT; ++i)
        {
            uint offset = offset_fp16(buf, int(width) + i, gidH) >> 2;
            int  pos    = i + BORDER_SIZE_LEFT + int(width);

            if(i == 0)
            {
                if(pos % 2 == 0)
                {
                    STORE1(buf, offset, packed_b);
                }
                else
                {
                    set_constant(offset, pos);
                }
            }
            else
            {
                if(pos % 2 == 0)
                {
                    STORE1(buf, offset, packed_b);
                }
            }
        }
    }
    else
    {
        // Handle top border
        for(int i = -BORDER_SIZE_TOP; i < 0; ++i)
        {
            uint offset = offset_fp16(buf, gidW, i) >> 2;

            if(gid0 % 2 == 0)
            {
                STORE1(buf, offset, packed_b);
            }
        }
        // Handle bottom border
        for(int i = 0; i < BORDER_SIZE_BOTTOM; ++i)
        {
            uint offset = offset_fp16(buf, gidW, int(height) + i) >> 2;

            if(gid0 % 2 == 0)
            {
                STORE1(buf, offset, packed_b);
            }
        }
    }
}
#endif /* FILL_IMAGE_BORDERS_CONSTANT */
#endif /* DATA_TYPE_FP32 */