1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if ENABLE(GRAPHICS_CONTEXT_3D)
29
30#include "Extensions3DOpenGL.h"
31
32#include "GraphicsContext3D.h"
33
34#if PLATFORM(GTK) || PLATFORM(WIN)
35#include "OpenGLShims.h"
36#elif USE(OPENGL_ES)
37#include <OpenGLES/ES2/glext.h>
38#elif USE(OPENGL)
39#include <OpenGL/gl.h>
40#endif
41
42#if PLATFORM(IOS_FAMILY)
43#include "GraphicsContext3DIOS.h"
44#endif
45
46namespace WebCore {
47
48Extensions3DOpenGL::Extensions3DOpenGL(GraphicsContext3D* context, bool useIndexedGetString)
49 : Extensions3DOpenGLCommon(context, useIndexedGetString)
50{
51}
52
53Extensions3DOpenGL::~Extensions3DOpenGL() = default;
54
55
56void Extensions3DOpenGL::blitFramebuffer(long srcX0, long srcY0, long srcX1, long srcY1, long dstX0, long dstY0, long dstX1, long dstY1, unsigned long mask, unsigned long filter)
57{
58#if PLATFORM(COCOA) && USE(OPENGL_ES)
59 UNUSED_PARAM(srcX0);
60 UNUSED_PARAM(srcY0);
61 UNUSED_PARAM(srcX1);
62 UNUSED_PARAM(srcY1);
63 UNUSED_PARAM(dstX0);
64 UNUSED_PARAM(dstY0);
65 UNUSED_PARAM(dstX1);
66 UNUSED_PARAM(dstY1);
67 UNUSED_PARAM(mask);
68 UNUSED_PARAM(filter);
69 ::glResolveMultisampleFramebufferAPPLE();
70#else
71 ::glBlitFramebufferEXT(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
72#endif
73}
74
75void Extensions3DOpenGL::renderbufferStorageMultisample(unsigned long target, unsigned long samples, unsigned long internalformat, unsigned long width, unsigned long height)
76{
77 ::glRenderbufferStorageMultisampleEXT(target, samples, internalformat, width, height);
78}
79
80Platform3DObject Extensions3DOpenGL::createVertexArrayOES()
81{
82 m_context->makeContextCurrent();
83 GLuint array = 0;
84#if PLATFORM(GTK) || PLATFORM(WIN) || (PLATFORM(COCOA) && USE(OPENGL_ES))
85 if (isVertexArrayObjectSupported())
86 glGenVertexArrays(1, &array);
87#elif defined(GL_APPLE_vertex_array_object) && GL_APPLE_vertex_array_object
88 glGenVertexArraysAPPLE(1, &array);
89#endif
90 return array;
91}
92
93void Extensions3DOpenGL::deleteVertexArrayOES(Platform3DObject array)
94{
95 if (!array)
96 return;
97
98 m_context->makeContextCurrent();
99#if PLATFORM(GTK) || PLATFORM(WIN) || (PLATFORM(COCOA) && USE(OPENGL_ES))
100 if (isVertexArrayObjectSupported())
101 glDeleteVertexArrays(1, &array);
102#elif defined(GL_APPLE_vertex_array_object) && GL_APPLE_vertex_array_object
103 glDeleteVertexArraysAPPLE(1, &array);
104#endif
105}
106
107GC3Dboolean Extensions3DOpenGL::isVertexArrayOES(Platform3DObject array)
108{
109 if (!array)
110 return GL_FALSE;
111
112 m_context->makeContextCurrent();
113#if PLATFORM(GTK) || PLATFORM(WIN) || (PLATFORM(COCOA) && USE(OPENGL_ES))
114 if (isVertexArrayObjectSupported())
115 return glIsVertexArray(array);
116#elif defined(GL_APPLE_vertex_array_object) && GL_APPLE_vertex_array_object
117 return glIsVertexArrayAPPLE(array);
118#endif
119 return GL_FALSE;
120}
121
122void Extensions3DOpenGL::bindVertexArrayOES(Platform3DObject array)
123{
124 m_context->makeContextCurrent();
125#if PLATFORM(GTK) || PLATFORM(WIN) || (PLATFORM(COCOA) && USE(OPENGL_ES))
126 if (isVertexArrayObjectSupported())
127 glBindVertexArray(array);
128#elif defined(GL_APPLE_vertex_array_object) && GL_APPLE_vertex_array_object
129 glBindVertexArrayAPPLE(array);
130#else
131 UNUSED_PARAM(array);
132#endif
133}
134
135void Extensions3DOpenGL::insertEventMarkerEXT(const String&)
136{
137 // FIXME: implement this function and add GL_EXT_debug_marker in supports().
138 return;
139}
140
141void Extensions3DOpenGL::pushGroupMarkerEXT(const String&)
142{
143 // FIXME: implement this function and add GL_EXT_debug_marker in supports().
144 return;
145}
146
147void Extensions3DOpenGL::popGroupMarkerEXT(void)
148{
149 // FIXME: implement this function and add GL_EXT_debug_marker in supports().
150 return;
151}
152
153bool Extensions3DOpenGL::supportsExtension(const String& name)
154{
155 // GL_ANGLE_framebuffer_blit and GL_ANGLE_framebuffer_multisample are "fake". They are implemented using other
156 // extensions. In particular GL_EXT_framebuffer_blit and GL_EXT_framebuffer_multisample/GL_APPLE_framebuffer_multisample.
157 if (name == "GL_ANGLE_framebuffer_blit")
158 return m_availableExtensions.contains("GL_EXT_framebuffer_blit");
159 if (name == "GL_ANGLE_framebuffer_multisample")
160#if PLATFORM(IOS_FAMILY)
161 return m_availableExtensions.contains("GL_APPLE_framebuffer_multisample");
162#else
163 return m_availableExtensions.contains("GL_EXT_framebuffer_multisample");
164#endif
165
166 if (name == "GL_ANGLE_instanced_arrays") {
167 return (m_availableExtensions.contains("GL_ARB_instanced_arrays") || m_availableExtensions.contains("GL_EXT_instanced_arrays"))
168 && (m_availableExtensions.contains("GL_ARB_draw_instanced") || m_availableExtensions.contains("GL_EXT_draw_instanced"));
169 }
170
171 if (name == "GL_EXT_sRGB")
172#if PLATFORM(IOS_FAMILY)
173 return m_availableExtensions.contains("GL_EXT_sRGB");
174#else
175 return m_availableExtensions.contains("GL_EXT_texture_sRGB") && (m_availableExtensions.contains("GL_EXT_framebuffer_sRGB") || m_availableExtensions.contains("GL_ARB_framebuffer_sRGB"));
176#endif
177
178 if (name == "GL_EXT_frag_depth")
179#if PLATFORM(MAC)
180 return true;
181#else
182 return m_availableExtensions.contains("GL_EXT_frag_depth");
183#endif
184
185 // Desktop GL always supports GL_OES_rgb8_rgba8.
186 if (name == "GL_OES_rgb8_rgba8")
187 return true;
188
189 // If GL_ARB_texture_float or GL_OES_texture_float is available then we report
190 // GL_OES_texture_half_float, GL_OES_texture_float_linear and GL_OES_texture_half_float_linear as available.
191 if (name == "GL_OES_texture_float" || name == "GL_OES_texture_half_float" || name == "GL_OES_texture_float_linear" || name == "GL_OES_texture_half_float_linear")
192 return m_availableExtensions.contains("GL_ARB_texture_float") || m_availableExtensions.contains("GL_OES_texture_float");
193
194 // GL_OES_vertex_array_object
195 if (name == "GL_OES_vertex_array_object") {
196#if (PLATFORM(GTK))
197 return m_availableExtensions.contains("GL_ARB_vertex_array_object");
198#elif PLATFORM(IOS_FAMILY)
199 return m_availableExtensions.contains("GL_OES_vertex_array_object");
200#else
201 return m_availableExtensions.contains("GL_APPLE_vertex_array_object");
202#endif
203 }
204
205 // Desktop GL always supports the standard derivative functions
206 if (name == "GL_OES_standard_derivatives")
207 return true;
208
209 // Desktop GL always supports UNSIGNED_INT indices
210 if (name == "GL_OES_element_index_uint")
211 return true;
212
213 if (name == "GL_EXT_shader_texture_lod")
214 return m_availableExtensions.contains("GL_EXT_shader_texture_lod");
215
216 if (name == "GL_EXT_texture_filter_anisotropic")
217 return m_availableExtensions.contains("GL_EXT_texture_filter_anisotropic");
218
219 if (name == "GL_EXT_draw_buffers") {
220#if PLATFORM(IOS_FAMILY)
221 return m_availableExtensions.contains(name);
222#elif PLATFORM(MAC) || PLATFORM(GTK)
223 return m_availableExtensions.contains("GL_ARB_draw_buffers");
224#else
225 // FIXME: implement support for other platforms.
226 return false;
227#endif
228 }
229
230#if PLATFORM(IOS_FAMILY)
231 if (name == "GL_EXT_packed_depth_stencil")
232 return m_availableExtensions.contains("GL_OES_packed_depth_stencil");
233#endif
234
235 return m_availableExtensions.contains(name);
236}
237
238void Extensions3DOpenGL::drawBuffersEXT(GC3Dsizei n, const GC3Denum* bufs)
239{
240 // FIXME: implement support for other platforms.
241#if PLATFORM(MAC)
242 ::glDrawBuffersARB(n, bufs);
243#elif PLATFORM(GTK)
244 ::glDrawBuffers(n, bufs);
245#else
246 UNUSED_PARAM(n);
247 UNUSED_PARAM(bufs);
248#endif
249}
250
251void Extensions3DOpenGL::drawArraysInstanced(GC3Denum mode, GC3Dint first, GC3Dsizei count, GC3Dsizei primcount)
252{
253 m_context->makeContextCurrent();
254#if PLATFORM(GTK)
255 ::glDrawArraysInstanced(mode, first, count, primcount);
256#elif PLATFORM(COCOA)
257 ::glDrawArraysInstancedARB(mode, first, count, primcount);
258#else
259 UNUSED_PARAM(mode);
260 UNUSED_PARAM(first);
261 UNUSED_PARAM(count);
262 UNUSED_PARAM(primcount);
263#endif
264}
265
266void Extensions3DOpenGL::drawElementsInstanced(GC3Denum mode, GC3Dsizei count, GC3Denum type, long long offset, GC3Dsizei primcount)
267{
268 m_context->makeContextCurrent();
269#if PLATFORM(GTK)
270 ::glDrawElementsInstanced(mode, count, type, reinterpret_cast<GLvoid*>(static_cast<intptr_t>(offset)), primcount);
271#elif PLATFORM(COCOA)
272 ::glDrawElementsInstancedARB(mode, count, type, reinterpret_cast<GLvoid*>(static_cast<intptr_t>(offset)), primcount);
273#else
274 UNUSED_PARAM(mode);
275 UNUSED_PARAM(count);
276 UNUSED_PARAM(type);
277 UNUSED_PARAM(offset);
278 UNUSED_PARAM(primcount);
279#endif
280}
281
282void Extensions3DOpenGL::vertexAttribDivisor(GC3Duint index, GC3Duint divisor)
283{
284 m_context->makeContextCurrent();
285#if PLATFORM(GTK)
286 ::glVertexAttribDivisor(index, divisor);
287#elif PLATFORM(COCOA)
288 ::glVertexAttribDivisorARB(index, divisor);
289#else
290 UNUSED_PARAM(index);
291 UNUSED_PARAM(divisor);
292#endif
293}
294
295String Extensions3DOpenGL::getExtensions()
296{
297 ASSERT(!m_useIndexedGetString);
298 return String(reinterpret_cast<const char*>(::glGetString(GL_EXTENSIONS)));
299}
300
301#if PLATFORM(GTK) || PLATFORM(WIN) || (PLATFORM(COCOA) && USE(OPENGL_ES))
302bool Extensions3DOpenGL::isVertexArrayObjectSupported()
303{
304 static const bool supportsVertexArrayObject = supports("GL_OES_vertex_array_object");
305 return supportsVertexArrayObject;
306}
307#endif
308
309} // namespace WebCore
310
311#endif // ENABLE(GRAPHICS_CONTEXT_3D)
312