OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 if (m_vertexShader) { | 57 if (m_vertexShader) { |
58 m_vertexShader->onDetached(context3d); | 58 m_vertexShader->onDetached(context3d); |
59 m_vertexShader = 0; | 59 m_vertexShader = 0; |
60 } | 60 } |
61 if (m_fragmentShader) { | 61 if (m_fragmentShader) { |
62 m_fragmentShader->onDetached(context3d); | 62 m_fragmentShader->onDetached(context3d); |
63 m_fragmentShader = 0; | 63 m_fragmentShader = 0; |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 unsigned WebGLProgram::numActiveAttribLocations() | 67 unsigned WebGLProgram::numActiveAttribLocations(WebGLRenderingContext* context) |
68 { | 68 { |
69 cacheInfoIfNeeded(); | 69 cacheInfoIfNeeded(context); |
70 return m_activeAttribLocations.size(); | 70 return m_activeAttribLocations.size(); |
71 } | 71 } |
72 | 72 |
73 GC3Dint WebGLProgram::getActiveAttribLocation(GC3Duint index) | 73 GC3Dint WebGLProgram::getActiveAttribLocation(WebGLRenderingContext* context, GC
3Duint index) |
74 { | 74 { |
75 cacheInfoIfNeeded(); | 75 cacheInfoIfNeeded(context); |
76 if (index >= numActiveAttribLocations()) | 76 if (index >= numActiveAttribLocations(context)) |
77 return -1; | 77 return -1; |
78 return m_activeAttribLocations[index]; | 78 return m_activeAttribLocations[index]; |
79 } | 79 } |
80 | 80 |
81 bool WebGLProgram::isUsingVertexAttrib0() | 81 bool WebGLProgram::isUsingVertexAttrib0(WebGLRenderingContext* context) |
82 { | 82 { |
83 cacheInfoIfNeeded(); | 83 cacheInfoIfNeeded(context); |
84 for (unsigned ii = 0; ii < numActiveAttribLocations(); ++ii) { | 84 unsigned numLocations = numActiveAttribLocations(context); |
85 if (!getActiveAttribLocation(ii)) | 85 for (unsigned ii = 0; ii < numLocations; ++ii) { |
| 86 if (!getActiveAttribLocation(context, ii)) |
86 return true; | 87 return true; |
87 } | 88 } |
88 return false; | 89 return false; |
89 } | 90 } |
90 | 91 |
91 bool WebGLProgram::getLinkStatus() | 92 bool WebGLProgram::getLinkStatus(WebGLRenderingContext* context) |
92 { | 93 { |
93 cacheInfoIfNeeded(); | 94 cacheInfoIfNeeded(context); |
94 return m_linkStatus; | 95 return m_linkStatus; |
95 } | 96 } |
96 | 97 |
97 void WebGLProgram::setLinkStatus(bool status) | 98 void WebGLProgram::setLinkStatus(WebGLRenderingContext* context, bool status) |
98 { | 99 { |
99 cacheInfoIfNeeded(); | 100 cacheInfoIfNeeded(context); |
100 m_linkStatus = status; | 101 m_linkStatus = status; |
101 } | 102 } |
102 | 103 |
103 void WebGLProgram::increaseLinkCount() | 104 void WebGLProgram::increaseLinkCount() |
104 { | 105 { |
105 ++m_linkCount; | 106 ++m_linkCount; |
106 m_infoValid = false; | 107 m_infoValid = false; |
107 } | 108 } |
108 | 109 |
109 WebGLShader* WebGLProgram::getAttachedShader(GC3Denum type) | 110 WebGLShader* WebGLProgram::getAttachedShader(GC3Denum type) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 GC3Dint numAttribs = 0; | 166 GC3Dint numAttribs = 0; |
166 context3d->getProgramiv(object(), GraphicsContext3D::ACTIVE_ATTRIBUTES, &num
Attribs); | 167 context3d->getProgramiv(object(), GraphicsContext3D::ACTIVE_ATTRIBUTES, &num
Attribs); |
167 m_activeAttribLocations.resize(static_cast<size_t>(numAttribs)); | 168 m_activeAttribLocations.resize(static_cast<size_t>(numAttribs)); |
168 for (int i = 0; i < numAttribs; ++i) { | 169 for (int i = 0; i < numAttribs; ++i) { |
169 ActiveInfo info; | 170 ActiveInfo info; |
170 context3d->getActiveAttrib(object(), i, info); | 171 context3d->getActiveAttrib(object(), i, info); |
171 m_activeAttribLocations[i] = context3d->getAttribLocation(object(), info
.name.charactersWithNullTermination()); | 172 m_activeAttribLocations[i] = context3d->getAttribLocation(object(), info
.name.charactersWithNullTermination()); |
172 } | 173 } |
173 } | 174 } |
174 | 175 |
175 void WebGLProgram::cacheInfoIfNeeded() | 176 void WebGLProgram::cacheSamplerLocations(GraphicsContext3D* context3d) |
| 177 { |
| 178 m_samplerLocationToTextureUnit.clear(); |
| 179 |
| 180 GC3Dint numUniforms = 0; |
| 181 context3d->getProgramiv(object(), GraphicsContext3D::ACTIVE_UNIFORMS, &numUn
iforms); |
| 182 for (int i = 0; i < numUniforms; ++i) { |
| 183 ActiveInfo info; |
| 184 context3d->getActiveUniform(object(), i, info); |
| 185 if (info.type != GraphicsContext3D::SAMPLER_2D && info.type != GraphicsC
ontext3D::SAMPLER_CUBE) { |
| 186 continue; |
| 187 } |
| 188 GC3Dint location = context3d->getUniformLocation(object(), info.name.cha
ractersWithNullTermination()); |
| 189 // This loop makes a chain of locations for elements of a uniform array.
Given the location of any |
| 190 // element in the array you can get the next element's location from 'ne
xtLocation'. If 'nextLocation' |
| 191 // is -1 it's the last element. |
| 192 for (int j = 0; location >= 0 && j < info.size; ++j) { |
| 193 GC3Dint nextLocation = -1; |
| 194 if (j < info.size - 1) { |
| 195 String elementName(info.name + "[" + String::number(j + 1) + "]"
); |
| 196 nextLocation = context3d->getUniformLocation(object(), elementNa
me.charactersWithNullTermination()); |
| 197 } |
| 198 // Add one because HashMap can't store 0 |
| 199 m_samplerLocationToTextureUnit.add(location + 1, SamplerInfo(info.ty
pe, nextLocation)); |
| 200 location = nextLocation; |
| 201 } |
| 202 } |
| 203 } |
| 204 |
| 205 void WebGLProgram::cacheInfoIfNeeded(WebGLRenderingContext* context) |
176 { | 206 { |
177 if (m_infoValid) | 207 if (m_infoValid) |
178 return; | 208 return; |
179 | 209 |
180 if (!object()) | 210 if (!object()) |
181 return; | 211 return; |
182 | 212 |
183 GraphicsContext3D* context = getAGraphicsContext3D(); | 213 GraphicsContext3D* gc3d = context->graphicsContext3D(); |
184 if (!context) | 214 if (!context) |
185 return; | 215 return; |
186 GC3Dint linkStatus = 0; | 216 GC3Dint linkStatus = 0; |
187 context->getProgramiv(object(), GraphicsContext3D::LINK_STATUS, &linkStatus)
; | 217 gc3d->getProgramiv(object(), GraphicsContext3D::LINK_STATUS, &linkStatus); |
188 m_linkStatus = linkStatus; | 218 m_linkStatus = linkStatus; |
189 if (m_linkStatus) | 219 if (m_linkStatus) { |
190 cacheActiveAttribLocations(context); | 220 cacheActiveAttribLocations(gc3d); |
| 221 cacheSamplerLocations(gc3d); |
| 222 } |
191 m_infoValid = true; | 223 m_infoValid = true; |
192 } | 224 } |
193 | 225 |
| 226 void WebGLProgram::conditionallyRecordTextureUnitsAssignedToSamplers(WebGLRender
ingContext* context, GC3Dint location, GC3Dsizei size, const GC3Dint* v) |
| 227 { |
| 228 cacheInfoIfNeeded(context); |
| 229 |
| 230 // Locations are stored such that each element of a uniform array has a "nex
tLocation" field |
| 231 // that gives the location of the next element in the array. So, given the l
ocation of any element |
| 232 // you can fill out subsequent elements by following 'nextLocation' until th
e nextLocation is < 0. |
| 233 for (; location >= 0 && size > 0; --size) { |
| 234 // Add one because HashMap can't store 0. |
| 235 SamplerTextureUnitMap::iterator it = m_samplerLocationToTextureUnit.find
(location + 1); |
| 236 if (it == m_samplerLocationToTextureUnit.end()) { |
| 237 return; |
| 238 } |
| 239 it->value.unit = *v++; |
| 240 location = it->value.nextLocation; |
| 241 } |
194 } | 242 } |
| 243 |
| 244 } |
OLD | NEW |