3 <script src=
"../../../resources/js-test.js"></script>
4 <script src=
"resources/webgl-test.js"></script>
5 <script src=
"resources/webgl-test-utils.js"></script>
6 <script id=
"fshader" type=
"x-shader/x-fragment">
10 uniform sampler2D tex;
11 varying vec2 texCoord;
15 float intensity = texture2D(tex, texCoord).a;
16 gl_FragColor = vec4(intensity, intensity, intensity,
1.0);
22 <canvas id=
"example" width=
"256px" height=
"1px"></canvas>
23 <div id=
"description"></div>
24 <div id=
"console"></div>
26 description('Tests texSubImage2D upload path from Uint8Array');
28 var wtu
= WebGLTestUtils
;
29 var canvas
= document
.getElementById("example");
30 var gl
= wtu
.create3DContext(canvas
);
31 var program
= wtu
.setupProgram(
33 [wtu
.setupSimpleTextureVertexShader(gl
),
34 wtu
.loadShaderFromScript(gl
, "fshader")],
35 ['vPosition', 'texCoord0']);
36 wtu
.setupUnitQuad(gl
);
37 var textureWidth
= 256;
38 var textureHeight
= 1;
40 textureLoc
= gl
.getUniformLocation(program
, "tex");
42 var texture
= gl
.createTexture();
43 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
44 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_WRAP_S
, gl
.CLAMP_TO_EDGE
);
45 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_WRAP_T
, gl
.CLAMP_TO_EDGE
);
46 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MIN_FILTER
, gl
.NEAREST
);
47 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MAG_FILTER
, gl
.NEAREST
);
48 // Allocate the texture object
49 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.ALPHA
, textureWidth
, textureHeight
, 0, gl
.ALPHA
, gl
.UNSIGNED_BYTE
, null);
50 // Prepare the image data
51 var array
= new Uint8Array(textureWidth
);
52 for (var i
= 0; i
< textureWidth
; i
++)
54 // Fill the texture object with data -- this is actually the code path being tested
55 gl
.texSubImage2D(gl
.TEXTURE_2D
, 0, 0, 0, textureWidth
, textureHeight
, gl
.ALPHA
, gl
.UNSIGNED_BYTE
, array
);
58 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
59 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
60 gl
.useProgram(program
);
61 gl
.uniform1i(textureLoc
, 0);
62 // Draw the texture to the frame buffer
63 gl
.drawArrays(gl
.TRIANGLES
, 0, 6);
65 // Read back the frame buffer
66 var buf
= new Uint8Array(textureWidth
* textureHeight
* 4);
67 gl
.readPixels(0, 0, textureWidth
, textureHeight
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
69 // Verify the frame buffer's contents
71 for (var i
= 0; i
< textureWidth
; i
++) {
73 if (buf
[4 * i
+ 0] != val
||
74 buf
[4 * i
+ 1] != val
||
75 buf
[4 * i
+ 2] != val
) {
76 testFailed("pixel at (" + i
+ ", 0) was (" +
77 buf
[4 * i
+ 0] + ", " +
78 buf
[4 * i
+ 1] + ", " +
79 buf
[4 * i
+ 2] + "), should be (" +
80 val
+ ", " + val
+ ", " + val
+ ")");