epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +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. |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 10 | #include "GrGLInterface.h" |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 11 | #define WIN32_LEAN_AND_MEAN |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 12 | #include <Windows.h> |
| 13 | #include <GL/GL.h> |
| 14 | |
| 15 | /* |
| 16 | * Windows makes the GL funcs all be __stdcall instead of __cdecl :( |
| 17 | * This implementation will only work if GR_GL_FUNCTION_TYPE is __stdcall. |
| 18 | * Otherwise, a springboard would be needed that hides the calling convention. |
| 19 | */ |
| 20 | |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 21 | #define GR_GL_GET_PROC(F) interface->f ## F = (GrGL ## F ## Proc) wglGetProcAddress("gl" #F); |
| 22 | #define GR_GL_GET_PROC_SUFFIX(F, S) interface->f ## F = (GrGL ## F ## Proc) wglGetProcAddress("gl" #F #S); |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 24 | const GrGLInterface* GrGLCreateNativeInterface() { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 25 | // wglGetProcAddress requires a context. |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 26 | // GL Function pointers retrieved in one context may not be valid in another |
| 27 | // context. For that reason we create a new GrGLInterface each time we're |
| 28 | // called. |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 29 | if (NULL != wglGetCurrentContext()) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 30 | const char* versionString = (const char*) glGetString(GL_VERSION); |
| 31 | const char* extString = (const char*) glGetString(GL_EXTENSIONS); |
bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 32 | GrGLVersion glVer = GrGLGetVersionFromString(versionString); |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 33 | |
bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 34 | if (glVer < GR_GL_VER(1,5)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 35 | // We must have array and element_array buffer objects. |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 36 | return NULL; |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 37 | } |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 38 | GrGLInterface* interface = new GrGLInterface(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 39 | |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 40 | interface->fNPOTRenderTargetSupport = kProbe_GrGLCapability; |
| 41 | interface->fMinRenderTargetHeight = kProbe_GrGLCapability; |
| 42 | interface->fMinRenderTargetWidth = kProbe_GrGLCapability; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 43 | |
| 44 | // Functions that are part of GL 1.1 will return NULL in |
| 45 | // wglGetProcAddress |
bsalomon@google.com | 7d1276f | 2011-10-07 17:01:32 +0000 | [diff] [blame] | 46 | interface->fBindTexture = glBindTexture; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 47 | interface->fBlendFunc = glBlendFunc; |
| 48 | interface->fClear = glClear; |
| 49 | interface->fClearColor = glClearColor; |
| 50 | interface->fClearStencil = glClearStencil; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 51 | interface->fColorMask = glColorMask; |
| 52 | interface->fColorPointer = glColorPointer; |
| 53 | interface->fCullFace = glCullFace; |
| 54 | interface->fDeleteTextures = glDeleteTextures; |
| 55 | interface->fDepthMask = glDepthMask; |
| 56 | interface->fDisable = glDisable; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 57 | interface->fDrawArrays = glDrawArrays; |
| 58 | interface->fDrawElements = glDrawElements; |
| 59 | interface->fDrawBuffer = glDrawBuffer; |
| 60 | interface->fEnable = glEnable; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 61 | interface->fFrontFace = glFrontFace; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 62 | interface->fFinish = glFinish; |
| 63 | interface->fFlush = glFlush; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 64 | interface->fGenTextures = glGenTextures; |
| 65 | interface->fGetError = glGetError; |
| 66 | interface->fGetIntegerv = glGetIntegerv; |
| 67 | interface->fGetString = glGetString; |
| 68 | interface->fGetTexLevelParameteriv = glGetTexLevelParameteriv; |
| 69 | interface->fLineWidth = glLineWidth; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 70 | interface->fPixelStorei = glPixelStorei; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 71 | interface->fReadBuffer = glReadBuffer; |
| 72 | interface->fReadPixels = glReadPixels; |
| 73 | interface->fScissor = glScissor; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 74 | interface->fStencilFunc = glStencilFunc; |
| 75 | interface->fStencilMask = glStencilMask; |
| 76 | interface->fStencilOp = glStencilOp; |
| 77 | interface->fTexImage2D = glTexImage2D; |
| 78 | interface->fTexParameteri = glTexParameteri; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 79 | interface->fTexSubImage2D = glTexSubImage2D; |
| 80 | interface->fViewport = glViewport; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 81 | |
| 82 | GR_GL_GET_PROC(ActiveTexture); |
| 83 | GR_GL_GET_PROC(AttachShader); |
bsalomon@google.com | f97c194 | 2011-10-19 21:35:26 +0000 | [diff] [blame] | 84 | GR_GL_GET_PROC(BeginQuery); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 85 | GR_GL_GET_PROC(BindAttribLocation); |
| 86 | GR_GL_GET_PROC(BindBuffer); |
bsalomon@google.com | bc5cf51 | 2011-09-21 16:21:07 +0000 | [diff] [blame] | 87 | GR_GL_GET_PROC(BindFragDataLocation); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 88 | GR_GL_GET_PROC(BlendColor); |
| 89 | GR_GL_GET_PROC(BufferData); |
| 90 | GR_GL_GET_PROC(BufferSubData); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 91 | GR_GL_GET_PROC(CompileShader); |
| 92 | GR_GL_GET_PROC(CompressedTexImage2D); |
| 93 | GR_GL_GET_PROC(CreateProgram); |
| 94 | GR_GL_GET_PROC(CreateShader); |
| 95 | GR_GL_GET_PROC(DeleteBuffers); |
bsalomon@google.com | f97c194 | 2011-10-19 21:35:26 +0000 | [diff] [blame] | 96 | GR_GL_GET_PROC(DeleteQueries); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 97 | GR_GL_GET_PROC(DeleteProgram); |
| 98 | GR_GL_GET_PROC(DeleteShader); |
| 99 | GR_GL_GET_PROC(DisableVertexAttribArray); |
| 100 | GR_GL_GET_PROC(DrawBuffers); |
| 101 | GR_GL_GET_PROC(EnableVertexAttribArray); |
bsalomon@google.com | f97c194 | 2011-10-19 21:35:26 +0000 | [diff] [blame] | 102 | GR_GL_GET_PROC(EndQuery); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 103 | GR_GL_GET_PROC(GenBuffers); |
bsalomon@google.com | f97c194 | 2011-10-19 21:35:26 +0000 | [diff] [blame] | 104 | GR_GL_GET_PROC(GenQueries); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 105 | GR_GL_GET_PROC(GetBufferParameteriv); |
bsalomon@google.com | f97c194 | 2011-10-19 21:35:26 +0000 | [diff] [blame] | 106 | GR_GL_GET_PROC(GetQueryiv); |
| 107 | GR_GL_GET_PROC(GetQueryObjectiv); |
| 108 | GR_GL_GET_PROC(GetQueryObjectuiv); |
| 109 | if (glVer > GR_GL_VER(3,3) || |
| 110 | GrGLHasExtensionFromString("GL_ARB_timer_query", extString)) { |
| 111 | GR_GL_GET_PROC(GetQueryObjecti64v); |
| 112 | GR_GL_GET_PROC(GetQueryObjectui64v); |
| 113 | GR_GL_GET_PROC(QueryCounter); |
| 114 | } else if (GrGLHasExtensionFromString("GL_EXT_timer_query", extString)) { |
| 115 | GR_GL_GET_PROC_SUFFIX(GetQueryObjecti64v, EXT); |
bsalomon@google.com | e173992 | 2011-10-20 13:26:21 +0000 | [diff] [blame] | 116 | GR_GL_GET_PROC_SUFFIX(GetQueryObjectui64v, EXT); |
bsalomon@google.com | f97c194 | 2011-10-19 21:35:26 +0000 | [diff] [blame] | 117 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 118 | GR_GL_GET_PROC(GetProgramInfoLog); |
| 119 | GR_GL_GET_PROC(GetProgramiv); |
| 120 | GR_GL_GET_PROC(GetShaderInfoLog); |
| 121 | GR_GL_GET_PROC(GetShaderiv); |
| 122 | GR_GL_GET_PROC(GetUniformLocation); |
| 123 | GR_GL_GET_PROC(LinkProgram); |
| 124 | GR_GL_GET_PROC(ShaderSource); |
| 125 | GR_GL_GET_PROC(StencilFuncSeparate); |
| 126 | GR_GL_GET_PROC(StencilMaskSeparate); |
| 127 | GR_GL_GET_PROC(StencilOpSeparate); |
| 128 | GR_GL_GET_PROC(Uniform1f); |
| 129 | GR_GL_GET_PROC(Uniform1i); |
| 130 | GR_GL_GET_PROC(Uniform1fv); |
| 131 | GR_GL_GET_PROC(Uniform1iv); |
| 132 | GR_GL_GET_PROC(Uniform2f); |
| 133 | GR_GL_GET_PROC(Uniform2i); |
| 134 | GR_GL_GET_PROC(Uniform2fv); |
| 135 | GR_GL_GET_PROC(Uniform2iv); |
| 136 | GR_GL_GET_PROC(Uniform3f); |
| 137 | GR_GL_GET_PROC(Uniform3i); |
| 138 | GR_GL_GET_PROC(Uniform3fv); |
| 139 | GR_GL_GET_PROC(Uniform3iv); |
| 140 | GR_GL_GET_PROC(Uniform4f); |
| 141 | GR_GL_GET_PROC(Uniform4i); |
| 142 | GR_GL_GET_PROC(Uniform4fv); |
| 143 | GR_GL_GET_PROC(Uniform4iv); |
| 144 | GR_GL_GET_PROC(UniformMatrix2fv); |
| 145 | GR_GL_GET_PROC(UniformMatrix3fv); |
| 146 | GR_GL_GET_PROC(UniformMatrix4fv); |
| 147 | GR_GL_GET_PROC(UseProgram); |
| 148 | GR_GL_GET_PROC(VertexAttrib4fv); |
| 149 | GR_GL_GET_PROC(VertexAttribPointer); |
| 150 | GR_GL_GET_PROC(BindFragDataLocationIndexed); |
| 151 | |
| 152 | // First look for GL3.0 FBO or GL_ARB_framebuffer_object (same since |
| 153 | // GL_ARB_framebuffer_object doesn't use ARB suffix.) |
bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 154 | if (glVer > GR_GL_VER(3,0) || |
| 155 | GrGLHasExtensionFromString("GL_ARB_framebuffer_object", extString)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 156 | GR_GL_GET_PROC(GenFramebuffers); |
| 157 | GR_GL_GET_PROC(GetFramebufferAttachmentParameteriv); |
| 158 | GR_GL_GET_PROC(GetRenderbufferParameteriv); |
| 159 | GR_GL_GET_PROC(BindFramebuffer); |
| 160 | GR_GL_GET_PROC(FramebufferTexture2D); |
| 161 | GR_GL_GET_PROC(CheckFramebufferStatus); |
| 162 | GR_GL_GET_PROC(DeleteFramebuffers); |
| 163 | GR_GL_GET_PROC(RenderbufferStorage); |
| 164 | GR_GL_GET_PROC(GenRenderbuffers); |
| 165 | GR_GL_GET_PROC(DeleteRenderbuffers); |
| 166 | GR_GL_GET_PROC(FramebufferRenderbuffer); |
| 167 | GR_GL_GET_PROC(BindRenderbuffer); |
| 168 | GR_GL_GET_PROC(RenderbufferStorageMultisample); |
| 169 | GR_GL_GET_PROC(BlitFramebuffer); |
bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 170 | } else if (GrGLHasExtensionFromString("GL_EXT_framebuffer_object", |
| 171 | extString)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 172 | GR_GL_GET_PROC_SUFFIX(GenFramebuffers, EXT); |
| 173 | GR_GL_GET_PROC_SUFFIX(GetFramebufferAttachmentParameteriv, EXT); |
| 174 | GR_GL_GET_PROC_SUFFIX(GetRenderbufferParameteriv, EXT); |
| 175 | GR_GL_GET_PROC_SUFFIX(BindFramebuffer, EXT); |
| 176 | GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, EXT); |
| 177 | GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, EXT); |
| 178 | GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, EXT); |
| 179 | GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, EXT); |
| 180 | GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, EXT); |
| 181 | GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, EXT); |
| 182 | GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT); |
| 183 | GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, EXT); |
bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 184 | if (GrGLHasExtensionFromString("GL_EXT_framebuffer_multisample", extString)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 185 | GR_GL_GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT); |
| 186 | } |
bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 187 | if (GrGLHasExtensionFromString("GL_EXT_framebuffer_blit", extString)) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 188 | GR_GL_GET_PROC_SUFFIX(BlitFramebuffer, EXT); |
| 189 | } |
| 190 | } else { |
| 191 | // we must have FBOs |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 192 | delete interface; |
| 193 | return NULL; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 194 | } |
| 195 | GR_GL_GET_PROC(MapBuffer); |
| 196 | GR_GL_GET_PROC(UnmapBuffer); |
| 197 | |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 198 | interface->fBindingsExported = kDesktop_GrGLBinding; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 199 | |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 200 | return interface; |
| 201 | } else { |
| 202 | return NULL; |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 203 | } |
| 204 | } |