| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gpu/command_buffer/service/shader_translator.h" | 5 #include "gpu/command_buffer/service/shader_translator.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <GLES2/gl2.h> | 8 #include <GLES2/gl2.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 ~ShaderTranslatorInitializer() { | 28 ~ShaderTranslatorInitializer() { |
| 29 TRACE_EVENT0("gpu", "ShFinalize"); | 29 TRACE_EVENT0("gpu", "ShFinalize"); |
| 30 ShFinalize(); | 30 ShFinalize(); |
| 31 } | 31 } |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 base::LazyInstance<ShaderTranslatorInitializer> g_translator_initializer = | 34 base::LazyInstance<ShaderTranslatorInitializer> g_translator_initializer = |
| 35 LAZY_INSTANCE_INITIALIZER; | 35 LAZY_INSTANCE_INITIALIZER; |
| 36 | 36 |
| 37 #if !defined(ANGLE_SH_VERSION) || ANGLE_SH_VERSION < 108 | |
| 38 typedef int ANGLEGetInfoType; | |
| 39 #else | |
| 40 typedef size_t ANGLEGetInfoType; | |
| 41 #endif | |
| 42 | |
| 43 void GetVariableInfo(ShHandle compiler, ShShaderInfo var_type, | 37 void GetVariableInfo(ShHandle compiler, ShShaderInfo var_type, |
| 44 ShaderTranslator::VariableMap* var_map) { | 38 ShaderTranslator::VariableMap* var_map) { |
| 45 ANGLEGetInfoType name_len = 0, mapped_name_len = 0; | 39 size_t name_len = 0, mapped_name_len = 0; |
| 46 switch (var_type) { | 40 switch (var_type) { |
| 47 case SH_ACTIVE_ATTRIBUTES: | 41 case SH_ACTIVE_ATTRIBUTES: |
| 48 ShGetInfo(compiler, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, &name_len); | 42 ShGetInfo(compiler, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, &name_len); |
| 49 break; | 43 break; |
| 50 case SH_ACTIVE_UNIFORMS: | 44 case SH_ACTIVE_UNIFORMS: |
| 51 ShGetInfo(compiler, SH_ACTIVE_UNIFORM_MAX_LENGTH, &name_len); | 45 ShGetInfo(compiler, SH_ACTIVE_UNIFORM_MAX_LENGTH, &name_len); |
| 52 break; | 46 break; |
| 53 case SH_VARYINGS: | 47 case SH_VARYINGS: |
| 54 ShGetInfo(compiler, SH_VARYING_MAX_LENGTH, &name_len); | 48 ShGetInfo(compiler, SH_VARYING_MAX_LENGTH, &name_len); |
| 55 break; | 49 break; |
| 56 default: NOTREACHED(); | 50 default: NOTREACHED(); |
| 57 } | 51 } |
| 58 ShGetInfo(compiler, SH_MAPPED_NAME_MAX_LENGTH, &mapped_name_len); | 52 ShGetInfo(compiler, SH_MAPPED_NAME_MAX_LENGTH, &mapped_name_len); |
| 59 if (name_len <= 1 || mapped_name_len <= 1) return; | 53 if (name_len <= 1 || mapped_name_len <= 1) return; |
| 60 scoped_ptr<char[]> name(new char[name_len]); | 54 scoped_ptr<char[]> name(new char[name_len]); |
| 61 scoped_ptr<char[]> mapped_name(new char[mapped_name_len]); | 55 scoped_ptr<char[]> mapped_name(new char[mapped_name_len]); |
| 62 | 56 |
| 63 ANGLEGetInfoType num_vars = 0; | 57 size_t num_vars = 0; |
| 64 ShGetInfo(compiler, var_type, &num_vars); | 58 ShGetInfo(compiler, var_type, &num_vars); |
| 65 for (ANGLEGetInfoType i = 0; i < num_vars; ++i) { | 59 for (size_t i = 0; i < num_vars; ++i) { |
| 66 ANGLEGetInfoType len = 0; | 60 size_t len = 0; |
| 67 int size = 0; | 61 int size = 0; |
| 68 #if (ANGLE_SH_VERSION >= 126) | |
| 69 sh::GLenum type = GL_NONE; | 62 sh::GLenum type = GL_NONE; |
| 70 #else | |
| 71 ShDataType type = SH_NONE; | |
| 72 #endif | |
| 73 ShPrecisionType precision = SH_PRECISION_UNDEFINED; | 63 ShPrecisionType precision = SH_PRECISION_UNDEFINED; |
| 74 int static_use = 0; | 64 int static_use = 0; |
| 75 | 65 |
| 76 ShGetVariableInfo(compiler, var_type, i, | 66 ShGetVariableInfo(compiler, var_type, i, |
| 77 &len, &size, &type, &precision, &static_use, | 67 &len, &size, &type, &precision, &static_use, |
| 78 name.get(), mapped_name.get()); | 68 name.get(), mapped_name.get()); |
| 79 | 69 |
| 80 // In theory we should CHECK(len <= name_len - 1) here, but ANGLE needs | 70 // In theory we should CHECK(len <= name_len - 1) here, but ANGLE needs |
| 81 // to handle long struct field name mapping before we can do this. | 71 // to handle long struct field name mapping before we can do this. |
| 82 // Also, we should modify the ANGLE interface to also return a length | 72 // Also, we should modify the ANGLE interface to also return a length |
| 83 // for mapped_name. | 73 // for mapped_name. |
| 84 std::string name_string(name.get(), std::min(len, name_len - 1)); | 74 std::string name_string(name.get(), std::min(len, name_len - 1)); |
| 85 mapped_name.get()[mapped_name_len - 1] = '\0'; | 75 mapped_name.get()[mapped_name_len - 1] = '\0'; |
| 86 | 76 |
| 87 ShaderTranslator::VariableInfo info( | 77 ShaderTranslator::VariableInfo info( |
| 88 type, size, precision, static_use, name_string); | 78 type, size, precision, static_use, name_string); |
| 89 (*var_map)[mapped_name.get()] = info; | 79 (*var_map)[mapped_name.get()] = info; |
| 90 } | 80 } |
| 91 } | 81 } |
| 92 | 82 |
| 93 void GetNameHashingInfo( | 83 void GetNameHashingInfo( |
| 94 ShHandle compiler, ShaderTranslator::NameMap* name_map) { | 84 ShHandle compiler, ShaderTranslator::NameMap* name_map) { |
| 95 ANGLEGetInfoType hashed_names_count = 0; | 85 size_t hashed_names_count = 0; |
| 96 ShGetInfo(compiler, SH_HASHED_NAMES_COUNT, &hashed_names_count); | 86 ShGetInfo(compiler, SH_HASHED_NAMES_COUNT, &hashed_names_count); |
| 97 if (hashed_names_count == 0) | 87 if (hashed_names_count == 0) |
| 98 return; | 88 return; |
| 99 | 89 |
| 100 ANGLEGetInfoType name_max_len = 0, hashed_name_max_len = 0; | 90 size_t name_max_len = 0, hashed_name_max_len = 0; |
| 101 ShGetInfo(compiler, SH_NAME_MAX_LENGTH, &name_max_len); | 91 ShGetInfo(compiler, SH_NAME_MAX_LENGTH, &name_max_len); |
| 102 ShGetInfo(compiler, SH_HASHED_NAME_MAX_LENGTH, &hashed_name_max_len); | 92 ShGetInfo(compiler, SH_HASHED_NAME_MAX_LENGTH, &hashed_name_max_len); |
| 103 | 93 |
| 104 scoped_ptr<char[]> name(new char[name_max_len]); | 94 scoped_ptr<char[]> name(new char[name_max_len]); |
| 105 scoped_ptr<char[]> hashed_name(new char[hashed_name_max_len]); | 95 scoped_ptr<char[]> hashed_name(new char[hashed_name_max_len]); |
| 106 | 96 |
| 107 for (ANGLEGetInfoType i = 0; i < hashed_names_count; ++i) { | 97 for (size_t i = 0; i < hashed_names_count; ++i) { |
| 108 ShGetNameHashingEntry(compiler, i, name.get(), hashed_name.get()); | 98 ShGetNameHashingEntry(compiler, i, name.get(), hashed_name.get()); |
| 109 (*name_map)[hashed_name.get()] = name.get(); | 99 (*name_map)[hashed_name.get()] = name.get(); |
| 110 } | 100 } |
| 111 } | 101 } |
| 112 | 102 |
| 113 } // namespace | 103 } // namespace |
| 114 | 104 |
| 115 namespace gpu { | 105 namespace gpu { |
| 116 namespace gles2 { | 106 namespace gles2 { |
| 117 | 107 |
| 118 ShaderTranslator::DestructionObserver::DestructionObserver() { | 108 ShaderTranslator::DestructionObserver::DestructionObserver() { |
| 119 } | 109 } |
| 120 | 110 |
| 121 ShaderTranslator::DestructionObserver::~DestructionObserver() { | 111 ShaderTranslator::DestructionObserver::~DestructionObserver() { |
| 122 } | 112 } |
| 123 | 113 |
| 124 ShaderTranslator::ShaderTranslator() | 114 ShaderTranslator::ShaderTranslator() |
| 125 : compiler_(NULL), | 115 : compiler_(NULL), |
| 126 implementation_is_glsl_es_(false), | 116 implementation_is_glsl_es_(false), |
| 127 driver_bug_workarounds_(static_cast<ShCompileOptions>(0)) { | 117 driver_bug_workarounds_(static_cast<ShCompileOptions>(0)) { |
| 128 } | 118 } |
| 129 | 119 |
| 130 bool ShaderTranslator::Init( | 120 bool ShaderTranslator::Init( |
| 131 #if (ANGLE_SH_VERSION >= 126) | |
| 132 GLenum shader_type, | 121 GLenum shader_type, |
| 133 #else | |
| 134 ShShaderType shader_type, | |
| 135 #endif | |
| 136 ShShaderSpec shader_spec, | 122 ShShaderSpec shader_spec, |
| 137 const ShBuiltInResources* resources, | 123 const ShBuiltInResources* resources, |
| 138 ShaderTranslatorInterface::GlslImplementationType glsl_implementation_type, | 124 ShaderTranslatorInterface::GlslImplementationType glsl_implementation_type, |
| 139 ShCompileOptions driver_bug_workarounds) { | 125 ShCompileOptions driver_bug_workarounds) { |
| 140 // Make sure Init is called only once. | 126 // Make sure Init is called only once. |
| 141 DCHECK(compiler_ == NULL); | 127 DCHECK(compiler_ == NULL); |
| 142 #if (ANGLE_SH_VERSION >= 126) | |
| 143 DCHECK(shader_type == GL_FRAGMENT_SHADER || shader_type == GL_VERTEX_SHADER); | 128 DCHECK(shader_type == GL_FRAGMENT_SHADER || shader_type == GL_VERTEX_SHADER); |
| 144 #else | |
| 145 DCHECK(shader_type == SH_FRAGMENT_SHADER || shader_type == SH_VERTEX_SHADER); | |
| 146 #endif | |
| 147 DCHECK(shader_spec == SH_GLES2_SPEC || shader_spec == SH_WEBGL_SPEC); | 129 DCHECK(shader_spec == SH_GLES2_SPEC || shader_spec == SH_WEBGL_SPEC); |
| 148 DCHECK(resources != NULL); | 130 DCHECK(resources != NULL); |
| 149 | 131 |
| 150 g_translator_initializer.Get(); | 132 g_translator_initializer.Get(); |
| 151 | 133 |
| 152 ShShaderOutput shader_output = | 134 ShShaderOutput shader_output = |
| 153 (glsl_implementation_type == kGlslES ? SH_ESSL_OUTPUT : SH_GLSL_OUTPUT); | 135 (glsl_implementation_type == kGlslES ? SH_ESSL_OUTPUT : SH_GLSL_OUTPUT); |
| 154 | 136 |
| 155 { | 137 { |
| 156 TRACE_EVENT0("gpu", "ShConstructCompiler"); | 138 TRACE_EVENT0("gpu", "ShConstructCompiler"); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 180 DCHECK(shader != NULL); | 162 DCHECK(shader != NULL); |
| 181 ClearResults(); | 163 ClearResults(); |
| 182 | 164 |
| 183 bool success = false; | 165 bool success = false; |
| 184 { | 166 { |
| 185 TRACE_EVENT0("gpu", "ShCompile"); | 167 TRACE_EVENT0("gpu", "ShCompile"); |
| 186 success = !!ShCompile(compiler_, &shader, 1, GetCompileOptions()); | 168 success = !!ShCompile(compiler_, &shader, 1, GetCompileOptions()); |
| 187 } | 169 } |
| 188 if (success) { | 170 if (success) { |
| 189 // Get translated shader. | 171 // Get translated shader. |
| 190 ANGLEGetInfoType obj_code_len = 0; | 172 size_t obj_code_len = 0; |
| 191 ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len); | 173 ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len); |
| 192 if (obj_code_len > 1) { | 174 if (obj_code_len > 1) { |
| 193 translated_shader_.reset(new char[obj_code_len]); | 175 translated_shader_.reset(new char[obj_code_len]); |
| 194 ShGetObjectCode(compiler_, translated_shader_.get()); | 176 ShGetObjectCode(compiler_, translated_shader_.get()); |
| 195 } | 177 } |
| 196 // Get info for attribs and uniforms. | 178 // Get info for attribs and uniforms. |
| 197 GetVariableInfo(compiler_, SH_ACTIVE_ATTRIBUTES, &attrib_map_); | 179 GetVariableInfo(compiler_, SH_ACTIVE_ATTRIBUTES, &attrib_map_); |
| 198 GetVariableInfo(compiler_, SH_ACTIVE_UNIFORMS, &uniform_map_); | 180 GetVariableInfo(compiler_, SH_ACTIVE_UNIFORMS, &uniform_map_); |
| 199 GetVariableInfo(compiler_, SH_VARYINGS, &varying_map_); | 181 GetVariableInfo(compiler_, SH_VARYINGS, &varying_map_); |
| 200 // Get info for name hashing. | 182 // Get info for name hashing. |
| 201 GetNameHashingInfo(compiler_, &name_map_); | 183 GetNameHashingInfo(compiler_, &name_map_); |
| 202 } | 184 } |
| 203 | 185 |
| 204 // Get info log. | 186 // Get info log. |
| 205 ANGLEGetInfoType info_log_len = 0; | 187 size_t info_log_len = 0; |
| 206 ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len); | 188 ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len); |
| 207 if (info_log_len > 1) { | 189 if (info_log_len > 1) { |
| 208 info_log_.reset(new char[info_log_len]); | 190 info_log_.reset(new char[info_log_len]); |
| 209 ShGetInfoLog(compiler_, info_log_.get()); | 191 ShGetInfoLog(compiler_, info_log_.get()); |
| 210 } else { | 192 } else { |
| 211 info_log_.reset(); | 193 info_log_.reset(); |
| 212 } | 194 } |
| 213 | 195 |
| 214 return success; | 196 return success; |
| 215 } | 197 } |
| 216 | 198 |
| 217 std::string ShaderTranslator::GetStringForOptionsThatWouldAffectCompilation() | 199 std::string ShaderTranslator::GetStringForOptionsThatWouldAffectCompilation() |
| 218 const { | 200 const { |
| 219 #if ANGLE_SH_VERSION >= 124 | |
| 220 DCHECK(compiler_ != NULL); | 201 DCHECK(compiler_ != NULL); |
| 221 | 202 |
| 222 ANGLEGetInfoType resource_len = 0; | 203 size_t resource_len = 0; |
| 223 ShGetInfo(compiler_, SH_RESOURCES_STRING_LENGTH, &resource_len); | 204 ShGetInfo(compiler_, SH_RESOURCES_STRING_LENGTH, &resource_len); |
| 224 DCHECK(resource_len > 1); | 205 DCHECK(resource_len > 1); |
| 225 scoped_ptr<char[]> resource_str(new char[resource_len]); | 206 scoped_ptr<char[]> resource_str(new char[resource_len]); |
| 226 | 207 |
| 227 ShGetBuiltInResourcesString(compiler_, resource_len, resource_str.get()); | 208 ShGetBuiltInResourcesString(compiler_, resource_len, resource_str.get()); |
| 228 | 209 |
| 229 return std::string(":CompileOptions:" + | 210 return std::string(":CompileOptions:" + |
| 230 base::IntToString(GetCompileOptions())) + | 211 base::IntToString(GetCompileOptions())) + |
| 231 std::string(resource_str.get()); | 212 std::string(resource_str.get()); |
| 232 #else | |
| 233 #if ANGLE_SH_VERSION >= 123 | |
| 234 const size_t kNumIntFields = 21; | |
| 235 #elif ANGLE_SH_VERSION >= 122 | |
| 236 const size_t kNumIntFields = 20; | |
| 237 #else | |
| 238 const size_t kNumIntFields = 16; | |
| 239 #endif | |
| 240 const size_t kNumEnumFields = 1; | |
| 241 const size_t kNumFunctionPointerFields = 1; | |
| 242 struct MustMatchShBuiltInResource { | |
| 243 typedef khronos_uint64_t (*FunctionPointer)(const char*, size_t); | |
| 244 enum Enum { | |
| 245 kFirst, | |
| 246 }; | |
| 247 int int_fields[kNumIntFields]; | |
| 248 FunctionPointer pointer_fields[kNumFunctionPointerFields]; | |
| 249 Enum enum_fields[kNumEnumFields]; | |
| 250 }; | |
| 251 // If this assert fails most likely that means something below needs updating. | |
| 252 COMPILE_ASSERT( | |
| 253 sizeof(ShBuiltInResources) == sizeof(MustMatchShBuiltInResource), | |
| 254 Fields_Have_Changed_In_ShBuiltInResource_So_Update_Below); | |
| 255 | |
| 256 return std::string( | |
| 257 ":CompileOptions:" + | |
| 258 base::IntToString(GetCompileOptions()) + | |
| 259 ":MaxVertexAttribs:" + | |
| 260 base::IntToString(compiler_options_.MaxVertexAttribs) + | |
| 261 ":MaxVertexUniformVectors:" + | |
| 262 base::IntToString(compiler_options_.MaxVertexUniformVectors) + | |
| 263 ":MaxVaryingVectors:" + | |
| 264 base::IntToString(compiler_options_.MaxVaryingVectors) + | |
| 265 ":MaxVertexTextureImageUnits:" + | |
| 266 base::IntToString(compiler_options_.MaxVertexTextureImageUnits) + | |
| 267 ":MaxCombinedTextureImageUnits:" + | |
| 268 base::IntToString(compiler_options_.MaxCombinedTextureImageUnits) + | |
| 269 ":MaxTextureImageUnits:" + | |
| 270 base::IntToString(compiler_options_.MaxTextureImageUnits) + | |
| 271 ":MaxFragmentUniformVectors:" + | |
| 272 base::IntToString(compiler_options_.MaxFragmentUniformVectors) + | |
| 273 ":MaxDrawBuffers:" + | |
| 274 base::IntToString(compiler_options_.MaxDrawBuffers) + | |
| 275 ":OES_standard_derivatives:" + | |
| 276 base::IntToString(compiler_options_.OES_standard_derivatives) + | |
| 277 ":OES_EGL_image_external:" + | |
| 278 base::IntToString(compiler_options_.OES_EGL_image_external) + | |
| 279 ":ARB_texture_rectangle:" + | |
| 280 base::IntToString(compiler_options_.ARB_texture_rectangle) + | |
| 281 ":EXT_draw_buffers:" + | |
| 282 base::IntToString(compiler_options_.EXT_draw_buffers) + | |
| 283 ":FragmentPrecisionHigh:" + | |
| 284 base::IntToString(compiler_options_.FragmentPrecisionHigh) + | |
| 285 ":MaxExpressionComplexity:" + | |
| 286 base::IntToString(compiler_options_.MaxExpressionComplexity) + | |
| 287 ":MaxCallStackDepth:" + | |
| 288 base::IntToString(compiler_options_.MaxCallStackDepth) + | |
| 289 ":EXT_frag_depth:" + | |
| 290 #if ANGLE_SH_VERSION >= 122 | |
| 291 base::IntToString(compiler_options_.EXT_frag_depth) + | |
| 292 #if ANGLE_SH_VERSION >= 123 | |
| 293 ":EXT_shader_texture_lod:" + | |
| 294 base::IntToString(compiler_options_.EXT_shader_texture_lod) + | |
| 295 #endif | |
| 296 ":MaxVertexOutputVectors:" + | |
| 297 base::IntToString(compiler_options_.MaxVertexOutputVectors) + | |
| 298 ":MaxFragmentInputVectors:" + | |
| 299 base::IntToString(compiler_options_.MaxFragmentInputVectors) + | |
| 300 ":MinProgramTexelOffset:" + | |
| 301 base::IntToString(compiler_options_.MinProgramTexelOffset) + | |
| 302 ":MaxProgramTexelOffset:" + | |
| 303 base::IntToString(compiler_options_.MaxProgramTexelOffset)); | |
| 304 #else // ANGLE_SH_VERSION < 122 | |
| 305 base::IntToString(compiler_options_.EXT_frag_depth)); | |
| 306 #endif | |
| 307 #endif | |
| 308 } | 213 } |
| 309 | 214 |
| 310 const char* ShaderTranslator::translated_shader() const { | 215 const char* ShaderTranslator::translated_shader() const { |
| 311 return translated_shader_.get(); | 216 return translated_shader_.get(); |
| 312 } | 217 } |
| 313 | 218 |
| 314 const char* ShaderTranslator::info_log() const { | 219 const char* ShaderTranslator::info_log() const { |
| 315 return info_log_.get(); | 220 return info_log_.get(); |
| 316 } | 221 } |
| 317 | 222 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 info_log_.reset(); | 264 info_log_.reset(); |
| 360 attrib_map_.clear(); | 265 attrib_map_.clear(); |
| 361 uniform_map_.clear(); | 266 uniform_map_.clear(); |
| 362 varying_map_.clear(); | 267 varying_map_.clear(); |
| 363 name_map_.clear(); | 268 name_map_.clear(); |
| 364 } | 269 } |
| 365 | 270 |
| 366 } // namespace gles2 | 271 } // namespace gles2 |
| 367 } // namespace gpu | 272 } // namespace gpu |
| 368 | 273 |
| OLD | NEW |