2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "Extensions3DOpenGLCommon.h"
32 #include "ANGLEWebKitBridge.h"
33 #include "GraphicsContext3D.h"
35 #if PLATFORM(BLACKBERRY)
36 #include <BlackBerryPlatformLog.h>
40 #include "OpenGLESShims.h"
41 #include <GLES2/gl2.h>
42 #include <GLES2/gl2ext.h>
44 #include <OpenGL/gl.h>
45 #elif PLATFORM(GTK) || PLATFORM(EFL) || PLATFORM(WIN) || PLATFORM(NIX)
46 #include "OpenGLShims.h"
49 #include <wtf/MainThread.h>
50 #include <wtf/Vector.h>
54 Extensions3DOpenGLCommon::Extensions3DOpenGLCommon(GraphicsContext3D* context)
55 : m_initializedAvailableExtensions(false)
60 , m_maySupportMultisampling(true)
61 , m_requiresBuiltInFunctionEmulation(false)
63 m_vendor = String(reinterpret_cast<const char*>(::glGetString(GL_VENDOR)));
65 Vector<String> vendorComponents;
66 m_vendor.lower().split(' ', vendorComponents);
67 if (vendorComponents.contains("nvidia"))
69 if (vendorComponents.contains("ati") || vendorComponents.contains("amd"))
71 if (vendorComponents.contains("intel"))
75 if (m_isAMD || m_isIntel)
76 m_requiresBuiltInFunctionEmulation = true;
78 // Currently in Mac we only allow multisampling if the vendor is NVIDIA,
79 // or if the vendor is AMD/ATI and the system is 10.7.2 and above.
81 bool systemSupportsMultisampling = true;
82 #if !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED < 1080
83 ASSERT(isMainThread());
84 static SInt32 version;
86 if (Gestalt(gestaltSystemVersion, &version) != noErr)
87 systemSupportsMultisampling = false;
89 // See https://bugs.webkit.org/show_bug.cgi?id=77922 for more details
90 if (systemSupportsMultisampling)
91 systemSupportsMultisampling = version >= 0x1072;
92 #endif // SNOW_LEOPARD and LION
94 if (m_isAMD && !systemSupportsMultisampling)
95 m_maySupportMultisampling = false;
99 Extensions3DOpenGLCommon::~Extensions3DOpenGLCommon()
103 bool Extensions3DOpenGLCommon::supports(const String& name)
105 if (!m_initializedAvailableExtensions)
106 initializeAvailableExtensions();
108 return supportsExtension(name);
111 void Extensions3DOpenGLCommon::ensureEnabled(const String& name)
113 if (name == "GL_OES_standard_derivatives") {
114 // Enable support in ANGLE (if not enabled already)
115 ANGLEWebKitBridge& compiler = m_context->m_compiler;
116 ShBuiltInResources ANGLEResources = compiler.getResources();
117 if (!ANGLEResources.OES_standard_derivatives) {
118 ANGLEResources.OES_standard_derivatives = 1;
119 compiler.setResources(ANGLEResources);
121 } else if (name == "GL_EXT_draw_buffers") {
122 // Enable support in ANGLE (if not enabled already)
123 ANGLEWebKitBridge& compiler = m_context->m_compiler;
124 ShBuiltInResources ANGLEResources = compiler.getResources();
125 if (!ANGLEResources.EXT_draw_buffers) {
126 ANGLEResources.EXT_draw_buffers = 1;
127 m_context->getIntegerv(Extensions3D::MAX_DRAW_BUFFERS_EXT, &ANGLEResources.MaxDrawBuffers);
128 compiler.setResources(ANGLEResources);
133 bool Extensions3DOpenGLCommon::isEnabled(const String& name)
135 if (name == "GL_OES_standard_derivatives") {
136 ANGLEWebKitBridge& compiler = m_context->m_compiler;
137 return compiler.getResources().OES_standard_derivatives;
139 return supports(name);
142 int Extensions3DOpenGLCommon::getGraphicsResetStatusARB()
144 return GraphicsContext3D::NO_ERROR;
147 String Extensions3DOpenGLCommon::getTranslatedShaderSourceANGLE(Platform3DObject shader)
151 ANGLEShaderType shaderType;
153 ANGLEWebKitBridge& compiler = m_context->m_compiler;
155 m_context->getShaderiv(shader, GraphicsContext3D::SHADER_TYPE, &GLshaderType);
157 if (GLshaderType == GraphicsContext3D::VERTEX_SHADER)
158 shaderType = SHADER_TYPE_VERTEX;
159 else if (GLshaderType == GraphicsContext3D::FRAGMENT_SHADER)
160 shaderType = SHADER_TYPE_FRAGMENT;
162 return ""; // Invalid shader type.
164 HashMap<Platform3DObject, GraphicsContext3D::ShaderSourceEntry>::iterator result = m_context->m_shaderSourceMap.find(shader);
166 if (result == m_context->m_shaderSourceMap.end())
169 GraphicsContext3D::ShaderSourceEntry& entry = result->value;
171 String translatedShaderSource;
172 String shaderInfoLog;
173 int extraCompileOptions = SH_MAP_LONG_VARIABLE_NAMES | SH_CLAMP_INDIRECT_ARRAY_BOUNDS | SH_UNFOLD_SHORT_CIRCUIT | SH_ENFORCE_PACKING_RESTRICTIONS;
175 if (m_requiresBuiltInFunctionEmulation)
176 extraCompileOptions |= SH_EMULATE_BUILT_IN_FUNCTIONS;
178 Vector<ANGLEShaderSymbol> symbols;
179 bool isValid = compiler.compileShaderSource(entry.source.utf8().data(), shaderType, translatedShaderSource, shaderInfoLog, symbols, extraCompileOptions);
181 entry.log = shaderInfoLog;
182 entry.isValid = isValid;
184 size_t numSymbols = symbols.size();
185 for (size_t i = 0; i < numSymbols; ++i) {
186 ANGLEShaderSymbol shaderSymbol = symbols[i];
187 GraphicsContext3D::SymbolInfo symbolInfo(shaderSymbol.dataType, shaderSymbol.size, shaderSymbol.mappedName, shaderSymbol.precision, shaderSymbol.staticUse);
188 entry.symbolMap(shaderSymbol.symbolType).set(shaderSymbol.name, symbolInfo);
194 return translatedShaderSource;
197 void Extensions3DOpenGLCommon::initializeAvailableExtensions()
199 String extensionsString = getExtensions();
200 Vector<String> availableExtensions;
201 extensionsString.split(" ", availableExtensions);
202 for (size_t i = 0; i < availableExtensions.size(); ++i)
203 m_availableExtensions.add(availableExtensions[i]);
204 m_initializedAvailableExtensions = true;
207 void Extensions3DOpenGLCommon::readnPixelsEXT(int, int, GC3Dsizei, GC3Dsizei, GC3Denum, GC3Denum, GC3Dsizei, void *)
209 m_context->synthesizeGLError(GL_INVALID_OPERATION);
212 void Extensions3DOpenGLCommon::getnUniformfvEXT(GC3Duint, int, GC3Dsizei, float *)
214 m_context->synthesizeGLError(GL_INVALID_OPERATION);
217 void Extensions3DOpenGLCommon::getnUniformivEXT(GC3Duint, int, GC3Dsizei, int *)
219 m_context->synthesizeGLError(GL_INVALID_OPERATION);
222 } // namespace WebCore
224 #endif // USE(3D_GRAPHICS)