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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// 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 "base/logging.h"
#include "base/string_util.h"
namespace gpu {
namespace gles2 {
ShaderManager::ShaderInfo::ShaderInfo(GLuint service_id, GLenum shader_type)
: use_count_(0),
service_id_(service_id),
shader_type_(shader_type),
valid_(false) {
}
ShaderManager::ShaderInfo::~ShaderInfo() {
}
void ShaderManager::ShaderInfo::IncUseCount() {
++use_count_;
}
void ShaderManager::ShaderInfo::DecUseCount() {
--use_count_;
DCHECK_GE(use_count_, 0);
}
void ShaderManager::ShaderInfo::MarkAsDeleted() {
DCHECK_NE(service_id_, 0u);
service_id_ = 0;
}
void ShaderManager::ShaderInfo::SetStatus(
bool valid, const char* log, ShaderTranslatorInterface* translator) {
valid_ = valid;
log_info_.reset(log ? new std::string(log) : NULL);
if (translator && valid) {
attrib_map_ = translator->attrib_map();
uniform_map_ = translator->uniform_map();
} else {
attrib_map_.clear();
uniform_map_.clear();
}
}
const ShaderManager::ShaderInfo::VariableInfo*
ShaderManager::ShaderInfo::GetAttribInfo(
const std::string& name) const {
VariableMap::const_iterator it = attrib_map_.find(name);
return it != attrib_map_.end() ? &it->second : NULL;
}
const ShaderManager::ShaderInfo::VariableInfo*
ShaderManager::ShaderInfo::GetUniformInfo(
const std::string& name) const {
VariableMap::const_iterator it = uniform_map_.find(name);
return it != uniform_map_.end() ? &it->second : NULL;
}
ShaderManager::ShaderManager() {}
ShaderManager::~ShaderManager() {
DCHECK(shader_infos_.empty());
}
void ShaderManager::Destroy(bool have_context) {
while (!shader_infos_.empty()) {
if (have_context) {
ShaderInfo* info = shader_infos_.begin()->second;
if (!info->IsDeleted()) {
glDeleteShader(info->service_id());
info->MarkAsDeleted();
}
}
shader_infos_.erase(shader_infos_.begin());
}
}
ShaderManager::ShaderInfo* ShaderManager::CreateShaderInfo(
GLuint client_id,
GLuint service_id,
GLenum shader_type) {
std::pair<ShaderInfoMap::iterator, bool> result =
shader_infos_.insert(std::make_pair(
client_id, ShaderInfo::Ref(new ShaderInfo(service_id, shader_type))));
DCHECK(result.second);
return result.first->second;
}
ShaderManager::ShaderInfo* ShaderManager::GetShaderInfo(GLuint client_id) {
ShaderInfoMap::iterator it = shader_infos_.find(client_id);
return it != shader_infos_.end() ? it->second : NULL;
}
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 (ShaderInfoMap::const_iterator it = shader_infos_.begin();
it != shader_infos_.end(); ++it) {
if (it->second->service_id() == service_id) {
*client_id = it->first;
return true;
}
}
return false;
}
bool ShaderManager::IsOwned(ShaderManager::ShaderInfo* info) {
for (ShaderInfoMap::iterator it = shader_infos_.begin();
it != shader_infos_.end(); ++it) {
if (it->second.get() == info) {
return true;
}
}
return false;
}
void ShaderManager::RemoveShaderInfoIfUnused(ShaderManager::ShaderInfo* info) {
DCHECK(info);
DCHECK(IsOwned(info));
if (info->IsDeleted() && !info->InUse()) {
for (ShaderInfoMap::iterator it = shader_infos_.begin();
it != shader_infos_.end(); ++it) {
if (it->second.get() == info) {
shader_infos_.erase(it);
return;
}
}
NOTREACHED();
}
}
void ShaderManager::MarkAsDeleted(ShaderManager::ShaderInfo* info) {
DCHECK(info);
DCHECK(IsOwned(info));
info->MarkAsDeleted();
RemoveShaderInfoIfUnused(info);
}
void ShaderManager::UseShader(ShaderManager::ShaderInfo* info) {
DCHECK(info);
DCHECK(IsOwned(info));
info->IncUseCount();
}
void ShaderManager::UnuseShader(ShaderManager::ShaderInfo* info) {
DCHECK(info);
DCHECK(IsOwned(info));
info->DecUseCount();
RemoveShaderInfoIfUnused(info);
}
namespace {
// Strips comments from shader text. This allows non-ASCII characters
// to be used in comments without potentially breaking OpenGL
// implementations not expecting characters outside the GLSL ES set.
class Stripper {
public:
Stripper(const std::string& str)
: parse_state_(kBeginningOfLine)
, source_string_(str)
, length_(str.length())
, position_(0) {
Parse();
}
const std::string& result() {
return result_;
}
private:
bool HasMoreCharacters() {
return position_ < length_;
}
void Parse() {
while (HasMoreCharacters()) {
Process(Current());
// Process() might Advance the position.
if (HasMoreCharacters()) {
Advance();
}
}
}
void Process(char);
bool Peek(char* character) {
DCHECK(character);
if (position_ + 1 >= length_) {
return false;
}
*character = source_string_[position_ + 1];
return true;
}
char Current() {
DCHECK_LT(position_, length_);
return source_string_[position_];
}
void Advance() {
++position_;
}
bool IsNewline(char character) {
// Don't attempt to canonicalize newline related characters.
return (character == '\n' || character == '\r');
}
void Emit(char character) {
result_.push_back(character);
}
enum ParseState {
// Have not seen an ASCII non-whitespace character yet on
// this line. Possible that we might see a preprocessor
// directive.
kBeginningOfLine,
// Have seen at least one ASCII non-whitespace character
// on this line.
kMiddleOfLine,
// Handling a preprocessor directive. Passes through all
// characters up to the end of the line. Disables comment
// processing.
kInPreprocessorDirective,
// Handling a single-line comment. The comment text is
// replaced with a single space.
kInSingleLineComment,
// Handling a multi-line comment. Newlines are passed
// through to preserve line numbers.
kInMultiLineComment
};
ParseState parse_state_;
std::string source_string_;
unsigned length_;
unsigned position_;
std::string result_;
};
void Stripper::Process(char c) {
if (IsNewline(c)) {
// No matter what state we are in, pass through newlines
// so we preserve line numbers.
Emit(c);
if (parse_state_ != kInMultiLineComment)
parse_state_ = kBeginningOfLine;
return;
}
char temp = 0;
switch (parse_state_) {
case kBeginningOfLine:
if (IsAsciiWhitespace(c)) {
Emit(c);
break;
}
if (c == '#') {
parse_state_ = kInPreprocessorDirective;
Emit(c);
break;
}
// Transition to normal state and re-handle character.
parse_state_ = kMiddleOfLine;
Process(c);
break;
case kMiddleOfLine:
if (c == '/' && Peek(&temp)) {
if (temp == '/') {
parse_state_ = kInSingleLineComment;
Emit(' ');
Advance();
break;
}
if (temp == '*') {
parse_state_ = kInMultiLineComment;
// Emit the comment start in case the user has
// an unclosed comment and we want to later
// signal an error.
Emit('/');
Emit('*');
Advance();
break;
}
}
Emit(c);
break;
case kInPreprocessorDirective:
// No matter what the character is, just pass it
// through. Do not Parse comments in this state. This
// might not be the right thing to do long term, but it
// should handle the #error preprocessor directive.
Emit(c);
break;
case kInSingleLineComment:
// The newline code at the top of this function takes care
// of resetting our state when we get out of the
// single-line comment. Swallow all other characters.
break;
case kInMultiLineComment:
if (c == '*' && Peek(&temp) && temp == '/') {
Emit('*');
Emit('/');
parse_state_ = kMiddleOfLine;
Advance();
break;
}
// Swallow all other characters. Unclear whether we may
// want or need to just Emit a space per character to try
// to preserve column numbers for debugging purposes.
break;
}
}
} // anonymous namespace
std::string ShaderManager::StripComments(const std::string source) {
Stripper stripper(source);
return stripper.result();
}
} // namespace gles2
} // namespace gpu
|