Compute Library
 21.02
GCKernelLibrary Class Referencefinal

GCKernelLibrary class. More...

#include <GCKernelLibrary.h>

Public Member Functions

 GCKernelLibrary ()
 Default Constructor. More...
 
 ~GCKernelLibrary ()
 Default Destructor. More...
 
 GCKernelLibrary (const GCKernelLibrary &)=delete
 Prevent instances of this class from being copied. More...
 
const GCKernelLibraryoperator= (const GCKernelLibrary &)=delete
 Prevent instances of this class from being copied. More...
 
void init (std::string shader_path="./", EGLDisplay dpy=EGL_NO_DISPLAY, EGLContext ctx=EGL_NO_CONTEXT)
 Initialises the kernel library. More...
 
void set_shader_path (const std::string &shader_path)
 Sets the path that the shaders reside in. More...
 
void set_context (EGLDisplay dpy, EGLContext ctx)
 Sets display and context to create kernel. More...
 
GCKernel create_kernel (const std::string &shader_name, const StringSet &build_options_set={}) const
 Creates a kernel from the kernel library. More...
 
void save_binary ()
 Serializes and saves programs to a binary. More...
 
void load_binary ()
 Load serialized binary with all the programs. More...
 
void setup_dummy_fbo ()
 Setup a dummy fbo to workaround an issue on Galaxy S8. More...
 

Static Public Member Functions

static GCKernelLibraryget ()
 Get the static instance of GCKernelLibrary. More...
 

Detailed Description

GCKernelLibrary class.

Definition at line 187 of file GCKernelLibrary.h.

Constructor & Destructor Documentation

◆ GCKernelLibrary() [1/2]

Default Constructor.

Definition at line 328 of file GCKernelLibrary.cpp.

329  : _display(EGL_NO_DISPLAY), _context(EGL_NO_CONTEXT), _frame_buffer(0), _tex_rt(0), _shader_path("./"), _programs_map(), _built_programs_map()
330 {
331 }

◆ ~GCKernelLibrary()

Default Destructor.

Definition at line 535 of file GCKernelLibrary.cpp.

References ARM_COMPUTE_GL_CHECK, glBindFramebuffer(), glBindTexture(), glDeleteFramebuffers(), and glDeleteTextures().

536 {
537  for(auto &program : _built_programs_map)
538  {
539  static_cast<GCKernel>(program.second).cleanup();
540  }
541 
542  ARM_COMPUTE_GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
543  ARM_COMPUTE_GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
545  ARM_COMPUTE_GL_CHECK(glDeleteFramebuffers(1, &_frame_buffer));
546 }
#define ARM_COMPUTE_GL_CHECK(x)
Definition: OpenGLES.h:45
void GL_APIENTRY glBindTexture(GLenum target, GLuint texture)
Definition: OpenGLES.cpp:750
void GL_APIENTRY glDeleteTextures(GLsizei n, const GLuint *textures)
Definition: OpenGLES.cpp:737
void GL_APIENTRY glBindFramebuffer(GLenum target, GLuint framebuffer)
Definition: OpenGLES.cpp:802
GCKernel class.
void GL_APIENTRY glDeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
Definition: OpenGLES.cpp:789

◆ GCKernelLibrary() [2/2]

GCKernelLibrary ( const GCKernelLibrary )
delete

Prevent instances of this class from being copied.

Member Function Documentation

◆ create_kernel()

GCKernel create_kernel ( const std::string &  shader_name,
const StringSet &  build_options_set = {} 
) const

Creates a kernel from the kernel library.

Parameters
[in]shader_nameShader name.
[in]build_options_setShader build options as a set.
Returns
The created kernel.

Definition at line 366 of file GCKernelLibrary.cpp.

References ARM_COMPUTE_ERROR_VAR, build_options, GCKernel::clear_arguments(), GCProgram::compile_shader(), arm_compute::test::validation::dst, GCProgram::link_program(), arm_compute::read_file(), GCKernel::set_shader_params_binding_point(), arm_compute::test::validation::src, and GCKernel::use().

Referenced by GCLogits1DMaxKernel::configure(), GCScaleKernel::configure(), GCTransposeKernel::configure(), GCGEMMMatrixAccumulateBiasesKernel::configure(), GCPixelWiseMultiplicationKernel::configure(), GCDepthwiseConvolutionLayer3x3Kernel::configure(), GCFillBorderKernel::configure(), GCNormalizationLayerKernel::configure(), GCPoolingLayerKernel::configure(), GCGEMMMatrixAdditionKernel::configure(), GCNormalizePlanarYUVLayerKernel::configure(), GCAbsoluteDifferenceKernel::configure(), GCArithmeticAdditionKernel::configure(), GCGEMMTranspose1xWKernel::configure(), GCDirectConvolutionLayerKernel< kernel_size >::configure(), GCBatchNormalizationLayerKernel::configure(), GCDepthConcatenateLayerKernel::configure(), GCDropoutLayerKernel::configure(), GCLogits1DShiftExpSumKernel::configure(), GCGEMMMatrixMultiplyKernel::configure(), GCGEMMInterleave4x4Kernel::configure(), GCTensorShiftKernel::configure(), GCWeightsReshapeKernel::configure(), GCCol2ImKernel::configure(), GCIm2ColKernel::configure(), GCLogits1DNormKernel::configure(), and arm_compute::create_opengl_kernel().

