| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2010 Apple 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 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "config.h" | |
| 27 | |
| 28 #include "core/platform/graphics/ANGLEWebKitBridge.h" | |
| 29 #include "wtf/OwnArrayPtr.h" | |
| 30 | |
| 31 namespace WebCore { | |
| 32 | |
| 33 typedef size_t ANGLEGetInfoType; | |
| 34 | |
| 35 inline static ANGLEGetInfoType getValidationResultValue(const ShHandle compiler,
ShShaderInfo shaderInfo) | |
| 36 { | |
| 37 ANGLEGetInfoType value = 0; | |
| 38 ShGetInfo(compiler, shaderInfo, &value); | |
| 39 return value; | |
| 40 } | |
| 41 | |
| 42 static bool getSymbolInfo(ShHandle compiler, ShShaderInfo symbolType, Vector<ANG
LEShaderSymbol>& symbols) | |
| 43 { | |
| 44 ShShaderInfo symbolMaxNameLengthType; | |
| 45 | |
| 46 switch (symbolType) { | |
| 47 case SH_ACTIVE_ATTRIBUTES: | |
| 48 symbolMaxNameLengthType = SH_ACTIVE_ATTRIBUTE_MAX_LENGTH; | |
| 49 break; | |
| 50 case SH_ACTIVE_UNIFORMS: | |
| 51 symbolMaxNameLengthType = SH_ACTIVE_UNIFORM_MAX_LENGTH; | |
| 52 break; | |
| 53 default: | |
| 54 ASSERT_NOT_REACHED(); | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 ANGLEGetInfoType numSymbols = getValidationResultValue(compiler, symbolType)
; | |
| 59 | |
| 60 ANGLEGetInfoType maxNameLength = getValidationResultValue(compiler, symbolMa
xNameLengthType); | |
| 61 if (maxNameLength <= 1) | |
| 62 return false; | |
| 63 | |
| 64 ANGLEGetInfoType maxMappedNameLength = getValidationResultValue(compiler, SH
_MAPPED_NAME_MAX_LENGTH); | |
| 65 if (maxMappedNameLength <= 1) | |
| 66 return false; | |
| 67 | |
| 68 // The maximum allowed symbol name length is 256 characters. | |
| 69 Vector<char, 256> nameBuffer(maxNameLength); | |
| 70 Vector<char, 256> mappedNameBuffer(maxMappedNameLength); | |
| 71 | |
| 72 for (ANGLEGetInfoType i = 0; i < numSymbols; ++i) { | |
| 73 ANGLEShaderSymbol symbol; | |
| 74 ANGLEGetInfoType nameLength = 0; | |
| 75 switch (symbolType) { | |
| 76 case SH_ACTIVE_ATTRIBUTES: | |
| 77 symbol.symbolType = SHADER_SYMBOL_TYPE_ATTRIBUTE; | |
| 78 #if ANGLE_SH_VERSION >= 112 | |
| 79 ShGetVariableInfo(compiler, symbolType, i, &nameLength, &symbol.size
, &symbol.dataType, &symbol.precision, &symbol.staticUse, nameBuffer.data(), map
pedNameBuffer.data()); | |
| 80 #else | |
| 81 ShGetVariableInfo(compiler, symbolType, i, &nameLength, &symbol.size
, &symbol.dataType, &symbol.precision, nameBuffer.data(), mappedNameBuffer.data(
)); | |
| 82 #endif | |
| 83 break; | |
| 84 case SH_ACTIVE_UNIFORMS: | |
| 85 symbol.symbolType = SHADER_SYMBOL_TYPE_UNIFORM; | |
| 86 #if ANGLE_SH_VERSION >= 112 | |
| 87 ShGetVariableInfo(compiler, symbolType, i, &nameLength, &symbol.size
, &symbol.dataType, &symbol.precision, &symbol.staticUse, nameBuffer.data(), map
pedNameBuffer.data()); | |
| 88 #else | |
| 89 ShGetVariableInfo(compiler, symbolType, i, &nameLength, &symbol.size
, &symbol.dataType, &symbol.precision, nameBuffer.data(), mappedNameBuffer.data(
)); | |
| 90 #endif | |
| 91 break; | |
| 92 default: | |
| 93 ASSERT_NOT_REACHED(); | |
| 94 return false; | |
| 95 } | |
| 96 if (!nameLength) | |
| 97 return false; | |
| 98 | |
| 99 // The ShGetActive* calls above are guaranteed to produce null-terminate
d strings for | |
| 100 // nameBuffer and mappedNameBuffer. Also, the character set for symbol n
ames | |
| 101 // is a subset of Latin-1 as specified by the OpenGL ES Shading Language
, Section 3.1 and | |
| 102 // WebGL, Section "Characters Outside the GLSL Source Character Set". | |
| 103 | |
| 104 String name = String(nameBuffer.data()); | |
| 105 String mappedName = String(mappedNameBuffer.data()); | |
| 106 | |
| 107 // ANGLE returns array names in the format "array[0]". | |
| 108 // The only way to know if a symbol is an array is to check if it ends w
ith "[0]". | |
| 109 // We can't check the size because regular symbols and arrays of length
1 both have a size of 1. | |
| 110 symbol.isArray = name.endsWith("[0]") && mappedName.endsWith("[0]"); | |
| 111 if (symbol.isArray) { | |
| 112 // Add a symbol for the array name without the "[0]" suffix. | |
| 113 name.truncate(name.length() - 3); | |
| 114 mappedName.truncate(mappedName.length() - 3); | |
| 115 } | |
| 116 | |
| 117 symbol.name = name; | |
| 118 symbol.mappedName = mappedName; | |
| 119 symbols.append(symbol); | |
| 120 | |
| 121 if (symbol.isArray) { | |
| 122 // Add symbols for each array element. | |
| 123 symbol.isArray = false; | |
| 124 for (int i = 0; i < symbol.size; i++) { | |
| 125 String arrayBrackets = "[" + String::number(i) + "]"; | |
| 126 symbol.name = name + arrayBrackets; | |
| 127 symbol.mappedName = mappedName + arrayBrackets; | |
| 128 symbols.append(symbol); | |
| 129 } | |
| 130 } | |
| 131 } | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 ANGLEWebKitBridge::ANGLEWebKitBridge(ShShaderOutput shaderOutput, ShShaderSpec s
haderSpec) | |
| 136 : builtCompilers(false) | |
| 137 , m_fragmentCompiler(0) | |
| 138 , m_vertexCompiler(0) | |
| 139 , m_shaderOutput(shaderOutput) | |
| 140 , m_shaderSpec(shaderSpec) | |
| 141 { | |
| 142 // This is a no-op if it's already initialized. | |
| 143 ShInitialize(); | |
| 144 } | |
| 145 | |
| 146 ANGLEWebKitBridge::~ANGLEWebKitBridge() | |
| 147 { | |
| 148 cleanupCompilers(); | |
| 149 } | |
| 150 | |
| 151 void ANGLEWebKitBridge::cleanupCompilers() | |
| 152 { | |
| 153 if (m_fragmentCompiler) | |
| 154 ShDestruct(m_fragmentCompiler); | |
| 155 m_fragmentCompiler = 0; | |
| 156 if (m_vertexCompiler) | |
| 157 ShDestruct(m_vertexCompiler); | |
| 158 m_vertexCompiler = 0; | |
| 159 | |
| 160 builtCompilers = false; | |
| 161 } | |
| 162 | |
| 163 void ANGLEWebKitBridge::setResources(ShBuiltInResources resources) | |
| 164 { | |
| 165 // Resources are (possibly) changing - cleanup compilers if we had them alre
ady | |
| 166 cleanupCompilers(); | |
| 167 | |
| 168 m_resources = resources; | |
| 169 } | |
| 170 | |
| 171 bool ANGLEWebKitBridge::compileShaderSource(const char* shaderSource, ANGLEShade
rType shaderType, String& translatedShaderSource, String& shaderValidationLog, V
ector<ANGLEShaderSymbol>& symbols, int extraCompileOptions) | |
| 172 { | |
| 173 if (!builtCompilers) { | |
| 174 m_fragmentCompiler = ShConstructCompiler(SH_FRAGMENT_SHADER, m_shaderSpe
c, m_shaderOutput, &m_resources); | |
| 175 m_vertexCompiler = ShConstructCompiler(SH_VERTEX_SHADER, m_shaderSpec, m
_shaderOutput, &m_resources); | |
| 176 if (!m_fragmentCompiler || !m_vertexCompiler) { | |
| 177 cleanupCompilers(); | |
| 178 return false; | |
| 179 } | |
| 180 | |
| 181 builtCompilers = true; | |
| 182 } | |
| 183 | |
| 184 ShHandle compiler; | |
| 185 | |
| 186 if (shaderType == SHADER_TYPE_VERTEX) | |
| 187 compiler = m_vertexCompiler; | |
| 188 else | |
| 189 compiler = m_fragmentCompiler; | |
| 190 | |
| 191 const char* const shaderSourceStrings[] = { shaderSource }; | |
| 192 | |
| 193 #if ANGLE_SH_VERSION >= 111 | |
| 194 bool validateSuccess = ShCompile(compiler, shaderSourceStrings, 1, SH_OBJECT
_CODE | SH_VARIABLES | extraCompileOptions); | |
| 195 #else | |
| 196 bool validateSuccess = ShCompile(compiler, shaderSourceStrings, 1, SH_OBJECT
_CODE | SH_ATTRIBUTES_UNIFORMS | extraCompileOptions); | |
| 197 #endif | |
| 198 if (!validateSuccess) { | |
| 199 int logSize = getValidationResultValue(compiler, SH_INFO_LOG_LENGTH); | |
| 200 if (logSize > 1) { | |
| 201 OwnArrayPtr<char> logBuffer = adoptArrayPtr(new char[logSize]); | |
| 202 if (logBuffer) { | |
| 203 ShGetInfoLog(compiler, logBuffer.get()); | |
| 204 shaderValidationLog = logBuffer.get(); | |
| 205 } | |
| 206 } | |
| 207 return false; | |
| 208 } | |
| 209 | |
| 210 int translationLength = getValidationResultValue(compiler, SH_OBJECT_CODE_LE
NGTH); | |
| 211 if (translationLength > 1) { | |
| 212 OwnArrayPtr<char> translationBuffer = adoptArrayPtr(new char[translation
Length]); | |
| 213 if (!translationBuffer) | |
| 214 return false; | |
| 215 ShGetObjectCode(compiler, translationBuffer.get()); | |
| 216 translatedShaderSource = translationBuffer.get(); | |
| 217 } | |
| 218 | |
| 219 if (!getSymbolInfo(compiler, SH_ACTIVE_ATTRIBUTES, symbols)) | |
| 220 return false; | |
| 221 if (!getSymbolInfo(compiler, SH_ACTIVE_UNIFORMS, symbols)) | |
| 222 return false; | |
| 223 | |
| 224 return true; | |
| 225 } | |
| 226 | |
| 227 } | |
| OLD | NEW |