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 | */ |
| 24 | |
| 25 | #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h" |
| 26 | |
Michele Di Giorgio | b8fc60f | 2018-04-25 11:58:07 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" |
| 29 | |
| 30 | using namespace arm_compute; |
| 31 | |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 32 | std::once_flag GCScheduler::_initialize_symbols; |
| 33 | |
| 34 | GCScheduler::GCScheduler() |
Michele Di Giorgio | b8fc60f | 2018-04-25 11:58:07 +0100 | [diff] [blame] | 35 | : _display(EGL_NO_DISPLAY), _context(EGL_NO_CONTEXT), _target(GPUTarget::MIDGARD) |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | GCScheduler::~GCScheduler() |
| 40 | { |
| 41 | eglDestroyContext(_display, _context); |
| 42 | eglTerminate(_display); |
| 43 | |
| 44 | _context = EGL_NO_CONTEXT; |
| 45 | _display = EGL_NO_DISPLAY; |
| 46 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 47 | |
| 48 | void GCScheduler::default_init() |
| 49 | { |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 50 | setup_context(); |
| 51 | |
Michele Di Giorgio | b8fc60f | 2018-04-25 11:58:07 +0100 | [diff] [blame] | 52 | init(_display, _context); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Georgios Pinitas | 7ae80a9 | 2019-10-25 18:25:17 +0100 | [diff] [blame] | 55 | void GCScheduler::default_init_with_context(EGLDisplay display, EGLContext ctx) |
| 56 | { |
| 57 | _context = ctx; |
| 58 | _display = display; |
| 59 | |
| 60 | _target = get_target_from_device(); |
| 61 | } |
| 62 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 63 | void GCScheduler::init(EGLDisplay dpy, EGLContext ctx) |
| 64 | { |
Michele Di Giorgio | b8fc60f | 2018-04-25 11:58:07 +0100 | [diff] [blame] | 65 | _target = get_target_from_device(); |
| 66 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 67 | GCKernelLibrary::get().init("./cs_shaders/", dpy, ctx); |
| 68 | } |
| 69 | |
| 70 | GCScheduler &GCScheduler::get() |
| 71 | { |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 72 | std::call_once(_initialize_symbols, opengles31_is_available); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 73 | static GCScheduler scheduler; |
| 74 | return scheduler; |
| 75 | } |
| 76 | |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 77 | void GCScheduler::dispatch(IGCKernel &kernel, bool flush) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 78 | { |
| 79 | kernel.run(kernel.window()); |
| 80 | if(flush) |
| 81 | { |
| 82 | ARM_COMPUTE_GL_CHECK(glFlush()); |
| 83 | } |
| 84 | } |
| 85 | |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 86 | void GCScheduler::memory_barrier() |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 87 | { |
| 88 | ARM_COMPUTE_GL_CHECK(glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT)); |
| 89 | } |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 90 | |
| 91 | void GCScheduler::setup_context() |
| 92 | { |
| 93 | EGLBoolean res; |
| 94 | _display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 95 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 96 | ARM_COMPUTE_ERROR_ON_MSG_VAR(_display == EGL_NO_DISPLAY, "Failed to get display: 0x%x.", eglGetError()); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 97 | |
| 98 | res = eglInitialize(_display, nullptr, nullptr); |
| 99 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to initialize egl: 0x%x.", eglGetError()); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 101 | ARM_COMPUTE_UNUSED(res); |
| 102 | |
| 103 | const char *egl_extension_st = eglQueryString(_display, EGL_EXTENSIONS); |
| 104 | ARM_COMPUTE_ERROR_ON_MSG((strstr(egl_extension_st, "EGL_KHR_create_context") == nullptr), "Failed to query EGL_KHR_create_context"); |
| 105 | ARM_COMPUTE_ERROR_ON_MSG((strstr(egl_extension_st, "EGL_KHR_surfaceless_context") == nullptr), "Failed to query EGL_KHR_surfaceless_context"); |
| 106 | ARM_COMPUTE_UNUSED(egl_extension_st); |
| 107 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 108 | const std::array<EGLint, 3> config_attribs = |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 109 | { |
| 110 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR, |
| 111 | EGL_NONE |
| 112 | }; |
| 113 | EGLConfig cfg; |
| 114 | EGLint count; |
| 115 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 116 | res = eglChooseConfig(_display, config_attribs.data(), &cfg, 1, &count); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 117 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to choose config: 0x%x.", eglGetError()); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 119 | ARM_COMPUTE_UNUSED(res); |
| 120 | |
| 121 | res = eglBindAPI(EGL_OPENGL_ES_API); |
| 122 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 123 | ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to bind api: 0x%x.", eglGetError()); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 124 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 125 | const std::array<EGLint, 3> attribs = |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 126 | { |
| 127 | EGL_CONTEXT_CLIENT_VERSION, 3, |
| 128 | EGL_NONE |
| 129 | }; |
| 130 | _context = eglCreateContext(_display, |
| 131 | cfg, |
| 132 | EGL_NO_CONTEXT, |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 133 | attribs.data()); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 134 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_ERROR_ON_MSG_VAR(_context == EGL_NO_CONTEXT, "Failed to create context: 0x%x.", eglGetError()); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 136 | ARM_COMPUTE_UNUSED(res); |
| 137 | |
| 138 | res = eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, _context); |
| 139 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 140 | ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to make current: 0x%x.", eglGetError()); |
Ioan-Cristian Szabo | 77eb21f | 2017-12-22 17:32:17 +0000 | [diff] [blame] | 141 | ARM_COMPUTE_UNUSED(res); |
| 142 | } |