-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
attribute-manager.js
420 lines (364 loc) · 13 KB
/
attribute-manager.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
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
// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/* eslint-disable guard-for-in */
import Attribute from './attribute';
import log from '../utils/log';
import AttributeTransitionManager from './attribute-transition-manager';
const LOG_START_END_PRIORITY = 2;
const LOG_DETAIL_PRIORITY = 3;
function noop() {}
// Default loggers
const logFunctions = {
savedMessages: null,
timeStart: null,
onLog: ({level, message}) => {
log.log(level, message)();
},
onUpdateStart: ({level, numInstances}) => {
logFunctions.savedMessages = [];
logFunctions.timeStart = new Date();
},
onUpdate: ({level, message}) => {
if (logFunctions.savedMessages) {
logFunctions.savedMessages.push(message);
}
},
onUpdateEnd: ({level, id, numInstances}) => {
const timeMs = Math.round(new Date() - logFunctions.timeStart);
const time = `${timeMs}ms`;
log.group(level, `Updated attributes for ${numInstances} instances in ${id} in ${time}`, {
collapsed: true
})();
for (const message of logFunctions.savedMessages) {
log.log(level, message)();
}
log.groupEnd(level, `Updated attributes for ${numInstances} instances in ${id} in ${time}`)();
logFunctions.savedMessages = null;
}
};
export default class AttributeManager {
/**
* Sets log functions to help trace or time attribute updates.
* Default logging uses deck logger.
*
* `onLog` is called for each attribute.
*
* To enable detailed control of timming and e.g. hierarchical logging,
* hooks are also provided for update start and end.
*
* @param {Object} [opts]
* @param {String} [onLog=] - called to print
* @param {String} [onUpdateStart=] - called before update() starts
* @param {String} [onUpdateEnd=] - called after update() ends
*/
static setDefaultLogFunctions({onLog, onUpdateStart, onUpdate, onUpdateEnd} = {}) {
if (onLog !== undefined) {
logFunctions.onLog = onLog || noop;
}
if (onUpdateStart !== undefined) {
logFunctions.onUpdateStart = onUpdateStart || noop;
}
if (onUpdate !== undefined) {
logFunctions.onUpdate = onUpdate || noop;
}
if (onUpdateEnd !== undefined) {
logFunctions.onUpdateEnd = onUpdateEnd || noop;
}
}
/**
* @classdesc
* Automated attribute generation and management. Suitable when a set of
* vertex shader attributes are generated by iteration over a data array,
* and updates to these attributes are needed either when the data itself
* changes, or when other data relevant to the calculations change.
*
* - First the application registers descriptions of its dynamic vertex
* attributes using AttributeManager.add().
* - Then, when any change that affects attributes is detected by the
* application, the app will call AttributeManager.invalidate().
* - Finally before it renders, it calls AttributeManager.update() to
* ensure that attributes are automatically rebuilt if anything has been
* invalidated.
*
* The application provided update functions describe how attributes
* should be updated from a data array and are expected to traverse
* that data array (or iterable) and fill in the attribute's typed array.
*
* Note that the attribute manager intentionally does not do advanced
* change detection, but instead makes it easy to build such detection
* by offering the ability to "invalidate" each attribute separately.
*/
constructor(gl, {id = 'attribute-manager', stats} = {}) {
this.id = id;
this.gl = gl;
this.attributes = {};
this.updateTriggers = {};
this.accessors = {};
this.needsRedraw = true;
this.userData = {};
this.stats = stats;
this.attributeTransitionManager = new AttributeTransitionManager(gl, {
id: `${id}-transitions`
});
// For debugging sanity, prevent uninitialized members
Object.seal(this);
}
finalize() {
for (const attributeName in this.attributes) {
this.attributes[attributeName].delete();
}
this.attributeTransitionManager.finalize();
}
// Returns the redraw flag, optionally clearing it.
// Redraw flag will be set if any attributes attributes changed since
// flag was last cleared.
//
// @param {String} [clearRedrawFlags=false] - whether to clear the flag
// @return {false|String} - reason a redraw is needed.
getNeedsRedraw(opts = {clearRedrawFlags: false}) {
const redraw = this.needsRedraw;
this.needsRedraw = this.needsRedraw && !opts.clearRedrawFlags;
return redraw && this.id;
}
// Sets the redraw flag.
// @param {Boolean} redraw=true
// @return {AttributeManager} - for chaining
setNeedsRedraw(redraw = true) {
this.needsRedraw = true;
return this;
}
// Adds attributes
add(attributes, updaters) {
this._add(attributes, updaters);
}
// Adds attributes
addInstanced(attributes, updaters) {
this._add(attributes, updaters, {instanced: 1});
}
/**
* Removes attributes
* Takes an array of attribute names and delete them from
* the attribute map if they exists
*
* @example
* attributeManager.remove(['position']);
*
* @param {Object} attributeNameArray - attribute name array (see above)
*/
remove(attributeNameArray) {
for (let i = 0; i < attributeNameArray.length; i++) {
const name = attributeNameArray[i];
if (this.attributes[name] !== undefined) {
this.attributes[name].delete();
delete this.attributes[name];
}
}
}
// Marks an attribute for update
invalidate(triggerName) {
const invalidatedAttributes = this._invalidateTrigger(triggerName);
// For performance tuning
logFunctions.onLog({
level: LOG_DETAIL_PRIORITY,
message: `invalidated attributes ${invalidatedAttributes} (${triggerName}) for ${this.id}`
});
}
invalidateAll() {
for (const attributeName in this.attributes) {
this.attributes[attributeName].setNeedsUpdate();
}
// For performance tuning
logFunctions.onLog({
level: LOG_DETAIL_PRIORITY,
message: `invalidated all attributes for ${this.id}`
});
}
// Ensure all attribute buffers are updated from props or data.
update({
data,
numInstances,
bufferLayout,
transitions,
startIndex,
endIndex,
props = {},
buffers = {},
context = {}
} = {}) {
// keep track of whether some attributes are updated
let updated = false;
logFunctions.onUpdateStart({level: LOG_START_END_PRIORITY, id: this.id, numInstances});
if (this.stats) {
this.stats.get('Update Attributes').timeStart();
}
for (const attributeName in this.attributes) {
const attribute = this.attributes[attributeName];
if (attribute.setExternalBuffer(buffers[attributeName], this.numInstances)) {
// Attribute is using external buffer from the props
} else if (attribute.setGenericValue(props[attribute.getAccessor()])) {
// Attribute is using generic value from the props
} else if (attribute.needsUpdate()) {
updated = true;
this._updateAttribute({
attribute,
numInstances,
bufferLayout,
data,
startIndex,
endIndex,
props,
context
});
}
this.needsRedraw |= attribute.needsRedraw();
}
if (updated) {
// Only initiate alloc/update (and logging) if actually needed
logFunctions.onUpdateEnd({level: LOG_START_END_PRIORITY, id: this.id, numInstances});
}
if (this.stats) {
this.stats.get('Update Attributes').timeEnd();
}
this.attributeTransitionManager.update({
attributes: this.attributes,
numInstances,
transitions
});
}
// Update attribute transition to the current timestamp
// Returns `true` if any transition is in progress
updateTransition(timestamp) {
const {attributeTransitionManager} = this;
const transitionUpdated = attributeTransitionManager.setCurrentTime(timestamp);
this.needsRedraw = this.needsRedraw || transitionUpdated;
return transitionUpdated;
}
/**
* Returns all attribute descriptors
* Note: Format matches luma.gl Model/Program.setAttributes()
* @return {Object} attributes - descriptors
*/
getAttributes() {
return this.attributes;
}
/**
* Returns changed attribute descriptors
* This indicates which WebGLBuffers need to be updated
* @return {Object} attributes - descriptors
*/
getChangedAttributes(opts = {clearChangedFlags: false}) {
const {attributes, attributeTransitionManager} = this;
const changedAttributes = Object.assign({}, attributeTransitionManager.getAttributes());
for (const attributeName in attributes) {
const attribute = attributes[attributeName];
if (attribute.needsRedraw(opts) && !attributeTransitionManager.hasAttribute(attributeName)) {
changedAttributes[attributeName] = attribute;
}
}
return changedAttributes;
}
// PROTECTED METHODS - Only to be used by collaborating classes, not by apps
// Returns object containing all accessors as keys, with non-null values
// @return {Object} - accessors object
getAccessors() {
return this.updateTriggers;
}
// PRIVATE METHODS
// Used to register an attribute
_add(attributes, updaters, extraProps = {}) {
if (updaters) {
log.warn('AttributeManager.add({updaters}) - updater map no longer supported')();
}
const newAttributes = {};
for (const attributeName in attributes) {
const attribute = attributes[attributeName];
// Initialize the attribute descriptor, with WebGL and metadata fields
const newAttribute = this._createAttribute(attributeName, attribute, extraProps);
newAttributes[attributeName] = newAttribute;
}
Object.assign(this.attributes, newAttributes);
this._mapUpdateTriggersToAttributes();
}
/* eslint-enable max-statements */
_createAttribute(name, attribute, extraProps) {
const props = {
id: name,
// Luma fields
constant: attribute.constant || false,
isIndexed: attribute.isIndexed || attribute.elements,
size: (attribute.elements && 1) || attribute.size,
value: attribute.value || null,
divisor: attribute.instanced || extraProps.instanced ? 1 : attribute.divisor
};
return new Attribute(this.gl, Object.assign({}, attribute, props));
}
// build updateTrigger name to attribute name mapping
_mapUpdateTriggersToAttributes() {
const triggers = {};
for (const attributeName in this.attributes) {
const attribute = this.attributes[attributeName];
attribute.getUpdateTriggers().forEach(triggerName => {
if (!triggers[triggerName]) {
triggers[triggerName] = [];
}
triggers[triggerName].push(attributeName);
});
}
this.updateTriggers = triggers;
}
_invalidateTrigger(triggerName) {
const {attributes, updateTriggers} = this;
const invalidatedAttributes = updateTriggers[triggerName];
if (invalidatedAttributes) {
invalidatedAttributes.forEach(name => {
const attribute = attributes[name];
if (attribute) {
attribute.setNeedsUpdate();
}
});
} else {
let message = `invalidating non-existent trigger ${triggerName} for ${this.id}\n`;
message += `Valid triggers: ${Object.keys(attributes).join(', ')}`;
log.warn(message, invalidatedAttributes)();
}
return invalidatedAttributes;
}
_updateAttribute(opts) {
const {attribute, numInstances} = opts;
if (attribute.allocate(numInstances)) {
logFunctions.onUpdate({
level: LOG_DETAIL_PRIORITY,
message: `${attribute.id} allocated ${numInstances}`,
id: this.id
});
}
// Calls update on any buffers that need update
const timeStart = Date.now();
const updated = attribute.updateBuffer(opts);
if (updated) {
this.needsRedraw = true;
const timeMs = Math.round(Date.now() - timeStart);
logFunctions.onUpdate({
level: LOG_DETAIL_PRIORITY,
message: `${attribute.id} updated ${numInstances} in ${timeMs}ms`
});
}
}
}