c5b8377c6ce95565d9d426e6d9020caae853f908
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / rendering / point-with-gl-pointcoord-in-fragment-shader.html
1 <!--
2
3 /*
4 ** Copyright (c) 2013 The Khronos Group Inc.
5 **
6 ** Permission is hereby granted, free of charge, to any person obtaining a
7 ** copy of this software and/or associated documentation files (the
8 ** "Materials"), to deal in the Materials without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Materials, and to
11 ** permit persons to whom the Materials are furnished to do so, subject to
12 ** the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
16 **
17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
24 */
25
26 -->
27
28 <!DOCTYPE html>
29 <html>
30 <head>
31 <meta charset="utf-8">
32 <title>WebGL Point with gl_PointCoord in Fragment Shader Test</title>
33 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
34 <script src="../../resources/js-test-pre.js"></script>
35 <script src="../resources/webgl-test-utils.js"></script>
36 </head>
37 <body>
38 <div id="description"></div>
39 <canvas id="canvas" width="64" height="64"> </canvas>
40 <div id="console"></div>
41 <script id="vs" type="x-shader/x-vertex">
42 varying vec4 v_color;
43
44 // The X and Y coordinates of the center of the point.
45 attribute vec2 a_vertex;
46
47 uniform float u_pointSize;
48
49 void main(void) {
50   gl_PointSize = u_pointSize;
51   gl_Position = vec4(a_vertex, 0.0, 1.0);
52
53   // The color of the point.
54   v_color = vec4(0.0, 1.0, 0.0, 1.0);
55 }
56
57 </script>
58 <script id="fs" type="x-shader/x-fragment">
59 precision mediump float;
60
61 varying vec4 v_color;
62
63 void main(void) {
64   // It seems as long as this mathematical expression references
65   // gl_PointCoord, the fragment's color is incorrect.
66   vec2 diff = gl_PointCoord - vec2(.5, .5);
67   if (length(diff) > 0.5)
68     discard;
69
70   // The point should be a solid color.
71   gl_FragColor = v_color;
72 }
73 </script>
74 <script>
75 "use strict";
76 // Radar 13239314
77 description("This is a regression test for a graphics driver bug affecting end caps on roads in MapsGL.");
78
79 debug("");
80
81 var wtu = WebGLTestUtils;
82 var canvas = document.getElementById("canvas");
83 var canvasWidth = canvas.width;
84 var canvasHeight = canvas.height;
85 var output = document.getElementById("console");
86 var gl = wtu.create3DContext(canvas);
87
88 function withinRange(value, expected, delta) {
89   return Math.abs(value - expected) < delta;
90 }
91
92 function runTest() {
93   var pointSizeRange = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE);
94   // This test can't really run without a maximum point size of at least 2
95   if (pointSizeRange[1] < 2.0) {
96     debug("This test needs a maximum ALIASED_POINT_SIZE_RANGE of at least 2");
97     return;
98   }
99
100   var vs = wtu.loadShaderFromScript(gl, "vs", gl.VERTEX_SHADER);
101   var fs = wtu.loadShaderFromScript(gl, "fs", gl.FRAGMENT_SHADER);
102   if (!vs || !fs) {
103     testFailed("Loading shaders failed");
104     return;
105   }
106
107   var program = wtu.setupProgram(gl, [vs, fs], ['a_vertex']);
108   if (!program) {
109     testFailed("Loading program failed");
110     return;
111   }
112
113   gl.useProgram(program);
114   gl.clearColor(0, 0, 0, 1.0);
115   gl.disable(gl.DEPTH_TEST);
116   gl.clear(gl.COLOR_BUFFER_BIT);
117
118   // uniform float u_pointSize;
119   var uni = gl.getUniformLocation(program, 'u_pointSize');
120   gl.uniform1f(uni, Math.min(20.0, pointSizeRange[1]));
121
122   // vertex
123   var buffer = gl.createBuffer();
124   gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
125   var vertexData = new Float32Array([
126     0, 0,
127   ]);
128   gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.STATIC_DRAW);
129   gl.enableVertexAttribArray(0);
130   gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
131
132   gl.drawArrays(gl.POINTS, 0, 1);
133   var pixel = new Uint8Array(4);
134   // Read from approximately the center of the canvas
135   gl.readPixels(canvasWidth / 2, canvasHeight / 2, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
136   if (withinRange(pixel[0], 0, 2) &&
137       withinRange(pixel[1], 255, 2) &&
138       withinRange(pixel[2], 0, 2) &&
139       withinRange(pixel[3], 255, 2)) {
140     testPassed("Center pixel was green");
141   } else {
142     testFailed("Center pixel was [" + pixel[0] + ", " + pixel[1] + ", " + pixel[2] + ", " + pixel[3] + "], should be [0, 255, 0, 255]");
143   }
144 }
145
146 runTest();
147
148 debug("");
149 var successfullyParsed = true;
150 </script>
151 <script src="../../resources/js-test-post.js"></script>
152 </body>
153 </html>