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 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_
9 #include "base/basictypes.h"
10 #include "base/containers/hash_tables.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "gpu/command_buffer/service/gl_utils.h"
15 #include "gpu/command_buffer/service/shader_translator.h"
16 #include "gpu/gpu_export.h"
21 // This is used to keep the source code for a shader. This is because in order
22 // to emluate GLES2 the shaders will have to be re-written before passed to
23 // the underlying OpenGL. But, when the user calls glGetShaderSource they
24 // should get the source they passed in, not the re-written source.
25 class GPU_EXPORT Shader
: public base::RefCounted
<Shader
> {
27 enum TranslatedShaderSourceType
{
34 kShaderStateCompileRequested
,
35 kShaderStateCompiled
, // Signifies compile happened, not valid compile.
38 static const int kUndefinedShaderVersion
= -1;
40 void RequestCompile(scoped_refptr
<ShaderTranslatorInterface
> translator
,
41 TranslatedShaderSourceType type
);
45 ShaderState
shader_state() const {
49 GLuint
service_id() const {
50 return marked_for_deletion_
? 0 : service_id_
;
53 GLenum
shader_type() const {
57 int shader_version() const {
58 return shader_version_
;
61 const std::string
& source() const {
65 void set_source(const std::string
& source
) {
69 const std::string
& translated_source() const {
70 return translated_source_
;
73 std::string
last_compiled_source() const {
74 return last_compiled_source_
;
77 std::string
last_compiled_signature() const {
78 if (translator_
.get()) {
79 return last_compiled_source_
+
80 translator_
->GetStringForOptionsThatWouldAffectCompilation();
82 return last_compiled_source_
;
85 const sh::Attribute
* GetAttribInfo(const std::string
& name
) const;
86 const sh::Uniform
* GetUniformInfo(const std::string
& name
) const;
87 const sh::Varying
* GetVaryingInfo(const std::string
& name
) const;
89 // If the original_name is not found, return NULL.
90 const std::string
* GetAttribMappedName(
91 const std::string
& original_name
) const;
93 // If the hashed_name is not found, return NULL.
94 const std::string
* GetOriginalNameFromHashedName(
95 const std::string
& hashed_name
) const;
97 const std::string
& log_info() const {
102 return shader_state_
== kShaderStateCompiled
&& valid_
;
105 bool IsDeleted() const {
106 return marked_for_deletion_
;
110 DCHECK_GE(use_count_
, 0);
111 return use_count_
!= 0;
114 // Used by program cache.
115 const AttributeMap
& attrib_map() const {
119 // Used by program cache.
120 const UniformMap
& uniform_map() const {
124 // Used by program cache.
125 const VaryingMap
& varying_map() const {
129 // Used by program cache.
130 void set_attrib_map(const AttributeMap
& attrib_map
) {
131 // copied because cache might be cleared
132 attrib_map_
= AttributeMap(attrib_map
);
135 // Used by program cache.
136 void set_uniform_map(const UniformMap
& uniform_map
) {
137 // copied because cache might be cleared
138 uniform_map_
= UniformMap(uniform_map
);
141 // Used by program cache.
142 void set_varying_map(const VaryingMap
& varying_map
) {
143 // copied because cache might be cleared
144 varying_map_
= VaryingMap(varying_map
);
148 friend class base::RefCounted
<Shader
>;
149 friend class ShaderManager
;
151 Shader(GLuint service_id
, GLenum shader_type
);
154 // Must be called only if we currently own the context. Forces the deletion
155 // of the underlying shader service id.
160 void MarkForDeletion();
161 void DeleteServiceID();
165 // The current state of the shader.
166 ShaderState shader_state_
;
168 // The shader has been marked for deletion.
169 bool marked_for_deletion_
;
171 // The shader this Shader is tracking.
174 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
177 // Version of the shader. Can be kUndefinedShaderVersion or version returned
181 // Translated source type when shader was last requested to be compiled.
182 TranslatedShaderSourceType source_type_
;
184 // Translator to use, set when shader was last requested to be compiled.
185 scoped_refptr
<ShaderTranslatorInterface
> translator_
;
187 // True if compilation succeeded.
190 // The shader source as passed to glShaderSource.
193 // The source the last compile used.
194 std::string last_compiled_source_
;
196 // The translated shader source.
197 std::string translated_source_
;
199 // The shader translation log.
200 std::string log_info_
;
202 // The type info when the shader was last compiled.
203 AttributeMap attrib_map_
;
204 UniformMap uniform_map_
;
205 VaryingMap varying_map_
;
207 // The name hashing info when the shader was last compiled.
211 // Tracks the Shaders.
213 // NOTE: To support shared resources an instance of this class will
214 // need to be shared by multiple GLES2Decoders.
215 class GPU_EXPORT ShaderManager
{
220 // Must call before destruction.
221 void Destroy(bool have_context
);
223 // Creates a shader for the given shader ID.
224 Shader
* CreateShader(
229 // Gets an existing shader info for the given shader ID. Returns NULL if none
231 Shader
* GetShader(GLuint client_id
);
233 // Gets a client id for a given service id.
234 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
236 void Delete(Shader
* shader
);
238 // Mark a shader as used
239 void UseShader(Shader
* shader
);
241 // Unmark a shader as used. If it has been deleted and is not used
242 // then we free the shader.
243 void UnuseShader(Shader
* shader
);
245 // Check if a Shader is owned by this ShaderManager.
246 bool IsOwned(Shader
* shader
);
251 // Info for each shader by service side shader Id.
252 typedef base::hash_map
<GLuint
, scoped_refptr
<Shader
> > ShaderMap
;
255 void RemoveShader(Shader
* shader
);
257 DISALLOW_COPY_AND_ASSIGN(ShaderManager
);
263 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_