/
base-attribute.js
164 lines (144 loc) · 4.46 KB
/
base-attribute.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
/* eslint-disable complexity */
import GL from '@luma.gl/constants';
import {Buffer, hasFeature, FEATURES} from '@luma.gl/core';
import {log, uid} from '@luma.gl/core';
export default class BaseAttribute {
constructor(gl, opts = {}) {
const {id = uid('attribute'), type, isIndexed = false} = opts;
// Options that cannot be changed later
this.gl = gl;
this.id = id;
this.isIndexed = isIndexed;
this.target = isIndexed ? GL.ELEMENT_ARRAY_BUFFER : GL.ARRAY_BUFFER;
this.type = type;
if (isIndexed && !type) {
// If the attribute is indices, auto infer the correct type
// WebGL2 and WebGL1 w/ uint32 index extension support accepts Uint32Array, otherwise Uint16Array
this.type =
gl && hasFeature(gl, FEATURES.ELEMENT_INDEX_UINT32) ? GL.UNSIGNED_INT : GL.UNSIGNED_SHORT;
}
// Initialize the attribute descriptor, with WebGL and metadata fields
this.value = null;
this.externalBuffer = null;
this.buffer = null;
this.userData = {}; // Reserved for application
this.update(opts);
// Check all fields and generate helpful error messages
this._validateAttributeDefinition();
}
delete() {
if (this.buffer) {
this.buffer.delete();
this.buffer = null;
}
}
/* eslint-disable max-statements */
update(opts) {
const {value, buffer, constant = this.constant || false} = opts;
this.constant = constant;
if (buffer) {
this.externalBuffer = buffer;
this.constant = false;
this.type = opts.type || buffer.accessor.type;
if (buffer.accessor.divisor !== undefined) {
this.divisor = buffer.accessor.divisor;
}
if (opts.divisor !== undefined) {
this.divisor = opts.divisor;
}
} else if (value) {
this.externalBuffer = null;
const size = this.size || opts.size || 0;
if (constant && value.length !== size) {
// NOTE(Tarek): Assuming float constants.
// This is all we currently use, but we'll
// have to update this if we start using int
// attributes (WebGL 2-only)
this.value = new Float32Array(size);
const index = this.offset / 4; // Always 4 bytes/element (float, int or uint)
for (let i = 0; i < this.size; ++i) {
this.value[i] = value[index + i];
}
} else {
this.value = value;
}
// Create buffer if needed
if (!constant && this.gl) {
// Move accessor fields to accessor object
const props = {
...opts,
id: this.id,
target: this.target,
accessor: {
type: this.type
}
};
if (Number.isFinite(props.divisor)) {
props.accessor.divisor = props.divisor;
}
delete props.divisor;
if (Number.isFinite(props.size)) {
props.accessor.size = props.size;
}
delete props.size;
this.buffer = this.buffer || new Buffer(this.gl, props);
this.buffer.setData({data: value});
this.type = this.buffer.accessor.type;
}
}
this._setAccessor(opts);
}
getBuffer() {
if (this.constant) {
return null;
}
return this.externalBuffer || this.buffer;
}
getValue() {
if (this.constant) {
return this.value;
}
const buffer = this.externalBuffer || this.buffer;
if (buffer) {
return [buffer, this];
}
return null;
}
// Sets all accessor props except type
// TODO - store on `this.accessor`
_setAccessor(opts) {
const {
// accessor props
size = this.size,
offset = this.offset || 0,
stride = this.stride || 0,
normalized = this.normalized || false,
integer = this.integer || false,
divisor = this.divisor || 0,
instanced,
isInstanced
} = opts;
this.size = size;
this.offset = offset;
this.stride = stride;
this.normalized = normalized;
this.integer = integer;
this.divisor = divisor;
if (isInstanced !== undefined) {
log.deprecated('Attribute.isInstanced')();
this.divisor = isInstanced ? 1 : 0;
}
if (instanced !== undefined) {
log.deprecated('Attribute.instanced')();
this.divisor = instanced ? 1 : 0;
}
}
_validateAttributeDefinition() {
// Can be undefined for buffers (auto deduced from shaders)
// or larger than 4 for uniform arrays
// assert(
// this.size >= 1 && this.size <= 4,
// `Attribute definition for ${this.id} invalid size`
// );
}
}