forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.js
153 lines (132 loc) Β· 4.24 KB
/
read.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
var createContext = require('./util/create-context')
var createREGL = require('../regl')
var tape = require('tape')
var safari = require('is-safari')
tape('read pixels', function (t) {
var W = 5
var H = 5
var gl = createContext(W, H)
var regl = createREGL({
gl: gl,
optionalExtensions: ['oes_texture_float', 'ext_srgb']
})
function checkFBO (pixels, color, name) {
function checkPixels () {
for (var i = 0; i < W * H * 4; i += 4) {
if (pixels[i] !== color[0] ||
pixels[i + 1] !== color[1] ||
pixels[i + 2] !== color[2] ||
pixels[i + 3] !== color[3]) {
return false
}
}
return true
}
t.ok(checkPixels(), name)
}
function throws (name, args) {
t.throws(function () {
regl.read.apply(regl, args)
}, /\(regl\)/, name)
}
// typedarray input
var bytes = new Uint8Array(100)
var result = regl.read(bytes)
t.equals(result, bytes, 'read typedarray ok')
// width/height input
t.equals(regl.read({width: 2, height: 2}).length, 16, 'width/height ok')
// options input
t.equals(regl.read({x: 3, y: 3}).length, 16, 'offset ok')
// read out of bounds
throws('bad width', [{width: -2}])
throws('bad height', [{height: -2}])
throws('bad offset', [{ x: -2 }])
throws('bad typedarray', [{data: []}])
throws('small typedarray', [new Uint8Array(1)])
// check pixels for default framebuffer
regl.clear({color: [1, 0, 0, 1]})
var pixels = regl.read()
checkFBO(pixels, [255, 0, 0, 255], 'read null fbo ok')
regl.clear({color: [1, 0, 0, 1]})
pixels = new Uint8Array(W * H * 4)
regl.read({data: pixels})
checkFBO(pixels, [255, 0, 0, 255], 'read null fbo, reuse buffer, ok')
pixels = new Float32Array(W * H * 4)
throws('throws if attempt use Float32Array to null fbo', [{data: pixels}])
throws('throws if attempt use object to null fbo', [{data: {}}])
// now do it for an uint8 fbo
var fbo = regl.framebuffer({
width: W,
height: H,
colorFormat: 'rgba',
colorType: 'uint8'
})
regl({framebuffer: fbo})(function () {
regl.clear({color: [1, 0, 0, 1]})
pixels = regl.read()
checkFBO(pixels, [255, 0, 0, 255], 'read uint8 fbo ok')
})
regl({framebuffer: fbo})(function () {
regl.clear({color: [1, 0, 0, 1]})
pixels = new Uint8Array(W * H * 4)
regl.read({data: pixels})
checkFBO(pixels, [255, 0, 0, 255], 'read uint8 fbo, reuse buffer, ok')
})
regl({framebuffer: fbo})(function () {
pixels = new Float32Array(W * H * 4)
throws('throws if attempt use Float32Array to uint8 fbo', [{data: pixels}])
throws('throws if attempt use object to uint8 fbo', [{data: {}}])
})
// now do it for an float fbo
if (regl.hasExtension('oes_texture_float') && !safari) {
fbo = regl.framebuffer({
width: W,
height: H,
colorFormat: 'rgba',
colorType: 'float'
})
regl({framebuffer: fbo})(function () {
regl.clear({color: [0.5, 0.25, 0.5, 0.25]})
pixels = regl.read()
checkFBO(pixels, [0.5, 0.25, 0.5, 0.25], 'read float fbo ok')
})
regl({framebuffer: fbo})(function () {
regl.clear({color: [0.5, 0.25, 0.5, 0.25]})
pixels = new Float32Array(W * H * 4)
regl.read({data: pixels})
checkFBO(pixels, [0.5, 0.25, 0.5, 0.25], 'read float fbo, reuse buffer, ok')
})
regl({framebuffer: fbo})(function () {
pixels = new Uint8Array(W * H * 4)
throws('throws if attempt use Uint8Array to float fbo', [{data: pixels}])
throws('throws if attempt use object to float fbo', [{data: {}}])
})
}
var badTestCases = []
badTestCases.push('rgba4')
badTestCases.push('rgb565')
badTestCases.push('rgb5 a1')
if (regl.hasExtension('ext_srgb')) {
badTestCases.push('srgba')
}
badTestCases.forEach(function (testCase, i) {
try {
fbo = regl.framebuffer({
colorFormat: testCase,
width: W,
height: H
})
regl({framebuffer: fbo})(function () {
regl.read()
})
} catch (e) {
t.pass('attempt to read from renderbuffer of type ' + testCase, [{}])
return
}
t.fail('attempt to read from renderbuffer of type ' + testCase, [{}])
})
regl.destroy()
t.equals(gl.getError(), 0, 'error ok')
createContext.destroy(gl)
t.end()
})