OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
piman
2015/01/30 21:55:39
Why separate this into another file?
Zhenyao Mo
2015/01/30 22:11:38
Because I am planning to add UniformBlock related
piman
2015/01/30 22:18:23
I disagree :) If a class is defined in foo.h, the
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "gpu/command_buffer/client/program_info_manager.h" | |
6 | |
7 namespace { | |
8 | |
9 template<typename T> static T LocalGetAs( | |
10 const std::vector<int8>& data, uint32 offset, size_t size) { | |
11 const int8* p = &data[0] + offset; | |
12 if (offset + size > data.size()) { | |
13 NOTREACHED(); | |
14 return NULL; | |
15 } | |
16 return static_cast<T>(static_cast<const void*>(p)); | |
17 } | |
18 | |
19 } // namespace anonymous | |
20 | |
21 namespace gpu { | |
22 namespace gles2 { | |
23 | |
24 ProgramInfoManager::Program::UniformInfo::UniformInfo( | |
25 GLsizei _size, GLenum _type, const std::string& _name) | |
26 : size(_size), | |
27 type(_type), | |
28 name(_name) { | |
29 is_array = (!name.empty() && name[name.size() - 1] == ']'); | |
30 DCHECK(!(size > 1 && !is_array)); | |
31 } | |
32 | |
33 ProgramInfoManager::Program::Program() | |
34 : cached_(false), | |
35 max_attrib_name_length_(0), | |
36 max_uniform_name_length_(0), | |
37 link_status_(false) { | |
38 } | |
39 | |
40 // TODO(gman): Add a faster lookup. | |
41 GLint ProgramInfoManager::Program::GetAttribLocation( | |
42 const std::string& name) const { | |
43 for (GLuint ii = 0; ii < attrib_infos_.size(); ++ii) { | |
44 const VertexAttrib& info = attrib_infos_[ii]; | |
45 if (info.name == name) { | |
46 return info.location; | |
47 } | |
48 } | |
49 return -1; | |
50 } | |
51 | |
52 GLint ProgramInfoManager::Program::GetUniformLocation( | |
53 const std::string& name) const { | |
54 bool getting_array_location = false; | |
55 size_t open_pos = std::string::npos; | |
56 int index = 0; | |
57 if (!GLES2Util::ParseUniformName( | |
58 name, &open_pos, &index, &getting_array_location)) { | |
59 return -1; | |
60 } | |
61 for (GLuint ii = 0; ii < uniform_infos_.size(); ++ii) { | |
62 const UniformInfo& info = uniform_infos_[ii]; | |
63 if (info.name == name || | |
64 (info.is_array && | |
65 info.name.compare(0, info.name.size() - 3, name) == 0)) { | |
66 return info.element_locations[0]; | |
67 } else if (getting_array_location && info.is_array) { | |
68 // Look for an array specification. | |
69 size_t open_pos_2 = info.name.find_last_of('['); | |
70 if (open_pos_2 == open_pos && | |
71 name.compare(0, open_pos, info.name, 0, open_pos) == 0) { | |
72 if (index >= 0 && index < info.size) { | |
73 return info.element_locations[index]; | |
74 } | |
75 } | |
76 } | |
77 } | |
78 return -1; | |
79 } | |
80 | |
81 GLint ProgramInfoManager::Program::GetFragDataLocation( | |
82 const std::string& name) const { | |
83 base::hash_map<std::string, GLint>::const_iterator iter = | |
84 frag_data_locations_.find(name); | |
85 if (iter == frag_data_locations_.end()) | |
86 return -1; | |
87 return iter->second; | |
88 } | |
89 | |
90 void ProgramInfoManager::Program::CacheFragDataLocation( | |
91 const std::string& name, GLint loc) { | |
92 frag_data_locations_[name] = loc; | |
93 } | |
94 | |
95 bool ProgramInfoManager::Program::GetProgramiv( | |
96 GLenum pname, GLint* params) { | |
97 switch (pname) { | |
98 case GL_LINK_STATUS: | |
99 *params = link_status_; | |
100 return true; | |
101 case GL_ACTIVE_ATTRIBUTES: | |
102 *params = attrib_infos_.size(); | |
103 return true; | |
104 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH: | |
105 *params = max_attrib_name_length_; | |
106 return true; | |
107 case GL_ACTIVE_UNIFORMS: | |
108 *params = uniform_infos_.size(); | |
109 return true; | |
110 case GL_ACTIVE_UNIFORM_MAX_LENGTH: | |
111 *params = max_uniform_name_length_; | |
112 return true; | |
113 default: | |
114 break; | |
115 } | |
116 return false; | |
117 } | |
118 | |
119 void ProgramInfoManager::Program::Update( | |
120 GLES2Implementation* gl, | |
121 GLuint program, | |
122 const std::vector<int8>& result) { | |
123 if (cached_) { | |
124 return; | |
125 } | |
126 if (result.empty()) { | |
127 // This should only happen on a lost context. | |
128 return; | |
129 } | |
130 DCHECK_GE(result.size(), sizeof(ProgramInfoHeader)); | |
131 const ProgramInfoHeader* header = LocalGetAs<const ProgramInfoHeader*>( | |
132 result, 0, sizeof(header)); | |
133 link_status_ = header->link_status != 0; | |
134 if (!link_status_) { | |
135 return; | |
136 } | |
137 attrib_infos_.clear(); | |
138 uniform_infos_.clear(); | |
139 frag_data_locations_.clear(); | |
140 max_attrib_name_length_ = 0; | |
141 max_uniform_name_length_ = 0; | |
142 const ProgramInput* inputs = LocalGetAs<const ProgramInput*>( | |
143 result, sizeof(*header), | |
144 sizeof(ProgramInput) * (header->num_attribs + header->num_uniforms)); | |
145 const ProgramInput* input = inputs; | |
146 for (uint32 ii = 0; ii < header->num_attribs; ++ii) { | |
147 const int32* location = LocalGetAs<const int32*>( | |
148 result, input->location_offset, sizeof(int32)); | |
149 const char* name_buf = LocalGetAs<const char*>( | |
150 result, input->name_offset, input->name_length); | |
151 std::string name(name_buf, input->name_length); | |
152 attrib_infos_.push_back( | |
153 VertexAttrib(input->size, input->type, name, *location)); | |
154 max_attrib_name_length_ = std::max( | |
155 static_cast<GLsizei>(name.size() + 1), max_attrib_name_length_); | |
156 ++input; | |
157 } | |
158 for (uint32 ii = 0; ii < header->num_uniforms; ++ii) { | |
159 const int32* locations = LocalGetAs<const int32*>( | |
160 result, input->location_offset, sizeof(int32) * input->size); | |
161 const char* name_buf = LocalGetAs<const char*>( | |
162 result, input->name_offset, input->name_length); | |
163 std::string name(name_buf, input->name_length); | |
164 UniformInfo info(input->size, input->type, name); | |
165 max_uniform_name_length_ = std::max( | |
166 static_cast<GLsizei>(name.size() + 1), max_uniform_name_length_); | |
167 for (int32 jj = 0; jj < input->size; ++jj) { | |
168 info.element_locations.push_back(locations[jj]); | |
169 } | |
170 uniform_infos_.push_back(info); | |
171 ++input; | |
172 } | |
173 DCHECK_EQ(header->num_attribs + header->num_uniforms, | |
174 static_cast<uint32>(input - inputs)); | |
175 cached_ = true; | |
176 } | |
177 | |
178 } // namespace gles2 | |
179 } // namespace gpu | |
180 | |
OLD | NEW |