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 <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <algorithm> | 10 #include <algorithm> |
11 | 11 |
12 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
18 #include "ui/gl/gl_implementation.h" | 18 #include "ui/gl/gl_implementation.h" |
19 #include "ui/gl/gl_version_info.h" | 19 #include "ui/gl/gl_version_info.h" |
20 | 20 |
21 namespace gpu { | 21 namespace gpu { |
22 namespace gles2 { | 22 namespace gles2 { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 class ShaderTranslatorInitializer { | 26 class ShaderTranslatorInitializer { |
27 public: | 27 public: |
28 ShaderTranslatorInitializer() { | 28 ShaderTranslatorInitializer() { |
29 TRACE_EVENT0("gpu", "ShInitialize"); | 29 TRACE_EVENT0("gpu", "ShInitialize"); |
30 CHECK(ShInitialize()); | 30 CHECK(sh::Initialize()); |
31 } | 31 } |
32 | 32 |
33 ~ShaderTranslatorInitializer() { | 33 ~ShaderTranslatorInitializer() { |
34 TRACE_EVENT0("gpu", "ShFinalize"); | 34 TRACE_EVENT0("gpu", "ShFinalize"); |
35 ShFinalize(); | 35 sh::Finalize(); |
36 } | 36 } |
37 }; | 37 }; |
38 | 38 |
39 base::LazyInstance<ShaderTranslatorInitializer> g_translator_initializer = | 39 base::LazyInstance<ShaderTranslatorInitializer> g_translator_initializer = |
40 LAZY_INSTANCE_INITIALIZER; | 40 LAZY_INSTANCE_INITIALIZER; |
41 | 41 |
42 void GetAttributes(ShHandle compiler, AttributeMap* var_map) { | 42 void GetAttributes(ShHandle compiler, AttributeMap* var_map) { |
43 if (!var_map) | 43 if (!var_map) |
44 return; | 44 return; |
45 var_map->clear(); | 45 var_map->clear(); |
46 const std::vector<sh::Attribute>* attribs = ShGetAttributes(compiler); | 46 const std::vector<sh::Attribute>* attribs = sh::GetAttributes(compiler); |
47 if (attribs) { | 47 if (attribs) { |
48 for (size_t ii = 0; ii < attribs->size(); ++ii) | 48 for (size_t ii = 0; ii < attribs->size(); ++ii) |
49 (*var_map)[(*attribs)[ii].mappedName] = (*attribs)[ii]; | 49 (*var_map)[(*attribs)[ii].mappedName] = (*attribs)[ii]; |
50 } | 50 } |
51 } | 51 } |
52 | 52 |
53 void GetUniforms(ShHandle compiler, UniformMap* var_map) { | 53 void GetUniforms(ShHandle compiler, UniformMap* var_map) { |
54 if (!var_map) | 54 if (!var_map) |
55 return; | 55 return; |
56 var_map->clear(); | 56 var_map->clear(); |
57 const std::vector<sh::Uniform>* uniforms = ShGetUniforms(compiler); | 57 const std::vector<sh::Uniform>* uniforms = sh::GetUniforms(compiler); |
58 if (uniforms) { | 58 if (uniforms) { |
59 for (size_t ii = 0; ii < uniforms->size(); ++ii) | 59 for (size_t ii = 0; ii < uniforms->size(); ++ii) |
60 (*var_map)[(*uniforms)[ii].mappedName] = (*uniforms)[ii]; | 60 (*var_map)[(*uniforms)[ii].mappedName] = (*uniforms)[ii]; |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 void GetVaryings(ShHandle compiler, VaryingMap* var_map) { | 64 void GetVaryings(ShHandle compiler, VaryingMap* var_map) { |
65 if (!var_map) | 65 if (!var_map) |
66 return; | 66 return; |
67 var_map->clear(); | 67 var_map->clear(); |
68 const std::vector<sh::Varying>* varyings = ShGetVaryings(compiler); | 68 const std::vector<sh::Varying>* varyings = sh::GetVaryings(compiler); |
69 if (varyings) { | 69 if (varyings) { |
70 for (size_t ii = 0; ii < varyings->size(); ++ii) | 70 for (size_t ii = 0; ii < varyings->size(); ++ii) |
71 (*var_map)[(*varyings)[ii].mappedName] = (*varyings)[ii]; | 71 (*var_map)[(*varyings)[ii].mappedName] = (*varyings)[ii]; |
72 } | 72 } |
73 } | 73 } |
74 void GetOutputVariables(ShHandle compiler, OutputVariableList* var_list) { | 74 void GetOutputVariables(ShHandle compiler, OutputVariableList* var_list) { |
75 if (!var_list) | 75 if (!var_list) |
76 return; | 76 return; |
77 *var_list = *ShGetOutputVariables(compiler); | 77 *var_list = *sh::GetOutputVariables(compiler); |
78 } | 78 } |
79 | 79 |
80 void GetInterfaceBlocks(ShHandle compiler, InterfaceBlockMap* var_map) { | 80 void GetInterfaceBlocks(ShHandle compiler, InterfaceBlockMap* var_map) { |
81 if (!var_map) | 81 if (!var_map) |
82 return; | 82 return; |
83 var_map->clear(); | 83 var_map->clear(); |
84 const std::vector<sh::InterfaceBlock>* interface_blocks = | 84 const std::vector<sh::InterfaceBlock>* interface_blocks = |
85 ShGetInterfaceBlocks(compiler); | 85 sh::GetInterfaceBlocks(compiler); |
86 if (interface_blocks) { | 86 if (interface_blocks) { |
87 for (const auto& block : *interface_blocks) { | 87 for (const auto& block : *interface_blocks) { |
88 (*var_map)[block.mappedName] = block; | 88 (*var_map)[block.mappedName] = block; |
89 } | 89 } |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 void GetNameHashingInfo(ShHandle compiler, NameMap* name_map) { | 93 void GetNameHashingInfo(ShHandle compiler, NameMap* name_map) { |
94 if (!name_map) | 94 if (!name_map) |
95 return; | 95 return; |
96 name_map->clear(); | 96 name_map->clear(); |
97 | 97 |
98 typedef std::map<std::string, std::string> NameMapANGLE; | 98 typedef std::map<std::string, std::string> NameMapANGLE; |
99 const NameMapANGLE* angle_map = ShGetNameHashingMap(compiler); | 99 const NameMapANGLE* angle_map = sh::GetNameHashingMap(compiler); |
100 DCHECK(angle_map); | 100 DCHECK(angle_map); |
101 | 101 |
102 for (NameMapANGLE::const_iterator iter = angle_map->begin(); | 102 for (NameMapANGLE::const_iterator iter = angle_map->begin(); |
103 iter != angle_map->end(); ++iter) { | 103 iter != angle_map->end(); ++iter) { |
104 // Note that in ANGLE, the map is (original_name, hash); | 104 // Note that in ANGLE, the map is (original_name, hash); |
105 // here, we want (hash, original_name). | 105 // here, we want (hash, original_name). |
106 (*name_map)[iter->second] = iter->first; | 106 (*name_map)[iter->second] = iter->first; |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 DCHECK(shader_type == GL_FRAGMENT_SHADER || shader_type == GL_VERTEX_SHADER); | 175 DCHECK(shader_type == GL_FRAGMENT_SHADER || shader_type == GL_VERTEX_SHADER); |
176 DCHECK(shader_spec == SH_GLES2_SPEC || shader_spec == SH_WEBGL_SPEC || | 176 DCHECK(shader_spec == SH_GLES2_SPEC || shader_spec == SH_WEBGL_SPEC || |
177 shader_spec == SH_GLES3_SPEC || shader_spec == SH_WEBGL2_SPEC); | 177 shader_spec == SH_GLES3_SPEC || shader_spec == SH_WEBGL2_SPEC); |
178 DCHECK(resources != NULL); | 178 DCHECK(resources != NULL); |
179 | 179 |
180 g_translator_initializer.Get(); | 180 g_translator_initializer.Get(); |
181 | 181 |
182 | 182 |
183 { | 183 { |
184 TRACE_EVENT0("gpu", "ShConstructCompiler"); | 184 TRACE_EVENT0("gpu", "ShConstructCompiler"); |
185 compiler_ = ShConstructCompiler(shader_type, shader_spec, | 185 compiler_ = sh::ConstructCompiler(shader_type, shader_spec, |
186 shader_output_language, resources); | 186 shader_output_language, resources); |
187 } | 187 } |
188 | 188 |
189 compile_options_ = | 189 compile_options_ = |
190 SH_OBJECT_CODE | SH_VARIABLES | SH_ENFORCE_PACKING_RESTRICTIONS | | 190 SH_OBJECT_CODE | SH_VARIABLES | SH_ENFORCE_PACKING_RESTRICTIONS | |
191 SH_LIMIT_EXPRESSION_COMPLEXITY | SH_LIMIT_CALL_STACK_DEPTH | | 191 SH_LIMIT_EXPRESSION_COMPLEXITY | SH_LIMIT_CALL_STACK_DEPTH | |
192 SH_CLAMP_INDIRECT_ARRAY_BOUNDS; | 192 SH_CLAMP_INDIRECT_ARRAY_BOUNDS; |
193 if (gl_shader_interm_output) | 193 if (gl_shader_interm_output) |
194 compile_options_ |= SH_INTERMEDIATE_TREE; | 194 compile_options_ |= SH_INTERMEDIATE_TREE; |
195 compile_options_ |= driver_bug_workarounds; | 195 compile_options_ |= driver_bug_workarounds; |
196 switch (shader_spec) { | 196 switch (shader_spec) { |
(...skipping 22 matching lines...) Expand all Loading... |
219 InterfaceBlockMap* interface_block_map, | 219 InterfaceBlockMap* interface_block_map, |
220 OutputVariableList* output_variable_list, | 220 OutputVariableList* output_variable_list, |
221 NameMap* name_map) const { | 221 NameMap* name_map) const { |
222 // Make sure this instance is initialized. | 222 // Make sure this instance is initialized. |
223 DCHECK(compiler_ != NULL); | 223 DCHECK(compiler_ != NULL); |
224 | 224 |
225 bool success = false; | 225 bool success = false; |
226 { | 226 { |
227 TRACE_EVENT0("gpu", "ShCompile"); | 227 TRACE_EVENT0("gpu", "ShCompile"); |
228 const char* const shader_strings[] = { shader_source.c_str() }; | 228 const char* const shader_strings[] = { shader_source.c_str() }; |
229 success = ShCompile( | 229 success = sh::Compile(compiler_, shader_strings, 1, GetCompileOptions()); |
230 compiler_, shader_strings, 1, GetCompileOptions()); | |
231 } | 230 } |
232 if (success) { | 231 if (success) { |
233 // Get translated shader. | 232 // Get translated shader. |
234 if (translated_source) { | 233 if (translated_source) { |
235 *translated_source = ShGetObjectCode(compiler_); | 234 *translated_source = sh::GetObjectCode(compiler_); |
236 } | 235 } |
237 // Get shader version. | 236 // Get shader version. |
238 *shader_version = ShGetShaderVersion(compiler_); | 237 *shader_version = sh::GetShaderVersion(compiler_); |
239 // Get info for attribs, uniforms, varyings and output variables. | 238 // Get info for attribs, uniforms, varyings and output variables. |
240 GetAttributes(compiler_, attrib_map); | 239 GetAttributes(compiler_, attrib_map); |
241 GetUniforms(compiler_, uniform_map); | 240 GetUniforms(compiler_, uniform_map); |
242 GetVaryings(compiler_, varying_map); | 241 GetVaryings(compiler_, varying_map); |
243 GetInterfaceBlocks(compiler_, interface_block_map); | 242 GetInterfaceBlocks(compiler_, interface_block_map); |
244 GetOutputVariables(compiler_, output_variable_list); | 243 GetOutputVariables(compiler_, output_variable_list); |
245 // Get info for name hashing. | 244 // Get info for name hashing. |
246 GetNameHashingInfo(compiler_, name_map); | 245 GetNameHashingInfo(compiler_, name_map); |
247 } | 246 } |
248 | 247 |
249 // Get info log. | 248 // Get info log. |
250 if (info_log) { | 249 if (info_log) { |
251 *info_log = ShGetInfoLog(compiler_); | 250 *info_log = sh::GetInfoLog(compiler_); |
252 } | 251 } |
253 | 252 |
254 // We don't need results in the compiler anymore. | 253 // We don't need results in the compiler anymore. |
255 ShClearResults(compiler_); | 254 sh::ClearResults(compiler_); |
256 | 255 |
257 return success; | 256 return success; |
258 } | 257 } |
259 | 258 |
260 std::string ShaderTranslator::GetStringForOptionsThatWouldAffectCompilation() | 259 std::string ShaderTranslator::GetStringForOptionsThatWouldAffectCompilation() |
261 const { | 260 const { |
262 DCHECK(compiler_ != NULL); | 261 DCHECK(compiler_ != NULL); |
263 return std::string(":CompileOptions:" + | 262 return std::string(":CompileOptions:" + |
264 base::Uint64ToString(GetCompileOptions())) + | 263 base::Uint64ToString(GetCompileOptions())) + |
265 ShGetBuiltInResourcesString(compiler_); | 264 sh::GetBuiltInResourcesString(compiler_); |
266 } | 265 } |
267 | 266 |
268 void ShaderTranslator::AddDestructionObserver( | 267 void ShaderTranslator::AddDestructionObserver( |
269 DestructionObserver* observer) { | 268 DestructionObserver* observer) { |
270 destruction_observers_.AddObserver(observer); | 269 destruction_observers_.AddObserver(observer); |
271 } | 270 } |
272 | 271 |
273 void ShaderTranslator::RemoveDestructionObserver( | 272 void ShaderTranslator::RemoveDestructionObserver( |
274 DestructionObserver* observer) { | 273 DestructionObserver* observer) { |
275 destruction_observers_.RemoveObserver(observer); | 274 destruction_observers_.RemoveObserver(observer); |
276 } | 275 } |
277 | 276 |
278 ShaderTranslator::~ShaderTranslator() { | 277 ShaderTranslator::~ShaderTranslator() { |
279 for (auto& observer : destruction_observers_) | 278 for (auto& observer : destruction_observers_) |
280 observer.OnDestruct(this); | 279 observer.OnDestruct(this); |
281 | 280 |
282 if (compiler_ != NULL) | 281 if (compiler_ != NULL) |
283 ShDestruct(compiler_); | 282 sh::Destruct(compiler_); |
284 } | 283 } |
285 | 284 |
286 } // namespace gles2 | 285 } // namespace gles2 |
287 } // namespace gpu | 286 } // namespace gpu |
288 | 287 |
OLD | NEW |