blob: 3b3118bc3d7748867eae845c7b487a1921a291a6 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/GLES_COMPUTE/kernels/GCFillBorderKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
29#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
30#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010036
37#include <cstdint>
38#include <set>
39#include <string>
40
41using namespace arm_compute;
42
43GCFillBorderKernel::GCFillBorderKernel()
44 : IGCKernel(), _tensor(nullptr)
45{
46}
47
48bool GCFillBorderKernel::is_parallelisable() const
49{
50 return false;
51}
52
53template <class T>
54void GCFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value)
55{
56 T value;
57 constant_border_value.get(value);
Joel Liangf1f3ebd2017-11-10 09:59:19 +080058 _kernel.set_argument(idx, static_cast<T>(value));
Anthony Barbier7068f992017-10-26 15:23:08 +010059}
60
61void GCFillBorderKernel::configure(const IGCTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
62{
63 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
64 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(tensor, 1, DataType::F32, DataType::F16);
65 ARM_COMPUTE_ERROR_ON(tensor->info()->num_channels() != 1);
66
67 border_size.limit(tensor->info()->padding());
68
69 // If there is no border: early exit
70 if(border_size.empty() || border_mode == BorderMode::UNDEFINED)
71 {
72 return;
73 }
74
75 // Select appropriate kernel
76 std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode));
77
78 // Define build options
79 std::set<std::string> build_opts;
80 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
81 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
82 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
83 build_opts.emplace("#define BORDER_SIZE_TOP " + support::cpp11::to_string(border_size.top));
84 build_opts.emplace("#define BORDER_SIZE_BOTTOM " + support::cpp11::to_string(border_size.bottom));
85 build_opts.emplace("#define BORDER_SIZE_LEFT " + support::cpp11::to_string(border_size.left));
86 build_opts.emplace("#define BORDER_SIZE_RIGHT " + support::cpp11::to_string(border_size.right));
87
88 if(border_mode == BorderMode::REPLICATE)
89 {
90 build_opts.emplace("#define FILL_IMAGE_BORDERS_REPLICATE\n");
91 }
92 else
93 {
94 build_opts.emplace("#define FILL_IMAGE_BORDERS_CONSTANT\n");
95 }
96
97 switch(tensor->info()->data_type())
98 {
99 case DataType::F16:
100 build_opts.emplace("#define DATA_TYPE_FP16");
101 break;
102
103 case DataType::F32:
104 build_opts.emplace("#define DATA_TYPE_FP32");
105 break;
106
107 default:
108 ARM_COMPUTE_ERROR("Current data type is not supported");
109 break;
110 }
111
112 // Create kernel
113 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, build_opts));
114 _tensor = tensor;
115
Anthony Barbier7068f992017-10-26 15:23:08 +0100116 // Create static kernel arguments
117 const unsigned int valid_width = tensor->info()->valid_region().shape[0];
118 const unsigned int valid_height = tensor->info()->valid_region().shape[1];
119 const unsigned int total_valid_width = border_size.left + valid_width + border_size.right;
120
121 // Set static kernel arguments
122 unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800123 _kernel.set_argument(idx++, valid_width);
124 _kernel.set_argument(idx++, valid_height);
125 _kernel.set_argument(idx++, tensor->info()->valid_region().anchor[0]);
126 _kernel.set_argument(idx++, tensor->info()->valid_region().anchor[1]);
Anthony Barbier7068f992017-10-26 15:23:08 +0100127
128 if(BorderMode::CONSTANT == border_mode)
129 {
130 set_constant_border<float>(idx++, constant_border_value);
131 }
132
133 // Configure kernel window
134 Window win;
135 win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height));
136 win.set(Window::DimY, Window::Dimension(0, 1, 1));
137 win.use_tensor_dimensions(tensor->info()->tensor_shape(), Window::DimZ);
138
Anthony Barbier7068f992017-10-26 15:23:08 +0100139 IGCKernel::configure(win);
140}
141
142void GCFillBorderKernel::run(const Window &window)
143{
144 // Border mode undefined or border width == 0
145 if(_kernel.get_program() == 0)
146 {
147 return;
148 }
149
150 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
151 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
152
153 _kernel.use();
154 Window slice = window.first_slice_window_3D();
155
156 do
157 {
158 unsigned int idx = 0;
159 add_3D_tensor_argument(idx, _tensor, 1, slice);
160
161 _kernel.update_shader_params();
162
163 enqueue(*this, slice);
164 }
165 while(window.slide_window_slice_3D(slice));
166}