forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvao.js
210 lines (179 loc) · 4.16 KB
/
vao.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
var tape = require('tape')
var createContext = require('./util/create-context')
var createREGL = require('../regl')
function testVAO (regl, t) {
var frag = [
'precision highp float;',
'void main() {',
'gl_FragColor = vec4(1, 1, 1, 1);',
'}'
].join('\n')
var vert = [
'precision highp float;',
'attribute vec2 position;',
'varying vec4 fragColor;',
'void main() {',
'gl_Position=vec4(position, 0, 1);',
'gl_PointSize=1.;',
'}'
].join('\n')
var baseCommand = {
frag: frag,
vert: vert,
attributes: {
position: 0
},
primitive: 'lines',
depth: false,
count: 2
}
var vaoHorizontal = [
[ [-10, 0], [10, 0] ]
]
var vaoVertical = [
[ [0, -10], [0, 10] ]
]
var vaoHorizontalResource = regl.vao(vaoHorizontal)
var vaoVerticalResource = regl.vao(vaoVertical)
var horizontalExpected = [
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 1, 1, 1, 1,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
]
var verticalExpected = [
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0
]
var crossExpected = [
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
1, 1, 1, 1, 1,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0
]
function check (body, expected, name) {
regl.clear({
color: [0, 0, 0, 0],
depth: 1
})
body()
var actual = regl.read()
var actualStr = []
var exptectedStr = []
for (var i = 0; i < 5; ++i) {
for (var j = 0; j < 5; ++j) {
var ptr = 5 * i + j
exptectedStr.push(expected[ptr])
actualStr.push(actual[4 * ptr] ? 1 : 0)
}
actualStr.push('\n')
exptectedStr.push('\n')
}
t.equals(actualStr.join(''), exptectedStr.join(''), name)
}
var staticScope = regl({
vao: vaoVertical
})
var staticResourceScope = regl({
vao: vaoVerticalResource
})
var dynamicScope = regl({
vao: regl.prop('vao')
})
var drawContext = regl(baseCommand)
var drawStatic = regl(Object.assign({
vao: vaoHorizontal
}, baseCommand))
var drawStaticResource = regl(Object.assign({
vao: vaoHorizontalResource
}, baseCommand))
var drawDynamic = regl(Object.assign({
vao: regl.prop('vao')
}, baseCommand))
check(function () {
drawStatic()
}, horizontalExpected, 'draw/static')
check(function () {
drawStaticResource()
}, horizontalExpected, 'draw/static-resource')
check(function () {
drawDynamic({
vao: vaoVerticalResource
})
}, verticalExpected, 'draw/prop')
check(function () {
staticScope(function () {
drawContext()
})
}, verticalExpected, 'draw/scope/static')
check(function () {
dynamicScope(
{ vao: vaoHorizontalResource },
function () {
drawContext()
})
}, horizontalExpected, 'draw/scope/dynamic')
check(function () {
drawStatic(1)
}, horizontalExpected, 'batch/static')
check(function () {
drawStaticResource(1)
}, horizontalExpected, 'batch/static-resource')
check(function () {
drawDynamic([
{ vao: vaoVerticalResource },
{ vao: vaoHorizontalResource }
])
}, crossExpected, 'batch/prop')
check(function () {
staticScope(function () {
drawContext(1)
})
}, verticalExpected, 'batch/scope/static')
check(function () {
staticResourceScope(function () {
drawContext(1)
})
}, verticalExpected, 'batch/scope/static-resource')
check(function () {
dynamicScope(
{ vao: vaoHorizontalResource },
function () {
drawContext(1)
})
}, horizontalExpected, 'batch/scope/dynamic')
}
tape('vao - extension', function (t) {
var gl = createContext(5, 5)
var regl
try {
regl = createREGL({
gl: gl,
extensions: [ 'oes_vertex_array_object' ]
})
} catch (e) {
t.pass('extension not supported')
createContext.destroy(gl)
t.end()
return
}
testVAO(regl, t)
regl.destroy()
t.equals(gl.getError(), 0, 'error ok')
createContext.destroy(gl)
t.end()
})
tape('vao - emulation', function (t) {
var gl = createContext(5, 5)
var regl = createREGL(gl)
testVAO(regl, t)
regl.destroy()
t.equals(gl.getError(), 0, 'error ok')
createContext.destroy(gl)
t.end()
})