epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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. |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 10 | #include "GrGLInterface.h" |
| 11 | |
| 12 | #include "GL/osmesa.h" |
| 13 | #include <GL/glext.h> |
| 14 | #include <GL/glu.h> |
| 15 | |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 16 | #define GR_GL_GET_PROC(F) interface->f ## F = (GrGL ## F ## Proc) \ |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 17 | OSMesaGetProcAddress("gl" #F); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 18 | #define GR_GL_GET_PROC_SUFFIX(F, S) interface->f ## F = (GrGL ## F ## Proc) \ |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 19 | OSMesaGetProcAddress("gl" #F #S); |
| 20 | |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 21 | const GrGLInterface* GrGLDefaultInterface() { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 22 | if (NULL != OSMesaGetCurrentContext()) { |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 23 | int major, minor; |
| 24 | const char* versionString = (const char*) glGetString(GL_VERSION); |
| 25 | const char* extString = (const char*) glGetString(GL_EXTENSIONS); |
| 26 | gl_version_from_string(&major, &minor, versionString); |
| 27 | |
| 28 | if (major == 1 && minor < 5) { |
| 29 | // We must have array and element_array buffer objects. |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 30 | return NULL; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 31 | } |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 32 | GrGLInterface* interface = new GrGLInterface(); |
| 33 | interface->fNPOTRenderTargetSupport = kProbe_GrGLCapability; |
| 34 | interface->fMinRenderTargetHeight = kProbe_GrGLCapability; |
| 35 | interface->fMinRenderTargetWidth = kProbe_GrGLCapability; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 37 | interface->fActiveTexture = glActiveTexture; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 38 | GR_GL_GET_PROC(AttachShader); |
| 39 | GR_GL_GET_PROC(BindAttribLocation); |
| 40 | GR_GL_GET_PROC(BindBuffer); |
bsalomon@google.com | bc5cf51 | 2011-09-21 16:21:07 +0000 | [diff] [blame] | 41 | GR_GL_GET_PROC(BindFragDataLocation); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 42 | interface->fBindTexture = glBindTexture; |
| 43 | interface->fBlendColor = glBlendColor; |
| 44 | interface->fBlendFunc = glBlendFunc; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 45 | GR_GL_GET_PROC(BufferData); |
| 46 | GR_GL_GET_PROC(BufferSubData); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 47 | interface->fClear = glClear; |
| 48 | interface->fClearColor = glClearColor; |
| 49 | interface->fClearStencil = glClearStencil; |
| 50 | interface->fClientActiveTexture = glClientActiveTexture; |
| 51 | interface->fColorMask = glColorMask; |
| 52 | interface->fColorPointer = glColorPointer; |
| 53 | interface->fColor4ub = glColor4ub; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 54 | GR_GL_GET_PROC(CompileShader); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 55 | interface->fCompressedTexImage2D = glCompressedTexImage2D; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 56 | GR_GL_GET_PROC(CreateProgram); |
| 57 | GR_GL_GET_PROC(CreateShader); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 58 | interface->fCullFace = glCullFace; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 59 | GR_GL_GET_PROC(DeleteBuffers); |
| 60 | GR_GL_GET_PROC(DeleteProgram); |
| 61 | GR_GL_GET_PROC(DeleteShader); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 62 | interface->fDeleteTextures = glDeleteTextures; |
| 63 | interface->fDepthMask = glDepthMask; |
| 64 | interface->fDisable = glDisable; |
| 65 | interface->fDisableClientState = glDisableClientState; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 66 | GR_GL_GET_PROC(DisableVertexAttribArray); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 67 | interface->fDrawArrays = glDrawArrays; |
| 68 | interface->fDrawBuffer = glDrawBuffer; |
bsalomon@google.com | d32c5f5 | 2011-08-02 19:29:03 +0000 | [diff] [blame] | 69 | GR_GL_GET_PROC(DrawBuffers); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 70 | interface->fDrawElements = glDrawElements; |
| 71 | interface->fEnable = glEnable; |
| 72 | interface->fEnableClientState = glEnableClientState; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 73 | GR_GL_GET_PROC(EnableVertexAttribArray); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 74 | interface->fFrontFace = glFrontFace; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 75 | GR_GL_GET_PROC(GenBuffers); |
| 76 | GR_GL_GET_PROC(GetBufferParameteriv); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 77 | interface->fGetError = glGetError; |
| 78 | interface->fGetIntegerv = glGetIntegerv; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 79 | GR_GL_GET_PROC(GetProgramInfoLog); |
| 80 | GR_GL_GET_PROC(GetProgramiv); |
| 81 | GR_GL_GET_PROC(GetShaderInfoLog); |
| 82 | GR_GL_GET_PROC(GetShaderiv); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 83 | interface->fGetString = glGetString; |
| 84 | interface->fGetTexLevelParameteriv = glGetTexLevelParameteriv; |
| 85 | interface->fGenTextures = glGenTextures; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 86 | GR_GL_GET_PROC(GetUniformLocation); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 87 | interface->fLineWidth = glLineWidth; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 88 | GR_GL_GET_PROC(LinkProgram); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 89 | interface->fLoadMatrixf = glLoadMatrixf; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 90 | GR_GL_GET_PROC(MapBuffer); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 91 | interface->fMatrixMode = glMatrixMode; |
| 92 | interface->fPointSize = glPointSize; |
| 93 | interface->fPixelStorei = glPixelStorei; |
| 94 | interface->fReadBuffer = glReadBuffer; |
| 95 | interface->fReadPixels = glReadPixels; |
| 96 | interface->fScissor = glScissor; |
| 97 | interface->fShadeModel = glShadeModel; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 98 | GR_GL_GET_PROC(ShaderSource); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 99 | interface->fStencilFunc = glStencilFunc; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 100 | GR_GL_GET_PROC(StencilFuncSeparate); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 101 | interface->fStencilMask = glStencilMask; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 102 | GR_GL_GET_PROC(StencilMaskSeparate); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 103 | interface->fStencilOp = glStencilOp; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 104 | GR_GL_GET_PROC(StencilOpSeparate); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 105 | interface->fTexCoordPointer = glTexCoordPointer; |
| 106 | interface->fTexEnvi = glTexEnvi; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 107 | //OSMesa on Mac's glTexImage2D takes a GLenum for internalFormat rather than a GLint. |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 108 | interface->fTexImage2D = reinterpret_cast<GrGLTexImage2DProc>(glTexImage2D); |
| 109 | interface->fTexParameteri = glTexParameteri; |
| 110 | interface->fTexSubImage2D = glTexSubImage2D; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 111 | GR_GL_GET_PROC(Uniform1f); |
| 112 | GR_GL_GET_PROC(Uniform1i); |
| 113 | GR_GL_GET_PROC(Uniform1fv); |
| 114 | GR_GL_GET_PROC(Uniform1iv); |
| 115 | GR_GL_GET_PROC(Uniform2f); |
| 116 | GR_GL_GET_PROC(Uniform2i); |
| 117 | GR_GL_GET_PROC(Uniform2fv); |
| 118 | GR_GL_GET_PROC(Uniform2iv); |
| 119 | GR_GL_GET_PROC(Uniform3f); |
| 120 | GR_GL_GET_PROC(Uniform3i); |
| 121 | GR_GL_GET_PROC(Uniform3fv); |
| 122 | GR_GL_GET_PROC(Uniform3iv); |
| 123 | GR_GL_GET_PROC(Uniform4f); |
| 124 | GR_GL_GET_PROC(Uniform4i); |
| 125 | GR_GL_GET_PROC(Uniform4fv); |
| 126 | GR_GL_GET_PROC(Uniform4iv); |
| 127 | GR_GL_GET_PROC(UniformMatrix2fv); |
| 128 | GR_GL_GET_PROC(UniformMatrix3fv); |
| 129 | GR_GL_GET_PROC(UniformMatrix4fv); |
| 130 | GR_GL_GET_PROC(UnmapBuffer); |
| 131 | GR_GL_GET_PROC(UseProgram); |
| 132 | GR_GL_GET_PROC(VertexAttrib4fv); |
| 133 | GR_GL_GET_PROC(VertexAttribPointer); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 134 | interface->fVertexPointer = glVertexPointer; |
| 135 | interface->fViewport = glViewport; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 136 | |
| 137 | // First look for GL3.0 FBO or GL_ARB_framebuffer_object (same since |
| 138 | // GL_ARB_framebuffer_object doesn't use ARB suffix.) |
| 139 | if (major >= 3 || has_gl_extension_from_string( |
| 140 | "GL_ARB_framebuffer_object", extString)) { |
| 141 | GR_GL_GET_PROC(GenFramebuffers); |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 142 | GR_GL_GET_PROC(GetFramebufferAttachmentParameteriv); |
| 143 | GR_GL_GET_PROC(GetRenderbufferParameteriv); |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 144 | GR_GL_GET_PROC(BindFramebuffer); |
| 145 | GR_GL_GET_PROC(FramebufferTexture2D); |
| 146 | GR_GL_GET_PROC(CheckFramebufferStatus); |
| 147 | GR_GL_GET_PROC(DeleteFramebuffers); |
| 148 | GR_GL_GET_PROC(RenderbufferStorage); |
| 149 | GR_GL_GET_PROC(GenRenderbuffers); |
| 150 | GR_GL_GET_PROC(DeleteRenderbuffers); |
| 151 | GR_GL_GET_PROC(FramebufferRenderbuffer); |
| 152 | GR_GL_GET_PROC(BindRenderbuffer); |
| 153 | GR_GL_GET_PROC(RenderbufferStorageMultisample); |
| 154 | GR_GL_GET_PROC(BlitFramebuffer); |
| 155 | } else if (has_gl_extension_from_string("GL_EXT_framebuffer_object", |
| 156 | extString)) { |
| 157 | GR_GL_GET_PROC_SUFFIX(GenFramebuffers, EXT); |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 158 | GR_GL_GET_PROC_SUFFIX(GetFramebufferAttachmentParameteriv, EXT); |
| 159 | GR_GL_GET_PROC_SUFFIX(GetRenderbufferParameteriv, EXT); |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 160 | GR_GL_GET_PROC_SUFFIX(BindFramebuffer, EXT); |
| 161 | GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, EXT); |
| 162 | GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, EXT); |
| 163 | GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, EXT); |
| 164 | GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, EXT); |
| 165 | GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, EXT); |
| 166 | GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, EXT); |
| 167 | GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT); |
| 168 | GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, EXT); |
| 169 | if (has_gl_extension_from_string("GL_EXT_framebuffer_multisample", |
| 170 | extString)) { |
| 171 | GR_GL_GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT); |
| 172 | } |
| 173 | if (has_gl_extension_from_string("GL_EXT_framebuffer_blit", |
| 174 | extString)) { |
| 175 | GR_GL_GET_PROC_SUFFIX(BlitFramebuffer, EXT); |
| 176 | } |
| 177 | } else { |
| 178 | // we must have FBOs |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 179 | delete interface; |
| 180 | return NULL; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 181 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 182 | GR_GL_GET_PROC(BindFragDataLocationIndexed); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 183 | interface->fBindingsExported = kDesktop_GrGLBinding; |
| 184 | return interface; |
| 185 | } else { |
| 186 | return NULL; |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 187 | } |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 188 | } |