1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef libGLESv2_hpp 16 #define libGLESv2_hpp 17 18 #include <GLES/gl.h> 19 #include <GLES/glext.h> 20 #include <EGL/egl.h> 21 22 #include "Common/SharedLibrary.hpp" 23 24 namespace sw 25 { 26 class FrameBuffer; 27 enum Format : unsigned char; 28 } 29 30 namespace egl 31 { 32 class Display; 33 class Context; 34 class Image; 35 class Config; 36 class ClientBuffer; 37 } 38 39 class LibGLESv2exports 40 { 41 public: 42 LibGLESv2exports(); 43 44 void (*glActiveTexture)(GLenum texture); 45 void (*glAttachShader)(GLuint program, GLuint shader); 46 void (*glBeginQueryEXT)(GLenum target, GLuint name); 47 void (*glBindAttribLocation)(GLuint program, GLuint index, const GLchar* name); 48 void (*glBindBuffer)(GLenum target, GLuint buffer); 49 void (*glBindFramebuffer)(GLenum target, GLuint framebuffer); 50 void (*glBindRenderbuffer)(GLenum target, GLuint renderbuffer); 51 void (*glBindTexture)(GLenum target, GLuint texture); 52 void (*glBlendColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); 53 void (*glBlendEquation)(GLenum mode); 54 void (*glBlendEquationSeparate)(GLenum modeRGB, GLenum modeAlpha); 55 void (*glBlendFunc)(GLenum sfactor, GLenum dfactor); 56 void (*glBlendFuncSeparate)(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha); 57 void (*glBufferData)(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage); 58 void (*glBufferSubData)(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data); 59 GLenum (*glCheckFramebufferStatus)(GLenum target); 60 void (*glClear)(GLbitfield mask); 61 void (*glClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); 62 void (*glClearDepthf)(GLclampf depth); 63 void (*glClearStencil)(GLint s); 64 void (*glColorMask)(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha); 65 void (*glCompileShader)(GLuint shader); 66 void (*glCompressedTexImage2D)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, 67 GLint border, GLsizei imageSize, const GLvoid* data); 68 void (*glCompressedTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, 69 GLenum format, GLsizei imageSize, const GLvoid* data); 70 void (*glCopyTexImage2D)(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border); 71 void (*glCopyTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height); 72 GLuint (*glCreateProgram)(void); 73 GLuint (*glCreateShader)(GLenum type); 74 void (*glCullFace)(GLenum mode); 75 void (*glDeleteBuffers)(GLsizei n, const GLuint* buffers); 76 void (*glDeleteFencesNV)(GLsizei n, const GLuint* fences); 77 void (*glDeleteFramebuffers)(GLsizei n, const GLuint* framebuffers); 78 void (*glDeleteProgram)(GLuint program); 79 void (*glDeleteQueriesEXT)(GLsizei n, const GLuint *ids); 80 void (*glDeleteRenderbuffers)(GLsizei n, const GLuint* renderbuffers); 81 void (*glDeleteShader)(GLuint shader); 82 void (*glDeleteTextures)(GLsizei n, const GLuint* textures); 83 void (*glDepthFunc)(GLenum func); 84 void (*glDepthMask)(GLboolean flag); 85 void (*glDepthRangef)(GLclampf zNear, GLclampf zFar); 86 void (*glDetachShader)(GLuint program, GLuint shader); 87 void (*glDisable)(GLenum cap); 88 void (*glDisableVertexAttribArray)(GLuint index); 89 void (*glDrawArrays)(GLenum mode, GLint first, GLsizei count); 90 void (*glDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices); 91 void (*glDrawArraysInstancedEXT)(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount); 92 void (*glDrawElementsInstancedEXT)(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei instanceCount); 93 void (*glVertexAttribDivisorEXT)(GLuint index, GLuint divisor); 94 void (*glDrawArraysInstancedANGLE)(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount); 95 void (*glDrawElementsInstancedANGLE)(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei instanceCount); 96 void (*glVertexAttribDivisorANGLE)(GLuint index, GLuint divisor); 97 void (*glEnable)(GLenum cap); 98 void (*glEnableVertexAttribArray)(GLuint index); 99 void (*glEndQueryEXT)(GLenum target); 100 void (*glFinishFenceNV)(GLuint fence); 101 void (*glFinish)(void); 102 void (*glFlush)(void); 103 void (*glFramebufferRenderbuffer)(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer); 104 void (*glFramebufferTexture2D)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level); 105 void (*glFrontFace)(GLenum mode); 106 void (*glGenBuffers)(GLsizei n, GLuint* buffers); 107 void (*glGenerateMipmap)(GLenum target); 108 void (*glGenFencesNV)(GLsizei n, GLuint* fences); 109 void (*glGenFramebuffers)(GLsizei n, GLuint* framebuffers); 110 void (*glGenQueriesEXT)(GLsizei n, GLuint* ids); 111 void (*glGenRenderbuffers)(GLsizei n, GLuint* renderbuffers); 112 void (*glGenTextures)(GLsizei n, GLuint* textures); 113 void (*glGetActiveAttrib)(GLuint program, GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name); 114 void (*glGetActiveUniform)(GLuint program, GLuint index, GLsizei bufsize, GLsizei* length, GLint* size, GLenum* type, GLchar* name); 115 void (*glGetAttachedShaders)(GLuint program, GLsizei maxcount, GLsizei* count, GLuint* shaders); 116 int (*glGetAttribLocation)(GLuint program, const GLchar* name); 117 void (*glGetBooleanv)(GLenum pname, GLboolean* params); 118 void (*glGetBufferParameteriv)(GLenum target, GLenum pname, GLint* params); 119 GLenum (*glGetError)(void); 120 void (*glGetFenceivNV)(GLuint fence, GLenum pname, GLint *params); 121 void (*glGetFloatv)(GLenum pname, GLfloat* params); 122 void (*glGetFramebufferAttachmentParameteriv)(GLenum target, GLenum attachment, GLenum pname, GLint* params); 123 GLenum (*glGetGraphicsResetStatusEXT)(void); 124 void (*glGetIntegerv)(GLenum pname, GLint* params); 125 void (*glGetProgramiv)(GLuint program, GLenum pname, GLint* params); 126 void (*glGetProgramInfoLog)(GLuint program, GLsizei bufsize, GLsizei* length, GLchar* infolog); 127 void (*glGetQueryivEXT)(GLenum target, GLenum pname, GLint *params); 128 void (*glGetQueryObjectuivEXT)(GLuint name, GLenum pname, GLuint *params); 129 void (*glGetRenderbufferParameteriv)(GLenum target, GLenum pname, GLint* params); 130 void (*glGetShaderiv)(GLuint shader, GLenum pname, GLint* params); 131 void (*glGetShaderInfoLog)(GLuint shader, GLsizei bufsize, GLsizei* length, GLchar* infolog); 132 void (*glGetShaderPrecisionFormat)(GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision); 133 void (*glGetShaderSource)(GLuint shader, GLsizei bufsize, GLsizei* length, GLchar* source); 134 const GLubyte* (*glGetString)(GLenum name); 135 void (*glGetTexParameterfv)(GLenum target, GLenum pname, GLfloat* params); 136 void (*glGetTexParameteriv)(GLenum target, GLenum pname, GLint* params); 137 void (*glGetnUniformfvEXT)(GLuint program, GLint location, GLsizei bufSize, GLfloat* params); 138 void (*glGetUniformfv)(GLuint program, GLint location, GLfloat* params); 139 void (*glGetnUniformivEXT)(GLuint program, GLint location, GLsizei bufSize, GLint* params); 140 void (*glGetUniformiv)(GLuint program, GLint location, GLint* params); 141 int (*glGetUniformLocation)(GLuint program, const GLchar* name); 142 void (*glGetVertexAttribfv)(GLuint index, GLenum pname, GLfloat* params); 143 void (*glGetVertexAttribiv)(GLuint index, GLenum pname, GLint* params); 144 void (*glGetVertexAttribPointerv)(GLuint index, GLenum pname, GLvoid** pointer); 145 void (*glHint)(GLenum target, GLenum mode); 146 GLboolean (*glIsBuffer)(GLuint buffer); 147 GLboolean (*glIsEnabled)(GLenum cap); 148 GLboolean (*glIsFenceNV)(GLuint fence); 149 GLboolean (*glIsFramebuffer)(GLuint framebuffer); 150 GLboolean (*glIsProgram)(GLuint program); 151 GLboolean (*glIsQueryEXT)(GLuint name); 152 GLboolean (*glIsRenderbuffer)(GLuint renderbuffer); 153 GLboolean (*glIsShader)(GLuint shader); 154 GLboolean (*glIsTexture)(GLuint texture); 155 void (*glLineWidth)(GLfloat width); 156 void (*glLinkProgram)(GLuint program); 157 void (*glPixelStorei)(GLenum pname, GLint param); 158 void (*glPolygonOffset)(GLfloat factor, GLfloat units); 159 void (*glReadnPixelsEXT)(GLint x, GLint y, GLsizei width, GLsizei height, 160 GLenum format, GLenum type, GLsizei bufSize, GLvoid *data); 161 void (*glReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels); 162 void (*glReleaseShaderCompiler)(void); 163 void (*glRenderbufferStorageMultisample)(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height); 164 void (*glRenderbufferStorageMultisampleANGLE)(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height); 165 void (*glRenderbufferStorage)(GLenum target, GLenum internalformat, GLsizei width, GLsizei height); 166 void (*glSampleCoverage)(GLclampf value, GLboolean invert); 167 void (*glSetFenceNV)(GLuint fence, GLenum condition); 168 void (*glScissor)(GLint x, GLint y, GLsizei width, GLsizei height); 169 void (*glShaderBinary)(GLsizei n, const GLuint* shaders, GLenum binaryformat, const GLvoid* binary, GLsizei length); 170 void (*glShaderSource)(GLuint shader, GLsizei count, const GLchar *const *string, const GLint *length); 171 void (*glStencilFunc)(GLenum func, GLint ref, GLuint mask); 172 void (*glStencilFuncSeparate)(GLenum face, GLenum func, GLint ref, GLuint mask); 173 void (*glStencilMask)(GLuint mask); 174 void (*glStencilMaskSeparate)(GLenum face, GLuint mask); 175 void (*glStencilOp)(GLenum fail, GLenum zfail, GLenum zpass); 176 void (*glStencilOpSeparate)(GLenum face, GLenum fail, GLenum zfail, GLenum zpass); 177 GLboolean (*glTestFenceNV)(GLuint fence); 178 void (*glTexImage2D)(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, 179 GLint border, GLenum format, GLenum type, const GLvoid* pixels); 180 void (*glTexParameterf)(GLenum target, GLenum pname, GLfloat param); 181 void (*glTexParameterfv)(GLenum target, GLenum pname, const GLfloat* params); 182 void (*glTexParameteri)(GLenum target, GLenum pname, GLint param); 183 void (*glTexParameteriv)(GLenum target, GLenum pname, const GLint* params); 184 void (*glTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, 185 GLenum format, GLenum type, const GLvoid* pixels); 186 void (*glUniform1f)(GLint location, GLfloat x); 187 void (*glUniform1fv)(GLint location, GLsizei count, const GLfloat* v); 188 void (*glUniform1i)(GLint location, GLint x); 189 void (*glUniform1iv)(GLint location, GLsizei count, const GLint* v); 190 void (*glUniform2f)(GLint location, GLfloat x, GLfloat y); 191 void (*glUniform2fv)(GLint location, GLsizei count, const GLfloat* v); 192 void (*glUniform2i)(GLint location, GLint x, GLint y); 193 void (*glUniform2iv)(GLint location, GLsizei count, const GLint* v); 194 void (*glUniform3f)(GLint location, GLfloat x, GLfloat y, GLfloat z); 195 void (*glUniform3fv)(GLint location, GLsizei count, const GLfloat* v); 196 void (*glUniform3i)(GLint location, GLint x, GLint y, GLint z); 197 void (*glUniform3iv)(GLint location, GLsizei count, const GLint* v); 198 void (*glUniform4f)(GLint location, GLfloat x, GLfloat y, GLfloat z, GLfloat w); 199 void (*glUniform4fv)(GLint location, GLsizei count, const GLfloat* v); 200 void (*glUniform4i)(GLint location, GLint x, GLint y, GLint z, GLint w); 201 void (*glUniform4iv)(GLint location, GLsizei count, const GLint* v); 202 void (*glUniformMatrix2fv)(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); 203 void (*glUniformMatrix3fv)(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); 204 void (*glUniformMatrix4fv)(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); 205 void (*glUseProgram)(GLuint program); 206 void (*glValidateProgram)(GLuint program); 207 void (*glVertexAttrib1f)(GLuint index, GLfloat x); 208 void (*glVertexAttrib1fv)(GLuint index, const GLfloat* values); 209 void (*glVertexAttrib2f)(GLuint index, GLfloat x, GLfloat y); 210 void (*glVertexAttrib2fv)(GLuint index, const GLfloat* values); 211 void (*glVertexAttrib3f)(GLuint index, GLfloat x, GLfloat y, GLfloat z); 212 void (*glVertexAttrib3fv)(GLuint index, const GLfloat* values); 213 void (*glVertexAttrib4f)(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w); 214 void (*glVertexAttrib4fv)(GLuint index, const GLfloat* values); 215 void (*glVertexAttribPointer)(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* ptr); 216 void (*glViewport)(GLint x, GLint y, GLsizei width, GLsizei height); 217 void (*glBlitFramebufferNV)(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter); 218 void (*glBlitFramebufferANGLE)(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, 219 GLbitfield mask, GLenum filter); 220 void (*glTexImage3DOES)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, 221 GLint border, GLenum format, GLenum type, const GLvoid* pixels); 222 void (*glTexSubImage3DOES)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels); 223 void (*glCopyTexSubImage3DOES)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height); 224 void (*glCompressedTexImage3DOES)(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data); 225 void (*glCompressedTexSubImage3DOES)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const void *data); 226 void (*glFramebufferTexture3DOES)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset); 227 void (*glEGLImageTargetTexture2DOES)(GLenum target, GLeglImageOES image); 228 void (*glEGLImageTargetRenderbufferStorageOES)(GLenum target, GLeglImageOES image); 229 GLboolean (*glIsRenderbufferOES)(GLuint renderbuffer); 230 void (*glBindRenderbufferOES)(GLenum target, GLuint renderbuffer); 231 void (*glDeleteRenderbuffersOES)(GLsizei n, const GLuint* renderbuffers); 232 void (*glGenRenderbuffersOES)(GLsizei n, GLuint* renderbuffers); 233 void (*glRenderbufferStorageOES)(GLenum target, GLenum internalformat, GLsizei width, GLsizei height); 234 void (*glGetRenderbufferParameterivOES)(GLenum target, GLenum pname, GLint* params); 235 GLboolean (*glIsFramebufferOES)(GLuint framebuffer); 236 void (*glBindFramebufferOES)(GLenum target, GLuint framebuffer); 237 void (*glDeleteFramebuffersOES)(GLsizei n, const GLuint* framebuffers); 238 void (*glGenFramebuffersOES)(GLsizei n, GLuint* framebuffers); 239 GLenum (*glCheckFramebufferStatusOES)(GLenum target); 240 void (*glFramebufferRenderbufferOES)(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer); 241 void (*glFramebufferTexture2DOES)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level); 242 void (*glGetFramebufferAttachmentParameterivOES)(GLenum target, GLenum attachment, GLenum pname, GLint* params); 243 void (*glGenerateMipmapOES)(GLenum target); 244 void (*glDrawBuffersEXT)(GLsizei n, const GLenum *bufs); 245 246 egl::Context *(*es2CreateContext)(egl::Display *display, const egl::Context *shareContext, const egl::Config *config); 247 __eglMustCastToProperFunctionPointerType (*es2GetProcAddress)(const char *procname); 248 egl::Image *(*createBackBuffer)(int width, int height, sw::Format format, int multiSampleDepth); 249 egl::Image *(*createBackBufferFromClientBuffer)(const egl::ClientBuffer& clientBuffer); 250 egl::Image *(*createDepthStencil)(int width, int height, sw::Format format, int multiSampleDepth); 251 sw::FrameBuffer *(*createFrameBuffer)(void *nativeDisplay, EGLNativeWindowType window, int width, int height); 252 }; 253 254 class LibGLESv2 255 { 256 public: 257 LibGLESv2() 258 { 259 } 260 261 ~LibGLESv2() 262 { 263 freeLibrary(libGLESv2); 264 } 265 266 operator bool() 267 { 268 return loadExports() != nullptr; 269 } 270 271 LibGLESv2exports *operator->() 272 { 273 return loadExports(); 274 } 275 276 private: 277 LibGLESv2exports *loadExports() 278 { 279 if(!loadLibraryAttempted && !libGLESv2) 280 { 281 #if defined(_WIN32) 282 #if defined(__LP64__) 283 const char *libGLESv2_lib[] = {"libGLESv2.dll", "lib64GLES_V2_translator.dll"}; 284 #else 285 const char *libGLESv2_lib[] = {"libGLESv2.dll", "libGLES_V2_translator.dll"}; 286 #endif 287 #elif defined(__ANDROID__) 288 const char *libGLESv2_lib[] = {"libGLESv2_swiftshader.so", "libGLESv2_swiftshader.so"}; 289 #elif defined(__linux__) 290 #if defined(__LP64__) 291 const char *libGLESv2_lib[] = {"lib64GLES_V2_translator.so", "libGLESv2.so.2", "libGLESv2.so"}; 292 #else 293 const char *libGLESv2_lib[] = {"libGLES_V2_translator.so", "libGLESv2.so.2", "libGLESv2.so"}; 294 #endif 295 #elif defined(__APPLE__) 296 #if defined(__LP64__) 297 const char *libGLESv2_lib[] = {"libswiftshader_libGLESv2.dylib", "lib64GLES_V2_translator.dylib", "libGLESv2.dylib"}; 298 #else 299 const char *libGLESv2_lib[] = {"libswiftshader_libGLESv2.dylib", "libGLES_V2_translator.dylib", "libGLESv2.dylib"}; 300 #endif 301 #elif defined(__Fuchsia__) 302 const char *libGLESv2_lib[] = {"libswiftshader_libGLESv2.so", "libGLESv2.so"}; 303 #else 304 #error "libGLESv2::loadExports unimplemented for this platform" 305 #endif 306 307 std::string directory = getModuleDirectory(); 308 libGLESv2 = loadLibrary(directory, libGLESv2_lib, "libGLESv2_swiftshader"); 309 310 if(libGLESv2) 311 { 312 auto libGLESv2_swiftshader = (LibGLESv2exports *(*)())getProcAddress(libGLESv2, "libGLESv2_swiftshader"); 313 libGLESv2exports = libGLESv2_swiftshader(); 314 } 315 316 loadLibraryAttempted = true; 317 } 318 319 return libGLESv2exports; 320 } 321 322 void *libGLESv2 = nullptr; 323 LibGLESv2exports *libGLESv2exports = nullptr; 324 bool loadLibraryAttempted = false; 325 }; 326 327 #endif // libGLESv2_hpp 328