blob: ab0d351f2325deb76e5fb84815df0bc0484f07cb [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
Scroggo9df214e2011-04-15 14:48:08 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
Scroggo9df214e2011-04-15 14:48:08 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
Scroggo9df214e2011-04-15 14:48:08 +000010#include "GrGLInterface.h"
11
12#include <GL/glx.h>
13#include <GL/gl.h>
14#include <GL/glext.h>
15#include <GL/glu.h>
16
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000017#define GR_GL_GET_PROC(F) interface->f ## F = (GrGL ## F ## Proc) \
Scroggo9df214e2011-04-15 14:48:08 +000018 glXGetProcAddress(reinterpret_cast<const GLubyte*>("gl" #F));
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000019#define GR_GL_GET_PROC_SUFFIX(F, S) interface->f ## F = (GrGL ## F ## Proc) \
Scroggo9df214e2011-04-15 14:48:08 +000020 glXGetProcAddress(reinterpret_cast<const GLubyte*>("gl" #F #S));
21
bsalomon@google.com373a6632011-10-19 20:43:20 +000022const GrGLInterface* GrGLCreateNativeInterface() {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000023 if (NULL != glXGetCurrentContext()) {
Scroggo9df214e2011-04-15 14:48:08 +000024 const char* versionString = (const char*) glGetString(GL_VERSION);
25 const char* extString = (const char*) glGetString(GL_EXTENSIONS);
bsalomon@google.comc82b8892011-09-22 14:10:33 +000026 GrGLVersion glVer = GrGLGetVersionFromString(versionString);
Scroggo9df214e2011-04-15 14:48:08 +000027
bsalomon@google.comc82b8892011-09-22 14:10:33 +000028 if (glVer < GR_GL_VER(1,5)) {
Scroggo9df214e2011-04-15 14:48:08 +000029 // We must have array and element_array buffer objects.
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000030 return NULL;
Scroggo9df214e2011-04-15 14:48:08 +000031 }
32
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000033 GrGLInterface* interface = new GrGLInterface();
tomhudson@google.com747bf292011-06-14 18:16:52 +000034
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000035 interface->fActiveTexture = glActiveTexture;
Scroggo9df214e2011-04-15 14:48:08 +000036 GR_GL_GET_PROC(AttachShader);
37 GR_GL_GET_PROC(BindAttribLocation);
38 GR_GL_GET_PROC(BindBuffer);
bsalomon@google.combc5cf512011-09-21 16:21:07 +000039 GR_GL_GET_PROC(BindFragDataLocation);
bsalomon@google.com373a6632011-10-19 20:43:20 +000040 GR_GL_GET_PROC(BeginQuery);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000041 interface->fBindTexture = glBindTexture;
42 interface->fBlendColor = glBlendColor;
43 interface->fBlendFunc = glBlendFunc;
Scroggo9df214e2011-04-15 14:48:08 +000044 GR_GL_GET_PROC(BufferData);
45 GR_GL_GET_PROC(BufferSubData);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000046 interface->fClear = glClear;
47 interface->fClearColor = glClearColor;
48 interface->fClearStencil = glClearStencil;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000049 interface->fColorMask = glColorMask;
50 interface->fColorPointer = glColorPointer;
Scroggo9df214e2011-04-15 14:48:08 +000051 GR_GL_GET_PROC(CompileShader);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000052 interface->fCompressedTexImage2D = glCompressedTexImage2D;
Scroggo9df214e2011-04-15 14:48:08 +000053 GR_GL_GET_PROC(CreateProgram);
54 GR_GL_GET_PROC(CreateShader);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000055 interface->fCullFace = glCullFace;
Scroggo9df214e2011-04-15 14:48:08 +000056 GR_GL_GET_PROC(DeleteBuffers);
57 GR_GL_GET_PROC(DeleteProgram);
bsalomon@google.com373a6632011-10-19 20:43:20 +000058 GR_GL_GET_PROC(DeleteQueries);
Scroggo9df214e2011-04-15 14:48:08 +000059 GR_GL_GET_PROC(DeleteShader);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000060 interface->fDeleteTextures = glDeleteTextures;
61 interface->fDepthMask = glDepthMask;
62 interface->fDisable = glDisable;
Scroggo9df214e2011-04-15 14:48:08 +000063 GR_GL_GET_PROC(DisableVertexAttribArray);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000064 interface->fDrawArrays = glDrawArrays;
65 interface->fDrawBuffer = glDrawBuffer;
bsalomon@google.comd32c5f52011-08-02 19:29:03 +000066 GR_GL_GET_PROC(DrawBuffers);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000067 interface->fDrawElements = glDrawElements;
68 interface->fEnable = glEnable;
Scroggo9df214e2011-04-15 14:48:08 +000069 GR_GL_GET_PROC(EnableVertexAttribArray);
bsalomon@google.com373a6632011-10-19 20:43:20 +000070 GR_GL_GET_PROC(EndQuery);
71 interface->fFinish = glFinish;
72 interface->fFlush = glFlush;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000073 interface->fFrontFace = glFrontFace;
Scroggo9df214e2011-04-15 14:48:08 +000074 GR_GL_GET_PROC(GenBuffers);
75 GR_GL_GET_PROC(GetBufferParameteriv);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000076 interface->fGetError = glGetError;
77 interface->fGetIntegerv = glGetIntegerv;
bsalomon@google.com373a6632011-10-19 20:43:20 +000078 GR_GL_GET_PROC(GetQueryObjectiv);
79 GR_GL_GET_PROC(GetQueryObjectuiv);
80 if (glVer >= GR_GL_VER(3,3) ||
81 GrGLHasExtensionFromString("GL_ARB_timer_query", extString)) {
82 GR_GL_GET_PROC(GetQueryObjecti64v);
83 GR_GL_GET_PROC(GetQueryObjectui64v);
84 GR_GL_GET_PROC(QueryCounter);
85 } else if (GrGLHasExtensionFromString("GL_EXT_timer_query", extString)) {
bsalomon@google.comf97c1942011-10-19 21:35:26 +000086 GR_GL_GET_PROC_SUFFIX(GetQueryObjecti64v, EXT);
87 GR_GL_GET_PROC_SUFFIX(GetQueryObjectui64v, EXT);
bsalomon@google.com373a6632011-10-19 20:43:20 +000088 }
89 GR_GL_GET_PROC(GetQueryiv);
Scroggo9df214e2011-04-15 14:48:08 +000090 GR_GL_GET_PROC(GetProgramInfoLog);
91 GR_GL_GET_PROC(GetProgramiv);
92 GR_GL_GET_PROC(GetShaderInfoLog);
93 GR_GL_GET_PROC(GetShaderiv);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000094 interface->fGetString = glGetString;
95 interface->fGetTexLevelParameteriv = glGetTexLevelParameteriv;
bsalomon@google.com373a6632011-10-19 20:43:20 +000096 GR_GL_GET_PROC(GenQueries);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000097 interface->fGenTextures = glGenTextures;
Scroggo9df214e2011-04-15 14:48:08 +000098 GR_GL_GET_PROC(GetUniformLocation);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000099 interface->fLineWidth = glLineWidth;
Scroggo9df214e2011-04-15 14:48:08 +0000100 GR_GL_GET_PROC(LinkProgram);
Scroggo9df214e2011-04-15 14:48:08 +0000101 GR_GL_GET_PROC(MapBuffer);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000102 interface->fPixelStorei = glPixelStorei;
103 interface->fReadBuffer = glReadBuffer;
104 interface->fReadPixels = glReadPixels;
105 interface->fScissor = glScissor;
Scroggo9df214e2011-04-15 14:48:08 +0000106 GR_GL_GET_PROC(ShaderSource);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000107 interface->fStencilFunc = glStencilFunc;
Scroggo9df214e2011-04-15 14:48:08 +0000108 GR_GL_GET_PROC(StencilFuncSeparate);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000109 interface->fStencilMask = glStencilMask;
Scroggo9df214e2011-04-15 14:48:08 +0000110 GR_GL_GET_PROC(StencilMaskSeparate);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000111 interface->fStencilOp = glStencilOp;
Scroggo9df214e2011-04-15 14:48:08 +0000112 GR_GL_GET_PROC(StencilOpSeparate);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000113 interface->fTexImage2D = glTexImage2D;
114 interface->fTexParameteri = glTexParameteri;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000115 if (glVer >= GR_GL_VER(4,2) ||
116 GrGLHasExtensionFromString("GL_ARB_texture_storage", extString)) {
117 GR_GL_GET_PROC(TexStorage2D);
118 } else if (GrGLHasExtensionFromString("GL_EXT_texture_storage", extString)) {
119 GR_GL_GET_PROC_SUFFIX(TexStorage2D, EXT);
120 }
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000121 interface->fTexSubImage2D = glTexSubImage2D;
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000122 GR_GL_GET_PROC(Uniform1f);
Scroggo9df214e2011-04-15 14:48:08 +0000123 GR_GL_GET_PROC(Uniform1i);
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000124 GR_GL_GET_PROC(Uniform1fv);
125 GR_GL_GET_PROC(Uniform1iv);
126 GR_GL_GET_PROC(Uniform2f);
127 GR_GL_GET_PROC(Uniform2i);
128 GR_GL_GET_PROC(Uniform2fv);
129 GR_GL_GET_PROC(Uniform2iv);
130 GR_GL_GET_PROC(Uniform3f);
131 GR_GL_GET_PROC(Uniform3i);
132 GR_GL_GET_PROC(Uniform3fv);
133 GR_GL_GET_PROC(Uniform3iv);
134 GR_GL_GET_PROC(Uniform4f);
135 GR_GL_GET_PROC(Uniform4i);
Scroggo9df214e2011-04-15 14:48:08 +0000136 GR_GL_GET_PROC(Uniform4fv);
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000137 GR_GL_GET_PROC(Uniform4iv);
138 GR_GL_GET_PROC(UniformMatrix2fv);
Scroggo9df214e2011-04-15 14:48:08 +0000139 GR_GL_GET_PROC(UniformMatrix3fv);
bsalomon@google.combf2a4692011-05-04 12:35:39 +0000140 GR_GL_GET_PROC(UniformMatrix4fv);
Scroggo9df214e2011-04-15 14:48:08 +0000141 GR_GL_GET_PROC(UnmapBuffer);
142 GR_GL_GET_PROC(UseProgram);
143 GR_GL_GET_PROC(VertexAttrib4fv);
144 GR_GL_GET_PROC(VertexAttribPointer);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000145 interface->fViewport = glViewport;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000146 GR_GL_GET_PROC(BindFragDataLocationIndexed);
Scroggo9df214e2011-04-15 14:48:08 +0000147
148 // First look for GL3.0 FBO or GL_ARB_framebuffer_object (same since
149 // GL_ARB_framebuffer_object doesn't use ARB suffix.)
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000150 if (glVer >= GR_GL_VER(3,0) ||
151 GrGLHasExtensionFromString("GL_ARB_framebuffer_object",
152 extString)) {
Scroggo9df214e2011-04-15 14:48:08 +0000153 GR_GL_GET_PROC(GenFramebuffers);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000154 GR_GL_GET_PROC(GetFramebufferAttachmentParameteriv);
155 GR_GL_GET_PROC(GetRenderbufferParameteriv);
Scroggo9df214e2011-04-15 14:48:08 +0000156 GR_GL_GET_PROC(BindFramebuffer);
157 GR_GL_GET_PROC(FramebufferTexture2D);
158 GR_GL_GET_PROC(CheckFramebufferStatus);
159 GR_GL_GET_PROC(DeleteFramebuffers);
160 GR_GL_GET_PROC(RenderbufferStorage);
161 GR_GL_GET_PROC(GenRenderbuffers);
162 GR_GL_GET_PROC(DeleteRenderbuffers);
163 GR_GL_GET_PROC(FramebufferRenderbuffer);
164 GR_GL_GET_PROC(BindRenderbuffer);
165 GR_GL_GET_PROC(RenderbufferStorageMultisample);
166 GR_GL_GET_PROC(BlitFramebuffer);
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000167 } else if (GrGLHasExtensionFromString("GL_EXT_framebuffer_object",
168 extString)) {
Scroggo9df214e2011-04-15 14:48:08 +0000169 GR_GL_GET_PROC_SUFFIX(GenFramebuffers, EXT);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000170 GR_GL_GET_PROC_SUFFIX(GetFramebufferAttachmentParameteriv, EXT);
171 GR_GL_GET_PROC_SUFFIX(GetRenderbufferParameteriv, EXT);
Scroggo9df214e2011-04-15 14:48:08 +0000172 GR_GL_GET_PROC_SUFFIX(BindFramebuffer, EXT);
173 GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, EXT);
174 GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, EXT);
175 GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, EXT);
176 GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, EXT);
177 GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, EXT);
178 GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, EXT);
179 GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT);
180 GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, EXT);
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000181 if (GrGLHasExtensionFromString("GL_EXT_framebuffer_multisample",
Scroggo9df214e2011-04-15 14:48:08 +0000182 extString)) {
183 GR_GL_GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT);
184 }
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000185 if (GrGLHasExtensionFromString("GL_EXT_framebuffer_blit",
Scroggo9df214e2011-04-15 14:48:08 +0000186 extString)) {
187 GR_GL_GET_PROC_SUFFIX(BlitFramebuffer, EXT);
188 }
189 } else {
190 // we must have FBOs
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000191 delete interface;
192 return NULL;
Scroggo9df214e2011-04-15 14:48:08 +0000193 }
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000194 interface->fBindingsExported = kDesktop_GrGLBinding;
Scroggo9df214e2011-04-15 14:48:08 +0000195
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000196 return interface;
197 } else {
198 return NULL;
Scroggo9df214e2011-04-15 14:48:08 +0000199 }
Scroggo9df214e2011-04-15 14:48:08 +0000200}