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.es3fDefaultVertexAttributeTests');
23 goog.require('framework.common.tcuLogImage');
24 goog.require('framework.common.tcuSurface');
25 goog.require('framework.common.tcuTestCase');
26 goog.require('framework.delibs.debase.deMath');
27 goog.require('framework.opengl.gluShaderProgram');
28 goog.require('framework.opengl.gluShaderUtil');
30 goog.scope(function() {
31 var es3fDefaultVertexAttributeTests = functional.gles3.es3fDefaultVertexAttributeTests;
32 var tcuTestCase = framework.common.tcuTestCase;
33 var tcuSurface = framework.common.tcuSurface;
34 var deMath = framework.delibs.debase.deMath;
35 var gluShaderProgram = framework.opengl.gluShaderProgram;
36 var gluShaderUtil = framework.opengl.gluShaderUtil;
37 var tcuLogImage = framework.common.tcuLogImage;
39 var setParentClass = function(child, parent) {
40 child.prototype = Object.create(parent.prototype);
41 child.prototype.constructor = child;
47 es3fDefaultVertexAttributeTests.LoaderVertexAttrib1f = function() {
48 this.caseName = 'vertex_attrib_1f';
49 this.name = 'VertexAttrib1f';
51 this.load = function(index, value) {
52 gl.vertexAttrib1f(index, value[0]);
53 return [value[0], 0, 0, 1];
60 es3fDefaultVertexAttributeTests.LoaderVertexAttrib2f = function() {
61 this.caseName = 'vertex_attrib_2f';
62 this.name = 'VertexAttrib2f';
64 this.load = function(index, value) {
65 gl.vertexAttrib2f(index, value[0], value[1]);
66 return [value[0], value[1], 0, 1];
73 es3fDefaultVertexAttributeTests.LoaderVertexAttrib3f = function() {
74 this.caseName = 'vertex_attrib_3f';
75 this.name = 'VertexAttrib3f';
77 this.load = function(index, value) {
78 gl.vertexAttrib3f(index, value[0], value[1], value[2]);
79 return [value[0], value[1], value[2], 1];
86 es3fDefaultVertexAttributeTests.LoaderVertexAttrib4f = function() {
87 this.caseName = 'vertex_attrib_4f';
88 this.name = 'VertexAttrib4f';
90 this.load = function(index, value) {
91 gl.vertexAttrib4f(index, value[0], value[1], value[2], value[3]);
92 return [value[0], value[1], value[2], value[3]];
99 es3fDefaultVertexAttributeTests.LoaderVertexAttrib1fv = function() {
100 this.caseName = 'vertex_attrib_1fv';
101 this.name = 'VertexAttrib1fv';
103 this.load = function(index, value) {
104 gl.vertexAttrib1fv(index, value.slice(0, 1));
105 return [value[0], 0, 0, 1];
112 es3fDefaultVertexAttributeTests.LoaderVertexAttrib2fv = function() {
113 this.caseName = 'vertex_attrib_2fv';
114 this.name = 'VertexAttrib2fv';
116 this.load = function(index, value) {
117 gl.vertexAttrib2fv(index, value.slice(0, 2));
118 return [value[0], value[1], 0, 1];
125 es3fDefaultVertexAttributeTests.LoaderVertexAttrib3fv = function() {
126 this.caseName = 'vertex_attrib_3fv';
127 this.name = 'VertexAttrib3fv';
129 this.load = function(index, value) {
130 gl.vertexAttrib3fv(index, value.slice(0, 3));
131 return [value[0], value[1], value[2], 1];
138 es3fDefaultVertexAttributeTests.LoaderVertexAttrib4fv = function() {
139 this.caseName = 'vertex_attrib_4fv';
140 this.name = 'VertexAttrib4fv';
142 this.load = function(index, value) {
143 gl.vertexAttrib4fv(index, value.slice(0, 4));
144 return [value[0], value[1], value[2], value[3]];
151 es3fDefaultVertexAttributeTests.LoaderVertexAttribI4i = function() {
152 this.caseName = 'vertex_attrib_4i';
153 this.name = 'VertexAttribI4i';
155 this.load = function(index, value) {
156 var v = new Int32Array(value);
157 gl.vertexAttribI4i(index, v[0], v[1], v[2], v[3]);
158 return [v[0], v[1], v[2], v[3]];
165 es3fDefaultVertexAttributeTests.LoaderVertexAttribI4iv = function() {
166 this.caseName = 'vertex_attrib_4iv';
167 this.name = 'VertexAttribI4iv';
169 this.load = function(index, value) {
170 var v = new Int32Array(value);
171 gl.vertexAttribI4iv(index, v);
172 return [v[0], v[1], v[2], v[3]];
179 es3fDefaultVertexAttributeTests.LoaderVertexAttribI4ui = function() {
180 this.caseName = 'vertex_attrib_4ui';
181 this.name = 'VertexAttribI4ui';
183 this.load = function(index, value) {
184 var v = new Uint32Array(value);
185 gl.vertexAttribI4ui(index, v[0], v[1], v[2], v[3]);
186 return [v[0], v[1], v[2], v[3]];
193 es3fDefaultVertexAttributeTests.LoaderVertexAttribI4uiv = function() {
194 this.caseName = 'vertex_attrib_4uiv';
195 this.name = 'VertexAttribI4uiv';
197 this.load = function(index, value) {
198 var v = new Uint32Array(value);
199 gl.vertexAttribI4uiv(index, v);
200 return [v[0], v[1], v[2], v[3]];
204 /** @const */ var RENDER_SIZE = 32;
205 /** @const */ var s_valueRange = 10;
206 /** @const */ var s_passThroughFragmentShaderSource = '#version 300 es\n' +
207 'layout(location = 0) out mediump vec4 fragColor;\n' +
208 'in mediump vec4 v_color;\n' +
209 'void main (void)\n' +
211 ' fragColor = v_color;\n' +
216 * @extends {tcuTestCase.DeqpTest}
218 es3fDefaultVertexAttributeTests.AttributeCase = function(loaderType, dataType) {
219 var loader = new loaderType();
220 var name = loader.caseName;
221 var description = 'Test ' + loader.name;
222 tcuTestCase.DeqpTest.call(this, name, description);
223 this.m_funcName = loader.name;
224 this.m_useNegativeValues = loader.signed;
225 this.m_dataType = dataType;
226 this.m_allIterationsPassed = true;
227 this.m_loader = loader;
228 this.m_iteration = 0;
231 setParentClass(es3fDefaultVertexAttributeTests.AttributeCase, tcuTestCase.DeqpTest);
233 es3fDefaultVertexAttributeTests.AttributeCase.prototype.init = function() {
236 var maxRange = s_valueRange;
237 var minRange = (this.m_useNegativeValues) ? (-maxRange) : (0.0);
239 bufferedLogToConsole(
240 'Loading attribute values using ' + this.m_funcName + '\n' +
241 'Attribute type: ' + gluShaderUtil.getDataTypeName(this.m_dataType) + '\n' +
242 'Attribute value range: [' + minRange + ', ' + maxRange + ']');
244 // gen shader and base quad
246 this.m_program = new gluShaderProgram.ShaderProgram(gl, gluShaderProgram.makeVtxFragSources(this.genVertexSource(), s_passThroughFragmentShaderSource));
247 if (!this.m_program.isOk())
248 testFailedOptions('could not build program', true);
250 var fullscreenQuad = [
257 this.m_bufID = gl.createBuffer();
258 gl.bindBuffer(gl.ARRAY_BUFFER, this.m_bufID);
259 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(fullscreenQuad), gl.STATIC_DRAW);
262 es3fDefaultVertexAttributeTests.AttributeCase.prototype.deinit = function() {
263 this.m_loader = null;
266 this.m_program = null;
269 gl.deleteBuffer(this.m_bufID);
274 es3fDefaultVertexAttributeTests.AttributeCase.prototype.iterate = function() {
276 [0.0, 0.5, 0.2, 1.0],
277 [0.1, 0.7, 1.0, 0.6],
278 [0.4, 0.2, 0.0, 0.5],
279 [0.5, 0.0, 0.9, 0.1],
280 [0.6, 0.2, 0.2, 0.9],
281 [0.9, 1.0, 0.0, 0.0],
285 bufferedLogToConsole('Iteration ' + (this.m_iteration + 1) + '/' + testValues.length);
287 var testValue = this.m_useNegativeValues ?
288 deMath.subScalar(deMath.scale(testValues[this.m_iteration], 2), 1) :
289 deMath.scale(testValues[this.m_iteration], s_valueRange);
291 if (!this.renderWithValue(testValue))
292 this.m_allIterationsPassed = false;
296 if (++this.m_iteration < testValues.length)
297 return tcuTestCase.IterateResult.CONTINUE;
299 if (this.m_allIterationsPassed)
302 testFailed('Got unexpected values');
304 return tcuTestCase.IterateResult.STOP;
307 es3fDefaultVertexAttributeTests.AttributeCase.prototype.genVertexSource = function() {
308 var vectorSize = (gluShaderUtil.isDataTypeMatrix(this.m_dataType)) ? (gluShaderUtil.getDataTypeMatrixNumRows(this.m_dataType)) : (gluShaderUtil.isDataTypeVector(this.m_dataType)) ? (gluShaderUtil.getDataTypeScalarSize(this.m_dataType)) : (-1);
309 var vectorType = gluShaderUtil.getDataTypeName((gluShaderUtil.isDataTypeMatrix(this.m_dataType)) ? (gluShaderUtil.getDataTypeVector(gluShaderUtil.DataType.FLOAT, vectorSize)) : (gluShaderUtil.isDataTypeVector(this.m_dataType)) ? (gluShaderUtil.getDataTypeVector(gluShaderUtil.DataType.FLOAT, vectorSize)) : (gluShaderUtil.DataType.FLOAT));
310 var components = (gluShaderUtil.isDataTypeMatrix(this.m_dataType)) ? (gluShaderUtil.getDataTypeMatrixNumRows(this.m_dataType)) : (gluShaderUtil.getDataTypeScalarSize(this.m_dataType));
312 var buf = '#version 300 es\n' +
313 'in highp vec4 a_position;\n' +
314 'in highp ' + gluShaderUtil.getDataTypeName(this.m_dataType) + ' a_value;\n' +
315 'out highp vec4 v_color;\n' +
316 'void main (void)\n' +
318 ' gl_Position = a_position;\n' +
321 buf += ' highp ' + vectorType + ' normalizedValue = ' + ((gluShaderUtil.getDataTypeScalarType(this.m_dataType) == gluShaderUtil.DataType.FLOAT) ? ('') : (vectorType)) + '(a_value' + ((gluShaderUtil.isDataTypeMatrix(this.m_dataType)) ? ('[1]') : ('')) + ') / float(' + s_valueRange + ');\n';
323 if (this.m_useNegativeValues)
324 buf += ' highp ' + vectorType + ' positiveNormalizedValue = (normalizedValue + ' + vectorType + '(1.0)) / 2.0;\n';
326 buf += ' highp ' + vectorType + ' positiveNormalizedValue = normalizedValue;\n';
329 buf += ' v_color = vec4(positiveNormalizedValue, 0.0, 0.0, 1.0);\n';
330 else if (components == 2)
331 buf += ' v_color = vec4(positiveNormalizedValue.xy, 0.0, 1.0);\n';
332 else if (components == 3)
333 buf += ' v_color = vec4(positiveNormalizedValue.xyz, 1.0);\n';
334 else if (components == 4)
335 buf += ' v_color = vec4((positiveNormalizedValue.xy + positiveNormalizedValue.zz) / 2.0, positiveNormalizedValue.w, 1.0);\n';
337 throw new Error('Wrong component size: ' + components);
344 es3fDefaultVertexAttributeTests.AttributeCase.prototype.renderWithValue = function(v) {
345 var positionIndex = gl.getAttribLocation(this.m_program.getProgram(), 'a_position');
346 var valueIndex = gl.getAttribLocation(this.m_program.getProgram(), 'a_value');
347 var dest = new tcuSurface.Surface(RENDER_SIZE, RENDER_SIZE);
349 gl.clearColor(0.0, 0.0, 0.0, 0.0);
350 gl.clear(gl.COLOR_BUFFER_BIT);
351 gl.viewport(0, 0, RENDER_SIZE, RENDER_SIZE);
353 gl.bindBuffer(gl.ARRAY_BUFFER, this.m_bufID);
354 gl.vertexAttribPointer(positionIndex, 4, gl.FLOAT, false, 0, 0);
355 gl.enableVertexAttribArray(positionIndex);
357 // transfer test value. Load to the second column in the matrix case
358 var loadedValue = this.m_loader.load((gluShaderUtil.isDataTypeMatrix(this.m_dataType)) ? (valueIndex + 1) : (valueIndex), v);
360 gl.useProgram(this.m_program.getProgram());
361 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
363 // The original c++ test does not disable vertex attrib array, which is wrong.
364 // On most drivers all tests pass because a_position is assigned location 0.
365 // On MacOSX some tests fail because a_value is assigned location 0 and vertex
366 // attrib array is left enabled and affects later tests.
367 gl.disableVertexAttribArray(positionIndex);
368 dest.readViewport(gl);
370 // check whole result is colored correctly
371 return this.verifyUnicoloredBuffer(dest, this.computeColor(loadedValue));
374 es3fDefaultVertexAttributeTests.AttributeCase.prototype.computeColor = function(value) {
375 var normalizedValue = deMath.scale(value, 1 / s_valueRange);
376 var positiveNormalizedValue = this.m_useNegativeValues ?
377 deMath.scale(deMath.addScalar(normalizedValue, 1), 0.5) :
379 var components = (gluShaderUtil.isDataTypeMatrix(this.m_dataType)) ? (gluShaderUtil.getDataTypeMatrixNumRows(this.m_dataType)) : (gluShaderUtil.getDataTypeScalarSize(this.m_dataType));
382 return [positiveNormalizedValue[0], 0.0, 0.0, 1.0];
383 else if (components == 2)
384 return [positiveNormalizedValue[0], positiveNormalizedValue[1], 0.0, 1.0];
385 else if (components == 3)
386 return [positiveNormalizedValue[0], positiveNormalizedValue[1], positiveNormalizedValue[2], 1.0];
387 else if (components == 4)
388 return [(positiveNormalizedValue[0] + positiveNormalizedValue[2]) / 2.0, (positiveNormalizedValue[1] + positiveNormalizedValue[2]) / 2.0, positiveNormalizedValue[3], 1.0];
390 throw new Error('Wrong component size: ' + components);
394 * @param {tcuSurface.Surface} scene
395 * @param {Array<number>} refColor
398 es3fDefaultVertexAttributeTests.AttributeCase.prototype.verifyUnicoloredBuffer = function(scene, refColor) {
399 var access = scene.getAccess();
400 var errorMask = new tcuSurface.Surface(RENDER_SIZE, RENDER_SIZE);
401 var colorThreshold = [6, 6, 6, 6];
404 errorMask.getAccess().clear([0, 1, 0, 1]);
406 bufferedLogToConsole('Verifying rendered image. Expecting color ' + refColor + ', threshold ' + colorThreshold);
408 for (var y = 0; y < RENDER_SIZE; ++y)
409 for (var x = 0; x < RENDER_SIZE; ++x) {
410 var color = access.getPixel(x, y);
412 if (Math.abs(color[0] - refColor[0]) > colorThreshold[0] ||
413 Math.abs(color[1] - refColor[1]) > colorThreshold[1] ||
414 Math.abs(color[2] - refColor[2]) > colorThreshold[2]) {
418 debug('Found invalid pixel(s). Pixel at (' + x + ', ' + y + ') color: ' + color);
421 errorMask.setPixel(x, y, [1, 0, 0, 1]);
426 bufferedLogToConsole('Rendered image is valid.');
428 tcuLogImage.logImage('Result', '', access);
429 tcuLogImage.logImage('Error mask', '', errorMask.getAccess());
437 * @extends {tcuTestCase.DeqpTest}
439 es3fDefaultVertexAttributeTests.DefaultVertexAttributeTests = function() {
440 tcuTestCase.DeqpTest.call(this, 'default_vertex_attrib', 'Test default vertex attributes');
443 es3fDefaultVertexAttributeTests.DefaultVertexAttributeTests.prototype = Object.create(tcuTestCase.DeqpTest.prototype);
444 es3fDefaultVertexAttributeTests.DefaultVertexAttributeTests.prototype.constructor = es3fDefaultVertexAttributeTests.DefaultVertexAttributeTests;
446 es3fDefaultVertexAttributeTests.DefaultVertexAttributeTests.prototype.init = function() {
449 ['float', gluShaderUtil.DataType.FLOAT, false],
450 ['vec2', gluShaderUtil.DataType.FLOAT_VEC2, true],
451 ['vec3', gluShaderUtil.DataType.FLOAT_VEC3, true],
452 ['vec4', gluShaderUtil.DataType.FLOAT_VEC4, false],
453 ['mat2', gluShaderUtil.DataType.FLOAT_MAT2, true],
454 ['mat2x3', gluShaderUtil.DataType.FLOAT_MAT2X3, true],
455 ['mat2x4', gluShaderUtil.DataType.FLOAT_MAT2X4, true],
456 ['mat3', gluShaderUtil.DataType.FLOAT_MAT3, true],
457 ['mat3x2', gluShaderUtil.DataType.FLOAT_MAT3X2, true],
458 ['mat3x4', gluShaderUtil.DataType.FLOAT_MAT3X4, true],
459 ['mat4', gluShaderUtil.DataType.FLOAT_MAT4, false],
460 ['mat4x2', gluShaderUtil.DataType.FLOAT_MAT4X2, true],
461 ['mat4x3', gluShaderUtil.DataType.FLOAT_MAT4X3, true]
464 floatTargets.forEach(function(elem) {
466 var dataType = elem[1];
467 var reduced = elem[2];
468 var group = new tcuTestCase.DeqpTest(name, 'test with ' + name);
469 tests.addChild(group);
470 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttrib1f, dataType));
472 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttrib2f, dataType));
473 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttrib3f, dataType));
475 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttrib4f, dataType));
477 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttrib1fv, dataType));
479 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttrib2fv, dataType));
480 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttrib3fv, dataType));
482 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttrib4fv, dataType));
487 ['int', gluShaderUtil.DataType.INT, false],
488 ['ivec2', gluShaderUtil.DataType.INT_VEC2, true],
489 ['ivec3', gluShaderUtil.DataType.INT_VEC3, true],
490 ['ivec4', gluShaderUtil.DataType.INT_VEC4, false]
493 intTargets.forEach(function(elem) {
495 var dataType = elem[1];
496 var reduced = elem[2];
497 var group = new tcuTestCase.DeqpTest(name, 'test with ' + name);
498 tests.addChild(group);
499 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttribI4i, dataType));
500 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttribI4iv, dataType));
504 ['uint', gluShaderUtil.DataType.UINT, false],
505 ['uvec2', gluShaderUtil.DataType.UINT_VEC2, true],
506 ['uvec3', gluShaderUtil.DataType.UINT_VEC3, true],
507 ['uvec4', gluShaderUtil.DataType.UINT_VEC4, false]
510 uintTargets.forEach(function(elem) {
512 var dataType = elem[1];
513 var reduced = elem[2];
514 var group = new tcuTestCase.DeqpTest(name, 'test with ' + name);
515 tests.addChild(group);
516 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttribI4ui, dataType));
517 group.addChild(new es3fDefaultVertexAttributeTests.AttributeCase(es3fDefaultVertexAttributeTests.LoaderVertexAttribI4uiv, dataType));
524 * @param {WebGL2RenderingContext} context
526 es3fDefaultVertexAttributeTests.run = function(context) {
528 //Set up Test Root parameters
529 var state = tcuTestCase.runner;
530 state.setRoot(new es3fDefaultVertexAttributeTests.DefaultVertexAttributeTests());
532 //Set up name and description of this test series.
533 setCurrentTestName(state.testCases.fullName());
534 description(state.testCases.getDescription());
538 tcuTestCase.runTestCases();
541 testFailedOptions('Failed to es3fDefaultVertexAttributeTests.run tests', false);
542 tcuTestCase.runner.terminate();