blob: 4da35c7d5e5b9d825c3a30cca5a4d0e9d85e19ae [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michalis Spyrou7c60c992019-10-10 14:33:47 +01002 * Copyright (c) 2017-2019 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/IGCKernel.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/IGCTensor.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
35
36#include <cstddef>
37#include <sstream>
38
39using namespace arm_compute;
40
41void arm_compute::enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws)
42{
43 ARM_COMPUTE_UNUSED(kernel);
44
45 if(kernel.kernel().get_program() == 0)
46 {
47 return;
48 }
49
50 ARM_COMPUTE_ERROR_ON((0 == (window.x().end() - window.x().start())) || (0 == (window.y().end() - window.y().start())));
51
Michalis Spyrou7c60c992019-10-10 14:33:47 +010052 ARM_COMPUTE_ERROR_ON_MSG_VAR((((window.x().end() - window.x().start()) % (window.x().step() * lws[0])) != 0),
53 "window x end =%d, start=%d, step=%d, lws x=%zu", window.x().end(), window.x().start(), window.x().step(), lws[0]);
54 ARM_COMPUTE_ERROR_ON_MSG_VAR((((window.y().end() - window.y().start()) % (window.y().step() * lws[1])) != 0),
55 "window y end =%d, start=%d, step=%d, lws y=%zu", window.y().end(), window.y().start(), window.y().step(), lws[1]);
56 ARM_COMPUTE_ERROR_ON_MSG_VAR((((window.z().end() - window.z().start()) % (window.z().step() * lws[2])) != 0),
57 "window z end =%d, start=%d, step=%d, lws z=%zu", window.z().end(), window.z().start(), window.z().step(), lws[2]);
Anthony Barbier7068f992017-10-26 15:23:08 +010058
zhenglind9a9c502017-12-01 12:04:32 +080059 ARM_COMPUTE_GL_CHECK(glDispatchCompute(((window.x().end() - window.x().start()) / window.x().step()) / lws[0],
60 ((window.y().end() - window.y().start()) / window.y().step()) / lws[1],
61 ((window.z().end() - window.z().start()) / window.z().step()) / lws[2]));
Anthony Barbier7068f992017-10-26 15:23:08 +010062}
63
64IGCKernel::IGCKernel()
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +010065 : _kernel(), _lws_hint(gles::NDRange(1U, 1U, 1U)), _target(GPUTarget::MIDGARD)
Anthony Barbier7068f992017-10-26 15:23:08 +010066{
67}
68
69GCKernel &IGCKernel::kernel()
70{
71 return _kernel;
72}
73
74template <unsigned int dimension_size>
75unsigned int IGCKernel::num_arguments_per_tensor() const
76{
Joel Liangf1f3ebd2017-11-10 09:59:19 +080077 // Rounding up the tensor attributes structure in compute shader to a multiple of a vec4
78 return ceil_to_multiple(1 + 2 * dimension_size, 4);
Anthony Barbier7068f992017-10-26 15:23:08 +010079}
80
81template <unsigned int dimension_size>
Joel Liangabd03cf2018-01-08 15:20:48 +080082void IGCKernel::add_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Anthony Barbier7068f992017-10-26 15:23:08 +010083{
84 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
85
86 const ITensorInfo *info = tensor->info();
87 const Strides &strides = info->strides_in_bytes();
88
89 // Calculate offset to the start of the window
90 unsigned int offset_first_element = info->offset_first_element_in_bytes();
91
92 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
93 {
94 offset_first_element += window[n].start() * strides[n];
95 }
96
97 unsigned int idx_start = idx;
98
99 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
100 {
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800101 _kernel.set_argument(idx++, strides[dimension]);
102 _kernel.set_argument(idx++, strides[dimension] * window[dimension].step());
Anthony Barbier7068f992017-10-26 15:23:08 +0100103 }
104
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800105 _kernel.set_argument(idx++, offset_first_element);
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800106
107 // Rounding up the tensor attributes structure in compute shader to a multiple of a vec4
108 unsigned int idx_end = ceil_to_multiple(idx, 4);
109 for(unsigned int i = idx; i < idx_end; ++i)
110 {
111 _kernel.set_argument(i, 0);
112 }
113 idx = idx_end;
Anthony Barbier7068f992017-10-26 15:23:08 +0100114
Joel Liangabd03cf2018-01-08 15:20:48 +0800115 ARM_COMPUTE_GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding_point, tensor->gc_buffer()));
Anthony Barbier7068f992017-10-26 15:23:08 +0100116
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100117 ARM_COMPUTE_ERROR_ON_MSG_VAR(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
118 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
Anthony Barbier7068f992017-10-26 15:23:08 +0100119 ARM_COMPUTE_UNUSED(idx_start);
120}
121
122void IGCKernel::add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
123{
Joel Liangabd03cf2018-01-08 15:20:48 +0800124 add_tensor_argument<1>(idx, tensor, binding_point, window);
Anthony Barbier7068f992017-10-26 15:23:08 +0100125}
126
127void IGCKernel::add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
128{
Joel Liangabd03cf2018-01-08 15:20:48 +0800129 add_tensor_argument<2>(idx, tensor, binding_point, window);
Anthony Barbier7068f992017-10-26 15:23:08 +0100130}
131
132void IGCKernel::add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
133{
Joel Liangabd03cf2018-01-08 15:20:48 +0800134 add_tensor_argument<3>(idx, tensor, binding_point, window);
Anthony Barbier7068f992017-10-26 15:23:08 +0100135}
136
137unsigned int IGCKernel::num_arguments_per_1D_tensor() const
138{
139 return num_arguments_per_tensor<1>();
140}
141
142unsigned int IGCKernel::num_arguments_per_2D_tensor() const
143{
144 return num_arguments_per_tensor<2>();
145}
146
147unsigned int IGCKernel::num_arguments_per_3D_tensor() const
148{
149 return num_arguments_per_tensor<3>();
150}