Bug 1918529 - fix some subpixel misalignment issues with gfx.webrender.svg-filter...
[gecko.git] / dom / canvas / test / webgl-conf / checkout / deqp / framework / common / tcuMatrix.js
blobe2959ecdc2ce16f29f5d32ede897e28b96b869ae
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.
20 'use strict';
21 goog.provide('framework.common.tcuMatrix');
22 goog.require('framework.delibs.debase.deMath');
24 goog.scope(function() {
26 var tcuMatrix = framework.common.tcuMatrix;
27 var deMath = framework.delibs.debase.deMath;
29 var DE_ASSERT = function(x) {
30 if (!x)
31 throw new Error('Assert failed');
34 /**
35 * @constructor
36 * @param {number} rows
37 * @param {number} cols
38 * @param {*=} value
39 * Initialize to identity.
41 tcuMatrix.Matrix = function(rows, cols, value) {
42 value = value == undefined ? 1 : value;
43 this.rows = rows;
44 this.cols = cols;
45 this.matrix = [];
46 for (var i = 0; i < cols; i++)
47 this.matrix[i] = [];
48 for (var row = 0; row < rows; row++)
49 for (var col = 0; col < cols; col++)
50 this.set(row, col, (row == col) ? value : 0);
53 /**
54 * @param {number} rows
55 * @param {number} cols
56 * @param {Array<number>} vector
57 * @return {tcuMatrix.Matrix}
59 tcuMatrix.matrixFromVector = function(rows, cols, vector) {
60 var matrix = new tcuMatrix.Matrix(rows, cols);
61 for (var row = 0; row < vector.length; row++)
62 for (var col = 0; col < vector.length; col++)
63 matrix.matrix[col][row] = row == col ? vector[row] : 0;
64 return matrix;
67 /**
68 * @param {number} rows
69 * @param {number} cols
70 * @param {Array<number>} src
71 * @return {tcuMatrix.Matrix}
73 tcuMatrix.matrixFromDataArray = function(rows, cols, src) {
74 var matrix = new tcuMatrix.Matrix(rows, cols);
75 for (var row = 0; row < rows; row++) {
76 for (var col = 0; col < cols; col++) {
77 matrix.matrix[col][row] = src[row * cols + col];
80 return matrix;
83 /**
84 * Fill the Matrix with data from array
85 * @param {number} rows
86 * @param {number} cols
87 * @param {Array<number>} array
88 * @return {tcuMatrix.Matrix}
90 tcuMatrix.matrixFromArray = function(rows, cols, array) {
91 DE_ASSERT(array.length === rows * cols);
92 var matrix = new tcuMatrix.Matrix(rows, cols);
93 for (var row = 0; row < rows; row++)
94 for (var col = 0; col < cols; col++)
95 matrix.matrix[col][row] = array[row * cols + col];
96 return matrix;
99 tcuMatrix.Matrix.prototype.set = function(x, y, value) {
100 this.isRangeValid(x, y);
101 this.matrix[y][x] = value;
104 tcuMatrix.Matrix.prototype.setRow = function(row, values) {
105 if (!deMath.deInBounds32(row, 0, this.rows))
106 throw new Error('Rows out of range');
107 if (values.length > this.cols)
108 throw new Error('Too many columns');
109 for (var col = 0; col < values.length; col++)
110 this.matrix[col][row] = values[col];
113 tcuMatrix.Matrix.prototype.setCol = function(col, values) {
114 if (!deMath.deInBounds32(col, 0, this.cols))
115 throw new Error('Columns out of range');
116 if (values.length > this.rows)
117 throw new Error('Too many rows');
118 for (var row = 0; row < values.length; row++)
119 this.matrix[col][row] = values[row];
122 tcuMatrix.Matrix.prototype.get = function(x, y) {
123 this.isRangeValid(x, y);
124 return this.matrix[y][x];
127 tcuMatrix.Matrix.prototype.getColumn = function(y) {
128 return this.matrix[y];
131 tcuMatrix.Matrix.prototype.isRangeValid = function(x, y) {
132 if (!deMath.deInBounds32(x, 0, this.rows))
133 throw new Error('Rows out of range');
134 if (!deMath.deInBounds32(y, 0, this.cols))
135 throw new Error('Columns out of range');
139 * @return {Array<number>}
141 tcuMatrix.Matrix.prototype.getColumnMajorData = function() {
142 /** @type {Array<number>} */ var a = [];
143 for (var col = 0; col < this.cols; col++)
144 for (var row = 0; row < this.rows; row++)
145 a.push(this.get(row, col));
146 return a;
150 * @param {tcuMatrix.Matrix} matrixA
151 * @param {tcuMatrix.Matrix} matrixB
152 * @return {tcuMatrix.Matrix}
154 tcuMatrix.add = function(matrixA, matrixB) {
155 var res = new tcuMatrix.Matrix(matrixA.rows, matrixB.cols);
156 for (var col = 0; col < matrixA.cols; col++)
157 for (var row = 0; row < matrixA.rows; row++)
158 res.set(row, col, matrixA.get(row, col) + matrixB.get(row, col));
159 return res;
163 * @param {tcuMatrix.Matrix} matrixA
164 * @param {tcuMatrix.Matrix} matrixB
165 * @return {tcuMatrix.Matrix}
167 tcuMatrix.subtract = function(matrixA, matrixB) {
168 var res = new tcuMatrix.Matrix(matrixA.rows, matrixB.cols);
169 for (var col = 0; col < matrixA.cols; col++)
170 for (var row = 0; row < matrixA.rows; row++)
171 res.set(row, col, matrixA.get(row, col) - matrixB.get(row, col));
172 return res;
176 * @param {tcuMatrix.Matrix} matrixA
177 * @param {tcuMatrix.Matrix} matrixB
178 * @return {tcuMatrix.Matrix}
179 * Multiplication of two matrices.
181 tcuMatrix.multiply = function(matrixA, matrixB) {
182 if (matrixA.cols != matrixB.rows)
183 throw new Error('Wrong matrices sizes');
184 var res = new tcuMatrix.Matrix(matrixA.rows, matrixB.cols);
185 for (var row = 0; row < matrixA.rows; row++)
186 for (var col = 0; col < matrixB.cols; col++) {
187 var v = 0;
188 for (var ndx = 0; ndx < matrixA.cols; ndx++)
189 v += matrixA.get(row, ndx) * matrixB.get(ndx, col);
190 res.set(row, col, v);
192 return res;
196 * @param {tcuMatrix.Matrix} matrixA
197 * @param {tcuMatrix.Matrix} matrixB
198 * @return {tcuMatrix.Matrix}
200 tcuMatrix.divide = function(matrixA, matrixB) {
201 var res = new tcuMatrix.Matrix(matrixA.rows, matrixA.cols);
202 for (var col = 0; col < matrixA.cols; col++)
203 for (var row = 0; row < matrixA.rows; row++)
204 res.set(row, col, matrixA.get(row, col) / matrixB.get(row, col));
205 return res;
209 * @param {tcuMatrix.Matrix} mtx
210 * @param {Array<number>} vec
211 * @return {Array<number>}
213 tcuMatrix.multiplyMatVec = function(mtx, vec) {
214 /** @type {Array<number>} */ var res = [];
215 /** @type {number} */ var value;
216 for (var row = 0; row < mtx.rows; row++) {
217 value = 0;
218 for (var col = 0; col < mtx.cols; col++)
219 value += mtx.get(row, col) * vec[col];
220 res[row] = value;
223 return res;
227 * @param {Array<number>} vec
228 * @param {tcuMatrix.Matrix} mtx
229 * @return {Array<number>}
231 tcuMatrix.multiplyVecMat = function(vec, mtx) {
232 /** @type {Array<number>} */ var res = [];
233 /** @type {number} */ var value;
234 for (var col = 0; col < mtx.cols; col++) {
235 value = 0;
236 for (var row = 0; row < mtx.rows; row++)
237 value += mtx.get(row, col) * vec[row];
238 res[col] = value;
241 return res;
244 tcuMatrix.Matrix.prototype.toString = function() {
245 var str = 'mat' + this.cols;
246 if (this.rows !== this.cols)
247 str += 'x' + this.rows;
248 str += '(';
249 for (var col = 0; col < this.cols; col++) {
250 str += '[';
251 for (var row = 0; row < this.rows; row++) {
252 str += this.matrix[col][row];
253 if (row != this.rows - 1)
254 str += ', ';
256 str += ']';
258 if (col != this.cols - 1)
259 str += ', ';
261 str += ')';
262 return str;
266 * @param {tcuMatrix.Matrix} mtx
267 * @param {number} scalar
268 * @return {tcuMatrix.Matrix}
270 tcuMatrix.subtractMatScal = function(mtx, scalar) {
271 /** @type {tcuMatrix.Matrix} */ var res = new tcuMatrix.Matrix(mtx.rows, mtx.cols);
272 for (var col = 0; col < mtx.cols; col++)
273 for (var row = 0; row < mtx.rows; row++)
274 res.set(row, col, mtx.get(row, col) - scalar);
276 return res;
280 * @param {tcuMatrix.Matrix} mtx
281 * @param {number} scalar
282 * @return {tcuMatrix.Matrix}
284 tcuMatrix.addMatScal = function(mtx, scalar) {
285 /** @type {tcuMatrix.Matrix} */ var res = new tcuMatrix.Matrix(mtx.rows, mtx.cols);
286 for (var col = 0; col < mtx.cols; col++)
287 for (var row = 0; row < mtx.rows; row++)
288 res.set(row, col, mtx.get(row, col) + scalar);
290 return res;
294 * @param {tcuMatrix.Matrix} mtx
295 * @param {number} scalar
296 * @return {tcuMatrix.Matrix}
298 tcuMatrix.multiplyMatScal = function(mtx, scalar) {
299 /** @type {tcuMatrix.Matrix} */ var res = new tcuMatrix.Matrix(mtx.rows, mtx.cols);
300 for (var col = 0; col < mtx.cols; col++)
301 for (var row = 0; row < mtx.rows; row++)
302 res.set(row, col, mtx.get(row, col) * scalar);
304 return res;
308 * @param {tcuMatrix.Matrix} mtx
309 * @param {number} scalar
310 * @return {tcuMatrix.Matrix}
312 tcuMatrix.divideMatScal = function(mtx, scalar) {
313 /** @type {tcuMatrix.Matrix} */ var res = new tcuMatrix.Matrix(mtx.rows, mtx.cols);
314 for (var col = 0; col < mtx.cols; col++)
315 for (var row = 0; row < mtx.rows; row++)
316 res.set(row, col, mtx.get(row, col) / scalar);
318 return res;
322 * @constructor
323 * @extends {tcuMatrix.Matrix}
325 tcuMatrix.Mat2 = function() {
326 tcuMatrix.Matrix.call(this, 2, 2);
329 tcuMatrix.Mat2.prototype = Object.create(tcuMatrix.Matrix.prototype);
330 tcuMatrix.Mat2.prototype.constructor = tcuMatrix.Mat2;
333 * @constructor
334 * @extends {tcuMatrix.Matrix}
336 tcuMatrix.Mat3 = function() {
337 tcuMatrix.Matrix.call(this, 3, 3);
340 tcuMatrix.Mat3.prototype = Object.create(tcuMatrix.Matrix.prototype);
341 tcuMatrix.Mat3.prototype.constructor = tcuMatrix.Mat3;
344 * @constructor
345 * @extends {tcuMatrix.Matrix}
347 tcuMatrix.Mat4 = function() {
348 tcuMatrix.Matrix.call(this, 4, 4);
351 tcuMatrix.Mat4.prototype = Object.create(tcuMatrix.Matrix.prototype);
352 tcuMatrix.Mat4.prototype.constructor = tcuMatrix.Mat4;