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 void RequestCompile(scoped_refptr
<ShaderTranslatorInterface
> translator
,
39 TranslatedShaderSourceType type
);
43 ShaderState
shader_state() const {
47 GLuint
service_id() const {
48 return marked_for_deletion_
? 0 : service_id_
;
51 GLenum
shader_type() const {
55 const std::string
& source() const {
59 void set_source(const std::string
& source
) {
63 const std::string
& translated_source() const {
64 return translated_source_
;
67 std::string
last_compiled_source() const {
68 return last_compiled_source_
;
71 std::string
last_compiled_signature() const {
72 if (translator_
.get()) {
73 return last_compiled_source_
+
74 translator_
->GetStringForOptionsThatWouldAffectCompilation();
76 return last_compiled_source_
;
79 const sh::Attribute
* GetAttribInfo(const std::string
& name
) const;
80 const sh::Uniform
* GetUniformInfo(const std::string
& name
) const;
81 const sh::Varying
* GetVaryingInfo(const std::string
& name
) const;
83 // If the original_name is not found, return NULL.
84 const std::string
* GetAttribMappedName(
85 const std::string
& original_name
) const;
87 // If the hashed_name is not found, return NULL.
88 const std::string
* GetOriginalNameFromHashedName(
89 const std::string
& hashed_name
) const;
91 const std::string
& log_info() const {
96 return shader_state_
== kShaderStateCompiled
&& valid_
;
99 bool IsDeleted() const {
100 return marked_for_deletion_
;
104 DCHECK_GE(use_count_
, 0);
105 return use_count_
!= 0;
108 // Used by program cache.
109 const AttributeMap
& attrib_map() const {
113 // Used by program cache.
114 const UniformMap
& uniform_map() const {
118 // Used by program cache.
119 const VaryingMap
& varying_map() const {
123 // Used by program cache.
124 void set_attrib_map(const AttributeMap
& attrib_map
) {
125 // copied because cache might be cleared
126 attrib_map_
= AttributeMap(attrib_map
);
129 // Used by program cache.
130 void set_uniform_map(const UniformMap
& uniform_map
) {
131 // copied because cache might be cleared
132 uniform_map_
= UniformMap(uniform_map
);
135 // Used by program cache.
136 void set_varying_map(const VaryingMap
& varying_map
) {
137 // copied because cache might be cleared
138 varying_map_
= VaryingMap(varying_map
);
142 friend class base::RefCounted
<Shader
>;
143 friend class ShaderManager
;
145 Shader(GLuint service_id
, GLenum shader_type
);
148 // Must be called only if we currently own the context. Forces the deletion
149 // of the underlying shader service id.
154 void MarkForDeletion();
155 void DeleteServiceID();
159 // The current state of the shader.
160 ShaderState shader_state_
;
162 // The shader has been marked for deletion.
163 bool marked_for_deletion_
;
165 // The shader this Shader is tracking.
168 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
171 // Translated source type when shader was last requested to be compiled.
172 TranslatedShaderSourceType source_type_
;
174 // Translator to use, set when shader was last requested to be compiled.
175 scoped_refptr
<ShaderTranslatorInterface
> translator_
;
177 // True if compilation succeeded.
180 // The shader source as passed to glShaderSource.
183 // The source the last compile used.
184 std::string last_compiled_source_
;
186 // The translated shader source.
187 std::string translated_source_
;
189 // The shader translation log.
190 std::string log_info_
;
192 // The type info when the shader was last compiled.
193 AttributeMap attrib_map_
;
194 UniformMap uniform_map_
;
195 VaryingMap varying_map_
;
197 // The name hashing info when the shader was last compiled.
201 // Tracks the Shaders.
203 // NOTE: To support shared resources an instance of this class will
204 // need to be shared by multiple GLES2Decoders.
205 class GPU_EXPORT ShaderManager
{
210 // Must call before destruction.
211 void Destroy(bool have_context
);
213 // Creates a shader for the given shader ID.
214 Shader
* CreateShader(
219 // Gets an existing shader info for the given shader ID. Returns NULL if none
221 Shader
* GetShader(GLuint client_id
);
223 // Gets a client id for a given service id.
224 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
226 void Delete(Shader
* shader
);
228 // Mark a shader as used
229 void UseShader(Shader
* shader
);
231 // Unmark a shader as used. If it has been deleted and is not used
232 // then we free the shader.
233 void UnuseShader(Shader
* shader
);
235 // Check if a Shader is owned by this ShaderManager.
236 bool IsOwned(Shader
* shader
);
241 // Info for each shader by service side shader Id.
242 typedef base::hash_map
<GLuint
, scoped_refptr
<Shader
> > ShaderMap
;
245 void RemoveShader(Shader
* shader
);
247 DISALLOW_COPY_AND_ASSIGN(ShaderManager
);
253 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_