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_
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"
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
> {
33 static const int kMaxAttachedShaders
= 2;
35 enum VaryingsPackingOption
{
36 kCountOnlyStaticallyUsed
,
50 kUniformMatrix2f
= 1 << 8,
51 kUniformMatrix3f
= 1 << 9,
52 kUniformMatrix4f
= 1 << 10,
58 GLsizei _size
, GLenum _type
, GLint _fake_location_base
,
59 const std::string
& _name
);
62 bool IsValid() const {
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
;
73 uint32 accepts_api_type
;
74 GLint fake_location_base
;
77 std::vector
<GLint
> element_locations
;
78 std::vector
<GLuint
> texture_units
;
81 VertexAttrib(GLsizei _size
, GLenum _type
, const 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 {
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
];
124 return &attrib_infos_
[index
];
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.
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.
170 GLint num_texture_units
, GLint fake_location
,
171 GLsizei count
, const GLint
* value
);
173 bool IsDeleted() const {
177 void GetProgramiv(GLenum pname
, GLint
* params
);
179 bool IsValid() const {
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.
198 const std::string
* log_info() const {
199 return log_info_
.get();
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 the shader version combination is not valid.
217 bool DetectShaderVersionMismatch() const;
219 // Detects if there are attribute location conflicts from
220 // glBindAttribLocation() calls.
221 // We only consider the declared attributes in the program.
222 bool DetectAttribLocationBindingConflicts() const;
224 // Detects if there are uniforms of the same name but different type
225 // or precision in vertex/fragment shaders.
226 // Return true and set the first found conflicting hashed name to
227 // conflicting_name if such cases are detected.
228 bool DetectUniformsMismatch(std::string
* conflicting_name
) const;
230 // Return true if a varying is statically used in fragment shader, but it
231 // is not declared in vertex shader.
232 bool DetectVaryingsMismatch(std::string
* conflicting_name
) const;
234 // Return true if any built-in invariant matching rules are broken as in
235 // GLSL ES spec 1.00.17, section 4.6.4, Invariance and Linkage.
236 bool DetectBuiltInInvariantConflicts() const;
238 // Return true if an uniform and an attribute share the same name.
239 bool DetectGlobalNameConflicts(std::string
* conflicting_name
) const;
241 // Return false if varyings can't be packed into the max available
242 // varying registers.
243 bool CheckVaryingsPacking(VaryingsPackingOption option
) const;
245 void TransformFeedbackVaryings(GLsizei count
, const char* const* varyings
,
248 // Visible for testing
249 const LocationMap
& bind_attrib_location_map() const {
250 return bind_attrib_location_map_
;
253 const std::vector
<std::string
>& transform_feedback_varyings() const {
254 return transform_feedback_varyings_
;
257 GLenum
transform_feedback_buffer_mode() const {
258 return transform_feedback_buffer_mode_
;
262 friend class base::RefCounted
<Program
>;
263 friend class ProgramManager
;
267 void set_log_info(const char* str
) {
268 log_info_
.reset(str
? new std::string(str
) : NULL
);
271 void ClearLinkStatus() {
272 link_status_
= false;
281 DCHECK_GE(use_count_
, 0);
284 void MarkAsDeleted() {
289 // Resets the program.
292 // Updates the program info after a successful link.
295 // Process the program log, replacing the hashed names with original names.
296 std::string
ProcessLogInfo(const std::string
& log
);
298 // Updates the program log info from GL
299 void UpdateLogInfo();
301 // Clears all the uniforms.
302 void ClearUniforms(std::vector
<uint8
>* zero_buffer
);
304 // If long attribate names are mapped during shader translation, call
305 // glBindAttribLocation() again with the mapped names.
306 // This is called right before the glLink() call, but after shaders are
308 void ExecuteBindAttribLocationCalls();
311 GLsizei size
, GLenum type
, GLint location
, GLint fake_base_location
,
312 const std::string
& name
, const std::string
& original_name
,
313 size_t* next_available_index
);
315 // Query uniform data returned by ANGLE translator by the mapped name.
316 // Some drivers incorrectly return an uniform name of size-1 array without
317 // "[0]". In this case, we correct the name by appending "[0]" to it.
318 void GetCorrectedUniformData(
319 const std::string
& name
,
320 std::string
* corrected_name
, std::string
* original_name
,
321 GLsizei
* size
, GLenum
* type
) const;
323 // Query VertexAttrib data returned by ANGLE translator by the mapped name.
324 void GetVertexAttribData(
325 const std::string
& name
, std::string
* original_name
, GLenum
* type
) const;
327 void DetachShaders(ShaderManager
* manager
);
329 static inline GLint
GetUniformInfoIndexFromFakeLocation(
330 GLint fake_location
) {
331 return fake_location
& 0xFFFF;
334 static inline GLint
GetArrayElementIndexFromFakeLocation(
335 GLint fake_location
) {
336 return (fake_location
>> 16) & 0xFFFF;
339 ProgramManager
* manager_
;
343 GLsizei max_attrib_name_length_
;
346 AttribInfoVector attrib_infos_
;
348 // Attrib by location to index.
349 std::vector
<GLint
> attrib_location_to_index_map_
;
351 GLsizei max_uniform_name_length_
;
353 // Uniform info by index.
354 UniformInfoVector uniform_infos_
;
356 // The indices of the uniforms that are samplers.
357 SamplerIndices sampler_indices_
;
359 // The program this Program is tracking.
362 // Shaders by type of shader.
363 scoped_refptr
<Shader
>
364 attached_shaders_
[kMaxAttachedShaders
];
366 // True if this program is marked as deleted.
369 // This is true if glLinkProgram was successful at least once.
372 // This is true if glLinkProgram was successful last time it was called.
375 // True if the uniforms have been cleared.
376 bool uniforms_cleared_
;
378 // This is different than uniform_infos_.size() because
379 // that is a sparce array.
383 scoped_ptr
<std::string
> log_info_
;
385 // attribute-location binding map from glBindAttribLocation() calls.
386 LocationMap bind_attrib_location_map_
;
388 // uniform-location binding map from glBindUniformLocationCHROMIUM() calls.
389 LocationMap bind_uniform_location_map_
;
391 std::vector
<std::string
> transform_feedback_varyings_
;
393 GLenum transform_feedback_buffer_mode_
;
396 // Tracks the Programs.
398 // NOTE: To support shared resources an instance of this class will
399 // need to be shared by multiple GLES2Decoders.
400 class GPU_EXPORT ProgramManager
{
402 explicit ProgramManager(ProgramCache
* program_cache
,
403 uint32 max_varying_vectors
);
406 // Must call before destruction.
407 void Destroy(bool have_context
);
409 // Creates a new program.
410 Program
* CreateProgram(GLuint client_id
, GLuint service_id
);
413 Program
* GetProgram(GLuint client_id
);
415 // Gets a client id for a given service id.
416 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
418 // Gets the shader cache
419 ProgramCache
* program_cache() const;
421 // Marks a program as deleted. If it is not used the program will be deleted.
422 void MarkAsDeleted(ShaderManager
* shader_manager
, Program
* program
);
424 // Marks a program as used.
425 void UseProgram(Program
* program
);
427 // Makes a program as unused. If deleted the program will be removed.
428 void UnuseProgram(ShaderManager
* shader_manager
, Program
* program
);
430 // Clears the uniforms for this program.
431 void ClearUniforms(Program
* program
);
433 // Returns true if prefix is invalid for gl.
434 static bool IsInvalidPrefix(const char* name
, size_t length
);
436 // Check if a Program is owned by this ProgramManager.
437 bool IsOwned(Program
* program
);
439 static int32
MakeFakeLocation(int32 index
, int32 element
);
441 uint32
max_varying_vectors() const {
442 return max_varying_vectors_
;
446 friend class Program
;
448 void StartTracking(Program
* program
);
449 void StopTracking(Program
* program
);
451 void RemoveProgramInfoIfUnused(
452 ShaderManager
* shader_manager
, Program
* program
);
454 // Info for each "successfully linked" program by service side program Id.
455 // TODO(gman): Choose a faster container.
456 typedef std::map
<GLuint
, scoped_refptr
<Program
> > ProgramMap
;
457 ProgramMap programs_
;
459 // Counts the number of Program allocated with 'this' as its manager.
460 // Allows to check no Program will outlive this.
461 unsigned int program_count_
;
465 // Used to clear uniforms.
466 std::vector
<uint8
> zero_
;
468 ProgramCache
* program_cache_
;
470 uint32 max_varying_vectors_
;
472 DISALLOW_COPY_AND_ASSIGN(ProgramManager
);
478 #endif // GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_