blob: c60c1674bfb1559a312cc0cd3c0d11296750ac1a [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/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
52 ARM_COMPUTE_ERROR_ON_MSG((((window.x().end() - window.x().start()) % (window.x().step() * lws[0])) != 0),
53 "window x end =%d, start=%d, step=%d, lws x=%d", window.x().end(), window.x().start(), window.x().step(), lws[0]);
54 ARM_COMPUTE_ERROR_ON_MSG((((window.y().end() - window.y().start()) % (window.y().step() * lws[1])) != 0),
55 "window y end =%d, start=%d, step=%d, lws y=%d", window.y().end(), window.y().start(), window.y().step(), lws[1]);
56 ARM_COMPUTE_ERROR_ON_MSG((((window.z().end() - window.z().start()) % (window.z().step() * lws[2])) != 0),
57 "window z end =%d, start=%d, step=%d, lws z=%d", window.z().end(), window.z().start(), window.z().step(), lws[2]);
58
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()
65 : _kernel()
66{
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>
82void IGCKernel::add_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const BufferParam &param, const Window &window)
83{
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);
106 _kernel.set_argument(idx++, param.buffer_data_type_shift);
107
108 // Rounding up the tensor attributes structure in compute shader to a multiple of a vec4
109 unsigned int idx_end = ceil_to_multiple(idx, 4);
110 for(unsigned int i = idx; i < idx_end; ++i)
111 {
112 _kernel.set_argument(i, 0);
113 }
114 idx = idx_end;
Anthony Barbier7068f992017-10-26 15:23:08 +0100115
116 ARM_COMPUTE_GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, param.binding_point, tensor->gc_buffer()));
117
118 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
119 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
120 ARM_COMPUTE_UNUSED(idx_start);
121}
122
123void IGCKernel::add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
124{
125 add_tensor_argument<1>(idx, tensor, BufferParam(binding_point, 0), window);
126}
127
128void IGCKernel::add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const BufferParam &param, const Window &window)
129{
130 add_tensor_argument<1>(idx, tensor, param, window);
131}
132
133void IGCKernel::add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
134{
135 add_tensor_argument<2>(idx, tensor, BufferParam(binding_point, 0), window);
136}
137
138void IGCKernel::add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const BufferParam &param, const Window &window)
139{
140 add_tensor_argument<2>(idx, tensor, param, window);
141}
142
143void IGCKernel::add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
144{
145 add_tensor_argument<3>(idx, tensor, BufferParam(binding_point, 0), window);
146}
147
148void IGCKernel::add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const BufferParam &param, const Window &window)
149{
150 add_tensor_argument<3>(idx, tensor, param, window);
151}
152
153unsigned int IGCKernel::num_arguments_per_1D_tensor() const
154{
155 return num_arguments_per_tensor<1>();
156}
157
158unsigned int IGCKernel::num_arguments_per_2D_tensor() const
159{
160 return num_arguments_per_tensor<2>();
161}
162
163unsigned int IGCKernel::num_arguments_per_3D_tensor() const
164{
165 return num_arguments_per_tensor<3>();
166}