bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 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 | #include "GrGLInterface.h" |
| 18 | |
| 19 | #include "GL/osmesa.h" |
| 20 | #include <GL/glext.h> |
| 21 | #include <GL/glu.h> |
| 22 | |
| 23 | #define GR_GL_GET_PROC(F) gDefaultInterface.f ## F = (GrGL ## F ## Proc) \ |
| 24 | OSMesaGetProcAddress("gl" #F); |
| 25 | #define GR_GL_GET_PROC_SUFFIX(F, S) gDefaultInterface.f ## F = (GrGL ## F ## Proc) \ |
| 26 | OSMesaGetProcAddress("gl" #F #S); |
| 27 | |
| 28 | void GrGLSetDefaultGLInterface() { |
| 29 | static GrGLInterface gDefaultInterface; |
| 30 | static bool gDefaultInterfaceInit; |
| 31 | if (!gDefaultInterfaceInit && NULL != OSMesaGetCurrentContext()) { |
| 32 | int major, minor; |
| 33 | const char* versionString = (const char*) glGetString(GL_VERSION); |
| 34 | const char* extString = (const char*) glGetString(GL_EXTENSIONS); |
| 35 | gl_version_from_string(&major, &minor, versionString); |
| 36 | |
| 37 | if (major == 1 && minor < 5) { |
| 38 | // We must have array and element_array buffer objects. |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | gDefaultInterface.fActiveTexture = glActiveTexture; |
| 43 | GR_GL_GET_PROC(AttachShader); |
| 44 | GR_GL_GET_PROC(BindAttribLocation); |
| 45 | GR_GL_GET_PROC(BindBuffer); |
| 46 | gDefaultInterface.fBindTexture = glBindTexture; |
| 47 | gDefaultInterface.fBlendColor = glBlendColor; |
| 48 | gDefaultInterface.fBlendFunc = glBlendFunc; |
| 49 | GR_GL_GET_PROC(BufferData); |
| 50 | GR_GL_GET_PROC(BufferSubData); |
| 51 | gDefaultInterface.fClear = glClear; |
| 52 | gDefaultInterface.fClearColor = glClearColor; |
| 53 | gDefaultInterface.fClearStencil = glClearStencil; |
| 54 | gDefaultInterface.fClientActiveTexture = glClientActiveTexture; |
| 55 | gDefaultInterface.fColorMask = glColorMask; |
| 56 | gDefaultInterface.fColorPointer = glColorPointer; |
| 57 | gDefaultInterface.fColor4ub = glColor4ub; |
| 58 | GR_GL_GET_PROC(CompileShader); |
| 59 | gDefaultInterface.fCompressedTexImage2D = glCompressedTexImage2D; |
| 60 | GR_GL_GET_PROC(CreateProgram); |
| 61 | GR_GL_GET_PROC(CreateShader); |
| 62 | gDefaultInterface.fCullFace = glCullFace; |
| 63 | GR_GL_GET_PROC(DeleteBuffers); |
| 64 | GR_GL_GET_PROC(DeleteProgram); |
| 65 | GR_GL_GET_PROC(DeleteShader); |
| 66 | gDefaultInterface.fDeleteTextures = glDeleteTextures; |
| 67 | gDefaultInterface.fDepthMask = glDepthMask; |
| 68 | gDefaultInterface.fDisable = glDisable; |
| 69 | gDefaultInterface.fDisableClientState = glDisableClientState; |
| 70 | GR_GL_GET_PROC(DisableVertexAttribArray); |
| 71 | gDefaultInterface.fDrawArrays = glDrawArrays; |
| 72 | gDefaultInterface.fDrawElements = glDrawElements; |
| 73 | gDefaultInterface.fEnable = glEnable; |
| 74 | gDefaultInterface.fEnableClientState = glEnableClientState; |
| 75 | GR_GL_GET_PROC(EnableVertexAttribArray); |
| 76 | gDefaultInterface.fFrontFace = glFrontFace; |
| 77 | GR_GL_GET_PROC(GenBuffers); |
| 78 | GR_GL_GET_PROC(GetBufferParameteriv); |
| 79 | gDefaultInterface.fGetError = glGetError; |
| 80 | gDefaultInterface.fGetIntegerv = glGetIntegerv; |
| 81 | GR_GL_GET_PROC(GetProgramInfoLog); |
| 82 | GR_GL_GET_PROC(GetProgramiv); |
| 83 | GR_GL_GET_PROC(GetShaderInfoLog); |
| 84 | GR_GL_GET_PROC(GetShaderiv); |
| 85 | gDefaultInterface.fGetString = glGetString; |
| 86 | gDefaultInterface.fGenTextures = glGenTextures; |
| 87 | GR_GL_GET_PROC(GetUniformLocation); |
| 88 | gDefaultInterface.fLineWidth = glLineWidth; |
| 89 | GR_GL_GET_PROC(LinkProgram); |
| 90 | gDefaultInterface.fLoadMatrixf = glLoadMatrixf; |
| 91 | GR_GL_GET_PROC(MapBuffer); |
| 92 | gDefaultInterface.fMatrixMode = glMatrixMode; |
| 93 | gDefaultInterface.fPointSize = glPointSize; |
| 94 | gDefaultInterface.fPixelStorei = glPixelStorei; |
| 95 | gDefaultInterface.fReadPixels = glReadPixels; |
| 96 | gDefaultInterface.fScissor = glScissor; |
| 97 | gDefaultInterface.fShadeModel = glShadeModel; |
| 98 | GR_GL_GET_PROC(ShaderSource); |
| 99 | gDefaultInterface.fStencilFunc = glStencilFunc; |
| 100 | GR_GL_GET_PROC(StencilFuncSeparate); |
| 101 | gDefaultInterface.fStencilMask = glStencilMask; |
| 102 | GR_GL_GET_PROC(StencilMaskSeparate); |
| 103 | gDefaultInterface.fStencilOp = glStencilOp; |
| 104 | GR_GL_GET_PROC(StencilOpSeparate); |
| 105 | gDefaultInterface.fTexCoordPointer = glTexCoordPointer; |
| 106 | gDefaultInterface.fTexEnvi = glTexEnvi; |
| 107 | //OSMesa on Mac's glTexImage2D takes a GLenum for internalFormat rather than a GLint. |
| 108 | gDefaultInterface.fTexImage2D = reinterpret_cast<GrGLTexImage2DProc>(glTexImage2D); |
| 109 | gDefaultInterface.fTexParameteri = glTexParameteri; |
| 110 | gDefaultInterface.fTexSubImage2D = glTexSubImage2D; |
| 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); |
| 134 | gDefaultInterface.fVertexPointer = glVertexPointer; |
| 135 | gDefaultInterface.fViewport = glViewport; |
| 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); |
| 142 | GR_GL_GET_PROC(BindFramebuffer); |
| 143 | GR_GL_GET_PROC(FramebufferTexture2D); |
| 144 | GR_GL_GET_PROC(CheckFramebufferStatus); |
| 145 | GR_GL_GET_PROC(DeleteFramebuffers); |
| 146 | GR_GL_GET_PROC(RenderbufferStorage); |
| 147 | GR_GL_GET_PROC(GenRenderbuffers); |
| 148 | GR_GL_GET_PROC(DeleteRenderbuffers); |
| 149 | GR_GL_GET_PROC(FramebufferRenderbuffer); |
| 150 | GR_GL_GET_PROC(BindRenderbuffer); |
| 151 | GR_GL_GET_PROC(RenderbufferStorageMultisample); |
| 152 | GR_GL_GET_PROC(BlitFramebuffer); |
| 153 | } else if (has_gl_extension_from_string("GL_EXT_framebuffer_object", |
| 154 | extString)) { |
| 155 | GR_GL_GET_PROC_SUFFIX(GenFramebuffers, EXT); |
| 156 | GR_GL_GET_PROC_SUFFIX(BindFramebuffer, EXT); |
| 157 | GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, EXT); |
| 158 | GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, EXT); |
| 159 | GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, EXT); |
| 160 | GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, EXT); |
| 161 | GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, EXT); |
| 162 | GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, EXT); |
| 163 | GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT); |
| 164 | GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, EXT); |
| 165 | if (has_gl_extension_from_string("GL_EXT_framebuffer_multisample", |
| 166 | extString)) { |
| 167 | GR_GL_GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT); |
| 168 | } |
| 169 | if (has_gl_extension_from_string("GL_EXT_framebuffer_blit", |
| 170 | extString)) { |
| 171 | GR_GL_GET_PROC_SUFFIX(BlitFramebuffer, EXT); |
| 172 | } |
| 173 | } else { |
| 174 | // we must have FBOs |
| 175 | return; |
| 176 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame^] | 177 | GR_GL_GET_PROC(BindFragDataLocationIndexed); |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 178 | gDefaultInterface.fBindingsExported = kDesktop_GrGLBinding; |
| 179 | |
| 180 | gDefaultInterfaceInit = true; |
| 181 | } |
| 182 | if (gDefaultInterfaceInit) |
| 183 | GrGLSetGLInterface(&gDefaultInterface); |
| 184 | } |