-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathVertexArray.js
More file actions
949 lines (859 loc) · 30.4 KB
/
VertexArray.js
File metadata and controls
949 lines (859 loc) · 30.4 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
import Check from "../Core/Check.js";
import ComponentDatatype from "../Core/ComponentDatatype.js";
import Frozen from "../Core/Frozen.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import Geometry from "../Core/Geometry.js";
import IndexDatatype from "../Core/IndexDatatype.js";
import CesiumMath from "../Core/Math.js";
import RuntimeError from "../Core/RuntimeError.js";
import Buffer from "./Buffer.js";
import BufferUsage from "./BufferUsage.js";
import ContextLimits from "./ContextLimits.js";
import AttributeType from "../Scene/AttributeType.js";
import assert from "../Core/assert.js";
/** @import {TypedArray, TypedArrayConstructor} from "../Core/globalTypes.js"; */
function addAttribute(attributes, attribute, index, context) {
const hasVertexBuffer = defined(attribute.vertexBuffer);
const hasValue = defined(attribute.value);
const 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.",
);
}
if (
defined(attribute.instanceDivisor) &&
attribute.instanceDivisor > 0 &&
!context.instancedArrays
) {
throw new DeveloperError("instanced arrays is not supported");
}
if (defined(attribute.instanceDivisor) && attribute.instanceDivisor < 0) {
throw new DeveloperError(
"attribute must have an instanceDivisor greater than or equal to zero",
);
}
if (defined(attribute.instanceDivisor) && hasValue) {
throw new DeveloperError(
"attribute cannot have have an instanceDivisor if it is not backed by a buffer",
);
}
if (
defined(attribute.instanceDivisor) &&
attribute.instanceDivisor > 0 &&
attribute.index === 0
) {
throw new DeveloperError(
"attribute zero cannot have an instanceDivisor greater than 0",
);
}
//>>includeEnd('debug');
// Shallow copy the attribute; we do not want to copy the vertex buffer.
const attr = {
index: attribute.index ?? index,
enabled: attribute.enabled ?? true,
vertexBuffer: attribute.vertexBuffer,
value: hasValue ? attribute.value.slice(0) : undefined,
componentsPerAttribute: componentsPerAttribute,
componentDatatype: attribute.componentDatatype ?? ComponentDatatype.FLOAT,
normalize: attribute.normalize ?? false,
offsetInBytes: attribute.offsetInBytes ?? 0,
strideInBytes: attribute.strideInBytes ?? 0,
instanceDivisor: attribute.instanceDivisor ?? 0,
};
if (hasVertexBuffer) {
// Common case: vertex buffer for per-vertex data
attr.vertexAttrib = function (gl) {
const index = this.index;
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer._getBuffer());
gl.vertexAttribPointer(
index,
this.componentsPerAttribute,
this.componentDatatype,
this.normalize,
this.strideInBytes,
this.offsetInBytes,
);
gl.enableVertexAttribArray(index);
if (this.instanceDivisor > 0) {
context.glVertexAttribDivisor(index, this.instanceDivisor);
context._vertexAttribDivisors[index] = this.instanceDivisor;
context._previousDrawInstanced = true;
}
};
attr.disableVertexAttribArray = function (gl) {
gl.disableVertexAttribArray(this.index);
if (this.instanceDivisor > 0) {
context.glVertexAttribDivisor(index, 0);
}
};
} 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 (let i = 0; i < attributes.length; ++i) {
const attribute = attributes[i];
if (attribute.enabled) {
attribute.vertexAttrib(gl);
}
}
if (defined(indexBuffer)) {
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer._getBuffer());
}
}
/**
* Creates a vertex array, which defines the attributes making up a vertex, and contains an optional index buffer
* to select vertices for rendering. Attributes are defined using object literals as shown in Example 1 below.
*
* @param {object} options Object with the following properties:
* @param {Context} options.context The context in which the VertexArray gets created.
* @param {object[]} options.attributes An array of attributes.
* @param {IndexBuffer} [options.indexBuffer] An optional index buffer.
*
* @returns {VertexArray} The vertex array, ready for use with drawing.
*
* @exception {DeveloperError} Attribute must have a <code>vertexBuffer</code>.
* @exception {DeveloperError} Attribute must have a <code>componentsPerAttribute</code>.
* @exception {DeveloperError} Attribute must have a valid <code>componentDatatype</code> or not specify it.
* @exception {DeveloperError} Attribute must have a <code>strideInBytes</code> less than or equal to 255 or not specify it.
* @exception {DeveloperError} Index n is used by more than one attribute.
*
*
* @example
* // Example 1. Create a vertex array with vertices made up of three floating point
* // values, e.g., a position, from a single vertex buffer. No index buffer is used.
* const positionBuffer = Buffer.createVertexBuffer({
* context : context,
* sizeInBytes : 12,
* usage : BufferUsage.STATIC_DRAW
* });
* const attributes = [
* {
* index : 0,
* enabled : true,
* vertexBuffer : positionBuffer,
* componentsPerAttribute : 3,
* componentDatatype : ComponentDatatype.FLOAT,
* normalize : false,
* offsetInBytes : 0,
* strideInBytes : 0 // tightly packed
* instanceDivisor : 0 // not instanced
* }
* ];
* const va = new VertexArray({
* context : context,
* attributes : attributes
* });
*
* @example
* // Example 2. Create a vertex array with vertices from two different vertex buffers.
* // Each vertex has a three-component position and three-component normal.
* const positionBuffer = Buffer.createVertexBuffer({
* context : context,
* sizeInBytes : 12,
* usage : BufferUsage.STATIC_DRAW
* });
* const normalBuffer = Buffer.createVertexBuffer({
* context : context,
* sizeInBytes : 12,
* usage : BufferUsage.STATIC_DRAW
* });
* const attributes = [
* {
* index : 0,
* vertexBuffer : positionBuffer,
* componentsPerAttribute : 3,
* componentDatatype : ComponentDatatype.FLOAT
* },
* {
* index : 1,
* vertexBuffer : normalBuffer,
* componentsPerAttribute : 3,
* componentDatatype : ComponentDatatype.FLOAT
* }
* ];
* const va = new VertexArray({
* context : context,
* attributes : attributes
* });
*
* @example
* // Example 3. Creates the same vertex layout as Example 2 using a single
* // vertex buffer, instead of two.
* const buffer = Buffer.createVertexBuffer({
* context : context,
* sizeInBytes : 24,
* usage : BufferUsage.STATIC_DRAW
* });
* const attributes = [
* {
* vertexBuffer : buffer,
* componentsPerAttribute : 3,
* componentDatatype : ComponentDatatype.FLOAT,
* offsetInBytes : 0,
* strideInBytes : 24
* },
* {
* vertexBuffer : buffer,
* componentsPerAttribute : 3,
* componentDatatype : ComponentDatatype.FLOAT,
* normalize : true,
* offsetInBytes : 12,
* strideInBytes : 24
* }
* ];
* const va = new VertexArray({
* context : context,
* attributes : attributes
* });
*
* @see Buffer#createVertexBuffer
* @see Buffer#createIndexBuffer
* @see Context#draw
*
* @private
*/
function VertexArray(options) {
options = options ?? Frozen.EMPTY_OBJECT;
//>>includeStart('debug', pragmas.debug);
Check.defined("options.context", options.context);
Check.defined("options.attributes", options.attributes);
//>>includeEnd('debug');
const context = options.context;
const gl = context._gl;
const attributes = options.attributes;
const indexBuffer = options.indexBuffer;
let i;
const vaAttributes = [];
let numberOfVertices = 1; // if every attribute is backed by a single value
let hasInstancedAttributes = false;
let hasConstantAttributes = false;
let length = attributes.length;
for (i = 0; i < length; ++i) {
addAttribute(vaAttributes, attributes[i], i, context);
}
length = vaAttributes.length;
for (i = 0; i < length; ++i) {
const attribute = vaAttributes[i];
if (defined(attribute.vertexBuffer) && attribute.instanceDivisor === 0) {
// This assumes that each vertex buffer in the vertex array has the same number of vertices.
const bytes =
attribute.strideInBytes ||
attribute.componentsPerAttribute *
ComponentDatatype.getSizeInBytes(attribute.componentDatatype);
numberOfVertices = attribute.vertexBuffer.sizeInBytes / bytes;
break;
}
}
for (i = 0; i < length; ++i) {
if (vaAttributes[i].instanceDivisor > 0) {
hasInstancedAttributes = true;
}
if (defined(vaAttributes[i].value)) {
hasConstantAttributes = true;
}
}
//>>includeStart('debug', pragmas.debug);
// Verify all attribute names are unique
const uniqueIndices = {};
for (i = 0; i < length; ++i) {
const index = vaAttributes[i].index;
if (uniqueIndices[index]) {
throw new DeveloperError(
`Index ${index} is used by more than one attribute.`,
);
}
uniqueIndices[index] = true;
}
//>>includeEnd('debug');
let vao;
// Setup VAO if supported
if (context.vertexArrayObject) {
vao = context.glCreateVertexArray();
context.glBindVertexArray(vao);
bind(gl, vaAttributes, indexBuffer);
context.glBindVertexArray(null);
}
this._numberOfVertices = numberOfVertices;
this._hasInstancedAttributes = hasInstancedAttributes;
this._hasConstantAttributes = hasConstantAttributes;
this._context = context;
this._gl = gl;
this._vao = vao;
this._attributes = vaAttributes;
this._indexBuffer = indexBuffer;
}
function computeNumberOfVertices(attribute) {
return attribute.values.length / attribute.componentsPerAttribute;
}
function computeAttributeSizeInBytes(attribute) {
return (
ComponentDatatype.getSizeInBytes(attribute.componentDatatype) *
attribute.componentsPerAttribute
);
}
function interleaveAttributes(attributes) {
let j;
let name;
let attribute;
// Extract attribute names.
const names = [];
for (name in attributes) {
// Attribute needs to have per-vertex values; not a constant value for all vertices.
if (
attributes.hasOwnProperty(name) &&
defined(attributes[name]) &&
defined(attributes[name].values)
) {
names.push(name);
if (attributes[name].componentDatatype === ComponentDatatype.DOUBLE) {
attributes[name].componentDatatype = ComponentDatatype.FLOAT;
attributes[name].values = ComponentDatatype.createTypedArray(
ComponentDatatype.FLOAT,
attributes[name].values,
);
}
}
}
// Validation. Compute number of vertices.
let numberOfVertices;
const namesLength = names.length;
if (namesLength > 0) {
numberOfVertices = computeNumberOfVertices(attributes[names[0]]);
for (j = 1; j < namesLength; ++j) {
const currentNumberOfVertices = computeNumberOfVertices(
attributes[names[j]],
);
if (currentNumberOfVertices !== numberOfVertices) {
throw new RuntimeError(
`${
"Each attribute list must have the same number of vertices. " +
"Attribute "
}${names[j]} has a different number of vertices ` +
`(${currentNumberOfVertices.toString()})` +
` than attribute ${names[0]} (${numberOfVertices.toString()}).`,
);
}
}
}
// Sort attributes by the size of their components. From left to right, a vertex stores floats, shorts, and then bytes.
names.sort(function (left, right) {
return (
ComponentDatatype.getSizeInBytes(attributes[right].componentDatatype) -
ComponentDatatype.getSizeInBytes(attributes[left].componentDatatype)
);
});
// Compute sizes and strides.
let vertexSizeInBytes = 0;
const offsetsInBytes = {};
for (j = 0; j < namesLength; ++j) {
name = names[j];
attribute = attributes[name];
offsetsInBytes[name] = vertexSizeInBytes;
vertexSizeInBytes += computeAttributeSizeInBytes(attribute);
}
if (vertexSizeInBytes > 0) {
// Pad each vertex to be a multiple of the largest component datatype so each
// attribute can be addressed using typed arrays.
const maxComponentSizeInBytes = ComponentDatatype.getSizeInBytes(
attributes[names[0]].componentDatatype,
); // Sorted large to small
const remainder = vertexSizeInBytes % maxComponentSizeInBytes;
if (remainder !== 0) {
vertexSizeInBytes += maxComponentSizeInBytes - remainder;
}
// Total vertex buffer size in bytes, including per-vertex padding.
const vertexBufferSizeInBytes = numberOfVertices * vertexSizeInBytes;
// Create array for interleaved vertices. Each attribute has a different view (pointer) into the array.
const buffer = new ArrayBuffer(vertexBufferSizeInBytes);
const views = {};
for (j = 0; j < namesLength; ++j) {
name = names[j];
const sizeInBytes = ComponentDatatype.getSizeInBytes(
attributes[name].componentDatatype,
);
views[name] = {
pointer: ComponentDatatype.createTypedArray(
attributes[name].componentDatatype,
buffer,
),
index: offsetsInBytes[name] / sizeInBytes, // Offset in ComponentType
strideInComponentType: vertexSizeInBytes / sizeInBytes,
};
}
// Copy attributes into one interleaved array.
// PERFORMANCE_IDEA: Can we optimize these loops?
for (j = 0; j < numberOfVertices; ++j) {
for (let n = 0; n < namesLength; ++n) {
name = names[n];
attribute = attributes[name];
const values = attribute.values;
const view = views[name];
const pointer = view.pointer;
const numberOfComponents = attribute.componentsPerAttribute;
for (let k = 0; k < numberOfComponents; ++k) {
pointer[view.index + k] = values[j * numberOfComponents + k];
}
view.index += view.strideInComponentType;
}
}
return {
buffer: buffer,
offsetsInBytes: offsetsInBytes,
vertexSizeInBytes: vertexSizeInBytes,
};
}
// No attributes to interleave.
return undefined;
}
/**
* Creates a vertex array from a geometry. A geometry contains vertex attributes and optional index data
* in system memory, whereas a vertex array contains vertex buffers and an optional index buffer in WebGL
* memory for use with rendering.
* <br /><br />
* The <code>geometry</code> argument should use the standard layout like the geometry returned by {@link BoxGeometry}.
* <br /><br />
* <code>options</code> can have four properties:
* <ul>
* <li><code>geometry</code>: The source geometry containing data used to create the vertex array.</li>
* <li><code>attributeLocations</code>: An object that maps geometry attribute names to vertex shader attribute locations.</li>
* <li><code>bufferUsage</code>: The expected usage pattern of the vertex array's buffers. On some WebGL implementations, this can significantly affect performance. See {@link BufferUsage}. Default: <code>BufferUsage.DYNAMIC_DRAW</code>.</li>
* <li><code>interleave</code>: Determines if all attributes are interleaved in a single vertex buffer or if each attribute is stored in a separate vertex buffer. Default: <code>false</code>.</li>
* </ul>
* <br />
* If <code>options</code> is not specified or the <code>geometry</code> contains no data, the returned vertex array is empty.
*
* @param {object} options An object defining the geometry, attribute indices, buffer usage, and vertex layout used to create the vertex array.
*
* @exception {RuntimeError} Each attribute list must have the same number of vertices.
* @exception {DeveloperError} The geometry must have zero or one index lists.
* @exception {DeveloperError} Index n is used by more than one attribute.
*
*
* @example
* // Example 1. Creates a vertex array for rendering a box. The default dynamic draw
* // usage is used for the created vertex and index buffer. The attributes are not
* // interleaved by default.
* const geometry = new BoxGeometry();
* const va = VertexArray.fromGeometry({
* context : context,
* geometry : geometry,
* attributeLocations : GeometryPipeline.createAttributeLocations(geometry),
* });
*
* @example
* // Example 2. Creates a vertex array with interleaved attributes in a
* // single vertex buffer. The vertex and index buffer have static draw usage.
* const va = VertexArray.fromGeometry({
* context : context,
* geometry : geometry,
* attributeLocations : GeometryPipeline.createAttributeLocations(geometry),
* bufferUsage : BufferUsage.STATIC_DRAW,
* interleave : true
* });
*
* @example
* // Example 3. When the caller destroys the vertex array, it also destroys the
* // attached vertex buffer(s) and index buffer.
* va = va.destroy();
*
* @see Buffer#createVertexBuffer
* @see Buffer#createIndexBuffer
* @see GeometryPipeline.createAttributeLocations
* @see ShaderProgram
*/
VertexArray.fromGeometry = function (options) {
options = options ?? Frozen.EMPTY_OBJECT;
//>>includeStart('debug', pragmas.debug);
Check.defined("options.context", options.context);
//>>includeEnd('debug');
const context = options.context;
const geometry = options.geometry ?? Frozen.EMPTY_OBJECT;
const bufferUsage = options.bufferUsage ?? BufferUsage.DYNAMIC_DRAW;
const attributeLocations = options.attributeLocations ?? Frozen.EMPTY_OBJECT;
const interleave = options.interleave ?? false;
const createdVAAttributes = options.vertexArrayAttributes;
let name;
let attribute;
let vertexBuffer;
const vaAttributes = defined(createdVAAttributes) ? createdVAAttributes : [];
const attributes = geometry.attributes;
if (interleave) {
// Use a single vertex buffer with interleaved vertices.
const interleavedAttributes = interleaveAttributes(attributes);
if (defined(interleavedAttributes)) {
vertexBuffer = Buffer.createVertexBuffer({
context: context,
typedArray: interleavedAttributes.buffer,
usage: bufferUsage,
});
const offsetsInBytes = interleavedAttributes.offsetsInBytes;
const strideInBytes = interleavedAttributes.vertexSizeInBytes;
for (name in attributes) {
if (attributes.hasOwnProperty(name) && defined(attributes[name])) {
attribute = attributes[name];
if (defined(attribute.values)) {
// Common case: per-vertex attributes
vaAttributes.push({
index: attributeLocations[name],
vertexBuffer: vertexBuffer,
componentDatatype: attribute.componentDatatype,
componentsPerAttribute: attribute.componentsPerAttribute,
normalize: attribute.normalize,
offsetInBytes: offsetsInBytes[name],
strideInBytes: strideInBytes,
});
} else {
// Constant attribute for all vertices
vaAttributes.push({
index: attributeLocations[name],
value: attribute.value,
componentDatatype: attribute.componentDatatype,
normalize: attribute.normalize,
});
}
}
}
}
} else {
// One vertex buffer per attribute.
for (name in attributes) {
if (attributes.hasOwnProperty(name) && defined(attributes[name])) {
attribute = attributes[name];
let componentDatatype = attribute.componentDatatype;
if (componentDatatype === ComponentDatatype.DOUBLE) {
componentDatatype = ComponentDatatype.FLOAT;
}
let attrProps = {};
vertexBuffer = undefined;
if (defined(attribute.values)) {
vertexBuffer = Buffer.createVertexBuffer({
context: context,
typedArray: ComponentDatatype.createTypedArray(
componentDatatype,
attribute.values,
),
usage: bufferUsage,
});
attrProps = {
index: attributeLocations[name],
vertexBuffer: vertexBuffer,
value: attribute.value,
componentDatatype: componentDatatype,
componentsPerAttribute: attribute.componentsPerAttribute,
normalize: attribute.normalize,
};
}
//if we already have a typedArray lets use it
if (defined(attribute.typedArray)) {
vertexBuffer = Buffer.createVertexBuffer({
context: context,
typedArray: attribute.typedArray,
usage: bufferUsage,
});
attrProps = {
index: attributeLocations[name],
vertexBuffer: vertexBuffer,
value: undefined,
componentDatatype: componentDatatype,
componentsPerAttribute: AttributeType.getNumberOfComponents(
attribute.type,
),
normalize: attribute.normalized,
instanceDivisor: attribute.instanceDivisor,
};
}
vaAttributes.push(attrProps);
}
}
}
let indexBuffer;
const indices = geometry.indices;
if (defined(indices)) {
if (
Geometry.computeNumberOfVertices(geometry) >=
CesiumMath.SIXTY_FOUR_KILOBYTES &&
context.elementIndexUint
) {
indexBuffer = Buffer.createIndexBuffer({
context: context,
typedArray: new Uint32Array(indices),
usage: bufferUsage,
indexDatatype: IndexDatatype.UNSIGNED_INT,
});
} else {
indexBuffer = Buffer.createIndexBuffer({
context: context,
typedArray: new Uint16Array(indices),
usage: bufferUsage,
indexDatatype: IndexDatatype.UNSIGNED_SHORT,
});
}
}
return new VertexArray({
context: context,
attributes: vaAttributes,
indexBuffer: indexBuffer,
});
};
Object.defineProperties(VertexArray.prototype, {
numberOfAttributes: {
get: function () {
return this._attributes.length;
},
},
numberOfVertices: {
get: function () {
return this._numberOfVertices;
},
},
indexBuffer: {
get: function () {
return this._indexBuffer;
},
},
});
/**
* index is the location in the array of attributes, not the index property of an attribute.
*/
VertexArray.prototype.getAttribute = function (index) {
//>>includeStart('debug', pragmas.debug);
Check.defined("index", index);
//>>includeEnd('debug');
return this._attributes[index];
};
// Workaround for ANGLE, where the attribute divisor seems to be part of the global state instead
// of the VAO state. This function is called when the vao is bound, and should be removed
// once the ANGLE issue is resolved. Setting the divisor should normally happen in vertexAttrib and
// disableVertexAttribArray.
function setVertexAttribDivisor(vertexArray) {
const context = vertexArray._context;
const hasInstancedAttributes = vertexArray._hasInstancedAttributes;
if (!hasInstancedAttributes && !context._previousDrawInstanced) {
return;
}
context._previousDrawInstanced = hasInstancedAttributes;
const divisors = context._vertexAttribDivisors;
const attributes = vertexArray._attributes;
const maxAttributes = ContextLimits.maximumVertexAttributes;
let i;
if (hasInstancedAttributes) {
const length = attributes.length;
for (i = 0; i < length; ++i) {
const attribute = attributes[i];
if (attribute.enabled) {
const divisor = attribute.instanceDivisor;
const index = attribute.index;
if (divisor !== divisors[index]) {
context.glVertexAttribDivisor(index, divisor);
divisors[index] = divisor;
}
}
}
} else {
for (i = 0; i < maxAttributes; ++i) {
if (divisors[i] > 0) {
context.glVertexAttribDivisor(i, 0);
divisors[i] = 0;
}
}
}
}
// Vertex attributes backed by a constant value go through vertexAttrib[1234]f[v]
// which is part of context state rather than VAO state.
function setConstantAttributes(vertexArray, gl) {
const attributes = vertexArray._attributes;
const length = attributes.length;
for (let i = 0; i < length; ++i) {
const attribute = attributes[i];
if (attribute.enabled && defined(attribute.value)) {
attribute.vertexAttrib(gl);
}
}
}
/**
* Copies into a vertex attribute buffer from the given array, at a given
* range specified as offset and count, in number of (VECN) vertices. Array
* and vertex attribute must have the same length, which can be larger
* than the specified range to update.
* @param {number} attributeIndex
* @param {TypedArray} array
* @param {number} vertexOffset
* @param {number} vertexCount
*/
VertexArray.prototype.copyAttributeFromRange = function (
attributeIndex,
array,
vertexOffset,
vertexCount,
) {
const attribute = this.getAttribute(attributeIndex);
const buffer = /** @type {Buffer} */ (attribute.vertexBuffer);
const elementsPerVertex = attribute.componentsPerAttribute;
//>>includeStart('debug', pragmas.debug);
assert(buffer.sizeInBytes === array.byteLength, "Invalid buffer length");
//>>includeEnd('debug');
const ArrayConstructor = /** @type {TypedArrayConstructor} */ (
array.constructor
);
const byteOffset =
vertexOffset * elementsPerVertex * ArrayConstructor.BYTES_PER_ELEMENT;
// Create a zero-copy ArrayView onto the specified range of the source array.
const rangeArrayView = new ArrayConstructor(
/** @type {ArrayBuffer} */ (array.buffer),
array.byteOffset + byteOffset,
vertexCount * elementsPerVertex,
);
buffer.copyFromArrayView(rangeArrayView, byteOffset);
};
/**
* Copies into the index buffer from the given array, at a given range
* specified as offset and count, in number of (uint) indices. Array
* and index buffer must have the same length, which can be larger
* than the specified range to update.
* @param {TypedArray} array
* @param {number} indexOffset
* @param {number} indexCount
*/
VertexArray.prototype.copyIndexFromRange = function (
array,
indexOffset,
indexCount,
) {
const buffer = /** @type {Buffer} */ (this._indexBuffer);
//>>includeStart('debug', pragmas.debug);
assert(buffer.sizeInBytes === array.byteLength, "Invalid buffer length");
//>>includeEnd('debug');
const ArrayConstructor = /** @type {TypedArrayConstructor} */ (
array.constructor
);
const byteOffset = indexOffset * ArrayConstructor.BYTES_PER_ELEMENT;
// Create a zero-copy ArrayView onto the specified range of the source array.
const rangeArrayView = new ArrayConstructor(
/** @type {ArrayBuffer} */ (array.buffer),
array.byteOffset + byteOffset,
indexCount,
);
buffer.copyFromArrayView(rangeArrayView, byteOffset);
};
VertexArray.prototype._bind = function () {
if (defined(this._vao)) {
this._context.glBindVertexArray(this._vao);
if (this._context.instancedArrays) {
setVertexAttribDivisor(this);
}
if (this._hasConstantAttributes) {
setConstantAttributes(this, this._gl);
}
} else {
bind(this._gl, this._attributes, this._indexBuffer);
}
};
VertexArray.prototype._unBind = function () {
if (defined(this._vao)) {
this._context.glBindVertexArray(null);
} else {
const attributes = this._attributes;
const gl = this._gl;
for (let i = 0; i < attributes.length; ++i) {
const attribute = attributes[i];
if (attribute.enabled) {
attribute.disableVertexAttribArray(gl);
}
}
if (this._indexBuffer) {
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}
}
};
VertexArray.prototype.isDestroyed = function () {
return false;
};
VertexArray.prototype.destroy = function () {
const attributes = this._attributes;
for (let i = 0; i < attributes.length; ++i) {
const vertexBuffer = attributes[i].vertexBuffer;
if (
defined(vertexBuffer) &&
!vertexBuffer.isDestroyed() &&
vertexBuffer.vertexArrayDestroyable
) {
vertexBuffer.destroy();
}
}
const indexBuffer = this._indexBuffer;
if (
defined(indexBuffer) &&
!indexBuffer.isDestroyed() &&
indexBuffer.vertexArrayDestroyable
) {
indexBuffer.destroy();
}
if (defined(this._vao)) {
this._context.glDeleteVertexArray(this._vao);
}
return destroyObject(this);
};
export default VertexArray;