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