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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 for (size_t ii = 0; ii < varyings->size(); ++ii) | 66 for (size_t ii = 0; ii < varyings->size(); ++ii) |
67 (*var_map)[(*varyings)[ii].mappedName] = (*varyings)[ii]; | 67 (*var_map)[(*varyings)[ii].mappedName] = (*varyings)[ii]; |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 void GetNameHashingInfo(ShHandle compiler, NameMap* name_map) { | 71 void GetNameHashingInfo(ShHandle compiler, NameMap* name_map) { |
72 if (!name_map) | 72 if (!name_map) |
73 return; | 73 return; |
74 name_map->clear(); | 74 name_map->clear(); |
75 | 75 |
76 size_t hashed_names_count = 0; | 76 typedef std::map<std::string, std::string> NameMapANGLE; |
77 ShGetInfo(compiler, SH_HASHED_NAMES_COUNT, &hashed_names_count); | 77 const NameMapANGLE* angle_map = ShGetNameHashingMap(compiler); |
78 if (hashed_names_count == 0) | 78 DCHECK(angle_map); |
79 return; | |
80 | 79 |
81 size_t name_max_len = 0, hashed_name_max_len = 0; | 80 for (NameMapANGLE::const_iterator iter = angle_map->begin(); |
82 ShGetInfo(compiler, SH_NAME_MAX_LENGTH, &name_max_len); | 81 iter != angle_map->end(); ++iter) { |
83 ShGetInfo(compiler, SH_HASHED_NAME_MAX_LENGTH, &hashed_name_max_len); | 82 // Note that in ANGLE, the map is (original_name, hash); |
84 | 83 // here, we want (hash, original_name). |
85 scoped_ptr<char[]> name(new char[name_max_len]); | 84 (*name_map)[iter->second] = iter->first; |
86 scoped_ptr<char[]> hashed_name(new char[hashed_name_max_len]); | |
87 | |
88 for (size_t i = 0; i < hashed_names_count; ++i) { | |
89 ShGetNameHashingEntry(compiler, i, name.get(), hashed_name.get()); | |
90 (*name_map)[hashed_name.get()] = name.get(); | |
91 } | 85 } |
92 } | 86 } |
93 | 87 |
94 } // namespace | 88 } // namespace |
95 | 89 |
96 ShaderTranslator::DestructionObserver::DestructionObserver() { | 90 ShaderTranslator::DestructionObserver::DestructionObserver() { |
97 } | 91 } |
98 | 92 |
99 ShaderTranslator::DestructionObserver::~DestructionObserver() { | 93 ShaderTranslator::DestructionObserver::~DestructionObserver() { |
100 } | 94 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 UniformMap* uniform_map, | 145 UniformMap* uniform_map, |
152 VaryingMap* varying_map, | 146 VaryingMap* varying_map, |
153 NameMap* name_map) const { | 147 NameMap* name_map) const { |
154 // Make sure this instance is initialized. | 148 // Make sure this instance is initialized. |
155 DCHECK(compiler_ != NULL); | 149 DCHECK(compiler_ != NULL); |
156 | 150 |
157 bool success = false; | 151 bool success = false; |
158 { | 152 { |
159 TRACE_EVENT0("gpu", "ShCompile"); | 153 TRACE_EVENT0("gpu", "ShCompile"); |
160 const char* const shader_strings[] = { shader_source.c_str() }; | 154 const char* const shader_strings[] = { shader_source.c_str() }; |
161 success = !!ShCompile( | 155 success = ShCompile( |
162 compiler_, shader_strings, 1, GetCompileOptions()); | 156 compiler_, shader_strings, 1, GetCompileOptions()); |
163 } | 157 } |
164 if (success) { | 158 if (success) { |
| 159 // Get translated shader. |
165 if (translated_source) { | 160 if (translated_source) { |
166 translated_source->clear(); | 161 *translated_source = ShGetObjectCode(compiler_); |
167 // Get translated shader. | |
168 size_t obj_code_len = 0; | |
169 ShGetInfo(compiler_, SH_OBJECT_CODE_LENGTH, &obj_code_len); | |
170 if (obj_code_len > 1) { | |
171 scoped_ptr<char[]> buffer(new char[obj_code_len]); | |
172 ShGetObjectCode(compiler_, buffer.get()); | |
173 *translated_source = std::string(buffer.get(), obj_code_len - 1); | |
174 } | |
175 } | 162 } |
176 // Get info for attribs, uniforms, and varyings. | 163 // Get info for attribs, uniforms, and varyings. |
177 GetAttributes(compiler_, attrib_map); | 164 GetAttributes(compiler_, attrib_map); |
178 GetUniforms(compiler_, uniform_map); | 165 GetUniforms(compiler_, uniform_map); |
179 GetVaryings(compiler_, varying_map); | 166 GetVaryings(compiler_, varying_map); |
180 // Get info for name hashing. | 167 // Get info for name hashing. |
181 GetNameHashingInfo(compiler_, name_map); | 168 GetNameHashingInfo(compiler_, name_map); |
182 } | 169 } |
183 | 170 |
184 // Get info log. | 171 // Get info log. |
185 if (info_log) { | 172 if (info_log) { |
186 info_log->clear(); | 173 *info_log = ShGetInfoLog(compiler_); |
187 size_t info_log_len = 0; | |
188 ShGetInfo(compiler_, SH_INFO_LOG_LENGTH, &info_log_len); | |
189 if (info_log_len > 1) { | |
190 scoped_ptr<char[]> buffer(new char[info_log_len]); | |
191 ShGetInfoLog(compiler_, buffer.get()); | |
192 *info_log = std::string(buffer.get(), info_log_len - 1); | |
193 } | |
194 } | 174 } |
195 | 175 |
196 return success; | 176 return success; |
197 } | 177 } |
198 | 178 |
199 std::string ShaderTranslator::GetStringForOptionsThatWouldAffectCompilation() | 179 std::string ShaderTranslator::GetStringForOptionsThatWouldAffectCompilation() |
200 const { | 180 const { |
201 DCHECK(compiler_ != NULL); | 181 DCHECK(compiler_ != NULL); |
202 | |
203 size_t resource_len = 0; | |
204 ShGetInfo(compiler_, SH_RESOURCES_STRING_LENGTH, &resource_len); | |
205 DCHECK(resource_len > 1); | |
206 scoped_ptr<char[]> resource_str(new char[resource_len]); | |
207 | |
208 ShGetBuiltInResourcesString(compiler_, resource_len, resource_str.get()); | |
209 | |
210 return std::string(":CompileOptions:" + | 182 return std::string(":CompileOptions:" + |
211 base::IntToString(GetCompileOptions())) + | 183 base::IntToString(GetCompileOptions())) + |
212 std::string(resource_str.get()); | 184 ShGetBuiltInResourcesString(compiler_); |
213 } | 185 } |
214 | 186 |
215 void ShaderTranslator::AddDestructionObserver( | 187 void ShaderTranslator::AddDestructionObserver( |
216 DestructionObserver* observer) { | 188 DestructionObserver* observer) { |
217 destruction_observers_.AddObserver(observer); | 189 destruction_observers_.AddObserver(observer); |
218 } | 190 } |
219 | 191 |
220 void ShaderTranslator::RemoveDestructionObserver( | 192 void ShaderTranslator::RemoveDestructionObserver( |
221 DestructionObserver* observer) { | 193 DestructionObserver* observer) { |
222 destruction_observers_.RemoveObserver(observer); | 194 destruction_observers_.RemoveObserver(observer); |
223 } | 195 } |
224 | 196 |
225 ShaderTranslator::~ShaderTranslator() { | 197 ShaderTranslator::~ShaderTranslator() { |
226 FOR_EACH_OBSERVER(DestructionObserver, | 198 FOR_EACH_OBSERVER(DestructionObserver, |
227 destruction_observers_, | 199 destruction_observers_, |
228 OnDestruct(this)); | 200 OnDestruct(this)); |
229 | 201 |
230 if (compiler_ != NULL) | 202 if (compiler_ != NULL) |
231 ShDestruct(compiler_); | 203 ShDestruct(compiler_); |
232 } | 204 } |
233 | 205 |
234 } // namespace gles2 | 206 } // namespace gles2 |
235 } // namespace gpu | 207 } // namespace gpu |
236 | 208 |
OLD | NEW |