2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <title>WebGL Viewport Test
</title>
12 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
13 <script src=
"../../js/js-test-pre.js"></script>
14 <script src=
"../../js/webgl-test-utils.js"></script>
17 border:
1px solid #
000;
22 <canvas id=
"canvas1" width=
"64" height=
"128"> </canvas>
23 <canvas id=
"canvas2" width=
"64" height=
"128"> </canvas>
24 <div id=
"description"></div>
25 <div id=
"console"></div>
29 var wtu
= WebGLTestUtils
;
31 function test(canvas
, attribs
) {
32 var gl
= wtu
.create3DContext(canvas
, attribs
);
35 testFailed("context does not exist");
37 testPassed("context exists");
39 var blue
= [0, 0, 255, 255];
40 var black
= [0, 0, 0, 0];
42 var draw = function(viewportX
, viewportY
, viewportWidth
, viewportHeight
) {
43 gl
.viewport(viewportX
, viewportY
, viewportWidth
, viewportHeight
);
44 gl
.clear(gl
.COLOR_BUFFER_BIT
);
45 wtu
.drawUByteColorQuad(gl
, blue
);
48 var drawAndCheck = function(viewportX
, viewportY
, viewportWidth
, viewportHeight
) {
49 var clipSpaceToPixelSpace = function(clip
, viewportOffset
, viewportSize
, max
) {
50 var pixel
= viewportSize
/ 2 * clip
+ viewportOffset
+ viewportSize
/ 2;
51 return Math
.min(max
, Math
.max(0, pixel
));
54 var x1
= clipSpaceToPixelSpace(-0.5, viewportX
, viewportWidth
, gl
.canvas
.width
);
55 var x2
= clipSpaceToPixelSpace( 0.5, viewportX
, viewportWidth
, gl
.canvas
.width
);
56 var y1
= clipSpaceToPixelSpace(-0.5, viewportY
, viewportHeight
, gl
.canvas
.height
);
57 var y2
= clipSpaceToPixelSpace( 0.5, viewportY
, viewportHeight
, gl
.canvas
.height
);
61 debug("checking viewport: " + viewportX
+ ", " + viewportY
+ ", " + viewportWidth
+ ", " + viewportHeight
);
62 debug("rect: " + x1
+ ", " + y1
+ ", " + width
+ ", " + height
);
63 draw(viewportX
, viewportY
, viewportWidth
, viewportHeight
);
64 wtu
.checkAreaInAndOut(gl
, x1
, y1
, width
, height
, blue
, black
);
67 var program
= wtu
.setupSimpleColorProgram(gl
);
68 wtu
.setupQuad(gl
, {scale
: 0.5});
70 var w
= gl
.canvas
.width
;
71 var h
= gl
.canvas
.height
;
73 drawAndCheck(0, 0, w
, h
);
74 drawAndCheck(0, 0, w
/2, h/4);
75 drawAndCheck(0, 0, w
/4, h/2);
76 drawAndCheck(0, 0, w
*2, h
*2);
78 drawAndCheck(-w
, 0, w
, h
);
79 drawAndCheck(0, -h
, w
, h
);
80 drawAndCheck(w
, 0, w
, h
);
81 drawAndCheck(0, h
, w
, h
);
83 drawAndCheck(w
/4, h/2, w
, h
);
84 drawAndCheck(w
/4, h/2, w
/2, h/4);
85 drawAndCheck(w
/2, h/4, w
/4, h/2);
86 drawAndCheck(w
/2, h/4, w
, h
*2);
88 drawAndCheck(-w
, 0, w
*2, h
);
89 drawAndCheck(0, -h
/4, w/2, h
);
90 drawAndCheck(-w
/4, 0, w, h/2);
91 drawAndCheck(0, -h
, w
*2, h
*2);
94 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "there should be no errors");
98 debug("test antialias: false");
99 test(document
.getElementById("canvas1"), {antialias
: false});
102 debug("test antialias: true");
103 test(document
.getElementById("canvas2"), {antialias
: true});
106 var successfullyParsed
= true;
109 <script src=
"../../js/js-test-post.js"></script>