summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/program_manager.h
blob: ec4fa21023c1446c42d2bae4417e43a6e052cdf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_
#define GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_

#include <map>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/logging.h"
#include "gpu/command_buffer/service/gl_utils.h"

namespace gpu {
namespace gles2 {

// Tracks the Programs.
//
// NOTE: To support shared resources an instance of this class will
// need to be shared by multiple GLES2Decoders.
class ProgramManager {
 public:
  // This is used to track which attributes a particular program needs
  // so we can verify at glDrawXXX time that every attribute is either disabled
  // or if enabled that it points to a valid source.
  class ProgramInfo {
   public:
    struct UniformInfo {
      GLsizei size;
      GLenum type;
      std::string name;
      std::vector<GLint> element_locations;
    };
    struct VertexAttribInfo {
      GLsizei size;
      GLenum type;
      GLint location;
      std::string name;
    };

    typedef std::vector<UniformInfo> UniformInfoVector;
    typedef std::vector<VertexAttribInfo> AttribInfoVector;

    explicit ProgramInfo(GLuint program)
        : program_(program) {
    }

    void Update();

    const AttribInfoVector& GetAttribInfos() const {
      return attrib_infos_;
    }

    const VertexAttribInfo* GetAttribInfo(GLint index) const {
      return (static_cast<size_t>(index) < attrib_infos_.size()) ?
         &attrib_infos_[index] : NULL;
    }

    GLint GetAttribLocation(const std::string& name) const;

    const UniformInfo* GetUniformInfo(GLint index) const {
      return (static_cast<size_t>(index) < uniform_infos_.size()) ?
         &uniform_infos_[index] : NULL;
    }

    GLint GetUniformLocation(const std::string& name);

    bool GetUniformTypeByLocation(GLint location, GLenum* type) const;

   private:
    void SetNumAttributes(int num_attribs) {
      attrib_infos_.resize(num_attribs);
    }

    void SetAttributeInfo(
        GLint index, GLsizei size, GLenum type, GLint location,
        const std::string& name) {
      DCHECK(static_cast<unsigned>(index) < attrib_infos_.size());
      VertexAttribInfo& info = attrib_infos_[index];
      info.size = size;
      info.type = type;
      info.name = name;
      info.location = location;
    }

    void SetNumUniforms(int num_uniforms) {
      uniform_infos_.resize(num_uniforms);
    }

    void SetUniformInfo(
        GLint index, GLsizei size, GLenum type, GLint location,
        const std::string& name);

    AttribInfoVector attrib_infos_;

    // Uniform info by info.
    UniformInfoVector uniform_infos_;

    // The program this ProgramInfo is tracking.
    GLuint program_;
  };

  ProgramManager() { };

  // Creates a new program info.
  void CreateProgramInfo(GLuint program);

  // Gets a program info
  ProgramInfo* GetProgramInfo(GLuint program);

  // Deletes the program info for the given program.
  void RemoveProgramInfo(GLuint program);

  // Returns true if prefix is invalid for gl.
  static bool IsInvalidPrefix(const char* name, size_t length);

 private:
  // Info for each "successfully linked" program by service side program Id.
  // TODO(gman): Choose a faster container.
  typedef std::map<GLuint, ProgramInfo> ProgramInfoMap;
  ProgramInfoMap program_infos_;

  DISALLOW_COPY_AND_ASSIGN(ProgramManager);
};

}  // namespace gles2
}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_PROGRAM_MANAGER_H_