-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
gpu-transform.js
116 lines (108 loc) · 3.79 KB
/
gpu-transform.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
// Copyright (c) 2015 - 2019 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.
// should create a new column based on mapping and gpuTable.
import {Buffer, Transform, _Accessor as Accessor} from '@luma.gl/core';
import {log} from '@deck.gl/core';
import GL from '@luma.gl/constants';
export default function gpuTransform(gpuTable, mappings) {
// TODO: For now hardcoding everything for position
const longitudeBuffer = gpuTable.buffers.longitude;
const latitudeBuffer = gpuTable.buffers.latitude;
const byteLength = longitudeBuffer.byteLength * 3;
const positionAccessor = Accessor.resolve({size: 3, type: GL.FLOAT});
const positionBuffer = new Buffer(gpuTable.gl, {byteLength, accessor: positionAccessor});
const targetAccessors = {position: positionAccessor};
const {inject, varyings} = getShaderOptions(gpuTable, mappings, targetAccessors);
const transform = new Transform(gpuTable.gl, {
sourceBuffers: {
longitude: longitudeBuffer,
latitude: latitudeBuffer
},
feedbackBuffers: {
position: positionBuffer
},
varyings,
inject,
elementCount: longitudeBuffer.getElementCount(),
vs: 'void main() {}'
});
transform.run();
transform.delete();
gpuTable.addColumns({position: {buffer: positionBuffer, accessor: positionAccessor}});
return;
}
function getShaderOptions(gpuTable, mappings, targetAccessors) {
let columns = [];
let glslCode = '';
let declarations = '';
const varyings = [];
for (const target in mappings) {
if (mappings[target].sourceColumns) {
columns = columns.concat(mappings[target].sourceColumns);
}
glslCode = `${glslCode}${mappings[target].glslCode}\n`;
const accessor = targetAccessors[target];
const type = getType(accessor);
declarations = `${declarations}varying ${type} ${target};\n`;
varyings.push(target);
}
// filter out duplicates
columns = new Set(columns);
columns.forEach(column => {
const accessor = gpuTable.accessors[column];
const type = getType(accessor);
declarations = `${declarations}attribute ${type} ${column};\n`;
});
return {
inject: {
'vs:#decl': declarations,
'vs:#main-start': glslCode
},
varyings
};
}
// TODO
// Utility methods that can be moved to some global scope
// or just any existing luma.gl shadertools methods
function getType(accessor) {
switch (accessor.type) {
case GL.FLOAT:
switch (accessor.size) {
case 1:
return 'float';
case 2:
return 'vec2';
case 3:
return 'vec3';
case 4:
return 'vec4';
default:
// invalid size
log.assert(false);
break;
}
break;
default:
// TODO: add other cases
log.assert(false);
break;
}
return null;
}