blob: b4efc0b9a05d8cc2e6bd6aa1e191d26fca4bba62 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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"
35
36#include <cstdint>
37#include <set>
38#include <string>
39
40using namespace arm_compute;
41
42GCFillBorderKernel::GCFillBorderKernel()
43 : IGCKernel(), _tensor(nullptr)
44{
45}
46
47bool GCFillBorderKernel::is_parallelisable() const
48{
49 return false;
50}
51
52template <class T>
53void GCFillBorderKernel::set_constant_border(unsigned int idx, const PixelValue &constant_border_value)
54{
55 T value;
56 constant_border_value.get(value);
Joel Liangf1f3ebd2017-11-10 09:59:19 +080057 _kernel.set_argument(idx, static_cast<T>(value));
Anthony Barbier7068f992017-10-26 15:23:08 +010058}
59
60void GCFillBorderKernel::configure(const IGCTensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value)
61{
62 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
63 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(tensor, 1, DataType::F32, DataType::F16);
64 ARM_COMPUTE_ERROR_ON(tensor->info()->num_channels() != 1);
65
66 border_size.limit(tensor->info()->padding());
67
68 // If there is no border: early exit
69 if(border_size.empty() || border_mode == BorderMode::UNDEFINED)
70 {
71 return;
72 }
73
74 // Select appropriate kernel
75 std::string kernel_name = "fill_image_borders_" + lower_string(string_from_border_mode(border_mode));
76
77 // Define build options
78 std::set<std::string> build_opts;
79 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
80 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
81 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
82 build_opts.emplace("#define BORDER_SIZE_TOP " + support::cpp11::to_string(border_size.top));
83 build_opts.emplace("#define BORDER_SIZE_BOTTOM " + support::cpp11::to_string(border_size.bottom));
84 build_opts.emplace("#define BORDER_SIZE_LEFT " + support::cpp11::to_string(border_size.left));
85 build_opts.emplace("#define BORDER_SIZE_RIGHT " + support::cpp11::to_string(border_size.right));
86
87 if(border_mode == BorderMode::REPLICATE)
88 {
89 build_opts.emplace("#define FILL_IMAGE_BORDERS_REPLICATE\n");
90 }
91 else
92 {
93 build_opts.emplace("#define FILL_IMAGE_BORDERS_CONSTANT\n");
94 }
95
96 switch(tensor->info()->data_type())
97 {
98 case DataType::F16:
99 build_opts.emplace("#define DATA_TYPE_FP16");
100 break;
101
102 case DataType::F32:
103 build_opts.emplace("#define DATA_TYPE_FP32");
104 break;
105
106 default:
107 ARM_COMPUTE_ERROR("Current data type is not supported");
108 break;
109 }
110
111 // Create kernel
112 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, build_opts));
113 _tensor = tensor;
114
Anthony Barbier7068f992017-10-26 15:23:08 +0100115 // Create static kernel arguments
116 const unsigned int valid_width = tensor->info()->valid_region().shape[0];
117 const unsigned int valid_height = tensor->info()->valid_region().shape[1];
118 const unsigned int total_valid_width = border_size.left + valid_width + border_size.right;
119
120 // Set static kernel arguments
121 unsigned int idx = num_arguments_per_3D_tensor(); //Skip the tensor parameters
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800122 _kernel.set_argument(idx++, valid_width);
123 _kernel.set_argument(idx++, valid_height);
124 _kernel.set_argument(idx++, tensor->info()->valid_region().anchor[0]);
125 _kernel.set_argument(idx++, tensor->info()->valid_region().anchor[1]);
Anthony Barbier7068f992017-10-26 15:23:08 +0100126
127 if(BorderMode::CONSTANT == border_mode)
128 {
129 set_constant_border<float>(idx++, constant_border_value);
130 }
131
132 // Configure kernel window
133 Window win;
134 win.set(Window::DimX, Window::Dimension(0, total_valid_width + valid_height));
135 win.set(Window::DimY, Window::Dimension(0, 1, 1));
136 win.use_tensor_dimensions(tensor->info()->tensor_shape(), Window::DimZ);
137
Anthony Barbier7068f992017-10-26 15:23:08 +0100138 IGCKernel::configure(win);
139}
140
141void GCFillBorderKernel::run(const Window &window)
142{
143 // Border mode undefined or border width == 0
144 if(_kernel.get_program() == 0)
145 {
146 return;
147 }
148
149 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
150 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IGCKernel::window(), window);
151
152 _kernel.use();
153 Window slice = window.first_slice_window_3D();
154
155 do
156 {
157 unsigned int idx = 0;
158 add_3D_tensor_argument(idx, _tensor, 1, slice);
159
160 _kernel.update_shader_params();
161
162 enqueue(*this, slice);
163 }
164 while(window.slide_window_slice_3D(slice));
165}