1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
gpu / command_buffer / service / shader_manager.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/shader_manager.h"
#include <stddef.h>
#include <utility>
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "ui/gl/progress_reporter.h"
namespace gpu {
namespace gles2 {
namespace {
// Given a variable name | a[0].b.c[0] |, return |a|.
std::string GetTopVariableName(const std::string& fullname) {
size_t pos = fullname.find_first_of("[.");
if (pos == std::string::npos)
return fullname;
return fullname.substr(0, pos);
}
} // namespace anonymous
void CompileShaderWithLog(GLuint shader, const char* shader_source) {
glShaderSource(shader, 1, &shader_source, 0);
glCompileShader(shader);
#if DCHECK_IS_ON()
GLint compile_status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if (GL_TRUE != compile_status) {
GLint info_log_length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector<GLchar> info_log(info_log_length);
glGetShaderInfoLog(shader, info_log_length, nullptr, &info_log[0]);
std::string log(&info_log[0], info_log_length - 1);
DLOG(ERROR) << "Error compiling shader: " << log;
DLOG(ERROR) << "Shader compilation failure.";
}
#endif
}
Shader::Shader(GLuint service_id, GLenum shader_type)
: use_count_(0),
shader_state_(kShaderStateWaiting),
marked_for_deletion_(false),
service_id_(service_id),
shader_type_(shader_type),
shader_version_(kUndefinedShaderVersion),
source_type_(kANGLE),
valid_(false) {
}
Shader::~Shader() = default;
void Shader::Destroy() {
DeleteServiceID();
}
void Shader::RequestCompile(scoped_refptr<ShaderTranslatorInterface> translator,
TranslatedShaderSourceType type) {
shader_state_ = kShaderStateCompileRequested;
translator_ = translator;
if (translator_) {
options_affecting_compilation_ =
translator_->GetStringForOptionsThatWouldAffectCompilation();
}
source_type_ = type;
last_compiled_source_ = source_;
}
void Shader::DoCompile() {
if (!CanCompile()) {
return;
}
// Signify the shader has been compiled, whether or not it is valid
// is dependent on the |valid_| member variable.
shader_state_ = kShaderStateCompiled;
valid_ = false;
// Translate GL ES 2.0 shader to Desktop GL shader and pass that to
// glShaderSource and then glCompileShader.
const char* source_for_driver = last_compiled_source_.c_str();
ShaderTranslatorInterface* translator = translator_.get();
if (translator) {
bool success = translator->Translate(
last_compiled_source_, &log_info_, &translated_source_,
&shader_version_, &attrib_map_, &uniform_map_, &varying_map_,
&interface_block_map_, &output_variable_list_);
if (!success) {
return;
}
source_for_driver = translated_source_.c_str();
}
glShaderSource(service_id_, 1, &source_for_driver, nullptr);
glCompileShader(service_id_);
if (source_type_ == kANGLE) {
RefreshTranslatedShaderSource();
source_for_driver = translated_source_.c_str();
}
GLint status = GL_FALSE;
glGetShaderiv(service_id_, GL_COMPILE_STATUS, &status);
if (status == GL_TRUE) {
valid_ = true;
} else {
valid_ = false;
// We cannot reach here if we are using the shader translator.
// All invalid shaders must be rejected by the translator.
// All translated shaders must compile.
std::string translator_log = log_info_;
GLint max_len = 0;
glGetShaderiv(service_id_, GL_INFO_LOG_LENGTH, &max_len);
log_info_.resize(max_len);
if (max_len) {
GLint len = 0;
glGetShaderInfoLog(service_id_, log_info_.size(), &len, &log_info_.at(0));
DCHECK(max_len == 0 || len < max_len);
DCHECK(len == 0 || log_info_[len] == '\0');
log_info_.resize(len);
}
LOG_IF(ERROR, translator)
<< "Shader translator allowed/produced an invalid shader "
<< "unless the driver is buggy:"
<< "\n--Log from shader translator--\n" << translator_log
<< "\n--original-shader--\n" << last_compiled_source_
<< "\n--translated-shader--\n" << source_for_driver
<< "\n--info-log--\n" << log_info_;
}
// Translator is no longer required and can be released
translator_ = nullptr;
}
void Shader::RefreshTranslatedShaderSource() {
if (source_type_ == kANGLE) {
GLint max_len = 0;
glGetShaderiv(service_id_, GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE,
&max_len);
translated_source_.resize(max_len);
if (max_len) {
GLint len = 0;
glGetTranslatedShaderSourceANGLE(service_id_, translated_source_.size(),
&len, &translated_source_.at(0));
DCHECK(max_len == 0 || len < max_len);
DCHECK(len == 0 || translated_source_[len] == '\0');
translated_source_.resize(len);
}
}
}
void Shader::MarkForDeletion() {
DCHECK(!marked_for_deletion_);
DCHECK_NE(service_id_, 0u);
marked_for_deletion_ = true;
}
void Shader::DeleteServiceID() {
if (service_id_) {
glDeleteShader(service_id_);
service_id_ = 0;
}
}
const sh::Attribute* Shader::GetAttribInfo(const std::string& name) const {
// Vertex attributes can't be arrays or structs (GLSL ES 3.00.4, section
// 4.3.4, "Input Variables"), so |name| is the top level name used as
// the AttributeMap key.
AttributeMap::const_iterator it = attrib_map_.find(name);
return it != attrib_map_.end() ? &it->second : nullptr;
}
const std::string* Shader::GetAttribMappedName(
const std::string& original_name) const {
for (const auto& key_value : attrib_map_) {
if (key_value.second.name == original_name)
return &(key_value.first);
}
return nullptr;
}
const std::string* Shader::GetUniformMappedName(
const std::string& original_name) const {
for (const auto& key_value : uniform_map_) {
if (key_value.second.name == original_name)
return &(key_value.first);
}
return nullptr;
}
const std::string* Shader::GetVaryingMappedName(
const std::string& original_name) const {
for (VaryingMap::const_iterator it = varying_map_.begin();
it != varying_map_.end(); ++it) {
if (it->second.name == original_name)
return &(it->first);
}
return nullptr;
}
const std::string* Shader::GetInterfaceBlockMappedName(
const std::string& original_name) const {
for (const auto& key_value : interface_block_map_) {
if (key_value.second.name == original_name)
return &(key_value.first);
}
return nullptr;
}
const std::string* Shader::GetOutputVariableMappedName(
const std::string& original_name) const {
for (const auto& value : output_variable_list_) {
if (value.name == original_name)
return &value.mappedName;
}
return nullptr;
}
const std::string* Shader::GetOriginalNameFromHashedName(
const std::string& hashed_name) const {
if (const auto* info = GetAttribInfo(hashed_name)) {
return &info->name;
}
if (const auto* info = GetUniformInfo(hashed_name)) {
return &info->name;
}
if (const auto* info = GetVaryingInfo(hashed_name)) {
return &info->name;
}
if (const auto* info = GetInterfaceBlockInfo(hashed_name)) {
return &info->name;
}
if (const auto* info = GetOutputVariableInfo(hashed_name)) {
return &info->name;
}
return nullptr;
}
const sh::Uniform* Shader::GetUniformInfo(const std::string& name) const {
UniformMap::const_iterator it = uniform_map_.find(GetTopVariableName(name));
return it != uniform_map_.end() ? &it->second : nullptr;
}
const sh::Varying* Shader::GetVaryingInfo(const std::string& name) const {
VaryingMap::const_iterator it = varying_map_.find(GetTopVariableName(name));
return it != varying_map_.end() ? &it->second : nullptr;
}
const sh::InterfaceBlock* Shader::GetInterfaceBlockInfo(
const std::string& name) const {
InterfaceBlockMap::const_iterator it =
interface_block_map_.find(GetTopVariableName(name));
return it != interface_block_map_.end() ? &it->second : nullptr;
}
const sh::OutputVariable* Shader::GetOutputVariableInfo(
const std::string& name) const {
std::string mapped_name = GetTopVariableName(name);
// Number of output variables is expected to be so low that
// a linear search of a list should be faster than using a map.
for (const auto& value : output_variable_list_) {
if (value.mappedName == mapped_name)
return &value;
}
return nullptr;
}
ShaderManager::ShaderManager(gl::ProgressReporter* progress_reporter)
: progress_reporter_(progress_reporter) {}
ShaderManager::~ShaderManager() {
DCHECK(shaders_.empty());
}
void ShaderManager::Destroy(bool have_context) {
while (!shaders_.empty()) {
if (have_context) {
Shader* shader = shaders_.begin()->second.get();
shader->Destroy();
}
shaders_.erase(shaders_.begin());
if (progress_reporter_)
progress_reporter_->ReportProgress();
}
}
Shader* ShaderManager::CreateShader(
GLuint client_id,
GLuint service_id,
GLenum shader_type) {
std::pair<ShaderMap::iterator, bool> result =
shaders_.insert(std::make_pair(
client_id, scoped_refptr<Shader>(
new Shader(service_id, shader_type))));
DCHECK(result.second);
return result.first->second.get();
}
Shader* ShaderManager::GetShader(GLuint client_id) {
ShaderMap::iterator it = shaders_.find(client_id);
return it != shaders_.end() ? it->second.get() : nullptr;
}
bool ShaderManager::GetClientId(GLuint service_id, GLuint* client_id) const {
// This doesn't need to be fast. It's only used during slow queries.
for (ShaderMap::const_iterator it = shaders_.begin();
it != shaders_.end(); ++it) {
if (it->second->service_id() == service_id) {
*client_id = it->first;
return true;
}
}
return false;
}
bool ShaderManager::IsOwned(Shader* shader) {
for (ShaderMap::iterator it = shaders_.begin();
it != shaders_.end(); ++it) {
if (it->second.get() == shader) {
return true;
}
}
return false;
}
void ShaderManager::RemoveShaderIfUnused(Shader* shader) {
DCHECK(shader);
DCHECK(IsOwned(shader));
if (shader->IsDeleted() && !shader->InUse()) {
shader->DeleteServiceID();
for (ShaderMap::iterator it = shaders_.begin();
it != shaders_.end(); ++it) {
if (it->second.get() == shader) {
shaders_.erase(it);
return;
}
}
NOTREACHED();
}
}
void ShaderManager::Delete(Shader* shader) {
DCHECK(shader);
DCHECK(IsOwned(shader));
shader->MarkForDeletion();
RemoveShaderIfUnused(shader);
}
void ShaderManager::UseShader(Shader* shader) {
DCHECK(shader);
DCHECK(IsOwned(shader));
shader->IncUseCount();
}
void ShaderManager::UnuseShader(Shader* shader) {
DCHECK(shader);
DCHECK(IsOwned(shader));
shader->DecUseCount();
RemoveShaderIfUnused(shader);
}
} // namespace gles2
} // namespace gpu