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 = (GrGL ## F ## Proc) wglGetProcAddress("gl" #F); 29 #define GR_GL_GET_PROC_SUFFIX(F, S) gDefaultInterface.f ## F = (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(Uniform1f); 121 GR_GL_GET_PROC(Uniform1i); 122 GR_GL_GET_PROC(Uniform1fv); 123 GR_GL_GET_PROC(Uniform1iv); 124 GR_GL_GET_PROC(Uniform2f); 125 GR_GL_GET_PROC(Uniform2i); 126 GR_GL_GET_PROC(Uniform2fv); 127 GR_GL_GET_PROC(Uniform2iv); 128 GR_GL_GET_PROC(Uniform3f); 129 GR_GL_GET_PROC(Uniform3i); 130 GR_GL_GET_PROC(Uniform3fv); 131 GR_GL_GET_PROC(Uniform3iv); 132 GR_GL_GET_PROC(Uniform4f); 133 GR_GL_GET_PROC(Uniform4i); 134 GR_GL_GET_PROC(Uniform4fv); 135 GR_GL_GET_PROC(Uniform4iv); 136 GR_GL_GET_PROC(UniformMatrix2fv); 137 GR_GL_GET_PROC(UniformMatrix3fv); 138 GR_GL_GET_PROC(UniformMatrix4fv); 139 GR_GL_GET_PROC(UseProgram); 140 GR_GL_GET_PROC(VertexAttrib4fv); 141 GR_GL_GET_PROC(VertexAttribPointer); 142 GR_GL_GET_PROC(BindFragDataLocationIndexed); 143 144 // First look for GL3.0 FBO or GL_ARB_framebuffer_object (same since 145 // GL_ARB_framebuffer_object doesn't use ARB suffix.) 146 if (major >= 3 || has_gl_extension_from_string("GL_ARB_framebuffer_object", extString)) { 147 GR_GL_GET_PROC(GenFramebuffers); 148 GR_GL_GET_PROC(BindFramebuffer); 149 GR_GL_GET_PROC(FramebufferTexture2D); 150 GR_GL_GET_PROC(CheckFramebufferStatus); 151 GR_GL_GET_PROC(DeleteFramebuffers); 152 GR_GL_GET_PROC(RenderbufferStorage); 153 GR_GL_GET_PROC(GenRenderbuffers); 154 GR_GL_GET_PROC(DeleteRenderbuffers); 155 GR_GL_GET_PROC(FramebufferRenderbuffer); 156 GR_GL_GET_PROC(BindRenderbuffer); 157 GR_GL_GET_PROC(RenderbufferStorageMultisample); 158 GR_GL_GET_PROC(BlitFramebuffer); 159 } else if (has_gl_extension_from_string("GL_EXT_framebuffer_object", extString)) { 160 GR_GL_GET_PROC_SUFFIX(GenFramebuffers, EXT); 161 GR_GL_GET_PROC_SUFFIX(BindFramebuffer, EXT); 162 GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, EXT); 163 GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, EXT); 164 GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, EXT); 165 GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, EXT); 166 GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, EXT); 167 GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, EXT); 168 GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT); 169 GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, EXT); 170 if (has_gl_extension_from_string("GL_EXT_framebuffer_multisample", extString)) { 171 GR_GL_GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT); 172 } 173 if (has_gl_extension_from_string("GL_EXT_framebuffer_blit", extString)) { 174 GR_GL_GET_PROC_SUFFIX(BlitFramebuffer, EXT); 175 } 176 } else { 177 // we must have FBOs 178 return; 179 } 180 GR_GL_GET_PROC(MapBuffer); 181 GR_GL_GET_PROC(UnmapBuffer); 182 183 gDefaultInterface.fBindingsExported = kDesktop_GrGLBinding; 184 185 gDefaultInterfaceInit = true; 186 } 187 } 188 if (gDefaultInterfaceInit) { 189 GrGLSetGLInterface(&gDefaultInterface); 190 } 191 } 192