Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | void 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 | |
zhenglin | d9a9c50 | 2017-12-01 12:04:32 +0800 | [diff] [blame] | 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])); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | IGCKernel::IGCKernel() |
Michele Di Giorgio | b8fc60f | 2018-04-25 11:58:07 +0100 | [diff] [blame] | 65 | : _kernel(), _lws_hint(gles::NDRange(1U, 1U, 1U)), _target(GPUTarget::MIDGARD) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 66 | { |
| 67 | } |
| 68 | |
| 69 | GCKernel &IGCKernel::kernel() |
| 70 | { |
| 71 | return _kernel; |
| 72 | } |
| 73 | |
| 74 | template <unsigned int dimension_size> |
| 75 | unsigned int IGCKernel::num_arguments_per_tensor() const |
| 76 | { |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 77 | // 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | template <unsigned int dimension_size> |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame] | 82 | void IGCKernel::add_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 101 | _kernel.set_argument(idx++, strides[dimension]); |
| 102 | _kernel.set_argument(idx++, strides[dimension] * window[dimension].step()); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 105 | _kernel.set_argument(idx++, offset_first_element); |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 106 | |
| 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 114 | |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame] | 115 | ARM_COMPUTE_GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding_point, tensor->gc_buffer())); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 116 | |
| 117 | ARM_COMPUTE_ERROR_ON_MSG(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>()); |
| 119 | ARM_COMPUTE_UNUSED(idx_start); |
| 120 | } |
| 121 | |
| 122 | void IGCKernel::add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window) |
| 123 | { |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame] | 124 | add_tensor_argument<1>(idx, tensor, binding_point, window); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void IGCKernel::add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window) |
| 128 | { |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame] | 129 | add_tensor_argument<2>(idx, tensor, binding_point, window); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void IGCKernel::add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window) |
| 133 | { |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame] | 134 | add_tensor_argument<3>(idx, tensor, binding_point, window); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | unsigned int IGCKernel::num_arguments_per_1D_tensor() const |
| 138 | { |
| 139 | return num_arguments_per_tensor<1>(); |
| 140 | } |
| 141 | |
| 142 | unsigned int IGCKernel::num_arguments_per_2D_tensor() const |
| 143 | { |
| 144 | return num_arguments_per_tensor<2>(); |
| 145 | } |
| 146 | |
| 147 | unsigned int IGCKernel::num_arguments_per_3D_tensor() const |
| 148 | { |
| 149 | return num_arguments_per_tensor<3>(); |
| 150 | } |