aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h
blob: 0f6daf786b73193ef9b4f9b85bb2c4d45d580119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2017-2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef ARM_COMPUTE_GCKERNELLIBRARY_H
#define ARM_COMPUTE_GCKERNELLIBRARY_H

#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
#include "arm_compute/core/Utils.h"

#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>

namespace arm_compute
{
/** GCProgram class */
class GCProgram final
{
public:
    /** Default constructor. */
    GCProgram();
    /** Construct program from source file.
     *
     * @param[in] name   Program name.
     * @param[in] source Program source.
     */
    GCProgram(std::string name, std::string source);
    /** Default Copy Constructor. */
    GCProgram(const GCProgram &) = default;
    /** Default Move Constructor. */
    GCProgram(GCProgram &&) = default;
    /** Default copy assignment operator */
    GCProgram &operator=(const GCProgram &) = default;
    /** Default move assignment operator */
    GCProgram &operator=(GCProgram &&) = default;
    /** Returns program name.
     *
     * @return Program's name.
     */
    std::string name() const
    {
        return _name;
    }
    /** Link program.
     *
     * @param[in] shader Shader used to link program.
     *
     * @return linked program id .
     */
    GLuint link_program(GLuint shader);
    /** Compile shader.
     *
     * @param[in] build_options Shader build options.
     *
     * @return GLES shader object.
     */
    GLuint compile_shader(const std::string &build_options);

private:
    std::string _name;   /**< Program name. */
    std::string _source; /**< Source code for the program. */
};

/** GCKernel class */
class GCKernel final
{
public:
    /** Default Constructor. */
    GCKernel();
    /** Default Copy Constructor. */
    GCKernel(const GCKernel &) = default;
    /** Default Move Constructor. */
    GCKernel(GCKernel &&) = default;
    /** Default copy assignment operator */
    GCKernel &operator=(const GCKernel &) = default;
    /** Default move assignment operator */
    GCKernel &operator=(GCKernel &&) = default;
    /** Constructor.
     *
     * @param[in] name    Kernel name.
     * @param[in] program Built program.
     */
    GCKernel(std::string name, GLuint program);
    /** Destructor.
     */
    ~GCKernel();
    /** Returns kernel name.
     *
     * @return Kernel's name.
     */
    std::string name() const
    {
        return _name;
    }
    /** Get program id.
     *
     * @return program id.
     */
    GLuint get_program() const
    {
        return _program;
    }
    /** Use current program.
     *
     * @return program id.
     */
    void use();
    /** Unuse current program.
     *
     * @return program id.
     */
    void unuse();
    /** Set argument value at index of shader params.
     *
     * @param[in] idx   Index in shader params.
     * @param[in] value Argument value to be set.
     */
    template <class T>
    void set_argument(unsigned int idx, T value)
    {
        if(idx >= _shader_arguments.size())
        {
            _shader_arguments.resize(idx + 1, 0);
        }

        unsigned int *p        = reinterpret_cast<unsigned int *>(&value);
        _shader_arguments[idx] = *p;
    }
    /** Clear shader arguments.
     *
     */
    void clear_arguments()
    {
        _shader_arguments.clear();
    }
    /** Set shader params binding point.
     *
     * @param[in] binding Shader params binding point.
     */
    void set_shader_params_binding_point(unsigned int binding)
    {
        _shader_params_binding_point = binding;
    }
    /** Update shader params.
     *
     */
    void update_shader_params();
    /** Clean up program and ubo.
     *
     */
    void cleanup();

private:
    std::string                  _name;                                 /**< Kernel name */
    GLuint                       _program;                              /**< Linked program id */
    std::vector<unsigned int>    _shader_arguments;                     /**< Store all the values of the shader arguments */
    GLuint                       _shader_params_ubo_name;               /**< Uniform buffer object name for shader parameters */
    GLuint                       _shader_params_binding_point;          /**< The binding point of the uniform block for shader parameters */
    GLuint                       _shader_params_index;                  /**< The index of the uniform block */
    GLint                        _shader_params_size;                   /**< The uniform block data size in the shader */
    static constexpr const char *_shader_params_name = "shader_params"; /**< The uniform block name in the shader */
};

/** GCKernelLibrary class */
class GCKernelLibrary final
{
    using StringSet = std::set<std::string>;

public:
    /** Default Constructor. */
    GCKernelLibrary();
    /** Default Destructor */
    ~GCKernelLibrary();
    /** Prevent instances of this class from being copied */
    GCKernelLibrary(const GCKernelLibrary &) = delete;
    /** Prevent instances of this class from being copied */
    const GCKernelLibrary &operator=(const GCKernelLibrary &) = delete;
    /** Get the static instance of @ref GCKernelLibrary.
     * This method has been deprecated and will be removed in the next release.
     * @return The static instance of GCKernelLibrary.
     */
    static GCKernelLibrary &get();
    /** Initialises the kernel library.
     *
     * @param[in] shader_path (Optional) Path of the directory from which shader sources are loaded.
     * @param[in] dpy         (Optional) EGLdisplay set by external application.
     * @param[in] ctx         (Optional) EGLContext set by external application.
     */
    void init(std::string shader_path = "./", EGLDisplay dpy = EGL_NO_DISPLAY, EGLContext ctx = EGL_NO_CONTEXT);
    /** Sets the path that the shaders reside in.
     *
     * @param[in] shader_path Path of the shader.
     */
    void set_shader_path(const std::string &shader_path);
    /** Sets display and context to create kernel.
     *
     * @param[in] dpy EGLdisplay set by external application.
     * @param[in] ctx EGLContext set by external application.
     */
    void set_context(EGLDisplay dpy, EGLContext ctx);
    /** Creates a kernel from the kernel library.
     *
     * @param[in] shader_name       Shader name.
     * @param[in] build_options_set Shader build options as a set.
     *
     * @return The created kernel.
     */
    GCKernel create_kernel(const std::string &shader_name, const StringSet &build_options_set = {}) const;
    /** Serializes and saves programs to a binary. */
    void save_binary();
    /** Load serialized binary with all the programs. */
    void load_binary();
    /** Setup a dummy fbo to workaround an issue on Galaxy S8. */
    void setup_dummy_fbo();

private:
    /** Preprocess GLES shader
     *
     * @param[in] shader_source Source code of the shader to preprocess.
     *
     * @return Preprocessed GLES shader object.
     */
    std::string preprocess_shader(const std::string &shader_source) const;
    /** Load program and its dependencies.
     *
     * @param[in] program_name Name of the program to load.
     */
    const GCProgram &load_program(const std::string &program_name) const;
    /** Concatenates contents of a set into a single string.
     *
     * @param[in] s Input set to concatenate.
     *
     * @return Concatenated string.
     */
    std::string stringify_set(const StringSet &s) const;

    EGLDisplay  _display;                                                /**< Underlying EGL Display. */
    EGLContext  _context;                                                /**< Underlying EGL Context. */
    GLuint      _frame_buffer;                                           /**< Dummy fbo */
    GLuint      _tex_rt;                                                 /**< Dummy texture for render target */
    std::string _shader_path;                                            /**< Path to the shaders folder. */
    mutable std::map<std::string, const GCProgram>  _programs_map;       /**< Map with all already loaded program data. */
    mutable std::map<std::string, const GCKernel>   _built_programs_map; /**< Map with all already built program data. */
    static const std::map<std::string, std::string> _shader_program_map; /**< Map that associates kernel names with programs. */
    static const std::map<std::string, std::string> _program_source_map; /**< Contains sources for all programs.
                                                                              Used for compile-time shader inclusion. */
};
} // namespace arm_compute
#endif /* ARM_COMPUTE_GCKERNELLIBRARY_H */