blob: 70dd019f836e14c6cb32e2b3bb741eb269e95e26 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
twiz@google.com59a190b2011-03-14 21:23:01 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
twiz@google.com59a190b2011-03-14 21:23:01 +00007 */
8
9
twiz@google.com59a190b2011-03-14 21:23:01 +000010#include "GrTypes.h"
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000011#include "GrGLInterface.h"
12#include "GrGLDefines.h"
twiz@google.com59a190b2011-03-14 21:23:01 +000013
14#include <stdio.h>
15
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000016#if GR_GL_PER_GL_FUNC_CALLBACK
17namespace {
18void GrGLDefaultInterfaceCallback(const GrGLInterface*) {}
19}
20#endif
21
bsalomon@google.comc82b8892011-09-22 14:10:33 +000022GrGLVersion GrGLGetVersionFromString(const char* versionString) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000023 if (NULL == versionString) {
24 GrAssert(!"NULL GL version string.");
25 return 0;
26 }
27
bsalomon@google.comc82b8892011-09-22 14:10:33 +000028 int major, minor;
twiz@google.com0f31ca72011-03-18 17:38:11 +000029
bsalomon@google.comc82b8892011-09-22 14:10:33 +000030 int n = sscanf(versionString, "%d.%d", &major, &minor);
twiz@google.com0f31ca72011-03-18 17:38:11 +000031 if (2 == n) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +000032 return GR_GL_VER(major, minor);
twiz@google.com59a190b2011-03-14 21:23:01 +000033 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000034
twiz@google.com59a190b2011-03-14 21:23:01 +000035 char profile[2];
twiz@google.com0f31ca72011-03-18 17:38:11 +000036 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
bsalomon@google.comc82b8892011-09-22 14:10:33 +000037 &major, &minor);
38 if (4 == n) {
39 return GR_GL_VER(major, minor);
40 }
41
42 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
43 if (2 == n) {
44 return GR_GL_VER(major, minor);
twiz@google.com59a190b2011-03-14 21:23:01 +000045 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000046
bsalomon@google.comc82b8892011-09-22 14:10:33 +000047 return 0;
twiz@google.com59a190b2011-03-14 21:23:01 +000048}
49
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000050GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) {
51 if (NULL == versionString) {
52 GrAssert(!"NULL GLSL version string.");
53 return 0;
54 }
55
56 int major, minor;
57
58 int n = sscanf(versionString, "%d.%d", &major, &minor);
59 if (2 == n) {
60 return GR_GLSL_VER(major, minor);
61 }
62
63 n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
64 if (2 == n) {
65 return GR_GLSL_VER(major, minor);
66 }
67 return 0;
68}
69
bsalomon@google.comc82b8892011-09-22 14:10:33 +000070bool GrGLHasExtensionFromString(const char* ext, const char* extensionString) {
twiz@google.com59a190b2011-03-14 21:23:01 +000071 int extLength = strlen(ext);
72
73 while (true) {
74 int n = strcspn(extensionString, " ");
75 if (n == extLength && 0 == strncmp(ext, extensionString, n)) {
76 return true;
77 }
78 if (0 == extensionString[n]) {
79 return false;
80 }
81 extensionString += n+1;
82 }
83
84 return false;
85}
86
bsalomon@google.comc82b8892011-09-22 14:10:33 +000087bool GrGLHasExtension(const GrGLInterface* gl, const char* ext) {
bsalomon@google.comdca4aab2011-09-06 19:05:24 +000088 const GrGLubyte* glstr;
89 GR_GL_CALL_RET(gl, glstr, GetString(GR_GL_EXTENSIONS));
bsalomon@google.comc82b8892011-09-22 14:10:33 +000090 return GrGLHasExtensionFromString(ext, (const char*) glstr);
twiz@google.com59a190b2011-03-14 21:23:01 +000091}
92
bsalomon@google.comc82b8892011-09-22 14:10:33 +000093GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
bsalomon@google.comdca4aab2011-09-06 19:05:24 +000094 const GrGLubyte* v;
95 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
bsalomon@google.comc82b8892011-09-22 14:10:33 +000096 return GrGLGetVersionFromString((const char*) v);
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000097}
98
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000099GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
100 const GrGLubyte* v;
101 GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
102 return GrGLGetGLSLVersionFromString((const char*) v);
103}
104
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000105GrGLInterface::GrGLInterface() {
106 fBindingsExported = (GrGLBinding)0;
107 fNPOTRenderTargetSupport = kProbe_GrGLCapability;
108 fMinRenderTargetHeight = kProbe_GrGLCapability;
109 fMinRenderTargetWidth = kProbe_GrGLCapability;
110
111 fActiveTexture = NULL;
112 fAttachShader = NULL;
113 fBindAttribLocation = NULL;
114 fBindBuffer = NULL;
bsalomon@google.combc5cf512011-09-21 16:21:07 +0000115 fBindFragDataLocation = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000116 fBindTexture = NULL;
117 fBlendColor = NULL;
118 fBlendFunc = NULL;
119 fBufferData = NULL;
120 fBufferSubData = NULL;
121 fClear = NULL;
122 fClearColor = NULL;
123 fClearStencil = NULL;
124 fClientActiveTexture = NULL;
125 fColor4ub = NULL;
126 fColorMask = NULL;
127 fColorPointer = NULL;
128 fCompileShader = NULL;
129 fCompressedTexImage2D = NULL;
130 fCreateProgram = NULL;
131 fCreateShader = NULL;
132 fCullFace = NULL;
133 fDeleteBuffers = NULL;
134 fDeleteProgram = NULL;
135 fDeleteShader = NULL;
136 fDeleteTextures = NULL;
137 fDepthMask = NULL;
138 fDisable = NULL;
139 fDisableClientState = NULL;
140 fDisableVertexAttribArray = NULL;
141 fDrawArrays = NULL;
142 fDrawBuffer = NULL;
143 fDrawBuffers = NULL;
144 fDrawElements = NULL;
145 fEnable = NULL;
146 fEnableClientState = NULL;
147 fEnableVertexAttribArray = NULL;
148 fFrontFace = NULL;
149 fGenBuffers = NULL;
150 fGenTextures = NULL;
151 fGetBufferParameteriv = NULL;
152 fGetError = NULL;
153 fGetIntegerv = NULL;
154 fGetProgramInfoLog = NULL;
155 fGetProgramiv = NULL;
156 fGetShaderInfoLog = NULL;
157 fGetShaderiv = NULL;
158 fGetString = NULL;
159 fGetTexLevelParameteriv = NULL;
160 fGetUniformLocation = NULL;
161 fLineWidth = NULL;
162 fLinkProgram = NULL;
163 fLoadMatrixf = NULL;
164 fMatrixMode = NULL;
165 fPixelStorei = NULL;
166 fPointSize = NULL;
167 fReadBuffer = NULL;
168 fReadPixels = NULL;
169 fScissor = NULL;
170 fShadeModel = NULL;
171 fShaderSource = NULL;
172 fStencilFunc = NULL;
173 fStencilFuncSeparate = NULL;
174 fStencilMask = NULL;
175 fStencilMaskSeparate = NULL;
176 fStencilOp = NULL;
177 fStencilOpSeparate = NULL;
178 fTexCoordPointer = NULL;
179 fTexEnvi = NULL;
180 fTexImage2D = NULL;
181 fTexParameteri = NULL;
182 fTexSubImage2D = NULL;
183 fUniform1f = NULL;
184 fUniform1i = NULL;
185 fUniform1fv = NULL;
186 fUniform1iv = NULL;
187 fUniform2f = NULL;
188 fUniform2i = NULL;
189 fUniform2fv = NULL;
190 fUniform2iv = NULL;
191 fUniform3f = NULL;
192 fUniform3i = NULL;
193 fUniform3fv = NULL;
194 fUniform3iv = NULL;
195 fUniform4f = NULL;
196 fUniform4i = NULL;
197 fUniform4fv = NULL;
198 fUniform4iv = NULL;
199 fUniformMatrix2fv = NULL;
200 fUniformMatrix3fv = NULL;
201 fUniformMatrix4fv = NULL;
202 fUseProgram = NULL;
203 fVertexAttrib4fv = NULL;
204 fVertexAttribPointer = NULL;
205 fVertexPointer = NULL;
206 fViewport = NULL;
207 fBindFramebuffer = NULL;
208 fBindRenderbuffer = NULL;
209 fCheckFramebufferStatus = NULL;
210 fDeleteFramebuffers = NULL;
211 fDeleteRenderbuffers = NULL;
212 fFramebufferRenderbuffer = NULL;
213 fFramebufferTexture2D = NULL;
214 fGenFramebuffers = NULL;
215 fGenRenderbuffers = NULL;
216 fGetFramebufferAttachmentParameteriv = NULL;
217 fGetRenderbufferParameteriv = NULL;
218 fRenderbufferStorage = NULL;
219 fRenderbufferStorageMultisample = NULL;
220 fBlitFramebuffer = NULL;
221 fResolveMultisampleFramebuffer = NULL;
222 fMapBuffer = NULL;
223 fUnmapBuffer = NULL;
224 fBindFragDataLocationIndexed = NULL;
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000225
226#if GR_GL_PER_GL_FUNC_CALLBACK
227 fCallback = GrGLDefaultInterfaceCallback;
228 fCallbackData = 0;
229#endif
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000230}
231
232
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000233bool GrGLInterface::validateShaderFunctions() const {
234 // required for GrGpuGLShaders
235 if (NULL == fAttachShader ||
236 NULL == fBindAttribLocation ||
237 NULL == fCompileShader ||
238 NULL == fCreateProgram ||
239 NULL == fCreateShader ||
240 NULL == fDeleteProgram ||
241 NULL == fDeleteShader ||
242 NULL == fDisableVertexAttribArray ||
243 NULL == fEnableVertexAttribArray ||
244 NULL == fGetProgramInfoLog ||
245 NULL == fGetProgramiv ||
246 NULL == fGetShaderInfoLog ||
247 NULL == fGetShaderiv ||
248 NULL == fGetUniformLocation ||
249 NULL == fLinkProgram ||
250 NULL == fShaderSource ||
251 NULL == fUniform1f ||
252 NULL == fUniform1i ||
253 NULL == fUniform1fv ||
254 NULL == fUniform1iv ||
255 NULL == fUniform2f ||
256 NULL == fUniform2i ||
257 NULL == fUniform2fv ||
258 NULL == fUniform2iv ||
259 NULL == fUniform3f ||
260 NULL == fUniform3i ||
261 NULL == fUniform3fv ||
262 NULL == fUniform3iv ||
263 NULL == fUniform4f ||
264 NULL == fUniform4i ||
265 NULL == fUniform4fv ||
266 NULL == fUniform4iv ||
267 NULL == fUniformMatrix2fv ||
268 NULL == fUniformMatrix3fv ||
269 NULL == fUniformMatrix4fv ||
270 NULL == fUseProgram ||
271 NULL == fVertexAttrib4fv ||
272 NULL == fVertexAttribPointer) {
273 return false;
274 }
275 return true;
276}
277
278bool GrGLInterface::validateFixedFunctions() const {
279 if (NULL == fClientActiveTexture ||
280 NULL == fColor4ub ||
281 NULL == fColorPointer ||
282 NULL == fDisableClientState ||
283 NULL == fEnableClientState ||
284 NULL == fLoadMatrixf ||
285 NULL == fMatrixMode ||
286 NULL == fPointSize ||
287 NULL == fShadeModel ||
288 NULL == fTexCoordPointer ||
bsalomon@google.com4b9b6a22011-05-04 15:01:16 +0000289 NULL == fTexEnvi ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000290 NULL == fVertexPointer) {
291 return false;
292 }
293 return true;
294}
295
296bool GrGLInterface::validate(GrEngine engine) const {
297
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000298 bool isDesktop = this->supportsDesktop();
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000299
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000300 bool isES = this->supportsES();
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000301
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000302 if (isDesktop == isES) {
303 // must have one, don't support both in same interface
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000304 return false;
305 }
306
307 // functions that are always required
308 if (NULL == fActiveTexture ||
309 NULL == fBindBuffer ||
310 NULL == fBindTexture ||
311 NULL == fBlendFunc ||
312 NULL == fBufferData ||
313 NULL == fBufferSubData ||
314 NULL == fClear ||
315 NULL == fClearColor ||
316 NULL == fClearStencil ||
317 NULL == fColorMask ||
318 NULL == fCullFace ||
319 NULL == fDeleteBuffers ||
320 NULL == fDeleteTextures ||
321 NULL == fDepthMask ||
322 NULL == fDisable ||
323 NULL == fDrawArrays ||
324 NULL == fDrawElements ||
325 NULL == fEnable ||
326 NULL == fFrontFace ||
327 NULL == fGenBuffers ||
328 NULL == fGenTextures ||
329 NULL == fGetBufferParameteriv ||
330 NULL == fGetError ||
331 NULL == fGetIntegerv ||
332 NULL == fGetString ||
333 NULL == fPixelStorei ||
334 NULL == fReadPixels ||
335 NULL == fScissor ||
336 NULL == fStencilFunc ||
337 NULL == fStencilMask ||
338 NULL == fStencilOp ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000339 NULL == fTexImage2D ||
340 NULL == fTexParameteri ||
341 NULL == fTexSubImage2D ||
342 NULL == fViewport ||
343 NULL == fBindFramebuffer ||
344 NULL == fBindRenderbuffer ||
345 NULL == fCheckFramebufferStatus ||
346 NULL == fDeleteFramebuffers ||
347 NULL == fDeleteRenderbuffers ||
348 NULL == fFramebufferRenderbuffer ||
349 NULL == fFramebufferTexture2D ||
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000350 NULL == fGetFramebufferAttachmentParameteriv ||
351 NULL == fGetRenderbufferParameteriv ||
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000352 NULL == fGenFramebuffers ||
353 NULL == fGenRenderbuffers ||
354 NULL == fRenderbufferStorage) {
355 return false;
356 }
357
358 switch (engine) {
359 case kOpenGL_Shaders_GrEngine:
360 if (kES1_GrGLBinding == fBindingsExported) {
361 return false;
362 }
363 if (!this->validateShaderFunctions()) {
364 return false;
365 }
366 break;
367 case kOpenGL_Fixed_GrEngine:
368 if (kES1_GrGLBinding == fBindingsExported) {
369 return false;
370 }
371 if (!this->validateFixedFunctions()) {
372 return false;
373 }
374 break;
375 default:
376 return false;
377 }
378
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000379 const char* ext;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000380 GrGLVersion glVer = GrGLGetVersion(this);
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000381 ext = (const char*)fGetString(GR_GL_EXTENSIONS);
382
383 // Now check that baseline ES/Desktop fns not covered above are present
384 // and that we have fn pointers for any advertised extensions that we will
385 // try to use.
386
387 // these functions are part of ES2, we assume they are available
388 // On the desktop we assume they are available if the extension
389 // is present or GL version is high enough.
390 if ((kES2_GrGLBinding & fBindingsExported)) {
391 if (NULL == fBlendColor ||
392 NULL == fStencilFuncSeparate ||
393 NULL == fStencilMaskSeparate ||
394 NULL == fStencilOpSeparate) {
395 return false;
396 }
397 } else if (kDesktop_GrGLBinding == fBindingsExported) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000398 if (glVer >= GR_GL_VER(2,0)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000399 if (NULL == fStencilFuncSeparate ||
400 NULL == fStencilMaskSeparate ||
401 NULL == fStencilOpSeparate) {
402 return false;
403 }
404 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000405 if (glVer >= GR_GL_VER(3,0) && NULL == fBindFragDataLocation) {
bsalomon@google.combc5cf512011-09-21 16:21:07 +0000406 return false;
407 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000408 if (glVer >= GR_GL_VER(2,0) ||
409 GrGLHasExtensionFromString("GL_ARB_draw_buffers", ext)) {
bsalomon@google.comd32c5f52011-08-02 19:29:03 +0000410 if (NULL == fDrawBuffers) {
411 return false;
412 }
413 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000414 if (glVer >= GR_GL_VER(1,4) ||
415 GrGLHasExtensionFromString("GL_EXT_blend_color", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000416 if (NULL == fBlendColor) {
417 return false;
418 }
419 }
420 }
421
422 // optional function on desktop before 1.3
423 if (kDesktop_GrGLBinding != fBindingsExported ||
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000424 (glVer >= GR_GL_VER(1,3) ||
425 GrGLHasExtensionFromString("GL_ARB_texture_compression", ext))) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000426 if (NULL == fCompressedTexImage2D) {
427 return false;
428 }
429 }
430
bsalomon@google.comd32c5f52011-08-02 19:29:03 +0000431 // part of desktop GL, but not ES
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000432 if (kDesktop_GrGLBinding == fBindingsExported &&
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000433 (NULL == fLineWidth ||
bsalomon@google.comd32c5f52011-08-02 19:29:03 +0000434 NULL == fGetTexLevelParameteriv ||
bsalomon@google.comc49d66b2011-08-03 14:22:30 +0000435 NULL == fDrawBuffer ||
436 NULL == fReadBuffer)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000437 return false;
438 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000439
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000440 // FBO MSAA
441 if (kDesktop_GrGLBinding == fBindingsExported) {
442 // GL 3.0 and the ARB extension have multisample + blit
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000443 if (glVer >= GR_GL_VER(3,0) || GrGLHasExtensionFromString("GL_ARB_framebuffer_object", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000444 if (NULL == fRenderbufferStorageMultisample ||
445 NULL == fBlitFramebuffer) {
446 return false;
447 }
448 } else {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000449 if (GrGLHasExtensionFromString("GL_EXT_framebuffer_blit", ext) &&
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000450 NULL == fBlitFramebuffer) {
451 return false;
452 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000453 if (GrGLHasExtensionFromString("GL_EXT_framebuffer_multisample", ext) &&
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000454 NULL == fRenderbufferStorageMultisample) {
455 return false;
456 }
457 }
458 } else {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000459 if (GrGLHasExtensionFromString("GL_CHROMIUM_framebuffer_multisample", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000460 if (NULL == fRenderbufferStorageMultisample ||
461 NULL == fBlitFramebuffer) {
462 return false;
463 }
464 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000465 if (GrGLHasExtensionFromString("GL_APPLE_framebuffer_multisample", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000466 if (NULL == fRenderbufferStorageMultisample ||
467 NULL == fResolveMultisampleFramebuffer) {
468 return false;
469 }
470 }
471 }
472
473 // On ES buffer mapping is an extension. On Desktop
474 // buffer mapping was part of original VBO extension
475 // which we require.
476 if (kDesktop_GrGLBinding == fBindingsExported ||
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000477 GrGLHasExtensionFromString("GL_OES_mapbuffer", ext)) {
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000478 if (NULL == fMapBuffer ||
479 NULL == fUnmapBuffer) {
480 return false;
481 }
482 }
483
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000484 // Dual source blending
485 if (kDesktop_GrGLBinding == fBindingsExported &&
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000486 (glVer >= GR_GL_VER(3,3) ||
487 GrGLHasExtensionFromString("GL_ARB_blend_func_extended", ext))) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000488 if (NULL == fBindFragDataLocationIndexed) {
489 return false;
490 }
491 }
492
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000493 return true;
494}
495