| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> |
| 8 |
| 7 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 | 11 |
| 10 namespace { | 12 namespace { |
| 11 void FinalizeShaderTranslator(void* /* dummy */) { | 13 void FinalizeShaderTranslator(void* /* dummy */) { |
| 12 ShFinalize(); | 14 ShFinalize(); |
| 13 } | 15 } |
| 14 | 16 |
| 15 bool InitializeShaderTranslator() { | 17 bool InitializeShaderTranslator() { |
| 16 static bool initialized = false; | 18 static bool initialized = false; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 57 |
| 56 ShaderTranslator::VariableInfo info(type, size); | 58 ShaderTranslator::VariableInfo info(type, size); |
| 57 (*var_map)[name.get()] = info; | 59 (*var_map)[name.get()] = info; |
| 58 } | 60 } |
| 59 } | 61 } |
| 60 } // namespace | 62 } // namespace |
| 61 | 63 |
| 62 namespace gpu { | 64 namespace gpu { |
| 63 namespace gles2 { | 65 namespace gles2 { |
| 64 | 66 |
| 65 ShaderTranslator::ShaderTranslator() : compiler_(NULL) { | 67 ShaderTranslator::ShaderTranslator() |
| 68 : compiler_(NULL), |
| 69 implementation_is_glsl_es_(false) { |
| 66 } | 70 } |
| 67 | 71 |
| 68 ShaderTranslator::~ShaderTranslator() { | 72 ShaderTranslator::~ShaderTranslator() { |
| 69 if (compiler_ != NULL) | 73 if (compiler_ != NULL) |
| 70 ShDestruct(compiler_); | 74 ShDestruct(compiler_); |
| 71 } | 75 } |
| 72 | 76 |
| 73 bool ShaderTranslator::Init(ShShaderType shader_type, | 77 bool ShaderTranslator::Init(ShShaderType shader_type, |
| 74 ShShaderSpec shader_spec, | 78 ShShaderSpec shader_spec, |
| 75 const ShBuiltInResources* resources) { | 79 const ShBuiltInResources* resources, |
| 80 bool implementation_is_glsl_es) { |
| 76 // Make sure Init is called only once. | 81 // Make sure Init is called only once. |
| 77 DCHECK(compiler_ == NULL); | 82 DCHECK(compiler_ == NULL); |
| 78 DCHECK(shader_type == SH_FRAGMENT_SHADER || shader_type == SH_VERTEX_SHADER); | 83 DCHECK(shader_type == SH_FRAGMENT_SHADER || shader_type == SH_VERTEX_SHADER); |
| 79 DCHECK(shader_spec == SH_GLES2_SPEC || shader_spec == SH_WEBGL_SPEC); | 84 DCHECK(shader_spec == SH_GLES2_SPEC || shader_spec == SH_WEBGL_SPEC); |
| 80 DCHECK(resources != NULL); | 85 DCHECK(resources != NULL); |
| 81 | 86 |
| 82 if (!InitializeShaderTranslator()) | 87 if (!InitializeShaderTranslator()) |
| 83 return false; | 88 return false; |
| 84 | 89 |
| 85 compiler_ = ShConstructCompiler(shader_type, shader_spec, resources); | 90 compiler_ = ShConstructCompiler(shader_type, shader_spec, resources); |
| 91 implementation_is_glsl_es_ = implementation_is_glsl_es; |
| 86 return compiler_ != NULL; | 92 return compiler_ != NULL; |
| 87 } | 93 } |
| 88 | 94 |
| 89 bool ShaderTranslator::Translate(const char* shader) { | 95 bool ShaderTranslator::Translate(const char* shader) { |
| 90 // Make sure this instance is initialized. | 96 // Make sure this instance is initialized. |
| 91 DCHECK(compiler_ != NULL); | 97 DCHECK(compiler_ != NULL); |
| 92 DCHECK(shader != NULL); | 98 DCHECK(shader != NULL); |
| 93 ClearResults(); | 99 ClearResults(); |
| 94 | 100 |
| 95 bool success = false; | 101 bool success = false; |
| 96 int compile_options = SH_OBJECT_CODE | SH_ATTRIBUTES_UNIFORMS; | 102 int compile_options = SH_OBJECT_CODE | SH_ATTRIBUTES_UNIFORMS; |
| 97 if (ShCompile(compiler_, &shader, 1, compile_options)) { | 103 if (ShCompile(compiler_, &shader, 1, compile_options)) { |
| 98 success = true; | 104 success = true; |
| 99 // Get translated shader. | 105 if (!implementation_is_glsl_es_) { |
| 100 int obj_code_len = 0; | 106 // Get translated shader. |
| 101 ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len); | 107 int obj_code_len = 0; |
| 102 if (obj_code_len > 1) { | 108 ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len); |
| 103 translated_shader_.reset(new char[obj_code_len]); | 109 if (obj_code_len > 1) { |
| 104 ShGetObjectCode(compiler_, translated_shader_.get()); | 110 translated_shader_.reset(new char[obj_code_len]); |
| 111 ShGetObjectCode(compiler_, translated_shader_.get()); |
| 112 } |
| 113 } else { |
| 114 // Pass down the original shader's source rather than the |
| 115 // compiler's output. TODO(kbr): once the shader compiler has a |
| 116 // GLSL ES backend, use its output. |
| 117 int shader_code_len = 1 + strlen(shader); |
| 118 if (shader_code_len > 1) { |
| 119 translated_shader_.reset(new char[shader_code_len]); |
| 120 strncpy(translated_shader_.get(), shader, shader_code_len); |
| 121 } |
| 105 } | 122 } |
| 106 // Get info for attribs and uniforms. | 123 // Get info for attribs and uniforms. |
| 107 GetVariableInfo(compiler_, SH_ACTIVE_ATTRIBUTES, &attrib_map_); | 124 GetVariableInfo(compiler_, SH_ACTIVE_ATTRIBUTES, &attrib_map_); |
| 108 GetVariableInfo(compiler_, SH_ACTIVE_UNIFORMS, &uniform_map_); | 125 GetVariableInfo(compiler_, SH_ACTIVE_UNIFORMS, &uniform_map_); |
| 109 } | 126 } |
| 110 | 127 |
| 111 // Get info log. | 128 // Get info log. |
| 112 int info_log_len = 0; | 129 int info_log_len = 0; |
| 113 ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len); | 130 ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len); |
| 114 if (info_log_len > 1) { | 131 if (info_log_len > 1) { |
| 115 info_log_.reset(new char[info_log_len]); | 132 info_log_.reset(new char[info_log_len]); |
| 116 ShGetInfoLog(compiler_, info_log_.get()); | 133 ShGetInfoLog(compiler_, info_log_.get()); |
| 117 } | 134 } |
| 118 | 135 |
| 119 return success; | 136 return success; |
| 120 } | 137 } |
| 121 | 138 |
| 122 void ShaderTranslator::ClearResults() { | 139 void ShaderTranslator::ClearResults() { |
| 123 translated_shader_.reset(); | 140 translated_shader_.reset(); |
| 124 info_log_.reset(); | 141 info_log_.reset(); |
| 125 attrib_map_.clear(); | 142 attrib_map_.clear(); |
| 126 uniform_map_.clear(); | 143 uniform_map_.clear(); |
| 127 } | 144 } |
| 128 | 145 |
| 129 } // namespace gles2 | 146 } // namespace gles2 |
| 130 } // namespace gpu | 147 } // namespace gpu |
| 131 | 148 |
| OLD | NEW |