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 18 matching lines...) Expand all Loading... |
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 void GetVariableInfo(ShHandle compiler, ShShaderInfo var_type, | 37 void GetVariableInfo(ShHandle compiler, ShShaderInfo var_type, |
38 ShaderTranslator::VariableMap* var_map) { | 38 ShaderTranslator::VariableMap* var_map) { |
| 39 if (!var_map) |
| 40 return; |
| 41 var_map->clear(); |
| 42 |
39 size_t name_len = 0, mapped_name_len = 0; | 43 size_t name_len = 0, mapped_name_len = 0; |
40 switch (var_type) { | 44 switch (var_type) { |
41 case SH_ACTIVE_ATTRIBUTES: | 45 case SH_ACTIVE_ATTRIBUTES: |
42 ShGetInfo(compiler, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, &name_len); | 46 ShGetInfo(compiler, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, &name_len); |
43 break; | 47 break; |
44 case SH_ACTIVE_UNIFORMS: | 48 case SH_ACTIVE_UNIFORMS: |
45 ShGetInfo(compiler, SH_ACTIVE_UNIFORM_MAX_LENGTH, &name_len); | 49 ShGetInfo(compiler, SH_ACTIVE_UNIFORM_MAX_LENGTH, &name_len); |
46 break; | 50 break; |
47 case SH_VARYINGS: | 51 case SH_VARYINGS: |
48 ShGetInfo(compiler, SH_VARYING_MAX_LENGTH, &name_len); | 52 ShGetInfo(compiler, SH_VARYING_MAX_LENGTH, &name_len); |
(...skipping 26 matching lines...) Expand all Loading... |
75 mapped_name.get()[mapped_name_len - 1] = '\0'; | 79 mapped_name.get()[mapped_name_len - 1] = '\0'; |
76 | 80 |
77 ShaderTranslator::VariableInfo info( | 81 ShaderTranslator::VariableInfo info( |
78 type, size, precision, static_use, name_string); | 82 type, size, precision, static_use, name_string); |
79 (*var_map)[mapped_name.get()] = info; | 83 (*var_map)[mapped_name.get()] = info; |
80 } | 84 } |
81 } | 85 } |
82 | 86 |
83 void GetNameHashingInfo( | 87 void GetNameHashingInfo( |
84 ShHandle compiler, ShaderTranslator::NameMap* name_map) { | 88 ShHandle compiler, ShaderTranslator::NameMap* name_map) { |
| 89 if (!name_map) |
| 90 return; |
| 91 name_map->clear(); |
| 92 |
85 size_t hashed_names_count = 0; | 93 size_t hashed_names_count = 0; |
86 ShGetInfo(compiler, SH_HASHED_NAMES_COUNT, &hashed_names_count); | 94 ShGetInfo(compiler, SH_HASHED_NAMES_COUNT, &hashed_names_count); |
87 if (hashed_names_count == 0) | 95 if (hashed_names_count == 0) |
88 return; | 96 return; |
89 | 97 |
90 size_t name_max_len = 0, hashed_name_max_len = 0; | 98 size_t name_max_len = 0, hashed_name_max_len = 0; |
91 ShGetInfo(compiler, SH_NAME_MAX_LENGTH, &name_max_len); | 99 ShGetInfo(compiler, SH_NAME_MAX_LENGTH, &name_max_len); |
92 ShGetInfo(compiler, SH_HASHED_NAME_MAX_LENGTH, &hashed_name_max_len); | 100 ShGetInfo(compiler, SH_HASHED_NAME_MAX_LENGTH, &hashed_name_max_len); |
93 | 101 |
94 scoped_ptr<char[]> name(new char[name_max_len]); | 102 scoped_ptr<char[]> name(new char[name_max_len]); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 int compile_options = | 157 int compile_options = |
150 SH_OBJECT_CODE | SH_VARIABLES | SH_ENFORCE_PACKING_RESTRICTIONS | | 158 SH_OBJECT_CODE | SH_VARIABLES | SH_ENFORCE_PACKING_RESTRICTIONS | |
151 SH_LIMIT_EXPRESSION_COMPLEXITY | SH_LIMIT_CALL_STACK_DEPTH | | 159 SH_LIMIT_EXPRESSION_COMPLEXITY | SH_LIMIT_CALL_STACK_DEPTH | |
152 SH_CLAMP_INDIRECT_ARRAY_BOUNDS; | 160 SH_CLAMP_INDIRECT_ARRAY_BOUNDS; |
153 | 161 |
154 compile_options |= driver_bug_workarounds_; | 162 compile_options |= driver_bug_workarounds_; |
155 | 163 |
156 return compile_options; | 164 return compile_options; |
157 } | 165 } |
158 | 166 |
159 bool ShaderTranslator::Translate(const char* shader) { | 167 bool ShaderTranslator::Translate(const std::string& shader_source, |
| 168 std::string* info_log, |
| 169 std::string* translated_source, |
| 170 VariableMap* attrib_map, |
| 171 VariableMap* uniform_map, |
| 172 VariableMap* varying_map, |
| 173 NameMap* name_map) const { |
160 // Make sure this instance is initialized. | 174 // Make sure this instance is initialized. |
161 DCHECK(compiler_ != NULL); | 175 DCHECK(compiler_ != NULL); |
162 DCHECK(shader != NULL); | |
163 ClearResults(); | |
164 | 176 |
165 bool success = false; | 177 bool success = false; |
166 { | 178 { |
167 TRACE_EVENT0("gpu", "ShCompile"); | 179 TRACE_EVENT0("gpu", "ShCompile"); |
168 success = !!ShCompile(compiler_, &shader, 1, GetCompileOptions()); | 180 const char* const shader_strings[] = { shader_source.c_str() }; |
| 181 success = !!ShCompile( |
| 182 compiler_, shader_strings, 1, GetCompileOptions()); |
169 } | 183 } |
170 if (success) { | 184 if (success) { |
171 // Get translated shader. | 185 if (translated_source) { |
172 size_t obj_code_len = 0; | 186 translated_source->clear(); |
173 ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len); | 187 // Get translated shader. |
174 if (obj_code_len > 1) { | 188 size_t obj_code_len = 0; |
175 translated_shader_.reset(new char[obj_code_len]); | 189 ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len); |
176 ShGetObjectCode(compiler_, translated_shader_.get()); | 190 if (obj_code_len > 1) { |
| 191 scoped_ptr<char[]> buffer(new char[obj_code_len]); |
| 192 ShGetObjectCode(compiler_, buffer.get()); |
| 193 *translated_source = std::string(buffer.get(), obj_code_len - 1); |
| 194 } |
177 } | 195 } |
178 // Get info for attribs and uniforms. | 196 // Get info for attribs, uniforms, and varyings. |
179 GetVariableInfo(compiler_, SH_ACTIVE_ATTRIBUTES, &attrib_map_); | 197 GetVariableInfo(compiler_, SH_ACTIVE_ATTRIBUTES, attrib_map); |
180 GetVariableInfo(compiler_, SH_ACTIVE_UNIFORMS, &uniform_map_); | 198 GetVariableInfo(compiler_, SH_ACTIVE_UNIFORMS, uniform_map); |
181 GetVariableInfo(compiler_, SH_VARYINGS, &varying_map_); | 199 GetVariableInfo(compiler_, SH_VARYINGS, varying_map); |
182 // Get info for name hashing. | 200 // Get info for name hashing. |
183 GetNameHashingInfo(compiler_, &name_map_); | 201 GetNameHashingInfo(compiler_, name_map); |
184 } | 202 } |
185 | 203 |
186 // Get info log. | 204 // Get info log. |
187 size_t info_log_len = 0; | 205 if (info_log) { |
188 ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len); | 206 info_log->clear(); |
189 if (info_log_len > 1) { | 207 size_t info_log_len = 0; |
190 info_log_.reset(new char[info_log_len]); | 208 ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len); |
191 ShGetInfoLog(compiler_, info_log_.get()); | 209 if (info_log_len > 1) { |
192 } else { | 210 scoped_ptr<char[]> buffer(new char[info_log_len]); |
193 info_log_.reset(); | 211 ShGetInfoLog(compiler_, buffer.get()); |
| 212 *info_log = std::string(buffer.get(), info_log_len - 1); |
| 213 } |
194 } | 214 } |
195 | 215 |
196 return success; | 216 return success; |
197 } | 217 } |
198 | 218 |
199 std::string ShaderTranslator::GetStringForOptionsThatWouldAffectCompilation() | 219 std::string ShaderTranslator::GetStringForOptionsThatWouldAffectCompilation() |
200 const { | 220 const { |
201 DCHECK(compiler_ != NULL); | 221 DCHECK(compiler_ != NULL); |
202 | 222 |
203 size_t resource_len = 0; | 223 size_t resource_len = 0; |
204 ShGetInfo(compiler_, SH_RESOURCES_STRING_LENGTH, &resource_len); | 224 ShGetInfo(compiler_, SH_RESOURCES_STRING_LENGTH, &resource_len); |
205 DCHECK(resource_len > 1); | 225 DCHECK(resource_len > 1); |
206 scoped_ptr<char[]> resource_str(new char[resource_len]); | 226 scoped_ptr<char[]> resource_str(new char[resource_len]); |
207 | 227 |
208 ShGetBuiltInResourcesString(compiler_, resource_len, resource_str.get()); | 228 ShGetBuiltInResourcesString(compiler_, resource_len, resource_str.get()); |
209 | 229 |
210 return std::string(":CompileOptions:" + | 230 return std::string(":CompileOptions:" + |
211 base::IntToString(GetCompileOptions())) + | 231 base::IntToString(GetCompileOptions())) + |
212 std::string(resource_str.get()); | 232 std::string(resource_str.get()); |
213 } | 233 } |
214 | 234 |
215 const char* ShaderTranslator::translated_shader() const { | |
216 return translated_shader_.get(); | |
217 } | |
218 | |
219 const char* ShaderTranslator::info_log() const { | |
220 return info_log_.get(); | |
221 } | |
222 | |
223 const ShaderTranslatorInterface::VariableMap& | |
224 ShaderTranslator::attrib_map() const { | |
225 return attrib_map_; | |
226 } | |
227 | |
228 const ShaderTranslatorInterface::VariableMap& | |
229 ShaderTranslator::uniform_map() const { | |
230 return uniform_map_; | |
231 } | |
232 | |
233 const ShaderTranslatorInterface::VariableMap& | |
234 ShaderTranslator::varying_map() const { | |
235 return varying_map_; | |
236 } | |
237 | |
238 const ShaderTranslatorInterface::NameMap& | |
239 ShaderTranslator::name_map() const { | |
240 return name_map_; | |
241 } | |
242 | |
243 void ShaderTranslator::AddDestructionObserver( | 235 void ShaderTranslator::AddDestructionObserver( |
244 DestructionObserver* observer) { | 236 DestructionObserver* observer) { |
245 destruction_observers_.AddObserver(observer); | 237 destruction_observers_.AddObserver(observer); |
246 } | 238 } |
247 | 239 |
248 void ShaderTranslator::RemoveDestructionObserver( | 240 void ShaderTranslator::RemoveDestructionObserver( |
249 DestructionObserver* observer) { | 241 DestructionObserver* observer) { |
250 destruction_observers_.RemoveObserver(observer); | 242 destruction_observers_.RemoveObserver(observer); |
251 } | 243 } |
252 | 244 |
253 ShaderTranslator::~ShaderTranslator() { | 245 ShaderTranslator::~ShaderTranslator() { |
254 FOR_EACH_OBSERVER(DestructionObserver, | 246 FOR_EACH_OBSERVER(DestructionObserver, |
255 destruction_observers_, | 247 destruction_observers_, |
256 OnDestruct(this)); | 248 OnDestruct(this)); |
257 | 249 |
258 if (compiler_ != NULL) | 250 if (compiler_ != NULL) |
259 ShDestruct(compiler_); | 251 ShDestruct(compiler_); |
260 } | 252 } |
261 | 253 |
262 void ShaderTranslator::ClearResults() { | |
263 translated_shader_.reset(); | |
264 info_log_.reset(); | |
265 attrib_map_.clear(); | |
266 uniform_map_.clear(); | |
267 varying_map_.clear(); | |
268 name_map_.clear(); | |
269 } | |
270 | |
271 } // namespace gles2 | 254 } // namespace gles2 |
272 } // namespace gpu | 255 } // namespace gpu |
273 | 256 |
OLD | NEW |