3 <script src=
"../../../resources/js-test.js"></script>
4 <script src=
"resources/webgl-test.js"></script>
5 <script id=
"vshader" type=
"x-shader/x-vertex">
6 attribute vec3 g_Position;
7 attribute vec2 g_TexCoord0;
13 gl_Position = vec4(g_Position.x, g_Position.y, g_Position.z,
1.0);
14 texCoord = g_TexCoord0;
18 <script id=
"fshader" type=
"x-shader/x-fragment">
20 precision mediump float;
22 uniform sampler2D tex;
23 varying vec2 texCoord;
26 gl_FragColor = texture2D(tex, texCoord);
34 if (window
.initNonKhronosFramework
) {
35 window
.initNonKhronosFramework(true);
38 description('Verify copyTexImage2D and copyTexSubImage2D');
43 // These two declarations need to be global for "shouldBe" to see them
44 var pixel
= [0, 0, 0];
45 var correctColor
= null;
48 function runTestIteration(antialias
)
51 gl
= initWebGL("antialiasOn", "vshader", "fshader", [ "g_Position", "g_TexCoord0" ], [ 0, 0, 0, 1 ], 1);
53 gl
= initWebGL("antialiasOff", "vshader", "fshader", [ "g_Position", "g_TexCoord0" ], [ 0, 0, 0, 1 ], 1, { antialias
: false });
55 var textureLoc
= gl
.getUniformLocation(gl
.program
, "tex");
57 var vertices
= new Float32Array([
64 var texCoords
= new Float32Array([
71 var texCoordOffset
= vertices
.byteLength
;
73 var vbo
= gl
.createBuffer();
74 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vbo
);
75 gl
.bufferData(gl
.ARRAY_BUFFER
,
76 texCoordOffset
+ texCoords
.byteLength
,
78 gl
.bufferSubData(gl
.ARRAY_BUFFER
, 0, vertices
);
79 gl
.bufferSubData(gl
.ARRAY_BUFFER
, texCoordOffset
, texCoords
);
81 gl
.enableVertexAttribArray(0);
82 gl
.vertexAttribPointer(0, 3, gl
.FLOAT
, false, 0, 0);
83 gl
.enableVertexAttribArray(1);
84 gl
.vertexAttribPointer(1, 2, gl
.FLOAT
, false, 0, texCoordOffset
);
86 gl
.colorMask(1, 1, 1, 0);
88 debug('Testing copyTexImage2D');
91 gl
.clearColor(1, 0, 0, 1);
92 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
94 var texture
= gl
.createTexture();
95 // Bind the texture to texture unit 0
96 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
98 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, 2, 2, 0, gl
.RGBA
, gl
.UNSIGNED_BYTE
, null);
99 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MIN_FILTER
, gl
.NEAREST
);
100 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MAG_FILTER
, gl
.NEAREST
);
102 glErrorShouldBe(gl
, gl
.NO_ERROR
);
103 gl
.copyTexImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, 0, 0, 2, 2, 0);
104 glErrorShouldBe(gl
, gl
.NO_ERROR
);
107 gl
.clearColor(0, 1, 0, 1);
108 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
110 // Point the uniform sampler to texture unit 0
111 gl
.uniform1i(textureLoc
, 0);
112 // Draw the triangles
113 gl
.drawArrays(gl
.TRIANGLES
, 0, 6);
115 // Read back the rendering results, should be red
116 var buf
= new Uint8Array(2 * 2 * 4);
117 gl
.readPixels(0, 0, 2, 2, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
119 correctColor
= [255, 0, 0];
120 for (var y
= 0; y
< 2; y
++) {
121 for (var x
= 0; x
< 2; x
++) {
122 idx
= (y
* 2 + x
) * 4;
124 pixel
[1] = buf
[idx
+ 1];
125 pixel
[2] = buf
[idx
+ 2];
126 shouldBe("pixel", "correctColor");
130 debug('Testing copyTexSubImage2D');
133 gl
.clearColor(0, 1, 0, 1);
134 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
136 glErrorShouldBe(gl
, gl
.NO_ERROR
);
137 gl
.copyTexSubImage2D(gl
.TEXTURE_2D
, 0, 0, 0, 0, 0, 2, 2);
138 glErrorShouldBe(gl
, gl
.NO_ERROR
);
141 gl
.clearColor(0, 0, 1, 1);
142 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
);
144 // Draw the triangles
145 gl
.drawArrays(gl
.TRIANGLES
, 0, 6);
147 // Read back the rendering results, should be green
148 gl
.readPixels(0, 0, 2, 2, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
149 correctColor
= [0, 255, 0];
150 for (var y
= 0; y
< 2; y
++) {
151 for (var x
= 0; x
< 2; x
++) {
152 idx
= (y
* 2 + x
) * 4;
154 pixel
[1] = buf
[idx
+ 1];
155 pixel
[2] = buf
[idx
+ 2];
156 shouldBe("pixel", "correctColor");
161 function runTest(antialias
)
163 debug("Testing with antialias on");
164 runTestIteration(true);
165 debug("Testing with antialias off");
166 runTestIteration(false);
171 <body onload=
"init()">
172 <canvas id=
"antialiasOn" width=
"2px" height=
"2px"></canvas>
173 <canvas id=
"antialiasOff" width=
"2px" height=
"2px"></canvas>
174 <div id=
"description"></div>
175 <div id=
"console"></div>