367 {
368  // Find which program contains the kernel
369  auto shader_program_it = _shader_program_map.find(shader_name);
370 
371  if(_shader_program_map.end() == shader_program_it)
372  {
373  ARM_COMPUTE_ERROR_VAR("Shader %s not found in the GCKernelLibrary", shader_name.c_str());
374  }
375 
376  // Check if the program has been built before with same build options.
377  const std::string program_name = shader_program_it->second;
378  const std::string build_options = stringify_set(build_options_set);
379  const std::string built_program_name = program_name + "_" + build_options;
380  auto built_program_it = _built_programs_map.find(built_program_name);
381 
382  GCKernel kernel;
383 
384  if(_built_programs_map.end() != built_program_it)
385  {
386  // If program has been built, retrieve to create kernel from it
387  kernel = built_program_it->second;
388  }
389  else
390  {
391  GCProgram program = load_program(program_name);
392 
393  std::string source_name = _shader_path + shader_program_it->second;
394 
395  // load shader
396  GLuint shader = program.compile_shader(build_options);
397 
398  // Build program
399  GLuint gles_program = program.link_program(shader);
400 
401  // Create GCKernel
402  kernel = GCKernel(shader_name, gles_program);
403 
404  // Add built program to internal map
405  _built_programs_map.emplace(built_program_name, kernel);
406  }
407 
408  kernel.use();
409  kernel.clear_arguments();
410  // set shader params binding point
412 
413  return kernel;
414 }
GLuint compile_shader(const std::string &build_options)
Compile shader.
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:346
GCKernel class.
void use()
Use current program.
GCProgram class.
void set_shader_params_binding_point(unsigned int binding)
Set shader params binding point.
std::set< std::string > build_options
GLuint link_program(GLuint shader)
Link program.
void clear_arguments()
Clear shader arguments.

◆ get()

GCKernelLibrary & get ( )
static

◆ init()

void init ( std::string  shader_path = "./",
EGLDisplay  dpy = EGL_NO_DISPLAY,
EGLContext  ctx = EGL_NO_CONTEXT 
)

Initialises the kernel library.

Parameters
[in]shader_path(Optional) Path of the directory from which shader sources are loaded.
[in]dpy(Optional) EGLdisplay set by external application.
[in]ctx(Optional) EGLContext set by external application.

Definition at line 339 of file GCKernelLibrary.cpp.

References eglMakeCurrent(), and GCKernelLibrary::setup_dummy_fbo().

Referenced by GCRuntimeContext::GCRuntimeContext(), and GCScheduler::init().

340 {
341  //TODO: deal with old display and context.
342  _shader_path = std::move(shader_path);
343 
344  _display = dpy;
345  _context = ctx;
346 
347  eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, _context);
348  setup_dummy_fbo();
349 }
EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
Definition: OpenGLES.cpp:256
void setup_dummy_fbo()
Setup a dummy fbo to workaround an issue on Galaxy S8.

◆ load_binary()

void load_binary ( )

Load serialized binary with all the programs.

◆ operator=()

const GCKernelLibrary& operator= ( const GCKernelLibrary )
delete

Prevent instances of this class from being copied.

◆ save_binary()

void save_binary ( )

Serializes and saves programs to a binary.

◆ set_context()

void set_context ( EGLDisplay  dpy,
EGLContext  ctx 
)

Sets display and context to create kernel.

Parameters
[in]dpyEGLdisplay set by external application.
[in]ctxEGLContext set by external application.

Definition at line 356 of file GCKernelLibrary.cpp.

References eglMakeCurrent(), and GCKernelLibrary::setup_dummy_fbo().

357 {
358  //TODO: deal with old display and context.
359  _display = dpy;
360  _context = ctx;
361 
362  eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
363  setup_dummy_fbo();
364 }
EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
Definition: OpenGLES.cpp:256
void setup_dummy_fbo()
Setup a dummy fbo to workaround an issue on Galaxy S8.

◆ set_shader_path()

void set_shader_path ( const std::string &  shader_path)

Sets the path that the shaders reside in.

Parameters
[in]shader_pathPath of the shader.

Definition at line 351 of file GCKernelLibrary.cpp.

352 {
353  _shader_path = shader_path;
354 }

◆ setup_dummy_fbo()

void setup_dummy_fbo ( )

Setup a dummy fbo to workaround an issue on Galaxy S8.

Definition at line 525 of file GCKernelLibrary.cpp.

References ARM_COMPUTE_GL_CHECK, glBindFramebuffer(), glBindTexture(), glFramebufferTexture2D(), glGenFramebuffers(), glGenTextures(), and glTexImage2D().

Referenced by GCKernelLibrary::init(), and GCKernelLibrary::set_context().

526 {
527  ARM_COMPUTE_GL_CHECK(glGenFramebuffers(1, &_frame_buffer));
528  ARM_COMPUTE_GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, _frame_buffer));
529  ARM_COMPUTE_GL_CHECK(glGenTextures(1, &_tex_rt));
530  ARM_COMPUTE_GL_CHECK(glBindTexture(GL_TEXTURE_2D, _tex_rt));
531  ARM_COMPUTE_GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr));
532  ARM_COMPUTE_GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _tex_rt, 0));
533 }
void GL_APIENTRY glGenTextures(GLsizei n, GLuint *textures)
Definition: OpenGLES.cpp:724
#define ARM_COMPUTE_GL_CHECK(x)
Definition: OpenGLES.h:45
void GL_APIENTRY glBindTexture(GLenum target, GLuint texture)
Definition: OpenGLES.cpp:750
void GL_APIENTRY glBindFramebuffer(GLenum target, GLuint framebuffer)
Definition: OpenGLES.cpp:802
void GL_APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
Definition: OpenGLES.cpp:763
void GL_APIENTRY glFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
Definition: OpenGLES.cpp:815
void GL_APIENTRY glGenFramebuffers(GLsizei n, GLuint *framebuffers)
Definition: OpenGLES.cpp:776

The documentation for this class was generated from the following files: