-
Notifications
You must be signed in to change notification settings - Fork 204
/
program.spec.js
104 lines (75 loc) · 2.76 KB
/
program.spec.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
import test from 'tape-catch';
import {Program, Buffer, VertexArray} from '@luma.gl/webgl';
import {fixture} from 'test/setup';
const vs = `
attribute vec3 positions;
uniform mat4 uMVMatrix[2];
uniform mat4 uPMatrix;
varying vec3 vPosition;
void main(void) {
gl_Position = uPMatrix * uMVMatrix[0] * vec4(positions, 1.0);
vPosition = positions;
}
`;
const fs = `
void main(void) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
`;
const BUFFER_DATA = new Float32Array([0, 1, 0, -1, -1, 0, 1, -1, 0]);
test('WebGL#Program construct/delete', t => {
const {gl} = fixture;
t.throws(
() => new Program(),
/.*WebGLRenderingContext.*/,
'Program throws on missing gl context'
);
t.throws(() => new Program(gl), 'Program throws on missing shader');
const program = new Program(gl, {vs, fs});
t.ok(program instanceof Program, 'Program construction successful');
program.delete();
t.ok(program instanceof Program, 'Program delete successful');
program.delete();
t.ok(program instanceof Program, 'Program repeated delete successful');
t.end();
});
test('WebGL#Program draw', t => {
const {gl} = fixture;
const program = new Program(gl, {fs, vs});
const vertexArray = new VertexArray(gl, {program});
vertexArray.setAttributes({
positions: new Buffer(gl, {data: BUFFER_DATA, accessor: {size: 3}}),
unusedAttributeName: new Buffer(gl, {data: BUFFER_DATA, accessor: {size: 3}})
});
t.ok(vertexArray instanceof VertexArray, 'VertexArray set buffers successful');
program.draw({vertexArray, vertexCount: 3});
t.ok(program instanceof Program, 'Program draw successful');
let didDraw = program.draw({vertexArray, vertexCount: 3, parameters: {blend: true}});
t.ok(program instanceof Program, 'Program draw with parameters is successful');
t.ok(didDraw, 'Program draw successful');
didDraw = program.draw({vertexArray, vertexCount: 0});
t.notOk(didDraw, 'Program draw succesfully skipped');
didDraw = program.draw({vertexArray, vertexCount: 3, instanceCount: 0, isInstanced: true});
t.notOk(didDraw, 'Instanced Program draw succesfully skipped');
t.end();
});
test('WebGL#Program caching', t => {
const {gl} = fixture;
const program = new Program(gl, {fs, vs});
program._isCached = true;
program.delete();
t.ok(program._handle, 'Program should not be deleted');
program._isCached = false;
program.delete();
t.ok(!program._handle, 'Program should be deleted');
t.end();
});
test('WebGL#Program uniform array', t => {
const {gl} = fixture;
const program = new Program(gl, {vs, fs});
t.ok(program._uniformSetters.uMVMatrix, 'uniform array is ok');
t.ok(program._uniformSetters[`uMVMatrix[0]`], 'uniform array is ok');
t.ok(program._uniformSetters[`uMVMatrix[1]`], 'uniform array is ok');
program.delete();
t.end();
});