Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / gpu / command_buffer / service / shader_manager.h
blob1324238f2a9c2c2d8047b3602dbff6f63f196abe
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_
8 #include <string>
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"
18 namespace gpu {
19 namespace gles2 {
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> {
26 public:
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 {
39 return service_id_;
42 GLenum shader_type() const {
43 return shader_type_;
46 const std::string* source() const {
47 return source_.get();
50 const std::string* translated_source() const {
51 return translated_source_.get();
54 const std::string* signature_source() const {
55 return signature_source_.get();
58 void SetStatus(
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 {
78 return valid_;
81 bool IsDeleted() const {
82 return service_id_ == 0;
85 bool InUse() const {
86 DCHECK_GE(use_count_, 0);
87 return use_count_ != 0;
90 // Used by program cache.
91 const ShaderTranslator::VariableMap& attrib_map() const {
92 return attrib_map_;
95 // Used by program cache.
96 const ShaderTranslator::VariableMap& uniform_map() const {
97 return uniform_map_;
100 // Used by program cache.
101 const ShaderTranslator::VariableMap& varying_map() const {
102 return varying_map_;
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);
123 private:
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);
131 ~Shader();
133 void IncUseCount();
134 void DecUseCount();
135 void MarkAsDeleted();
137 int use_count_;
139 // The shader this Shader is tracking.
140 GLuint service_id_;
141 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
142 GLenum shader_type_;
144 // True if compilation succeeded.
145 bool valid_;
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.
165 NameMap name_map_;
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 {
173 public:
174 ShaderManager();
175 ~ShaderManager();
177 // Must call before destruction.
178 void Destroy(bool have_context);
180 // Creates a shader for the given shader ID.
181 Shader* CreateShader(
182 GLuint client_id,
183 GLuint service_id,
184 GLenum shader_type);
186 // Gets an existing shader info for the given shader ID. Returns NULL if none
187 // exists.
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);
205 private:
206 friend class Shader;
208 // Info for each shader by service side shader Id.
209 typedef base::hash_map<GLuint, scoped_refptr<Shader> > ShaderMap;
210 ShaderMap shaders_;
212 void RemoveShader(Shader* shader);
214 DISALLOW_COPY_AND_ASSIGN(ShaderManager);
217 } // namespace gles2
218 } // namespace gpu
220 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_