wgl-vbuffer.js
4.33 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
/* *
*
* Copyright (c) 2019-2019 Highsoft AS
*
* Boost module: stripped-down renderer for higher performance
*
* License: highcharts.com/license
*
* */
'use strict';
/**
* Vertex Buffer abstraction.
* A vertex buffer is a set of vertices which are passed to the GPU
* in a single call.
*
* @private
* @function GLVertexBuffer
*
* @param {WebGLContext} gl
* the context in which to create the buffer
*
* @param {GLShader} shader
* the shader to use
*
* @return {*}
*/
function GLVertexBuffer(gl, shader, dataComponents /* , type */) {
var buffer = false,
vertAttribute = false,
components = dataComponents || 2,
preAllocated = false,
iterator = 0,
// farray = false,
data;
// type = type || 'float';
function destroy() {
if (buffer) {
gl.deleteBuffer(buffer);
buffer = false;
vertAttribute = false;
}
iterator = 0;
components = dataComponents || 2;
data = [];
}
/**
* Build the buffer
* @private
* @param dataIn {Array<float>} - a 0 padded array of indices
* @param attrib {String} - the name of the Attribute to bind the buffer to
* @param dataComponents {Integer} - the number of components per. indice
*/
function build(dataIn, attrib, dataComponents) {
var farray;
data = dataIn || [];
if ((!data || data.length === 0) && !preAllocated) {
// console.error('trying to render empty vbuffer');
destroy();
return false;
}
components = dataComponents || components;
if (buffer) {
gl.deleteBuffer(buffer);
}
if (!preAllocated) {
farray = new Float32Array(data);
}
buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(
gl.ARRAY_BUFFER,
preAllocated || farray,
gl.STATIC_DRAW
);
// gl.bindAttribLocation(shader.program(), 0, 'aVertexPosition');
vertAttribute = gl.getAttribLocation(shader.program(), attrib);
gl.enableVertexAttribArray(vertAttribute);
// Trigger cleanup
farray = false;
return true;
}
/**
* Bind the buffer
* @private
*/
function bind() {
if (!buffer) {
return false;
}
// gl.bindAttribLocation(shader.program(), 0, 'aVertexPosition');
// gl.enableVertexAttribArray(vertAttribute);
// gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.vertexAttribPointer(
vertAttribute, components, gl.FLOAT, false, 0, 0
);
// gl.enableVertexAttribArray(vertAttribute);
}
/**
* Render the buffer
* @private
* @param from {Integer} - the start indice
* @param to {Integer} - the end indice
* @param drawMode {String} - the draw mode
*/
function render(from, to, drawMode) {
var length = preAllocated ? preAllocated.length : data.length;
if (!buffer) {
return false;
}
if (!length) {
return false;
}
if (!from || from > length || from < 0) {
from = 0;
}
if (!to || to > length) {
to = length;
}
drawMode = drawMode || 'points';
gl.drawArrays(
gl[drawMode.toUpperCase()],
from / components,
(to - from) / components
);
return true;
}
function push(x, y, a, b) {
if (preAllocated) { // && iterator <= preAllocated.length - 4) {
preAllocated[++iterator] = x;
preAllocated[++iterator] = y;
preAllocated[++iterator] = a;
preAllocated[++iterator] = b;
}
}
/**
* Note about pre-allocated buffers:
* - This is slower for charts with many series
* @private
*/
function allocate(size) {
size *= 4;
iterator = -1;
preAllocated = new Float32Array(size);
}
// /////////////////////////////////////////////////////////////////////////
return {
destroy: destroy,
bind: bind,
data: data,
build: build,
render: render,
allocate: allocate,
push: push
};
}
export default GLVertexBuffer;