5 <title>WebGL Canvas Conformance Tests
</title>
6 <script src=
"../../../resources/js-test.js"></script>
7 <script src=
"resources/webgl-test.js"></script>
8 <script src=
"resources/webgl-test-utils.js"></script>
11 <div id=
"description"></div>
12 <div id=
"console"></div>
13 <script id=
"vshaderGrid" type=
"x-shader/x-vertex">
14 attribute vec4 a_position;
17 gl_Position = a_position;
21 <script id=
"fshaderGrid" type=
"x-shader/x-fragment">
22 precision mediump float;
25 float r = mod(gl_FragCoord.x,
2.0) <
1.0 ?
0.0 :
1.0;
26 float g = mod(gl_FragCoord.y,
2.0) <
1.0 ?
0.0 :
1.0;
27 gl_FragColor = vec4(r, g,
0,
1);
31 description("This test ensures WebGL implementations correctly implement drawingbufferWidth/Height.");
33 function getMaxViewportDimensions() {
34 // create a fresh canvas. This canvas will be discarded
35 // after exiting this function.
36 var canvas
= document
.createElement("canvas");
37 gl
= wtu
.create3DContext(canvas
, {antialias
: false});
39 testFailed("context does not exist");
42 testPassed("context exists");
44 // For a default size canvas these should be equal.
45 // WebGL contexts are not allowed to change the size of the drawingBuffer
46 // for things like hi-res displays.
47 shouldBe('gl.drawingBufferWidth', 'gl.canvas.width');
48 shouldBe('gl.drawingBufferHeight', 'gl.canvas.height');
49 return gl
.getParameter(gl
.MAX_VIEWPORT_DIMS
);
53 // This uses gl_FragCoord to draw a device pixel relavent pattern.
54 // If drawBufferWidth or drawBufferHeight are not in device pixels
55 // this test should fail.
56 function checkGrid(gl
) {
57 var program
= wtu
.setupProgram(gl
, ["vshaderGrid", "fshaderGrid"], ["a_position"]);
58 wtu
.setupUnitQuad(gl
);
59 gl
.useProgram(program
);
60 shouldBe('gl.getError()', 'gl.NO_ERROR');
64 var pixels
= new Uint8Array(gl
.drawingBufferWidth
* gl
.drawingBufferHeight
* 4);
65 gl
.readPixels(0, 0, gl
.drawingBufferWidth
, gl
.drawingBufferHeight
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixels
);
68 [ { color
: [0, 0, 0, 255], name
: "black" }, { color
: [255, 0, 0, 255], name
: "red" } ],
69 [ { color
: [0, 255, 0, 255], name
: "green" }, { color
: [255, 255, 0, 255], name
: "yellow" } ],
72 for (var yy
= 0; yy
< gl
.drawingBufferHeight
; ++yy
) {
73 for (var xx
= 0; xx
< gl
.drawingBufferWidth
; ++xx
) {
74 var info
= colors
[yy
% 2][xx
% 2];
75 var color
= info
.color
;
76 var offset
= (yy
* gl
.drawingBufferWidth
+ xx
) * 4;
77 for (var jj
= 0; jj
< 4; ++jj
) {
78 if (pixels
[offset
+ jj
] != color
[jj
]) {
79 var actual
= [pixels
[offset
], pixels
[offset
+ 1], pixels
[offset
+ 2], pixels
[offset
+ 3]];
80 testFailed("at " + xx
+ ", " + yy
+ " expected " + color
+ "(" + info
.name
+ ") was " + actual
);
86 testPassed("grid rendered correctly");
89 // This passes device coordinate vertices in to make sure gl.viewport is not being mucked with.
90 function checkQuad(gl
) {
91 var deviceToClipSpace = function(value
, range
) {
92 return value
/ range
* 2 - 1;
95 var program
= wtu
.setupColorQuad(gl
);
97 // draw a small green square in the top right corner.
98 var deviceX1
= gl
.drawingBufferWidth
- 4;
99 var deviceX2
= gl
.drawingBufferWidth
;
100 var deviceY1
= gl
.drawingBufferHeight
- 4;
101 var deviceY2
= gl
.drawingBufferHeight
;
103 var clipX1
= deviceToClipSpace(deviceX1
, gl
.drawingBufferWidth
);
104 var clipX2
= deviceToClipSpace(deviceX2
, gl
.drawingBufferWidth
);
105 var clipY1
= deviceToClipSpace(deviceY1
, gl
.drawingBufferHeight
);
106 var clipY2
= deviceToClipSpace(deviceY2
, gl
.drawingBufferHeight
);
108 var vertexObject
= gl
.createBuffer();
109 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vertexObject
);
120 gl
.enableVertexAttribArray(0);
121 gl
.vertexAttribPointer(0, 2, gl
.FLOAT
, false, 0, 0);
123 var green
= [0, 255, 0, 255];
124 var black
= [0, 0, 0, 0];
125 gl
.clearColor(0, 0, 0, 0);
126 gl
.clear(gl
.COLOR_BUFFER_BIT
);
127 wtu
.drawUByteColorQuad(gl
, [0, 255, 0, 255]);
129 var squareWidth
= deviceX2
- deviceX1
;
130 var squareHeight
= deviceY2
- deviceY1
;
133 wtu
.checkCanvasRect(gl
, deviceX1
, deviceY1
, squareWidth
, squareHeight
, green
, "should be green");
134 // check outside the square.
135 wtu
.checkCanvasRect(gl
, 0, 0, gl
.drawingBufferWidth
, gl
.drawingBufferHeight
- squareHeight
, black
, "should be black");
136 wtu
.checkCanvasRect(gl
, 0, gl
.drawingBufferHeight
- squareHeight
, gl
.drawingBufferWidth
- squareWidth
, squareHeight
, black
, "should be black");
140 function test(desiredWidth
, desiredHeight
) {
142 debug("Checking drawingBufferWidth/drawingBufferHeight");
144 // Make a fresh canvas.
145 var canvas
= document
.createElement("canvas");
146 canvas
.width
= desiredWidth
;
147 canvas
.height
= desiredHeight
;
149 // This 'gl' must be global for shouldBe to work.
150 gl
= wtu
.create3DContext(canvas
, {antialias
: false});
152 testFailed("context does not exist");
154 testPassed("context exists");
156 // Verify these stats didn't change since they come from a different
158 shouldBe('gl.getParameter(gl.MAX_VIEWPORT_DIMS)[0]', 'maxSize[0]');
159 shouldBe('gl.getParameter(gl.MAX_VIEWPORT_DIMS)[1]', 'maxSize[1]');
161 // check the initial viewport matches the drawingBufferWidth and drawingBufferHeight
162 shouldBe('gl.getParameter(gl.VIEWPORT)[0]', '0');
163 shouldBe('gl.getParameter(gl.VIEWPORT)[1]', '0');
164 shouldBe('gl.getParameter(gl.VIEWPORT)[2]', 'gl.drawingBufferWidth');
165 shouldBe('gl.getParameter(gl.VIEWPORT)[3]', 'gl.drawingBufferHeight');
167 // Draw a pixel grid using a shader that draws in device coordinates
169 // Draw a quad in the top right corner.
172 shouldBe('gl.getError()', 'gl.NO_ERROR');
175 debug("Testing resizing canvas");
177 oldViewport
= gl
.getParameter(gl
.VIEWPORT
);
179 // flip width and height
180 canvas
.width
= desiredHeight
;
181 canvas
.height
= desiredWidth
;
183 // Verify the viewport didn't change.
184 shouldBe('gl.getParameter(gl.VIEWPORT)[0]', 'oldViewport[0]');
185 shouldBe('gl.getParameter(gl.VIEWPORT)[1]', 'oldViewport[1]');
186 shouldBe('gl.getParameter(gl.VIEWPORT)[2]', 'oldViewport[2]');
187 shouldBe('gl.getParameter(gl.VIEWPORT)[3]', 'oldViewport[3]');
190 gl
.viewport(0, 0, gl
.drawingBufferWidth
, gl
.drawingBufferHeight
);
192 // Draw a pixel grid using a shader that draws in device coordinates
195 // Draw a quad in the top right corner.
197 shouldBe('gl.getError()', 'gl.NO_ERROR');
201 var wtu
= WebGLTestUtils
;
202 var maxSize
= getMaxViewportDimensions();
204 shouldBeTrue('maxSize[0] > 0');
205 shouldBeTrue('maxSize[1] > 0');
207 // test a small size to make sure it works at all.
210 // Make a canvas slightly larger than the max size WebGL can handle.
211 // From section 2.2 of the spec the WebGL implementation should allow this to work.
213 // test a size larger than MAX_VIEWPORT_DIMS in both dimensions
214 test(maxSize
[0] + 32, 8);