blob: 154a2c0c662c81267df6a537545c8d669887c629 [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
59 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])));
62}
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{
77 return 2 + 2 * dimension_size;
78}
79
80template <unsigned int dimension_size>
81void IGCKernel::add_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const BufferParam &param, const Window &window)
82{
83 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
84
85 const ITensorInfo *info = tensor->info();
86 const Strides &strides = info->strides_in_bytes();
87
88 // Calculate offset to the start of the window
89 unsigned int offset_first_element = info->offset_first_element_in_bytes();
90
91 for(unsigned int n = 0; n < info->num_dimensions(); ++n)
92 {
93 offset_first_element += window[n].start() * strides[n];
94 }
95
96 unsigned int idx_start = idx;
97
98 for(unsigned int dimension = 0; dimension < dimension_size; dimension++)
99 {
100 _kernel.set_params(idx++, strides[dimension]);
101 _kernel.set_params(idx++, strides[dimension] * window[dimension].step());
102 }
103
104 _kernel.set_params(idx++, offset_first_element);
105 _kernel.set_params(idx++, param.buffer_data_type_shift);
106
107 ARM_COMPUTE_GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, param.binding_point, tensor->gc_buffer()));
108
109 ARM_COMPUTE_ERROR_ON_MSG(idx_start + num_arguments_per_tensor<dimension_size>() != idx,
110 "add_%dD_tensor_argument() is supposed to add exactly %d arguments to the kernel", dimension_size, num_arguments_per_tensor<dimension_size>());
111 ARM_COMPUTE_UNUSED(idx_start);
112}
113
114void IGCKernel::add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
115{
116 add_tensor_argument<1>(idx, tensor, BufferParam(binding_point, 0), window);
117}
118
119void IGCKernel::add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const BufferParam &param, const Window &window)
120{
121 add_tensor_argument<1>(idx, tensor, param, window);
122}
123
124void IGCKernel::add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
125{
126 add_tensor_argument<2>(idx, tensor, BufferParam(binding_point, 0), window);
127}
128
129void IGCKernel::add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const BufferParam &param, const Window &window)
130{
131 add_tensor_argument<2>(idx, tensor, param, window);
132}
133
134void IGCKernel::add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
135{
136 add_tensor_argument<3>(idx, tensor, BufferParam(binding_point, 0), window);
137}
138
139void IGCKernel::add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const BufferParam &param, const Window &window)
140{
141 add_tensor_argument<3>(idx, tensor, param, window);
142}
143
144unsigned int IGCKernel::num_arguments_per_1D_tensor() const
145{
146 return num_arguments_per_tensor<1>();
147}
148
149unsigned int IGCKernel::num_arguments_per_2D_tensor() const
150{
151 return num_arguments_per_tensor<2>();
152}
153
154unsigned int IGCKernel::num_arguments_per_3D_tensor() const
155{
156 return num_arguments_per_tensor<3>();
157}