forked from CesiumGS/cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertexArray.js
333 lines (299 loc) · 11.9 KB
/
VertexArray.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*global define*/
define([
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/ComponentDatatype'
], function(
defaultValue,
defined,
defineProperties,
destroyObject,
DeveloperError,
ComponentDatatype) {
"use strict";
function addAttribute(attributes, attribute, index) {
var hasVertexBuffer = defined(attribute.vertexBuffer);
var hasValue = defined(attribute.value);
var componentsPerAttribute = attribute.value ? attribute.value.length : attribute.componentsPerAttribute;
//>>includeStart('debug', pragmas.debug);
if (!hasVertexBuffer && !hasValue) {
throw new DeveloperError('attribute must have a vertexBuffer or a value.');
}
if (hasVertexBuffer && hasValue) {
throw new DeveloperError('attribute cannot have both a vertexBuffer and a value. It must have either a vertexBuffer property defining per-vertex data or a value property defining data for all vertices.');
}
if ((componentsPerAttribute !== 1) &&
(componentsPerAttribute !== 2) &&
(componentsPerAttribute !== 3) &&
(componentsPerAttribute !== 4)) {
if (hasValue) {
throw new DeveloperError('attribute.value.length must be in the range [1, 4].');
}
throw new DeveloperError('attribute.componentsPerAttribute must be in the range [1, 4].');
}
if (defined(attribute.componentDatatype) && !ComponentDatatype.validate(attribute.componentDatatype)) {
throw new DeveloperError('attribute must have a valid componentDatatype or not specify it.');
}
if (defined(attribute.strideInBytes) && (attribute.strideInBytes > 255)) {
// WebGL limit. Not in GL ES.
throw new DeveloperError('attribute must have a strideInBytes less than or equal to 255 or not specify it.');
}
//>>includeEnd('debug');
// Shallow copy the attribute; we do not want to copy the vertex buffer.
var attr = {
index : defaultValue(attribute.index, index),
enabled : defaultValue(attribute.enabled, true),
vertexBuffer : attribute.vertexBuffer,
value : hasValue ? attribute.value.slice(0) : undefined,
componentsPerAttribute : componentsPerAttribute,
componentDatatype : defaultValue(attribute.componentDatatype, ComponentDatatype.FLOAT),
normalize : defaultValue(attribute.normalize, false),
offsetInBytes : defaultValue(attribute.offsetInBytes, 0),
strideInBytes : defaultValue(attribute.strideInBytes, 0)
};
if (hasVertexBuffer) {
// Common case: vertex buffer for per-vertex data
attr.vertexAttrib = function(gl) {
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer._getBuffer());
gl.vertexAttribPointer(this.index, this.componentsPerAttribute, this.componentDatatype.value, this.normalize, this.strideInBytes, this.offsetInBytes);
gl.enableVertexAttribArray(this.index);
};
attr.disableVertexAttribArray = function(gl) {
gl.disableVertexAttribArray(this.index);
};
} else {
// Less common case: value array for the same data for each vertex
switch (attr.componentsPerAttribute) {
case 1:
attr.vertexAttrib = function(gl) {
gl.vertexAttrib1fv(this.index, this.value);
};
break;
case 2:
attr.vertexAttrib = function(gl) {
gl.vertexAttrib2fv(this.index, this.value);
};
break;
case 3:
attr.vertexAttrib = function(gl) {
gl.vertexAttrib3fv(this.index, this.value);
};
break;
case 4:
attr.vertexAttrib = function(gl) {
gl.vertexAttrib4fv(this.index, this.value);
};
break;
}
attr.disableVertexAttribArray = function(gl) {
};
}
attributes.push(attr);
}
function bind(gl, attributes, indexBuffer) {
for ( var i = 0; i < attributes.length; ++i) {
var attribute = attributes[i];
if (attribute.enabled) {
attribute.vertexAttrib(gl);
}
}
if (defined(indexBuffer)) {
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer._getBuffer());
}
}
/**
* DOC_TBA
*
* @alias VertexArray
*
* @internalConstructor
*
* @see {@link Context#createVertexArray}
* @see {@link Context#createVertexArrayFromGeometry}
*/
var VertexArray = function(gl, vertexArrayObject, attributes, indexBuffer) {
//>>includeStart('debug', pragmas.debug
if (!defined(attributes)) {
throw new DeveloperError('attributes is required.');
}
//>>includeEnd('debug');
var i;
var vaAttributes = [];
var numberOfVertices = 1; // if every attribute is backed by a single value
for (i = 0; i < attributes.length; ++i) {
addAttribute(vaAttributes, attributes[i], i);
}
for (i = 0; i < vaAttributes.length; ++i) {
var attribute = vaAttributes[i];
if (defined(attribute.vertexBuffer)) {
// This assumes that each vertex buffer in the vertex array has the same number of vertices.
var bytes = attribute.strideInBytes || (attribute.componentsPerAttribute * attribute.componentDatatype.sizeInBytes);
numberOfVertices = attribute.vertexBuffer.sizeInBytes / bytes;
break;
}
}
// Verify all attribute names are unique
var uniqueIndices = {};
for ( var j = 0; j < vaAttributes.length; ++j) {
var index = vaAttributes[j].index;
if (uniqueIndices[index]) {
throw new DeveloperError('Index ' + index + ' is used by more than one attribute.');
}
uniqueIndices[index] = true;
}
var vao;
// Setup VAO if extension is supported
if (defined(vertexArrayObject)) {
vao = vertexArrayObject.createVertexArrayOES();
vertexArrayObject.bindVertexArrayOES(vao);
bind(gl, vaAttributes, indexBuffer);
vertexArrayObject.bindVertexArrayOES(null);
}
/**
* @readonly
* @private
*/
this.numberOfVertices = numberOfVertices;
this._gl = gl;
this._vaoExtension = vertexArrayObject;
this._vao = vao;
this._attributes = vaAttributes;
this._indexBuffer = indexBuffer;
};
defineProperties(VertexArray.prototype, {
/**
* DOC_TBA
* @memberof VertexArray.prototype
* @type {Number}
*/
numberOfAttributes : {
get : function() {
return this._attributes.length;
}
},
/**
* DOC_TBA
* @memberof VertexArray.prototype
* @type {Buffer}
*/
indexBuffer : {
get : function() {
return this._indexBuffer;
}
}
});
/**
* DOC_TBA
*
* index is the location in the array of attributes, not the index property of an attribute.
*
* @memberof VertexArray
*
* @exception {DeveloperError} This vertex array was destroyed, i.e., destroy() was called.
*/
VertexArray.prototype.getAttribute = function(index) {
//>>includeStart('debug', pragmas.debug);
if (!defined(index)) {
throw new DeveloperError('index is required.');
}
//>>includeEnd('debug');
return this._attributes[index];
};
VertexArray.prototype._bind = function() {
if (defined(this._vao)) {
this._vaoExtension.bindVertexArrayOES(this._vao);
} else {
bind(this._gl, this._attributes, this._indexBuffer);
}
};
VertexArray.prototype._unBind = function() {
if (defined(this._vao)) {
this._vaoExtension.bindVertexArrayOES(null);
} else {
var attributes = this._attributes;
var gl = this._gl;
for ( var i = 0; i < attributes.length; ++i) {
var attribute = attributes[i];
if (attribute.enabled) {
attribute.disableVertexAttribArray(gl);
}
}
if (this._indexBuffer) {
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}
}
};
/**
* Returns true if this object was destroyed; otherwise, false.
* <br /><br />
* If this object was destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
*
* @memberof VertexArray
*
* @returns {Boolean} True if this object was destroyed; otherwise, false.
*
* @see VertexArray#destroy
*/
VertexArray.prototype.isDestroyed = function() {
return false;
};
/**
* Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
* release of WebGL resources, instead of relying on the garbage collector to destroy this object.
* <br /><br />
* Only call this if the vertex array owns the vertex buffers referenced by the attributes and owns its
* index buffer; otherwise, the owner of the buffers is responsible for destroying them. A vertex or
* index buffer is only destroyed if it's <code>getVertexArrayDestroyable</code> function returns
* <code>true</code> (the default). This allows combining destroyable and non-destroyable buffers
* in the same vertex array.
* <br /><br />
* Once an object is destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
* assign the return value (<code>undefined</code>) to the object as done in the example.
*
* @memberof VertexArray
*
* @returns {undefined}
*
* @exception {DeveloperError} This vertex array was destroyed, i.e., destroy() was called.
*
* @see VertexArray#isDestroyed
* @see Buffer#getVertexArrayDestroyable
* @see <a href='http://www.khronos.org/opengles/sdk/2.0/docs/man/glDeleteBuffers.xml'>glDeleteBuffers</a>
*
* @example
* // Destroying the vertex array implicitly calls destroy for each of its vertex
* // buffers and its index buffer.
* var vertexBuffer = context.createVertexBuffer(new Float32Array([0, 0, 0]),
* Cesium.BufferUsage.STATIC_DRAW);
* var vertexArray = context.createVertexArray({
* vertexBuffer : vertexBuffer,
* componentsPerAttribute : 3
* });
* // ...
* vertexArray = vertexArray.destroy();
* // Calling vertexBuffer.destroy() would throw DeveloperError at this point.
*/
VertexArray.prototype.destroy = function() {
var attributes = this._attributes;
for ( var i = 0; i < attributes.length; ++i) {
var vertexBuffer = attributes[i].vertexBuffer;
if (defined(vertexBuffer) && !vertexBuffer.isDestroyed() && vertexBuffer.vertexArrayDestroyable) {
vertexBuffer.destroy();
}
}
var indexBuffer = this._indexBuffer;
if (defined(indexBuffer) && !indexBuffer.isDestroyed() && indexBuffer.vertexArrayDestroyable) {
indexBuffer.destroy();
}
if (defined(this._vao)) {
this._vaoExtension.deleteVertexArrayOES(this._vao);
}
return destroyObject(this);
};
return VertexArray;
});