4 <script src='/tests/SimpleTest/SimpleTest.js'
></script>
5 <link rel='stylesheet' href='/tests/SimpleTest/test.css'
>
8 <canvas id='c' width='
200' height='
200'
></canvas>
9 <canvas id='c2' width='
200' height='
200'
></canvas>
13 function RGBAToString(arr
) {
14 return '[' + arr
[0].toPrecision(4) + ', ' +
15 arr
[1].toPrecision(4) + ', ' +
16 arr
[2].toPrecision(4) + ', ' +
17 arr
[3].toPrecision(4) + ']';
20 function TestScreenColor(gl
, r
, g
, b
, a
) {
21 var arr
= new SharedArrayBuffer(4);
22 var view
= new Uint8Array(arr
);
23 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, view
);
25 var err
= gl
.getError();
26 ok(err
== 0, 'Should be no errors.');
31 floatArr
= new Float32Array(4);
32 floatArr
[0] = view
[0] / 255.0;
33 floatArr
[1] = view
[1] / 255.0;
34 floatArr
[2] = view
[2] / 255.0;
35 floatArr
[3] = view
[3] / 255.0;
37 var testText
= RGBAToString(floatArr
);
38 var refText
= RGBAToString([r
, g
, b
, a
]);
40 var eps
= 1.0 / 255.0;
41 var isSame
= (Math
.abs(floatArr
[0] - r
) < eps
&&
42 Math
.abs(floatArr
[1] - g
) < eps
&&
43 Math
.abs(floatArr
[2] - b
) < eps
&&
44 Math
.abs(floatArr
[3] - a
) < eps
);
46 ok(isSame
, 'Should be ' + refText
+ ', was ' + testText
+ ',');
49 // Give ourselves a scope to return early from:
51 var canvas
= document
.getElementById('c');
56 let gl
= canvas
.getContext('experimental-webgl', attribs
);
58 todo(false, 'WebGL is unavailable.');
61 if (typeof SharedArrayBuffer
=== 'undefined') {
62 todo(false, 'SharedArrayBuffer is unavailable.');
66 var vs
= gl
.createShader(gl
.VERTEX_SHADER
);
67 gl
.shaderSource(vs
, "attribute vec2 aVertCoord; void main(void) { gl_Position = vec4(aVertCoord, 0.0, 1.0); }");
69 var fs
= gl
.createShader(gl
.FRAGMENT_SHADER
);
70 gl
.shaderSource(fs
, "precision mediump float; uniform vec4 uFragColor; void main(void) { gl_FragColor = uFragColor; }");
72 var prog
= gl
.createProgram();
73 gl
.attachShader(prog
, vs
);
74 gl
.attachShader(prog
, fs
);
77 var success
= gl
.getProgramParameter(prog
, gl
.LINK_STATUS
);
79 console
.log('Error linking program for \'' + vsId
+ '\' and \'' + fsId
+ '\'.');
80 console
.log('\nLink log: ' + gl
.getProgramInfoLog(prog
));
81 console
.log('\nVert shader log: ' + gl
.getShaderInfoLog(vs
));
82 console
.log('\nFrag shader log: ' + gl
.getShaderInfoLog(fs
));
84 ok(prog
, 'Program should link.');
89 prog
.aVertCoord
= gl
.getAttribLocation(prog
, 'aVertCoord');
90 prog
.uFragColor
= gl
.getUniformLocation(prog
, 'uFragColor');
94 // Test gl.bufferData(), gl.bufferSubData() and gl.readPixels() APIs with SAB as input.
95 var arr
= new SharedArrayBuffer(8*4);
96 var view
= new Float32Array(arr
);
97 view
.set(new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]));
98 var vb
= gl
.createBuffer();
99 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vb
);
100 gl
.bufferData(gl
.ARRAY_BUFFER
, view
, gl
.STATIC_DRAW
);
101 ok(gl
.getError() == 0, 'bufferData with SAB as input parameter works ok.');
102 gl
.bufferSubData(gl
.ARRAY_BUFFER
, 0, view
);
103 ok(gl
.getError() == 0, 'bufferSubData with SAB as input parameter works ok.');
104 gl
.enableVertexAttribArray(0);
105 gl
.vertexAttribPointer(0, 2, gl
.FLOAT
, false, 0, 0);
106 gl
.clearColor(0, 0, 0, 1.0);
107 gl
.clear(gl
.COLOR_BUFFER_BIT
);
108 gl
.uniform4f(prog
.uFragColor
, 0.2, 0.4, 0.6, 1.0);
109 gl
.drawArrays(gl
.TRIANGLE_STRIP
, 0, 4);
110 var arr
= new Uint8Array(4);
111 TestScreenColor(gl
, 0.2, 0.4, 0.6, 1.0);
113 // Test gl.texImage2D() and gl.texSubImage2D() APIs with SAB as input.
114 var tex
= gl
.createTexture();
115 gl
.bindTexture(gl
.TEXTURE_2D
, tex
);
119 var sab
= new SharedArrayBuffer(width
* height
* numChannels
);
120 var data
= new Uint8Array(sab
);
121 for (var i
= 0; i
< data
.length
; ++i
) {
124 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, width
, height
, 0, gl
.RGBA
, gl
.UNSIGNED_BYTE
, data
);
125 ok(gl
.getError() == 0, 'texImage2D() with SAB as input parameter works ok.');
126 gl
.texSubImage2D(gl
.TEXTURE_2D
, 0, 0, 0, width
, height
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, data
);
127 ok(gl
.getError() == 0, 'texSubImage2D() with SAB as input parameter works ok.');
129 ok(gl
.getError() == 0, 'Should be no errors after test.');
134 var canvas
= document
.getElementById('c2');
139 let gl
= canvas
.getContext('webgl2', attribs
);
141 todo(false, 'WebGL 2 is unavailable.');
144 if (typeof SharedArrayBuffer
=== 'undefined') {
145 todo(false, 'SharedArrayBuffer is unavailable.');
149 var arr
= new SharedArrayBuffer(8*4);
150 var view
= new Float32Array(arr
);
151 view
.set(new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]));
152 var vb
= gl
.createBuffer();
153 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vb
);
154 gl
.bufferData(gl
.ARRAY_BUFFER
, view
, gl
.STATIC_DRAW
);
156 var arr2
= new SharedArrayBuffer(8*4);
157 var view2
= new Float32Array(arr2
);
158 gl
.getBufferSubData(gl
.ARRAY_BUFFER
, 0, view2
);
160 for(var i
= 0; i
< 8; ++i
) {
161 if (view
[i
] != view2
[i
]) equal
= false;
163 ok(equal
, 'getBufferSubData with SAB as input parameter works ok.');
165 // Test gl.texImage3D() and gl.texSubImage3D() APIs with SAB as input.
166 var tex
= gl
.createTexture();
167 gl
.bindTexture(gl
.TEXTURE_3D
, tex
);
172 var sab
= new SharedArrayBuffer(width
* height
* depth
* numChannels
);
173 var data
= new Uint8Array(sab
);
174 for (var i
= 0; i
< data
.length
; ++i
) {
177 gl
.texImage3D(gl
.TEXTURE_3D
, 0, gl
.RGBA
, width
, height
, depth
, 0, gl
.RGBA
, gl
.UNSIGNED_BYTE
, data
);
178 ok(gl
.getError() == 0, 'texImage3D() with SAB as input parameter works ok.');
179 gl
.texSubImage3D(gl
.TEXTURE_3D
, 0, 0, 0, 0, width
, height
, depth
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, data
);
180 ok(gl
.getError() == 0, 'texSubImage3D() with SAB as input parameter works ok.');
182 ok(gl
.getError() == 0, 'Should be no errors after test.');
185 ok(true, 'TEST COMPLETE');