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