Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / service / shader_manager.h
blob1f97f42b41294f4ceddf0808a429a1b34e0c382a
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 enum TranslatedShaderSourceType {
28 kANGLE,
29 kGL, // GL or GLES
32 enum ShaderState {
33 kShaderStateWaiting,
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);
43 void DoCompile();
45 ShaderState shader_state() const {
46 return shader_state_;
49 GLuint service_id() const {
50 return marked_for_deletion_ ? 0 : service_id_;
53 GLenum shader_type() const {
54 return shader_type_;
57 int shader_version() const {
58 return shader_version_;
61 const std::string& source() const {
62 return source_;
65 void set_source(const std::string& source) {
66 source_ = 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 original_name is not found, return NULL.
94 const std::string* GetUniformMappedName(
95 const std::string& original_name) const;
97 // If the original_name is not found, return NULL.
98 const std::string* GetVaryingMappedName(
99 const std::string& original_name) const;
101 // If the hashed_name is not found, return NULL.
102 const std::string* GetOriginalNameFromHashedName(
103 const std::string& hashed_name) const;
105 const std::string& log_info() const {
106 return log_info_;
109 bool valid() const {
110 return shader_state_ == kShaderStateCompiled && valid_;
113 bool IsDeleted() const {
114 return marked_for_deletion_;
117 bool InUse() const {
118 DCHECK_GE(use_count_, 0);
119 return use_count_ != 0;
122 // Used by program cache.
123 const AttributeMap& attrib_map() const {
124 return attrib_map_;
127 // Used by program cache.
128 const UniformMap& uniform_map() const {
129 return uniform_map_;
132 // Used by program cache.
133 const VaryingMap& varying_map() const {
134 return varying_map_;
137 // Used by program cache.
138 void set_attrib_map(const AttributeMap& attrib_map) {
139 // copied because cache might be cleared
140 attrib_map_ = AttributeMap(attrib_map);
143 // Used by program cache.
144 void set_uniform_map(const UniformMap& uniform_map) {
145 // copied because cache might be cleared
146 uniform_map_ = UniformMap(uniform_map);
149 // Used by program cache.
150 void set_varying_map(const VaryingMap& varying_map) {
151 // copied because cache might be cleared
152 varying_map_ = VaryingMap(varying_map);
155 private:
156 friend class base::RefCounted<Shader>;
157 friend class ShaderManager;
159 Shader(GLuint service_id, GLenum shader_type);
160 ~Shader();
162 // Must be called only if we currently own the context. Forces the deletion
163 // of the underlying shader service id.
164 void Destroy();
166 void IncUseCount();
167 void DecUseCount();
168 void MarkForDeletion();
169 void DeleteServiceID();
171 int use_count_;
173 // The current state of the shader.
174 ShaderState shader_state_;
176 // The shader has been marked for deletion.
177 bool marked_for_deletion_;
179 // The shader this Shader is tracking.
180 GLuint service_id_;
182 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
183 GLenum shader_type_;
185 // Version of the shader. Can be kUndefinedShaderVersion or version returned
186 // by ANGLE.
187 int shader_version_;
189 // Translated source type when shader was last requested to be compiled.
190 TranslatedShaderSourceType source_type_;
192 // Translator to use, set when shader was last requested to be compiled.
193 scoped_refptr<ShaderTranslatorInterface> translator_;
195 // True if compilation succeeded.
196 bool valid_;
198 // The shader source as passed to glShaderSource.
199 std::string source_;
201 // The source the last compile used.
202 std::string last_compiled_source_;
204 // The translated shader source.
205 std::string translated_source_;
207 // The shader translation log.
208 std::string log_info_;
210 // The type info when the shader was last compiled.
211 AttributeMap attrib_map_;
212 UniformMap uniform_map_;
213 VaryingMap varying_map_;
215 // The name hashing info when the shader was last compiled.
216 NameMap name_map_;
219 // Tracks the Shaders.
221 // NOTE: To support shared resources an instance of this class will
222 // need to be shared by multiple GLES2Decoders.
223 class GPU_EXPORT ShaderManager {
224 public:
225 ShaderManager();
226 ~ShaderManager();
228 // Must call before destruction.
229 void Destroy(bool have_context);
231 // Creates a shader for the given shader ID.
232 Shader* CreateShader(
233 GLuint client_id,
234 GLuint service_id,
235 GLenum shader_type);
237 // Gets an existing shader info for the given shader ID. Returns NULL if none
238 // exists.
239 Shader* GetShader(GLuint client_id);
241 // Gets a client id for a given service id.
242 bool GetClientId(GLuint service_id, GLuint* client_id) const;
244 void Delete(Shader* shader);
246 // Mark a shader as used
247 void UseShader(Shader* shader);
249 // Unmark a shader as used. If it has been deleted and is not used
250 // then we free the shader.
251 void UnuseShader(Shader* shader);
253 // Check if a Shader is owned by this ShaderManager.
254 bool IsOwned(Shader* shader);
256 private:
257 friend class Shader;
259 // Info for each shader by service side shader Id.
260 typedef base::hash_map<GLuint, scoped_refptr<Shader> > ShaderMap;
261 ShaderMap shaders_;
263 void RemoveShader(Shader* shader);
265 DISALLOW_COPY_AND_ASSIGN(ShaderManager);
268 } // namespace gles2
269 } // namespace gpu
271 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_