Fix bad field type in ScriptRequestIncident.
[chromium-blink-merge.git] / gpu / command_buffer / service / program_manager.h
bloba3de24bfc5a42be6509e67eedd96d5bb59028a9c
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_PROGRAM_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h"
14 #include "gpu/command_buffer/service/common_decoder.h"
15 #include "gpu/command_buffer/service/gl_utils.h"
16 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
17 #include "gpu/command_buffer/service/shader_manager.h"
18 #include "gpu/gpu_export.h"
20 namespace gpu {
21 namespace gles2 {
23 class ProgramCache;
24 class ProgramManager;
25 class Shader;
26 class ShaderManager;
28 // This is used to track which attributes a particular program needs
29 // so we can verify at glDrawXXX time that every attribute is either disabled
30 // or if enabled that it points to a valid source.
31 class GPU_EXPORT Program : public base::RefCounted<Program> {
32 public:
33 static const int kMaxAttachedShaders = 2;
35 enum VaryingsPackingOption {
36 kCountOnlyStaticallyUsed,
37 kCountAll
40 enum UniformApiType {
41 kUniformNone = 0,
42 kUniform1i = 1 << 0,
43 kUniform2i = 1 << 1,
44 kUniform3i = 1 << 2,
45 kUniform4i = 1 << 3,
46 kUniform1f = 1 << 4,
47 kUniform2f = 1 << 5,
48 kUniform3f = 1 << 6,
49 kUniform4f = 1 << 7,
50 kUniformMatrix2f = 1 << 8,
51 kUniformMatrix3f = 1 << 9,
52 kUniformMatrix4f = 1 << 10,
55 struct UniformInfo {
56 UniformInfo();
57 UniformInfo(
58 GLsizei _size, GLenum _type, GLint _fake_location_base,
59 const std::string& _name);
60 ~UniformInfo();
62 bool IsValid() const {
63 return size != 0;
66 bool IsSampler() const {
67 return type == GL_SAMPLER_2D || type == GL_SAMPLER_2D_RECT_ARB ||
68 type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES;
71 GLsizei size;
72 GLenum type;
73 uint32 accepts_api_type;
74 GLint fake_location_base;
75 bool is_array;
76 std::string name;
77 std::vector<GLint> element_locations;
78 std::vector<GLuint> texture_units;
80 struct VertexAttrib {
81 VertexAttrib(GLsizei _size, GLenum _type, const std::string& _name,
82 GLint _location)
83 : size(_size),
84 type(_type),
85 location(_location),
86 name(_name) {
88 GLsizei size;
89 GLenum type;
90 GLint location;
91 std::string name;
94 typedef std::vector<UniformInfo> UniformInfoVector;
95 typedef std::vector<VertexAttrib> AttribInfoVector;
96 typedef std::vector<int> SamplerIndices;
97 typedef std::map<std::string, GLint> LocationMap;
99 Program(ProgramManager* manager, GLuint service_id);
101 GLuint service_id() const {
102 return service_id_;
105 const SamplerIndices& sampler_indices() {
106 return sampler_indices_;
109 const AttribInfoVector& GetAttribInfos() const {
110 return attrib_infos_;
113 const VertexAttrib* GetAttribInfo(GLint index) const {
114 return (static_cast<size_t>(index) < attrib_infos_.size()) ?
115 &attrib_infos_[index] : NULL;
118 GLint GetAttribLocation(const std::string& original_name) const;
120 const VertexAttrib* GetAttribInfoByLocation(GLuint location) const {
121 if (location < attrib_location_to_index_map_.size()) {
122 GLint index = attrib_location_to_index_map_[location];
123 if (index >= 0) {
124 return &attrib_infos_[index];
127 return NULL;
130 const UniformInfo* GetUniformInfo(GLint index) const;
132 // If the original name is not found, return NULL.
133 const std::string* GetAttribMappedName(
134 const std::string& original_name) const;
136 // If the hashed name is not found, return NULL.
137 const std::string* GetOriginalNameFromHashedName(
138 const std::string& hashed_name) const;
140 // Gets the fake location of a uniform by name.
141 GLint GetUniformFakeLocation(const std::string& name) const;
143 // Gets the UniformInfo of a uniform by location.
144 const UniformInfo* GetUniformInfoByFakeLocation(
145 GLint fake_location, GLint* real_location, GLint* array_index) const;
147 // Gets all the program info.
148 void GetProgramInfo(
149 ProgramManager* manager, CommonDecoder::Bucket* bucket) const;
151 // Gets all the UniformBlock info.
152 // Return false on overflow.
153 bool GetUniformBlocks(CommonDecoder::Bucket* bucket) const;
155 // Gets all the TransformFeedbackVarying info.
156 // Return false on overflow.
157 bool GetTransformFeedbackVaryings(CommonDecoder::Bucket* bucket) const;
159 // Gather all info through glGetActiveUniformsiv, except for size, type,
160 // name_length, which we gather through glGetActiveUniform in
161 // glGetProgramInfoCHROMIUM.
162 bool GetUniformsES3(CommonDecoder::Bucket* bucket) const;
164 // Sets the sampler values for a uniform.
165 // This is safe to call for any location. If the location is not
166 // a sampler uniform nothing will happen.
167 // Returns false if fake_location is a sampler and any value
168 // is >= num_texture_units. Returns true otherwise.
169 bool SetSamplers(
170 GLint num_texture_units, GLint fake_location,
171 GLsizei count, const GLint* value);
173 bool IsDeleted() const {
174 return deleted_;
177 void GetProgramiv(GLenum pname, GLint* params);
179 bool IsValid() const {
180 return valid_;
183 bool AttachShader(ShaderManager* manager, Shader* shader);
184 bool DetachShader(ShaderManager* manager, Shader* shader);
186 void CompileAttachedShaders();
187 bool AttachedShadersExist() const;
188 bool CanLink() const;
190 // Performs glLinkProgram and related activities.
191 bool Link(ShaderManager* manager,
192 VaryingsPackingOption varyings_packing_option,
193 const ShaderCacheCallback& shader_callback);
195 // Performs glValidateProgram and related activities.
196 void Validate();
198 const std::string* log_info() const {
199 return log_info_.get();
202 bool InUse() const {
203 DCHECK_GE(use_count_, 0);
204 return use_count_ != 0;
207 // Sets attribute-location binding from a glBindAttribLocation() call.
208 void SetAttribLocationBinding(const std::string& attrib, GLint location) {
209 bind_attrib_location_map_[attrib] = location;
212 // Sets uniform-location binding from a glBindUniformLocationCHROMIUM call.
213 // returns false if error.
214 bool SetUniformLocationBinding(const std::string& name, GLint location);
216 // Detects if there are attribute location conflicts from
217 // glBindAttribLocation() calls.
218 // We only consider the declared attributes in the program.
219 bool DetectAttribLocationBindingConflicts() const;
221 // Detects if there are uniforms of the same name but different type
222 // or precision in vertex/fragment shaders.
223 // Return true and set the first found conflicting hashed name to
224 // conflicting_name if such cases are detected.
225 bool DetectUniformsMismatch(std::string* conflicting_name) const;
227 // Return true if a varying is statically used in fragment shader, but it
228 // is not declared in vertex shader.
229 bool DetectVaryingsMismatch(std::string* conflicting_name) const;
231 // Return true if any built-in invariant matching rules are broken as in
232 // GLSL ES spec 1.00.17, section 4.6.4, Invariance and Linkage.
233 bool DetectBuiltInInvariantConflicts() const;
235 // Return true if an uniform and an attribute share the same name.
236 bool DetectGlobalNameConflicts(std::string* conflicting_name) const;
238 // Return false if varyings can't be packed into the max available
239 // varying registers.
240 bool CheckVaryingsPacking(VaryingsPackingOption option) const;
242 void TransformFeedbackVaryings(GLsizei count, const char* const* varyings,
243 GLenum buffer_mode);
245 // Visible for testing
246 const LocationMap& bind_attrib_location_map() const {
247 return bind_attrib_location_map_;
250 const std::vector<std::string>& transform_feedback_varyings() const {
251 return transform_feedback_varyings_;
254 GLenum transform_feedback_buffer_mode() const {
255 return transform_feedback_buffer_mode_;
258 private:
259 friend class base::RefCounted<Program>;
260 friend class ProgramManager;
262 ~Program();
264 void set_log_info(const char* str) {
265 log_info_.reset(str ? new std::string(str) : NULL);
268 void ClearLinkStatus() {
269 link_status_ = false;
272 void IncUseCount() {
273 ++use_count_;
276 void DecUseCount() {
277 --use_count_;
278 DCHECK_GE(use_count_, 0);
281 void MarkAsDeleted() {
282 DCHECK(!deleted_);
283 deleted_ = true;
286 // Resets the program.
287 void Reset();
289 // Updates the program info after a successful link.
290 void Update();
292 // Process the program log, replacing the hashed names with original names.
293 std::string ProcessLogInfo(const std::string& log);
295 // Updates the program log info from GL
296 void UpdateLogInfo();
298 // Clears all the uniforms.
299 void ClearUniforms(std::vector<uint8>* zero_buffer);
301 // If long attribate names are mapped during shader translation, call
302 // glBindAttribLocation() again with the mapped names.
303 // This is called right before the glLink() call, but after shaders are
304 // translated.
305 void ExecuteBindAttribLocationCalls();
307 bool AddUniformInfo(
308 GLsizei size, GLenum type, GLint location, GLint fake_base_location,
309 const std::string& name, const std::string& original_name,
310 size_t* next_available_index);
312 // Query uniform data returned by ANGLE translator by the mapped name.
313 // Some drivers incorrectly return an uniform name of size-1 array without
314 // "[0]". In this case, we correct the name by appending "[0]" to it.
315 void GetCorrectedUniformData(
316 const std::string& name,
317 std::string* corrected_name, std::string* original_name,
318 GLsizei* size, GLenum* type) const;
320 // Query VertexAttrib data returned by ANGLE translator by the mapped name.
321 void GetVertexAttribData(
322 const std::string& name, std::string* original_name, GLenum* type) const;
324 void DetachShaders(ShaderManager* manager);
326 static inline GLint GetUniformInfoIndexFromFakeLocation(
327 GLint fake_location) {
328 return fake_location & 0xFFFF;
331 static inline GLint GetArrayElementIndexFromFakeLocation(
332 GLint fake_location) {
333 return (fake_location >> 16) & 0xFFFF;
336 ProgramManager* manager_;
338 int use_count_;
340 GLsizei max_attrib_name_length_;
342 // Attrib by index.
343 AttribInfoVector attrib_infos_;
345 // Attrib by location to index.
346 std::vector<GLint> attrib_location_to_index_map_;
348 GLsizei max_uniform_name_length_;
350 // Uniform info by index.
351 UniformInfoVector uniform_infos_;
353 // The indices of the uniforms that are samplers.
354 SamplerIndices sampler_indices_;
356 // The program this Program is tracking.
357 GLuint service_id_;
359 // Shaders by type of shader.
360 scoped_refptr<Shader>
361 attached_shaders_[kMaxAttachedShaders];
363 // True if this program is marked as deleted.
364 bool deleted_;
366 // This is true if glLinkProgram was successful at least once.
367 bool valid_;
369 // This is true if glLinkProgram was successful last time it was called.
370 bool link_status_;
372 // True if the uniforms have been cleared.
373 bool uniforms_cleared_;
375 // This is different than uniform_infos_.size() because
376 // that is a sparce array.
377 GLint num_uniforms_;
379 // Log info
380 scoped_ptr<std::string> log_info_;
382 // attribute-location binding map from glBindAttribLocation() calls.
383 LocationMap bind_attrib_location_map_;
385 // uniform-location binding map from glBindUniformLocationCHROMIUM() calls.
386 LocationMap bind_uniform_location_map_;
388 std::vector<std::string> transform_feedback_varyings_;
390 GLenum transform_feedback_buffer_mode_;
393 // Tracks the Programs.
395 // NOTE: To support shared resources an instance of this class will
396 // need to be shared by multiple GLES2Decoders.
397 class GPU_EXPORT ProgramManager {
398 public:
399 explicit ProgramManager(ProgramCache* program_cache,
400 uint32 max_varying_vectors);
401 ~ProgramManager();
403 // Must call before destruction.
404 void Destroy(bool have_context);
406 // Creates a new program.
407 Program* CreateProgram(GLuint client_id, GLuint service_id);
409 // Gets a program.
410 Program* GetProgram(GLuint client_id);
412 // Gets a client id for a given service id.
413 bool GetClientId(GLuint service_id, GLuint* client_id) const;
415 // Gets the shader cache
416 ProgramCache* program_cache() const;
418 // Marks a program as deleted. If it is not used the program will be deleted.
419 void MarkAsDeleted(ShaderManager* shader_manager, Program* program);
421 // Marks a program as used.
422 void UseProgram(Program* program);
424 // Makes a program as unused. If deleted the program will be removed.
425 void UnuseProgram(ShaderManager* shader_manager, Program* program);
427 // Clears the uniforms for this program.
428 void ClearUniforms(Program* program);
430 // Returns true if prefix is invalid for gl.
431 static bool IsInvalidPrefix(const char* name, size_t length);
433 // Check if a Program is owned by this ProgramManager.
434 bool IsOwned(Program* program);
436 static int32 MakeFakeLocation(int32 index, int32 element);
438 uint32 max_varying_vectors() const {
439 return max_varying_vectors_;
442 private:
443 friend class Program;
445 void StartTracking(Program* program);
446 void StopTracking(Program* program);
448 void RemoveProgramInfoIfUnused(
449 ShaderManager* shader_manager, Program* program);
451 // Info for each "successfully linked" program by service side program Id.
452 // TODO(gman): Choose a faster container.
453 typedef std::map<GLuint, scoped_refptr<Program> > ProgramMap;
454 ProgramMap programs_;
456 // Counts the number of Program allocated with 'this' as its manager.
457 // Allows to check no Program will outlive this.
458 unsigned int program_count_;
460 bool have_context_;
462 // Used to clear uniforms.
463 std::vector<uint8> zero_;
465 ProgramCache* program_cache_;
467 uint32 max_varying_vectors_;
469 DISALLOW_COPY_AND_ASSIGN(ProgramManager);
472 } // namespace gles2
473 } // namespace gpu
475 #endif // GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_