Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
| 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/GCKernelLibrary.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | |
| 29 | #include <fstream> |
| 30 | #include <iomanip> |
| 31 | #include <iostream> |
| 32 | #include <regex> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
| 38 | GCProgram::GCProgram() |
| 39 | : _name(), _source() |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | GCProgram::GCProgram(std::string name, std::string source) |
| 44 | : _name(std::move(name)), _source(std::move(source)) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | GLuint GCProgram::link_program(GLuint shader) |
| 49 | { |
| 50 | GLuint program = ARM_COMPUTE_GL_CHECK(glCreateProgram()); |
| 51 | |
| 52 | GLint rvalue; |
| 53 | GLsizei length; |
| 54 | |
| 55 | ARM_COMPUTE_GL_CHECK(glAttachShader(program, shader)); |
| 56 | ARM_COMPUTE_GL_CHECK(glLinkProgram(program)); |
| 57 | ARM_COMPUTE_GL_CHECK(glDetachShader(program, shader)); |
| 58 | ARM_COMPUTE_GL_CHECK(glDeleteShader(shader)); |
| 59 | |
| 60 | // Check if there were some issues when linking the shader. |
| 61 | ARM_COMPUTE_GL_CHECK(glGetProgramiv(program, GL_LINK_STATUS, &rvalue)); |
| 62 | |
| 63 | if(rvalue == 0) |
| 64 | { |
| 65 | ARM_COMPUTE_GL_CHECK(glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length)); |
| 66 | |
| 67 | std::vector<GLchar> log(length); |
| 68 | ARM_COMPUTE_GL_CHECK(glGetProgramInfoLog(program, length, nullptr, log.data())); |
| 69 | ARM_COMPUTE_ERROR("Error: Linker log:\n%s\n", log.data()); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | ARM_COMPUTE_GL_CHECK(glUseProgram(program)); |
| 75 | |
| 76 | return program; |
| 77 | } |
| 78 | |
| 79 | GLuint GCProgram::compile_shader(const std::string &build_options) |
| 80 | { |
| 81 | GLuint shader = ARM_COMPUTE_GL_CHECK(glCreateShader(GL_COMPUTE_SHADER)); |
| 82 | |
| 83 | const char *src[] |
| 84 | { |
| 85 | "#version 310 es\n", |
| 86 | build_options.c_str(), |
| 87 | _source.c_str() |
| 88 | }; |
| 89 | |
| 90 | ARM_COMPUTE_GL_CHECK(glShaderSource(shader, sizeof(src) / sizeof(src[0]), src, nullptr)); |
| 91 | |
| 92 | ARM_COMPUTE_GL_CHECK(glCompileShader(shader)); |
| 93 | |
| 94 | // Check if there were any issues when compiling the shader |
| 95 | GLint rvalue; |
| 96 | GLsizei length; |
| 97 | |
| 98 | ARM_COMPUTE_GL_CHECK(glGetShaderiv(shader, GL_COMPILE_STATUS, &rvalue)); |
| 99 | |
| 100 | if(rvalue == 0) |
| 101 | { |
| 102 | ARM_COMPUTE_GL_CHECK(glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length)); |
| 103 | |
| 104 | std::vector<GLchar> log(length); |
| 105 | ARM_COMPUTE_GL_CHECK(glGetShaderInfoLog(shader, length, nullptr, log.data())); |
| 106 | |
| 107 | #ifdef ARM_COMPUTE_DEBUG_ENABLED |
| 108 | std::istringstream ss(_source); |
| 109 | std::stringstream output_stream; |
| 110 | std::string line; |
| 111 | size_t line_num = 1; |
| 112 | |
| 113 | ARM_COMPUTE_LOG_INFO_MSG_WITH_FORMAT_CORE("GLES Shader build options:\n%s\n", build_options.c_str()); |
| 114 | while(std::getline(ss, line, '\n')) |
| 115 | { |
| 116 | output_stream << std::setw(6) << line_num << ": " << line << std::endl; |
| 117 | line_num++; |
| 118 | } |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 119 | ARM_COMPUTE_LOG_INFO_STREAM_CORE("GLES Shader source code:\n" |
| 120 | << output_stream.rdbuf()); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 121 | #endif /* ARM_COMPUTE_DEBUG_ENABLED */ |
| 122 | |
| 123 | ARM_COMPUTE_ERROR("Error: Compiler log:\n%s\n", log.data()); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | return shader; |
| 129 | } |
| 130 | |
| 131 | GCKernel::GCKernel() |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 132 | : _name(), _program(), _shader_arguments(), _shader_params_ubo_name(), _shader_params_binding_point(), _shader_params_index(), _shader_params_size() |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 133 | { |
| 134 | } |
| 135 | |
Joel Liang | 5542ba8 | 2017-12-01 15:33:41 +0800 | [diff] [blame] | 136 | // Add a default destructor in cpp file to workaround the free unallocated value issue on Android |
| 137 | GCKernel::~GCKernel() // NOLINT |
| 138 | { |
| 139 | } |
| 140 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 141 | GCKernel::GCKernel(std::string name, GLuint program) |
| 142 | : _name(std::move(name)), |
| 143 | _program(program), |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 144 | _shader_arguments(), |
| 145 | _shader_params_ubo_name(0), |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 146 | _shader_params_binding_point(0), |
| 147 | _shader_params_index(0), |
| 148 | _shader_params_size(0) |
| 149 | { |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 150 | _shader_arguments.clear(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 151 | |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 152 | ARM_COMPUTE_GL_CHECK(glGenBuffers(1, &_shader_params_ubo_name)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 153 | |
| 154 | _shader_params_index = ARM_COMPUTE_GL_CHECK(glGetUniformBlockIndex(_program, _shader_params_name)); |
| 155 | ARM_COMPUTE_ERROR_ON_MSG((_shader_params_index == GL_INVALID_INDEX), "Failed to get index of %s", _shader_params_name); |
| 156 | ARM_COMPUTE_GL_CHECK(glGetActiveUniformBlockiv(_program, _shader_params_index, GL_UNIFORM_BLOCK_DATA_SIZE, &_shader_params_size)); |
| 157 | ARM_COMPUTE_ERROR_ON_MSG((_shader_params_size == 0), "Failed to get size of %s", _shader_params_name); |
| 158 | } |
| 159 | |
| 160 | void GCKernel::cleanup() |
| 161 | { |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 162 | ARM_COMPUTE_GL_CHECK(glDeleteBuffers(1, &_shader_params_ubo_name)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 163 | ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_UNIFORM_BUFFER, 0)); |
| 164 | ARM_COMPUTE_GL_CHECK(glDeleteProgram(_program)); |
| 165 | ARM_COMPUTE_GL_CHECK(glUseProgram(0)); |
| 166 | } |
| 167 | |
| 168 | void GCKernel::use() |
| 169 | { |
| 170 | ARM_COMPUTE_GL_CHECK(glUseProgram(_program)); |
| 171 | } |
| 172 | |
| 173 | void GCKernel::unuse() |
| 174 | { |
| 175 | ARM_COMPUTE_GL_CHECK(glUseProgram(0)); |
| 176 | } |
| 177 | |
| 178 | void GCKernel::update_shader_params() |
| 179 | { |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 180 | ARM_COMPUTE_ERROR_ON_MSG((_shader_params_size != (int)(_shader_arguments.size() * sizeof(_shader_arguments[0]))), "Arguments size (%d) is not equal to shader params block size (%d)", |
| 181 | _shader_arguments.size() * sizeof(_shader_arguments[0]), _shader_params_size); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 182 | |
| 183 | ARM_COMPUTE_GL_CHECK(glUniformBlockBinding(_program, _shader_params_index, _shader_params_binding_point)); |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 184 | ARM_COMPUTE_GL_CHECK(glBindBufferBase(GL_UNIFORM_BUFFER, _shader_params_binding_point, _shader_params_ubo_name)); |
| 185 | ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_UNIFORM_BUFFER, _shader_params_ubo_name)); |
| 186 | ARM_COMPUTE_GL_CHECK(glBufferData(GL_UNIFORM_BUFFER, _shader_params_size, _shader_arguments.data(), GL_DYNAMIC_DRAW)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 187 | ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_UNIFORM_BUFFER, 0)); |
| 188 | } |
| 189 | |
| 190 | const std::map<std::string, std::string> GCKernelLibrary::_shader_program_map = |
| 191 | { |
| 192 | { "absdiff", "absdiff.cs" }, |
| 193 | { "col2im", "convolution_layer.cs" }, |
| 194 | { "direct_convolution1x1", "direct_convolution1x1.cs" }, |
| 195 | { "direct_convolution3x3", "direct_convolution3x3.cs" }, |
| 196 | { "direct_convolution5x5", "direct_convolution5x5.cs" }, |
| 197 | { "pooling_layer_2", "pooling_layer.cs" }, |
| 198 | { "pooling_layer_3", "pooling_layer.cs" }, |
| 199 | { "pooling_layer_7", "pooling_layer.cs" }, |
| 200 | { "pooling_layer_3_optimized", "pooling_layer.cs" }, |
| 201 | { "pooling_layer_n", "pooling_layer.cs" }, |
| 202 | { "fill_image_borders_replicate", "fill_border.cs" }, |
| 203 | { "fill_image_borders_constant", "fill_border.cs" }, |
| 204 | { "gemm_accumulate_biases", "gemm.cs" }, |
| 205 | { "gemm_interleave4x4", "gemm.cs" }, |
| 206 | { "gemm_ma", "gemm.cs" }, |
| 207 | { "gemm_mm_interleaved_transposed", "gemm.cs" }, |
| 208 | { "gemm_mm_floating_point", "gemm.cs" }, |
| 209 | { "gemm_transpose1x4", "gemm.cs" }, |
| 210 | { "im2col_kernel3x3_padx0_pady0", "convolution_layer.cs" }, |
| 211 | { "im2col_generic", "convolution_layer.cs" }, |
| 212 | { "im2col_reduced", "convolution_layer.cs" }, |
| 213 | { "transpose", "transpose.cs" }, |
| 214 | { "activation_layer", "activation_layer.cs" }, |
| 215 | { "softmax_layer_max", "softmax_layer.cs" }, |
| 216 | { "softmax_layer_shift_exp_sum", "softmax_layer.cs" }, |
| 217 | { "softmax_layer_norm", "softmax_layer.cs" }, |
| 218 | { "pixelwise_mul_float", "pixelwise_mul_float.cs" }, |
| 219 | { "normalization_layer", "normalization_layer.cs" }, |
| 220 | { "batchnormalization_layer", "batchnormalization_layer.cs" }, |
| 221 | { "concatenate_depth", "concatenate.cs" }, |
| 222 | { "dropout", "dropout.cs" }, |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 223 | { "normalize_planar_yuv_layer", "normalize_planar_yuv_layer.cs" }, |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame^] | 224 | { "scale_nearest_neighbour", "scale.cs" }, |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | const std::map<std::string, std::string> GCKernelLibrary::_program_source_map = |
| 228 | { |
| 229 | #ifdef EMBEDDED_KERNELS |
| 230 | { |
| 231 | "absdiff.cs", |
| 232 | #include "./cs_shaders/absdiff.csembed" |
| 233 | }, |
| 234 | { |
| 235 | "convolution_layer.cs", |
| 236 | #include "./cs_shaders/convolution_layer.csembed" |
| 237 | }, |
| 238 | { |
| 239 | "direct_convolution1x1.cs", |
| 240 | #include "./cs_shaders/direct_convolution1x1.csembed" |
| 241 | }, |
| 242 | { |
| 243 | "direct_convolution3x3.cs", |
| 244 | #include "./cs_shaders/direct_convolution3x3.csembed" |
| 245 | }, |
| 246 | { |
| 247 | "direct_convolution5x5.cs", |
| 248 | #include "./cs_shaders/direct_convolution5x5.csembed" |
| 249 | }, |
| 250 | { |
| 251 | "pooling_layer.cs", |
| 252 | #include "./cs_shaders/pooling_layer.csembed" |
| 253 | }, |
| 254 | { |
| 255 | "fill_border.cs", |
| 256 | #include "./cs_shaders/fill_border.csembed" |
| 257 | }, |
| 258 | { |
| 259 | "gemm.cs", |
| 260 | #include "./cs_shaders/gemm.csembed" |
| 261 | }, |
| 262 | { |
| 263 | "transpose.cs", |
| 264 | #include "./cs_shaders/transpose.csembed" |
| 265 | }, |
| 266 | { |
| 267 | "activation_layer.cs", |
| 268 | #include "./cs_shaders/activation_layer.csembed" |
| 269 | }, |
| 270 | { |
| 271 | "softmax_layer.cs", |
| 272 | #include "./cs_shaders/softmax_layer.csembed" |
| 273 | }, |
| 274 | { |
| 275 | "pixelwise_mul_float.cs", |
| 276 | #include "./cs_shaders/pixelwise_mul_float.csembed" |
| 277 | }, |
| 278 | { |
| 279 | "normalization_layer.cs", |
| 280 | #include "./cs_shaders/normalization_layer.csembed" |
| 281 | }, |
| 282 | { |
| 283 | "batchnormalization_layer.cs", |
| 284 | #include "./cs_shaders/batchnormalization_layer.csembed" |
| 285 | }, |
| 286 | { |
| 287 | "concatenate.cs", |
| 288 | #include "./cs_shaders/concatenate.csembed" |
| 289 | }, |
| 290 | { |
| 291 | "dropout.cs", |
| 292 | #include "./cs_shaders/dropout.csembed" |
| 293 | }, |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 294 | { |
| 295 | "normalize_planar_yuv_layer.cs", |
| 296 | #include "./cs_shaders/normalize_planar_yuv_layer.csembed" |
| 297 | }, |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame^] | 298 | { |
| 299 | "scale.cs", |
| 300 | #include "./cs_shaders/scale.csembed" |
| 301 | }, |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 302 | #endif /* EMBEDDED_KERNELS */ |
| 303 | }; |
| 304 | |
| 305 | GCKernelLibrary::GCKernelLibrary() |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 306 | : _display(EGL_NO_DISPLAY), _context(EGL_NO_CONTEXT), _frame_buffer(0), _tex_rt(0), _shader_path("./"), _programs_map(), _built_programs_map() |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 307 | { |
| 308 | } |
| 309 | |
| 310 | GCKernelLibrary &GCKernelLibrary::get() |
| 311 | { |
| 312 | static GCKernelLibrary _kernel_library; |
| 313 | return _kernel_library; |
| 314 | } |
| 315 | |
| 316 | GCKernel GCKernelLibrary::create_kernel(const std::string &shader_name, const StringSet &build_options_set) const |
| 317 | { |
| 318 | // Find which program contains the kernel |
| 319 | auto shader_program_it = _shader_program_map.find(shader_name); |
| 320 | |
| 321 | if(_shader_program_map.end() == shader_program_it) |
| 322 | { |
| 323 | ARM_COMPUTE_ERROR("Shader %s not found in the GCKernelLibrary", shader_name.c_str()); |
| 324 | } |
| 325 | |
| 326 | // Check if the program has been built before with same build options. |
| 327 | const std::string program_name = shader_program_it->second; |
| 328 | const std::string build_options = stringify_set(build_options_set); |
| 329 | const std::string built_program_name = program_name + "_" + build_options; |
| 330 | auto built_program_it = _built_programs_map.find(built_program_name); |
| 331 | |
| 332 | GCKernel kernel; |
| 333 | |
| 334 | if(_built_programs_map.end() != built_program_it) |
| 335 | { |
| 336 | // If program has been built, retrieve to create kernel from it |
| 337 | kernel = built_program_it->second; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 338 | } |
| 339 | else |
| 340 | { |
| 341 | GCProgram program = load_program(program_name); |
| 342 | |
| 343 | std::string source_name = _shader_path + shader_program_it->second; |
| 344 | |
| 345 | // load shader |
| 346 | GLuint shader = program.compile_shader(build_options); |
| 347 | |
| 348 | // Build program |
| 349 | GLuint gles_program = program.link_program(shader); |
| 350 | |
| 351 | // Create GCKernel |
| 352 | kernel = GCKernel(shader_name, gles_program); |
| 353 | |
| 354 | // Add built program to internal map |
| 355 | _built_programs_map.emplace(built_program_name, kernel); |
| 356 | } |
| 357 | |
Joel Liang | f1f3ebd | 2017-11-10 09:59:19 +0800 | [diff] [blame] | 358 | kernel.use(); |
| 359 | kernel.clear_arguments(); |
| 360 | // set shader params binding point |
| 361 | kernel.set_shader_params_binding_point(0); |
| 362 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 363 | return kernel; |
| 364 | } |
| 365 | |
| 366 | const std::string GCKernelLibrary::preprocess_shader(const std::string &shader_source) const |
| 367 | { |
| 368 | enum class ParserStage |
| 369 | { |
| 370 | FIRST, |
| 371 | SKIP_COMMENTS = FIRST, |
| 372 | RESOLVE_INCLUDES, |
| 373 | SKIP_PREPROCESSOR_DIRECTIVES, |
| 374 | SEARCH_MACRO_DEFINITIONS, |
| 375 | EXPAND_MACRO_USES, |
| 376 | LAST |
| 377 | }; |
| 378 | |
| 379 | struct MacroDefinitionInfo |
| 380 | { |
| 381 | const std::vector<std::string> param_list; |
| 382 | const std::string content; |
| 383 | }; |
| 384 | |
| 385 | // Found macro definitions so far |
| 386 | std::map<const std::string, const MacroDefinitionInfo> macro_definitions; |
| 387 | |
| 388 | // Define a GLES compute shader parser function |
| 389 | std::function<std::string(const std::string &, ParserStage, int)> cs_parser; |
| 390 | cs_parser = [&](const std::string & src, ParserStage stage, int nested_level) -> std::string |
| 391 | { |
| 392 | std::string dst; |
| 393 | |
| 394 | if(stage == ParserStage::LAST || std::regex_match(src, std::regex(R"(\s*)"))) |
| 395 | { |
| 396 | return src; |
| 397 | } |
| 398 | auto next_stage = static_cast<ParserStage>(static_cast<int>(stage) + 1); |
| 399 | |
| 400 | std::string search_pattern; |
| 401 | switch(stage) |
| 402 | { |
| 403 | case ParserStage::SKIP_COMMENTS: |
| 404 | search_pattern = R"((/\*([^*]|\n|(\*+([^*/]|\n)))*\*+/)|(//.*))"; |
| 405 | break; |
| 406 | case ParserStage::RESOLVE_INCLUDES: |
| 407 | search_pattern = R"rgx((?:^|\n)[ \t]*#include "(.*)")rgx"; |
| 408 | break; |
| 409 | case ParserStage::SKIP_PREPROCESSOR_DIRECTIVES: |
| 410 | search_pattern = R"((^|\n)[ \t]*(#ifdef|#ifndef|#if)[^\n]+)"; |
| 411 | break; |
| 412 | case ParserStage::SEARCH_MACRO_DEFINITIONS: |
| 413 | search_pattern = R"((?:^|\n)[ \t]*#define[ \t]+(\w+)(?:\((\w+(?:[ \t]*,[ \t]*\w+)*)\))?(?: |\t|\\\n)*((?:(?:[^\\\n]|\\[^\n])*\\+\n)*(?:[ \t]*[^ \t\n]+)*)[ \t]*)"; |
| 414 | break; |
| 415 | case ParserStage::EXPAND_MACRO_USES: |
| 416 | { |
| 417 | if(macro_definitions.empty()) |
| 418 | { |
| 419 | // Nothing to expand |
| 420 | return src; |
| 421 | } |
| 422 | int i = 0; |
| 423 | for(auto &def : macro_definitions) |
| 424 | { |
| 425 | if(i == 0) |
| 426 | { |
| 427 | search_pattern = R"((\b)" + def.first; |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | search_pattern += R"(\b|\b)" + def.first; |
| 432 | } |
| 433 | i++; |
| 434 | } |
| 435 | search_pattern += R"(\b))"; |
| 436 | break; |
| 437 | } |
| 438 | default: |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | std::regex search_regex(search_pattern); |
| 443 | std::smatch match; |
| 444 | ptrdiff_t parsed_pos = 0; |
| 445 | if(std::regex_search(src, match, search_regex)) |
| 446 | { |
| 447 | // Pass the content before the match to the next stage |
| 448 | dst.append(cs_parser(src.substr(0, match.position()), next_stage, 0)); |
| 449 | parsed_pos = match.position() + match.length(); |
| 450 | |
| 451 | // Deal with the matched content |
| 452 | switch(stage) |
| 453 | { |
| 454 | case ParserStage::RESOLVE_INCLUDES: |
| 455 | { |
| 456 | // Replace with the included file contents |
| 457 | // And parse the content from the first stage |
| 458 | const std::string source_name = _shader_path + match.str(1); |
| 459 | dst.append(cs_parser(read_file(source_name, false), ParserStage::FIRST, 0)); |
| 460 | break; |
| 461 | } |
| 462 | case ParserStage::SEARCH_MACRO_DEFINITIONS: |
| 463 | { |
| 464 | std::regex params_regex(R"(\b\w+\b)"); |
| 465 | const std::string macro_param_str = match.str(2); |
| 466 | const std::vector<std::string> macro_param_list( |
| 467 | std::sregex_token_iterator(macro_param_str.begin(), |
| 468 | macro_param_str.end(), |
| 469 | params_regex), |
| 470 | std::sregex_token_iterator()); |
| 471 | |
| 472 | const MacroDefinitionInfo info = |
| 473 | { |
| 474 | macro_param_list, |
| 475 | match.str(3) |
| 476 | }; |
| 477 | // Collect the macro definition data and not change the shader source |
| 478 | macro_definitions.insert(std::pair<const std::string, const MacroDefinitionInfo>(match.str(1), info)); |
| 479 | dst.append(match.str()); |
| 480 | break; |
| 481 | } |
| 482 | case ParserStage::EXPAND_MACRO_USES: |
| 483 | { |
| 484 | ptrdiff_t args_str_length = 0; |
| 485 | std::vector<std::string> args_list; |
| 486 | |
| 487 | // Walk through argument list, because the regular expression does NOT support nested parentheses |
| 488 | size_t cur_args_str_pos = match.position() + match.length(); |
| 489 | if(src[cur_args_str_pos++] == '(') |
| 490 | { |
| 491 | int nested_parentheses = 0; |
| 492 | ptrdiff_t cur_arg_pos = cur_args_str_pos; |
| 493 | ptrdiff_t cur_arg_length = 0; |
| 494 | |
| 495 | args_str_length++; |
| 496 | while(src[cur_args_str_pos] != ')' || nested_parentheses != 0) |
| 497 | { |
| 498 | switch(src[cur_args_str_pos++]) |
| 499 | { |
| 500 | case '(': |
| 501 | nested_parentheses++; |
| 502 | cur_arg_length++; |
| 503 | break; |
| 504 | case ',': |
| 505 | if(nested_parentheses == 0) |
| 506 | { |
| 507 | args_list.push_back(src.substr(cur_arg_pos, cur_arg_length)); |
| 508 | cur_arg_pos = cur_args_str_pos; |
| 509 | cur_arg_length = 0; |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | cur_arg_length++; |
| 514 | } |
| 515 | break; |
| 516 | case ' ': |
| 517 | case '\t': |
| 518 | if(cur_arg_length == 0) |
| 519 | { |
| 520 | cur_arg_pos++; |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | cur_arg_length++; |
| 525 | } |
| 526 | break; |
| 527 | case ')': |
| 528 | nested_parentheses--; |
| 529 | // no break here! |
| 530 | default: |
| 531 | cur_arg_length++; |
| 532 | break; |
| 533 | } |
| 534 | args_str_length++; |
| 535 | } |
| 536 | if(src[cur_args_str_pos] == ')' && nested_parentheses == 0) |
| 537 | { |
| 538 | args_list.push_back(src.substr(cur_arg_pos, cur_arg_length)); |
| 539 | } |
| 540 | args_str_length++; |
| 541 | } |
| 542 | |
| 543 | std::string expanded_content = match.str(); |
| 544 | const std::vector<std::string> macro_param_list = macro_definitions.at(match.str()).param_list; |
| 545 | |
| 546 | if((nested_level != 0 || !macro_param_list.empty()) && macro_param_list.size() == args_list.size()) |
| 547 | { |
| 548 | parsed_pos += args_str_length; |
| 549 | expanded_content = macro_definitions.at(match.str()).content; |
| 550 | size_t i = 0; |
| 551 | for(auto ¶m_name : macro_param_list) |
| 552 | { |
| 553 | std::regex params_regex(R"(\b)" + param_name + R"(\b)"); |
| 554 | expanded_content.assign(std::regex_replace(expanded_content, params_regex, args_list[i])); |
| 555 | ++i; |
| 556 | } |
| 557 | // Expand macro recursively |
| 558 | expanded_content = cs_parser(expanded_content, stage, nested_level + 1); |
| 559 | |
| 560 | if(nested_level == 0) |
| 561 | { |
| 562 | const std::regex token_pasting_rgx = std::regex(R"(\b##\b)"); |
| 563 | if(std::regex_search(expanded_content, token_pasting_rgx)) |
| 564 | { |
| 565 | // Remove token pasting operator "##" |
| 566 | expanded_content.assign(std::regex_replace(expanded_content, std::regex(token_pasting_rgx), "")); |
| 567 | // Trim trailing whitespace |
| 568 | expanded_content.assign(std::regex_replace(expanded_content, std::regex(R"([ \t]*\\\n)"), "\n")); |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | // Do not expand the macro if the result does not have token pasting operator "##" |
| 573 | expanded_content = src.substr(match.position(), match.length() + args_str_length); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | dst.append(expanded_content); |
| 578 | break; |
| 579 | } |
| 580 | case ParserStage::SKIP_COMMENTS: |
| 581 | case ParserStage::SKIP_PREPROCESSOR_DIRECTIVES: |
| 582 | default: |
| 583 | dst.append(match.str()); |
| 584 | break; |
| 585 | } |
| 586 | next_stage = stage; |
| 587 | } |
| 588 | dst.append(cs_parser(src.substr(parsed_pos, src.length() - parsed_pos), next_stage, 0)); |
| 589 | |
| 590 | return dst; |
| 591 | }; |
| 592 | |
| 593 | return cs_parser(shader_source, ParserStage::FIRST, 0); |
| 594 | } |
| 595 | |
| 596 | const GCProgram &GCKernelLibrary::load_program(const std::string &program_name) const |
| 597 | { |
| 598 | const auto program_it = _programs_map.find(program_name); |
| 599 | |
| 600 | if(program_it != _programs_map.end()) |
| 601 | { |
| 602 | return program_it->second; |
| 603 | } |
| 604 | |
| 605 | GCProgram program; |
| 606 | |
| 607 | #ifdef EMBEDDED_KERNELS |
| 608 | const auto program_source_it = _program_source_map.find(program_name); |
| 609 | |
| 610 | if(_program_source_map.end() == program_source_it) |
| 611 | { |
| 612 | ARM_COMPUTE_ERROR("Embedded program for %s does not exist.", program_name.c_str()); |
| 613 | } |
| 614 | |
| 615 | // TODO(APPBROWSER-298): Do not call shader preprocessor here |
| 616 | // We should do the preprocess at compile time |
| 617 | // The preprocess_shader function is used for support "#include" directive and token pasting operator "##". |
| 618 | // This job could be done at compile time by using a python script in order to get better performance at runtime. |
| 619 | // BTW: We usually defined EMBEDDED_KERNELS in release build. |
| 620 | program = GCProgram(program_name, preprocess_shader(program_source_it->second)); |
| 621 | #else /* EMBEDDED_KERNELS */ |
| 622 | // Check for binary |
| 623 | std::string source_name = _shader_path + program_name; |
| 624 | if(std::ifstream(source_name).is_open()) |
| 625 | { |
| 626 | program = GCProgram(program_name, preprocess_shader(read_file(source_name, false))); |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | ARM_COMPUTE_ERROR("Shader file %s does not exist.", source_name.c_str()); |
| 631 | } |
| 632 | #endif /* EMBEDDED_KERNELS */ |
| 633 | |
| 634 | // Insert program to program map |
| 635 | const auto new_program = _programs_map.emplace(program_name, std::move(program)); |
| 636 | |
| 637 | return new_program.first->second; |
| 638 | } |
| 639 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 640 | void GCKernelLibrary::setup_dummy_fbo() |
| 641 | { |
| 642 | ARM_COMPUTE_GL_CHECK(glGenFramebuffers(1, &_frame_buffer)); |
| 643 | ARM_COMPUTE_GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, _frame_buffer)); |
| 644 | ARM_COMPUTE_GL_CHECK(glGenTextures(1, &_tex_rt)); |
| 645 | ARM_COMPUTE_GL_CHECK(glBindTexture(GL_TEXTURE_2D, _tex_rt)); |
| 646 | ARM_COMPUTE_GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr)); |
| 647 | ARM_COMPUTE_GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _tex_rt, 0)); |
| 648 | } |
| 649 | |
| 650 | GCKernelLibrary::~GCKernelLibrary() |
| 651 | { |
| 652 | for(auto &program : _built_programs_map) |
| 653 | { |
| 654 | static_cast<GCKernel>(program.second).cleanup(); |
| 655 | } |
| 656 | |
| 657 | ARM_COMPUTE_GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0)); |
| 658 | ARM_COMPUTE_GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0)); |
| 659 | ARM_COMPUTE_GL_CHECK(glDeleteTextures(1, &_tex_rt)); |
| 660 | ARM_COMPUTE_GL_CHECK(glDeleteFramebuffers(1, &_frame_buffer)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | std::string GCKernelLibrary::stringify_set(const StringSet &s) const |
| 664 | { |
| 665 | std::string concat_set; |
| 666 | |
| 667 | // Concatenate set |
| 668 | for(const auto &el : s) |
| 669 | { |
| 670 | concat_set += el + "\n"; |
| 671 | } |
| 672 | |
| 673 | return concat_set; |
| 674 | } |