• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_LITE_TOOLS_COMMON_OPENGL_UTIL_H_
18 #define MINDSPORE_LITE_TOOLS_COMMON_OPENGL_UTIL_H_
19 
20 #include <map>
21 #include <vector>
22 #include <utility>
23 #include "src/common/log_adapter.h"
24 #include "nnacl/op_base.h"
25 
26 #if defined(GPU_OPENCL) && defined(__ANDROID__) && defined(ENABLE_ARM64)
27 #include "EGL/egl.h"
28 #include "GLES3/gl3.h"
29 #include "GLES3/gl32.h"
30 #else
31 typedef unsigned int GLuint;
32 typedef unsigned int GLenum;
33 typedef signed int khronos_ssize_t;
34 typedef khronos_ssize_t GLsizeiptr;
35 #define GL_RGBA32F 0x8814
36 #define GL_TEXTURE_2D 0x0DE1
37 #define GL_SHADER_STORAGE_BUFFER 0x90D2
38 #define GL_DYNAMIC_DRAW 0x88E8
39 #define GL_NONE 0x00
40 typedef void *EGLContext;
41 typedef void *EGLSurface;
42 typedef void *EGLDisplay;
43 
eglGetCurrentContext(void)44 inline EGLContext eglGetCurrentContext(void) { return nullptr; }
eglGetCurrentDisplay(void)45 inline EGLContext eglGetCurrentDisplay(void) { return nullptr; }
46 #endif
47 
48 #define OPEN_GL_CHECK_ERROR
49 
50 #ifdef OPEN_GL_CHECK_ERROR
51 #define OPENGL_CHECK_ERROR                                  \
52   {                                                         \
53     GLenum error = glGetError();                            \
54     if (GL_NO_ERROR != error) {                             \
55       MS_LOG(ERROR) << "ERR CHECK Fail, error = " << error; \
56       return 0;                                             \
57     }                                                       \
58     MS_ASSERT(GL_NO_ERROR == error);                        \
59   }
60 #else
61 #define OPENGL_CHECK_ERROR
62 #endif
63 
64 namespace mindspore {
65 namespace OpenGL {
66 #define BIND_INDEX_0 0
67 #define BIND_INDEX_1 1
68 #define BIND_INDEX_2 2
69 #define BIND_INDEX_3 3
70 #define BIND_INDEX_4 4
71 
72 class OpenGLRuntime {
73  public:
OpenGLRuntime()74   OpenGLRuntime() {}
~OpenGLRuntime()75   virtual ~OpenGLRuntime() {}
76 
77   bool Init();
78   GLuint GLCreateTexture(int w, int h, int c, GLenum textrueFormat = GL_RGBA32F, GLenum target = GL_TEXTURE_2D);
79 
80   GLuint CopyHostToDeviceTexture(void *hostData, int width, int height, int channel);
81   void *CopyDeviceTextureToHost(GLuint textureID);
82 
83   void PrintImage2DData(float *data, int w, int h, int c = 4);
84 
85  private:
86   static GLuint LoadShader(GLenum shaderType, const char *pSource);
87   static GLuint CreateComputeProgram(const char *pComputeSource);
88   GLuint GLCreateSSBO(GLsizeiptr size, void *hostData = nullptr, GLenum type = GL_SHADER_STORAGE_BUFFER,
89                       GLenum usage = GL_DYNAMIC_DRAW);
90   bool CopyDeviceSSBOToHost(GLuint ssboBufferID, void *hostData, GLsizeiptr size);
91   bool CopyHostToDeviceSSBO(void *hostData, GLuint ssboBufferID, GLsizeiptr size);
92 
93   bool CopyDeviceTextureToSSBO(GLuint textureID, GLuint ssboBufferID);
94   bool CopyDeviceSSBOToTexture(GLuint ssboBufferID, GLuint textureID);
95 
96   std::map<GLuint, std::pair<GLsizeiptr, GLenum>> m_ssbo_pool_;
97   std::map<GLuint, std::pair<std::vector<int>, std::vector<GLenum>>> m_texture_pool_;
98   EGLContext m_context_ = nullptr;
99   EGLDisplay m_display_ = nullptr;
100   EGLSurface m_surface_ = nullptr;
101 };
102 }  // namespace OpenGL
103 }  // namespace mindspore
104 
105 #endif  // MINDSPORE_LITE_TOOLS_COMMON_OPENGL_UTIL_H_
106