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