OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ | 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ |
6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ | 6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "gpu/command_buffer/service/gl_utils.h" | 14 #include "gpu/command_buffer/service/gl_utils.h" |
15 #include "gpu/command_buffer/service/shader_translator.h" | 15 #include "gpu/command_buffer/service/shader_translator.h" |
16 #include "gpu/gpu_export.h" | 16 #include "gpu/gpu_export.h" |
17 | 17 |
18 namespace gpu { | 18 namespace gpu { |
19 namespace gles2 { | 19 namespace gles2 { |
20 | 20 |
| 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; |
| 28 |
| 29 enum CompilationStatus { |
| 30 NOT_COMPILED, |
| 31 // We're pending compilation for a cache hit with the program cache. |
| 32 PENDING_DEFERRED_COMPILE, |
| 33 COMPILED |
| 34 }; |
| 35 |
| 36 void UpdateSource(const char* source) { |
| 37 // If the source is flagged as compiled, then store our previous source |
| 38 // for deferred compile and caching. |
| 39 if (!deferred_compilation_source_.get()) { |
| 40 deferred_compilation_source_.reset(source_.release()); |
| 41 } |
| 42 source_.reset(source ? new std::string(source) : NULL); |
| 43 } |
| 44 |
| 45 void UpdateTranslatedSource(const char* translated_source) { |
| 46 translated_source_.reset( |
| 47 translated_source ? new std::string(translated_source) : NULL); |
| 48 } |
| 49 |
| 50 GLuint service_id() const { |
| 51 return service_id_; |
| 52 } |
| 53 |
| 54 GLenum shader_type() const { |
| 55 return shader_type_; |
| 56 } |
| 57 |
| 58 const std::string* source() const { |
| 59 return source_.get(); |
| 60 } |
| 61 |
| 62 const std::string* translated_source() const { |
| 63 return translated_source_.get(); |
| 64 } |
| 65 |
| 66 void SetStatus( |
| 67 bool valid, const char* log, |
| 68 ShaderTranslatorInterface* translator); |
| 69 |
| 70 CompilationStatus compilation_status() const { |
| 71 return compilation_status_; |
| 72 } |
| 73 |
| 74 // The source that was used when the user called CompileShader. |
| 75 // This is used for a deferred compile and in the program cache |
| 76 const std::string* deferred_compilation_source() const { |
| 77 return deferred_compilation_source_.get() != NULL ? |
| 78 deferred_compilation_source_.get() : |
| 79 source_.get(); |
| 80 } |
| 81 |
| 82 // Resets our deferred compilation source and stores if the source was |
| 83 // actually compiled, or if we're expecting a cache hit |
| 84 void FlagSourceAsCompiled(bool actually_compiled) { |
| 85 compilation_status_ = actually_compiled ? |
| 86 COMPILED : |
| 87 PENDING_DEFERRED_COMPILE; |
| 88 deferred_compilation_source_.reset(); |
| 89 } |
| 90 |
| 91 const VariableInfo* GetAttribInfo(const std::string& name) const; |
| 92 const VariableInfo* GetUniformInfo(const std::string& name) const; |
| 93 |
| 94 // If the original_name is not found, return NULL. |
| 95 const std::string* GetAttribMappedName( |
| 96 const std::string& original_name) const; |
| 97 |
| 98 // If the hashed_name is not found, return NULL. |
| 99 const std::string* GetOriginalNameFromHashedName( |
| 100 const std::string& hashed_name) const; |
| 101 |
| 102 const std::string* log_info() const { |
| 103 return log_info_.get(); |
| 104 } |
| 105 |
| 106 bool IsValid() const { |
| 107 return valid_; |
| 108 } |
| 109 |
| 110 bool IsDeleted() const { |
| 111 return service_id_ == 0; |
| 112 } |
| 113 |
| 114 bool InUse() const { |
| 115 DCHECK_GE(use_count_, 0); |
| 116 return use_count_ != 0; |
| 117 } |
| 118 |
| 119 // Used by program cache. |
| 120 const ShaderTranslator::VariableMap& attrib_map() const { |
| 121 return attrib_map_; |
| 122 } |
| 123 |
| 124 // Used by program cache. |
| 125 const ShaderTranslator::VariableMap& uniform_map() const { |
| 126 return uniform_map_; |
| 127 } |
| 128 |
| 129 // Used by program cache. |
| 130 void set_attrib_map(const ShaderTranslator::VariableMap& attrib_map) { |
| 131 // copied because cache might be cleared |
| 132 attrib_map_ = ShaderTranslator::VariableMap(attrib_map); |
| 133 } |
| 134 |
| 135 // Used by program cache. |
| 136 void set_uniform_map(const ShaderTranslator::VariableMap& uniform_map) { |
| 137 // copied because cache might be cleared |
| 138 uniform_map_ = ShaderTranslator::VariableMap(uniform_map); |
| 139 } |
| 140 |
| 141 private: |
| 142 typedef ShaderTranslator::VariableMap VariableMap; |
| 143 typedef ShaderTranslator::NameMap NameMap; |
| 144 |
| 145 friend class base::RefCounted<Shader>; |
| 146 friend class ShaderManager; |
| 147 |
| 148 Shader(GLuint service_id, GLenum shader_type); |
| 149 ~Shader(); |
| 150 |
| 151 void IncUseCount(); |
| 152 void DecUseCount(); |
| 153 void MarkAsDeleted(); |
| 154 |
| 155 int use_count_; |
| 156 |
| 157 // The shader this Shader is tracking. |
| 158 GLuint service_id_; |
| 159 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER. |
| 160 GLenum shader_type_; |
| 161 |
| 162 // True if compilation succeeded. |
| 163 bool valid_; |
| 164 |
| 165 // The shader source as passed to glShaderSource. |
| 166 scoped_ptr<std::string> source_; |
| 167 |
| 168 // The translated shader source. |
| 169 scoped_ptr<std::string> translated_source_; |
| 170 |
| 171 // The shader translation log. |
| 172 scoped_ptr<std::string> log_info_; |
| 173 |
| 174 // The type info when the shader was last compiled. |
| 175 VariableMap attrib_map_; |
| 176 VariableMap uniform_map_; |
| 177 |
| 178 // The name hashing info when the shader was last compiled. |
| 179 NameMap name_map_; |
| 180 |
| 181 // The current compilation status of the shader |
| 182 CompilationStatus compilation_status_; |
| 183 |
| 184 // Holds on to the source for a deferred compile. |
| 185 scoped_ptr<std::string> deferred_compilation_source_; |
| 186 }; |
| 187 |
21 // Tracks the Shaders. | 188 // Tracks the Shaders. |
22 // | 189 // |
23 // NOTE: To support shared resources an instance of this class will | 190 // NOTE: To support shared resources an instance of this class will |
24 // need to be shared by multiple GLES2Decoders. | 191 // need to be shared by multiple GLES2Decoders. |
25 class GPU_EXPORT ShaderManager { | 192 class GPU_EXPORT ShaderManager { |
26 public: | 193 public: |
27 // This is used to keep the source code for a shader. This is because in order | |
28 // to emluate GLES2 the shaders will have to be re-written before passed to | |
29 // the underlying OpenGL. But, when the user calls glGetShaderSource they | |
30 // should get the source they passed in, not the re-written source. | |
31 class GPU_EXPORT ShaderInfo : public base::RefCounted<ShaderInfo> { | |
32 public: | |
33 typedef scoped_refptr<ShaderInfo> Ref; | |
34 typedef ShaderTranslator::VariableInfo VariableInfo; | |
35 | |
36 enum CompilationStatus { | |
37 NOT_COMPILED, | |
38 // We're pending compilation for a cache hit with the program cache. | |
39 PENDING_DEFERRED_COMPILE, | |
40 COMPILED | |
41 }; | |
42 | |
43 void UpdateSource(const char* source) { | |
44 // If the source is flagged as compiled, then store our previous source | |
45 // for deferred compile and caching. | |
46 if (!deferred_compilation_source_.get()) { | |
47 deferred_compilation_source_.reset(source_.release()); | |
48 } | |
49 source_.reset(source ? new std::string(source) : NULL); | |
50 } | |
51 | |
52 void UpdateTranslatedSource(const char* translated_source) { | |
53 translated_source_.reset( | |
54 translated_source ? new std::string(translated_source) : NULL); | |
55 } | |
56 | |
57 GLuint service_id() const { | |
58 return service_id_; | |
59 } | |
60 | |
61 GLenum shader_type() const { | |
62 return shader_type_; | |
63 } | |
64 | |
65 const std::string* source() const { | |
66 return source_.get(); | |
67 } | |
68 | |
69 const std::string* translated_source() const { | |
70 return translated_source_.get(); | |
71 } | |
72 | |
73 void SetStatus( | |
74 bool valid, const char* log, | |
75 ShaderTranslatorInterface* translator); | |
76 | |
77 CompilationStatus compilation_status() const { | |
78 return compilation_status_; | |
79 } | |
80 | |
81 // The source that was used when the user called CompileShader. | |
82 // This is used for a deferred compile and in the program cache | |
83 const std::string* deferred_compilation_source() const { | |
84 return deferred_compilation_source_.get() != NULL ? | |
85 deferred_compilation_source_.get() : | |
86 source_.get(); | |
87 } | |
88 | |
89 // Resets our deferred compilation source and stores if the source was | |
90 // actually compiled, or if we're expecting a cache hit | |
91 void FlagSourceAsCompiled(bool actually_compiled) { | |
92 compilation_status_ = actually_compiled ? | |
93 COMPILED : | |
94 PENDING_DEFERRED_COMPILE; | |
95 deferred_compilation_source_.reset(); | |
96 } | |
97 | |
98 const VariableInfo* GetAttribInfo(const std::string& name) const; | |
99 const VariableInfo* GetUniformInfo(const std::string& name) const; | |
100 | |
101 // If the original_name is not found, return NULL. | |
102 const std::string* GetAttribMappedName( | |
103 const std::string& original_name) const; | |
104 | |
105 // If the hashed_name is not found, return NULL. | |
106 const std::string* GetOriginalNameFromHashedName( | |
107 const std::string& hashed_name) const; | |
108 | |
109 const std::string* log_info() const { | |
110 return log_info_.get(); | |
111 } | |
112 | |
113 bool IsValid() const { | |
114 return valid_; | |
115 } | |
116 | |
117 bool IsDeleted() const { | |
118 return service_id_ == 0; | |
119 } | |
120 | |
121 bool InUse() const { | |
122 DCHECK_GE(use_count_, 0); | |
123 return use_count_ != 0; | |
124 } | |
125 | |
126 // Used by program cache. | |
127 const ShaderTranslator::VariableMap& attrib_map() const { | |
128 return attrib_map_; | |
129 } | |
130 | |
131 // Used by program cache. | |
132 const ShaderTranslator::VariableMap& uniform_map() const { | |
133 return uniform_map_; | |
134 } | |
135 | |
136 // Used by program cache. | |
137 void set_attrib_map(const ShaderTranslator::VariableMap& attrib_map) { | |
138 // copied because cache might be cleared | |
139 attrib_map_ = ShaderTranslator::VariableMap(attrib_map); | |
140 } | |
141 | |
142 // Used by program cache. | |
143 void set_uniform_map(const ShaderTranslator::VariableMap& uniform_map) { | |
144 // copied because cache might be cleared | |
145 uniform_map_ = ShaderTranslator::VariableMap(uniform_map); | |
146 } | |
147 | |
148 private: | |
149 typedef ShaderTranslator::VariableMap VariableMap; | |
150 typedef ShaderTranslator::NameMap NameMap; | |
151 | |
152 friend class base::RefCounted<ShaderInfo>; | |
153 friend class ShaderManager; | |
154 | |
155 ShaderInfo(GLuint service_id, GLenum shader_type); | |
156 ~ShaderInfo(); | |
157 | |
158 void IncUseCount(); | |
159 void DecUseCount(); | |
160 void MarkAsDeleted(); | |
161 | |
162 int use_count_; | |
163 | |
164 // The shader this ShaderInfo is tracking. | |
165 GLuint service_id_; | |
166 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER. | |
167 GLenum shader_type_; | |
168 | |
169 // True if compilation succeeded. | |
170 bool valid_; | |
171 | |
172 // The shader source as passed to glShaderSource. | |
173 scoped_ptr<std::string> source_; | |
174 | |
175 // The translated shader source. | |
176 scoped_ptr<std::string> translated_source_; | |
177 | |
178 // The shader translation log. | |
179 scoped_ptr<std::string> log_info_; | |
180 | |
181 // The type info when the shader was last compiled. | |
182 VariableMap attrib_map_; | |
183 VariableMap uniform_map_; | |
184 | |
185 // The name hashing info when the shader was last compiled. | |
186 NameMap name_map_; | |
187 | |
188 // The current compilation status of the shader | |
189 CompilationStatus compilation_status_; | |
190 | |
191 // Holds on to the source for a deferred compile. | |
192 scoped_ptr<std::string> deferred_compilation_source_; | |
193 }; | |
194 | |
195 ShaderManager(); | 194 ShaderManager(); |
196 ~ShaderManager(); | 195 ~ShaderManager(); |
197 | 196 |
198 // Must call before destruction. | 197 // Must call before destruction. |
199 void Destroy(bool have_context); | 198 void Destroy(bool have_context); |
200 | 199 |
201 // Creates a shader info for the given shader ID. | 200 // Creates a shader info for the given shader ID. |
202 ShaderInfo* CreateShaderInfo( | 201 Shader* CreateShader( |
203 GLuint client_id, | 202 GLuint client_id, |
204 GLuint service_id, | 203 GLuint service_id, |
205 GLenum shader_type); | 204 GLenum shader_type); |
206 | 205 |
207 // Gets an existing shader info for the given shader ID. Returns NULL if none | 206 // Gets an existing shader info for the given shader ID. Returns NULL if none |
208 // exists. | 207 // exists. |
209 ShaderInfo* GetShaderInfo(GLuint client_id); | 208 Shader* GetShader(GLuint client_id); |
210 | 209 |
211 // Gets a client id for a given service id. | 210 // Gets a client id for a given service id. |
212 bool GetClientId(GLuint service_id, GLuint* client_id) const; | 211 bool GetClientId(GLuint service_id, GLuint* client_id) const; |
213 | 212 |
214 void MarkAsDeleted(ShaderInfo* info); | 213 void MarkAsDeleted(Shader* info); |
215 | 214 |
216 // Mark a shader as used | 215 // Mark a shader as used |
217 void UseShader(ShaderInfo* info); | 216 void UseShader(Shader* info); |
218 | 217 |
219 // Unmark a shader as used. If it has been deleted and is not used | 218 // Unmark a shader as used. If it has been deleted and is not used |
220 // then we free the info. | 219 // then we free the info. |
221 void UnuseShader(ShaderInfo* info); | 220 void UnuseShader(Shader* info); |
222 | 221 |
223 // Check if a ShaderInfo is owned by this ShaderManager. | 222 // Check if a Shader is owned by this ShaderManager. |
224 bool IsOwned(ShaderInfo* info); | 223 bool IsOwned(Shader* info); |
225 | 224 |
226 private: | 225 private: |
| 226 friend class Shader; |
| 227 |
227 // Info for each shader by service side shader Id. | 228 // Info for each shader by service side shader Id. |
228 typedef base::hash_map<GLuint, ShaderInfo::Ref> ShaderInfoMap; | 229 typedef base::hash_map<GLuint, scoped_refptr<Shader> > ShaderInfoMap; |
229 ShaderInfoMap shader_infos_; | 230 ShaderInfoMap shader_infos_; |
230 | 231 |
231 void RemoveShaderInfoIfUnused(ShaderInfo* info); | 232 void RemoveShader(Shader* info); |
232 | 233 |
233 DISALLOW_COPY_AND_ASSIGN(ShaderManager); | 234 DISALLOW_COPY_AND_ASSIGN(ShaderManager); |
234 }; | 235 }; |
235 | 236 |
236 } // namespace gles2 | 237 } // namespace gles2 |
237 } // namespace gpu | 238 } // namespace gpu |
238 | 239 |
239 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ | 240 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_ |
240 | 241 |
OLD | NEW |