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
3 // found in the LICENSE file.
5 #include "gpu/command_buffer/service/shader_manager.h"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
17 // Given a variable name | a[0].b.c[0] |, return |a|.
18 std::string
GetTopVariableName(const std::string
& fullname
) {
19 size_t pos
= fullname
.find_first_of("[.");
20 if (pos
== std::string::npos
)
22 return fullname
.substr(0, pos
);
25 } // namespace anonymous
27 Shader::Shader(GLuint service_id
, GLenum shader_type
)
29 shader_state_(kShaderStateWaiting
),
30 marked_for_deletion_(false),
31 service_id_(service_id
),
32 shader_type_(shader_type
),
40 void Shader::Destroy() {
46 void Shader::RequestCompile(scoped_refptr
<ShaderTranslatorInterface
> translator
,
47 TranslatedShaderSourceType type
) {
48 shader_state_
= kShaderStateCompileRequested
;
49 translator_
= translator
;
51 last_compiled_source_
= source_
;
54 void Shader::DoCompile() {
55 // We require that RequestCompile() must be called before DoCompile(),
56 // so we can return early if the shader state is not what we expect.
57 if (shader_state_
!= kShaderStateCompileRequested
) {
61 // Signify the shader has been compiled, whether or not it is valid
62 // is dependent on the |valid_| member variable.
63 shader_state_
= kShaderStateCompiled
;
66 // Translate GL ES 2.0 shader to Desktop GL shader and pass that to
67 // glShaderSource and then glCompileShader.
68 const char* source_for_driver
= last_compiled_source_
.c_str();
69 ShaderTranslatorInterface
* translator
= translator_
.get();
71 bool success
= translator
->Translate(last_compiled_source_
,
81 source_for_driver
= translated_source_
.c_str();
84 glShaderSource(service_id_
, 1, &source_for_driver
, NULL
);
85 glCompileShader(service_id_
);
86 if (source_type_
== kANGLE
) {
88 glGetShaderiv(service_id_
,
89 GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE
,
91 source_for_driver
= "\0";
92 translated_source_
.resize(max_len
);
95 glGetTranslatedShaderSourceANGLE(
96 service_id_
, translated_source_
.size(),
97 &len
, &translated_source_
.at(0));
98 DCHECK(max_len
== 0 || len
< max_len
);
99 DCHECK(len
== 0 || translated_source_
[len
] == '\0');
100 translated_source_
.resize(len
);
101 source_for_driver
= translated_source_
.c_str();
105 GLint status
= GL_FALSE
;
106 glGetShaderiv(service_id_
, GL_COMPILE_STATUS
, &status
);
107 if (status
== GL_TRUE
) {
112 // We cannot reach here if we are using the shader translator.
113 // All invalid shaders must be rejected by the translator.
114 // All translated shaders must compile.
115 std::string translator_log
= log_info_
;
118 glGetShaderiv(service_id_
, GL_INFO_LOG_LENGTH
, &max_len
);
119 log_info_
.resize(max_len
);
122 glGetShaderInfoLog(service_id_
, log_info_
.size(), &len
, &log_info_
.at(0));
123 DCHECK(max_len
== 0 || len
< max_len
);
124 DCHECK(len
== 0 || log_info_
[len
] == '\0');
125 log_info_
.resize(len
);
128 LOG_IF(ERROR
, translator
)
129 << "Shader translator allowed/produced an invalid shader "
130 << "unless the driver is buggy:"
131 << "\n--Log from shader translator--\n" << translator_log
132 << "\n--original-shader--\n" << last_compiled_source_
133 << "\n--translated-shader--\n" << source_for_driver
134 << "\n--info-log--\n" << log_info_
;
138 void Shader::IncUseCount() {
142 void Shader::DecUseCount() {
144 DCHECK_GE(use_count_
, 0);
145 if (service_id_
&& use_count_
== 0 && marked_for_deletion_
) {
150 void Shader::MarkForDeletion() {
151 DCHECK(!marked_for_deletion_
);
152 DCHECK_NE(service_id_
, 0u);
154 marked_for_deletion_
= true;
155 if (use_count_
== 0) {
160 void Shader::DeleteServiceID() {
161 DCHECK_NE(service_id_
, 0u);
162 glDeleteShader(service_id_
);
166 const sh::Attribute
* Shader::GetAttribInfo(const std::string
& name
) const {
167 // Vertex attributes can't be arrays or structs (GLSL ES 3.00.4, section
168 // 4.3.4, "Input Variables"), so |name| is the top level name used as
169 // the AttributeMap key.
170 AttributeMap::const_iterator it
= attrib_map_
.find(name
);
171 return it
!= attrib_map_
.end() ? &it
->second
: NULL
;
174 const std::string
* Shader::GetAttribMappedName(
175 const std::string
& original_name
) const {
176 for (AttributeMap::const_iterator it
= attrib_map_
.begin();
177 it
!= attrib_map_
.end(); ++it
) {
178 if (it
->second
.name
== original_name
)
184 const std::string
* Shader::GetOriginalNameFromHashedName(
185 const std::string
& hashed_name
) const {
186 NameMap::const_iterator it
= name_map_
.find(hashed_name
);
187 if (it
!= name_map_
.end())
188 return &(it
->second
);
192 const sh::Uniform
* Shader::GetUniformInfo(const std::string
& name
) const {
193 UniformMap::const_iterator it
= uniform_map_
.find(GetTopVariableName(name
));
194 return it
!= uniform_map_
.end() ? &it
->second
: NULL
;
197 const sh::Varying
* Shader::GetVaryingInfo(const std::string
& name
) const {
198 VaryingMap::const_iterator it
= varying_map_
.find(GetTopVariableName(name
));
199 return it
!= varying_map_
.end() ? &it
->second
: NULL
;
202 ShaderManager::ShaderManager() {}
204 ShaderManager::~ShaderManager() {
205 DCHECK(shaders_
.empty());
208 void ShaderManager::Destroy(bool have_context
) {
209 while (!shaders_
.empty()) {
211 Shader
* shader
= shaders_
.begin()->second
.get();
214 shaders_
.erase(shaders_
.begin());
218 Shader
* ShaderManager::CreateShader(
221 GLenum shader_type
) {
222 std::pair
<ShaderMap::iterator
, bool> result
=
223 shaders_
.insert(std::make_pair(
224 client_id
, scoped_refptr
<Shader
>(
225 new Shader(service_id
, shader_type
))));
226 DCHECK(result
.second
);
227 return result
.first
->second
.get();
230 Shader
* ShaderManager::GetShader(GLuint client_id
) {
231 ShaderMap::iterator it
= shaders_
.find(client_id
);
232 return it
!= shaders_
.end() ? it
->second
.get() : NULL
;
235 bool ShaderManager::GetClientId(GLuint service_id
, GLuint
* client_id
) const {
236 // This doesn't need to be fast. It's only used during slow queries.
237 for (ShaderMap::const_iterator it
= shaders_
.begin();
238 it
!= shaders_
.end(); ++it
) {
239 if (it
->second
->service_id() == service_id
) {
240 *client_id
= it
->first
;
247 bool ShaderManager::IsOwned(Shader
* shader
) {
248 for (ShaderMap::iterator it
= shaders_
.begin();
249 it
!= shaders_
.end(); ++it
) {
250 if (it
->second
.get() == shader
) {
257 void ShaderManager::RemoveShader(Shader
* shader
) {
259 DCHECK(IsOwned(shader
));
260 if (shader
->IsDeleted() && !shader
->InUse()) {
261 for (ShaderMap::iterator it
= shaders_
.begin();
262 it
!= shaders_
.end(); ++it
) {
263 if (it
->second
.get() == shader
) {
272 void ShaderManager::Delete(Shader
* shader
) {
274 DCHECK(IsOwned(shader
));
275 shader
->MarkForDeletion();
276 RemoveShader(shader
);
279 void ShaderManager::UseShader(Shader
* shader
) {
281 DCHECK(IsOwned(shader
));
282 shader
->IncUseCount();
285 void ShaderManager::UnuseShader(Shader
* shader
) {
287 DCHECK(IsOwned(shader
));
288 shader
->DecUseCount();
289 RemoveShader(shader
);