Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_GCKERNELLIBRARY_H |
| 25 | #define ARM_COMPUTE_GCKERNELLIBRARY_H |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" |
| 28 | #include "arm_compute/core/Utils.h" |
| 29 | |
| 30 | #include <map> |
| 31 | #include <set> |
| 32 | #include <string> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | /** GCProgram class */ |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 39 | class GCProgram final |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | /** Default constructor. */ |
| 43 | GCProgram(); |
| 44 | /** Construct program from source file. |
| 45 | * |
| 46 | * @param[in] name Program name. |
| 47 | * @param[in] source Program source. |
| 48 | */ |
| 49 | GCProgram(std::string name, std::string source); |
| 50 | /** Default Copy Constructor. */ |
| 51 | GCProgram(const GCProgram &) = default; |
| 52 | /** Default Move Constructor. */ |
| 53 | GCProgram(GCProgram &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 54 | /** Default copy assignment operator */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 55 | GCProgram &operator=(const GCProgram &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 56 | /** Default move assignment operator */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 57 | GCProgram &operator=(GCProgram &&) = default; |
| 58 | /** Returns program name. |
| 59 | * |
| 60 | * @return Program's name. |
| 61 | */ |
| 62 | std::string name() const |
| 63 | { |
| 64 | return _name; |
| 65 | } |
| 66 | /** Link program. |
| 67 | * |
| 68 | * @param[in] shader Shader used to link program. |
| 69 | * |
| 70 | * @return linked program id . |
| 71 | */ |
| 72 | GLuint link_program(GLuint shader); |
| 73 | /** Compile shader. |
| 74 | * |
| 75 | * @param[in] build_options Shader build options. |
| 76 | * |
| 77 | * @return GLES shader object. |
| 78 | */ |
| 79 | GLuint compile_shader(const std::string &build_options); |
| 80 | |
| 81 | private: |
| 82 | std::string _name; /**< Program name. */ |
| 83 | std::string _source; /**< Source code for the program. */ |
| 84 | }; |
| 85 | |
| 86 | /** GCKernel class */ |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 87 | class GCKernel final |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 88 | { |
| 89 | public: |
| 90 | /** Default Constructor. */ |
| 91 | GCKernel(); |
| 92 | /** Default Copy Constructor. */ |
| 93 | GCKernel(const GCKernel &) = default; |
| 94 | /** Default Move Constructor. */ |
| 95 | GCKernel(GCKernel &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 96 | /** Default copy assignment operator */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 97 | GCKernel &operator=(const GCKernel &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 98 | /** Default move assignment operator */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 99 | GCKernel &operator=(GCKernel &&) = default; |
| 100 | /** Constructor. |
| 101 | * |
| 102 | * @param[in] name Kernel name. |
| 103 | * @param[in] program Built program. |
| 104 | */ |
| 105 | GCKernel(std::string name, GLuint program); |
Joel Liang | 5542ba8 | 2017-12-01 15:33:41 +0800 | [diff] [blame] | 106 | /** Destructor. |
| 107 | */ |
| 108 | ~GCKernel(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 109 | /** Returns kernel name. |
| 110 | * |
| 111 | * @return Kernel's name. |
| 112 | */ |
| 113 | std::string name() const |
| 114 | { |
| 115 | return _name; |
| 116 | } |
| 117 | /** Get program id. |
| 118 | * |
| 119 | * @return program id. |
| 120 | */ |
| 121 | GLuint get_program() const |
| 122 | { |
| 123 | return _program; |
| 124 | } |
| 125 | /** Use current program. |
| 126 | * |
| 127 | * @return program id. |
| 128 | */ |
| 129 | void use(); |
| 130 | /** Unuse current program. |
| 131 | * |
| 132 | * @return program id. |
| 133 | */ |
| 134 | void unuse(); |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 135 | /** Set argument value at index of shader params. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 136 | * |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 137 | * @param[in] idx Index in shader params. |
| 138 | * @param[in] value Argument value to be set. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 139 | */ |
| 140 | template <class T> |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 141 | void set_argument(unsigned int idx, T value) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 142 | { |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 143 | if(idx >= _shader_arguments.size()) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 144 | { |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 145 | _shader_arguments.resize(idx + 1, 0); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 148 | unsigned int *p = reinterpret_cast<unsigned int *>(&value); |
| 149 | _shader_arguments[idx] = *p; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 150 | } |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 151 | /** Clear shader arguments. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 152 | * |
| 153 | */ |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 154 | void clear_arguments() |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 155 | { |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 156 | _shader_arguments.clear(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 157 | } |
| 158 | /** Set shader params binding point. |
| 159 | * |
| 160 | * @param[in] binding Shader params binding point. |
| 161 | */ |
| 162 | void set_shader_params_binding_point(unsigned int binding) |
| 163 | { |
| 164 | _shader_params_binding_point = binding; |
| 165 | } |
| 166 | /** Update shader params. |
| 167 | * |
| 168 | */ |
| 169 | void update_shader_params(); |
| 170 | /** Clean up program and ubo. |
| 171 | * |
| 172 | */ |
| 173 | void cleanup(); |
| 174 | |
| 175 | private: |
| 176 | std::string _name; /**< Kernel name */ |
| 177 | GLuint _program; /**< Linked program id */ |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 178 | std::vector<unsigned int> _shader_arguments; /**< Store all the values of the shader arguments */ |
| 179 | GLuint _shader_params_ubo_name; /**< Uniform buffer object name for shader parameters */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 180 | GLuint _shader_params_binding_point; /**< The binding point of the uniform block for shader parameters */ |
| 181 | GLuint _shader_params_index; /**< The index of the uniform block */ |
| 182 | GLint _shader_params_size; /**< The uniform block data size in the shader */ |
| 183 | static constexpr const char *_shader_params_name = "shader_params"; /**< The uniform block name in the shader */ |
| 184 | }; |
| 185 | |
| 186 | /** GCKernelLibrary class */ |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 187 | class GCKernelLibrary final |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 188 | { |
| 189 | using StringSet = std::set<std::string>; |
| 190 | |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 191 | public: |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 192 | /** Default Constructor. */ |
| 193 | GCKernelLibrary(); |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 194 | /** Default Destructor */ |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 195 | ~GCKernelLibrary(); |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 196 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 197 | GCKernelLibrary(const GCKernelLibrary &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 198 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 199 | const GCKernelLibrary &operator=(const GCKernelLibrary &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 200 | /** Get the static instance of @ref GCKernelLibrary. |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 201 | * This method has been deprecated and will be removed in the next release. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 202 | * @return The static instance of GCKernelLibrary. |
| 203 | */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 204 | static GCKernelLibrary &get(); |
| 205 | /** Initialises the kernel library. |
| 206 | * |
| 207 | * @param[in] shader_path (Optional) Path of the directory from which shader sources are loaded. |
| 208 | * @param[in] dpy (Optional) EGLdisplay set by external application. |
| 209 | * @param[in] ctx (Optional) EGLContext set by external application. |
| 210 | */ |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 211 | void init(std::string shader_path = "./", EGLDisplay dpy = EGL_NO_DISPLAY, EGLContext ctx = EGL_NO_CONTEXT); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 212 | /** Sets the path that the shaders reside in. |
| 213 | * |
| 214 | * @param[in] shader_path Path of the shader. |
| 215 | */ |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 216 | void set_shader_path(const std::string &shader_path); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 217 | /** Sets display and context to create kernel. |
| 218 | * |
| 219 | * @param[in] dpy EGLdisplay set by external application. |
| 220 | * @param[in] ctx EGLContext set by external application. |
| 221 | */ |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 222 | void set_context(EGLDisplay dpy, EGLContext ctx); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 223 | /** Creates a kernel from the kernel library. |
| 224 | * |
| 225 | * @param[in] shader_name Shader name. |
| 226 | * @param[in] build_options_set Shader build options as a set. |
| 227 | * |
| 228 | * @return The created kernel. |
| 229 | */ |
| 230 | GCKernel create_kernel(const std::string &shader_name, const StringSet &build_options_set = {}) const; |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 231 | /** Serializes and saves programs to a binary. */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 232 | void save_binary(); |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 233 | /** Load serialized binary with all the programs. */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 234 | void load_binary(); |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 235 | /** Setup a dummy fbo to workaround an issue on Galaxy S8. */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 236 | void setup_dummy_fbo(); |
| 237 | |
| 238 | private: |
| 239 | /** Preprocess GLES shader |
| 240 | * |
| 241 | * @param[in] shader_source Source code of the shader to preprocess. |
| 242 | * |
| 243 | * @return Preprocessed GLES shader object. |
| 244 | */ |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 245 | std::string preprocess_shader(const std::string &shader_source) const; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 246 | /** Load program and its dependencies. |
| 247 | * |
| 248 | * @param[in] program_name Name of the program to load. |
| 249 | */ |
| 250 | const GCProgram &load_program(const std::string &program_name) const; |
| 251 | /** Concatenates contents of a set into a single string. |
| 252 | * |
| 253 | * @param[in] s Input set to concatenate. |
| 254 | * |
| 255 | * @return Concatenated string. |
| 256 | */ |
| 257 | std::string stringify_set(const StringSet &s) const; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 258 | |
| 259 | EGLDisplay _display; /**< Underlying EGL Display. */ |
| 260 | EGLContext _context; /**< Underlying EGL Context. */ |
| 261 | GLuint _frame_buffer; /**< Dummy fbo */ |
| 262 | GLuint _tex_rt; /**< Dummy texture for render target */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 263 | std::string _shader_path; /**< Path to the shaders folder. */ |
| 264 | mutable std::map<std::string, const GCProgram> _programs_map; /**< Map with all already loaded program data. */ |
| 265 | mutable std::map<std::string, const GCKernel> _built_programs_map; /**< Map with all already built program data. */ |
| 266 | static const std::map<std::string, std::string> _shader_program_map; /**< Map that associates kernel names with programs. */ |
| 267 | static const std::map<std::string, std::string> _program_source_map; /**< Contains sources for all programs. |
| 268 | Used for compile-time shader inclusion. */ |
| 269 | }; |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 270 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 271 | #endif /* ARM_COMPUTE_GCKERNELLIBRARY_H */ |