1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES Utilities
3 * ------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the 'License');
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an 'AS IS' BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 goog.provide('functional.gles3.es3fClippingTests');
23 goog.require('framework.common.tcuImageCompare');
24 goog.require('framework.common.tcuRGBA');
25 goog.require('framework.common.tcuTestCase');
26 goog.require('framework.delibs.debase.deMath');
27 goog.require('framework.delibs.debase.deRandom');
28 goog.require('framework.opengl.gluShaderUtil');
29 goog.require('framework.referencerenderer.rrUtil');
30 goog.require('functional.gles3.es3fFboTestCase');
31 goog.require('functional.gles3.es3fFboTestUtil');
33 goog.scope(function() {
34 var es3fClippingTests = functional.gles3.es3fClippingTests;
35 var tcuImageCompare = framework.common.tcuImageCompare;
36 var tcuTestCase = framework.common.tcuTestCase;
37 var es3fFboTestCase = functional.gles3.es3fFboTestCase;
38 var es3fFboTestUtil = functional.gles3.es3fFboTestUtil;
39 var rrUtil = framework.referencerenderer.rrUtil;
40 var gluShaderUtil = framework.opengl.gluShaderUtil;
41 var deRandom = framework.delibs.debase.deRandom;
42 var deMath = framework.delibs.debase.deMath;
43 var tcuRGBA = framework.common.tcuRGBA;
45 /** @type {WebGL2RenderingContext} */ var gl;
47 var setParentClass = function(child, parent) {
48 child.prototype = Object.create(parent.prototype);
49 child.prototype.constructor = child;
54 * @extends {es3fFboTestCase.FboTestCase}
55 * @param {string} name
56 * @param {string} desc
57 * @param {Array<number>} viewport
58 * @param {Array<number>} rangeX
59 * @param {Array<number>} rangeY
60 * @param {Array<number>} rangeZ
62 es3fClippingTests.TriangleCase = function(name, desc, viewport, rangeX, rangeY, rangeZ) {
63 es3fFboTestCase.FboTestCase.call(this, name, desc);
64 this.m_viewport = viewport;
65 this.m_rangeX = rangeX;
66 this.m_rangeY = rangeY;
67 this.m_rangeZ = rangeZ;
70 setParentClass(es3fClippingTests.TriangleCase, es3fFboTestCase.FboTestCase);
72 es3fClippingTests.TriangleCase.prototype.render = function(dst) {
73 var ctx = this.getCurrentContext();
74 var x = this.m_viewport[0];
75 var y = this.m_viewport[1];
76 var width = this.m_viewport[2];
77 var height = this.m_viewport[3];
78 ctx.viewport(x, y, width, height);
79 ctx.clearColor(0, 0, 0, 1);
80 ctx.clear(gl.COLOR_BUFFER_BIT);
82 var shader = new es3fFboTestUtil.GradientShader(gluShaderUtil.DataType.FLOAT_VEC4);
83 var program = ctx.createProgram(shader);
84 shader.setGradient(ctx, program, [0, 0, 0, 0], [1, 1, 1, 1]);
86 rrUtil.drawQuad(ctx, program,
87 [this.m_rangeX[0], this.m_rangeY[0], this.m_rangeZ[0]],
88 [this.m_rangeX[1], this.m_rangeY[1], this.m_rangeZ[1]]);
89 dst.readViewport(ctx, this.m_viewport);
93 * Move the vertex coordinate to pixel center
95 var center = function(x, width) {
97 var pos = half + x * half;
98 // almost to the center to avoid problems when rounding
99 // the position the pixel edge
100 pos = Math.round(pos) + 0.49;
101 return (pos - half) / half;
106 * @extends {es3fFboTestCase.FboTestCase}
107 * @param {string} name
108 * @param {string} desc
109 * @param {Array<number>} viewport
110 * @param {number} lineWidth
112 es3fClippingTests.LinesCase = function(name, desc, viewport, lineWidth) {
113 es3fFboTestCase.FboTestCase.call(this, name, desc);
114 this.m_viewport = viewport;
115 this.m_lineWidth = lineWidth;
118 setParentClass(es3fClippingTests.LinesCase, es3fFboTestCase.FboTestCase);
120 es3fClippingTests.LinesCase.prototype.compare = function(reference, result) {
121 return tcuImageCompare.bilinearCompare('Result', 'Image comparison result',
122 reference.getAccess(),
124 tcuRGBA.newRGBAComponents(3, 3, 3, 3));
127 es3fClippingTests.LinesCase.prototype.render = function(dst) {
128 var ctx = this.getCurrentContext();
129 var x = this.m_viewport[0];
130 var y = this.m_viewport[1];
131 var width = this.m_viewport[2];
132 var height = this.m_viewport[3];
133 ctx.viewport(x, y, width, height);
134 ctx.clearColor(0, 0, 0, 1);
135 ctx.clear(gl.COLOR_BUFFER_BIT);
137 var shader = new es3fFboTestUtil.GradientShader(gluShaderUtil.DataType.FLOAT_VEC4);
138 var program = ctx.createProgram(shader);
139 shader.setGradient(ctx, program, [0, 0, 0, 0], [1, 1, 1, 1]);
142 var posLoc = ctx.getAttribLocation(program, 'a_position');
144 throw new Error('a_position attribute is not defined.');
146 var buffer = ctx.createBuffer();
147 ctx.bindBuffer(gl.ARRAY_BUFFER, buffer);
149 ctx.lineWidth(this.m_lineWidth);
151 var y1 = center(-0.5, height);
152 var y2 = center(-0.2, height);
153 var y3 = center(0.2, height);
154 var y4 = center(0.5, height);
155 var y5 = center(0, height);
156 var x1 = center(-0.5, width);
157 var x2 = center(-0.2, width);
158 var x3 = center(0.2, width);
159 var x4 = center(0.5, width);
162 // both ends outside viewport
163 -1 - 1 / width, y1, 0, 1,
164 1 + 1 / width, y1, 0, 1,
165 // one end inside viewport
166 -1 + 1 / width, y2, 0, 1,
167 1 + 1 / width, y2, 0, 1,
169 -1 - 1 / width, y3, 0, 1,
170 1 - 1 / width, y3, 0, 1,
171 // both ends inside viewport
173 -1 + 1 / width, y4, 0, 1,
174 1 - 1 / width, y4, 0, 1,
177 // both ends outside viewport
178 x1, -1 - 1 / height, 0, 1,
179 x1, 1 + 1 / height, 0, 1,
181 // one end inside viewport
182 x2, -1 + 1 / height, 0, 1,
183 x2, 1 + 1 / height, 0, 1,
185 x3, -1 - 1 / height, 0, 1,
186 x3, 1 - 1 / height, 0, 1,
187 //both ends inside viewport
188 x4, -1 + 1 / height, 0, 1,
189 x4, 1 - 1 / height, 0, 1,
195 var numVertices = positions.length / 4;
197 ctx.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
199 ctx.enableVertexAttribArray(posLoc);
200 ctx.vertexAttribPointer(posLoc, 4, gl.FLOAT, false, 0, 0);
203 var coordLoc = ctx.getAttribLocation(program, 'a_coord');
205 throw new Error('a_coord attribute is not defined.');
207 var buffer2 = ctx.createBuffer();
208 ctx.bindBuffer(gl.ARRAY_BUFFER, buffer2);
211 for (var i = 0; i < numVertices / 2; i++) {
212 coords.push(0, 0, 1, 1);
214 ctx.bufferData(gl.ARRAY_BUFFER, new Float32Array(coords), gl.STATIC_DRAW);
216 ctx.enableVertexAttribArray(coordLoc);
217 ctx.vertexAttribPointer(coordLoc, 2, gl.FLOAT, false, 0, 0);
219 ctx.drawArrays(gl.LINES, 0, numVertices);
220 ctx.disableVertexAttribArray(posLoc);
221 ctx.disableVertexAttribArray(coordLoc);
222 ctx.bindBuffer(gl.ARRAY_BUFFER, null);
223 ctx.deleteBuffer(buffer);
224 ctx.deleteBuffer(buffer2);
225 dst.readViewport(ctx, this.m_viewport);
230 * @extends {es3fFboTestCase.FboTestCase}
231 * @param {string} name
232 * @param {string} desc
233 * @param {Array<number>} viewport
234 * @param {number} pointSize
236 es3fClippingTests.PointsCase = function(name, desc, viewport, pointSize) {
237 es3fFboTestCase.FboTestCase.call(this, name, desc);
238 this.m_viewport = viewport;
239 this.m_pointSize = pointSize;
242 setParentClass(es3fClippingTests.PointsCase, es3fFboTestCase.FboTestCase);
244 es3fClippingTests.PointsCase.prototype.compare = function(reference, result) {
245 return tcuImageCompare.bilinearCompare('Result', 'Image comparison result',
246 reference.getAccess(),
248 tcuRGBA.newRGBAComponents(3, 3, 3, 3));
251 es3fClippingTests.PointsCase.prototype.render = function(dst) {
252 var ctx = this.getCurrentContext();
253 var x = this.m_viewport[0];
254 var y = this.m_viewport[1];
255 var width = this.m_viewport[2];
256 var height = this.m_viewport[3];
257 ctx.viewport(x, y, width, height);
258 ctx.clearColor(0, 0, 0, 1);
259 ctx.clear(gl.COLOR_BUFFER_BIT);
261 var shader = new es3fFboTestUtil.FlatColorShader(gluShaderUtil.DataType.FLOAT_VEC4, this.m_pointSize);
262 var program = ctx.createProgram(shader);
263 shader.setColor(ctx, program, [0, 1, 0, 1]);
266 var posLoc = ctx.getAttribLocation(program, 'a_position');
268 throw new Error('a_position attribute is not defined.');
270 var buffer = ctx.createBuffer();
271 ctx.bindBuffer(gl.ARRAY_BUFFER, buffer);
274 // clipping in X axis
275 -1 - 1 / width, -0.5, 0, 1,
277 -1 + 1 / width, 0.5, 0, 1,
278 1 + 1 / width, -0.5, 0, 1,
280 1 - 1 / width, 0.5, 0, 1,
281 // clipping in Y axis
282 -0.5, -1 - 1 / height, 0, 1,
284 0.5, -1 + 1 / height, 0, 1,
285 -0.5, 1 - 1 / height, 0, 1,
287 0.5, 1 + 1 / height, 0, 1,
288 // clipping in Z axis
293 // move the vertices to pixel centers to avoid off-by-1 differences
294 for (var i = 0; i < positions.length; i += 4) {
295 positions[i + 0] = center(positions[i + 0], width);
296 positions[i + 1] = center(positions[i + 1], height);
298 // positions = [-1 + 3/width + 0.001, 1 + 1/height + 0.001, 0, 1];
299 // positions = [-1, -1, 0, 1];
301 var numVertices = positions.length / 4;
303 ctx.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
305 ctx.enableVertexAttribArray(posLoc);
306 ctx.vertexAttribPointer(posLoc, 4, gl.FLOAT, false, 0, 0);
308 ctx.drawArrays(gl.POINTS, 0, numVertices);
309 ctx.disableVertexAttribArray(posLoc);
310 ctx.bindBuffer(gl.ARRAY_BUFFER, null);
311 ctx.deleteBuffer(buffer);
312 dst.readViewport(ctx, this.m_viewport);
317 * @extends {tcuTestCase.DeqpTest}
319 es3fClippingTests.ClippingTests = function() {
320 tcuTestCase.DeqpTest.call(this, 'clipping', 'Clipping tests');
323 es3fClippingTests.ClippingTests.prototype = Object.create(tcuTestCase.DeqpTest.prototype);
324 es3fClippingTests.ClippingTests.prototype.constructor = es3fClippingTests.ClippingTests;
326 es3fClippingTests.ClippingTests.prototype.init = function() {
327 var width = gl.drawingBufferWidth;
328 var height = gl.drawingBufferHeight;
329 /** @const */ var WIDE_POINT = 5;
330 /** @const */ var WIDE_LINE = 5;
331 var viewports = [{ name: 'full_viewport', v: [0, 0, width, height] }, {
332 name: 'partial_viewport', v: [width * 0.3 , height * 0.2 , width * 0.6, height * 0.5] }
334 var pointSizeRange = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE);
335 var lineWidthRange = gl.getParameter(gl.ALIASED_LINE_WIDTH_RANGE);
337 for (var i = 0; i < viewports.length; i++) {
338 var v = viewports[i].v.map(Math.floor);
339 var vName = viewports[i].name;
340 this.addChild(new es3fClippingTests.LinesCase('narrow_lines_' + vName, 'lines', v, 1));
341 if (lineWidthRange[1] >= WIDE_LINE)
342 this.addChild(new es3fClippingTests.LinesCase('wide_lines_' + vName, 'lines', v, WIDE_LINE));
343 this.addChild(new es3fClippingTests.PointsCase('small_points_' + vName, 'points', v, 1));
344 if (pointSizeRange[1] >= WIDE_POINT)
345 this.addChild(new es3fClippingTests.PointsCase('wide_points_' + vName, 'points', v, WIDE_POINT));
362 for (var i = 0; i < viewports.length; i++) {
363 var v = viewports[i].v.map(Math.floor);
364 var vName = viewports[i].name;
365 for (var x = 0; x < rangesX.length; x++)
366 for (var y = 0; y < rangesY.length; y++)
367 for (var z = 0; z < rangesZ.length; z++) {
368 var rangeX = rangesX[x];
369 var rangeY = rangesY[y];
370 var rangeZ = rangesZ[z];
371 var name = 'triangles_' + viewports[i].name + '_' +
372 '(' + rangeX[0] + ',' + rangeY[0] + ',' + rangeZ[0] + ')-' +
373 '(' + rangeX[1] + ',' + rangeY[1] + ',' + rangeZ[1] + ')';
374 this.addChild(new es3fClippingTests.TriangleCase(name, 'triangles', v,
384 * @param {WebGL2RenderingContext} context
386 es3fClippingTests.run = function(context) {
388 //Set up Test Root parameters
389 var state = tcuTestCase.runner;
390 state.setRoot(new es3fClippingTests.ClippingTests());
392 //Set up name and description of this test series.
393 setCurrentTestName(state.testCases.fullName());
394 description(state.testCases.getDescription());
398 tcuTestCase.runTestCases();
401 testFailedOptions('Failed to es3fClippingTests.run tests', false);
402 tcuTestCase.runner.terminate();