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 typedef ShaderTranslator::VariableInfo VariableInfo
;
29 void UpdateSource(const char* source
) {
30 source_
.reset(source
? new std::string(source
) : NULL
);
33 void UpdateTranslatedSource(const char* translated_source
) {
34 translated_source_
.reset(
35 translated_source
? new std::string(translated_source
) : NULL
);
38 GLuint
service_id() const {
42 GLenum
shader_type() const {
46 const std::string
* source() const {
50 const std::string
* translated_source() const {
51 return translated_source_
.get();
54 const std::string
* signature_source() const {
55 return signature_source_
.get();
59 bool valid
, const char* log
,
60 ShaderTranslatorInterface
* translator
);
62 const VariableInfo
* GetAttribInfo(const std::string
& name
) const;
63 const VariableInfo
* GetUniformInfo(const std::string
& name
) const;
65 // If the original_name is not found, return NULL.
66 const std::string
* GetAttribMappedName(
67 const std::string
& original_name
) const;
69 // If the hashed_name is not found, return NULL.
70 const std::string
* GetOriginalNameFromHashedName(
71 const std::string
& hashed_name
) const;
73 const std::string
* log_info() const {
74 return log_info_
.get();
77 bool IsValid() const {
81 bool IsDeleted() const {
82 return service_id_
== 0;
86 DCHECK_GE(use_count_
, 0);
87 return use_count_
!= 0;
90 // Used by program cache.
91 const ShaderTranslator::VariableMap
& attrib_map() const {
95 // Used by program cache.
96 const ShaderTranslator::VariableMap
& uniform_map() const {
100 // Used by program cache.
101 const ShaderTranslator::VariableMap
& varying_map() const {
105 // Used by program cache.
106 void set_attrib_map(const ShaderTranslator::VariableMap
& attrib_map
) {
107 // copied because cache might be cleared
108 attrib_map_
= ShaderTranslator::VariableMap(attrib_map
);
111 // Used by program cache.
112 void set_uniform_map(const ShaderTranslator::VariableMap
& uniform_map
) {
113 // copied because cache might be cleared
114 uniform_map_
= ShaderTranslator::VariableMap(uniform_map
);
117 // Used by program cache.
118 void set_varying_map(const ShaderTranslator::VariableMap
& varying_map
) {
119 // copied because cache might be cleared
120 varying_map_
= ShaderTranslator::VariableMap(varying_map
);
124 typedef ShaderTranslator::VariableMap VariableMap
;
125 typedef ShaderTranslator::NameMap NameMap
;
127 friend class base::RefCounted
<Shader
>;
128 friend class ShaderManager
;
130 Shader(GLuint service_id
, GLenum shader_type
);
135 void MarkAsDeleted();
139 // The shader this Shader is tracking.
141 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
144 // True if compilation succeeded.
147 // The shader source as passed to glShaderSource.
148 scoped_ptr
<std::string
> source_
;
150 // The source the last compile used.
151 scoped_ptr
<std::string
> signature_source_
;
153 // The translated shader source.
154 scoped_ptr
<std::string
> translated_source_
;
156 // The shader translation log.
157 scoped_ptr
<std::string
> log_info_
;
159 // The type info when the shader was last compiled.
160 VariableMap attrib_map_
;
161 VariableMap uniform_map_
;
162 VariableMap varying_map_
;
164 // The name hashing info when the shader was last compiled.
168 // Tracks the Shaders.
170 // NOTE: To support shared resources an instance of this class will
171 // need to be shared by multiple GLES2Decoders.
172 class GPU_EXPORT ShaderManager
{
177 // Must call before destruction.
178 void Destroy(bool have_context
);
180 // Creates a shader for the given shader ID.
181 Shader
* CreateShader(
186 // Gets an existing shader info for the given shader ID. Returns NULL if none
188 Shader
* GetShader(GLuint client_id
);
190 // Gets a client id for a given service id.
191 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
193 void MarkAsDeleted(Shader
* shader
);
195 // Mark a shader as used
196 void UseShader(Shader
* shader
);
198 // Unmark a shader as used. If it has been deleted and is not used
199 // then we free the shader.
200 void UnuseShader(Shader
* shader
);
202 // Check if a Shader is owned by this ShaderManager.
203 bool IsOwned(Shader
* shader
);
208 // Info for each shader by service side shader Id.
209 typedef base::hash_map
<GLuint
, scoped_refptr
<Shader
> > ShaderMap
;
212 void RemoveShader(Shader
* shader
);
214 DISALLOW_COPY_AND_ASSIGN(ShaderManager
);
220 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_