bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +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 <Windows.h> |
| 20 | #include <GL/GL.h> |
| 21 | |
| 22 | /* |
| 23 | * Windows makes the GL funcs all be __stdcall instead of __cdecl :( |
| 24 | * This implementation will only work if GR_GL_FUNCTION_TYPE is __stdcall. |
| 25 | * Otherwise, a springboard would be needed that hides the calling convention. |
| 26 | */ |
| 27 | |
| 28 | #define GR_GL_GET_PROC(F) gDefaultInterface.f ## F = (GrGLInterface::GrGL ## F ## Proc) wglGetProcAddress("gl" #F); |
| 29 | #define GR_GL_GET_PROC_SUFFIX(F, S) gDefaultInterface.f ## F = (GrGLInterface::GrGL ## F ## Proc) wglGetProcAddress("gl" #F #S); |
| 30 | |
| 31 | void GrGLSetDefaultGLInterface() { |
| 32 | static GrGLInterface gDefaultInterface; |
| 33 | static bool gDefaultInterfaceInit; |
| 34 | if (!gDefaultInterfaceInit) { |
| 35 | |
| 36 | // wglGetProcAddress requires a context. |
| 37 | if (NULL != wglGetCurrentContext()) { |
| 38 | int major, minor; |
| 39 | const char* versionString = (const char*) glGetString(GL_VERSION); |
| 40 | const char* extString = (const char*) glGetString(GL_EXTENSIONS); |
| 41 | gl_version_from_string(&major, &minor, versionString); |
| 42 | |
| 43 | if (major == 1 && minor < 5) { |
| 44 | // We must have array and element_array buffer objects. |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | // Functions that are part of GL 1.1 will return NULL in |
| 49 | // wglGetProcAddress |
| 50 | gDefaultInterface.fBlendFunc = glBlendFunc; |
| 51 | gDefaultInterface.fClear = glClear; |
| 52 | gDefaultInterface.fClearColor = glClearColor; |
| 53 | gDefaultInterface.fClearStencil = glClearStencil; |
| 54 | gDefaultInterface.fColor4ub = glColor4ub; |
| 55 | gDefaultInterface.fColorMask = glColorMask; |
| 56 | gDefaultInterface.fColorPointer = glColorPointer; |
| 57 | gDefaultInterface.fCullFace = glCullFace; |
| 58 | gDefaultInterface.fDeleteTextures = glDeleteTextures; |
| 59 | gDefaultInterface.fDepthMask = glDepthMask; |
| 60 | gDefaultInterface.fDisable = glDisable; |
| 61 | gDefaultInterface.fDisableClientState = glDisableClientState; |
| 62 | gDefaultInterface.fDrawArrays = glDrawArrays; |
| 63 | gDefaultInterface.fDrawElements = glDrawElements; |
| 64 | gDefaultInterface.fEnable = glEnable; |
| 65 | gDefaultInterface.fEnableClientState = glEnableClientState; |
| 66 | gDefaultInterface.fFrontFace = glFrontFace; |
| 67 | gDefaultInterface.fGenTextures = glGenTextures; |
| 68 | gDefaultInterface.fGetError = glGetError; |
| 69 | gDefaultInterface.fGetIntegerv = glGetIntegerv; |
| 70 | gDefaultInterface.fGetString = glGetString; |
| 71 | gDefaultInterface.fLineWidth = glLineWidth; |
| 72 | gDefaultInterface.fLoadMatrixf = glLoadMatrixf; |
| 73 | gDefaultInterface.fMatrixMode = glMatrixMode; |
| 74 | gDefaultInterface.fPixelStorei = glPixelStorei; |
| 75 | gDefaultInterface.fPointSize = glPointSize; |
| 76 | gDefaultInterface.fReadPixels = glReadPixels; |
| 77 | gDefaultInterface.fScissor = glScissor; |
| 78 | gDefaultInterface.fShadeModel = glShadeModel; |
| 79 | gDefaultInterface.fStencilFunc = glStencilFunc; |
| 80 | gDefaultInterface.fStencilMask = glStencilMask; |
| 81 | gDefaultInterface.fStencilOp = glStencilOp; |
| 82 | gDefaultInterface.fTexImage2D = glTexImage2D; |
| 83 | gDefaultInterface.fTexParameteri = glTexParameteri; |
| 84 | gDefaultInterface.fTexCoordPointer = glTexCoordPointer; |
| 85 | gDefaultInterface.fTexEnvi = glTexEnvi; |
| 86 | gDefaultInterface.fTexSubImage2D = glTexSubImage2D; |
| 87 | gDefaultInterface.fViewport = glViewport; |
| 88 | gDefaultInterface.fVertexPointer = glVertexPointer; |
| 89 | |
| 90 | GR_GL_GET_PROC(ActiveTexture); |
| 91 | GR_GL_GET_PROC(AttachShader); |
| 92 | GR_GL_GET_PROC(BindAttribLocation); |
| 93 | GR_GL_GET_PROC(BindBuffer); |
| 94 | GR_GL_GET_PROC(BindTexture); |
| 95 | GR_GL_GET_PROC(BlendColor); |
| 96 | GR_GL_GET_PROC(BufferData); |
| 97 | GR_GL_GET_PROC(BufferSubData); |
| 98 | GR_GL_GET_PROC(ClientActiveTexture); |
| 99 | GR_GL_GET_PROC(CompileShader); |
| 100 | GR_GL_GET_PROC(CompressedTexImage2D); |
| 101 | GR_GL_GET_PROC(CreateProgram); |
| 102 | GR_GL_GET_PROC(CreateShader); |
| 103 | GR_GL_GET_PROC(DeleteBuffers); |
| 104 | GR_GL_GET_PROC(DeleteProgram); |
| 105 | GR_GL_GET_PROC(DeleteShader); |
| 106 | GR_GL_GET_PROC(DisableVertexAttribArray); |
| 107 | GR_GL_GET_PROC(EnableVertexAttribArray); |
| 108 | GR_GL_GET_PROC(GenBuffers); |
| 109 | GR_GL_GET_PROC(GetBufferParameteriv); |
| 110 | GR_GL_GET_PROC(GetProgramInfoLog); |
| 111 | GR_GL_GET_PROC(GetProgramiv); |
| 112 | GR_GL_GET_PROC(GetShaderInfoLog); |
| 113 | GR_GL_GET_PROC(GetShaderiv); |
| 114 | GR_GL_GET_PROC(GetUniformLocation); |
| 115 | GR_GL_GET_PROC(LinkProgram); |
| 116 | GR_GL_GET_PROC(ShaderSource); |
| 117 | GR_GL_GET_PROC(StencilFuncSeparate); |
| 118 | GR_GL_GET_PROC(StencilMaskSeparate); |
| 119 | GR_GL_GET_PROC(StencilOpSeparate); |
| 120 | GR_GL_GET_PROC(Uniform1fv); |
| 121 | GR_GL_GET_PROC(Uniform1i); |
| 122 | GR_GL_GET_PROC(Uniform4fv); |
| 123 | GR_GL_GET_PROC(UniformMatrix3fv); |
| 124 | GR_GL_GET_PROC(UseProgram); |
| 125 | GR_GL_GET_PROC(VertexAttrib4fv); |
| 126 | GR_GL_GET_PROC(VertexAttribPointer); |
| 127 | |
| 128 | // First look for GL3.0 FBO or GL_ARB_framebuffer_object (same since |
| 129 | // GL_ARB_framebuffer_object doesn't use ARB suffix.) |
bsalomon@google.com | 13a950a | 2011-04-12 14:16:21 +0000 | [diff] [blame^] | 130 | if (major >= 3 || has_gl_extension_from_string("GL_ARB_framebuffer_object", extString)) { |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 131 | GR_GL_GET_PROC(GenFramebuffers); |
| 132 | GR_GL_GET_PROC(BindFramebuffer); |
| 133 | GR_GL_GET_PROC(FramebufferTexture2D); |
| 134 | GR_GL_GET_PROC(CheckFramebufferStatus); |
| 135 | GR_GL_GET_PROC(DeleteFramebuffers); |
| 136 | GR_GL_GET_PROC(RenderbufferStorage); |
| 137 | GR_GL_GET_PROC(GenRenderbuffers); |
| 138 | GR_GL_GET_PROC(DeleteRenderbuffers); |
| 139 | GR_GL_GET_PROC(FramebufferRenderbuffer); |
| 140 | GR_GL_GET_PROC(BindRenderbuffer); |
| 141 | GR_GL_GET_PROC(RenderbufferStorageMultisample); |
| 142 | GR_GL_GET_PROC(BlitFramebuffer); |
bsalomon@google.com | 13a950a | 2011-04-12 14:16:21 +0000 | [diff] [blame^] | 143 | } else if (has_gl_extension_from_string("GL_EXT_framebuffer_object", extString)) { |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 144 | GR_GL_GET_PROC_SUFFIX(GenFramebuffers, EXT); |
| 145 | GR_GL_GET_PROC_SUFFIX(BindFramebuffer, EXT); |
| 146 | GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, EXT); |
| 147 | GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, EXT); |
| 148 | GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, EXT); |
| 149 | GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, EXT); |
| 150 | GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, EXT); |
| 151 | GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, EXT); |
| 152 | GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT); |
| 153 | GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, EXT); |
bsalomon@google.com | 13a950a | 2011-04-12 14:16:21 +0000 | [diff] [blame^] | 154 | if (has_gl_extension_from_string("GL_EXT_framebuffer_multisample", extString)) { |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 155 | GR_GL_GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT); |
| 156 | } |
bsalomon@google.com | 13a950a | 2011-04-12 14:16:21 +0000 | [diff] [blame^] | 157 | if (has_gl_extension_from_string("GL_EXT_framebuffer_blit", extString)) { |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 158 | GR_GL_GET_PROC_SUFFIX(BlitFramebuffer, EXT); |
| 159 | } |
| 160 | } else { |
| 161 | // we must have FBOs |
| 162 | return; |
| 163 | } |
| 164 | GR_GL_GET_PROC(MapBuffer); |
| 165 | GR_GL_GET_PROC(UnmapBuffer); |
| 166 | |
| 167 | gDefaultInterface.fBindingsExported = kDesktop_GrGLBinding; |
| 168 | |
| 169 | gDefaultInterfaceInit = true; |
| 170 | } |
| 171 | } |
| 172 | if (gDefaultInterfaceInit) { |
| 173 | GrGLSetGLInterface(&gDefaultInterface); |
| 174 | } |
| 175 | } |