diff options
Diffstat (limited to 'dom/canvas/test/webgl-conf/checkout/deqp/framework/common')
21 files changed, 14287 insertions, 0 deletions
diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuBilinearImageCompare.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuBilinearImageCompare.js new file mode 100644 index 000000000..bc23104c0 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuBilinearImageCompare.js @@ -0,0 +1,272 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuBilinearImageCompare'); +goog.require('framework.common.tcuRGBA'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + + var tcuBilinearImageCompare = framework.common.tcuBilinearImageCompare; + var deMath = framework.delibs.debase.deMath; + var tcuTexture = framework.common.tcuTexture; + var tcuRGBA = framework.common.tcuRGBA; + + var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); + }; + + // for bilinear interpolation + /** @const {number} */ tcuBilinearImageCompare.NUM_SUBPIXEL_BITS = 8; + + // Algorithm assumes that colors are packed to 32-bit values as dictated by + // tcu::RGBA::*_SHIFT values. + + function UintRGBA8_R(color) { + return (color >> 24) & 0xff; + } + function UintRGBA8_G(color) { + return (color >> 16) & 0xff; + } + function UintRGBA8_B(color) { + return (color >> 8) & 0xff; + } + function UintRGBA8_A(color) { + return color & 0xff; + } + + /** + * @param {number} fx1 deUint32 + * @param {number} fy1 deUint32 + * @param {number} p00 deUint8 + * @param {number} p01 deUint8 + * @param {number} p10 deUint8 + * @param {number} p11 deUint8 + * @return {number} deUint8 + */ + tcuBilinearImageCompare.interpolateChannel = function(fx1, fy1, p00, p01, p10, p11) { + /** @const {number} */ var fx0 = (1 << tcuBilinearImageCompare.NUM_SUBPIXEL_BITS) - fx1; + /** @const {number} */ var fy0 = (1 << tcuBilinearImageCompare.NUM_SUBPIXEL_BITS) - fy1; + /** @const {number} */ + var half = 1 << (tcuBilinearImageCompare.NUM_SUBPIXEL_BITS * 2 - 1); + /** @const {number} */ var sum = + (fx0 * fy0 * p00) + + (fx1 * fy0 * p10) + + (fx0 * fy1 * p01) + + (fx1 * fy1 * p11); + /** @const {number} */ + var rounded = (sum + half) >> (tcuBilinearImageCompare.NUM_SUBPIXEL_BITS * 2); + + DE_ASSERT(deMath.deInRange32(rounded, 0, 0xff)); + return rounded; + }; + + tcuBilinearImageCompare.compareUintRGBA8Threshold = function(a, b, thr) { + if (a == b) + return true; + + return (Math.abs(UintRGBA8_R(a) - UintRGBA8_R(b)) <= thr.getRed() && + Math.abs(UintRGBA8_G(a) - UintRGBA8_G(b)) <= thr.getGreen() && + Math.abs(UintRGBA8_B(a) - UintRGBA8_B(b)) <= thr.getBlue() && + Math.abs(UintRGBA8_A(a) - UintRGBA8_A(b)) <= thr.getAlpha()); + }; + + /** + * @param {tcuTexture.RGBA8View} view + * @param {number} u + * @param {number} v + * @return {number} + */ + tcuBilinearImageCompare.bilinearSampleUintRGBA8 = function(view, u, v) { + /** @type {number} */ var x0 = u >> tcuBilinearImageCompare.NUM_SUBPIXEL_BITS; + /** @type {number} */ var y0 = v >> tcuBilinearImageCompare.NUM_SUBPIXEL_BITS; + /** @type {number} */ var x1 = x0 + 1; + /** @type {number} */ var y1 = y0 + 1; + + DE_ASSERT(x1 < view.getWidth()); + DE_ASSERT(y1 < view.getHeight()); + + /** @type {number} */ var fx1 = u - (x0 << tcuBilinearImageCompare.NUM_SUBPIXEL_BITS); + /** @type {number} */ var fy1 = v - (y0 << tcuBilinearImageCompare.NUM_SUBPIXEL_BITS); + + /** @type {Array<number>} */ var channelsP00 = view.readUintRGBA8(x0, y0); + /** @type {Array<number>} */ var channelsP10 = view.readUintRGBA8(x1, y0); + /** @type {Array<number>} */ var channelsP01 = view.readUintRGBA8(x0, y1); + /** @type {Array<number>} */ var channelsP11 = view.readUintRGBA8(x1, y1); + + /** @type {number} */ var res = 0; + + res = (tcuBilinearImageCompare.interpolateChannel(fx1, fy1, UintRGBA8_R(channelsP00), + UintRGBA8_R(channelsP01), UintRGBA8_R(channelsP10), UintRGBA8_R(channelsP11)) & 0xff) << 24; + res += (tcuBilinearImageCompare.interpolateChannel(fx1, fy1, UintRGBA8_G(channelsP00), + UintRGBA8_G(channelsP01), UintRGBA8_G(channelsP10), UintRGBA8_G(channelsP11)) & 0xff) << 16; + res += (tcuBilinearImageCompare.interpolateChannel(fx1, fy1, UintRGBA8_B(channelsP00), + UintRGBA8_B(channelsP01), UintRGBA8_B(channelsP10), UintRGBA8_B(channelsP11)) & 0xff) << 8; + res += tcuBilinearImageCompare.interpolateChannel(fx1, fy1, UintRGBA8_A(channelsP00), + UintRGBA8_A(channelsP01), UintRGBA8_A(channelsP10), UintRGBA8_A(channelsP11)) & 0xff; + + return res; + }; + + /** + * @param {tcuTexture.RGBA8View} reference + * @param {tcuTexture.RGBA8View} result + * @param {tcuRGBA.RGBA} threshold + * @param {number} x + * @param {number} y + * @return {boolean} + */ + tcuBilinearImageCompare.comparePixelRGBA8 = function(reference, result, threshold, x, y) { + /** @const {tcuRGBA.RGBA} */ var resPix = result.readUintRGBA8(x, y); + + // Step 1: Compare result pixel to 3x3 neighborhood pixels in reference. + /** @const {number} */ var x0 = Math.max(x - 1, 0); + /** @const {number} */ var x1 = x; + /** @const {number} */ + var x2 = Math.min(x + 1, reference.getWidth() - 1); + /** @const {number} */ var y0 = Math.max(y - 1, 0); + /** @const {number} */ var y1 = y; + /** @const {number} */ + var y2 = Math.min(y + 1, reference.getHeight() - 1); + + //tcuBilinearImageCompare.readRGBA8List (reference, x0, y0, x2, y2); + + if (tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x1, y1), threshold) || + tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x0, y1), threshold) || + tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x2, y1), threshold) || + tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x0, y0), threshold) || + tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x1, y0), threshold) || + tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x2, y0), threshold) || + tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x0, y2), threshold) || + tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x1, y2), threshold) || + tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, reference.readUintRGBA8(x2, y2), threshold)) + return true; + + // Step 2: Compare using bilinear sampling. + // \todo [pyry] Optimize sample positions! + /** @const {Array<Array<number>>} */ var s_offsets = [ + [226, 186], + [335, 235], + [279, 334], + [178, 272], + [112, 202], + [306, 117], + [396, 299], + [206, 382], + [146, 96], + [423, 155], + [361, 412], + [84, 339], + [48, 130], + [367, 43], + [455, 367], + [105, 439], + [83, 46], + [217, 24], + [461, 71], + [450, 459], + [239, 469], + [67, 267], + [459, 255], + [13, 416], + [10, 192], + [141, 502], + [503, 304], + [380, 506] + ]; + + for (var sampleNdx = 0; sampleNdx < s_offsets.length; sampleNdx++) { + /** @const {number} */ + var u = ((x - 1) << tcuBilinearImageCompare.NUM_SUBPIXEL_BITS) + s_offsets[sampleNdx][0]; + /** @const {number} */ + var v = ((y - 1) << tcuBilinearImageCompare.NUM_SUBPIXEL_BITS) + s_offsets[sampleNdx][1]; + + if (!deMath.deInBounds32(u, 0, (reference.getWidth() - 1) << tcuBilinearImageCompare.NUM_SUBPIXEL_BITS) || + !deMath.deInBounds32(v, 0, (reference.getHeight() - 1) << tcuBilinearImageCompare.NUM_SUBPIXEL_BITS)) + continue; + + if (tcuBilinearImageCompare.compareUintRGBA8Threshold(resPix, tcuBilinearImageCompare.bilinearSampleUintRGBA8(reference, u, v), threshold)) + return true; + } + + return false; + }; + + /** + * @param {tcuTexture.RGBA8View} reference + * @param {tcuTexture.RGBA8View} result + * @param {tcuTexture.PixelBufferAccess} errorMask + * @param {tcuRGBA.RGBA} threshold + * @return {boolean} + */ + tcuBilinearImageCompare.bilinearCompareRGBA8 = function(reference, result, errorMask, threshold) { + DE_ASSERT(reference.getFormat().isEqual(new tcuTexture.TextureFormat( + tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8))); + DE_ASSERT(result.getFormat().isEqual(new tcuTexture.TextureFormat( + tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8))); + + // Clear error mask first to green (faster this way). + errorMask.clear([0.0, 1.0, 0.0, 1.0]); + + /** @type {boolean} */ var allOk = true; + + for (var y = 0; y < reference.getHeight(); y++) { + for (var x = 0; x < reference.getWidth(); x++) { + if (!tcuBilinearImageCompare.comparePixelRGBA8(reference, result, threshold, x, y) && + !tcuBilinearImageCompare.comparePixelRGBA8(result, reference, threshold, x, y)) { + allOk = false; + errorMask.setPixel([1.0, 0.0, 0.0, 1.0], x, y); + } + } + } + + return allOk; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} reference + * @param {tcuTexture.ConstPixelBufferAccess} result + * @param {tcuTexture.PixelBufferAccess} errorMask + * @param {tcuRGBA.RGBA} threshold + * @return {boolean} + */ + tcuBilinearImageCompare.bilinearCompare = function(reference, result, errorMask, threshold) { + assertMsgOptions(result.getWidth() == reference.getWidth() && result.getHeight() == reference.getHeight() && result.getDepth() == reference.getDepth(), + 'Reference and result images have different dimensions', false, true); + + assertMsgOptions(errorMask.getWidth() == reference.getWidth() && errorMask.getHeight() == reference.getHeight() && errorMask.getDepth() == reference.getDepth(), + 'Reference and error mask images have different dimensions', false, true); + + /** @type {boolean} */ var isEqual = reference.getFormat().isEqual( + new tcuTexture.TextureFormat( + tcuTexture.ChannelOrder.RGBA, + tcuTexture.ChannelType.UNORM_INT8)); + if (isEqual) { + /** @type {tcuTexture.RGBA8View} */ var refView = new tcuTexture.RGBA8View(reference); + /** @type {tcuTexture.RGBA8View} */ var resView = new tcuTexture.RGBA8View(result); + return tcuBilinearImageCompare.bilinearCompareRGBA8(refView, resView, errorMask, threshold); + } else + throw new Error('Unsupported format for bilinear comparison'); + }; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuCompressedTexture.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuCompressedTexture.js new file mode 100644 index 000000000..a309f81cf --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuCompressedTexture.js @@ -0,0 +1,967 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/*--------------------------------------------------------------------*//*! + * \brief Map tcu::TextureFormat to GL pixel transfer format. + * + * Maps generic texture format description to GL pixel transfer format. + * If no mapping is found, throws tcu::InternalError. + * + * \param texFormat Generic texture format. + * \return GL pixel transfer format. + *//*--------------------------------------------------------------------*/ +'use strict'; +goog.provide('framework.common.tcuCompressedTexture'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + +var tcuCompressedTexture = framework.common.tcuCompressedTexture; +var tcuTexture = framework.common.tcuTexture; +var deMath = framework.delibs.debase.deMath; + + var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); + }; + +/** + * @enum + */ +tcuCompressedTexture.Format = { + ETC1_RGB8: 0, + EAC_R11: 1, + EAC_SIGNED_R11: 2, + EAC_RG11: 3, + EAC_SIGNED_RG11: 4, + ETC2_RGB8: 5, + ETC2_SRGB8: 6, + ETC2_RGB8_PUNCHTHROUGH_ALPHA1: 7, + ETC2_SRGB8_PUNCHTHROUGH_ALPHA1: 8, + ETC2_EAC_RGBA8: 9, + ETC2_EAC_SRGB8_ALPHA8: 10, + + ASTC_4x4_RGBA: 11, + ASTC_5x4_RGBA: 12, + ASTC_5x5_RGBA: 13, + ASTC_6x5_RGBA: 14, + ASTC_6x6_RGBA: 15, + ASTC_8x5_RGBA: 16, + ASTC_8x6_RGBA: 17, + ASTC_8x8_RGBA: 18, + ASTC_10x5_RGBA: 19, + ASTC_10x6_RGBA: 20, + ASTC_10x8_RGBA: 21, + ASTC_10x10_RGBA: 22, + ASTC_12x10_RGBA: 23, + ASTC_12x12_RGBA: 24, + ASTC_4x4_SRGB8_ALPHA8: 25, + ASTC_5x4_SRGB8_ALPHA8: 26, + ASTC_5x5_SRGB8_ALPHA8: 27, + ASTC_6x5_SRGB8_ALPHA8: 28, + ASTC_6x6_SRGB8_ALPHA8: 29, + ASTC_8x5_SRGB8_ALPHA8: 30, + ASTC_8x6_SRGB8_ALPHA8: 31, + ASTC_8x8_SRGB8_ALPHA8: 32, + ASTC_10x5_SRGB8_ALPHA8: 33, + ASTC_10x6_SRGB8_ALPHA8: 34, + ASTC_10x8_SRGB8_ALPHA8: 35, + ASTC_10x10_SRGB8_ALPHA8: 36, + ASTC_12x10_SRGB8_ALPHA8: 37, + ASTC_12x12_SRGB8_ALPHA8: 38 +}; + +tcuCompressedTexture.divRoundUp = function(a, b) { + return Math.floor(a / b) + ((a % b) ? 1 : 0); +}; + +tcuCompressedTexture.isEtcFormat = function(fmt) { + // WebGL2 supports ETC2 and EAC formats + switch (fmt) { + // case tcuCompressedTexture.Format.ETC1_RGB8: + case tcuCompressedTexture.Format.EAC_R11: + case tcuCompressedTexture.Format.EAC_SIGNED_R11: + case tcuCompressedTexture.Format.EAC_RG11: + case tcuCompressedTexture.Format.EAC_SIGNED_RG11: + case tcuCompressedTexture.Format.ETC2_RGB8: + case tcuCompressedTexture.Format.ETC2_SRGB8: + case tcuCompressedTexture.Format.ETC2_RGB8_PUNCHTHROUGH_ALPHA1: + case tcuCompressedTexture.Format.ETC2_SRGB8_PUNCHTHROUGH_ALPHA1: + case tcuCompressedTexture.Format.ETC2_EAC_RGBA8: + case tcuCompressedTexture.Format.ETC2_EAC_SRGB8_ALPHA8: + return true; + + default: + return false; + } +}; + +tcuCompressedTexture.etcDecompressInternal = function() { + +var ETC2_BLOCK_WIDTH = 4; +var ETC2_BLOCK_HEIGHT = 4; +var ETC2_UNCOMPRESSED_PIXEL_SIZE_A8 = 1; +var ETC2_UNCOMPRESSED_PIXEL_SIZE_R11 = 2; +var ETC2_UNCOMPRESSED_PIXEL_SIZE_RG11 = 4; +var ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8 = 3; +var ETC2_UNCOMPRESSED_PIXEL_SIZE_RGBA8 = 4; +var ETC2_UNCOMPRESSED_BLOCK_SIZE_A8 = ETC2_BLOCK_WIDTH * ETC2_BLOCK_HEIGHT * ETC2_UNCOMPRESSED_PIXEL_SIZE_A8; +var ETC2_UNCOMPRESSED_BLOCK_SIZE_R11 = ETC2_BLOCK_WIDTH * ETC2_BLOCK_HEIGHT * ETC2_UNCOMPRESSED_PIXEL_SIZE_R11; +var ETC2_UNCOMPRESSED_BLOCK_SIZE_RG11 = ETC2_BLOCK_WIDTH * ETC2_BLOCK_HEIGHT * ETC2_UNCOMPRESSED_PIXEL_SIZE_RG11; +var ETC2_UNCOMPRESSED_BLOCK_SIZE_RGB8 = ETC2_BLOCK_WIDTH * ETC2_BLOCK_HEIGHT * ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; +var ETC2_UNCOMPRESSED_BLOCK_SIZE_RGBA8 = ETC2_BLOCK_WIDTH * ETC2_BLOCK_HEIGHT * ETC2_UNCOMPRESSED_PIXEL_SIZE_RGBA8; + +/** + * @param {ArrayBuffer} src Source ArrayBuffer + * @return {Uint8Array} + */ +var get64BitBlock = function(src, blockNdx) { + var block = new Uint8Array(src, blockNdx * 8, 8); + return block; +}; + +/** + * @param {ArrayBuffer} src Source ArrayBuffer + * Return the first 64 bits of a 128 bit block. + */ +var get128BitBlockStart = function(src, blockNdx) { + return get64BitBlock(src, 2 * blockNdx); +}; + +/** + * @param {ArrayBuffer} src Source ArrayBuffer + * Return the last 64 bits of a 128 bit block. + */ +var get128BitBlockEnd = function(src, blockNdx) { + return get64BitBlock(src, 2 * blockNdx + 1); +}; + +var mask8 = function(src, low, high) { + if (low > 7 || high < 0) + return { + value: 0, + bits: 0 + }; + + var numBits = high - low + 1; + var mask = (1 << numBits) - 1; + + return { + value: (src >> low) & mask, + bits: numBits + }; +}; + +var getBits64 = function(src, low, high) { + var result = 0; + var bits = 0; + var lowIndex = low; + var highIndex = high; + for (var i = 7; i >= 0; i--) { + var v = mask8(src[i], Math.max(0, lowIndex), Math.min(7, highIndex)); + lowIndex = lowIndex - 8; + highIndex = highIndex - 8; + result = result | (v.value << bits); + bits = v.bits; + } + return result; +}; + +var getBit64 = function(src, bit) { + return getBits64(src, bit, bit); +}; + +var extendSigned3To8 = function(src) { + var isNeg = (src & (1 << 2)) != 0; + var val = isNeg ? src - 8 : src; + return val; +}; + +var extend4To8 = function(src) { + return src * 255 / 15; +}; + +var extend5To8 = function(src) { + return src * 255 / 31; +}; + +var extend6To8 = function(src) { + return src * 255 / 63; +}; + +var extend7To8 = function(src) { + return src * 255 / 127; +}; + +var extend11To16 = function(src) { + return src * 32.015144; +}; + +var extend11To16WithSign = function(src) { + if (src < 0) + return -extend11To16(-src); + else + return extend11To16(src); +}; + +/** + * @param { (Uint16Array|Int16Array) } dst + * @param {Uint8Array} src + * @param {boolean} signedMode + */ +var decompressEAC11Block = function(dst, src, signedMode) { + var modifierTable = [ + [-3, -6, -9, -15, 2, 5, 8, 14], + [-3, -7, -10, -13, 2, 6, 9, 12], + [-2, -5, -8, -13, 1, 4, 7, 12], + [-2, -4, -6, -13, 1, 3, 5, 12], + [-3, -6, -8, -12, 2, 5, 7, 11], + [-3, -7, -9, -11, 2, 6, 8, 10], + [-4, -7, -8, -11, 3, 6, 7, 10], + [-3, -5, -8, -11, 2, 4, 7, 10], + [-2, -6, -8, -10, 1, 5, 7, 9], + [-2, -5, -8, -10, 1, 4, 7, 9], + [-2, -4, -8, -10, 1, 3, 7, 9], + [-2, -5, -7, -10, 1, 4, 6, 9], + [-3, -4, -7, -10, 2, 3, 6, 9], + [-1, -2, -3, -10, 0, 1, 2, 9], + [-4, -6, -8, -9, 3, 5, 7, 8], + [-3, -5, -7, -9, 2, 4, 6, 8] + ]; + + var multiplier = getBits64(src, 52, 55); + var tableNdx = getBits64(src, 48, 51); + var baseCodeword = getBits64(src, 56, 63); + + if (signedMode) { + if (baseCodeword > 127) + baseCodeword -= 256; + if (baseCodeword == -128) + baseCodeword = -127; + } + + var pixelNdx = 0; + for (var x = 0; x < ETC2_BLOCK_WIDTH; x++) { + for (var y = 0; y < ETC2_BLOCK_HEIGHT; y++) { + var dstOffset = (y * ETC2_BLOCK_WIDTH + x); + var pixelBitNdx = 45 - 3 * pixelNdx; + var modifierNdx = (getBit64(src, pixelBitNdx + 2) << 2) | (getBit64(src, pixelBitNdx + 1) << 1) | getBit64(src, pixelBitNdx); + var modifier = modifierTable[tableNdx][modifierNdx]; + + if (signedMode) { + if (multiplier != 0) + dst[dstOffset] = deMath.clamp(baseCodeword * 8 + multiplier * modifier * 8, -1023, 1023); + else + dst[dstOffset] = deMath.clamp(baseCodeword * 8 + modifier, -1023, 1023); + } else { + if (multiplier != 0) + dst[dstOffset] = deMath.clamp(baseCodeword * 8 + 4 + multiplier * modifier * 8, 0, 2047); + else + dst[dstOffset] = deMath.clamp(baseCodeword * 8 + 4 + modifier, 0, 2047); + } + pixelNdx++; + } + } +}; + +var decompressEAC_R11 = function(/*const tcu::PixelBufferAccess&*/ dst, width, height, src, signedMode) { + /** @const */ var numBlocksX = tcuCompressedTexture.divRoundUp(width, 4); + /** @const */ var numBlocksY = tcuCompressedTexture.divRoundUp(height, 4); + var dstPtr; + var dstRowPitch = dst.getRowPitch(); + var dstPixelSize = ETC2_UNCOMPRESSED_PIXEL_SIZE_R11; + var uncompressedBlockArray = new ArrayBuffer(ETC2_UNCOMPRESSED_BLOCK_SIZE_R11); + var uncompressedBlock16; + if (signedMode) { + dstPtr = new Int16Array(dst.m_data); + uncompressedBlock16 = new Int16Array(uncompressedBlockArray); + } else { + dstPtr = new Uint16Array(dst.m_data); + uncompressedBlock16 = new Uint16Array(uncompressedBlockArray); + } + + for (var blockY = 0; blockY < numBlocksY; blockY++) { + for (var blockX = 0; blockX < numBlocksX; blockX++) { + /*const deUint64*/ var compressedBlock = get64BitBlock(src, blockY * numBlocksX + blockX); + + // Decompress. + decompressEAC11Block(uncompressedBlock16, compressedBlock, signedMode); + + // Write to dst. + var baseX = blockX * ETC2_BLOCK_WIDTH; + var baseY = blockY * ETC2_BLOCK_HEIGHT; + for (var y = 0; y < Math.min(ETC2_BLOCK_HEIGHT, height - baseY); y++) { + for (var x = 0; x < Math.min(ETC2_BLOCK_WIDTH, width - baseX); x++) { + DE_ASSERT(ETC2_UNCOMPRESSED_PIXEL_SIZE_R11 == 2); + + if (signedMode) { + var srcIndex = y * ETC2_BLOCK_WIDTH + x; + var dstIndex = (baseY + y) * dstRowPitch / dstPixelSize + baseX + x; + + dstPtr[dstIndex] = extend11To16WithSign(uncompressedBlock16[srcIndex]); + } else { + var srcIndex = y * ETC2_BLOCK_WIDTH + x; + var dstIndex = (baseY + y) * dstRowPitch / dstPixelSize + baseX + x; + + dstPtr[dstIndex] = extend11To16(uncompressedBlock16[srcIndex]); + } + } + } + } + } +}; + +var decompressEAC_RG11 = function(/*const tcu::PixelBufferAccess&*/ dst, width, height, src, signedMode) { + /** @const */ var numBlocksX = tcuCompressedTexture.divRoundUp(width, 4); + /** @const */ var numBlocksY = tcuCompressedTexture.divRoundUp(height, 4); + var dstPtr; + var dstRowPitch = dst.getRowPitch(); + var dstPixelSize = ETC2_UNCOMPRESSED_PIXEL_SIZE_RG11; + var uncompressedBlockArrayR = new ArrayBuffer(ETC2_UNCOMPRESSED_BLOCK_SIZE_R11); + var uncompressedBlockArrayG = new ArrayBuffer(ETC2_UNCOMPRESSED_BLOCK_SIZE_R11); + var uncompressedBlockR16; + var uncompressedBlockG16; + if (signedMode) { + dstPtr = new Int16Array(dst.m_data); + uncompressedBlockR16 = new Int16Array(uncompressedBlockArrayR); + uncompressedBlockG16 = new Int16Array(uncompressedBlockArrayG); + } else { + dstPtr = new Uint16Array(dst.m_data); + uncompressedBlockR16 = new Uint16Array(uncompressedBlockArrayR); + uncompressedBlockG16 = new Uint16Array(uncompressedBlockArrayG); + } + + for (var blockY = 0; blockY < numBlocksY; blockY++) { + for (var blockX = 0; blockX < numBlocksX; blockX++) { + /*const deUint64*/ var compressedBlockR = get128BitBlockStart(src, blockY * numBlocksX + blockX); + /*const deUint64*/ var compressedBlockG = get128BitBlockEnd(src, blockY * numBlocksX + blockX); + + // Decompress. + decompressEAC11Block(uncompressedBlockR16, compressedBlockR, signedMode); + decompressEAC11Block(uncompressedBlockG16, compressedBlockG, signedMode); + + // Write to dst. + var baseX = blockX * ETC2_BLOCK_WIDTH; + var baseY = blockY * ETC2_BLOCK_HEIGHT; + for (var y = 0; y < Math.min(ETC2_BLOCK_HEIGHT, height - baseY); y++) { + for (var x = 0; x < Math.min(ETC2_BLOCK_WIDTH, width - baseX); x++) { + DE_ASSERT(ETC2_UNCOMPRESSED_PIXEL_SIZE_RG11 == 4); + + if (signedMode) { + var srcIndex = y * ETC2_BLOCK_WIDTH + x; + var dstIndex = 2 * ((baseY + y) * dstRowPitch / dstPixelSize + baseX + x); + + dstPtr[dstIndex] = extend11To16WithSign(uncompressedBlockR16[srcIndex]); + dstPtr[dstIndex + 1] = extend11To16WithSign(uncompressedBlockG16[srcIndex]); + } else { + var srcIndex = y * ETC2_BLOCK_WIDTH + x; + var dstIndex = 2 * ((baseY + y) * dstRowPitch / dstPixelSize + baseX + x); + + dstPtr[dstIndex] = extend11To16(uncompressedBlockR16[srcIndex]); + dstPtr[dstIndex + 1] = extend11To16(uncompressedBlockG16[srcIndex]); + } + } + } + } + } +}; + +// if alphaMode is true, do PUNCHTHROUGH and store alpha to alphaDst; otherwise do ordinary ETC2 RGB8. +/** + * @param {Uint8Array} dst Destination array + * @param {Uint8Array} src Source array + * @param {Uint8Array} alphaDst Optional Alpha output channel + */ +var decompressETC2Block = function(dst, src, alphaDst, alphaMode) { + /** + * enum + */ + var Etc2Mode = { + MODE_INDIVIDUAL: 0, + MODE_DIFFERENTIAL: 1, + MODE_T: 2, + MODE_H: 3, + MODE_PLANAR: 4 + }; + + var diffOpaqueBit = getBit64(src, 33); + var selBR = getBits64(src, 59, 63); // 5 bits. + var selBG = getBits64(src, 51, 55); + var selBB = getBits64(src, 43, 47); + var selDR = extendSigned3To8(getBits64(src, 56, 58)); // 3 bits. + var selDG = extendSigned3To8(getBits64(src, 48, 50)); + var selDB = extendSigned3To8(getBits64(src, 40, 42)); + + var mode; + + if (!alphaMode && diffOpaqueBit == 0) + mode = Etc2Mode.MODE_INDIVIDUAL; + else if (!deMath.deInRange32(selBR + selDR, 0, 31)) + mode = Etc2Mode.MODE_T; + else if (!deMath.deInRange32(selBG + selDG, 0, 31)) + mode = Etc2Mode.MODE_H; + else if (!deMath.deInRange32(selBB + selDB, 0, 31)) + mode = Etc2Mode.MODE_PLANAR; + else + mode = Etc2Mode.MODE_DIFFERENTIAL; + + if (mode == Etc2Mode.MODE_INDIVIDUAL || mode == Etc2Mode.MODE_DIFFERENTIAL) { + // Individual and differential modes have some steps in common, handle them here. + var modifierTable = [ + // 00 01 10 11 + [2, 8, -2, -8], + [5, 17, -5, -17], + [9, 29, -9, -29], + [13, 42, -13, -42], + [18, 60, -18, -60], + [24, 80, -24, -80], + [33, 106, -33, -106], + [47, 183, -47, -183] + ]; + + var flipBit = getBit64(src, 32); + var table = [getBits64(src, 37, 39), getBits64(src, 34, 36)]; + var baseR = []; + var baseG = []; + var baseB = []; + + if (mode == Etc2Mode.MODE_INDIVIDUAL) { + // Individual mode, initial values. + baseR[0] = extend4To8(getBits64(src, 60, 63)); + baseR[1] = extend4To8(getBits64(src, 56, 59)); + baseG[0] = extend4To8(getBits64(src, 52, 55)); + baseG[1] = extend4To8(getBits64(src, 48, 51)); + baseB[0] = extend4To8(getBits64(src, 44, 47)); + baseB[1] = extend4To8(getBits64(src, 40, 43)); + } else { + // Differential mode, initial values. + baseR[0] = extend5To8(selBR); + baseG[0] = extend5To8(selBG); + baseB[0] = extend5To8(selBB); + + baseR[1] = extend5To8((selBR + selDR)); + baseG[1] = extend5To8((selBG + selDG)); + baseB[1] = extend5To8((selBB + selDB)); + } + + // Write final pixels for individual or differential mode. + var pixelNdx = 0; + for (var x = 0; x < ETC2_BLOCK_WIDTH; x++) { + for (var y = 0; y < ETC2_BLOCK_HEIGHT; y++, pixelNdx++) { + var dstOffset = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; + var subBlock = ((flipBit ? y : x) >= 2) ? 1 : 0; + var tableNdx = table[subBlock]; + var modifierNdx = (getBit64(src, 16 + pixelNdx) << 1) | getBit64(src, pixelNdx); + var alphaDstOffset = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_A8; // Only needed for PUNCHTHROUGH version. + + // If doing PUNCHTHROUGH version (alphaMode), opaque bit may affect colors. + if (alphaMode && diffOpaqueBit == 0 && modifierNdx == 2) { + dst[dstOffset + 0] = 0; + dst[dstOffset + 1] = 0; + dst[dstOffset + 2] = 0; + alphaDst[alphaDstOffset] = 0; + } else { + var modifier; + + // PUNCHTHROUGH version and opaque bit may also affect modifiers. + if (alphaMode && diffOpaqueBit == 0 && (modifierNdx == 0 || modifierNdx == 2)) + modifier = 0; + else + modifier = modifierTable[tableNdx][modifierNdx]; + + dst[dstOffset + 0] = deMath.clamp(baseR[subBlock] + modifier, 0, 255); + dst[dstOffset + 1] = deMath.clamp(baseG[subBlock] + modifier, 0, 255); + dst[dstOffset + 2] = deMath.clamp(baseB[subBlock] + modifier, 0, 255); + + if (alphaMode) + alphaDst[alphaDstOffset] = 255; + } + } + } + } else if (mode == Etc2Mode.MODE_T || mode == Etc2Mode.MODE_H) { + // T and H modes have some steps in common, handle them here. + var distTable = [3, 6, 11, 16, 23, 32, 41, 64]; + + var paintR = []; + var paintG = []; + var paintB = []; + + if (mode == Etc2Mode.MODE_T) { + // T mode, calculate paint values. + var R1a = getBits64(src, 59, 60); + var R1b = getBits64(src, 56, 57); + var G1 = getBits64(src, 52, 55); + var B1 = getBits64(src, 48, 51); + var R2 = getBits64(src, 44, 47); + var G2 = getBits64(src, 40, 43); + var B2 = getBits64(src, 36, 39); + var distNdx = (getBits64(src, 34, 35) << 1) | getBit64(src, 32); + var dist = distTable[distNdx]; + + paintR[0] = extend4To8((R1a << 2) | R1b); + paintG[0] = extend4To8(G1); + paintB[0] = extend4To8(B1); + paintR[2] = extend4To8(R2); + paintG[2] = extend4To8(G2); + paintB[2] = extend4To8(B2); + paintR[1] = deMath.clamp(paintR[2] + dist, 0, 255); + paintG[1] = deMath.clamp(paintG[2] + dist, 0, 255); + paintB[1] = deMath.clamp(paintB[2] + dist, 0, 255); + paintR[3] = deMath.clamp(paintR[2] - dist, 0, 255); + paintG[3] = deMath.clamp(paintG[2] - dist, 0, 255); + paintB[3] = deMath.clamp(paintB[2] - dist, 0, 255); + } else { + // H mode, calculate paint values. + var R1 = getBits64(src, 59, 62); + var G1a = getBits64(src, 56, 58); + var G1b = getBit64(src, 52); + var B1a = getBit64(src, 51); + var B1b = getBits64(src, 47, 49); + var R2 = getBits64(src, 43, 46); + var G2 = getBits64(src, 39, 42); + var B2 = getBits64(src, 35, 38); + var baseR = []; + var baseG = []; + var baseB = []; + var baseValue = []; + var distNdx; + var dist; + + baseR[0] = extend4To8(R1); + baseG[0] = extend4To8((G1a << 1) | G1b); + baseB[0] = extend4To8((B1a << 3) | B1b); + baseR[1] = extend4To8(R2); + baseG[1] = extend4To8(G2); + baseB[1] = extend4To8(B2); + baseValue[0] = ((baseR[0]) << 16) | ((baseG[0]) << 8) | baseB[0]; + baseValue[1] = ((baseR[1]) << 16) | ((baseG[1]) << 8) | baseB[1]; + distNdx = (getBit64(src, 34) << 2) | (getBit64(src, 32) << 1); + if (baseValue[0] >= baseValue[1]) + distNdx += 1; + dist = distTable[distNdx]; + + paintR[0] = deMath.clamp(baseR[0] + dist, 0, 255); + paintG[0] = deMath.clamp(baseG[0] + dist, 0, 255); + paintB[0] = deMath.clamp(baseB[0] + dist, 0, 255); + paintR[1] = deMath.clamp(baseR[0] - dist, 0, 255); + paintG[1] = deMath.clamp(baseG[0] - dist, 0, 255); + paintB[1] = deMath.clamp(baseB[0] - dist, 0, 255); + paintR[2] = deMath.clamp(baseR[1] + dist, 0, 255); + paintG[2] = deMath.clamp(baseG[1] + dist, 0, 255); + paintB[2] = deMath.clamp(baseB[1] + dist, 0, 255); + paintR[3] = deMath.clamp(baseR[1] - dist, 0, 255); + paintG[3] = deMath.clamp(baseG[1] - dist, 0, 255); + paintB[3] = deMath.clamp(baseB[1] - dist, 0, 255); + } + + // Write final pixels for T or H mode. + var pixelNdx = 0; + for (var x = 0; x < ETC2_BLOCK_WIDTH; x++) { + for (var y = 0; y < ETC2_BLOCK_HEIGHT; y++, pixelNdx++) { + var dstOffset = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; + var paintNdx = (getBit64(src, 16 + pixelNdx) << 1) | getBit64(src, pixelNdx); + var alphaDstOffset = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_A8; // Only needed for PUNCHTHROUGH version. + + if (alphaMode && diffOpaqueBit == 0 && paintNdx == 2) { + dst[dstOffset + 0] = 0; + dst[dstOffset + 1] = 0; + dst[dstOffset + 2] = 0; + alphaDst[alphaDstOffset] = 0; + } else { + dst[dstOffset + 0] = deMath.clamp(paintR[paintNdx], 0, 255); + dst[dstOffset + 1] = deMath.clamp(paintG[paintNdx], 0, 255); + dst[dstOffset + 2] = deMath.clamp(paintB[paintNdx], 0, 255); + + if (alphaMode) + alphaDst[alphaDstOffset] = 255; + } + } + } + } else { + // Planar mode. + var GO1 = getBit64(src, 56); + var GO2 = getBits64(src, 49, 54); + var BO1 = getBit64(src, 48); + var BO2 = getBits64(src, 43, 44); + var BO3 = getBits64(src, 39, 41); + var RH1 = getBits64(src, 34, 38); + var RH2 = getBit64(src, 32); + var RO = extend6To8(getBits64(src, 57, 62)); + var GO = extend7To8((GO1 << 6) | GO2); + var BO = extend6To8((BO1 << 5) | (BO2 << 3) | BO3); + var RH = extend6To8((RH1 << 1) | RH2); + var GH = extend7To8(getBits64(src, 25, 31)); + var BH = extend6To8(getBits64(src, 19, 24)); + var RV = extend6To8(getBits64(src, 13, 18)); + var GV = extend7To8(getBits64(src, 6, 12)); + var BV = extend6To8(getBits64(src, 0, 5)); + + // Write final pixels for planar mode. + for (var y = 0; y < 4; y++) { + for (var x = 0; x < 4; x++) { + var dstOffset = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; + var unclampedR = (x * (RH - RO) + y * (RV - RO) + 4 * RO + 2) / 4; + var unclampedG = (x * (GH - GO) + y * (GV - GO) + 4 * GO + 2) / 4; + var unclampedB = (x * (BH - BO) + y * (BV - BO) + 4 * BO + 2) / 4; + var alphaDstOffset = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_A8; // Only needed for PUNCHTHROUGH version. + + dst[dstOffset + 0] = deMath.clamp(unclampedR, 0, 255); + dst[dstOffset + 1] = deMath.clamp(unclampedG, 0, 255); + dst[dstOffset + 2] = deMath.clamp(unclampedB, 0, 255); + + if (alphaMode) + alphaDst[alphaDstOffset] = 255; + } + } + } +}; + +var decompressEAC8Block = function(dst, src) { + var modifierTable = [ + [-3, -6, -9, -15, 2, 5, 8, 14], + [-3, -7, -10, -13, 2, 6, 9, 12], + [-2, -5, -8, -13, 1, 4, 7, 12], + [-2, -4, -6, -13, 1, 3, 5, 12], + [-3, -6, -8, -12, 2, 5, 7, 11], + [-3, -7, -9, -11, 2, 6, 8, 10], + [-4, -7, -8, -11, 3, 6, 7, 10], + [-3, -5, -8, -11, 2, 4, 7, 10], + [-2, -6, -8, -10, 1, 5, 7, 9], + [-2, -5, -8, -10, 1, 4, 7, 9], + [-2, -4, -8, -10, 1, 3, 7, 9], + [-2, -5, -7, -10, 1, 4, 6, 9], + [-3, -4, -7, -10, 2, 3, 6, 9], + [-1, -2, -3, -10, 0, 1, 2, 9], + [-4, -6, -8, -9, 3, 5, 7, 8], + [-3, -5, -7, -9, 2, 4, 6, 8] + ]; + + var baseCodeword = getBits64(src, 56, 63); + var multiplier = getBits64(src, 52, 55); + var tableNdx = getBits64(src, 48, 51); + + var pixelNdx = 0; + for (var x = 0; x < ETC2_BLOCK_WIDTH; x++) { + for (var y = 0; y < ETC2_BLOCK_HEIGHT; y++, pixelNdx++) { + var dstOffset = (y * ETC2_BLOCK_WIDTH + x); + var pixelBitNdx = 45 - 3 * pixelNdx; + var modifierNdx = (getBit64(src, pixelBitNdx + 2) << 2) | (getBit64(src, pixelBitNdx + 1) << 1) | getBit64(src, pixelBitNdx); + var modifier = modifierTable[tableNdx][modifierNdx]; + + dst[dstOffset] = deMath.clamp(baseCodeword + multiplier * modifier, 0, 255); + } + } +}; + +var decompressETC2 = function(/*const tcu::PixelBufferAccess&*/ dst, width, height, src) { + var numBlocksX = tcuCompressedTexture.divRoundUp(width, 4); + var numBlocksY = tcuCompressedTexture.divRoundUp(height, 4); + var dstPtr = new Uint8Array(dst.m_data); + var dstRowPitch = dst.getRowPitch(); + var dstPixelSize = ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; + var uncompressedBlockArray = new ArrayBuffer(ETC2_UNCOMPRESSED_BLOCK_SIZE_RGB8); + var uncompressedBlock = new Uint8Array(uncompressedBlockArray); + + for (var blockY = 0; blockY < numBlocksY; blockY++) { + for (var blockX = 0; blockX < numBlocksX; blockX++) { + var compressedBlock = get64BitBlock(src, blockY * numBlocksX + blockX); + + // Decompress. + decompressETC2Block(uncompressedBlock, compressedBlock, null, false); + + // Write to dst. + var baseX = blockX * ETC2_BLOCK_WIDTH; + var baseY = blockY * ETC2_BLOCK_HEIGHT; + for (var y = 0; y < Math.min(ETC2_BLOCK_HEIGHT, height - baseY); y++) { + for (var x = 0; x < Math.min(ETC2_BLOCK_WIDTH, width - baseX); x++) { + var srcIndex = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; + var dstIndex = (baseY + y) * dstRowPitch + (baseX + x) * dstPixelSize; + + for (var i = 0; i < ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; i++) + dstPtr[dstIndex + i] = uncompressedBlock[srcIndex + i]; + } + } + } + } +}; + +var decompressETC2_EAC_RGBA8 = function(/*const tcu::PixelBufferAccess&*/ dst, width, height, src) { + var numBlocksX = tcuCompressedTexture.divRoundUp(width, 4); + var numBlocksY = tcuCompressedTexture.divRoundUp(height, 4); + var dstPtr = new Uint8Array(dst.m_data); + var dstRowPitch = dst.getRowPitch(); + var dstPixelSize = ETC2_UNCOMPRESSED_PIXEL_SIZE_RGBA8; + var uncompressedBlockArray = new ArrayBuffer(ETC2_UNCOMPRESSED_BLOCK_SIZE_RGB8); + var uncompressedBlock = new Uint8Array(uncompressedBlockArray); + var uncompressedBlockAlphaArray = new ArrayBuffer(ETC2_UNCOMPRESSED_BLOCK_SIZE_A8); + var uncompressedBlockAlpha = new Uint8Array(uncompressedBlockAlphaArray); + + for (var blockY = 0; blockY < numBlocksY; blockY++) { + for (var blockX = 0; blockX < numBlocksX; blockX++) { + var compressedBlockAlpha = get128BitBlockStart(src, blockY * numBlocksX + blockX); + var compressedBlockRGB = get128BitBlockEnd(src, blockY * numBlocksX + blockX); + + // Decompress. + decompressETC2Block(uncompressedBlock, compressedBlockRGB, null, false); + decompressEAC8Block(uncompressedBlockAlpha, compressedBlockAlpha); + + // Write to dst. + var baseX = blockX * ETC2_BLOCK_WIDTH; + var baseY = blockY * ETC2_BLOCK_HEIGHT; + for (var y = 0; y < Math.min(ETC2_BLOCK_HEIGHT, height - baseY); y++) { + for (var x = 0; x < Math.min(ETC2_BLOCK_WIDTH, width - baseX); x++) { + var srcIndex = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; + var srcAlphaIndex = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_A8; + var dstIndex = (baseY + y) * dstRowPitch + (baseX + x) * dstPixelSize; + + for (var i = 0; i < ETC2_UNCOMPRESSED_PIXEL_SIZE_RGBA8 - 1; i++) + dstPtr[dstIndex + i] = uncompressedBlock[srcIndex + i]; + dstPtr[dstIndex + ETC2_UNCOMPRESSED_PIXEL_SIZE_RGBA8 - 1] = uncompressedBlockAlpha[srcAlphaIndex]; + + } + } + } + } +}; + +var decompressETC2_RGB8_PUNCHTHROUGH_ALPHA1 = function(/*const tcu::PixelBufferAccess&*/ dst, width, height, src) { + var numBlocksX = tcuCompressedTexture.divRoundUp(width, 4); + var numBlocksY = tcuCompressedTexture.divRoundUp(height, 4); + var dstPtr = new Uint8Array(dst.m_data); + var dstRowPitch = dst.getRowPitch(); + var dstPixelSize = ETC2_UNCOMPRESSED_PIXEL_SIZE_RGBA8; + var uncompressedBlockArray = new ArrayBuffer(ETC2_UNCOMPRESSED_BLOCK_SIZE_RGB8); + var uncompressedBlock = new Uint8Array(uncompressedBlockArray); + var uncompressedBlockAlphaArray = new ArrayBuffer(ETC2_UNCOMPRESSED_BLOCK_SIZE_A8); + var uncompressedBlockAlpha = new Uint8Array(uncompressedBlockAlphaArray); + + for (var blockY = 0; blockY < numBlocksY; blockY++) { + for (var blockX = 0; blockX < numBlocksX; blockX++) { + var compressedBlock = get64BitBlock(src, blockY * numBlocksX + blockX); + + // Decompress. + decompressETC2Block(uncompressedBlock, compressedBlock, uncompressedBlockAlpha, true); + + // Write to dst. + var baseX = blockX * ETC2_BLOCK_WIDTH; + var baseY = blockY * ETC2_BLOCK_HEIGHT; + for (var y = 0; y < Math.min(ETC2_BLOCK_HEIGHT, height - baseY); y++) { + for (var x = 0; x < Math.min(ETC2_BLOCK_WIDTH, width - baseX); x++) { + var srcIndex = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_RGB8; + var srcAlphaIndex = (y * ETC2_BLOCK_WIDTH + x) * ETC2_UNCOMPRESSED_PIXEL_SIZE_A8; + var dstIndex = (baseY + y) * dstRowPitch + (baseX + x) * dstPixelSize; + + for (var i = 0; i < ETC2_UNCOMPRESSED_PIXEL_SIZE_RGBA8 - 1; i++) + dstPtr[dstIndex + i] = uncompressedBlock[srcIndex + i]; + dstPtr[dstIndex + ETC2_UNCOMPRESSED_PIXEL_SIZE_RGBA8 - 1] = uncompressedBlockAlpha[srcAlphaIndex]; + + } + } + } + } +}; + +return { + decompressEAC_R11: decompressEAC_R11, + decompressEAC_RG11: decompressEAC_RG11, + decompressETC2: decompressETC2, + decompressETC2_RGB8_PUNCHTHROUGH_ALPHA1: decompressETC2_RGB8_PUNCHTHROUGH_ALPHA1, + decompressETC2_EAC_RGBA8: decompressETC2_EAC_RGBA8 +}; + +}(); + +/** + * @constructor + * @param {tcuCompressedTexture.Format} format + * @param {number} width + * @param {number} height + * @param {number=} depth + */ +tcuCompressedTexture.CompressedTexture = function(format, width, height, depth) { + depth = depth === undefined ? 1 : depth; + this.setStorage(format, width, height, depth); + /** @type {Uint8Array} */ this.m_data; +}; + +/** + * @return {number} + */ +tcuCompressedTexture.CompressedTexture.prototype.getDataSize = function() { + return this.m_data.length; +}; + +/** + * @return {Uint8Array} + */ +tcuCompressedTexture.CompressedTexture.prototype.getData = function() { + return this.m_data; +}; + +/** + * @return {number} + */ +tcuCompressedTexture.CompressedTexture.prototype.getWidth = function() { + return this.m_width; +}; + +/** + * @return {number} + */ +tcuCompressedTexture.CompressedTexture.prototype.getHeight = function() { + return this.m_height; +}; + +/** + * @return {tcuCompressedTexture.Format} + */ +tcuCompressedTexture.CompressedTexture.prototype.getFormat = function() { + return this.m_format; +}; + +tcuCompressedTexture.CompressedTexture.prototype.setStorage = function(format, width, height, depth) { + depth = depth === undefined ? 1 : depth; + this.m_format = format; + this.m_width = width; + this.m_height = height; + this.m_depth = depth; + + if (tcuCompressedTexture.isEtcFormat(this.m_format)) { + DE_ASSERT(this.m_depth == 1); + + var blockSizeMultiplier = 0; // How many 64-bit parts each compressed block contains. + + switch (this.m_format) { + case tcuCompressedTexture.Format.ETC1_RGB8: blockSizeMultiplier = 1; break; + case tcuCompressedTexture.Format.EAC_R11: blockSizeMultiplier = 1; break; + case tcuCompressedTexture.Format.EAC_SIGNED_R11: blockSizeMultiplier = 1; break; + case tcuCompressedTexture.Format.EAC_RG11: blockSizeMultiplier = 2; break; + case tcuCompressedTexture.Format.EAC_SIGNED_RG11: blockSizeMultiplier = 2; break; + case tcuCompressedTexture.Format.ETC2_RGB8: blockSizeMultiplier = 1; break; + case tcuCompressedTexture.Format.ETC2_SRGB8: blockSizeMultiplier = 1; break; + case tcuCompressedTexture.Format.ETC2_RGB8_PUNCHTHROUGH_ALPHA1: blockSizeMultiplier = 1; break; + case tcuCompressedTexture.Format.ETC2_SRGB8_PUNCHTHROUGH_ALPHA1: blockSizeMultiplier = 1; break; + case tcuCompressedTexture.Format.ETC2_EAC_RGBA8: blockSizeMultiplier = 2; break; + case tcuCompressedTexture.Format.ETC2_EAC_SRGB8_ALPHA8: blockSizeMultiplier = 2; break; + + default: + throw new Error('Unsupported format ' + format); + break; + } + + this.m_array = new ArrayBuffer(blockSizeMultiplier * 8 * tcuCompressedTexture.divRoundUp(this.m_width, 4) * tcuCompressedTexture.divRoundUp(this.m_height, 4)); + this.m_data = new Uint8Array(this.m_array); + } + // else if (isASTCFormat(this.m_format)) + // { + // if (this.m_depth > 1) + // throw tcu::InternalError("3D ASTC textures not currently supported"); + + // const IVec3 blockSize = getASTCBlockSize(this.m_format); + // this.m_data.resize(ASTC_BLOCK_SIZE_BYTES * tcuCompressedTexture.divRoundUp(this.m_width, blockSize[0]) * tcuCompressedTexture.divRoundUp(this.m_height, blockSize[1]) * tcuCompressedTexture.divRoundUp(this.m_depth, blockSize[2])); + // } + // else + // { + // DE_ASSERT(this.m_format == FORMAT_LAST); + // DE_ASSERT(this.m_width == 0 && this.m_height == 0 && this.m_depth == 0); + // this.m_data.resize(0); + // } +}; + +/*--------------------------------------------------------------------*//*! + * \brief Get uncompressed texture format + *//*--------------------------------------------------------------------*/ +tcuCompressedTexture.CompressedTexture.prototype.getUncompressedFormat = function() { + if (tcuCompressedTexture.isEtcFormat(this.m_format)) { + switch (this.m_format) { + case tcuCompressedTexture.Format.ETC1_RGB8: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGB, tcuTexture.ChannelType.UNORM_INT8); + case tcuCompressedTexture.Format.EAC_R11: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.R, tcuTexture.ChannelType.UNORM_INT16); + case tcuCompressedTexture.Format.EAC_SIGNED_R11: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.R, tcuTexture.ChannelType.SNORM_INT16); + case tcuCompressedTexture.Format.EAC_RG11: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RG, tcuTexture.ChannelType.UNORM_INT16); + case tcuCompressedTexture.Format.EAC_SIGNED_RG11: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RG, tcuTexture.ChannelType.SNORM_INT16); + case tcuCompressedTexture.Format.ETC2_RGB8: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGB, tcuTexture.ChannelType.UNORM_INT8); + case tcuCompressedTexture.Format.ETC2_SRGB8: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.sRGB, tcuTexture.ChannelType.UNORM_INT8); + case tcuCompressedTexture.Format.ETC2_RGB8_PUNCHTHROUGH_ALPHA1: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8); + case tcuCompressedTexture.Format.ETC2_SRGB8_PUNCHTHROUGH_ALPHA1: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.sRGBA, tcuTexture.ChannelType.UNORM_INT8); + case tcuCompressedTexture.Format.ETC2_EAC_RGBA8: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8); + case tcuCompressedTexture.Format.ETC2_EAC_SRGB8_ALPHA8: return new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.sRGBA, tcuTexture.ChannelType.UNORM_INT8); + default: + throw new Error('Unsupported format ' + this.m_format); + } + } + // else if (isASTCFormat(m_format)) + // { + // if (isASTCSRGBFormat(m_format)) + // return TextureFormat(tcuTexture.ChannelType.sRGBA, tcuTexture.ChannelType.UNORM_INT8); + // else + // return TextureFormat(tcuTexture.ChannelType.RGBA, tcuTexture.ChannelType.HALF_FLOAT); + // } + // else + // { + // DE_ASSERT(false); + // return TextureFormat(); + // } +}; + +/** + * Decode to uncompressed pixel data + * @param {tcuTexture.PixelBufferAccess} dst Destination buffer + */ +tcuCompressedTexture.CompressedTexture.prototype.decompress = function(dst) { + DE_ASSERT(dst.getWidth() == this.m_width && dst.getHeight() == this.m_height && dst.getDepth() == 1); + var format = this.getUncompressedFormat(); + if (dst.getFormat().order != format.order || dst.getFormat().type != format.type) + throw new Error('Formats do not match.'); + + if (tcuCompressedTexture.isEtcFormat(this.m_format)) { + switch (this.m_format) { + // case tcuCompressedTexture.Format.ETC1_RGB8: decompressETC1 (dst, this.m_width, this.m_height, this.m_data); break; + case tcuCompressedTexture.Format.EAC_R11: tcuCompressedTexture.etcDecompressInternal.decompressEAC_R11(dst, this.m_width, this.m_height, this.m_array, false); break; + case tcuCompressedTexture.Format.EAC_SIGNED_R11: tcuCompressedTexture.etcDecompressInternal.decompressEAC_R11(dst, this.m_width, this.m_height, this.m_array, true); break; + case tcuCompressedTexture.Format.EAC_RG11: tcuCompressedTexture.etcDecompressInternal.decompressEAC_RG11(dst, this.m_width, this.m_height, this.m_array, false); break; + case tcuCompressedTexture.Format.EAC_SIGNED_RG11: tcuCompressedTexture.etcDecompressInternal.decompressEAC_RG11(dst, this.m_width, this.m_height, this.m_array, true); break; + case tcuCompressedTexture.Format.ETC2_RGB8: tcuCompressedTexture.etcDecompressInternal.decompressETC2(dst, this.m_width, this.m_height, this.m_array); break; + case tcuCompressedTexture.Format.ETC2_SRGB8: tcuCompressedTexture.etcDecompressInternal.decompressETC2(dst, this.m_width, this.m_height, this.m_array); break; + case tcuCompressedTexture.Format.ETC2_RGB8_PUNCHTHROUGH_ALPHA1: tcuCompressedTexture.etcDecompressInternal.decompressETC2_RGB8_PUNCHTHROUGH_ALPHA1(dst, this.m_width, this.m_height, this.m_array); break; + case tcuCompressedTexture.Format.ETC2_SRGB8_PUNCHTHROUGH_ALPHA1: tcuCompressedTexture.etcDecompressInternal.decompressETC2_RGB8_PUNCHTHROUGH_ALPHA1(dst, this.m_width, this.m_height, this.m_array); break; + case tcuCompressedTexture.Format.ETC2_EAC_RGBA8: tcuCompressedTexture.etcDecompressInternal.decompressETC2_EAC_RGBA8(dst, this.m_width, this.m_height, this.m_array); break; + case tcuCompressedTexture.Format.ETC2_EAC_SRGB8_ALPHA8: tcuCompressedTexture.etcDecompressInternal.decompressETC2_EAC_RGBA8(dst, this.m_width, this.m_height, this.m_array); break; + + default: + throw new Error('Unsupported format ' + this.m_format); + break; + } + } + // else if (isASTCFormat(m_format)) + // { + // const tcu::IVec3 blockSize = getASTCBlockSize(m_format); + // const bool isSRGBFormat = isASTCSRGBFormat(m_format); + + // if (blockSize[2] > 1) + // throw tcu::InternalError("3D ASTC textures not currently supported"); + + // decompressASTC(dst, m_width, m_height, &m_data[0], blockSize[0], blockSize[1], isSRGBFormat, isSRGBFormat || params.isASTCModeLDR); + // } /**/ + else + throw new Error('Unsupported format ' + this.m_format); +}; + + }); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFloat.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFloat.js new file mode 100644 index 000000000..3d91ab5aa --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFloat.js @@ -0,0 +1,809 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuFloat'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + +var tcuFloat = framework.common.tcuFloat; +var deMath = framework.delibs.debase.deMath; + +var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); +}; + +tcuFloat.FloatFlags = { + FLOAT_HAS_SIGN: (1 << 0), + FLOAT_SUPPORT_DENORM: (1 << 1) +}; + +/** + * Defines a tcuFloat.FloatDescription object, which is an essential part of the tcuFloat.deFloat type. + * Holds the information that shapes the tcuFloat.deFloat. + * @constructor + */ +tcuFloat.FloatDescription = function(exponentBits, mantissaBits, exponentBias, flags) { + this.ExponentBits = exponentBits; + this.MantissaBits = mantissaBits; + this.ExponentBias = exponentBias; + this.Flags = flags; + + this.totalBitSize = 1 + this.ExponentBits + this.MantissaBits; + this.totalByteSize = Math.floor(this.totalBitSize / 8) + ((this.totalBitSize % 8) > 0 ? 1 : 0); +}; + +/** + * Builds a zero float of the current binary description. + * @param {number} sign + * @return {tcuFloat.deFloat} + */ +tcuFloat.FloatDescription.prototype.zero = function(sign) { + return tcuFloat.newDeFloatFromParameters(this.zeroNumber(sign), this); +}; + +tcuFloat.FloatDescription.prototype.zeroNumber = function(sign) { + return deMath.shiftLeft((sign > 0 ? 0 : 1), (this.ExponentBits + this.MantissaBits)); +}; + +/** + * Builds an infinity float representation of the current binary description. + * @param {number} sign + * @return {tcuFloat.deFloat} + */ +tcuFloat.FloatDescription.prototype.inf = function(sign) { + return tcuFloat.newDeFloatFromParameters(this.infNumber(sign), this); +}; + +tcuFloat.FloatDescription.prototype.infNumber = function(sign) { + return ((sign > 0 ? 0 : 1) << (this.ExponentBits + this.MantissaBits)) | + deMath.shiftLeft(((1 << this.ExponentBits) - 1), this.MantissaBits); //Unless using very large exponent types, native shift is safe here, i guess. +}; + +/** + * Builds a NaN float representation of the current binary description. + * @return {tcuFloat.deFloat} + */ +tcuFloat.FloatDescription.prototype.nan = function() { + return tcuFloat.newDeFloatFromParameters(this.nanNumber(), this); +}; + +tcuFloat.FloatDescription.prototype.nanNumber = function() { + return deMath.shiftLeft(1, (this.ExponentBits + this.MantissaBits)) - 1; +}; + +/** + * Builds a tcuFloat.deFloat number based on the description and the given + * sign, exponent and mantissa values. + * @param {number} sign + * @param {number} exponent + * @param {number} mantissa + * @return {tcuFloat.deFloat} + */ +tcuFloat.FloatDescription.prototype.construct = function(sign, exponent, mantissa) { + // Repurpose this otherwise invalid input as a shorthand notation for zero (no need for caller to care about internal representation) + /** @type {boolean} */ var isShorthandZero = exponent == 0 && mantissa == 0; + + // Handles the typical notation for zero (min exponent, mantissa 0). Note that the exponent usually used exponent (-ExponentBias) for zero/subnormals is not used. + // Instead zero/subnormals have the (normally implicit) leading mantissa bit set to zero. + + /** @type {boolean} */ var isDenormOrZero = (exponent == 1 - this.ExponentBias) && (deMath.shiftRight(mantissa, this.MantissaBits) == 0); + /** @type {number} */ var s = deMath.shiftLeft((sign < 0 ? 1 : 0), (this.ExponentBits + this.MantissaBits)); + /** @type {number} */ var exp = (isShorthandZero || isDenormOrZero) ? 0 : exponent + this.ExponentBias; + + DE_ASSERT(sign == +1 || sign == -1); + DE_ASSERT(isShorthandZero || isDenormOrZero || deMath.shiftRight(mantissa, this.MantissaBits) == 1); + DE_ASSERT((exp >> this.ExponentBits) == 0); //Native shift is safe + + return tcuFloat.newDeFloatFromParameters( + deMath.binaryOp( + deMath.binaryOp( + s, + deMath.shiftLeft(exp, this.MantissaBits), + deMath.BinaryOp.OR + ), + deMath.binaryOp( + mantissa, + deMath.shiftLeft(1, this.MantissaBits) - 1, + deMath.BinaryOp.AND + ), + deMath.BinaryOp.OR + ), + this + ); +}; + +/** + * Builds a tcuFloat.deFloat number based on the description and the given + * sign, exponent and binary mantissa values. + * @param {number} sign + * @param {number} exponent + * @param {number} mantissaBits The raw binary representation. + * @return {tcuFloat.deFloat} + */ +tcuFloat.FloatDescription.prototype.constructBits = function(sign, exponent, mantissaBits) { + /** @type {number} */ var signBit = sign < 0 ? 1 : 0; + /** @type {number} */ var exponentBits = exponent + this.ExponentBias; + + DE_ASSERT(sign == +1 || sign == -1); + DE_ASSERT((exponentBits >> this.ExponentBits) == 0); + DE_ASSERT(deMath.shiftRight(mantissaBits, this.MantissaBits) == 0); + + return tcuFloat.newDeFloatFromParameters( + deMath.binaryOp( + deMath.binaryOp( + deMath.shiftLeft( + signBit, + this.ExponentBits + this.MantissaBits + ), + deMath.shiftLeft(exponentBits, this.MantissaBits), + deMath.BinaryOp.OR + ), + mantissaBits, + deMath.BinaryOp.OR + ), + this + ); +}; + +/** + * Converts a tcuFloat.deFloat from it's own format description into the format described + * by this description. + * @param {tcuFloat.deFloat} other Other float to convert to this format description. + * @return {tcuFloat.deFloat} converted tcuFloat.deFloat + */ +tcuFloat.FloatDescription.prototype.convert = function(other) { + /** @type {number} */ var otherExponentBits = other.description.ExponentBits; + /** @type {number} */ var otherMantissaBits = other.description.MantissaBits; + /** @type {number} */ var otherExponentBias = other.description.ExponentBias; + /** @type {number} */ var otherFlags = other.description.Flags; + + /** @type {number} */ var bitDiff; + /** @type {number} */ var half; + /** @type {number} */ var bias; + + if (!(this.Flags & tcuFloat.FloatFlags.FLOAT_HAS_SIGN) && other.sign() < 0) { + // Negative number, truncate to zero. + return this.zero(+1); + } else if (other.isInf()) { + return this.inf(other.sign()); + } else if (other.isNaN()) { + return this.nan(); + } else if (other.isZero()) { + return this.zero(other.sign()); + } else { + /** @type {number} */ var eMin = 1 - this.ExponentBias; + /** @type {number} */ var eMax = ((1 << this.ExponentBits) - 2) - this.ExponentBias; + + /** @type {number} */ var s = deMath.shiftLeft(other.signBit(), (this.ExponentBits + this.MantissaBits)); // \note Not sign, but sign bit. + /** @type {number} */ var e = other.exponent(); + /** @type {number} */ var m = other.mantissa(); + + // Normalize denormalized values prior to conversion. + while (!deMath.binaryOp(m, deMath.shiftLeft(1, otherMantissaBits), deMath.BinaryOp.AND)) { + m = deMath.shiftLeft(m, 1); + e -= 1; + } + + if (e < eMin) { + // Underflow. + if ((this.Flags & tcuFloat.FloatFlags.FLOAT_SUPPORT_DENORM) && (eMin - e - 1 <= this.MantissaBits)) { + // Shift and round (RTE). + bitDiff = (otherMantissaBits - this.MantissaBits) + (eMin - e); + half = deMath.shiftLeft(1, (bitDiff - 1)) - 1; + bias = deMath.binaryOp(deMath.shiftRight(m, bitDiff), 1, deMath.BinaryOp.AND); + + return tcuFloat.newDeFloatFromParameters( + deMath.binaryOp( + s, + deMath.shiftRight( + m + half + bias, + bitDiff + ), + deMath.BinaryOp.OR + ), + this + ); + } else + return this.zero(other.sign()); + } else { + // Remove leading 1. + m = deMath.binaryOp(m, deMath.binaryNot(deMath.shiftLeft(1, otherMantissaBits)), deMath.BinaryOp.AND); + + if (this.MantissaBits < otherMantissaBits) { + // Round mantissa (round to nearest even). + bitDiff = otherMantissaBits - this.MantissaBits; + half = deMath.shiftLeft(1, (bitDiff - 1)) - 1; + bias = deMath.binaryOp(deMath.shiftRight(m, bitDiff), 1, deMath.BinaryOp.AND); + + m = deMath.shiftRight(m + half + bias, bitDiff); + + if (deMath.binaryOp(m, deMath.shiftLeft(1, this.MantissaBits), deMath.BinaryOp.AND)) { + // Overflow in mantissa. + m = 0; + e += 1; + } + } else { + bitDiff = this.MantissaBits - otherMantissaBits; + m = deMath.shiftLeft(m, bitDiff); + } + + if (e > eMax) { + // Overflow. + return this.inf(other.sign()); + } else { + DE_ASSERT(deMath.deInRange32(e, eMin, eMax)); + DE_ASSERT(deMath.binaryOp((e + this.ExponentBias), deMath.binaryNot(deMath.shiftLeft(1, this.ExponentBits) - 1), deMath.BinaryOp.AND) == 0); + DE_ASSERT(deMath.binaryOp(m, deMath.binaryNot(deMath.shiftLeft(1, this.MantissaBits) - 1), deMath.BinaryOp.AND) == 0); + + return tcuFloat.newDeFloatFromParameters( + deMath.binaryOp( + deMath.binaryOp( + s, + deMath.shiftLeft( + e + this.ExponentBias, + this.MantissaBits + ), + deMath.BinaryOp.OR + ), + m, + deMath.BinaryOp.OR + ), + this + ); + } + } + } +}; + +/** + * tcuFloat.deFloat class - Empty constructor, builds a 32 bit float by default + * @constructor + */ +tcuFloat.deFloat = function() { + this.description = tcuFloat.description32; + + this.m_buffer = null; + this.m_array = null; + this.bitValue = undefined; + this.signValue = undefined; + this.expValue = undefined; + this.mantissaValue = undefined; + + this.m_value = 0; +}; + +/** + * buffer - Get the deFloat's existing ArrayBuffer or create one if none exists. + * @return {ArrayBuffer} + */ +tcuFloat.deFloat.prototype.buffer = function() { + if (!this.m_buffer) + this.m_buffer = new ArrayBuffer(this.description.totalByteSize); + return this.m_buffer; +}; + +/** + * array - Get the deFloat's existing Uint8Array or create one if none exists. + * @return {Uint8Array} + */ +tcuFloat.deFloat.prototype.array = function() { + if (!this.m_array) + this.m_array = new Uint8Array(this.buffer()); + return this.m_array; +}; + +/** + * deFloatNumber - To be used immediately after constructor + * Builds a 32-bit tcuFloat.deFloat based on a 64-bit JS number. + * @param {number} jsnumber + * @return {tcuFloat.deFloat} + */ +tcuFloat.deFloat.prototype.deFloatNumber = function(jsnumber) { + var view32 = new DataView(this.buffer()); + view32.setFloat32(0, jsnumber, true); //little-endian + this.m_value = view32.getFloat32(0, true); //little-endian + + // Clear cached values + this.bitValue = undefined; + this.signValue = undefined; + this.expValue = undefined; + this.mantissaValue = undefined; + + return this; +}; + +/** + * Convenience function to build a 32-bit tcuFloat.deFloat based on a 64-bit JS number + * Builds a 32-bit tcuFloat.deFloat based on a 64-bit JS number. + * @param {number} jsnumber + * @return {tcuFloat.deFloat} + */ +tcuFloat.newDeFloatFromNumber = function(jsnumber) { + return new tcuFloat.deFloat().deFloatNumber(jsnumber); +}; + +/** + * deFloatBuffer - To be used immediately after constructor + * Builds a tcuFloat.deFloat based on a buffer and a format description. + * The buffer is assumed to contain data of the given description. + * @param {ArrayBuffer} buffer + * @param {tcuFloat.FloatDescription} description + * @return {tcuFloat.deFloat} + */ +tcuFloat.deFloat.prototype.deFloatBuffer = function(buffer, description) { + this.m_buffer = buffer; + this.m_array = new Uint8Array(this.m_buffer); + + this.m_value = deMath.arrayToNumber(this.m_array); + + // Clear cached values + this.bitValue = undefined; + this.signValue = undefined; + this.expValue = undefined; + this.mantissaValue = undefined; + + return this; +}; + +/** + * Convenience function to build a tcuFloat.deFloat based on a buffer and a format description + * The buffer is assumed to contain data of the given description. + * @param {ArrayBuffer} buffer + * @param {tcuFloat.FloatDescription} description + * @return {tcuFloat.deFloat} + */ +tcuFloat.newDeFloatFromBuffer = function(buffer, description) { + return new tcuFloat.deFloat().deFloatBuffer(buffer, description); +}; + +/** + * Set the tcuFloat.deFloat to the given number. + * It does not perform any conversion; it assumes the number is compatible with + * the previously set description. + * @param {number} jsnumber + * @return {tcuFloat.deFloat} + **/ +tcuFloat.deFloat.prototype.deFloatParametersNumber = function(jsnumber) { + this.m_value = jsnumber; + deMath.numberToArray(this.m_array, jsnumber); + + // Clear cached values + this.bitValue = undefined; + this.signValue = undefined; + this.expValue = undefined; + this.mantissaValue = undefined; + + return this; +}; + +/** + * Initializes a tcuFloat.deFloat from the given number, + * with the specified format description. + * It does not perform any conversion; it assumes the number is compatible with + * the given description. + * @param {number} jsnumber + * @param {tcuFloat.FloatDescription} description + * @return {tcuFloat.deFloat} + **/ +tcuFloat.deFloat.prototype.deFloatParameters = function(jsnumber, description) { + this.description = description; + + this.m_buffer = new ArrayBuffer(this.description.totalByteSize); + this.m_array = new Uint8Array(this.m_buffer); + + return this.deFloatParametersNumber(jsnumber); +}; + +/** + * Convenience function to create a tcuFloat.deFloat from the given number, + * with the specified format description. + * It does not perform any conversion; it assumes the number is compatible with + * the given description. + * @param {number} jsnumber + * @param {tcuFloat.FloatDescription} description + * @return {tcuFloat.deFloat} + **/ +tcuFloat.newDeFloatFromParameters = function(jsnumber, description) { + return new tcuFloat.deFloat().deFloatParameters(jsnumber, description); +}; + +/** + * Returns bit range [begin, end) + * @param {number} begin + * @param {number} end + * @return {number} + */ +tcuFloat.deFloat.prototype.getBitRange = function(begin, end) { + return deMath.getBitRange(this.bits(), begin, end); +}; + +/** + * Returns the raw binary representation value of the tcuFloat.deFloat + * @return {number} + */ +tcuFloat.deFloat.prototype.bits = function() { + if (typeof this.bitValue === 'undefined') + this.bitValue = deMath.arrayToNumber(this.array()); + return this.bitValue; +}; + +/** + * Returns the raw binary sign bit + * @return {number} + */ +tcuFloat.deFloat.prototype.signBit = function() { + if (typeof this.signValue === 'undefined') + this.signValue = this.getBitRange(this.description.totalBitSize - 1, this.description.totalBitSize); + return this.signValue; +}; + +/** + * Returns the raw binary exponent bits + * @return {number} + */ +tcuFloat.deFloat.prototype.exponentBits = function() { + if (typeof this.expValue === 'undefined') + this.expValue = this.getBitRange(this.description.MantissaBits, this.description.MantissaBits + this.description.ExponentBits); + return this.expValue; +}; + +/** + * Returns the raw binary mantissa bits + * @return {number} + */ +tcuFloat.deFloat.prototype.mantissaBits = function() { + if (typeof this.mantissaValue === 'undefined') + this.mantissaValue = this.getBitRange(0, this.description.MantissaBits); + return this.mantissaValue; +}; + +/** + * Returns the sign as a factor (-1 or 1) + * @return {number} + */ +tcuFloat.deFloat.prototype.sign = function() { + var sign = this.signBit(); + var signvalue = sign ? -1 : 1; + return signvalue; +}; + +/** + * Returns the real exponent, checking if it's a denorm or zero number or not + * @return {number} + */ +tcuFloat.deFloat.prototype.exponent = function() {return this.isDenorm() ? 1 - this.description.ExponentBias : this.exponentBits() - this.description.ExponentBias;}; + +/** + * Returns the (still raw) mantissa, checking if it's a denorm or zero number or not + * Makes the normally implicit bit explicit. + * @return {number} + */ +tcuFloat.deFloat.prototype.mantissa = function() {return this.isZero() || this.isDenorm() ? this.mantissaBits() : deMath.binaryOp(this.mantissaBits(), deMath.shiftLeft(1, this.description.MantissaBits), deMath.BinaryOp.OR);}; + +/** + * Returns if the number is infinity or not. + * @return {boolean} + */ +tcuFloat.deFloat.prototype.isInf = function() {return this.exponentBits() == ((1 << this.description.ExponentBits) - 1) && this.mantissaBits() == 0;}; + +/** + * Returns if the number is NaN or not. + * @return {boolean} + */ +tcuFloat.deFloat.prototype.isNaN = function() {return this.exponentBits() == ((1 << this.description.ExponentBits) - 1) && this.mantissaBits() != 0;}; + +/** + * Returns if the number is zero or not. + * @return {boolean} + */ +tcuFloat.deFloat.prototype.isZero = function() {return this.exponentBits() == 0 && this.mantissaBits() == 0;}; + +/** + * Returns if the number is denormalized or not. + * @return {boolean} + */ +tcuFloat.deFloat.prototype.isDenorm = function() {return this.exponentBits() == 0 && this.mantissaBits() != 0;}; + +/** + * Builds a zero float of the current binary description. + * @param {number} sign + * @return {tcuFloat.deFloat} + */ +tcuFloat.deFloat.prototype.zero = function(sign) { + return this.description.zero(sign); +}; + +/** + * Builds an infinity float representation of the current binary description. + * @param {number} sign + * @return {tcuFloat.deFloat} + */ +tcuFloat.deFloat.prototype.inf = function(sign) { + return this.description.inf(sign); +}; + +/** + * Builds a NaN float representation of the current binary description. + * @return {tcuFloat.deFloat} + */ +tcuFloat.deFloat.prototype.nan = function() { + return this.description.nan(); +}; + +/** + * Builds a float of the current binary description. + * Given a sign, exponent and mantissa. + * @param {number} sign + * @param {number} exponent + * @param {number} mantissa + * @return {tcuFloat.deFloat} + */ +tcuFloat.deFloat.prototype.construct = function(sign, exponent, mantissa) { + return this.description.construct(sign, exponent, mantissa); +}; + +/** + * Builds a float of the current binary description. + * Given a sign, exponent and a raw binary mantissa. + * @param {number} sign + * @param {number} exponent + * @param {number} mantissaBits Raw binary mantissa. + * @return {tcuFloat.deFloat} + */ +tcuFloat.deFloat.prototype.constructBits = function(sign, exponent, mantissaBits) { + return this.description.constructBits(sign, exponent, mantissaBits); +}; + +/** + * Calculates the JS float number from the internal representation. + * @return {number} The JS float value represented by this tcuFloat.deFloat. + */ +tcuFloat.deFloat.prototype.getValue = function() { + if ((this.description.Flags | tcuFloat.FloatFlags.FLOAT_HAS_SIGN) === 0 && this.sign() < 0) + return 0; + if (this.isInf()) + return Number.Infinity; + if (this.isNaN()) + return Number.NaN; + if (this.isZero()) + return this.sign() * 0; + /**@type {number} */ var mymantissa = this.mantissa(); + /**@type {number} */ var myexponent = this.exponent(); + /**@type {number} */ var sign = this.sign(); + + /**@type {number} */ var value = mymantissa / Math.pow(2, this.description.MantissaBits) * Math.pow(2, myexponent); + + if (this.description.Flags | tcuFloat.FloatFlags.FLOAT_HAS_SIGN != 0) + value = value * sign; + + return value; +}; + +tcuFloat.description10 = new tcuFloat.FloatDescription(5, 5, 15, 0); +tcuFloat.description11 = new tcuFloat.FloatDescription(5, 6, 15, 0); +tcuFloat.description16 = new tcuFloat.FloatDescription(5, 10, 15, tcuFloat.FloatFlags.FLOAT_HAS_SIGN); +tcuFloat.description32 = new tcuFloat.FloatDescription(8, 23, 127, tcuFloat.FloatFlags.FLOAT_HAS_SIGN | tcuFloat.FloatFlags.FLOAT_SUPPORT_DENORM); +tcuFloat.description64 = new tcuFloat.FloatDescription(11, 52, 1023, tcuFloat.FloatFlags.FLOAT_HAS_SIGN | tcuFloat.FloatFlags.FLOAT_SUPPORT_DENORM); + +tcuFloat.convertFloat32Inline = (function() { + var float32View = new Float32Array(1); + var int32View = new Int32Array(float32View.buffer); + + return function(fval, description) { + float32View[0] = fval; + var fbits = int32View[0]; + + var exponentBits = (fbits >> 23) & 0xff; + var mantissaBits = fbits & 0x7fffff; + var signBit = (fbits & 0x80000000) ? 1 : 0; + var sign = signBit ? -1 : 1; + + var isZero = exponentBits == 0 && mantissaBits == 0; + + var bitDiff; + var half; + var bias; + + if (!(description.Flags & tcuFloat.FloatFlags.FLOAT_HAS_SIGN) && sign < 0) { + // Negative number, truncate to zero. + return description.zeroNumber(+1); + } else if (exponentBits == ((1 << tcuFloat.description32.ExponentBits) - 1) && mantissaBits == 0) { // isInf + return description.infNumber(sign); + } else if (exponentBits == ((1 << tcuFloat.description32.ExponentBits) - 1) && mantissaBits != 0) { // isNaN + return description.nanNumber(); + } else if (isZero) { + return description.zeroNumber(sign); + } else { + var eMin = 1 - description.ExponentBias; + var eMax = ((1 << description.ExponentBits) - 2) - description.ExponentBias; + + var isDenorm = exponentBits == 0 && mantissaBits != 0; + + var s = signBit << (description.ExponentBits + description.MantissaBits); // \note Not sign, but sign bit. + var e = isDenorm ? 1 - tcuFloat.description32.ExponentBias : exponentBits - tcuFloat.description32.ExponentBias;// other.exponent(); + var m = isZero || isDenorm ? mantissaBits : mantissaBits | (1 << tcuFloat.description32.MantissaBits); // other.mantissa(); + + // Normalize denormalized values prior to conversion. + while (!(m & (1 << tcuFloat.description32.MantissaBits))) { + m = deMath.shiftLeft(m, 1); + e -= 1; + } + + if (e < eMin) { + // Underflow. + if ((description.Flags & tcuFloat.FloatFlags.FLOAT_SUPPORT_DENORM) && (eMin - e - 1 <= description.MantissaBits)) { + // Shift and round (RTE). + bitDiff = (tcuFloat.description32.MantissaBits - description.MantissaBits) + (eMin - e); + half = (1 << (bitDiff - 1)) - 1; + bias = ((m >> bitDiff) & 1); + return (s | ((m + half + bias) >> bitDiff)); + } else + return description.zeroNumber(sign); + } else { + // Remove leading 1. + m = (m & ~(1 << tcuFloat.description32.MantissaBits)); + + if (description.MantissaBits < tcuFloat.description32.MantissaBits) { + // Round mantissa (round to nearest even). + bitDiff = tcuFloat.description32.MantissaBits - description.MantissaBits; + half = (1 << (bitDiff - 1)) - 1; + bias = ((m >> bitDiff) & 1); + + m = (m + half + bias) >> bitDiff; + + if ((m & (1 << description.MantissaBits))) { + // Overflow in mantissa. + m = 0; + e += 1; + } + } else { + bitDiff = description.MantissaBits - tcuFloat.description32.MantissaBits; + m = (m << bitDiff); + } + + if (e > eMax) { + // Overflow. + return description.infNumber(sign); + } else { + DE_ASSERT(deMath.deInRange32(e, eMin, eMax)); + DE_ASSERT(((e + description.ExponentBias) & ~((1 << description.ExponentBits) - 1)) == 0); + DE_ASSERT((m & ~((1 << description.MantissaBits) - 1)) == 0); + + return (s | ((e + description.ExponentBias) << description.MantissaBits)) | m; + } + } + } + }; +})(); + +/** + * Builds a 10 bit tcuFloat.deFloat + * @param {number} value (64-bit JS float) + * @return {tcuFloat.deFloat} + */ +tcuFloat.newFloat10 = function(value) { + /**@type {tcuFloat.deFloat} */ var other32 = new tcuFloat.deFloat().deFloatNumber(value); + return tcuFloat.description10.convert(other32); +}; + +/** + * Builds a 11 bit tcuFloat.deFloat + * @param {number} value (64-bit JS float) + * @return {tcuFloat.deFloat} + */ +tcuFloat.newFloat11 = function(value) { + /**@type {tcuFloat.deFloat} */ var other32 = new tcuFloat.deFloat().deFloatNumber(value); + return tcuFloat.description11.convert(other32); +}; + +/** + * Builds a 16 bit tcuFloat.deFloat + * @param {number} value (64-bit JS float) + * @return {tcuFloat.deFloat} + */ +tcuFloat.newFloat16 = function(value) { + /**@type {tcuFloat.deFloat} */ var other32 = new tcuFloat.deFloat().deFloatNumber(value); + return tcuFloat.description16.convert(other32); +}; + +/** + * Builds a 16 bit tcuFloat.deFloat from raw bits + * @param {number} value (16-bit value) + * @return {tcuFloat.deFloat} + */ +tcuFloat.newFloat32From16 = function(value) { + var other16 = tcuFloat.newDeFloatFromParameters(value, tcuFloat.description16); + return tcuFloat.description32.convert(other16); +}; + +/** + * Builds a 16 bit tcuFloat.deFloat with no denorm support + * @param {number} value (64-bit JS float) + * @return {tcuFloat.deFloat} + */ +tcuFloat.newFloat16NoDenorm = function(value) { + /**@type {tcuFloat.deFloat} */ var other32 = new tcuFloat.deFloat().deFloatNumber(value); + return tcuFloat.description16.convert(other32); +}; + +/** + * Builds a 32 bit tcuFloat.deFloat + * @param {number} value (64-bit JS float) + * @return {tcuFloat.deFloat} + */ +tcuFloat.newFloat32 = function(value) { + return new tcuFloat.deFloat().deFloatNumber(value); +}; + +tcuFloat.numberToFloat11 = function(value) { + return tcuFloat.convertFloat32Inline(value, tcuFloat.description11); +}; + +tcuFloat.float11ToNumber = (function() { + var x = tcuFloat.newDeFloatFromParameters(0, tcuFloat.description11); + return function(float11) { + x.deFloatParametersNumber(float11); + return x.getValue(); + }; +})(); + +tcuFloat.numberToFloat10 = function(value) { + return tcuFloat.convertFloat32Inline(value, tcuFloat.description10); +}; + +tcuFloat.float10ToNumber = (function() { + var x = tcuFloat.newDeFloatFromParameters(0, tcuFloat.description10); + return function(float10) { + x.deFloatParametersNumber(float10); + return x.getValue(); + }; +})(); + +tcuFloat.numberToHalfFloat = function(value) { + return tcuFloat.convertFloat32Inline(value, tcuFloat.description16); +}; + +tcuFloat.numberToHalfFloatNoDenorm = function(value) { + return tcuFloat.newFloat16NoDenorm(value).bits(); +}; + +tcuFloat.halfFloatToNumber = (function() { + var x = tcuFloat.newDeFloatFromParameters(0, tcuFloat.description16); + return function(half) { + x.deFloatParametersNumber(half); + return x.getValue(); + }; +})(); + +tcuFloat.halfFloatToNumberNoDenorm = tcuFloat.halfFloatToNumber; + +/** + * Builds a 64 bit tcuFloat.deFloat + * @param {number} value (64-bit JS float) + * @return {tcuFloat.deFloat} + */ +tcuFloat.newFloat64 = function(value) { + return new tcuFloat.deFloat().deFloatParameters(value, tcuFloat.description64); +}; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFloatFormat.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFloatFormat.js new file mode 100644 index 000000000..a0b4dc82c --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFloatFormat.js @@ -0,0 +1,349 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program Tester Core + * ---------------------------------------- + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *//*! + * \file + * \brief Adjustable-precision floating point operations. + *//*--------------------------------------------------------------------*/ + 'use strict'; + goog.provide('framework.common.tcuFloatFormat'); + + goog.require('framework.common.tcuInterval'); +goog.require('framework.delibs.debase.deMath'); + + goog.scope(function() { + + var tcuFloatFormat = framework.common.tcuFloatFormat; + var deMath = framework.delibs.debase.deMath; + var tcuInterval = framework.common.tcuInterval; + + /** + * @param {tcuFloatFormat.YesNoMaybe} choice + * @param {tcuInterval.Interval} no + * @param {tcuInterval.Interval} yes + * @return {tcuInterval.Interval} + */ + tcuFloatFormat.chooseInterval = function(choice, no, yes) { + switch (choice) { + case tcuFloatFormat.YesNoMaybe.NO: return no; + case tcuFloatFormat.YesNoMaybe.YES: return yes; + case tcuFloatFormat.YesNoMaybe.MAYBE: return no.operatorOrBinary(yes); + default: throw new Error('Impossible case'); + } + }; + + /** + * @param {number} maxExp + * @param {number} fractionBits + * @return {number} + */ + tcuFloatFormat.computeMaxValue = function(maxExp, fractionBits) { + return deMath.deLdExp(1, maxExp) + deMath.deLdExp(Math.pow(2, fractionBits) - 1, maxExp - fractionBits); + }; + + /** + * @enum {number} + */ + tcuFloatFormat.YesNoMaybe = { + NO: 0, + MAYBE: 1, + YES: 2 + }; + + /** + * @constructor + * @param {number} minExp + * @param {number} maxExp + * @param {number} fractionBits + * @param {boolean} exactPrecision + * @param {tcuFloatFormat.YesNoMaybe=} hasSubnormal + * @param {tcuFloatFormat.YesNoMaybe=} hasInf + * @param {tcuFloatFormat.YesNoMaybe=} hasNaN + */ + tcuFloatFormat.FloatFormat = function(minExp, maxExp, fractionBits, exactPrecision, hasSubnormal, hasInf, hasNaN) { + // /** @type{number} */ var exponentShift (int exp) const; + // Interval clampValue (double d) const; + + /** @type {number} */ this.m_minExp = minExp; // Minimum exponent, inclusive + /** @type {number} */ this.m_maxExp = maxExp; // Maximum exponent, inclusive + /** @type {number} */ this.m_fractionBits = fractionBits; // Number of fractional bits in significand + /** @type {tcuFloatFormat.YesNoMaybe} */ this.m_hasSubnormal = hasSubnormal === undefined ? tcuFloatFormat.YesNoMaybe.MAYBE : hasSubnormal; // Does the format support denormalized numbers? + /** @type {tcuFloatFormat.YesNoMaybe} */ this.m_hasInf = hasInf === undefined ? tcuFloatFormat.YesNoMaybe.MAYBE : hasInf; // Does the format support infinities? + /** @type {tcuFloatFormat.YesNoMaybe} */ this.m_hasNaN = hasNaN === undefined ? tcuFloatFormat.YesNoMaybe.MAYBE : hasNaN; // Does the format support NaNs? + /** @type {boolean} */ this.m_exactPrecision = exactPrecision; // Are larger precisions disallowed? + /** @type {number} */ this.m_maxValue = tcuFloatFormat.computeMaxValue(maxExp, fractionBits); + }; + + /** + * @return {number} + */ + tcuFloatFormat.FloatFormat.prototype.getMinExp = function() { + return this.m_minExp; + }; + + /** + * @return {number} + */ + tcuFloatFormat.FloatFormat.prototype.getMaxExp = function() { + return this.m_maxExp; + }; + + /** + * @return {number} + */ + tcuFloatFormat.FloatFormat.prototype.getMaxValue = function() { + return this.m_maxValue; + }; + + /** + * @return {number} + */ + tcuFloatFormat.FloatFormat.prototype.getFractionBits = function() { + return this.m_fractionBits; + }; + + /** + * @return {tcuFloatFormat.YesNoMaybe} + */ + tcuFloatFormat.FloatFormat.prototype.hasSubnormal = function() { + return this.m_hasSubnormal; + }; + + /** + * @return {tcuFloatFormat.YesNoMaybe} + */ + tcuFloatFormat.FloatFormat.prototype.hasInf = function() { + return this.m_hasInf; + }; + + /** + * @param {number} x + * @param {number} count + * @return {number} + */ + tcuFloatFormat.FloatFormat.prototype.ulp = function(x, count) { + var breakdown = deMath.deFractExp(Math.abs(x)); + /** @type {number} */ var exp = breakdown.exponent; + /** @type {number} */ var frac = breakdown.significand; + + if (isNaN(frac)) + return NaN; + else if (!isFinite(frac)) + return deMath.deLdExp(1.0, this.m_maxExp - this.m_fractionBits); + else if (frac == 1.0) { + // Harrison's ULP: choose distance to closest (i.e. next lower) at binade + // boundary. + --exp; + } else if (frac == 0.0) + exp = this.m_minExp; + + // ULP cannot be lower than the smallest quantum. + exp = Math.max(exp, this.m_minExp); + + /** @type {number} */ var oneULP = deMath.deLdExp(1.0, exp - this.m_fractionBits); + // ScopedRoundingMode ctx (DE_ROUNDINGMODE_TO_POSITIVE_INF); + + return oneULP * count; + }; + + /** + * Return the difference between the given nominal exponent and + * the exponent of the lowest significand bit of the + * representation of a number with this format. + * For normal numbers this is the number of significand bits, but + * for subnormals it is less and for values of exp where 2^exp is too + * small to represent it is <0 + * @param {number} exp + * @return {number} + */ + tcuFloatFormat.FloatFormat.prototype.exponentShift = function(exp) { + return this.m_fractionBits - Math.max(this.m_minExp - exp, 0); + }; + + /** + * @param {number} d + * @param {boolean} upward + * @return {number} + */ + tcuFloatFormat.FloatFormat.prototype.round = function(d, upward) { + var breakdown = deMath.deFractExp(d); + /** @type {number} */ var exp = breakdown.exponent; + /** @type {number} */ var frac = breakdown.significand; + + var shift = this.exponentShift(exp); + var shiftFrac = deMath.deLdExp(frac, shift); + var roundFrac = upward ? Math.ceil(shiftFrac) : Math.floor(shiftFrac); + + return deMath.deLdExp(roundFrac, exp - shift); + }; + + /** + * Return the range of numbers that `d` might be converted to in the + * floatformat, given its limitations with infinities, subnormals and maximum + * exponent. + * @param {number} d + * @return {tcuInterval.Interval} + */ + tcuFloatFormat.FloatFormat.prototype.clampValue = function(d) { + /** @type {number} */ var rSign = deMath.deSign(d); + /** @type {number} */ var rExp = 0; + + // DE_ASSERT(!isNaN(d)); + + var breakdown = deMath.deFractExp(d); + rExp = breakdown.exponent; + if (rExp < this.m_minExp) + return tcuFloatFormat.chooseInterval(this.m_hasSubnormal, new tcuInterval.Interval(rSign * 0.0), new tcuInterval.Interval(d)); + else if (!isFinite(d) || rExp > this.m_maxExp) + return tcuFloatFormat.chooseInterval(this.m_hasInf, new tcuInterval.Interval(rSign * this.getMaxValue()), new tcuInterval.Interval(rSign * Number.POSITIVE_INFINITY)); + + return new tcuInterval.Interval(d); + }; + + /** + * @param {number} d + * @param {boolean} upward + * @param {boolean} roundUnderOverflow + * @return {number} + */ + tcuFloatFormat.FloatFormat.prototype.roundOutDir = function(d, upward, roundUnderOverflow) { + var breakdown = deMath.deFractExp(d); + var exp = breakdown.exponent; + + if (roundUnderOverflow && exp > this.m_maxExp && (upward == (d < 0.0))) + return deMath.deSign(d) * this.getMaxValue(); + else + return this.round(d, upward); + }; + + /** + * @param {tcuInterval.Interval} x + * @param {boolean} roundUnderOverflow + * @return {tcuInterval.Interval} + */ + tcuFloatFormat.FloatFormat.prototype.roundOut = function(x, roundUnderOverflow) { + /** @type {tcuInterval.Interval} */ var ret = x.nan(); + + if (!x.empty()) { + var a = new tcuInterval.Interval(this.roundOutDir(x.lo(), false, roundUnderOverflow)); + var b = new tcuInterval.Interval(this.roundOutDir(x.hi(), true, roundUnderOverflow)); + ret.operatorOrAssignBinary(tcuInterval.withIntervals(a, b)); + } + return ret; + }; + + //! Return the range of numbers that might be used with this format to + //! represent a number within `x`. + /** + * @param {tcuInterval.Interval} x + * @return {tcuInterval.Interval} + */ + tcuFloatFormat.FloatFormat.prototype.convert = function(x) { + /** @type {tcuInterval.Interval} */ var ret = new tcuInterval.Interval(); + /** @type {tcuInterval.Interval} */ var tmp = x; + + if (x.hasNaN()) { + // If NaN might be supported, NaN is a legal return value + if (this.m_hasNaN != tcuFloatFormat.YesNoMaybe.NO) + ret.operatorOrAssignBinary(new tcuInterval.Interval(NaN)); + + // If NaN might not be supported, any (non-NaN) value is legal, + // _subject_ to clamping. Hence we modify tmp, not ret. + if (this.m_hasNaN != tcuFloatFormat.YesNoMaybe.YES) + tmp = tcuInterval.unbounded(); + } + + // Round both bounds _inwards_ to closest representable values. + if (!tmp.empty()) + ret.operatorOrAssignBinary( + this.clampValue(this.round(tmp.lo(), true)).operatorOrBinary( + this.clampValue(this.round(tmp.hi(), false)))); + + // If this format's precision is not exact, the (possibly out-of-bounds) + // original value is also a possible result. + if (!this.m_exactPrecision) + ret.operatorOrAssignBinary(x); + + return ret; + }; + + /** + * @param {number} x + * @return {string} + */ + tcuFloatFormat.FloatFormat.prototype.floatToHex = function(x) { + if (isNaN(x)) + return 'NaN'; + else if (!isFinite(x)) + return (x < 0.0 ? '-' : '+') + ('inf'); + else if (x == 0.0) // \todo [2014-03-27 lauri] Negative zero + return '0.0'; + + return x.toString(10); + // TODO + // var breakdown = deMath.deFractExp(deAbs(x)); + // /** @type{number} */ var exp = breakdown.exponent; + // /** @type{number} */ var frac = breakdown.significand; + // /** @type{number} */ var shift = this.exponentShift(exp); + // /** @type{number} */ var bits = deUint64(deLdExp(frac, shift)); + // /** @type{number} */ var whole = bits >> m_fractionBits; + // /** @type{number} */ var fraction = bits & ((deUint64(1) << m_fractionBits) - 1); + // /** @type{number} */ var exponent = exp + m_fractionBits - shift; + // /** @type{number} */ var numDigits = (this.m_fractionBits + 3) / 4; + // /** @type{number} */ var aligned = fraction << (numDigits * 4 - m_fractionBits); + // /** @type{string} */ var oss = ''; + + // oss + (x < 0 ? '-' : '') + // + '0x' + whole + '.' + // + std::hex + std::setw(numDigits) + std::setfill('0') + aligned + // + 'p' + std::dec + std::setw(0) + exponent; + //return oss; + }; + + /** + * @param {tcuInterval.Interval} interval + * @return {string} + */ + tcuFloatFormat.FloatFormat.prototype.intervalToHex = function(interval) { + if (interval.empty()) + return interval.hasNaN() ? '{ NaN }' : '{}'; + + else if (interval.lo() == interval.hi()) + return ((interval.hasNaN() ? '{ NaN, ' : '{ ') + + this.floatToHex(interval.lo()) + ' }'); + else if (interval == tcuInterval.unbounded(true)) + return '<any>'; + + return ((interval.hasNaN() ? '{ NaN } | ' : '') + + '[' + this.floatToHex(interval.lo()) + ', ' + this.floatToHex(interval.hi()) + ']'); + }; + + /** + * @return {tcuFloatFormat.FloatFormat} + */ + tcuFloatFormat.nativeDouble = function() { + return new tcuFloatFormat.FloatFormat(-1021 - 1, // min_exponent + 1024 - 1, // max_exponent + 53 - 1, // digits + true, // has_denorm + tcuFloatFormat.YesNoMaybe.YES, // has_infinity + tcuFloatFormat.YesNoMaybe.YES, // has_quiet_nan + tcuFloatFormat.YesNoMaybe.YES); // has_denorm + }; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFuzzyImageCompare.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFuzzyImageCompare.js new file mode 100644 index 000000000..aea5bf7e0 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuFuzzyImageCompare.js @@ -0,0 +1,338 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuFuzzyImageCompare'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.common.tcuTextureUtil'); +goog.require('framework.delibs.debase.deMath'); +goog.require('framework.delibs.debase.deRandom'); + +goog.scope(function() { + +var tcuFuzzyImageCompare = framework.common.tcuFuzzyImageCompare; +var deMath = framework.delibs.debase.deMath; +var deRandom = framework.delibs.debase.deRandom; +var tcuTexture = framework.common.tcuTexture; +var tcuTextureUtil = framework.common.tcuTextureUtil; + + var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); + }; + + /** + * tcuFuzzyImageCompare.FuzzyCompareParams struct + * @constructor + * @param {number=} maxSampleSkip_ + * @param {number=} minErrThreshold_ + * @param {number=} errExp_ + */ + tcuFuzzyImageCompare.FuzzyCompareParams = function(maxSampleSkip_, minErrThreshold_, errExp_) { + /** @type {number} */ this.maxSampleSkip = maxSampleSkip_ === undefined ? 8 : maxSampleSkip_; + /** @type {number} */ this.minErrThreshold = minErrThreshold_ === undefined ? 4 : minErrThreshold_; + /** @type {number} */ this.errExp = errExp_ === undefined ? 4.0 : errExp_; + }; + + /** + * @param {Array<number>} v + * @return {Array<number>} + */ + tcuFuzzyImageCompare.roundArray4ToUint8Sat = function(v) { + return [ + deMath.clamp(Math.trunc(v[0] + 0.5), 0, 255), + deMath.clamp(Math.trunc(v[1] + 0.5), 0, 255), + deMath.clamp(Math.trunc(v[2] + 0.5), 0, 255), + deMath.clamp(Math.trunc(v[3] + 0.5), 0, 255) + ]; + }; + + /** + * @param {Array<number>} pa + * @param {Array<number>} pb + * @param {number} minErrThreshold + * @return {number} + */ + tcuFuzzyImageCompare.compareColors = function(pa, pb, minErrThreshold) { + /** @type {number}*/ var r = Math.max(Math.abs(pa[0] - pb[0]) - minErrThreshold, 0); + /** @type {number}*/ var g = Math.max(Math.abs(pa[1] - pb[1]) - minErrThreshold, 0); + /** @type {number}*/ var b = Math.max(Math.abs(pa[2] - pb[2]) - minErrThreshold, 0); + /** @type {number}*/ var a = Math.max(Math.abs(pa[3] - pb[3]) - minErrThreshold, 0); + + /** @type {number}*/ var scale = 1.0 / (255 - minErrThreshold); + /** @type {number}*/ var sqSum = (r * r + g * g + b * b + a * a) * (scale * scale); + + return Math.sqrt(sqSum); + }; + + /** + * @param {tcuTexture.RGBA8View} src + * @param {number} u + * @param {number} v + * @param {number} NumChannels + * @return {Array<number>} + */ + tcuFuzzyImageCompare.bilinearSample = function(src, u, v, NumChannels) { + /** @type {number}*/ var w = src.width; + /** @type {number}*/ var h = src.height; + + /** @type {number}*/ var x0 = Math.floor(u - 0.5); + /** @type {number}*/ var x1 = x0 + 1; + /** @type {number}*/ var y0 = Math.floor(v - 0.5); + /** @type {number}*/ var y1 = y0 + 1; + + /** @type {number}*/ var i0 = deMath.clamp(x0, 0, w - 1); + /** @type {number}*/ var i1 = deMath.clamp(x1, 0, w - 1); + /** @type {number}*/ var j0 = deMath.clamp(y0, 0, h - 1); + /** @type {number}*/ var j1 = deMath.clamp(y1, 0, h - 1); + + /** @type {number}*/ var a = (u - 0.5) - Math.floor(u - 0.5); + /** @type {number}*/ var b = (u - 0.5) - Math.floor(u - 0.5); + + /** @type {Array<number>} */ var p00 = src.read(i0, j0, NumChannels); + /** @type {Array<number>} */ var p10 = src.read(i1, j0, NumChannels); + /** @type {Array<number>} */ var p01 = src.read(i0, j1, NumChannels); + /** @type {Array<number>} */ var p11 = src.read(i1, j1, NumChannels); + /** @type {number} */ var dst = 0; + + // Interpolate. + /** @type {Array<number>}*/ var f = []; + for (var c = 0; c < NumChannels; c++) { + f[c] = p00[c] * (1.0 - a) * (1.0 - b) + + (p10[c] * a * (1.0 - b)) + + (p01[c] * (1.0 - a) * b) + + (p11[c] * a * b); + } + + return tcuFuzzyImageCompare.roundArray4ToUint8Sat(f); + }; + + /** + * @param {tcuTexture.RGBA8View} dst + * @param {tcuTexture.RGBA8View} src + * @param {number} shiftX + * @param {number} shiftY + * @param {Array<number>} kernelX + * @param {Array<number>} kernelY + * @param {number} DstChannels + * @param {number} SrcChannels + */ + tcuFuzzyImageCompare.separableConvolve = function(dst, src, shiftX, shiftY, kernelX, kernelY, DstChannels, SrcChannels) { + DE_ASSERT(dst.width == src.width && dst.height == src.height); + + /** @type {tcuTexture.TextureLevel} */ var tmp = new tcuTexture.TextureLevel(dst.getFormat(), dst.height, dst.width); + var tmpView = new tcuTexture.RGBA8View(tmp.getAccess()); + + /** @type {number} */ var kw = kernelX.length; + /** @type {number} */ var kh = kernelY.length; + + /** @type {Array<number>} */ var sum = []; + /** @type {number} */ var f; + /** @type {Array<number>} */ var p; + + // Horizontal pass + // \note Temporary surface is written in column-wise order + for (var j = 0; j < src.height; j++) { + for (var i = 0; i < src.width; i++) { + sum[0] = sum[1] = sum[2] = sum[3] = 0; + for (var kx = 0; kx < kw; kx++) { + f = kernelX[kw - kx - 1]; + p = src.read(deMath.clamp(i + kx - shiftX, 0, src.width - 1), j, SrcChannels); + sum = deMath.add(sum, deMath.scale(p, f)); + } + + sum = tcuFuzzyImageCompare.roundArray4ToUint8Sat(sum); + tmpView.write(j, i, sum, DstChannels); + } + } + + // Vertical pass + for (var j = 0; j < src.height; j++) { + for (var i = 0; i < src.width; i++) { + sum[0] = sum[1] = sum[2] = sum[3] = 0; + for (var ky = 0; ky < kh; ky++) { + f = kernelY[kh - ky - 1]; + p = tmpView.read(deMath.clamp(j + ky - shiftY, 0, tmpView.width - 1), i, DstChannels); + sum = deMath.add(sum, deMath.scale(p, f)); + } + + sum = tcuFuzzyImageCompare.roundArray4ToUint8Sat(sum); + dst.write(i, j, sum, DstChannels); + } + } + }; + + /** + * @param {tcuFuzzyImageCompare.FuzzyCompareParams} params + * @param {deRandom.Random} rnd + * @param {Array<number>} pixel + * @param {tcuTexture.RGBA8View} surface + * @param {number} x + * @param {number} y + * @param {number} NumChannels + * @return {number} + */ + tcuFuzzyImageCompare.compareToNeighbor = function(params, rnd, pixel, surface, x, y, NumChannels) { + /** @type {number} */ var minErr = 100; + + // (x, y) + (0, 0) + minErr = Math.min(minErr, tcuFuzzyImageCompare.compareColors(pixel, surface.read(x, y, NumChannels), params.minErrThreshold)); + if (minErr == 0.0) + return minErr; + + // Area around (x, y) + /** @type {Array<Array.<number>>} */ var s_coords = + [ + [-1, -1], + [0, -1], + [1, -1], + [-1, 0], + [1, 0], + [-1, 1], + [0, 1], + [1, 1] + ]; + + /** @type {number} */ var dx; + /** @type {number} */ var dy; + + for (var d = 0; d < s_coords.length; d++) { + dx = x + s_coords[d][0]; + dy = y + s_coords[d][1]; + + if (!deMath.deInBounds32(dx, 0, surface.width) || !deMath.deInBounds32(dy, 0, surface.height)) + continue; + + minErr = Math.min(minErr, tcuFuzzyImageCompare.compareColors(pixel, surface.read(dx, dy, NumChannels), params.minErrThreshold)); + if (minErr == 0.0) + return minErr; + } + + // Random bilinear-interpolated samples around (x, y) + for (var s = 0; s < 32; s++) { + dx = x + rnd.getFloat() * 2.0 - 0.5; + dy = y + rnd.getFloat() * 2.0 - 0.5; + + /** @type {Array<number>} */ var sample = tcuFuzzyImageCompare.bilinearSample(surface, dx, dy, NumChannels); + + minErr = Math.min(minErr, tcuFuzzyImageCompare.compareColors(pixel, sample, params.minErrThreshold)); + if (minErr == 0.0) + return minErr; + } + + return minErr; + }; + + /** + * @param {Array<number>} c + * @return {number} + */ + tcuFuzzyImageCompare.toGrayscale = function(c) { + return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]; + }; + + /** + * @param {tcuTexture.TextureFormat} format + * @return {boolean} + */ + tcuFuzzyImageCompare.isFormatSupported = function(format) { + return format.type == tcuTexture.ChannelType.UNORM_INT8 && (format.order == tcuTexture.ChannelOrder.RGB || format.order == tcuTexture.ChannelOrder.RGBA); + }; + + /** + * @param {tcuFuzzyImageCompare.FuzzyCompareParams} params + * @param {tcuTexture.ConstPixelBufferAccess} ref + * @param {tcuTexture.ConstPixelBufferAccess} cmp + * @param {tcuTexture.PixelBufferAccess} errorMask + * @return {number} + */ + tcuFuzzyImageCompare.fuzzyCompare = function(params, ref, cmp, errorMask) { + assertMsgOptions(ref.getWidth() == cmp.getWidth() && ref.getHeight() == cmp.getHeight(), + 'Reference and result images have different dimensions', false, true); + + assertMsgOptions(ref.getWidth() == errorMask.getWidth() && ref.getHeight() == errorMask.getHeight(), + 'Reference and error mask images have different dimensions', false, true); + + if (!tcuFuzzyImageCompare.isFormatSupported(ref.getFormat()) || !tcuFuzzyImageCompare.isFormatSupported(cmp.getFormat())) + throw new Error('Unsupported format in fuzzy comparison'); + + /** @type {number} */ var width = ref.getWidth(); + /** @type {number} */ var height = ref.getHeight(); + /** @type {deRandom.Random} */ var rnd = new deRandom.Random(667); + + // Filtered + /** @type {tcuTexture.TextureLevel} */ var refFiltered = new tcuTexture.TextureLevel(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8), width, height); + /** @type {tcuTexture.TextureLevel} */ var cmpFiltered = new tcuTexture.TextureLevel(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8), width, height); + + var refView = new tcuTexture.RGBA8View(ref); + var cmpView = new tcuTexture.RGBA8View(cmp); + var refFilteredView = new tcuTexture.RGBA8View(tcuTexture.PixelBufferAccess.newFromTextureLevel(refFiltered)); + var cmpFilteredView = new tcuTexture.RGBA8View(tcuTexture.PixelBufferAccess.newFromTextureLevel(cmpFiltered)); + + // Kernel = {0.15, 0.7, 0.15} + /** @type {Array<number>} */ var kernel = [0.1, 0.8, 0.1]; + /** @type {number} */ var shift = Math.floor((kernel.length - 1) / 2); + + switch (ref.getFormat().order) { + case tcuTexture.ChannelOrder.RGBA: tcuFuzzyImageCompare.separableConvolve(refFilteredView, refView, shift, shift, kernel, kernel, 4, 4); break; + case tcuTexture.ChannelOrder.RGB: tcuFuzzyImageCompare.separableConvolve(refFilteredView, refView, shift, shift, kernel, kernel, 4, 3); break; + default: + throw new Error('tcuFuzzyImageCompare.fuzzyCompare - Invalid ChannelOrder'); + } + + switch (cmp.getFormat().order) { + case tcuTexture.ChannelOrder.RGBA: tcuFuzzyImageCompare.separableConvolve(cmpFilteredView, cmpView, shift, shift, kernel, kernel, 4, 4); break; + case tcuTexture.ChannelOrder.RGB: tcuFuzzyImageCompare.separableConvolve(cmpFilteredView, cmpView, shift, shift, kernel, kernel, 4, 3); break; + default: + throw new Error('tcuFuzzyImageCompare.fuzzyCompare - Invalid ChannelOrder'); + } + + /** @type {number} */ var numSamples = 0; + /** @type {number} */ var errSum = 0.0; + + // Clear error mask to green. + errorMask.clear([0.0, 1.0, 0.0, 1.0]); + + for (var y = 1; y < height - 1; y++) { + for (var x = 1; x < width - 1; x += params.maxSampleSkip > 0 ? rnd.getInt(0, params.maxSampleSkip) : 1) { + /** @type {number} */ var err = Math.min(tcuFuzzyImageCompare.compareToNeighbor(params, rnd, refFilteredView.read(x, y, 4), cmpFilteredView, x, y, 4), + tcuFuzzyImageCompare.compareToNeighbor(params, rnd, cmpFilteredView.read(x, y, 4), refFilteredView, x, y, 4)); + + err = Math.pow(err, params.errExp); + + errSum += err; + numSamples += 1; + + // Build error image. + /** @type {number} */ var red = err * 500.0; + /** @type {number} */ var luma = tcuFuzzyImageCompare.toGrayscale(cmp.getPixel(x, y)); + /** @type {number} */ var rF = 0.7 + 0.3 * luma; + errorMask.setPixel([red * rF, (1.0 - red) * rF, 0.0, 1.0], x, y); + + } + } + + // Scale error sum based on number of samples taken + errSum *= ((width - 2) * (height - 2)) / numSamples; + + return errSum; + }; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuImageCompare.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuImageCompare.js new file mode 100644 index 000000000..3a8138ef2 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuImageCompare.js @@ -0,0 +1,757 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuImageCompare'); +goog.require('framework.common.tcuBilinearImageCompare'); +goog.require('framework.common.tcuFloat'); +goog.require('framework.common.tcuFuzzyImageCompare'); +goog.require('framework.common.tcuLogImage'); +goog.require('framework.common.tcuRGBA'); +goog.require('framework.common.tcuSurface'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.common.tcuTextureUtil'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + +var tcuImageCompare = framework.common.tcuImageCompare; +var tcuSurface = framework.common.tcuSurface; +var deMath = framework.delibs.debase.deMath; +var tcuTexture = framework.common.tcuTexture; +var tcuTextureUtil = framework.common.tcuTextureUtil; +var tcuFloat = framework.common.tcuFloat; +var tcuFuzzyImageCompare = framework.common.tcuFuzzyImageCompare; +var tcuBilinearImageCompare = framework.common.tcuBilinearImageCompare; +var tcuRGBA = framework.common.tcuRGBA; +var tcuLogImage = framework.common.tcuLogImage; + +/** + * @enum + */ +tcuImageCompare.CompareLogMode = { + EVERYTHING: 0, + RESULT: 1, + ON_ERROR: 2 +}; + +/** + * @param {framework.common.tcuTexture.ConstPixelBufferAccess} result + * @param {framework.common.tcuTexture.ConstPixelBufferAccess} reference + * @param {framework.common.tcuTexture.ConstPixelBufferAccess=} diff + */ +tcuImageCompare.displayImages = function(result, reference, diff) { + var limits = tcuImageCompare.computeScaleAndBias(reference, result); + tcuLogImage.logImage('Result', '', result, limits.scale, limits.bias); + tcuLogImage.logImage('Reference', '', reference, limits.scale, limits.bias); + if (diff) + tcuLogImage.logImage('Error', 'error mask', diff); +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} reference + * @param {tcuTexture.ConstPixelBufferAccess} result + * @return {{scale: Array<number>, bias: Array<number>}} + */ +tcuImageCompare.computeScaleAndBias = function(reference, result) { + var minVal = []; + var maxVal = []; + var scale = []; + var bias = []; + + var eps = 0.0001; + var referenceRange = tcuTextureUtil.estimatePixelValueRange(reference); + var resultRange = tcuTextureUtil.estimatePixelValueRange(result); + + minVal[0] = Math.min(referenceRange[0][0], resultRange[0][0]); + minVal[1] = Math.min(referenceRange[0][1], resultRange[0][1]); + minVal[2] = Math.min(referenceRange[0][2], resultRange[0][2]); + minVal[3] = Math.min(referenceRange[0][3], resultRange[0][3]); + + maxVal[0] = Math.max(referenceRange[1][0], resultRange[1][0]); + maxVal[1] = Math.max(referenceRange[1][1], resultRange[1][1]); + maxVal[2] = Math.max(referenceRange[1][2], resultRange[1][2]); + maxVal[3] = Math.max(referenceRange[1][3], resultRange[1][3]); + + for (var c = 0; c < 4; c++) { + if (maxVal[c] - minVal[c] < eps) { + scale[c] = (maxVal[c] < eps) ? 1 : (1 / maxVal[c]); + bias[c] = (c == 3) ? (1 - maxVal[c] * scale[c]) : (0 - minVal[c] * scale[c]); + } else { + scale[c] = 1 / (maxVal[c] - minVal[c]); + bias[c] = 0 - minVal[c] * scale[c]; + } + } + return { + scale: scale, + bias: bias + }; +}; + +/** + * \brief Per-pixel threshold-based comparison + * + * This compare computes per-pixel differences between result and reference + * image. Comparison fails if any pixels exceed the given threshold value. + * + * This comparison can be used for integer- and fixed-point texture formats. + * Difference is computed in integer space. + * + * On failure error image is generated that shows where the failing pixels + * are. + * + * @param {string} imageSetName Name for image set when logging results + * @param {string} imageSetDesc Description for image set + * @param {tcuTexture.ConstPixelBufferAccess} reference Reference image + * @param {tcuTexture.ConstPixelBufferAccess} result Result image + * @param {Array<number>} threshold Maximum allowed difference + * @param {tcuImageCompare.CompareLogMode=} logMode + * @param {Array< Array<number> >} skipPixels pixels that are skipped comparison + * @return {boolean} true if comparison passes, false otherwise + */ + tcuImageCompare.intThresholdCompare = function(imageSetName, imageSetDesc, reference, result, threshold, logMode, skipPixels) { + var width = reference.getWidth(); + var height = reference.getHeight(); + var depth = reference.getDepth(); + var errorMask = new tcuSurface.Surface(width, height); + + var maxDiff = [0, 0, 0, 0]; + // var pixelBias = [0, 0, 0, 0]; // Vec4 // TODO: check, only used in computeScaleAndBias, which is not included + // var pixelScale = [1, 1, 1, 1]; // Vec4 // TODO: check, only used in computeScaleAndBias + + assertMsgOptions(result.getWidth() == width && result.getHeight() == height && result.getDepth() == depth, + 'Reference and result images have different dimensions', false, true); + + for (var z = 0; z < depth; z++) { + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + if (skipPixels && skipPixels.length > 0) { + var skip = false; + for (var ii = 0; ii < skipPixels.length; ++ii) { + var refZ = (skipPixels[ii].length > 2 ? skipPixels[ii][2] : 0); + if (x == skipPixels[ii][0] && y == skipPixels[ii][1] && z == refZ) { + skip = true; + break; + } + } + if (skip) + continue; + } + var refPix = reference.getPixelInt(x, y, z); + var cmpPix = result.getPixelInt(x, y, z); + + var diff = deMath.absDiff(refPix, cmpPix); + var isOk = deMath.boolAll(deMath.lessThanEqual(diff, threshold)); + + maxDiff = deMath.max(maxDiff, diff); + var color = [0, 255, 0, 255]; + if (!isOk) + color = [255, 0, 0, 255]; + errorMask.setPixel(x, y, color); + } + } + } + + var compareOk = deMath.boolAll(deMath.lessThanEqual(maxDiff, threshold)); + + if (!compareOk) { + debug('Image comparison failed: max difference = ' + maxDiff + ', threshold = ' + threshold); + tcuImageCompare.displayImages(result, reference, errorMask.getAccess()); + } + + return compareOk; +}; + +/** + * \brief Per-pixel threshold-based deviation-ignoring comparison + * + * This compare computes per-pixel differences between result and reference + * image. Pixel fails the test if there is no pixel matching the given + * threshold value in the search volume. Comparison fails if the number of + * failing pixels exceeds the given limit. + * + * If the search volume contains out-of-bounds pixels, comparison can be set + * to either ignore these pixels in search or to accept any pixel that has + * out-of-bounds pixels in its search volume. + * + * This comparison can be used for integer- and fixed-point texture formats. + * Difference is computed in integer space. + * + * On failure error image is generated that shows where the failing pixels + * are. + * + * @param {string} imageSetName Name for image set when logging results + * @param {string} imageSetDesc Description for image set + * @param {tcuTexture.ConstPixelBufferAccess} reference Reference image + * @param {tcuTexture.ConstPixelBufferAccess} result Result image + * @param {Array<number>} threshold Maximum allowed difference + * @param {Array<number>} maxPositionDeviation Maximum allowed distance in the search volume. + * @param {boolean} acceptOutOfBoundsAsAnyValue Accept any pixel in the boundary region + * @param {number} maxAllowedFailingPixels Maximum number of failing pixels + * @return {boolean} true if comparison passes, false otherwise + */ +tcuImageCompare.intThresholdPositionDeviationErrorThresholdCompare = function( + imageSetName, imageSetDesc, reference, result, threshold, maxPositionDeviation, acceptOutOfBoundsAsAnyValue, maxAllowedFailingPixels) { + /** @type {number} */ var width = reference.getWidth(); + /** @type {number} */ var height = reference.getHeight(); + /** @type {number} */ var depth = reference.getDepth(); + /** @type {tcuSurface.Surface} */ var errorMask = new tcuSurface.Surface(width, height); + /** @type {number} */ var numFailingPixels = tcuImageCompare.findNumPositionDeviationFailingPixels(errorMask.getAccess(), reference, result, threshold, maxPositionDeviation, acceptOutOfBoundsAsAnyValue); + var compareOk = numFailingPixels <= maxAllowedFailingPixels; + /** @type {Array<number>} */ var pixelBias = [0.0, 0.0, 0.0, 0.0]; + /** @type {Array<number>} */ var pixelScale = [1.0, 1.0, 1.0, 1.0]; + + if (!compareOk) { + debug('Position deviation error threshold image comparison failed: failed pixels = ' + numFailingPixels + ', threshold = ' + threshold); + tcuImageCompare.displayImages(result, reference, errorMask.getAccess()); + } else + tcuLogImage.logImage('Result', '', result); + + /*if (!compareOk) { + // All formats except normalized unsigned fixed point ones need remapping in order to fit into unorm channels in logged images. + if (tcuTexture.getTextureChannelClass(reference.getFormat().type) != tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT || + tcuTexture.getTextureChannelClass(result.getFormat().type) != tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT) { + computeScaleAndBias(reference, result, pixelScale, pixelBias); + log << TestLog::Message << "Result and reference images are normalized with formula p * " << pixelScale << " + " << pixelBias << TestLog::EndMessage; + } + + if (!compareOk) + log << TestLog::Message + << "Image comparison failed:\n" + << "\tallowed position deviation = " << maxPositionDeviation << "\n" + << "\tcolor threshold = " << threshold + << TestLog::EndMessage; + log << TestLog::Message << "Number of failing pixels = " << numFailingPixels << ", max allowed = " << maxAllowedFailingPixels << TestLog::EndMessage; + + log << TestLog::ImageSet(imageSetName, imageSetDesc) + << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + << TestLog::Image("Reference", "Reference", reference, pixelScale, pixelBias) + << TestLog::Image("ErrorMask", "Error mask", errorMask) + << TestLog::EndImageSet; + } else if (logMode == COMPARE_LOG_RESULT) { + if (result.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8)) + computePixelScaleBias(result, pixelScale, pixelBias); + + log << TestLog::ImageSet(imageSetName, imageSetDesc) + << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + << TestLog::EndImageSet; + }*/ + + return compareOk; +}; + +/** + * tcuImageCompare.floatUlpThresholdCompare + * @param {string} imageSetName + * @param {string} imageSetDesc + * @param {tcuTexture.ConstPixelBufferAccess} reference + * @param {tcuTexture.ConstPixelBufferAccess} result + * @param {Array<number>} threshold - previously used as an Uint32Array + * @return {boolean} + */ +tcuImageCompare.floatUlpThresholdCompare = function(imageSetName, imageSetDesc, reference, result, threshold) { + /** @type {number} */ var width = reference.getWidth(); + /** @type {number} */ var height = reference.getHeight(); + /** @type {number} */ var depth = reference.getDepth(); + /** @type {tcuSurface.Surface} */ var errorMask = new tcuSurface.Surface(width, height); + + /** @type {Array<number>} */ var maxDiff = [0, 0, 0, 0]; // UVec4 + // var pixelBias = [0, 0, 0, 0]; // Vec4 + // var pixelScale = [1, 1, 1, 1]; // Vec4 + + assertMsgOptions(result.getWidth() == width && result.getHeight() == height && result.getDepth() == depth, + 'Reference and result images have different dimensions', false, true); + + for (var z = 0; z < depth; z++) { + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + /** @type {ArrayBuffer} */ var arrayBufferRef = new ArrayBuffer(4 * 4); + /** @type {ArrayBuffer} */ var arrayBufferCmp = new ArrayBuffer(4 * 4); + + /** @type {Array<number>} */ var refPix = reference.getPixel(x, y, z); // getPixel returns a Vec4 pixel color + + /** @type {Array<number>} */ var cmpPix = result.getPixel(x, y, z); // getPixel returns a Vec4 pixel color + + /** @type {Uint32Array} */ var refBits = new Uint32Array(arrayBufferRef); // UVec4 + /** @type {Uint32Array} */ var cmpBits = new Uint32Array(arrayBufferCmp); // UVec4 + + // Instead of memcpy(), which is the way to do float->uint32 reinterpretation in C++ + for (var i = 0; i < refPix.length; i++) { + refBits[i] = tcuFloat.convertFloat32Inline(refPix[i], tcuFloat.description32); + cmpBits[i] = tcuFloat.convertFloat32Inline(cmpPix[i], tcuFloat.description32); + } + + /** @type {Array<number>} */ var diff = deMath.absDiff(refBits, cmpBits); // UVec4 + /** @type {boolean} */ var isOk = deMath.boolAll(deMath.lessThanEqual(diff, threshold)); + + maxDiff = deMath.max(maxDiff, diff); + + errorMask.setPixel(x, y, isOk ? [0, 255, 0, 255] : [255, 0, 0, 255]); + } + } + } + + /** @type {boolean} */ var compareOk = deMath.boolAll(deMath.lessThanEqual(maxDiff, threshold)); + + if (!compareOk) { + debug('Image comparison failed: max difference = ' + maxDiff + ', threshold = ' + threshold); + tcuImageCompare.displayImages(result, reference, errorMask.getAccess()); + } + + /*if (!compareOk || logMode == COMPARE_LOG_EVERYTHING) { + // All formats except normalized unsigned fixed point ones need remapping in order to fit into unorm channels in logged images. + if (tcu::getTextureChannelClass(reference.getFormat().type) != tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT || + tcu::getTextureChannelClass(result.getFormat().type) != tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT) { + computeScaleAndBias(reference, result, pixelScale, pixelBias); + log << TestLog::Message << "Result and reference images are normalized with formula p * " << pixelScale << " + " << pixelBias << TestLog::EndMessage; + } + + if (!compareOk) + log << TestLog::Message << "Image comparison failed: max difference = " << maxDiff << ", threshold = " << threshold << TestLog::EndMessage; + + log << TestLog::ImageSet(imageSetName, imageSetDesc) + << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + << TestLog::Image("Reference", "Reference", reference, pixelScale, pixelBias) + << TestLog::Image("ErrorMask", "Error mask", errorMask) + << TestLog::EndImageSet; + } else if (logMode == COMPARE_LOG_RESULT) { + if (result.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8)) + computePixelScaleBias(result, pixelScale, pixelBias); + + log << TestLog::ImageSet(imageSetName, imageSetDesc) + << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + << TestLog::EndImageSet; + }*/ + + return compareOk; +}; + +/** + * tcuImageCompare.floatThresholdCompare + * @param {string} imageSetName + * @param {string} imageSetDesc + * @param {tcuTexture.ConstPixelBufferAccess} reference + * @param {tcuTexture.ConstPixelBufferAccess} result + * @param {Array<number>} threshold + * @return {boolean} + */ +tcuImageCompare.floatThresholdCompare = function(imageSetName, imageSetDesc, reference, result, threshold) { + /** @type {number} */ var width = reference.getWidth(); + /** @type {number} */ var height = reference.getHeight(); + /** @type {number} */ var depth = reference.getDepth(); + /** @type {tcuSurface.Surface} */ var errorMask = new tcuSurface.Surface(width, height); + + /** @type {Array<number>} */ var maxDiff = [0, 0, 0, 0]; // Vec4 + // var pixelBias = [0, 0, 0, 0]; // Vec4 + // var pixelScale = [1, 1, 1, 1]; // Vec4 + + assertMsgOptions(result.getWidth() == width && result.getHeight() == height && result.getDepth() == depth, + 'Reference and result images have different dimensions', false, true); + + for (var z = 0; z < depth; z++) { + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + var refPix = reference.getPixel(x, y, z); // Vec4 + var cmpPix = result.getPixel(x, y, z); // Vec4 + + /** @type {Array<number>} */ var diff = deMath.absDiff(refPix, cmpPix); // Vec4 + /** @type {boolean} */ var isOk = deMath.boolAll(deMath.lessThanEqual(diff, threshold)); + + maxDiff = deMath.max(maxDiff, diff); + + errorMask.setPixel(x, y, isOk ? [0, 255, 0, 255] : [255, 0, 0, 255]); + } + } + } + + /** @type {boolean} */ var compareOk = deMath.boolAll(deMath.lessThanEqual(maxDiff, threshold)); + + if (!compareOk) { + debug('Image comparison failed: max difference = ' + maxDiff + ', threshold = ' + threshold); + tcuImageCompare.displayImages(result, reference, errorMask.getAccess()); + } + + /*if (!compareOk || logMode == COMPARE_LOG_EVERYTHING) { + // All formats except normalized unsigned fixed point ones need remapping in order to fit into unorm channels in logged images. + if (tcu::getTextureChannelClass(reference.getFormat().type) != tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT || + tcu::getTextureChannelClass(result.getFormat().type) != tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT) { + computeScaleAndBias(reference, result, pixelScale, pixelBias); + log << TestLog::Message << "Result and reference images are normalized with formula p * " << pixelScale << " + " << pixelBias << TestLog::EndMessage; + } + + if (!compareOk) + log << TestLog::Message << "Image comparison failed: max difference = " << maxDiff << ", threshold = " << threshold << TestLog::EndMessage; + + log << TestLog::ImageSet(imageSetName, imageSetDesc) + << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + << TestLog::Image("Reference", "Reference", reference, pixelScale, pixelBias) + << TestLog::Image("ErrorMask", "Error mask", errorMask) + << TestLog::EndImageSet; + } else if (logMode == COMPARE_LOG_RESULT) { + if (result.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8)) + computePixelScaleBias(result, pixelScale, pixelBias); + + log << TestLog::ImageSet(imageSetName, imageSetDesc) + << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + << TestLog::EndImageSet; + }*/ + + return compareOk; +}; + +/** + * \brief Per-pixel threshold-based comparison + * + * This compare computes per-pixel differences between result and reference + * image. Comparison fails if any pixels exceed the given threshold value. + * + * On failure error image is generated that shows where the failing pixels + * are. + * + * @param {string} imageSetName Name for image set when logging results + * @param {string} imageSetDesc Description for image set + * @param {tcuSurface.Surface} reference Reference image + * @param {tcuSurface.Surface} result Result image + * @param {Array<number>} threshold Maximum allowed difference + * @param {tcuImageCompare.CompareLogMode=} logMode + * @param {Array< Array<number> >} skipPixels pixels that are skipped comparison + * @return {boolean} true if comparison passes, false otherwise + */ +tcuImageCompare.pixelThresholdCompare = function(imageSetName, imageSetDesc, reference, result, threshold, logMode, skipPixels) { + return tcuImageCompare.intThresholdCompare(imageSetName, imageSetDesc, reference.getAccess(), result.getAccess(), threshold, logMode, skipPixels); +}; + +/** + * @param {tcuTexture.PixelBufferAccess} errorMask + * @param {tcuTexture.ConstPixelBufferAccess} reference + * @param {tcuTexture.ConstPixelBufferAccess} result + * @param {Array<number>} threshold + * @param {Array<number>} maxPositionDeviation + * @param {boolean} acceptOutOfBoundsAsAnyValue + * @return {number} + */ +tcuImageCompare.findNumPositionDeviationFailingPixels = function(errorMask, reference, result, threshold, maxPositionDeviation, acceptOutOfBoundsAsAnyValue) { + /** @type {number} */ var width = reference.getWidth(); + /** @type {number} */ var height = reference.getHeight(); + /** @type {number} */ var depth = reference.getDepth(); + /** @type {number} */ var numFailingPixels = 0; + + checkMessage(result.getWidth() == width && result.getHeight() == height && result.getDepth() == depth, 'Surfaces have different dimensions'); + + for (var z = 0; z < depth; z++) { + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + /** @type {Array<number>} */ var refPix = reference.getPixelInt(x, y, z); + /** @type {Array<number>} */ var cmpPix = result.getPixelInt(x, y, z); + + // Exact match + /** @type {Array<number>} */ var diff = deMath.absDiff(refPix, cmpPix); + /** @type {boolean} */ var isOk = deMath.boolAll(deMath.lessThanEqual(diff, threshold)); + + if (isOk) { + errorMask.setPixel([0, 0xff, 0, 0xff], x, y, z); + continue; + } + + // Accept over the image bounds pixels since they could be anything + + if (acceptOutOfBoundsAsAnyValue && + (x < maxPositionDeviation[0] || x + maxPositionDeviation[0] >= width || + y < maxPositionDeviation[1] || y + maxPositionDeviation[1] >= height || + z < maxPositionDeviation[2] || z + maxPositionDeviation[2] >= depth)) { + errorMask.setPixel([0, 0xff, 0, 0xff], x, y, z); + continue; + } + + // Find matching pixels for both result and reference pixel + + var pixelFoundForReference = false; + var pixelFoundForResult = false; + + // Find deviated result pixel for reference + + for (var sz = Math.max(0, z - maxPositionDeviation[2]); sz <= Math.min(depth - 1, z + maxPositionDeviation[2]) && !pixelFoundForReference; ++sz) + for (var sy = Math.max(0, y - maxPositionDeviation[1]); sy <= Math.min(height - 1, y + maxPositionDeviation[1]) && !pixelFoundForReference; ++sy) + for (var sx = Math.max(0, x - maxPositionDeviation[0]); sx <= Math.min(width - 1, x + maxPositionDeviation[0]) && !pixelFoundForReference; ++sx) { + /** @type {Array<number>} */ var deviatedCmpPix = result.getPixelInt(sx, sy, sz); + diff = deMath.absDiff(refPix, deviatedCmpPix); + isOk = deMath.boolAll(deMath.lessThanEqual(diff, threshold)); + + pixelFoundForReference |= isOk; + } + + // Find deviated reference pixel for result + + for (var sz = Math.max(0, z - maxPositionDeviation[2]); sz <= Math.min(depth - 1, z + maxPositionDeviation[2]) && !pixelFoundForResult; ++sz) + for (var sy = Math.max(0, y - maxPositionDeviation[1]); sy <= Math.min(height - 1, y + maxPositionDeviation[1]) && !pixelFoundForResult; ++sy) + for (var sx = Math.max(0, x - maxPositionDeviation[0]); sx <= Math.min(width - 1, x + maxPositionDeviation[0]) && !pixelFoundForResult; ++sx) { + /** @type {Array<number>} */ var deviatedRefPix = reference.getPixelInt(sx, sy, sz); + diff = deMath.absDiff(cmpPix, deviatedRefPix); + isOk = deMath.boolAll(deMath.lessThanEqual(diff, threshold)); + + pixelFoundForResult |= isOk; + } + + if (pixelFoundForReference && pixelFoundForResult) + errorMask.setPixel([0, 0xff, 0, 0xff], x, y, z); + else { + errorMask.setPixel([0xff, 0, 0, 0xff], x, y, z); + ++numFailingPixels; + } + } + } + } + + return numFailingPixels; +}; + + /** + * tcuImageCompare.fuzzyCompare + * @param {string} imageSetName + * @param {string} imageSetDesc + * @param {tcuTexture.ConstPixelBufferAccess} reference + * @param {tcuTexture.ConstPixelBufferAccess} result + * @param {number} threshold + * @param {tcuImageCompare.CompareLogMode=} logMode + * @return {boolean} + */ +tcuImageCompare.fuzzyCompare = function(imageSetName, imageSetDesc, reference, result, threshold, logMode) { + /** @type {tcuFuzzyImageCompare.FuzzyCompareParams} */ var params = new tcuFuzzyImageCompare.FuzzyCompareParams(); // Use defaults. + /** @type {tcuTexture.TextureLevel} */ var errorMask = new tcuTexture.TextureLevel( + new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGB, + tcuTexture.ChannelType.UNORM_INT8), + reference.getWidth(), + reference.getHeight() + ); + /** @type {number} */ var difference = tcuFuzzyImageCompare.fuzzyCompare( + params, + reference, + result, + tcuTexture.PixelBufferAccess.newFromTextureLevel(errorMask) + ); + /** @type {boolean} */ var isOk = difference <= threshold; + /** @type {Array<number>} */ var pixelBias = [0.0, 0.0, 0.0, 0.0]; + /** @type {Array<number>} */ var pixelScale = [1.0, 1.0, 1.0, 1.0]; + + if (!isOk) { + debug('Fuzzy image comparison failed: difference = ' + difference + ', threshold = ' + threshold); + tcuImageCompare.displayImages(result, reference, errorMask.getAccess()); + } + + /* + if (!isOk || logMode == COMPARE_LOG_EVERYTHING) { + // Generate more accurate error mask. + params.maxSampleSkip = 0; + tcuImageCompare.fuzzyCompare(params, reference, result, errorMask.getAccess()); + + if (result.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8) && reference.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8)) + computeScaleAndBias(reference, result, pixelScale, pixelBias); + + if (!isOk) + log << TestLog::Message << "Image comparison failed: difference = " << difference << ", threshold = " << threshold << TestLog::EndMessage; + + log << TestLog::ImageSet(imageSetName, imageSetDesc) + << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + << TestLog::Image("Reference", "Reference", reference, pixelScale, pixelBias) + << TestLog::Image("ErrorMask", "Error mask", errorMask) + << TestLog::EndImageSet; + } else if (logMode == COMPARE_LOG_RESULT) { + if (result.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8)) + computePixelScaleBias(result, pixelScale, pixelBias); + + log << TestLog::ImageSet(imageSetName, imageSetDesc) + << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + << TestLog::EndImageSet; + } + */ + return isOk; +}; + +tcuImageCompare.unitTest = function() { + var width = 128; + var height = 128; + + var weirdLevel = new tcuTexture.TextureLevel(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RG, tcuTexture.ChannelType.SNORM_INT32), width, height); + var access = weirdLevel.getAccess(); + access.clear([0.1, 0.5, 0, 0]); + access.clear([0.11, 0.52, 0, 0], [0, width], [0, height / 2]); + access.clear([0.12, 0.52, 0, 0], [0, width], [height / 2, height / 2 + height / 8]); + var limits = tcuTextureUtil.computePixelScaleBias(access); + debug('Scale: ' + limits.scale); + debug('Bias: ' + limits.bias); + tcuLogImage.logImage('Weird', 'weird format without scaling', access); + tcuLogImage.logImage('Weird', 'weird format', access, limits.scale, limits.bias); + + var srcLevel = new tcuTexture.TextureLevel(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8), width, height); + var dstLevel = new tcuTexture.TextureLevel(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8), width, height); + var src = srcLevel.getAccess(); + var dst = dstLevel.getAccess(); + + src.clear(); + dst.clear(); + + for (var i = 0; i < width - 1; i++) { + for (var j = 0; j < height - 1; j++) { + src.setPixelInt([i, j, 90, 255], i, j); + dst.setPixelInt([i, j, 90, 255], i + 1, j + 1); + } + } + + debug('Src format: ' + src.getFormat()); + debug('Destination: ' + dst); + debug(src); + tcuLogImage.logImage('Source', 'Source image', src); + + if (!tcuImageCompare.fuzzyCompare('compare', 'compare similar images', src, dst, 0.05)) + throw new Error('Compare should return true'); + + src.clear(); + dst.clear(); + + for (var i = 0; i < width - 2; i++) { + for (var j = 0; j < height - 2; j++) { + src.setPixelInt([i, j, 90, 255], i, j); + dst.setPixelInt([i, j, 90, 255], i + 2, j + 2); + } + } + + if (tcuImageCompare.fuzzyCompare('compare', 'compare different images', src, dst, 0.05)) + throw new Error('Compare should return false'); + + debug('Passed'); +}; + +tcuImageCompare.unitTest2 = function() { + var width = 128; + var height = 128; + var srcLevel = new tcuTexture.TextureLevel(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8), width, height); + var dstLevel = new tcuTexture.TextureLevel(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8), width, height); + var src = srcLevel.getAccess(); + var dst = dstLevel.getAccess(); + var threshold = tcuRGBA.newRGBAComponents(1, 1, 1, 1); + debug('Threshold: ' + threshold); + + src.clear(); + dst.clear(); + + for (var i = 0; i < width - 1; i++) { + for (var j = 0; j < height - 1; j++) { + src.setPixelInt([i, j, 90, 255], i, j); + dst.setPixelInt([i, j, 90, 255], i, j); + } + } + if (!tcuImageCompare.bilinearCompare('compare', 'compare similar images', src, dst, threshold)) + throw new Error('Compare should return true'); + debug('bilinear compare the same images passed'); + + src.clear(); + dst.clear(); + + for (var i = 0; i < width - 1; i++) { + for (var j = 0; j < height - 1; j++) { + src.setPixelInt([i, j, 90, 255], i, j); + dst.setPixelInt([i, j + 1, 90, 255], i, j + 1); + } + } + if (!tcuImageCompare.bilinearCompare('compare', 'compare similar images', src, dst, threshold)) + throw new Error('Compare should return true'); + debug('bilinear compare very similar images passed'); + + src.clear(); + dst.clear(); + + for (var i = 0; i < width - 2; i++) { + for (var j = 0; j < height - 2; j++) { + src.setPixelInt([i, j, 90, 255], i, j); + // dst.setPixelInt([i, j, 90, 255], i + 2, j + 2); + } + } + + if (tcuImageCompare.bilinearCompare('compare', 'compare different images', src, dst, threshold)) + throw new Error('Compare should return false'); + + debug('bilinear compare very different images passed'); +}; + +/** + * Bilinear image comparison + * On failure error image is generated that shows where the failing pixels + * are. + * Currently supports only RGBA, UNORM_INT8 formats + * + * @param {string} imageSetName Name for image set when logging results + * @param {string} imageSetDesc Description for image set + * @param {tcuTexture.ConstPixelBufferAccess} reference Reference image + * @param {tcuTexture.ConstPixelBufferAccess} result Result image + * @param {tcuRGBA.RGBA} threshold Maximum local difference + * @param {tcuImageCompare.CompareLogMode=} logMode Logging mode + * @return {boolean} if comparison passes, false otherwise + */ +tcuImageCompare.bilinearCompare = function(imageSetName, imageSetDesc, reference, result, threshold, logMode) { + /** @type {tcuTexture.TextureLevel} */ + var errorMask = new tcuTexture.TextureLevel( + new tcuTexture.TextureFormat( + tcuTexture.ChannelOrder.RGB, + tcuTexture.ChannelType.UNORM_INT8), + reference.getWidth(), + reference.getHeight()); + + /** @type {boolean} */ + var isOk = tcuBilinearImageCompare.bilinearCompare( + reference, + result, + tcuTexture.PixelBufferAccess.newFromTextureLevel(errorMask), + threshold); + + if (!isOk) { + debug('Image comparison failed: threshold = ' + threshold); + tcuImageCompare.displayImages(result, reference, errorMask.getAccess()); + } + + // /* @type {Array<number>} */ var pixelBias = [0.0, 0.0, 0.0, 0.0]; + // /* @type {Array<number>} */ var pixelScale = [1.0, 1.0, 1.0, 1.0]; + // if (!isOk || logMode == COMPARE_LOG_EVERYTHING) + // { + // if (result.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8) && reference.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8)) + // computeScaleAndBias(reference, result, pixelScale, pixelBias); + // + // if (!isOk) + // log << TestLog::Message << "Image comparison failed, threshold = " << threshold << TestLog::EndMessage; + // + // log << TestLog::ImageSet(imageSetName, imageSetDesc) + // << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + // << TestLog::Image("Reference", "Reference", reference, pixelScale, pixelBias) + // << TestLog::Image("ErrorMask", "Error mask", errorMask) + // << TestLog::EndImageSet; + // } + // else if (logMode == COMPARE_LOG_RESULT) + // { + // if (result.getFormat() != TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8)) + // computePixelScaleBias(result, pixelScale, pixelBias); + // + // log << TestLog::ImageSet(imageSetName, imageSetDesc) + // << TestLog::Image("Result", "Result", result, pixelScale, pixelBias) + // << TestLog::EndImageSet; + // } + + return isOk; +}; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuInterval.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuInterval.js new file mode 100644 index 000000000..23296c1f3 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuInterval.js @@ -0,0 +1,609 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program Tester Core + * ---------------------------------------- + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *//*! + * \file + * \brief Interval arithmetic and floating point precisions. + *//*--------------------------------------------------------------------*/ + 'use strict'; + goog.provide('framework.common.tcuInterval'); + goog.require('framework.delibs.debase.deMath'); + + goog.scope(function() { + + var tcuInterval = framework.common.tcuInterval; + var deMath = framework.delibs.debase.deMath; + + /** + * @typedef {function(number):number} + */ + tcuInterval.DoubleFunc1; + + /** + * @typedef {function(number, number):number} + */ + tcuInterval.DoubleFunc2; + + /** + * @typedef {function(number,number,number):number} + */ + tcuInterval.DoubleFunc3; + + /** + * @typedef {function(number):tcuInterval.Interval} + */ + tcuInterval.DoubleIntervalFunc1; + + /** + * @typedef {function(number,number):tcuInterval.Interval} + */ + tcuInterval.DoubleIntervalFunc2; + + /** + * @typedef {function(number,number,number):tcuInterval.Interval} + */ + tcuInterval.DoubleIntervalFunc3; + + /** + * @param {function(number): number} func + * @param {tcuInterval.Interval} arg0 + * @return {tcuInterval.Interval} + */ + tcuInterval.applyMonotone1p = function(func, arg0) { + /** + * @param {number=} x + * @param {number=} y + * @return {number} + */ + var body = function(x, y) { + x = x || 0; + return func(x); + }; + return tcuInterval.applyMonotone1(arg0, + function(x) { return tcuInterval.setInterval(body, x); }); + }; + + /** + * @param {function(number): tcuInterval.Interval} func + * @param {tcuInterval.Interval} arg0 + * @return {tcuInterval.Interval} + */ + tcuInterval.applyMonotone1i = function(func, arg0) { + return tcuInterval.withIntervals(func(arg0.lo()), func(arg0.hi())); + }; + + /** + * @param {function(number, number): number} func + * @param {tcuInterval.Interval} arg0 + * @param {tcuInterval.Interval} arg1 + * @return {tcuInterval.Interval} + */ + tcuInterval.applyMonotone2p = function(func, arg0, arg1) { + /** + * @param {number=} x + * @param {number=} y + * @return {number} + */ + var body = function(x, y) { + x = x || 0; + y = y || 0; + return func(x, y); + }; + return tcuInterval.applyMonotone2(arg0, arg1, + function(x, y) { return tcuInterval.setInterval(body, x, y); }); + }; + + /** + * @param {function(number, number): tcuInterval.Interval} func + * @param {tcuInterval.Interval} arg0 + * @param {tcuInterval.Interval} arg1 + * @return {tcuInterval.Interval} + */ + tcuInterval.applyMonotone2i = function(func, arg0, arg1) { + /** @type {number} */ var lo0 = arg0.lo(); + /** @type {number} */ var hi0 = arg0.hi(); + /** @type {number} */ var lo1 = arg1.lo(); + /** @type {number} */ var hi1 = arg1.hi(); + var a = tcuInterval.withIntervals(func(lo0, lo1), func(lo0, hi1)); + var b = tcuInterval.withIntervals(func(hi0, lo1), func(hi0, hi1)); + return tcuInterval.withIntervals(a, b); + }; + + /** + * @constructor + * @param {number=} val + */ + tcuInterval.Interval = function(val) { + if (val === undefined) { + this.m_hasNaN = false; + this.m_lo = Number.POSITIVE_INFINITY; + this.m_hi = Number.NEGATIVE_INFINITY; + } else { + this.m_hasNaN = isNaN(val); + this.m_lo = this.m_hasNaN ? Number.POSITIVE_INFINITY : val; + this.m_hi = this.m_hasNaN ? Number.NEGATIVE_INFINITY : val; + } + }; + + tcuInterval.Interval.prototype.toString = function() { + var str = 'Interval(' + this.m_lo + ', ' + this.m_hi; + if (this.m_hasNaN) + str += ', hasNaN'; + str += ')'; + return str; + }; + + /** + * @param {tcuInterval.Interval} a + * @param {tcuInterval.Interval} b + * @return {tcuInterval.Interval} + */ + tcuInterval.withIntervals = function(a, b) { + /** @type {tcuInterval.Interval} */ var interval = new tcuInterval.Interval(); + interval.m_hasNaN = (a.m_hasNaN || b.m_hasNaN); + interval.m_lo = Math.min(a.m_lo, b.m_lo); + interval.m_hi = Math.max(a.m_hi, b.m_hi); + return interval; + }; + + /** + * @param {number} a + * @param {number} b + * @return {tcuInterval.Interval} + */ + tcuInterval.withNumbers = function(a, b) { + var x = new tcuInterval.Interval(a); + var y = new tcuInterval.Interval(b); + return tcuInterval.withIntervals(x, y); + }; + + /** + * @param {boolean} hasNaN_ + * @param {number} lo_ + * @param {number} hi_ + * @return {tcuInterval.Interval} + */ + tcuInterval.withParams = function(hasNaN_, lo_, hi_) { + /** @type {tcuInterval.Interval} */ var interval = new tcuInterval.Interval(); + interval.m_hasNaN = hasNaN_; + interval.m_lo = lo_; + interval.m_hi = hi_; + return interval; + }; + + /** + * @return {number} + */ + tcuInterval.Interval.prototype.length = function() { + return this.m_hi - this.m_lo; + }; + + /** + * @return {number} + */ + tcuInterval.Interval.prototype.lo = function() { + return this.m_lo; + }; + + /** + * @return {number} + */ + tcuInterval.Interval.prototype.hi = function() { + return this.m_hi; + }; + + /** + * @return {boolean} + */ + tcuInterval.Interval.prototype.hasNaN = function() { + return this.m_hasNaN; + }; + + /** + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.prototype.nan = function() { + return this.m_hasNaN ? new tcuInterval.Interval(NaN) : new tcuInterval.Interval(); + }; + + /** + * @return {boolean} + */ + tcuInterval.Interval.prototype.empty = function() { + return this.m_lo > this.m_hi; + }; + + /** + * @return {boolean} + */ + tcuInterval.Interval.prototype.isFinite = function() { + return isFinite(this.m_lo) && isFinite(this.m_hi); + }; + + /** + * @return {boolean} + */ + tcuInterval.Interval.prototype.isOrdinary = function() { + return !this.hasNaN() && !this.empty() && this.isFinite(); + }; + + /** + * @param {tcuInterval.Interval} other + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.prototype.operatorOrBinary = function(other) { + /** @type {tcuInterval.Interval} */ var temp = new tcuInterval.Interval(); + temp.m_hasNaN = this.m_hasNaN || other.m_hasNaN; + temp.m_lo = Math.min(this.m_lo, other.m_lo); + temp.m_hi = Math.max(this.m_hi, other.m_hi); + return temp; + }; + + /** + * @param {tcuInterval.Interval} other + */ + tcuInterval.Interval.prototype.operatorOrAssignBinary = function(other) { + /** @type {tcuInterval.Interval} */ var temp = this.operatorOrBinary(other); + this.m_hasNaN = temp.m_hasNaN; + this.m_lo = temp.m_lo; + this.m_hi = temp.m_hi; + }; + + /** + * @param {tcuInterval.Interval} other + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.prototype.operatorAndBinary = function(other) { + /** @type {tcuInterval.Interval} */ var temp = new tcuInterval.Interval(); + temp.m_hasNaN = this.m_hasNaN && other.m_hasNaN; + temp.m_lo = Math.max(this.m_lo, other.m_lo); + temp.m_hi = Math.min(this.m_hi, other.m_hi); + return temp; + }; + + /** + * @param {tcuInterval.Interval} other + */ + tcuInterval.Interval.prototype.operatorAndAssignBinary = function(other) { + /** @type {tcuInterval.Interval} */ var temp = this.operatorAndBinary(other); + this.m_hasNaN = temp.m_hasNaN; + this.m_lo = temp.m_lo; + this.m_hi = temp.m_hi; + }; + + /** + * @param {tcuInterval.Interval} other + * @return {boolean} + */ + tcuInterval.Interval.prototype.contains = function(other) { + return (other.lo() >= this.lo() && other.hi() <= this.hi() && + (!other.hasNaN() || this.hasNaN())); + }; + + /** + * @param {tcuInterval.Interval} other + * @return {boolean} + */ + tcuInterval.Interval.prototype.intersects = function(other) { + return ((other.hi() >= this.lo() && other.lo() >= this.hi()) || + (other.hasNaN() && this.hasNaN())); + }; + + /** + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.prototype.operatorNegative = function() { + /** @type {tcuInterval.Interval} */ var temp = new tcuInterval.Interval(); + temp.m_hasNaN = this.m_hasNaN; + temp.m_lo = -this.m_hi; + temp.m_hi = -this.m_lo; + return temp; + }; + + /** + * @param {boolean=} nan + * @return {tcuInterval.Interval} + */ + tcuInterval.unbounded = function(nan) { + if (nan === undefined) + nan = false; + return tcuInterval.withParams(nan, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY); + }; + + /** + * @return {number} + */ + tcuInterval.Interval.prototype.midpoint = function() { + return 0.5 * (this.hi() + this.lo()); // returns NaN when not bounded + }; + + /** + * @param {tcuInterval.Interval} other + * @return {boolean} + */ + tcuInterval.Interval.prototype.operatorCompare = function(other) { + return ((this.m_hasNaN == other.m_hasNaN) && + ((this.empty() && other.empty()) || + (this.m_lo == other.m_lo && this.m_hi == other.m_hi))); + }; + + /** + * @param {tcuInterval.Interval} x + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.operatorPositive = function(x) { + return x; + }; + + /** + * @param {tcuInterval.Interval} x + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.exp2 = function(x) { + // std::pow + return tcuInterval.applyMonotone2p(Math.pow, new tcuInterval.Interval(2), x); + }; + + /** + * @param {tcuInterval.Interval} x + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.exp = function(x) { + // std::exp + return tcuInterval.applyMonotone1p(Math.exp, x); + }; + + /** + * @param {tcuInterval.Interval} x + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.sign = function(x) { + // TODO + throw new Error('Unimplemented'); + }; + + /** + * @param {tcuInterval.Interval} x + * @param {tcuInterval.Interval} y + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.operatorSum = function(x, y) { + /** @type {tcuInterval.Interval} */ var ret = new tcuInterval.Interval(); + + if (!x.empty() && !y.empty()) + ret = tcuInterval.setIntervalBounds(function(dummy) {return x.lo() + y.lo();}, function(dummy) {return x.hi() + y.hi();}); + if (x.hasNaN() || y.hasNaN()) + ret.operatorOrAssignBinary(new tcuInterval.Interval(NaN)); + + return ret; + }; + + /** + * @param {tcuInterval.Interval} x + * @param {tcuInterval.Interval} y + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.operatorSub = function(x, y) { + /** @type {tcuInterval.Interval} */ var ret = new tcuInterval.Interval(); + + /** + * @param {number=} x + * @param {number=} y + * @return {tcuInterval.Interval} + */ + var body = function(x, y) { + return new tcuInterval.Interval(x - y); + }; + + ret = tcuInterval.applyMonotone2(x, y, body); + return ret; + }; + + /** + * @param {tcuInterval.Interval} x + * @param {tcuInterval.Interval} y + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.operatorMul = function(x, y) { + /** @type {tcuInterval.Interval} */ var ret = new tcuInterval.Interval(); + + /** + * @param {number=} x + * @param {number=} y + * @return {tcuInterval.Interval} + */ + var body = function(x, y) { + return new tcuInterval.Interval(x * y); + }; + + ret = tcuInterval.applyMonotone2(x, y, body); + + return ret; + }; + + /** + * @param {tcuInterval.Interval} nom + * @param {tcuInterval.Interval} den + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.operatorDiv = function(nom, den) { + if (den.contains(new tcuInterval.Interval(0))) { + // \todo [2014-03-21 lauri] Non-inf endpoint when one den endpoint is + // zero and nom doesn't cross zero? + return tcuInterval.unbounded(); + } else { + /** @type {tcuInterval.Interval} */ var ret = new tcuInterval.Interval(); + /** + * @param {number=} x + * @param {number=} y + * @return {tcuInterval.Interval} + */ + var body = function(x, y) { + return new tcuInterval.Interval(x / y); + }; + + ret = tcuInterval.applyMonotone2(nom, den, body); + + return ret; + } + }; + + /** + * @param {tcuInterval.Interval} x + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.prototype.abs = function(x) { + //std::abs + /** @type {tcuInterval.Interval} */ var mono = tcuInterval.applyMonotone1p(Math.abs, x); + var zero = new tcuInterval.Interval(0); + if (x.contains(zero)) + return tcuInterval.withIntervals(zero, mono); + + return mono; + }; + + /** + * @param {tcuInterval.Interval} x + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.sqrt = function(x) { + return tcuInterval.applyMonotone1p(Math.sqrt, x); + }; + + /** + * @param {tcuInterval.Interval} x + * @return {tcuInterval.Interval} + */ + tcuInterval.Interval.inverseSqrt = function(x) { + var ret = new tcuInterval.Interval(1); + ret = tcuInterval.Interval.operatorDiv(ret, tcuInterval.Interval.sqrt(x)); + return ret; + }; + +/** + * @param {function(number=, number=): number} setLow + * @param {function(number=, number=): number} setHigh + * @param {number=} arg0 + * @param {number=} arg1 + * @return {tcuInterval.Interval} + */ +tcuInterval.setIntervalBounds = function(setLow, setHigh, arg0, arg1) { + // TODO: No support for rounding modes. Originally, setLow() was rounded down and setHigh() rounded up + var lo = new tcuInterval.Interval(setLow(arg0, arg1)); + var hi = new tcuInterval.Interval(setHigh(arg0, arg1)); + return lo.operatorOrBinary(hi); +}; + +/** + * @param {function(number=, number=): number} set + * @param {number=} arg0 + * @param {number=} arg1 + * @return {tcuInterval.Interval} + */ +tcuInterval.setInterval = function(set, arg0, arg1) { + return tcuInterval.setIntervalBounds(set, set, arg0, arg1); +}; + +/** + * @param {tcuInterval.Interval} arg + * @param {function(number): tcuInterval.Interval} body + * @return {tcuInterval.Interval} + */ +tcuInterval.applyMonotone1 = function(arg, body) { + var ret = new tcuInterval.Interval(); + + if (!arg.empty()) { + var lo = body(arg.lo()); + var hi = body(arg.hi()); + ret = lo.operatorOrBinary(hi); + } + + if (arg.hasNaN()) { + ret = ret.operatorOrBinary(new tcuInterval.Interval(NaN)); + } + + return ret; +}; + +/** + * TODO: Check if this function works properly + * @param {tcuInterval.Interval} arg0 + * @param {tcuInterval.Interval} arg1 + * @param {function(number, number): tcuInterval.Interval} body + * @return {tcuInterval.Interval} + */ +tcuInterval.applyMonotone2 = function(arg0, arg1, body) { + var ret = new tcuInterval.Interval(); + + if (!arg0.empty() && !arg1.empty()) { + var lo0 = body(arg0.lo(), arg1.lo()); + var lo1 = body(arg0.lo(), arg1.hi()); + var hi0 = body(arg0.hi(), arg1.lo()); + var hi1 = body(arg0.hi(), arg1.hi()); + var a = lo0.operatorOrBinary(hi0); + var b = lo1.operatorOrBinary(hi1); + ret = a.operatorOrBinary(b); + } + + if (arg0.hasNaN() || arg1.hasNaN()) { + ret = ret.operatorOrBinary(new tcuInterval.Interval(NaN)); + } + + return ret; +}; + +/** + * TODO: Check if this function works properly + * @param {tcuInterval.Interval} arg0 + * @param {tcuInterval.Interval} arg1 + * @param {tcuInterval.Interval} arg2 + * @param {function(number, number, number): tcuInterval.Interval} body + * @return {tcuInterval.Interval} + */ +tcuInterval.applyMonotone3 = function(arg0, arg1, arg2, body) { + var ret = new tcuInterval.Interval(); + + if (!arg0.empty() && !arg1.empty() && !arg2.empty()) { + var i0 = body(arg0.lo(), arg1.lo(), arg2.lo()); + var i1 = body(arg0.lo(), arg1.lo(), arg2.hi()); + var i2 = body(arg0.lo(), arg1.hi(), arg2.lo()); + var i3 = body(arg0.lo(), arg1.hi(), arg2.hi()); + var i4 = body(arg0.hi(), arg1.lo(), arg2.lo()); + var i5 = body(arg0.hi(), arg1.lo(), arg2.hi()); + var i6 = body(arg0.hi(), arg1.hi(), arg2.lo()); + var i7 = body(arg0.hi(), arg1.hi(), arg2.hi()); + + var low = Math.min(i0.lo(), i1.lo(), i2.lo(), i3.lo(), i4.lo(), i5.lo(), i6.lo(), i7.lo()); + var high = Math.max(i0.hi(), i1.hi(), i2.hi(), i3.hi(), i4.hi(), i5.hi(), i6.hi(), i7.hi()); + var hasNaN = i0.hasNaN() || i1.hasNaN() || i2.hasNaN() || i3.hasNaN() || i4.hasNaN() || i5.hasNaN() || i6.hasNaN() || i7.hasNaN(); + + ret = tcuInterval.withParams(hasNaN, low, high); + } + + if (arg0.hasNaN() || arg1.hasNaN() || arg2.hasNaN()) { + ret = ret.operatorOrBinary(new tcuInterval.Interval(NaN)); + } + + return ret; +}; + +/** @const */ tcuInterval.POSITIVE_INFINITY = new tcuInterval.Interval(Infinity); +/** @const */ tcuInterval.NEGATIVE_INFINITY = new tcuInterval.Interval(-Infinity); +/** @const */ tcuInterval.ZERO = new tcuInterval.Interval(0); +/** @const */ tcuInterval.NAN = new tcuInterval.Interval(NaN); +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuLogImage.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuLogImage.js new file mode 100644 index 000000000..2dabc9060 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuLogImage.js @@ -0,0 +1,163 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuLogImage'); +goog.require('framework.common.tcuSurface'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + +var tcuLogImage = framework.common.tcuLogImage; +var tcuTexture = framework.common.tcuTexture; +var tcuSurface = framework.common.tcuSurface; +var deMath = framework.delibs.debase.deMath; + +/** @const */ var MAX_IMAGE_SIZE_2D = 4096; +/** + * @param {tcuTexture.ConstPixelBufferAccess} src + */ +tcuLogImage.createImage = function(ctx, src) { + var w = src.getWidth(); + var h = src.getHeight(); + var pixelSize = src.getFormat().getPixelSize(); + var imgData = ctx.createImageData(w, h); + var index = 0; + for (var y = 0; y < h; y++) { + for (var x = 0; x < w; x++) { + var pixel = src.getPixelInt(x, h - y - 1, 0); + for (var i = 0; i < pixelSize; i++) { + imgData.data[index] = pixel[i]; + index = index + 1; + } + if (pixelSize < 4) + imgData.data[index++] = 255; + } + } + return imgData; +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} image + * @param {string} info + */ +tcuLogImage.logImageWithInfo = function(image, info) { + var elem = document.getElementById('console'); + var span = document.createElement('span'); + tcuLogImage.logImage.counter = tcuLogImage.logImage.counter || 0; + var i = tcuLogImage.logImage.counter++; + var width = image.getWidth(); + var height = image.getHeight(); + + elem.appendChild(span); + span.innerHTML = info + '<br> <canvas id="logImage' + i + '" width=' + width + ' height=' + height + '></canvas><br>'; + + var imageCanvas = document.getElementById('logImage' + i); + var ctx = imageCanvas.getContext('2d'); + var data = tcuLogImage.createImage(ctx, image); + ctx.putImageData(data, 0, 0); +}; + + +/** + * @param {Array<number>=} scale + * @param {Array<number>=} bias + * @return {string} HTML string to add to log. + */ +tcuLogImage.logScaleAndBias = function(scale, bias) { + if (scale && bias) + return '<br> Image normalized with formula p * (' + scale + ') + (' + bias + ')'; + else if (scale) + return '<br> Image normalized with formula p * (' + scale + ')'; + else if (bias) + return '<br> Image normalized with formula p + (' + bias + ')'; + return ''; +}; + +/** + * @param {string} name + * @param {string} description + * @param {tcuTexture.ConstPixelBufferAccess} image + * @param {Array<number>=} scale + * @param {Array<number>=} bias + */ +tcuLogImage.logImageRGB = function(name, description, image, scale, bias) { + var elem = document.getElementById('console'); + var span = document.createElement('span'); + var info = name + ' ' + description + '<br> ' + image; + if (scale || bias) + info += tcuLogImage.logScaleAndBias(scale, bias); + tcuLogImage.logImageWithInfo(image, info); +}; + +/** + * @param {string} name + * @param {string} description + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {Array<number>=} pixelScale + * @param {Array<number>=} pixelBias + */ +tcuLogImage.logImage = function(name, description, access, pixelScale, pixelBias) { + pixelScale = pixelScale || [1, 1, 1, 1]; + pixelBias = pixelBias || [0, 0, 0, 0]; + var format = access.getFormat(); + var width = access.getWidth(); + var height = access.getHeight(); + var depth = access.getDepth(); + var needScaling = pixelBias[0] != 0 || pixelBias[1] != 0 || pixelBias[2] != 0 || pixelBias[3] != 0 || + pixelScale[0] != 1 || pixelScale[1] != 1 || pixelScale[2] != 1 || pixelScale[3] != 1; + + if (depth == 1 && format.type == tcuTexture.ChannelType.UNORM_INT8 && + width <= MAX_IMAGE_SIZE_2D && height <= MAX_IMAGE_SIZE_2D && + (format.order == tcuTexture.ChannelOrder.RGB || tcuTexture.ChannelOrder.RGBA) && + !needScaling) + // Fast-path. + tcuLogImage.logImageRGB(name, description, access); + else if (depth == 1) { + var sampler = new tcuTexture.Sampler(tcuTexture.WrapMode.CLAMP_TO_EDGE, tcuTexture.WrapMode.CLAMP_TO_EDGE, tcuTexture.WrapMode.CLAMP_TO_EDGE, + tcuTexture.FilterMode.LINEAR, tcuTexture.FilterMode.NEAREST); + var logImageSize = [width, height]; /* TODO: Add scaling */ + var logImageAccess = new tcuSurface.Surface(width, height).getAccess(); + + for (var y = 0; y < logImageAccess.getHeight(); y++) { + for (var x = 0; x < logImageAccess.getWidth(); x++) { + var yf = (y + 0.5) / logImageAccess.getHeight(); + var xf = (x + 0.5) / logImageAccess.getWidth(); + var s = access.sample2D(sampler, sampler.minFilter, xf, yf, 0); + + if (needScaling) + s = deMath.add(deMath.multiply(s, pixelScale), pixelBias); + + logImageAccess.setPixel(s, x, y); + } + } + var info = name + ' ' + description + '<br> ' + access; + if (needScaling) { + info += tcuLogImage.logScaleAndBias(pixelScale, pixelBias); + } + + tcuLogImage.logImageWithInfo(logImageAccess, info); + } else { + /* TODO: Implement */ + } +}; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuMatrix.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuMatrix.js new file mode 100644 index 000000000..e2959ecdc --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuMatrix.js @@ -0,0 +1,354 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +'use strict'; +goog.provide('framework.common.tcuMatrix'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + + var tcuMatrix = framework.common.tcuMatrix; + var deMath = framework.delibs.debase.deMath; + + var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); + }; + + /** + * @constructor + * @param {number} rows + * @param {number} cols + * @param {*=} value + * Initialize to identity. + */ + tcuMatrix.Matrix = function(rows, cols, value) { + value = value == undefined ? 1 : value; + this.rows = rows; + this.cols = cols; + this.matrix = []; + for (var i = 0; i < cols; i++) + this.matrix[i] = []; + for (var row = 0; row < rows; row++) + for (var col = 0; col < cols; col++) + this.set(row, col, (row == col) ? value : 0); + }; + + /** + * @param {number} rows + * @param {number} cols + * @param {Array<number>} vector + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.matrixFromVector = function(rows, cols, vector) { + var matrix = new tcuMatrix.Matrix(rows, cols); + for (var row = 0; row < vector.length; row++) + for (var col = 0; col < vector.length; col++) + matrix.matrix[col][row] = row == col ? vector[row] : 0; + return matrix; + }; + + /** + * @param {number} rows + * @param {number} cols + * @param {Array<number>} src + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.matrixFromDataArray = function(rows, cols, src) { + var matrix = new tcuMatrix.Matrix(rows, cols); + for (var row = 0; row < rows; row++) { + for (var col = 0; col < cols; col++) { + matrix.matrix[col][row] = src[row * cols + col]; + } + } + return matrix; + }; + + /** + * Fill the Matrix with data from array + * @param {number} rows + * @param {number} cols + * @param {Array<number>} array + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.matrixFromArray = function(rows, cols, array) { + DE_ASSERT(array.length === rows * cols); + var matrix = new tcuMatrix.Matrix(rows, cols); + for (var row = 0; row < rows; row++) + for (var col = 0; col < cols; col++) + matrix.matrix[col][row] = array[row * cols + col]; + return matrix; + }; + + tcuMatrix.Matrix.prototype.set = function(x, y, value) { + this.isRangeValid(x, y); + this.matrix[y][x] = value; + }; + + tcuMatrix.Matrix.prototype.setRow = function(row, values) { + if (!deMath.deInBounds32(row, 0, this.rows)) + throw new Error('Rows out of range'); + if (values.length > this.cols) + throw new Error('Too many columns'); + for (var col = 0; col < values.length; col++) + this.matrix[col][row] = values[col]; + }; + + tcuMatrix.Matrix.prototype.setCol = function(col, values) { + if (!deMath.deInBounds32(col, 0, this.cols)) + throw new Error('Columns out of range'); + if (values.length > this.rows) + throw new Error('Too many rows'); + for (var row = 0; row < values.length; row++) + this.matrix[col][row] = values[row]; + }; + + tcuMatrix.Matrix.prototype.get = function(x, y) { + this.isRangeValid(x, y); + return this.matrix[y][x]; + }; + + tcuMatrix.Matrix.prototype.getColumn = function(y) { + return this.matrix[y]; + }; + + tcuMatrix.Matrix.prototype.isRangeValid = function(x, y) { + if (!deMath.deInBounds32(x, 0, this.rows)) + throw new Error('Rows out of range'); + if (!deMath.deInBounds32(y, 0, this.cols)) + throw new Error('Columns out of range'); + }; + + /** + * @return {Array<number>} + */ + tcuMatrix.Matrix.prototype.getColumnMajorData = function() { + /** @type {Array<number>} */ var a = []; + for (var col = 0; col < this.cols; col++) + for (var row = 0; row < this.rows; row++) + a.push(this.get(row, col)); + return a; + }; + + /** + * @param {tcuMatrix.Matrix} matrixA + * @param {tcuMatrix.Matrix} matrixB + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.add = function(matrixA, matrixB) { + var res = new tcuMatrix.Matrix(matrixA.rows, matrixB.cols); + for (var col = 0; col < matrixA.cols; col++) + for (var row = 0; row < matrixA.rows; row++) + res.set(row, col, matrixA.get(row, col) + matrixB.get(row, col)); + return res; + }; + + /** + * @param {tcuMatrix.Matrix} matrixA + * @param {tcuMatrix.Matrix} matrixB + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.subtract = function(matrixA, matrixB) { + var res = new tcuMatrix.Matrix(matrixA.rows, matrixB.cols); + for (var col = 0; col < matrixA.cols; col++) + for (var row = 0; row < matrixA.rows; row++) + res.set(row, col, matrixA.get(row, col) - matrixB.get(row, col)); + return res; + }; + + /** + * @param {tcuMatrix.Matrix} matrixA + * @param {tcuMatrix.Matrix} matrixB + * @return {tcuMatrix.Matrix} + * Multiplication of two matrices. + */ + tcuMatrix.multiply = function(matrixA, matrixB) { + if (matrixA.cols != matrixB.rows) + throw new Error('Wrong matrices sizes'); + var res = new tcuMatrix.Matrix(matrixA.rows, matrixB.cols); + for (var row = 0; row < matrixA.rows; row++) + for (var col = 0; col < matrixB.cols; col++) { + var v = 0; + for (var ndx = 0; ndx < matrixA.cols; ndx++) + v += matrixA.get(row, ndx) * matrixB.get(ndx, col); + res.set(row, col, v); + } + return res; + }; + + /** + * @param {tcuMatrix.Matrix} matrixA + * @param {tcuMatrix.Matrix} matrixB + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.divide = function(matrixA, matrixB) { + var res = new tcuMatrix.Matrix(matrixA.rows, matrixA.cols); + for (var col = 0; col < matrixA.cols; col++) + for (var row = 0; row < matrixA.rows; row++) + res.set(row, col, matrixA.get(row, col) / matrixB.get(row, col)); + return res; + }; + + /** + * @param {tcuMatrix.Matrix} mtx + * @param {Array<number>} vec + * @return {Array<number>} + */ + tcuMatrix.multiplyMatVec = function(mtx, vec) { + /** @type {Array<number>} */ var res = []; + /** @type {number} */ var value; + for (var row = 0; row < mtx.rows; row++) { + value = 0; + for (var col = 0; col < mtx.cols; col++) + value += mtx.get(row, col) * vec[col]; + res[row] = value; + } + + return res; + }; + + /** + * @param {Array<number>} vec + * @param {tcuMatrix.Matrix} mtx + * @return {Array<number>} + */ + tcuMatrix.multiplyVecMat = function(vec, mtx) { + /** @type {Array<number>} */ var res = []; + /** @type {number} */ var value; + for (var col = 0; col < mtx.cols; col++) { + value = 0; + for (var row = 0; row < mtx.rows; row++) + value += mtx.get(row, col) * vec[row]; + res[col] = value; + } + + return res; + }; + + tcuMatrix.Matrix.prototype.toString = function() { + var str = 'mat' + this.cols; + if (this.rows !== this.cols) + str += 'x' + this.rows; + str += '('; + for (var col = 0; col < this.cols; col++) { + str += '['; + for (var row = 0; row < this.rows; row++) { + str += this.matrix[col][row]; + if (row != this.rows - 1) + str += ', '; + } + str += ']'; + + if (col != this.cols - 1) + str += ', '; + } + str += ')'; + return str; + }; + + /** + * @param {tcuMatrix.Matrix} mtx + * @param {number} scalar + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.subtractMatScal = function(mtx, scalar) { + /** @type {tcuMatrix.Matrix} */ var res = new tcuMatrix.Matrix(mtx.rows, mtx.cols); + for (var col = 0; col < mtx.cols; col++) + for (var row = 0; row < mtx.rows; row++) + res.set(row, col, mtx.get(row, col) - scalar); + + return res; + }; + + /** + * @param {tcuMatrix.Matrix} mtx + * @param {number} scalar + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.addMatScal = function(mtx, scalar) { + /** @type {tcuMatrix.Matrix} */ var res = new tcuMatrix.Matrix(mtx.rows, mtx.cols); + for (var col = 0; col < mtx.cols; col++) + for (var row = 0; row < mtx.rows; row++) + res.set(row, col, mtx.get(row, col) + scalar); + + return res; + }; + + /** + * @param {tcuMatrix.Matrix} mtx + * @param {number} scalar + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.multiplyMatScal = function(mtx, scalar) { + /** @type {tcuMatrix.Matrix} */ var res = new tcuMatrix.Matrix(mtx.rows, mtx.cols); + for (var col = 0; col < mtx.cols; col++) + for (var row = 0; row < mtx.rows; row++) + res.set(row, col, mtx.get(row, col) * scalar); + + return res; + }; + + /** + * @param {tcuMatrix.Matrix} mtx + * @param {number} scalar + * @return {tcuMatrix.Matrix} + */ + tcuMatrix.divideMatScal = function(mtx, scalar) { + /** @type {tcuMatrix.Matrix} */ var res = new tcuMatrix.Matrix(mtx.rows, mtx.cols); + for (var col = 0; col < mtx.cols; col++) + for (var row = 0; row < mtx.rows; row++) + res.set(row, col, mtx.get(row, col) / scalar); + + return res; + }; + + /** + * @constructor + * @extends {tcuMatrix.Matrix} + */ + tcuMatrix.Mat2 = function() { + tcuMatrix.Matrix.call(this, 2, 2); + }; + + tcuMatrix.Mat2.prototype = Object.create(tcuMatrix.Matrix.prototype); + tcuMatrix.Mat2.prototype.constructor = tcuMatrix.Mat2; + + /** + * @constructor + * @extends {tcuMatrix.Matrix} + */ + tcuMatrix.Mat3 = function() { + tcuMatrix.Matrix.call(this, 3, 3); + }; + + tcuMatrix.Mat3.prototype = Object.create(tcuMatrix.Matrix.prototype); + tcuMatrix.Mat3.prototype.constructor = tcuMatrix.Mat3; + + /** + * @constructor + * @extends {tcuMatrix.Matrix} + */ + tcuMatrix.Mat4 = function() { + tcuMatrix.Matrix.call(this, 4, 4); + }; + + tcuMatrix.Mat4.prototype = Object.create(tcuMatrix.Matrix.prototype); + tcuMatrix.Mat4.prototype.constructor = tcuMatrix.Mat4; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuMatrixUtil.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuMatrixUtil.js new file mode 100644 index 000000000..63dcaba87 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuMatrixUtil.js @@ -0,0 +1,70 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +'use strict'; +goog.provide('framework.common.tcuMatrixUtil'); +goog.require('framework.common.tcuMatrix'); + +goog.scope(function() { + + var tcuMatrixUtil = framework.common.tcuMatrixUtil; + var tcuMatrix = framework.common.tcuMatrix; + + /** + * @param {Array<number>} translation + * @return {tcuMatrix.Matrix} + */ + tcuMatrixUtil.translationMatrix = function(translation) { + var len = translation.length; + var res = new tcuMatrix.Matrix(len + 1, len + 1); + for (var row = 0; row < len; row++) + res.set(row, len, translation[row]); + return res; + }; + + /** + * Flatten an array of arrays or matrices + * @param {(Array<Array<number>> | Array<tcuMatrix.Matrix>)} a + * @return {Array<number>} + */ + tcuMatrixUtil.flatten = function(a) { + if (a[0] instanceof Array) { + var merged = []; + return merged.concat.apply(merged, a); + } + + if (a[0] instanceof tcuMatrix.Matrix) { + /** @type {tcuMatrix.Matrix} */ var m = a[0]; + var rows = m.rows; + var cols = m.cols; + var size = a.length; + var result = []; + for (var col = 0; col < cols; col++) + for (var i = 0; i < size; i++) + result.push(a[i].getColumn(col)); + return [].concat.apply([], result); + } + + if (typeof(a[0]) === 'number') + return a; + + throw new Error('Invalid input'); + }; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuPixelFormat.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuPixelFormat.js new file mode 100644 index 000000000..daf3297a9 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuPixelFormat.js @@ -0,0 +1,79 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuPixelFormat'); + +goog.scope(function() { + +var tcuPixelFormat = framework.common.tcuPixelFormat; + +/** + * @constructor + * @param {number=} r + * @param {number=} g + * @param {number=} b + * @param {number=} a + */ +tcuPixelFormat.PixelFormat = function(r, g, b, a) { + this.redBits = r || 0; + this.greenBits = g || 0; + this.blueBits = b || 0; + this.alphaBits = a || 0; +}; + +/** + * @param {WebGL2RenderingContext} context + * @return {tcuPixelFormat.PixelFormat} + */ +tcuPixelFormat.PixelFormatFromContext = function(context) { + var r = /** @type {number} */ (context.getParameter(gl.RED_BITS)); + var g = /** @type {number} */ (context.getParameter(gl.GREEN_BITS)); + var b = /** @type {number} */ (context.getParameter(gl.BLUE_BITS)); + var a = /** @type {number} */ (context.getParameter(gl.ALPHA_BITS)); + + return new tcuPixelFormat.PixelFormat(r, g, b, a); +}; + +/** + * @param {number} r + * @param {number} g + * @param {number} b + * @param {number} a + * @return {boolean} + */ +tcuPixelFormat.PixelFormat.prototype.equals = function(r, g, b, a) { + return this.redBits === r && + this.greenBits === g && + this.blueBits === b && + this.alphaBits === a; +}; + +/** + * @return {Array<number>} + */ +tcuPixelFormat.PixelFormat.prototype.getColorThreshold = function() { + return [1 << (8 - this.redBits), + 1 << (8 - this.greenBits), + 1 << (8 - this.blueBits), + (this.alphaBits > 0) ? (1 << (8 - this.alphaBits)) : 0]; +}; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuRGBA.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuRGBA.js new file mode 100644 index 000000000..0bab841d1 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuRGBA.js @@ -0,0 +1,279 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuRGBA'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + +var tcuRGBA = framework.common.tcuRGBA; +var deMath = framework.delibs.debase.deMath; + + var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); + }; + + /** + * class tcuRGBA.RGBA + * @constructor + * @struct + * @param {goog.NumberArray=} value + */ + tcuRGBA.RGBA = function(value) { + /** @type {goog.NumberArray} */ this.m_value = value || null; + + }; + + /** + * @enum + * In JS, these are not shift values, but positions in a typed array + */ + tcuRGBA.RGBA.Shift = { + RED: 0, + GREEN: 1, + BLUE: 2, + ALPHA: 3 + }; + + /** + * @enum + * Mask used as flags + * Hopefully will not use typed arrays + */ + tcuRGBA.RGBA.Mask = function() { + return { + RED: false, + GREEN: false, + BLUE: false, + ALPHA: false + }; + }; + + /** + * Builds an tcuRGBA.RGBA object from color components + * @param {number} r + * @param {number} g + * @param {number} b + * @param {number} a + * @return {tcuRGBA.RGBA} + */ + tcuRGBA.newRGBAComponents = function(r, g, b, a) { + DE_ASSERT(deMath.deInRange32(r, 0, 255)); + DE_ASSERT(deMath.deInRange32(g, 0, 255)); + DE_ASSERT(deMath.deInRange32(b, 0, 255)); + DE_ASSERT(deMath.deInRange32(a, 0, 255)); + + return new tcuRGBA.RGBA([r, g, b, a]); + }; + + /** + * Builds an tcuRGBA.RGBA object from a number array + * @param {goog.NumberArray} v + * @return {tcuRGBA.RGBA} + */ + tcuRGBA.newRGBAFromArray = function(v) { + return new tcuRGBA.RGBA(v.slice(0, 4)); + }; + + /** + * @param {number} value + * @return {tcuRGBA.RGBA} + */ + tcuRGBA.newRGBAFromValue = function(value) { + var rgba = new tcuRGBA.RGBA(); + var array32 = new Uint32Array([value]); + rgba.m_value = (new Uint8Array(array32.buffer)); + return rgba; + }; + + /** + * @param {Array<number>} v + * @return {tcuRGBA.RGBA} + */ + tcuRGBA.newRGBAFromVec = function (v) { + var r = deMath.clamp(v[0] * 255.0, 0, 255); + var g = deMath.clamp(v[1] * 255.0, 0, 255); + var b = deMath.clamp(v[2] * 255.0, 0, 255); + var a = deMath.clamp(v[3] * 255.0, 0, 255); + + return new tcuRGBA.RGBA([r, g, b, a]); + }; + + /** + * @param {number} v + */ + tcuRGBA.RGBA.prototype.setRed = function(v) { DE_ASSERT(deMath.deInRange32(v, 0, 255)); this.m_value[tcuRGBA.RGBA.Shift.RED] = v; }; + + /** + * @param {number} v + */ + tcuRGBA.RGBA.prototype.setGreen = function(v) { DE_ASSERT(deMath.deInRange32(v, 0, 255)); this.m_value[tcuRGBA.RGBA.Shift.GREEN] = v; }; + + /** + * @param {number} v + */ + tcuRGBA.RGBA.prototype.setBlue = function(v) { DE_ASSERT(deMath.deInRange32(v, 0, 255)); this.m_value[tcuRGBA.RGBA.Shift.BLUE] = v; }; + + /** + * @param {number} v + */ + tcuRGBA.RGBA.prototype.setAlpha = function(v) { DE_ASSERT(deMath.deInRange32(v, 0, 255)); this.m_value[tcuRGBA.RGBA.Shift.ALPHA] = v; }; + + /** + * @return {number} + */ + tcuRGBA.RGBA.prototype.getRed = function() { return this.m_value[tcuRGBA.RGBA.Shift.RED]; }; + + /** + * @return {number} + */ + tcuRGBA.RGBA.prototype.getGreen = function() { return this.m_value[tcuRGBA.RGBA.Shift.GREEN]; }; + + /** + * @return {number} + */ + tcuRGBA.RGBA.prototype.getBlue = function() { return this.m_value[tcuRGBA.RGBA.Shift.BLUE]; }; + + /** + * @return {number} + */ + tcuRGBA.RGBA.prototype.getAlpha = function() { return this.m_value[tcuRGBA.RGBA.Shift.ALPHA]; }; + + /** + * @param {tcuRGBA.RGBA} thr + * @return {boolean} + */ + tcuRGBA.RGBA.prototype.isBelowThreshold = function(thr) { return (this.getRed() <= thr.getRed()) && (this.getGreen() <= thr.getGreen()) && (this.getBlue() <= thr.getBlue()) && (this.getAlpha() <= thr.getAlpha()); }; + + /** + * @param {Uint8Array} bytes + * @return {tcuRGBA.RGBA} + */ + tcuRGBA.RGBA.fromBytes = function(bytes) { return tcuRGBA.newRGBAFromArray(bytes); }; + + /** + * @param {Uint8Array} bytes + */ + tcuRGBA.RGBA.prototype.toBytes = function(bytes) { var result = new Uint8Array(this.m_value); bytes[0] = result[0]; bytes[1] = result[1]; bytes[2] = result[2]; bytes[3] = result[3]; }; + + /** + * @return {Array<number>} + */ + tcuRGBA.RGBA.prototype.toVec = function() { + return [ + this.getRed() / 255.0, + this.getGreen() / 255.0, + this.getBlue() / 255.0, + this.getAlpha() / 255.0 + ]; + }; + + /** + * @return {Array<number>} + */ + tcuRGBA.RGBA.prototype.toIVec = function() { + return [ + this.getRed(), + this.getGreen(), + this.getBlue(), + this.getAlpha() + ]; + }; + + /** + * @param {tcuRGBA.RGBA} v + * @return {boolean} + */ + tcuRGBA.RGBA.prototype.equals = function(v) { + return ( + this.m_value[0] == v.m_value[0] && + this.m_value[1] == v.m_value[1] && + this.m_value[2] == v.m_value[2] && + this.m_value[3] == v.m_value[3] + ); + }; + + /** + * @param {tcuRGBA.RGBA} a + * @param {tcuRGBA.RGBA} b + * @param {tcuRGBA.RGBA} threshold + * @return {boolean} + */ + tcuRGBA.compareThreshold = function(a, b, threshold) { + if (a.equals(b)) return true; // Quick-accept + return tcuRGBA.computeAbsDiff(a, b).isBelowThreshold(threshold); + }; + + /** + * @param {tcuRGBA.RGBA} a + * @param {tcuRGBA.RGBA} b + * @return {tcuRGBA.RGBA} + */ + tcuRGBA.computeAbsDiff = function(a, b) { + return tcuRGBA.newRGBAComponents( + Math.abs(a.getRed() - b.getRed()), + Math.abs(a.getGreen() - b.getGreen()), + Math.abs(a.getBlue() - b.getBlue()), + Math.abs(a.getAlpha() - b.getAlpha()) + ); + }; + + /** + * @param {tcuRGBA.RGBA} a + * @param {number} b + * @return {tcuRGBA.RGBA} + */ + tcuRGBA.multiply = function(a, b) { + return tcuRGBA.newRGBAComponents( + deMath.clamp(a.getRed() * b, 0, 255), + deMath.clamp(a.getGreen() * b, 0, 255), + deMath.clamp(a.getBlue() * b, 0, 255), + deMath.clamp(a.getAlpha() * b, 0, 255)); + }; + + /** + * @param {tcuRGBA.RGBA} a + * @param {tcuRGBA.RGBA} b + * @return {tcuRGBA.RGBA} + */ + tcuRGBA.max = function(a, b) { + return tcuRGBA.newRGBAComponents( + Math.max(a.getRed(), b.getRed()), + Math.max(a.getGreen(), b.getGreen()), + Math.max(a.getBlue(), b.getBlue()), + Math.max(a.getAlpha(), b.getAlpha()) + ); + }; + + tcuRGBA.RGBA.prototype.toString = function() { + return '[' + this.m_value[0] + ',' + this.m_value[1] + ',' + this.m_value[2] + ',' + this.m_value[3] + ']'; + }; + + // Color constants + tcuRGBA.RGBA.red = tcuRGBA.newRGBAComponents(0xFF, 0, 0, 0xFF); + tcuRGBA.RGBA.green = tcuRGBA.newRGBAComponents(0, 0xFF, 0, 0xFF); + tcuRGBA.RGBA.blue = tcuRGBA.newRGBAComponents(0, 0, 0xFF, 0xFF); + tcuRGBA.RGBA.gray = tcuRGBA.newRGBAComponents(0x80, 0x80, 0x80, 0xFF); + tcuRGBA.RGBA.white = tcuRGBA.newRGBAComponents(0xFF, 0xFF, 0xFF, 0xFF); + tcuRGBA.RGBA.black = tcuRGBA.newRGBAComponents(0, 0, 0, 0xFF); + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuSkipList.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuSkipList.js new file mode 100644 index 000000000..541f074de --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuSkipList.js @@ -0,0 +1,324 @@ +/* +** Copyright (c) 2016 The Khronos Group Inc. +** +** Permission is hereby granted, free of charge, to any person obtaining a +** copy of this software and/or associated documentation files (the +** "Materials"), to deal in the Materials without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Materials, and to +** permit persons to whom the Materials are furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be included +** in all copies or substantial portions of the Materials. +** +** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. +*/ + +/** + * This class defines the individual tests which are skipped because + * of graphics driver bugs which simply can not be worked around in + * WebGL 2.0 implementations. + * + * The intent is that this list be kept as small as possible; and that + * bugs are filed with the respective GPU vendors for entries in this + * list. + * + * Pass the query argument "runSkippedTests" in the URL in order to + * force the skipped tests to be run. So, for example: + * + * http://localhost:8080/sdk/tests/deqp/functional/gles3/transformfeedback.html?filter=transform_feedback.basic_types.separate.points&runSkippedTests + */ +'use strict'; +goog.provide('framework.common.tcuSkipList'); + +goog.scope(function() { + + var tcuSkipList = framework.common.tcuSkipList; + + var _skipEntries = {}; + var _wildcardSkipEntries = {}; + var _reason = ""; + + function _setReason(reason) { + _reason = reason; + } + + function _skip(testName) { + if(testName.indexOf("*") >= 0){ + testName = testName.split("*")[0]; + _wildcardSkipEntries[testName] = _reason; + }else{ + _skipEntries[testName] = _reason; + } + } + + var runSkippedTests = false; + var queryVars = window.location.search.substring(1).split('&'); + for (var i = 0; i < queryVars.length; i++) { + var value = queryVars[i].split('='); + if (decodeURIComponent(value[0]) === 'runSkippedTests') { + // Assume that presence of this query arg implies to run + // the skipped tests; the value is ignored. + runSkippedTests = true; + break; + } + } + + if (!runSkippedTests) { + // Example usage: + // + // _setReason("Bugs in FooVendor 30.03 driver"); + // _skip("transform_feedback.basic_types.separate.points.lowp_mat2"); + + // Please see https://android.googlesource.com/platform/external/deqp/+/7c5323116bb164d64bfecb68e8da1af634317b24 + _setReason("Native dEQP also fails on these tests and suppresses them"); + _skip("texture_functions.textureoffset.sampler3d_fixed_fragment"); + _skip("texture_functions.textureoffset.isampler3d_fragment"); + _skip("texture_functions.textureoffset.usampler3d_fragment"); + _skip("texture_functions.textureprojoffset.sampler3d_fixed_fragment"); + _skip("texture_functions.textureprojoffset.isampler3d_fragment"); + _skip("texture_functions.textureprojoffset.usampler3d_fragment"); + // Please see https://android.googlesource.com/platform/external/deqp/+/master/android/cts/master/src/gles3-hw-issues.txt + _skip("texture_functions.textureprojlod.isampler3d_vertex"); + _skip("texture_functions.textureprojlod.usampler3d_vertex"); + // Please see https://android.googlesource.com/platform/external/deqp/+/master/android/cts/master/src/gles3-test-issues.txt + _skip("texture_functions.textureprojlodoffset.usampler3d_vertex"); + _skip("texture_functions.textureoffset.sampler3d_float_fragment"); + _skip("texture_functions.textureprojoffset.sampler3d_float_fragment"); + // Please see https://android.googlesource.com/platform/external/deqp/+/master/android/cts/master/src/gles3-driver-issues.txt + _skip("texture_functions.textureprojlodoffset.isampler3d_vertex"); + _skip("texture_functions.texturegrad.samplercubeshadow*"); + + // https://android.googlesource.com/platform/external/deqp/+/0c1f83aee4709eef7ef2a3edd384f9c192f476fd/android/cts/master/src/gles3-hw-issues.txt#801 + _setReason("Tricky blit rects can result in imperfect copies on some HW."); + _skip("blit.rect.nearest_consistency_mag"); + _skip("blit.rect.nearest_consistency_mag_reverse_dst_x"); + _skip("blit.rect.nearest_consistency_mag_reverse_src_dst_x"); + _skip("blit.rect.nearest_consistency_mag_reverse_src_x"); + _skip("blit.rect.nearest_consistency_mag_reverse_src_y"); + _skip("blit.rect.nearest_consistency_min"); + _skip("blit.rect.nearest_consistency_min_reverse_dst_x"); + _skip("blit.rect.nearest_consistency_min_reverse_src_dst_x"); + _skip("blit.rect.nearest_consistency_min_reverse_src_x"); + _skip("blit.rect.nearest_consistency_out_of_bounds_mag"); + _skip("blit.rect.nearest_consistency_out_of_bounds_mag_reverse_dst_x"); + _skip("blit.rect.nearest_consistency_out_of_bounds_mag_reverse_src_dst_x"); + _skip("blit.rect.nearest_consistency_out_of_bounds_mag_reverse_src_x"); + _skip("blit.rect.nearest_consistency_out_of_bounds_mag_reverse_src_y"); + _skip("blit.rect.nearest_consistency_out_of_bounds_min"); + _skip("blit.rect.nearest_consistency_out_of_bounds_min_reverse_dst_x"); + _skip("blit.rect.nearest_consistency_out_of_bounds_min_reverse_src_dst_x"); + _skip("blit.rect.nearest_consistency_out_of_bounds_min_reverse_src_x"); + _skip("blit.rect.nearest_consistency_out_of_bounds_min_reverse_src_y"); + + _setReason("Tricky blit rects can result in imperfect copies on Mac Intel driver."); + // crbug.com/658724 + // deqp/functional/gles3/framebufferblit/rect_03.html + _skip("blit.rect.nearest_consistency_mag_reverse_src_dst_y"); + // deqp/functional/gles3/framebufferblit/rect_04.html + _skip("blit.rect.nearest_consistency_min_reverse_src_dst_y"); + + // https://android.googlesource.com/platform/external/deqp/+/0c1f83aee4709eef7ef2a3edd384f9c192f476fd/android/cts/master/src/gles3-driver-issues.txt#381 + _setReason("Tricky blit rects can result in imperfect copies on some drivers."); + _skip("blit.rect.out_of_bounds_linear"); + _skip("blit.rect.out_of_bounds_reverse_src_x_linear"); + _skip("blit.rect.out_of_bounds_reverse_src_y_linear"); + _skip("blit.rect.out_of_bounds_reverse_dst_x_linear"); + _skip("blit.rect.out_of_bounds_reverse_dst_y_linear"); + _skip("blit.rect.out_of_bounds_reverse_src_dst_x_linear"); + _skip("blit.rect.out_of_bounds_reverse_src_dst_y_linear"); + + // https://android.googlesource.com/platform/external/deqp/+/0c1f83aee4709eef7ef2a3edd384f9c192f476fd/android/cts/master/src/gles3-driver-issues.txt#368 + _skip("blit.rect.nearest_consistency_out_of_bounds_mag_reverse_dst_y"); + _skip("blit.rect.nearest_consistency_out_of_bounds_mag_reverse_src_dst_y"); + _skip("blit.rect.nearest_consistency_out_of_bounds_min_reverse_dst_y"); + _skip("blit.rect.nearest_consistency_out_of_bounds_min_reverse_src_dst_y"); + + _setReason("Mac OSX drivers handle R11F_G11F_B10F format incorrectly"); + // https://github.com/KhronosGroup/WebGL/issues/1832 + // deqp/functional/gles3/fragmentoutput/basic.float.html + _skip("fragment_outputs.basic.float.r11f_g11f_b10f_mediump*"); + _skip("fragment_outputs.basic.float.r11f_g11f_b10f_highp*"); + // deqp/functional/gles3/fragmentoutput/array.float.html + _skip("fragment_outputs.array.float.r11f_g11f_b10f_mediump*"); + _skip("fragment_outputs.array.float.r11f_g11f_b10f_highp*"); + // deqp/functional/gles3/fragmentoutput/random_00.html + _skip("fragment_outputs.random.57"); + // deqp/functional/gles3/fragmentoutput/random_02.html + _skip("fragment_outputs.random.11"); + // deqp/functional/gles3/fborender/resize_01.html + _skip("render.resize.rbo_r11f_g11f_b10f"); + // deqp/functional/gles3/fborender/recreate_color_02.html + _skip("render.recreate_color.rbo_r11f_g11f_b10f_depth_stencil_rbo_depth24_stencil8"); + // deqp/functional/gles3/fbocolorbuffer/clear.html + _skip("color.clear.r11f_g11f_b10f"); + // deqp/functional/gles3/fbomultisample.2_samples.html + _skip("msaa.2_samples.r11f_g11f_b10f"); + // deqp/functional/gles3/fbomultisample.4_samples.html + _skip("msaa.4_samples.r11f_g11f_b10f"); + // deqp/functional/gles3/fbomultisample.8_samples.html + _skip("msaa.8_samples.r11f_g11f_b10f"); + + _setReason("Missing shadow sampler functions in D3D11"); + // https://github.com/KhronosGroup/WebGL/issues/1870 + // deqp/functional/gles3/shadertexturefunction/texture.html + _skip("texture_functions.texture.sampler2darrayshadow_vertex"); + // deqp/functional/gles3/shadertexturefunction/texturelod.html + _skip("texture_functions.texturelod.sampler2dshadow_vertex"); + _skip("texture_functions.texturelod.sampler2dshadow_fragment"); + // deqp/functional/gles3/shadertexturefunction/texturelodoffset.html + _skip("texture_functions.texturelodoffset.sampler2dshadow_vertex"); + _skip("texture_functions.texturelodoffset.sampler2dshadow_fragment"); + // deqp/functional/gles3/shadertexturefunction/textureprojlod.html + _skip("texture_functions.textureprojlod.sampler2dshadow_vertex"); + _skip("texture_functions.textureprojlod.sampler2dshadow_fragment"); + // deqp/functional/gles3/shadertexturefunction/textureprojlodoffset.html + _skip("texture_functions.textureprojlodoffset.sampler2dshadow_vertex"); + _skip("texture_functions.textureprojlodoffset.sampler2dshadow_fragment"); + // deqp/functional/gles3/shadertexturefunction/texturegrad.html + _skip("texture_functions.texturegrad.sampler2dshadow_vertex"); + _skip("texture_functions.texturegrad.sampler2dshadow_fragment"); + _skip("texture_functions.texturegrad.sampler2darrayshadow_vertex"); + _skip("texture_functions.texturegrad.sampler2darrayshadow_fragment"); + // deqp/functional/gles3/shadertexturefunction/texturegradoffset.html + _skip("texture_functions.texturegradoffset.sampler2dshadow_vertex"); + _skip("texture_functions.texturegradoffset.sampler2dshadow_fragment"); + _skip("texture_functions.texturegradoffset.sampler2darrayshadow_vertex"); + _skip("texture_functions.texturegradoffset.sampler2darrayshadow_fragment"); + // deqp/functional/gles3/shadertexturefunction/textureprojgrad.html + _skip("texture_functions.textureprojgrad.sampler2dshadow_vertex"); + _skip("texture_functions.textureprojgrad.sampler2dshadow_fragment"); + // deqp/functional/gles3/shadertexturefunction/textureprojgradoffset.html + _skip("texture_functions.textureprojgradoffset.sampler2dshadow_vertex"); + _skip("texture_functions.textureprojgradoffset.sampler2dshadow_fragment"); + + _setReason("MacOSX drivers share namespaces where they should not"); + // https://github.com/KhronosGroup/WebGL/issues/1890 + // deqp/data/gles3/shaders/scoping.html + _skip("scoping.valid.local_int_variable_hides_struct_type_vertex"); + _skip("scoping.valid.local_int_variable_hides_struct_type_fragment"); + _skip("scoping.valid.local_struct_variable_hides_struct_type_vertex"); + _skip("scoping.valid.local_struct_variable_hides_struct_type_fragment"); + _skip("scoping.valid.function_parameter_hides_struct_type_vertex"); + _skip("scoping.valid.function_parameter_hides_struct_type_fragment"); + + _setReason("NVidia Linux drivers does not clamp gl_FragDepth to [0.0, 1.0]"); + // Standalone Test case: + // https://github.com/Kangz/GLDriverBugs/blob/master/frag_depth_clamp_32f_depth/Main.cpp + // deqp/functional/gles3/fbodepthbuffer.html + _skip("depth.depth_write_clamp.depth_component32f"); + _skip("depth.depth_write_clamp.depth32f_stencil8"); + _skip("depth.depth_test_clamp.depth_component32f"); + _skip("depth.depth_test_clamp.depth32f_stencil8"); + + _setReason("NVidia Linux driver bug in nested uniform block location assignment"); + // crbug.com/621178 + // deqp/functional/gles3/uniformapi/random.html + _skip("uniform_api.random.64"); + + _setReason("Mac AMD / Linux AMD / older mesa handles clipping of wide points incorrectly"); + // crbug.com/642822 + // deqp/functional/gles3/clipping.html + _skip("clipping.wide_points_full_viewport"); + _skip("clipping.wide_points_partial_viewport"); + + + _setReason("Mac AMD drivers handle instanced rendering incorrectly under certain situation"); + // crbug.com/645298 + // deqp/functional/gles3/instancedrendering.html + _skip("instanced_rendering.draw_arrays_instanced.attribute_divisor.4_instances"); + _skip("instanced_rendering.draw_arrays_instanced.attribute_divisor.20_instances"); + _skip("instanced_rendering.draw_arrays_instanced.mixed.4_instances"); + _skip("instanced_rendering.draw_arrays_instanced.mixed.20_instances"); + _skip("instanced_rendering.draw_elements_instanced.attribute_divisor.4_instances"); + _skip("instanced_rendering.draw_elements_instanced.attribute_divisor.20_instances"); + _skip("instanced_rendering.draw_elements_instanced.mixed.4_instances"); + _skip("instanced_rendering.draw_elements_instanced.mixed.20_instances"); + + _setReason("Mac AMD drivers readPixels from RGB buffer into PIXEL_PACK_BUFFER got alpha values wrong"); + // crbug.com/646182 + // deqp/functional/gles3/pixelbufferobject.html + _skip("pixel_buffer_object.renderbuffer.rgb8_triangles"); + _skip("pixel_buffer_object.renderbuffer.rgb8_clears"); + + _setReason("Some Windows AMD D3D11 drivers have issues with blit and depth/stencil formats."); + // crbug.com/638323 + // deqp/functional/gles3/framebufferblit/depth_stencil.html + // Also see conformance2/rendering/blitframebuffer-stencil-only.html for 2.0.1 test. + _skip("blit.depth_stencil.depth24_stencil8_scale"); + _skip("blit.depth_stencil.depth24_stencil8_stencil_only"); + + _setReason("Transform feedback does not pass any tests on Mac AMD."); + // crbug.com/526748 + // Apple Radar: 28126946 + _skip("transform_feedback.*"); + + _setReason("Texture minification filtering is buggy for LINEAR mode on Mac Intel."); + // crbug.com/656478 + // Apple Radar: 28902129 + _skip("filtering.2d_combinations.linear_nearest_*"); + _skip("filtering.cube_combinations.linear_nearest_*"); + _skip("filtering.2d_array_combinations.linear_nearest_clamp_repeat"); + _skip("filtering.2d_array_combinations.linear_nearest_clamp_mirror"); + _skip("filtering.2d_array_combinations.linear_nearest_repeat_*"); + _skip("filtering.2d_array_combinations.linear_nearest_mirror_*"); + _skip("filtering.3d_combinations.linear_nearest_clamp_clamp_repeat"); + _skip("filtering.3d_combinations.linear_nearest_clamp_clamp_mirror"); + _skip("filtering.3d_combinations.linear_nearest_clamp_repeat_*"); + _skip("filtering.3d_combinations.linear_nearest_clamp_mirror_*"); + _skip("filtering.3d_combinations.linear_nearest_repeat_*"); + _skip("filtering.3d_combinations.linear_nearest_mirror_*"); + + _setReason("Setting of GL_READ_BUFFER and GL_DRAW_BUFFERs affects framebuffer completeness on Mac Intel."); + // crbug.com/630800 + // Apple Radar: 28236629 + _skip("completeness.attachment_combinations.none_rbo_none_none"); + _skip("completeness.attachment_combinations.none_tex_none_none"); + + _setReason("multisample constancy_alpha_to_coverage tests fail on Mac Intel."); + // crbug.com/663184 + _skip("multisample.fbo_4_samples.constancy_alpha_to_coverage"); + _skip("multisample.fbo_8_samples.constancy_alpha_to_coverage"); + _skip("multisample.fbo_max_samples.constancy_alpha_to_coverage"); + } // if (!runSkippedTests) + + /* + * Gets the skip status of the given test. Returns an + * object with the properties "skip", a boolean, and "reason", a + * string. + */ + tcuSkipList.getSkipStatus = function(testName) { + var skipEntry = _skipEntries[testName]; + if (skipEntry === undefined) { + return this._getWildcardSkipStatus(testName); + } else { + return { 'skip': true, 'reason': skipEntry }; + } + } + + /* + * Gets the skip status of the given tests like testpath* + * object with the properties "skip", a boolean, and "reason", a + * string. + */ + tcuSkipList._getWildcardSkipStatus = function(testName) { + var skipEntry; + for (var key in _wildcardSkipEntries) { + if (testName.indexOf(key) >=0 ) { + skipEntry = _wildcardSkipEntries[key]; + if (skipEntry != undefined) { + return { 'skip': true, 'reason': skipEntry }; + } + } + } + return { 'skip': false, 'reason': '' }; + } + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuStringTemplate.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuStringTemplate.js new file mode 100644 index 000000000..d70056733 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuStringTemplate.js @@ -0,0 +1,42 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuStringTemplate'); + +goog.scope(function() { + +var tcuStringTemplate = framework.common.tcuStringTemplate; + +tcuStringTemplate.escapeRegExp = function(string) { + return string.replace(/([.*+?^=!:$ {}()|\[\]\/\\])/g, '\\$1'); +}; + +tcuStringTemplate.specialize = function(str, params) { + var dst = str; + for (var key in params) { + var value = params[key]; + var re = new RegExp(tcuStringTemplate.escapeRegExp('\$\{' + key + '\}'), 'g'); + dst = dst.replace(re, value); + } + return dst; +}; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuSurface.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuSurface.js new file mode 100644 index 000000000..47d3634aa --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuSurface.js @@ -0,0 +1,184 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuSurface'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.delibs.debase.deMath'); +goog.require('framework.opengl.gluTextureUtil'); + +goog.scope(function() { + +var tcuSurface = framework.common.tcuSurface; +var tcuTexture = framework.common.tcuTexture; +var deMath = framework.delibs.debase.deMath; +var gluTextureUtil = framework.opengl.gluTextureUtil; + +var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); +}; + +/** + * \brief RGBA8888 surface + * + * tcuSurface.Surface provides basic pixel storage functionality. Only single format + * (RGBA8888) is supported. + * + * PixelBufferAccess (see tcuTexture.h) provides much more flexible API + * for handling various pixel formats. This is mainly a convinience class. + * @constructor + * @param {number=} width + * @param {number=} height + */ +tcuSurface.Surface = function(width, height) { + width = width || 0; + height = height || 0; + this.setSize(width, height); +}; + +/** + * @param {number} width + * @param {number} height + */ +tcuSurface.Surface.prototype.setSize = function(width, height) { + this.m_width = width; + this.m_height = height; + if (width * height > 0) { + this.m_data = new ArrayBuffer(4 * width * height); + this.m_pixels = new Uint8Array(this.m_data); + } else { + this.m_data = null; + this.m_pixels = null; + } +}; + +/** + * @return {number} + */ +tcuSurface.Surface.prototype.getWidth = function() { return this.m_width; }; + +/** + * @return {number} + */ +tcuSurface.Surface.prototype.getHeight = function() { return this.m_height; }; + +/** + * @param {number} x + * @param {number} y + * @param {Array<number>} color Vec4 color + */ +tcuSurface.Surface.prototype.setPixel = function(x, y, color) { + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + + var offset = 4 * (x + y * this.m_width); + for (var i = 0; i < 4; i++) + this.m_pixels[offset + i] = color[i]; +}; + +/** + * @param {number} x + * @param {number} y + * @return {Array<number>} + */ +tcuSurface.Surface.prototype.getPixel = function(x, y) { + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + + var color = []; + color.length = 4; + + var offset = 4 * (x + y * this.m_width); + for (var i = 0; i < 4; i++) + color[i] = this.m_pixels[offset + i]; + + return color; +}; + +/** + * @param {number} x + * @param {number} y + * @return {number} + */ +tcuSurface.Surface.prototype.getPixelUintRGB8 = function(x, y) { + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + + var offset = 4 * (x + y * this.m_width); + return (this.m_pixels[offset] << 16) + + (this.m_pixels[offset + 1] << 8) + + this.m_pixels[offset + 2]; +}; + +/** + * Read the viewport contents into this surface + * @param {*=} ctx WebGL or ref context + * @param {(Array<number>|{x: number, y: number, width: number, height: number})=} view + */ +tcuSurface.Surface.prototype.readViewport = function(ctx, view) { + ctx = ctx || gl; + var v = view || /** @type {Array<number>} */ (ctx.getParameter(gl.VIEWPORT)); + /** @type {number} */ var x; + /** @type {number} */ var y; + /** @type {number} */ var width; + /** @type {number} */ var height; + if (v instanceof Array || ArrayBuffer.isView(v)) { + x = v[0]; + y = v[1]; + width = v[2]; + height = v[3]; + } else { + x = v.x; + y = v.y; + width = v.width; + height = v.height; + } + if (width != this.m_width || height != this.m_height) + this.setSize(width, height); + ctx.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, this.m_pixels); +}; + +/** + * @return {Uint8Array} + */ +tcuSurface.Surface.prototype.getPixels = function() { return this.m_pixels || null; }; + +/** + * @return {tcuTexture.PixelBufferAccess} Pixel Buffer Access object + */ +tcuSurface.Surface.prototype.getAccess = function() { + return new tcuTexture.PixelBufferAccess({ + format: new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8), + width: this.m_width, + height: this.m_height, + data: this.m_data + }); + +}; + +tcuSurface.Surface.prototype.getSubAccess = function(x, y, width, height) { + /* TODO: Implement. the deqp getSubAccess() looks broken. It will probably fail if + * x != 0 or width != m_width + */ + throw new Error('Unimplemented'); +}; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTestCase.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTestCase.js new file mode 100644 index 000000000..d4322de4d --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTestCase.js @@ -0,0 +1,484 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/** + * This class allows one to create a hierarchy of tests and iterate over them. + * It replaces TestCase and TestCaseGroup classes. + */ +'use strict'; +goog.provide('framework.common.tcuTestCase'); +goog.require('framework.common.tcuSkipList'); + +goog.scope(function() { + + var tcuTestCase = framework.common.tcuTestCase; + var tcuSkipList = framework.common.tcuSkipList; + + /** + * Reads the filter parameter from the URL to filter tests. + * @return {?string } + */ + tcuTestCase.getFilter = function() { + var queryVars = window.location.search.substring(1).split('&'); + + for (var i = 0; i < queryVars.length; i++) { + var value = queryVars[i].split('='); + if (decodeURIComponent(value[0]) === 'filter') + return decodeURIComponent(value[1]); + } + return null; + }; + + /** + * Indicates the state of an iteration operation. + * @enum {number} + */ + tcuTestCase.IterateResult = { + STOP: 0, + CONTINUE: 1 + }; + + /**************************************** + * Runner + ***************************************/ + + /** + * A simple state machine. + * The purpose of this this object is to break + * long tests into small chunks that won't cause a timeout + * @constructor + */ + tcuTestCase.Runner = function() { + /** @type {tcuTestCase.DeqpTest} */ this.currentTest = null; + /** @type {tcuTestCase.DeqpTest} */ this.nextTest = null; + /** @type {tcuTestCase.DeqpTest} */ this.testCases = null; + /** @type {?string } */ this.filter = tcuTestCase.getFilter(); + }; + + /** + * @param {tcuTestCase.DeqpTest} root The root test of the test tree. + */ + tcuTestCase.Runner.prototype.setRoot = function(root) { + this.currentTest = null; + this.testCases = root; + }; + + tcuTestCase.Runner.prototype.setRange = function(range) { + this.range = range; + }; + + /** + * Searches the test tree for the next executable test + * @return {?tcuTestCase.DeqpTest } + */ + tcuTestCase.Runner.prototype.next = function() { + + // First time? Use root test + if (!this.currentTest) { + this.currentTest = this.testCases; + + // Root is executable? Use it + if (this.currentTest.isExecutable()) + return this.currentTest; + } + + // Should we proceed with the next test? + if (tcuTestCase.lastResult == tcuTestCase.IterateResult.STOP) { + // Look for next executable test + do { + if (this.range) + this.currentTest = this.currentTest.nextInRange(this.filter, this.range); + else + this.currentTest = this.currentTest.next(this.filter); + } while (this.currentTest && !this.currentTest.isExecutable()); + } + + return this.currentTest; + }; + + /** + * Schedule the callback to be run ASAP + * @param {function()} callback Callback to schedule + */ + tcuTestCase.Runner.prototype.runCallback = function(callback) { + setTimeout(function() { + callback(); + }.bind(this), 0); + }; + + /** + * Call this function at the end of the test + */ + tcuTestCase.Runner.prototype.terminate = function() { + finishTest(); + }; + + tcuTestCase.runner = new tcuTestCase.Runner(); + + /** @type {tcuTestCase.IterateResult} */ tcuTestCase.lastResult = tcuTestCase.IterateResult.STOP; + + /*************************************** + * DeqpTest + ***************************************/ + + /** + * Assigns name, description and specification to test + * @constructor + * @param {?string} name + * @param {?string} description + * @param {Object=} spec + */ + tcuTestCase.DeqpTest = function(name, description, spec) { + this.name = name || ''; + this.description = description || ''; + this.spec = spec; + this.currentTestNdx = 0; + this.parentTest = null; + this.childrenTests = []; + this.executeAlways = false; + }; + + /** + * Abstract init function(each particular test will implement it, or not) + */ + tcuTestCase.DeqpTest.prototype.init = function() {}; + + /** + * Abstract deinit function(each particular test will implement it, or not) + */ + tcuTestCase.DeqpTest.prototype.deinit = function() {}; + + /** + * Abstract iterate function(each particular test will implement it, or not) + * @return {tcuTestCase.IterateResult} + */ + tcuTestCase.DeqpTest.prototype.iterate = function() { return tcuTestCase.IterateResult.STOP; }; + + /** + * Checks if the test is executable + * @return {boolean} + */ + tcuTestCase.DeqpTest.prototype.isExecutable = function() { + return this.childrenTests.length == 0 || this.executeAlways; + }; + + /** + * Checks if the test is a leaf + */ + tcuTestCase.DeqpTest.prototype.isLeaf = function() { + return this.childrenTests.length == 0; + }; + + /** + * Marks the test as always executable + */ + tcuTestCase.DeqpTest.prototype.makeExecutable = function() { + this.executeAlways = true; + }; + + /** + * Adds a child test to the test's children + * @param {tcuTestCase.DeqpTest} test + */ + tcuTestCase.DeqpTest.prototype.addChild = function(test) { + test.parentTest = this; + this.childrenTests.push(test); + }; + + /** + * Sets the whole children tests array + * @param {Array<tcuTestCase.DeqpTest>} tests + */ + tcuTestCase.DeqpTest.prototype.setChildren = function(tests) { + for (var test in tests) + tests[test].parentTest = this; + this.childrenTests = tests; + }; + + /** + * Returns the next test in the hierarchy of tests + * + * @param {?string } pattern Optional pattern to search for + * @return {tcuTestCase.DeqpTest} + */ + tcuTestCase.DeqpTest.prototype.next = function(pattern) { + return this._nextHonoringSkipList(pattern); + }; + + /** + * Returns the next test in the hierarchy of tests, honoring the + * skip list, and reporting skipped tests. + * + * @param {?string } pattern Optional pattern to search for + * @return {tcuTestCase.DeqpTest} + */ + tcuTestCase.DeqpTest.prototype._nextHonoringSkipList = function(pattern) { + var tryAgain = false; + var test = null; + do { + tryAgain = false; + test = this._nextIgnoringSkipList(pattern); + if (test != null) { + // See whether the skip list vetoes the execution of + // this test. + var fullTestName = test.fullName(); + var skipDisposition = tcuSkipList.getSkipStatus(fullTestName); + if (skipDisposition.skip) { + tryAgain = true; + setCurrentTestName(fullTestName); + checkMessage(false, 'Skipping test due to tcuSkipList: ' + fullTestName); + } + } + } while (tryAgain); + return test; + }; + + + /** + * Returns the next test in the hierarchy of tests, ignoring the + * skip list. + * + * @param {?string } pattern Optional pattern to search for + * @return {tcuTestCase.DeqpTest} + */ + tcuTestCase.DeqpTest.prototype._nextIgnoringSkipList = function(pattern) { + if (pattern) + return this._findIgnoringSkipList(pattern); + + var test = null; + + //Look for the next child + if (this.currentTestNdx < this.childrenTests.length) { + test = this.childrenTests[this.currentTestNdx]; + this.currentTestNdx++; + } + + // If no more children, get the next brother + if (test == null && this.parentTest != null) { + test = this.parentTest._nextIgnoringSkipList(null); + } + + return test; + }; + + /** + * Returns the next test in the hierarchy of tests + * whose 1st level is in the given range + * + * @param {?string } pattern Optional pattern to search for + * @param {Array<number>} range + * @return {tcuTestCase.DeqpTest} + */ + tcuTestCase.DeqpTest.prototype.nextInRange = function(pattern, range) { + while (true) { + var test = this._nextHonoringSkipList(pattern); + if (!test) + return null; + var topLevelId = tcuTestCase.runner.testCases.currentTestNdx - 1; + if (topLevelId >= range[0] && topLevelId < range[1]) + return test; + } + }; + + /** + * Returns the full name of the test + * + * @return {string} Full test name. + */ + tcuTestCase.DeqpTest.prototype.fullName = function() { + if (this.parentTest) { + var parentName = this.parentTest.fullName(); + if (parentName) + return parentName + '.' + this.name; + } + return this.name; + }; + + /** + * Returns the description of the test + * + * @return {string} Test description. + */ + tcuTestCase.DeqpTest.prototype.getDescription = function() { + return this.description; + }; + + /** + * Find a test with a matching name. Fast-forwards to a test whose + * full name matches the given pattern. + * + * @param {string} pattern Regular expression to search for + * @return {?tcuTestCase.DeqpTest } Found test or null. + */ + tcuTestCase.DeqpTest.prototype.find = function(pattern) { + return this._findHonoringSkipList(pattern); + }; + + /** + * Find a test with a matching name. Fast-forwards to a test whose + * full name matches the given pattern, honoring the skip list, and + * reporting skipped tests. + * + * @param {string} pattern Regular expression to search for + * @return {?tcuTestCase.DeqpTest } Found test or null. + */ + tcuTestCase.DeqpTest.prototype._findHonoringSkipList = function(pattern) { + var tryAgain = false; + var test = null; + do { + tryAgain = false; + test = this._findIgnoringSkipList(pattern); + if (test != null) { + // See whether the skip list vetoes the execution of + // this test. + var fullTestName = test.fullName(); + var skipDisposition = tcuSkipList.getSkipStatus(fullTestName); + if (skipDisposition.skip) { + tryAgain = true; + checkMessage(false, 'Skipping test due to tcuSkipList: ' + fullTestName); + } + } + } while (tryAgain); + return test; + }; + + /** + * Find a test with a matching name. Fast-forwards to a test whose + * full name matches the given pattern. + * + * @param {string} pattern Regular expression to search for + * @return {?tcuTestCase.DeqpTest } Found test or null. + */ + tcuTestCase.DeqpTest.prototype._findIgnoringSkipList = function(pattern) { + var test = this; + while (true) { + test = test._nextIgnoringSkipList(null); + if (!test) + break; + if (test.fullName().match(pattern) || test.executeAlways) + break; + } + return test; + }; + + /** + * Reset the iterator. + */ + tcuTestCase.DeqpTest.prototype.reset = function() { + this.currentTestNdx = 0; + + for (var i = 0; i < this.childrenTests.length; i++) + this.childrenTests[i].reset(); + }; + + /** + * Defines a new test + * + * @param {?string} name Short test name + * @param {?string} description Description of the test + * @param {Object=} spec Test specification + * + * @return {tcuTestCase.DeqpTest} The new test + */ + tcuTestCase.newTest = function(name, description, spec) { + var test = new tcuTestCase.DeqpTest(name, description, spec); + + return test; + }; + + /** + * Defines a new executable test so it gets run even if it's not a leaf + * + * @param {string} name Short test name + * @param {string} description Description of the test + * @param {Object=} spec Test specification + * + * @return {tcuTestCase.DeqpTest} The new test + */ + tcuTestCase.newExecutableTest = function(name, description, spec) { + var test = tcuTestCase.newTest(name, description, spec); + test.makeExecutable(); + + return test; + }; + + /** + * Run through the test cases giving time to system operation. + */ + tcuTestCase.runTestCases = function() { + var state = tcuTestCase.runner; + + if (state.next()) { + try { + // If proceeding with the next test, prepare it. + var fullTestName = state.currentTest.fullName(); + var inited = true; + if (tcuTestCase.lastResult == tcuTestCase.IterateResult.STOP) { + // Update current test name + setCurrentTestName(fullTestName); + bufferedLogToConsole('Init testcase: ' + fullTestName); //Show also in console so we can see which test crashed the browser's tab + + // Initialize particular test + inited = state.currentTest.init(); + inited = inited === undefined ? true : inited; + + //If it's a leaf test, notify of it's execution. + if (state.currentTest.isLeaf() && inited) + debug('<hr/><br/>Start testcase: ' + fullTestName); + } + + if (inited) { + // Run the test, save the result. + tcuTestCase.lastResult = state.currentTest.iterate(); + } else { + // Skip uninitialized test. + tcuTestCase.lastResult = tcuTestCase.IterateResult.STOP; + } + + // Cleanup + if (tcuTestCase.lastResult == tcuTestCase.IterateResult.STOP) + state.currentTest.deinit(); + } + catch (err) { + // If the exception was not thrown by a test check, log it, but don't throw it again + if (!(err instanceof TestFailedException)) { + //Stop execution of current test. + tcuTestCase.lastResult = tcuTestCase.IterateResult.STOP; + try { + // Cleanup + if (tcuTestCase.lastResult == tcuTestCase.IterateResult.STOP) + state.currentTest.deinit(); + } catch (cerr) { + bufferedLogToConsole('Error while cleaning up test: ' + cerr); + } + var msg = err; + if (err.message) + msg = err.message; + testFailedOptions(msg, false); + } + bufferedLogToConsole(err); + } + + tcuTestCase.runner.runCallback(tcuTestCase.runTestCases); + } else + tcuTestCase.runner.terminate(); + }; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexCompareVerifier.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexCompareVerifier.js new file mode 100644 index 000000000..254963ae6 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexCompareVerifier.js @@ -0,0 +1,1356 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuTexCompareVerifier'); +goog.require('framework.common.tcuTexVerifierUtil'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.common.tcuTextureUtil'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + +var tcuTexCompareVerifier = framework.common.tcuTexCompareVerifier; +var tcuTexture = framework.common.tcuTexture; +var deMath = framework.delibs.debase.deMath; +var tcuTextureUtil = framework.common.tcuTextureUtil; +var tcuTexVerifierUtil = framework.common.tcuTexVerifierUtil; + +/** + * \brief Texture compare (shadow) lookup precision parameters. + * @constructor + * @struct + * @param {Array<number>=} coordBits + * @param {Array<number>=} uvwBits + * @param {number=} pcfBits + * @param {number=} referenceBits + * @param {number=} resultBits + */ +tcuTexCompareVerifier.TexComparePrecision = function(coordBits, uvwBits, pcfBits, referenceBits, resultBits) { + this.coordBits = coordBits === undefined ? [22, 22, 22] : coordBits; + this.uvwBits = uvwBits === undefined ? [22, 22, 22] : uvwBits; + this.pcfBits = pcfBits === undefined ? 16 : pcfBits; + this.referenceBits = referenceBits === undefined ? 16 : referenceBits; + this.resultBits = resultBits === undefined ? 16 : resultBits; +}; + +/** + * @constructor + * @struct + */ +tcuTexCompareVerifier.CmpResultSet = function() { + this.isTrue = false; + this.isFalse = false; +}; + +/** + * @param {tcuTexture.CompareMode} compareMode + * @param {number} cmpValue_ + * @param {number} cmpReference_ + * @param {number} referenceBits + * @param {boolean} isFixedPoint + * @return {tcuTexCompareVerifier.CmpResultSet} + */ +tcuTexCompareVerifier.execCompare = function(compareMode, + cmpValue_, + cmpReference_, + referenceBits, + isFixedPoint) { + var clampValues = isFixedPoint; // if comparing against a floating point texture, ref (and value) is not clamped + var cmpValue = (clampValues) ? (deMath.clamp(cmpValue_, 0, 1)) : (cmpValue_); + var cmpReference = (clampValues) ? (deMath.clamp(cmpReference_, 0, 1)) : (cmpReference_); + var err = tcuTexVerifierUtil.computeFixedPointError(referenceBits); + var res = new tcuTexCompareVerifier.CmpResultSet(); + + switch (compareMode) { + case tcuTexture.CompareMode.COMPAREMODE_LESS: + res.isTrue = cmpReference - err < cmpValue; + res.isFalse = cmpReference + err >= cmpValue; + break; + + case tcuTexture.CompareMode.COMPAREMODE_LESS_OR_EQUAL: + res.isTrue = cmpReference - err <= cmpValue; + res.isFalse = cmpReference + err > cmpValue; + break; + + case tcuTexture.CompareMode.COMPAREMODE_GREATER: + res.isTrue = cmpReference + err > cmpValue; + res.isFalse = cmpReference - err <= cmpValue; + break; + + case tcuTexture.CompareMode.COMPAREMODE_GREATER_OR_EQUAL: + res.isTrue = cmpReference + err >= cmpValue; + res.isFalse = cmpReference - err < cmpValue; + break; + + case tcuTexture.CompareMode.COMPAREMODE_EQUAL: + res.isTrue = deMath.deInRange32(cmpValue, cmpReference - err, cmpReference + err); + res.isFalse = err != 0 || cmpValue != cmpReference; + break; + + case tcuTexture.CompareMode.COMPAREMODE_NOT_EQUAL: + res.isTrue = err != 0 || cmpValue != cmpReference; + res.isFalse = deMath.deInRange32(cmpValue, cmpReference - err, cmpReference + err); + break; + + case tcuTexture.CompareMode.COMPAREMODE_ALWAYS: + res.isTrue = true; + break; + + case tcuTexture.CompareMode.COMPAREMODE_NEVER: + res.isFalse = true; + break; + + default: + throw new Error('Invalid compare mode:' + compareMode); + } + + assertMsgOptions(res.isTrue || res.isFalse, 'Both tests failed!', false, true); + return res; +}; + +/** + * @param {tcuTexture.TextureFormat} format + * @return {boolean} + */ +tcuTexCompareVerifier.isFixedPointDepthTextureFormat = function(format) { + var channelClass = tcuTexture.getTextureChannelClass(format.type); + + if (format.order == tcuTexture.ChannelOrder.D) { + // depth internal formats cannot be non-normalized integers + return channelClass != tcuTexture.TextureChannelClass.FLOATING_POINT; + } else if (format.order == tcuTexture.ChannelOrder.DS) { + // combined formats have no single channel class, detect format manually + switch (format.type) { + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: return false; + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: return true; + + default: + throw new Error('Invalid texture format: ' + format); + } + } + + return false; +}; + +/** + * @param {tcuTexture.CompareMode} compareMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} depths + * @param {Array<number>} fBounds + * @param {number} cmpReference + * @param {number} result + * @param {boolean} isFixedPointDepth + * @return {boolean} + */ +tcuTexCompareVerifier.isLinearCompareValid = function(compareMode, prec, depths, fBounds, cmpReference, result, isFixedPointDepth) { + assertMsgOptions(fBounds[0] >= 0 && fBounds[0] <= fBounds[1] && fBounds[1] <= 1, 'Invalid fBounds', false, true); + + var d0 = depths[0]; + var d1 = depths[1]; + + var cmp0 = tcuTexCompareVerifier.execCompare(compareMode, d0, cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp1 = tcuTexCompareVerifier.execCompare(compareMode, d1, cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp = [cmp0, cmp1]; + + var isTrue = getMask(cmp, function(x) {return x.isTrue;}); + var isFalse = getMask(cmp, function(x) {return x.isFalse;}); + + var f0 = fBounds[0]; + var f1 = fBounds[1]; + + var pcfErr = tcuTexVerifierUtil.computeFixedPointError(prec.pcfBits); + var resErr = tcuTexVerifierUtil.computeFixedPointError(prec.resultBits); + var totalErr = pcfErr + resErr; + + for (var comb = 0; comb < 4; comb++) { + if (((comb & isTrue) | (~comb & isFalse )) != 3) + continue; + + var cmp0True = ((comb >> 0) & 1) != 0; + var cmp1True = ((comb >> 1) & 1) != 0; + + var ref0 = cmp0True ? 1 : 0; + var ref1 = cmp1True ? 1 : 0; + + var v0 = ref0 * (1 - f0) + ref1 * f0; + var v1 = ref0 * (1 - f1) + ref1 * f1; + var minV = Math.min(v0, v1); + var maxV = Math.max(v0, v1); + var minR = minV - totalErr; + var maxR = maxV + totalErr; + + if (deMath.deInRange32(result, minR, maxR)) + return true; + } + return false; +}; + +/** + * @param {number} val + * @param {number} offset + * @return {Array<boolean>} + */ +tcuTexCompareVerifier.extractBVec4 = function(val, offset) { + return [ + ((val >> (offset + 0)) & 1) != 0, + ((val >> (offset + 1)) & 1) != 0, + ((val >> (offset + 2)) & 1) != 0, + ((val >> (offset + 3)) & 1) != 0]; +}; + +/** + * Values are in order (0,0), (1,0), (0,1), (1,1) + * @param {Array<number>} values + * @param {number} x + * @param {number} y + * @return {number} + */ +tcuTexCompareVerifier.bilinearInterpolate = function(values, x, y) { + var v00 = values[0]; + var v10 = values[1]; + var v01 = values[2]; + var v11 = values[3]; + var res = v00 * (1 - x) * (1 - y) + v10 * x * (1 - y) + v01 * (1 - x) * y + v11 * x * y; + return res; +}; + +/** + * @param {tcuTexture.CompareMode} compareMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} depths vec4 + * @param {number} cmpReference + * @param {number} result + * @param {boolean} isFixedPointDepth + * @return {boolean} + */ +tcuTexCompareVerifier.isBilinearAnyCompareValid = function(compareMode, + prec, + depths, + cmpReference, + result, + isFixedPointDepth) { + assertMsgOptions(prec.pcfBits === 0, 'PCF bits must be 0', false, true); + + var d0 = depths[0]; + var d1 = depths[1]; + var d2 = depths[2]; + var d3 = depths[3]; + + var cmp0 = tcuTexCompareVerifier.execCompare(compareMode, d0, cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp1 = tcuTexCompareVerifier.execCompare(compareMode, d1, cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp2 = tcuTexCompareVerifier.execCompare(compareMode, d2, cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp3 = tcuTexCompareVerifier.execCompare(compareMode, d3, cmpReference, prec.referenceBits, isFixedPointDepth); + + var canBeTrue = cmp0.isTrue || cmp1.isTrue || cmp2.isTrue || cmp3.isTrue; + var canBeFalse = cmp0.isFalse || cmp1.isFalse || cmp2.isFalse || cmp3.isFalse; + + var resErr = tcuTexVerifierUtil.computeFixedPointError(prec.resultBits); + + var minBound = canBeFalse ? 0 : 1; + var maxBound = canBeTrue ? 1 : 0; + + return deMath.deInRange32(result, minBound - resErr, maxBound + resErr); +}; + +/** + * @param {Array<tcuTexCompareVerifier.CmpResultSet>} arr + * @param {function(tcuTexCompareVerifier.CmpResultSet): boolean} getValue + * @return {number} + */ +var getMask = function(arr, getValue) { + var mask = 0; + for (var i = 0; i < arr.length; i++) { + var val = getValue(arr[i]); + if (val) + mask |= 1 << i; + } + return mask; +}; + +/** + * @param {tcuTexture.CompareMode} compareMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} depths vec4 + * @param {Array<number>} xBounds vec2 + * @param {Array<number>} yBounds vec2 + * @param {number} cmpReference + * @param {number} result + * @param {boolean} isFixedPointDepth + * @return {boolean} + */ +tcuTexCompareVerifier.isBilinearPCFCompareValid = function(compareMode, + prec, + depths, + xBounds, + yBounds, + cmpReference, + result, + isFixedPointDepth) { + assertMsgOptions(0.0 <= xBounds[0] && xBounds[0] <= xBounds[1] && xBounds[1] <= 1.0, 'x coordinate out of bounds', false, true); + assertMsgOptions(0.0 <= yBounds[0] && yBounds[0] <= yBounds[1] && yBounds[1] <= 1.0, 'y coordinate out of bounds', false, true); + assertMsgOptions(prec.pcfBits > 0, 'PCF bits must be > 0', false, true); + + var d0 = depths[0]; + var d1 = depths[1]; + var d2 = depths[2]; + var d3 = depths[3]; + + /** @type {Array<tcuTexCompareVerifier.CmpResultSet>} */ var cmp = []; + cmp[0] = tcuTexCompareVerifier.execCompare(compareMode, d0, cmpReference, prec.referenceBits, isFixedPointDepth); + cmp[1] = tcuTexCompareVerifier.execCompare(compareMode, d1, cmpReference, prec.referenceBits, isFixedPointDepth); + cmp[2] = tcuTexCompareVerifier.execCompare(compareMode, d2, cmpReference, prec.referenceBits, isFixedPointDepth); + cmp[3] = tcuTexCompareVerifier.execCompare(compareMode, d3, cmpReference, prec.referenceBits, isFixedPointDepth); + + var isTrue = getMask(cmp, function(x) {return x.isTrue}); + var isFalse = getMask(cmp, function(x) {return x.isFalse}); + + // Interpolation parameters + var x0 = xBounds[0]; + var x1 = xBounds[1]; + var y0 = yBounds[0]; + var y1 = yBounds[1]; + + // Error parameters + var pcfErr = tcuTexVerifierUtil.computeFixedPointError(prec.pcfBits); + var resErr = tcuTexVerifierUtil.computeFixedPointError(prec.resultBits); + var totalErr = pcfErr + resErr; + + // Iterate over all valid combinations. + // \note It is not enough to compute minmax over all possible result sets, as ranges may + // not necessarily overlap, i.e. there are gaps between valid ranges. + for (var comb = 0; comb < (1 << 4); comb++) { + // Filter out invalid combinations: + // 1) True bit is set in comb but not in isTrue => sample can not be true + // 2) True bit is NOT set in comb and not in isFalse => sample can not be false + if (((comb & isTrue) | (~comb & isFalse)) != (1 << 4) - 1) + continue; + + var cmpTrue = tcuTexCompareVerifier.extractBVec4(comb, 0); + var refVal = tcuTextureUtil.select([1, 1, 1, 1], [0, 0, 0, 0], cmpTrue); + + var v0 = tcuTexCompareVerifier.bilinearInterpolate(refVal, x0, y0); + var v1 = tcuTexCompareVerifier.bilinearInterpolate(refVal, x1, y0); + var v2 = tcuTexCompareVerifier.bilinearInterpolate(refVal, x0, y1); + var v3 = tcuTexCompareVerifier.bilinearInterpolate(refVal, x1, y1); + var minV = Math.min(v0, v1, v2, v3); + var maxV = Math.max(v0, v1, v2, v3); + var minR = minV - totalErr; + var maxR = maxV + totalErr; + + if (deMath.deInRange32(result, minR, maxR)) + return true; + } + + return false; +}; + +/** + * @param {tcuTexture.CompareMode} compareMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} depths vec4 + * @param {Array<number>} xBounds vec2 + * @param {Array<number>} yBounds vec2 + * @param {number} cmpReference + * @param {number} result + * @param {boolean} isFixedPointDepth + * @return {boolean} + */ +tcuTexCompareVerifier.isBilinearCompareValid = function(compareMode, + prec, + depths, + xBounds, + yBounds, + cmpReference, + result, + isFixedPointDepth) { + if (prec.pcfBits > 0) + return tcuTexCompareVerifier.isBilinearPCFCompareValid(compareMode, prec, depths, xBounds, yBounds, cmpReference, result, isFixedPointDepth); + else + return tcuTexCompareVerifier.isBilinearAnyCompareValid(compareMode, prec, depths, cmpReference, result, isFixedPointDepth); +}; +/** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec2 texture coordinates + * @param {number} coordZ + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isLinearCompareResultValid = function(level, + sampler, + prec, + coord, + coordZ, + cmpReference, + result) { + var isFixedPointDepth = tcuTexCompareVerifier.isFixedPointDepthTextureFormat(level.getFormat()); + var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, level.getWidth(), coord[0], prec.coordBits[0], prec.uvwBits[0]); + var vBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, level.getHeight(), coord[1], prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinate bounds for (x0,y0) - without wrap mode + var minI = Math.floor(uBounds[0] - 0.5); + var maxI = Math.floor(uBounds[1] - 0.5); + var minJ = Math.floor(vBounds[0] - 0.5); + var maxJ = Math.floor(vBounds[1] - 0.5); + + var w = level.getWidth(); + var h = level.getHeight(); + + // \todo [2013-07-03 pyry] This could be optimized by first computing ranges based on wrap mode. + + for (var j = minJ; j <= maxJ; j++) { + for (var i = minI; i <= maxI; i++) { + // Wrapped coordinates + var x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i, w); + var x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i + 1, w); + var y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j, h); + var y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j + 1, h); + + // Bounds for filtering factors + var minA = deMath.clamp((uBounds[0] - 0.5) - i, 0, 1); + var maxA = deMath.clamp((uBounds[1] - 0.5) - i, 0, 1); + var minB = deMath.clamp((vBounds[0] - 0.5) - j, 0, 1); + var maxB = deMath.clamp((vBounds[1] - 0.5) - j, 0, 1); + + var depths = [ + level.getPixDepth(x0, y0, coordZ), + level.getPixDepth(x1, y0, coordZ), + level.getPixDepth(x0, y1, coordZ), + level.getPixDepth(x1, y1, coordZ) + ]; + + if (tcuTexCompareVerifier.isBilinearCompareValid(sampler.compare, prec, depths, [minA, maxA], [minB, maxB], cmpReference, result, isFixedPointDepth)) + return true; + } + } + + return false; +}; + +/** + * @param {tcuTexCompareVerifier.CmpResultSet} resultSet + * @param {number} result + * @param {number} resultBits + */ +tcuTexCompareVerifier.isResultInSet = function(resultSet, result, resultBits) { + var err = tcuTexVerifierUtil.computeFixedPointError(resultBits); + var minR = result - err; + var maxR = result + err; + + return (resultSet.isTrue && deMath.deInRange32(1, minR, maxR)) || + (resultSet.isFalse && deMath.deInRange32(0, minR, maxR)); +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec2 texture coordinates + * @param {number} coordZ + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isNearestCompareResultValid = function(level, + sampler, + prec, + coord, + coordZ, + cmpReference, + result) { + var isFixedPointDepth = tcuTexCompareVerifier.isFixedPointDepthTextureFormat(level.getFormat()); + var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, level.getWidth(), coord[0], prec.coordBits[0], prec.uvwBits[0]); + var vBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, level.getHeight(), coord[1], prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinates - without wrap mode + var minI = Math.floor(uBounds[0]); + var maxI = Math.floor(uBounds[1]); + var minJ = Math.floor(vBounds[0]); + var maxJ = Math.floor(vBounds[1]); + + for (var j = minJ; j <= maxJ; j++) { + for (var i = minI; i <= maxI; i++) { + var x = tcuTexVerifierUtil.wrap(sampler.wrapS, i, level.getWidth()); + var y = tcuTexVerifierUtil.wrap(sampler.wrapT, j, level.getHeight()); + var depth = level.getPixDepth(x, y, coordZ); + var resSet = tcuTexCompareVerifier.execCompare(sampler.compare, depth, cmpReference, prec.referenceBits, isFixedPointDepth); + + if (tcuTexCompareVerifier.isResultInSet(resSet, result, prec.resultBits)) + return true; + } + } + + return false; +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} filterMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec2 texture coordinates + * @param {number} coordZ + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isLevelCompareResultValid = function(level, + sampler, + filterMode, + prec, + coord, + coordZ, + cmpReference, + result) { + if (filterMode == tcuTexture.FilterMode.LINEAR) + return tcuTexCompareVerifier.isLinearCompareResultValid(level, sampler, prec, coord, coordZ, cmpReference, result); + else + return tcuTexCompareVerifier.isNearestCompareResultValid(level, sampler, prec, coord, coordZ, cmpReference, result); +}; + +/** + * @param {tcuTexture.CompareMode} compareMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} depths0 vec4 + * @param {Array<number>} depths1 vec4 + * @param {number} cmpReference + * @param {number} result + * @param {boolean} isFixedPointDepth + * @return {boolean} + */ +tcuTexCompareVerifier.isTrilinearAnyCompareValid = function(compareMode, + prec, + depths0, + depths1, + cmpReference, + result, + isFixedPointDepth) { + assertMsgOptions(prec.pcfBits === 0, 'PCF bits must be 0', false, true); + + var cmp00 = tcuTexCompareVerifier.execCompare(compareMode, depths0[0], cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp01 = tcuTexCompareVerifier.execCompare(compareMode, depths0[1], cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp02 = tcuTexCompareVerifier.execCompare(compareMode, depths0[2], cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp03 = tcuTexCompareVerifier.execCompare(compareMode, depths0[3], cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp10 = tcuTexCompareVerifier.execCompare(compareMode, depths1[0], cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp11 = tcuTexCompareVerifier.execCompare(compareMode, depths1[1], cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp12 = tcuTexCompareVerifier.execCompare(compareMode, depths1[2], cmpReference, prec.referenceBits, isFixedPointDepth); + var cmp13 = tcuTexCompareVerifier.execCompare(compareMode, depths1[3], cmpReference, prec.referenceBits, isFixedPointDepth); + + var canBeTrue = cmp00.isTrue || + cmp01.isTrue || + cmp02.isTrue || + cmp03.isTrue || + cmp10.isTrue || + cmp11.isTrue || + cmp12.isTrue || + cmp13.isTrue; + var canBeFalse = cmp00.isFalse || + cmp01.isFalse || + cmp02.isFalse || + cmp03.isFalse || + cmp10.isFalse || + cmp11.isFalse || + cmp12.isFalse || + cmp13.isFalse; + + var resErr = tcuTexVerifierUtil.computeFixedPointError(prec.resultBits); + + var minBound = canBeFalse ? 0 : 1; + var maxBound = canBeTrue ? 1 : 0; + + return deMath.deInRange32(result, minBound - resErr, maxBound + resErr); +}; + +/** + * @param {tcuTexture.CompareMode} compareMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} depths0 vec4 + * @param {Array<number>} depths1 vec4 + * @param {Array<number>} xBounds0 + * @param {Array<number>} yBounds0 + * @param {Array<number>} xBounds1 + * @param {Array<number>} yBounds1 + * @param {Array<number>} fBounds + * @param {number} cmpReference + * @param {number} result + * @param {boolean} isFixedPointDepth + * @return {boolean} + */ +tcuTexCompareVerifier.isTrilinearPCFCompareValid = function(compareMode, + prec, + depths0, + depths1, + xBounds0, + yBounds0, + xBounds1, + yBounds1, + fBounds, + cmpReference, + result, + isFixedPointDepth) { + assertMsgOptions(0.0 <= xBounds0[0] && xBounds0[0] <= xBounds0[1] && xBounds0[1] <= 1.0, 'x0 coordinate out of bounds', false, true); + assertMsgOptions(0.0 <= yBounds0[0] && yBounds0[0] <= yBounds0[1] && yBounds0[1] <= 1.0, 'y0 coordinate out of bounds', false, true); + assertMsgOptions(0.0 <= xBounds1[0] && xBounds1[0] <= xBounds1[1] && xBounds1[1] <= 1.0, 'x1 coordinate out of bounds', false, true); + assertMsgOptions(0.0 <= yBounds1[0] && yBounds1[0] <= yBounds1[1] && yBounds1[1] <= 1.0, 'y1 coordinate out of bounds', false, true); + assertMsgOptions(0.0 <= fBounds[0] && fBounds[0] <= fBounds[1] && fBounds[1] <= 1.0, 'linear factor out of bounds', false, true); + assertMsgOptions(prec.pcfBits > 0, 'PCF bits must be > 0', false, true); + + /** @type {Array<tcuTexCompareVerifier.CmpResultSet>} */ var cmp = []; + cmp.push(tcuTexCompareVerifier.execCompare(compareMode, depths0[0], cmpReference, prec.referenceBits, isFixedPointDepth)); + cmp.push(tcuTexCompareVerifier.execCompare(compareMode, depths0[1], cmpReference, prec.referenceBits, isFixedPointDepth)); + cmp.push(tcuTexCompareVerifier.execCompare(compareMode, depths0[2], cmpReference, prec.referenceBits, isFixedPointDepth)); + cmp.push(tcuTexCompareVerifier.execCompare(compareMode, depths0[3], cmpReference, prec.referenceBits, isFixedPointDepth)); + cmp.push(tcuTexCompareVerifier.execCompare(compareMode, depths1[0], cmpReference, prec.referenceBits, isFixedPointDepth)); + cmp.push(tcuTexCompareVerifier.execCompare(compareMode, depths1[1], cmpReference, prec.referenceBits, isFixedPointDepth)); + cmp.push(tcuTexCompareVerifier.execCompare(compareMode, depths1[2], cmpReference, prec.referenceBits, isFixedPointDepth)); + cmp.push(tcuTexCompareVerifier.execCompare(compareMode, depths1[3], cmpReference, prec.referenceBits, isFixedPointDepth)); + + var isTrue = getMask(cmp, function(x) {return x.isTrue}); + var isFalse = getMask(cmp, function(x) {return x.isFalse}); + + // Error parameters + var pcfErr = tcuTexVerifierUtil.computeFixedPointError(prec.pcfBits); + var resErr = tcuTexVerifierUtil.computeFixedPointError(prec.resultBits); + var totalErr = pcfErr + resErr; + + // Iterate over all valid combinations. + for (var comb = 0; comb < (1 << 8); comb++) { + // Filter out invalid combinations. + if (((comb & isTrue) | (~comb & isFalse)) != (1 << 8) - 1) + continue; + + var cmpTrue0 = tcuTexCompareVerifier.extractBVec4(comb, 0); + var cmpTrue1 = tcuTexCompareVerifier.extractBVec4(comb, 4); + var refVal0 = tcuTextureUtil.select([1, 1, 1, 1], [0, 0, 0, 0], cmpTrue0); + var refVal1 = tcuTextureUtil.select([1, 1, 1, 1], [0, 0, 0, 0], cmpTrue1); + + // Bilinear interpolation within levels. + var v00 = tcuTexCompareVerifier.bilinearInterpolate(refVal0, xBounds0[0], yBounds0[0]); + var v01 = tcuTexCompareVerifier.bilinearInterpolate(refVal0, xBounds0[1], yBounds0[0]); + var v02 = tcuTexCompareVerifier.bilinearInterpolate(refVal0, xBounds0[0], yBounds0[1]); + var v03 = tcuTexCompareVerifier.bilinearInterpolate(refVal0, xBounds0[1], yBounds0[1]); + var minV0 = Math.min(v00, v01, v02, v03); + var maxV0 = Math.max(v00, v01, v02, v03); + + var v10 = tcuTexCompareVerifier.bilinearInterpolate(refVal1, xBounds1[0], yBounds1[0]); + var v11 = tcuTexCompareVerifier.bilinearInterpolate(refVal1, xBounds1[1], yBounds1[0]); + var v12 = tcuTexCompareVerifier.bilinearInterpolate(refVal1, xBounds1[0], yBounds1[1]); + var v13 = tcuTexCompareVerifier.bilinearInterpolate(refVal1, xBounds1[1], yBounds1[1]); + var minV1 = Math.min(v10, v11, v12, v13); + var maxV1 = Math.max(v10, v11, v12, v13); + + // Compute min-max bounds by filtering between minimum bounds and maximum bounds between levels. + // HW can end up choosing pretty much any of samples between levels, and thus interpolating + // between minimums should yield lower bound for range, and same for upper bound. + // \todo [2013-07-17 pyry] This seems separable? Can this be optimized? At least ranges could be pre-computed and later combined. + var minF0 = minV0 * (1 - fBounds[0]) + minV1 * fBounds[0]; + var minF1 = minV0 * (1 - fBounds[1]) + minV1 * fBounds[1]; + var maxF0 = maxV0 * (1 - fBounds[0]) + maxV1 * fBounds[0]; + var maxF1 = maxV0 * (1 - fBounds[1]) + maxV1 * fBounds[1]; + + var minF = Math.min(minF0, minF1); + var maxF = Math.max(maxF0, maxF1); + + var minR = minF - totalErr; + var maxR = maxF + totalErr; + + if (deMath.deInRange32(result, minR, maxR)) + return true; + } + + return false; + +}; + +/** + * @param {tcuTexture.CompareMode} compareMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} depths0 vec4 + * @param {Array<number>} depths1 vec4 + * @param {Array<number>} xBounds0 + * @param {Array<number>} yBounds0 + * @param {Array<number>} xBounds1 + * @param {Array<number>} yBounds1 + * @param {Array<number>} fBounds + * @param {number} cmpReference + * @param {number} result + * @param {boolean} isFixedPointDepth + * @return {boolean} + */ +tcuTexCompareVerifier.isTrilinearCompareValid = function(compareMode, + prec, + depths0, + depths1, + xBounds0, + yBounds0, + xBounds1, + yBounds1, + fBounds, + cmpReference, + result, + isFixedPointDepth) { + if (prec.pcfBits > 0) + return tcuTexCompareVerifier.isTrilinearPCFCompareValid(compareMode, prec, depths0, depths1, xBounds0, yBounds0, xBounds1, yBounds1, fBounds, cmpReference, result, isFixedPointDepth); + else + return tcuTexCompareVerifier.isTrilinearAnyCompareValid(compareMode, prec, depths0, depths1, cmpReference, result, isFixedPointDepth); +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec2 texture coordinates + * @param {number} coordZ + * @param {Array<number>} fBounds vec2 + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isLinearMipmapLinearCompareResultValid = function(level0, + level1, + sampler, + prec, + coord, + coordZ, + fBounds, + cmpReference, + result) { + var isFixedPointDepth = tcuTexCompareVerifier.isFixedPointDepthTextureFormat(level0.getFormat()); + + // \todo [2013-07-04 pyry] This is strictly not correct as coordinates between levels should be dependent. + // Right now this allows pairing any two valid bilinear quads. + + var w0 = level0.getWidth(); + var w1 = level1.getWidth(); + var h0 = level0.getHeight(); + var h1 = level1.getHeight(); + + var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, w0, coord[0], prec.coordBits[0], prec.uvwBits[0]); + var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, w1, coord[0], prec.coordBits[0], prec.uvwBits[0]); + var vBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, h0, coord[1], prec.coordBits[1], prec.uvwBits[1]); + var vBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, h1, coord[1], prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinates - without wrap mode + var minI0 = Math.floor(uBounds0[0] - 0.5); + var maxI0 = Math.floor(uBounds0[1] - 0.5); + var minI1 = Math.floor(uBounds1[0] - 0.5); + var maxI1 = Math.floor(uBounds1[1] - 0.5); + var minJ0 = Math.floor(vBounds0[0] - 0.5); + var maxJ0 = Math.floor(vBounds0[1] - 0.5); + var minJ1 = Math.floor(vBounds1[0] - 0.5); + var maxJ1 = Math.floor(vBounds1[1] - 0.5); + + for (var j0 = minJ0; j0 <= maxJ0; j0++) { + for (var i0 = minI0; i0 <= maxI0; i0++) { + var minA0 = deMath.clamp((uBounds0[0] - 0.5) - i0, 0, 1); + var maxA0 = deMath.clamp((uBounds0[1] - 0.5) - i0, 0, 1); + var minB0 = deMath.clamp((vBounds0[0] - 0.5) - j0, 0, 1); + var maxB0 = deMath.clamp((vBounds0[1] - 0.5) - j0, 0, 1); + var depths0 = []; + + var x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i0, w0); + var x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i0 + 1, w0); + var y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j0, h0); + var y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j0 + 1, h0); + + depths0[0] = level0.getPixDepth(x0, y0, coordZ); + depths0[1] = level0.getPixDepth(x1, y0, coordZ); + depths0[2] = level0.getPixDepth(x0, y1, coordZ); + depths0[3] = level0.getPixDepth(x1, y1, coordZ); + + for (var j1 = minJ1; j1 <= maxJ1; j1++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + var minA1 = deMath.clamp((uBounds1[0] - 0.5) - i1, 0, 1); + var maxA1 = deMath.clamp((uBounds1[1] - 0.5) - i1, 0, 1); + var minB1 = deMath.clamp((vBounds1[0] - 0.5) - j1, 0, 1); + var maxB1 = deMath.clamp((vBounds1[1] - 0.5) - j1, 0, 1); + var depths1 = []; + + x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i1, w1); + x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i1 + 1, w1); + y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j1, h1); + y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j1 + 1, h1); + + depths1[0] = level1.getPixDepth(x0, y0, coordZ); + depths1[1] = level1.getPixDepth(x1, y0, coordZ); + depths1[2] = level1.getPixDepth(x0, y1, coordZ); + depths1[3] = level1.getPixDepth(x1, y1, coordZ); + + if (tcuTexCompareVerifier.isTrilinearCompareValid(sampler.compare, prec, depths0, depths1, + [minA0, maxA0], [minB0, maxB0], + [minA1, maxA1], [minB1, maxB1], + fBounds, cmpReference, result, isFixedPointDepth)) + return true; + } + } + } + } + + return false; +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec2 texture coordinates + * @param {number} coordZ + * @param {Array<number>} fBounds vec2 + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isNearestMipmapLinearCompareResultValid = function(level0, + level1, + sampler, + prec, + coord, + coordZ, + fBounds, + cmpReference, + result) { + var isFixedPointDepth = tcuTexCompareVerifier.isFixedPointDepthTextureFormat(level0.getFormat()); + + var w0 = level0.getWidth(); + var w1 = level1.getWidth(); + var h0 = level0.getHeight(); + var h1 = level1.getHeight(); + + var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, w0, coord[0], prec.coordBits[0], prec.uvwBits[0]); + var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, w1, coord[0], prec.coordBits[0], prec.uvwBits[0]); + var vBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, h0, coord[1], prec.coordBits[1], prec.uvwBits[1]); + var vBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, h1, coord[1], prec.coordBits[1], prec.uvwBits[1]); + + var minI0 = Math.floor(uBounds0[0]); + var maxI0 = Math.floor(uBounds0[1]); + var minI1 = Math.floor(uBounds1[0]); + var maxI1 = Math.floor(uBounds1[1]); + var minJ0 = Math.floor(vBounds0[0]); + var maxJ0 = Math.floor(vBounds0[1]); + var minJ1 = Math.floor(vBounds1[0]); + var maxJ1 = Math.floor(vBounds1[1]); + + for (var j0 = minJ0; j0 <= maxJ0; j0++) { + for (var i0 = minI0; i0 <= maxI0; i0++) { + var x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i0, w0); + var y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j0, h0); + + // Derivated from C++ dEQP function lookupDepth() + // Since x0 and y0 are wrapped, here lookupDepth() returns the same result as getPixDepth() + assertMsgOptions(deMath.deInBounds32(x0, 0, level0.getWidth()) && deMath.deInBounds32(y0, 0, level0.getHeight()) && deMath.deInBounds32(coordZ, 0, level0.getDepth()), 'x0, y0 or coordZ out of bound.', false, true); + var depth0 = level0.getPixDepth(x0, y0, coordZ); + + for (var j1 = minJ1; j1 <= maxJ1; j1++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + var x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i1, w1); + var y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j1, h1); + + // Derivated from C++ dEQP function lookupDepth() + // Since x1 and y1 are wrapped, here lookupDepth() returns the same result as getPixDepth() + assertMsgOptions(deMath.deInBounds32(x1, 0, level1.getWidth()) && deMath.deInBounds32(y1, 0, level1.getHeight()), 'x1 or y1 out of bound.', false, true); + var depth1 = level1.getPixDepth(x1, y1, coordZ); + + if (tcuTexCompareVerifier.isLinearCompareValid(sampler.compare, prec, [depth0, depth1], fBounds, cmpReference, result, isFixedPointDepth)) + return true; + } + } + } + } + + return false; +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} levelFilter + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec2 texture coordinates + * @param {number} coordZ + * @param {Array<number>} fBounds vec2 + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isMipmapLinearCompareResultValid = function(level0, + level1, + sampler, + levelFilter, + prec, + coord, + coordZ, + fBounds, + cmpReference, + result) { + if (levelFilter == tcuTexture.FilterMode.LINEAR) + return tcuTexCompareVerifier.isLinearMipmapLinearCompareResultValid(level0, level1, sampler, prec, coord, coordZ, fBounds, cmpReference, result); + else + return tcuTexCompareVerifier.isNearestMipmapLinearCompareResultValid(level0, level1, sampler, prec, coord, coordZ, fBounds, cmpReference, result); +}; + +/** + * @param {tcuTexture.Texture2DView} texture + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec2 texture coordinates + * @param {Array<number>} lodBounds vec2 level-of-detail bounds + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isTexCompareResultValid2D = function(texture, sampler, prec, coord, lodBounds, cmpReference, result) { + var minLod = lodBounds[0]; + var maxLod = lodBounds[1]; + var canBeMagnified = minLod <= sampler.lodThreshold; + var canBeMinified = maxLod > sampler.lodThreshold; + + if (canBeMagnified) { + if (tcuTexCompareVerifier.isLevelCompareResultValid(texture.getLevel(0), sampler, sampler.magFilter, prec, coord, 0, cmpReference, result)) + return true; + } + + if (canBeMinified) { + var isNearestMipmap = tcuTexVerifierUtil.isNearestMipmapFilter(sampler.minFilter); + var isLinearMipmap = tcuTexVerifierUtil.isLinearMipmapFilter(sampler.minFilter); + var minTexLevel = 0; + var maxTexLevel = texture.getNumLevels() - 1; + + assertMsgOptions(minTexLevel < maxTexLevel, 'Invalid texture levels.', false, true); + + if (isLinearMipmap) { + var minLevel = deMath.clamp(Math.floor(minLod), minTexLevel, maxTexLevel - 1); + var maxLevel = deMath.clamp(Math.floor(maxLod), minTexLevel, maxTexLevel - 1); + + assertMsgOptions(minLevel <= maxLevel, 'Invalid texture levels.', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + var minF = deMath.clamp(minLod - level, 0, 1); + var maxF = deMath.clamp(maxLod - level, 0, 1); + + if (tcuTexCompareVerifier.isMipmapLinearCompareResultValid(texture.getLevel(level), texture.getLevel(level + 1), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, coord, 0, [minF, maxF], cmpReference, result)) + return true; + } + } else if (isNearestMipmap) { + // \note The accurate formula for nearest mipmapping is level = ceil(lod + 0.5) - 1 but Khronos has made + // decision to allow floor(lod + 0.5) as well. + var minLevel = deMath.clamp(Math.ceil(minLod + 0.5) - 1, minTexLevel, maxTexLevel); + var maxLevel = deMath.clamp(Math.floor(maxLod + 0.5), minTexLevel, maxTexLevel); + + assertMsgOptions(minLevel <= maxLevel, 'Invalid texture levels.', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + if (tcuTexCompareVerifier.isLevelCompareResultValid(texture.getLevel(level), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, coord, 0, cmpReference, result)) + return true; + } + } else { + if (tcuTexCompareVerifier.isLevelCompareResultValid(texture.getLevel(0), sampler, sampler.minFilter, prec, coord, 0, cmpReference, result)) + return true; + } + } + + return false; +}; + +/** + * @param {tcuTexture.TextureCubeView} texture + * @param {number} baseLevelNdx + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {tcuTexture.CubeFaceCoords} coords + * @param {Array<number>} fBounds vec2 + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isSeamplessLinearMipmapLinearCompareResultValid = function(texture, + baseLevelNdx, + sampler, + prec, + coords, + fBounds, + cmpReference, + result) { + var isFixedPointDepth = tcuTexCompareVerifier.isFixedPointDepthTextureFormat(texture.getLevelFace(baseLevelNdx, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X).getFormat()); + var size0 = texture.getLevelFace(baseLevelNdx, coords.face).getWidth(); + var size1 = texture.getLevelFace(baseLevelNdx + 1, coords.face).getWidth(); + + var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size0, coords.s, prec.coordBits[0], prec.uvwBits[0]); + var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size1, coords.s, prec.coordBits[0], prec.uvwBits[0]); + var vBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size0, coords.t, prec.coordBits[1], prec.uvwBits[1]); + var vBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size1, coords.t, prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinates - without wrap mode + var minI0 = Math.floor(uBounds0[0] - 0.5); + var maxI0 = Math.floor(uBounds0[1] - 0.5); + var minI1 = Math.floor(uBounds1[0] - 0.5); + var maxI1 = Math.floor(uBounds1[1] - 0.5); + var minJ0 = Math.floor(vBounds0[0] - 0.5); + var maxJ0 = Math.floor(vBounds0[1] - 0.5); + var minJ1 = Math.floor(vBounds1[0] - 0.5); + var maxJ1 = Math.floor(vBounds1[1] - 0.5); + + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faces0 = []; + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faces1 = []; + + for (var key in tcuTexture.CubeFace) { + var face = tcuTexture.CubeFace[key]; + faces0[face] = texture.getLevelFace(baseLevelNdx, face); + faces1[face] = texture.getLevelFace(baseLevelNdx + 1, face); + } + + for (var j0 = minJ0; j0 <= maxJ0; j0++) { + for (var i0 = minI0; i0 <= maxI0; i0++) { + var minA0 = deMath.clamp((uBounds0[0] - 0.5) - i0, 0, 1); + var maxA0 = deMath.clamp((uBounds0[1] - 0.5) - i0, 0, 1); + var minB0 = deMath.clamp((vBounds0[0] - 0.5) - j0, 0, 1); + var maxB0 = deMath.clamp((vBounds0[1] - 0.5) - j0, 0, 1); + var depths0 = []; + + var c00 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i0 + 0, j0 + 0]), size0); + var c10 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i0 + 1, j0 + 0]), size0); + var c01 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i0 + 0, j0 + 1]), size0); + var c11 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i0 + 1, j0 + 1]), size0); + + // If any of samples is out of both edges, implementations can do pretty much anything according to spec. + // \todo [2013-07-08 pyry] Test the special case where all corner pixels have exactly the same color. + if (c00 == null || c01 == null || c10 == null || c11 == null) + return true; + + depths0[0] = faces0[c00.face].getPixDepth(c00.s, c00.t); + depths0[1] = faces0[c10.face].getPixDepth(c10.s, c10.t); + depths0[2] = faces0[c01.face].getPixDepth(c01.s, c01.t); + depths0[3] = faces0[c11.face].getPixDepth(c11.s, c11.t); + + for (var j1 = minJ1; j1 <= maxJ1; j1++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + var minA1 = deMath.clamp((uBounds1[0] - 0.5) - i1, 0, 1); + var maxA1 = deMath.clamp((uBounds1[1] - 0.5) - i1, 0, 1); + var minB1 = deMath.clamp((vBounds1[0] - 0.5) - j1, 0, 1); + var maxB1 = deMath.clamp((vBounds1[1] - 0.5) - j1, 0, 1); + var depths1 = []; + + c00 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i1 + 0, j1 + 0]), size1); + c10 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i1 + 1, j1 + 0]), size1); + c01 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i1 + 0, j1 + 1]), size1); + c11 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i1 + 1, j1 + 1]), size1); + + if (c00 == null || c01 == null || c10 == null || c11 == null) + return true; + + depths1[0] = faces1[c00.face].getPixDepth(c00.s, c00.t); + depths1[1] = faces1[c10.face].getPixDepth(c10.s, c10.t); + depths1[2] = faces1[c01.face].getPixDepth(c01.s, c01.t); + depths1[3] = faces1[c11.face].getPixDepth(c11.s, c11.t); + + if (tcuTexCompareVerifier.isTrilinearCompareValid(sampler.compare, prec, depths0, depths1, + [minA0, maxA0], [minB0, maxB0], + [minA1, maxA1], [minB1, maxB1], + fBounds, cmpReference, result, isFixedPointDepth)) + return true; + } + } + } + } + + return false; +}; + +/** + * @param {tcuTexture.TextureCubeView} texture + * @param {number} levelNdx + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {tcuTexture.CubeFaceCoords} coords + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ + +tcuTexCompareVerifier.isSeamlessLinearCompareResultValid = function(texture, + levelNdx, + sampler, + prec, + coords, + cmpReference, + result) { + var isFixedPointDepth = tcuTexCompareVerifier.isFixedPointDepthTextureFormat(texture.getLevelFace(levelNdx, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X).getFormat()); + var size = texture.getLevelFace(levelNdx, coords.face).getWidth(); + + var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size, coords.s, prec.coordBits[0], prec.uvwBits[0]); + var vBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size, coords.t, prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinate bounds for (x0,y0) - without wrap mode + var minI = Math.floor(uBounds[0] - 0.5); + var maxI = Math.floor(uBounds[1] - 0.5); + var minJ = Math.floor(vBounds[0] - 0.5); + var maxJ = Math.floor(vBounds[1] - 0.5); + + // Face accesses + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faces = []; + + for (var key in tcuTexture.CubeFace) { + var face = tcuTexture.CubeFace[key]; + faces[face] = texture.getLevelFace(levelNdx, face); + } + + for (var j = minJ; j <= maxJ; j++) { + for (var i = minI; i <= maxI; i++) { + var c00 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i + 0, j + 0]), size); + var c10 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i + 1, j + 0]), size); + var c01 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i + 0, j + 1]), size); + var c11 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i + 1, j + 1]), size); + + // If any of samples is out of both edges, implementations can do pretty much anything according to spec. + // \todo [2013-07-08 pyry] Test the special case where all corner pixels have exactly the same color. + if (!c00 || !c01 || !c10 || !c11) + return true; + + // Bounds for filtering factors + var minA = deMath.clamp((uBounds[0] - 0.5) - i, 0, 1); + var maxA = deMath.clamp((uBounds[1] - 0.5) - i, 0, 1); + var minB = deMath.clamp((vBounds[0] - 0.5) - j, 0, 1); + var maxB = deMath.clamp((vBounds[1] - 0.5) - j, 0, 1); + + var depths = []; + depths[0] = faces[c00.face].getPixDepth(c00.s, c00.t); + depths[1] = faces[c10.face].getPixDepth(c10.s, c10.t); + depths[2] = faces[c01.face].getPixDepth(c01.s, c01.t); + depths[3] = faces[c11.face].getPixDepth(c11.s, c11.t); + + if (tcuTexCompareVerifier.isBilinearCompareValid(sampler.compare, prec, depths, [minA, maxA], [minB, maxB], cmpReference, result, isFixedPointDepth)) + return true; + } + } + + return false; +}; + +/** + * @param {tcuTexture.TextureCubeView} texture + * @param {number} levelNdx + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} filterMode + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {tcuTexture.CubeFaceCoords} coords + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isCubeLevelCompareResultValid = function(texture, + levelNdx, + sampler, + filterMode, + prec, + coords, + cmpReference, + result) { + if (filterMode == tcuTexture.FilterMode.LINEAR) { + if (sampler.seamlessCubeMap) + return tcuTexCompareVerifier.isSeamlessLinearCompareResultValid(texture, levelNdx, sampler, prec, coords, cmpReference, result); + else + return tcuTexCompareVerifier.isLinearCompareResultValid(texture.getLevelFace(levelNdx, coords.face), sampler, prec, [coords.s, coords.t], 0, cmpReference, result); + } else + return tcuTexCompareVerifier.isNearestCompareResultValid(texture.getLevelFace(levelNdx, coords.face), sampler, prec, [coords.s, coords.t], 0, cmpReference, result); +}; + +/** + * @param {tcuTexture.TextureCubeView} texture + * @param {number} baseLevelNdx + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} levelFilter + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {tcuTexture.CubeFaceCoords} coords + * @param {Array<number>} fBounds vec2 + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isCubeMipmapLinearCompareResultValid = function(texture, + baseLevelNdx, + sampler, + levelFilter, + prec, + coords, + fBounds, + cmpReference, + result) { + if (levelFilter == tcuTexture.FilterMode.LINEAR) { + if (sampler.seamlessCubeMap) + return tcuTexCompareVerifier.isSeamplessLinearMipmapLinearCompareResultValid(texture, baseLevelNdx, sampler, prec, coords, fBounds, cmpReference, result); + else + return tcuTexCompareVerifier.isLinearMipmapLinearCompareResultValid(texture.getLevelFace(baseLevelNdx, coords.face), + texture.getLevelFace(baseLevelNdx + 1, coords.face), + sampler, prec, [coords.s, coords.t], 0, fBounds, cmpReference, result); + } else + return tcuTexCompareVerifier.isNearestMipmapLinearCompareResultValid(texture.getLevelFace(baseLevelNdx, coords.face), + texture.getLevelFace(baseLevelNdx + 1, coords.face), + sampler, prec, [coords.s, coords.t], 0, fBounds, cmpReference, result); +}; + +/** + * @param {tcuTexture.TextureCubeView} texture + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec2 texture coordinates + * @param {Array<number>} lodBounds vec2 level-of-detail bounds + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isTexCompareResultValidCube = function(texture, sampler, prec, coord, lodBounds, cmpReference, result) { + /** @type {Array<tcuTexture.CubeFace>} */var possibleFaces = tcuTexVerifierUtil.getPossibleCubeFaces(coord, prec.coordBits); + + if (!possibleFaces) + return true; // Result is undefined. + + for (var tryFaceNdx = 0; tryFaceNdx < possibleFaces.length; tryFaceNdx++) { + var face = possibleFaces[tryFaceNdx]; + var faceCoords = new tcuTexture.CubeFaceCoords(face, tcuTexture.projectToFace(face, coord)); + var minLod = lodBounds[0]; + var maxLod = lodBounds[1]; + var canBeMagnified = minLod <= sampler.lodThreshold; + var canBeMinified = maxLod > sampler.lodThreshold; + + if (canBeMagnified) { + if (tcuTexCompareVerifier.isCubeLevelCompareResultValid(texture, 0, sampler, sampler.magFilter, prec, faceCoords, cmpReference, result)) + return true; + } + + if (canBeMinified) { + var isNearestMipmap = tcuTexVerifierUtil.isNearestMipmapFilter(sampler.minFilter); + var isLinearMipmap = tcuTexVerifierUtil.isLinearMipmapFilter(sampler.minFilter); + var minTexLevel = 0; + var maxTexLevel = texture.getNumLevels() - 1; + + assertMsgOptions(minTexLevel < maxTexLevel, 'Invalid texture levels.', false, true); + + if (isLinearMipmap) { + var minLevel = deMath.clamp(Math.floor(minLod), minTexLevel, maxTexLevel - 1); + var maxLevel = deMath.clamp(Math.floor(maxLod), minTexLevel, maxTexLevel - 1); + + assertMsgOptions(minLevel <= maxLevel, 'Invalid texture levels.', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + var minF = deMath.clamp(minLod - level, 0, 1); + var maxF = deMath.clamp(maxLod - level, 0, 1); + + if (tcuTexCompareVerifier.isCubeMipmapLinearCompareResultValid(texture, level, sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, faceCoords, [minF, maxF], cmpReference, result)) + return true; + } + } else if (isNearestMipmap) { + // \note The accurate formula for nearest mipmapping is level = ceil(lod + 0.5) - 1 but Khronos has made + // decision to allow floor(lod + 0.5) as well. + var minLevel = deMath.clamp(Math.ceil(minLod + 0.5) - 1, minTexLevel, maxTexLevel); + var maxLevel = deMath.clamp(Math.floor(maxLod + 0.5), minTexLevel, maxTexLevel); + + assertMsgOptions(minLevel <= maxLevel, 'Invalid texture levels.', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + if (tcuTexCompareVerifier.isCubeLevelCompareResultValid(texture, level, sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, faceCoords, cmpReference, result)) + return true; + } + } else { + if (tcuTexCompareVerifier.isCubeLevelCompareResultValid(texture, 0, sampler, sampler.minFilter, prec, faceCoords, cmpReference, result)) + return true; + } + } + } + + return false; +}; + +/** + * @param {tcuTexture.Texture2DArrayView} texture + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexCompareVerifier.TexComparePrecision} prec + * @param {Array<number>} coord vec3 texture coordinates + * @param {Array<number>} lodBounds vec2 level-of-detail bounds + * @param {number} cmpReference + * @param {number} result + * @return {boolean} + */ +tcuTexCompareVerifier.isTexCompareResultValid2DArray = function(texture, sampler, prec, coord, lodBounds, cmpReference, result) { + var depthErr = tcuTexVerifierUtil.computeFloatingPointError(coord[2], prec.coordBits[2]) + tcuTexVerifierUtil.computeFixedPointError(prec.uvwBits[2]); + var minZ = coord[2] - depthErr; + var maxZ = coord[2] + depthErr; + var minLayer = deMath.clamp(Math.floor(minZ + 0.5), 0, texture.getNumLayers() - 1); + var maxLayer = deMath.clamp(Math.floor(maxZ + 0.5), 0, texture.getNumLayers() - 1); + + for (var layer = minLayer; layer <= maxLayer; layer++) { + var minLod = lodBounds[0]; + var maxLod = lodBounds[1]; + var canBeMagnified = minLod <= sampler.lodThreshold; + var canBeMinified = maxLod > sampler.lodThreshold; + + if (canBeMagnified) { + if (tcuTexCompareVerifier.isLevelCompareResultValid(texture.getLevel(0), sampler, sampler.magFilter, prec, deMath.swizzle(coord, [0, 1]), layer, cmpReference, result)) + return true; + } + + if (canBeMinified) { + var isNearestMipmap = tcuTexVerifierUtil.isNearestMipmapFilter(sampler.minFilter); + var isLinearMipmap = tcuTexVerifierUtil.isLinearMipmapFilter(sampler.minFilter); + var minTexLevel = 0; + var maxTexLevel = texture.getNumLevels() - 1; + + assertMsgOptions(minTexLevel < maxTexLevel, 'Invalid texture levels.', false, true); + + if (isLinearMipmap) { + var minLevel = deMath.clamp(Math.floor(minLod), minTexLevel, maxTexLevel - 1); + var maxLevel = deMath.clamp(Math.floor(maxLod), minTexLevel, maxTexLevel - 1); + + assertMsgOptions(minLevel <= maxLevel, 'Invalid texture levels.', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + var minF = deMath.clamp(minLod - level, 0, 1); + var maxF = deMath.clamp(maxLod - level, 0, 1); + + if (tcuTexCompareVerifier.isMipmapLinearCompareResultValid(texture.getLevel(level), texture.getLevel(level + 1), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, deMath.swizzle(coord, [0, 1]), layer, [minF, maxF], cmpReference, result)) + return true; + } + } else if (isNearestMipmap) { + // \note The accurate formula for nearest mipmapping is level = ceil(lod + 0.5) - 1 but Khronos has made + // decision to allow floor(lod + 0.5) as well. + var minLevel = deMath.clamp(Math.ceil(minLod + 0.5) - 1, minTexLevel, maxTexLevel); + var maxLevel = deMath.clamp(Math.floor(maxLod + 0.5), minTexLevel, maxTexLevel); + + assertMsgOptions(minLevel <= maxLevel, 'Invalid texture levels.', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + if (tcuTexCompareVerifier.isLevelCompareResultValid(texture.getLevel(level), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, deMath.swizzle(coord, [0, 1]), layer, cmpReference, result)) + return true; + } + } else { + if (tcuTexCompareVerifier.isLevelCompareResultValid(texture.getLevel(0), sampler, sampler.minFilter, prec, deMath.swizzle(coord, [0, 1]), layer, cmpReference, result)) + return true; + } + } + } + + return false; +}; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexLookupVerifier.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexLookupVerifier.js new file mode 100644 index 000000000..6b471998a --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexLookupVerifier.js @@ -0,0 +1,2225 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuTexLookupVerifier'); +goog.require('framework.common.tcuTexVerifierUtil'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.common.tcuTextureUtil'); +goog.require('framework.delibs.debase.deMath'); + +goog.scope(function() { + + var tcuTexLookupVerifier = framework.common.tcuTexLookupVerifier; + var tcuTexture = framework.common.tcuTexture; + var tcuTextureUtil = framework.common.tcuTextureUtil; + var tcuTexVerifierUtil = framework.common.tcuTexVerifierUtil; + var deMath = framework.delibs.debase.deMath; + + /** @typedef {(tcuTexLookupVerifier.LookupPrecision|{tcuTexLookupVerifier.LookupPrecision})} */ + tcuTexLookupVerifier.PrecType; + + /** + * Generic lookup precision parameters + * @constructor + * @struct + * @param {Array<number>=} coordBits + * @param {Array<number>=} uvwBits + * @param {Array<number>=} colorThreshold + * @param {Array<boolean>=} colorMask + */ + tcuTexLookupVerifier.LookupPrecision = function(coordBits, uvwBits, colorThreshold, colorMask) { + /** @type {Array<number>} */ this.coordBits = coordBits || [22, 22, 22]; + /** @type {Array<number>} */ this.uvwBits = uvwBits || [16, 16, 16]; + /** @type {Array<number>} */ this.colorThreshold = colorThreshold || [0, 0, 0, 0]; + /** @type {Array<boolean>} */ this.colorMask = colorMask || [true, true, true, true]; + }; + + /** + * Lod computation precision parameters + * @constructor + * @struct + * @param {number=} derivateBits + * @param {number=} lodBits + */ + tcuTexLookupVerifier.LodPrecision = function(derivateBits, lodBits) { + /** @type {number} */ this.derivateBits = derivateBits === undefined ? 22 : derivateBits; + /** @type {number} */ this.lodBits = lodBits === undefined ? 16 : lodBits; + }; + + /** + * @enum {number} + */ + tcuTexLookupVerifier.TexLookupScaleMode = { + MINIFY: 0, + MAGNIFY: 1 + }; + + // Generic utilities + + /** + * @param {tcuTexture.Sampler} sampler + * @return {boolean} + */ + tcuTexLookupVerifier.isSamplerSupported = function(sampler) { + return sampler.compare == tcuTexture.CompareMode.COMPAREMODE_NONE && + tcuTexVerifierUtil.isWrapModeSupported(sampler.wrapS) && + tcuTexVerifierUtil.isWrapModeSupported(sampler.wrapT) && + tcuTexVerifierUtil.isWrapModeSupported(sampler.wrapR); + }; + + // Color read & compare utilities + + /** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {number} x + * @param {number} y + * @param {number} z + * @return {boolean} + */ + tcuTexLookupVerifier.coordsInBounds = function(access, x, y, z) { + return deMath.deInBounds32(x, 0, access.getWidth()) && deMath.deInBounds32(y, 0, access.getHeight()) && deMath.deInBounds32(z, 0, access.getDepth()); + }; + + /** + * @param {tcuTexture.TextureFormat} format + * @return {boolean} + */ + tcuTexLookupVerifier.isSRGB = function(format) { + return format.order == tcuTexture.ChannelOrder.sRGB || format.order == tcuTexture.ChannelOrder.sRGBA; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {number} i + * @param {number} j + * @param {number} k + * @return {Array<number>} + */ + tcuTexLookupVerifier.lookupScalar = function(access, sampler, i, j, k) { + if (tcuTexLookupVerifier.coordsInBounds(access, i, j, k)) + return access.getPixel(i, j, k); + else + return deMath.toIVec(sampler.borderColor); + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {number} i + * @param {number} j + * @param {number} k + * @return {Array<number>} + */ + tcuTexLookupVerifier.lookupFloat = function(access, sampler, i, j, k) { + // Specialization for float lookups: sRGB conversion is performed as specified in format. + if (tcuTexLookupVerifier.coordsInBounds(access, i, j, k)) { + /** @type {Array<number>} */ var p = access.getPixel(i, j, k); + return tcuTexLookupVerifier.isSRGB(access.getFormat()) ? tcuTextureUtil.sRGBToLinear(p) : p; + } else + return sampler.borderColor; + }; + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} ref + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isColorValid = function(prec, ref, result) { + return deMath.boolAll( + deMath.logicalOrBool( + deMath.lessThanEqual(deMath.absDiff(ref, result), prec.colorThreshold), + deMath.logicalNotBool(prec.colorMask))); + }; + + /** + * @constructor + * @struct + * @param {Array<number>=} p00 + * @param {Array<number>=} p01 + * @param {Array<number>=} p10 + * @param {Array<number>=} p11 + */ + tcuTexLookupVerifier.ColorQuad = function(p00, p01, p10, p11) { + /** @type {Array<number>} */ this.p00 = p00 || null; //!< (0, 0) + /** @type {Array<number>} */ this.p01 = p01 || null; //!< (1, 0) + /** @type {Array<number>} */ this.p10 = p10 || null; //!< (0, 1) + /** @type {Array<number>} */ this.p11 = p11 || null; //!< (1, 1) + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {number} x0 + * @param {number} x1 + * @param {number} y0 + * @param {number} y1 + * @param {number} z + * @return {tcuTexLookupVerifier.ColorQuad} + */ + tcuTexLookupVerifier.lookupQuad = function(level, sampler, x0, x1, y0, y1, z) { + var p00 = tcuTexLookupVerifier.lookupFloat(level, sampler, x0, y0, z); + var p10 = tcuTexLookupVerifier.lookupFloat(level, sampler, x1, y0, z); + var p01 = tcuTexLookupVerifier.lookupFloat(level, sampler, x0, y1, z); + var p11 = tcuTexLookupVerifier.lookupFloat(level, sampler, x1, y1, z); + return new tcuTexLookupVerifier.ColorQuad(p00, p01, p10, p11); + }; + + /** + * @constructor + * @struct + * @param {Array<number>=} p0 + * @param {Array<number>=} p1 + */ + tcuTexLookupVerifier.ColorLine = function(p0, p1) { + /** @type {Array<number>} */ this.p0 = p0 || null; //!< 0 + /** @type {Array<number>} */ this.p1 = p1 || null; //!< 1 + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {number} x0 + * @param {number} x1 + * @param {number} y + * @return {tcuTexLookupVerifier.ColorLine} + */ + tcuTexLookupVerifier.lookupLine = function(level, sampler, x0, x1, y) { + return new tcuTexLookupVerifier.ColorLine( + tcuTexLookupVerifier.lookupFloat(level, sampler, x0, y, 0), + tcuTexLookupVerifier.lookupFloat(level, sampler, x1, y, 0) + ); + }; + + /** + * @param {Array<number>} vec + * @return {number} + */ + tcuTexLookupVerifier.minComp = function(vec) { + /** @type {number} */ var minVal = vec[0]; + for (var ndx = 1; ndx < vec.length; ndx++) + minVal = Math.min(minVal, vec[ndx]); + return minVal; + }; + + /** + * @param {Array<number>} vec + * @return {number} + */ + tcuTexLookupVerifier.maxComp = function(vec) { + /** @type {number} */ var maxVal = vec[0]; + for (var ndx = 1; ndx < vec.length; ndx++) + maxVal = Math.max(maxVal, vec[ndx]); + return maxVal; + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorLine} line + * @return {number} + */ + tcuTexLookupVerifier.computeBilinearSearchStepFromFloatLine = function(prec, line) { + assertMsgOptions(deMath.boolAll(deMath.greaterThan(prec.colorThreshold, [0, 0, 0, 0])), 'Threshold not greater than 0.', false, true); + + /** @type {number} */ var maxSteps = 1 << 16; + /** @type {Array<number>} */ var d = deMath.absDiff(line.p1, line.p0); + /** @type {Array<number>} */ var stepCount = deMath.divide([d, d, d, d], prec.colorThreshold); + /** @type {Array<number>} */ + var minStep = deMath.divide([1, 1, 1, 1], deMath.add(stepCount, [1, 1, 1, 1])); + /** @type {number} */ var step = Math.max(tcuTexLookupVerifier.minComp(minStep), 1 / maxSteps); + + return step; + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorQuad} quad + * @return {number} + */ + tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad = function(prec, quad) { + assertMsgOptions(deMath.boolAll(deMath.greaterThan(prec.colorThreshold, [0, 0, 0, 0])), 'Threshold not greater than 0.', false, true); + + /** @type {number} */ var maxSteps = 1 << 16; + /** @type {Array<number>} */ var d0 = deMath.absDiff(quad.p10, quad.p00); + /** @type {Array<number>} */ var d1 = deMath.absDiff(quad.p01, quad.p00); + /** @type {Array<number>} */ var d2 = deMath.absDiff(quad.p11, quad.p10); + /** @type {Array<number>} */ var d3 = deMath.absDiff(quad.p11, quad.p01); + /** @type {Array<number>} */ var maxD = deMath.max(d0, deMath.max(d1, deMath.max(d2, d3))); + /** @type {Array<number>} */ var stepCount = deMath.divide(maxD, prec.colorThreshold); + /** @type {Array<number>} */ var minStep = deMath.divide([1, 1, 1, 1], deMath.add(stepCount, [1, 1, 1, 1])); + /** @type {number} */ var step = Math.max(tcuTexLookupVerifier.minComp(minStep), 1 / maxSteps); + + return step; + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @return {number} + */ + tcuTexLookupVerifier.computeBilinearSearchStepForUnorm = function(prec) { + assertMsgOptions(deMath.boolAll(deMath.greaterThan(prec.colorThreshold, [0, 0, 0, 0])), 'Threshold not greater than 0.', false, true); + + /** @type {Array<number>} */ var stepCount = deMath.divide([1, 1, 1, 1], prec.colorThreshold); + /** @type {Array<number>} */ var minStep = deMath.divide([1, 1, 1, 1], (deMath.add(stepCount, [1, 1, 1, 1]))); + /** @type {number} */ var step = tcuTexLookupVerifier.minComp(minStep); + + return step; + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @return {number} + */ + tcuTexLookupVerifier.computeBilinearSearchStepForSnorm = function(prec) { + assertMsgOptions(deMath.boolAll(deMath.greaterThan(prec.colorThreshold, [0, 0, 0, 0])), 'Threshold not greater than 0.', false, true); + + /** @type {Array<number>} */ var stepCount = deMath.divide([2.0, 2.0, 2.0, 2.0], prec.colorThreshold); + /** @type {Array<number>} */ var minStep = deMath.divide([1, 1, 1, 1], deMath.add(stepCount, [1, 1, 1, 1])); + /** @type {number} */ var step = tcuTexLookupVerifier.minComp(minStep); + + return step; + }; + + /** + * @param {tcuTexLookupVerifier.ColorLine} line + * @return {Array<number>} + */ + tcuTexLookupVerifier.minLine = function(line) { + return deMath.min(line.p0, line.p1); + }; + + /** + * @param {tcuTexLookupVerifier.ColorLine} line + * @return {Array<number>} + */ + tcuTexLookupVerifier.maxLine = function(line) { + var max = deMath.max; + return max(line.p0, line.p1); + }; + + /** + * @param {tcuTexLookupVerifier.ColorQuad} quad + * @return {Array<number>} + */ + tcuTexLookupVerifier.minQuad = function(quad) { + var min = deMath.min; + return min(quad.p00, min(quad.p10, min(quad.p01, quad.p11))); + }; + + /** + * @param {tcuTexLookupVerifier.ColorQuad} quad + * @return {Array<number>} + */ + tcuTexLookupVerifier.maxQuad = function(quad) { + var max = deMath.max; + return max(quad.p00, max(quad.p10, max(quad.p01, quad.p11))); + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorQuad} quad + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isInColorBounds_1Quad = function(prec, quad, result) { + var quadMin = tcuTexLookupVerifier.minQuad; + var quadMax = tcuTexLookupVerifier.maxQuad; + /** @type {Array<number>} */ var minVal = deMath.subtract(quadMin(quad), prec.colorThreshold); + /** @type {Array<number>} */ var maxVal = deMath.add(quadMax(quad), prec.colorThreshold); + return deMath.boolAll( + deMath.logicalOrBool( + deMath.logicalAndBool( + deMath.greaterThanEqual(result, minVal), + deMath.lessThanEqual(result, maxVal)), + deMath.logicalNotBool(prec.colorMask))); + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorQuad} quad0 + * @param {tcuTexLookupVerifier.ColorQuad} quad1 + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isInColorBounds_2Quad = function(prec, quad0, quad1, result) { + var min = deMath.min; + var max = deMath.max; + var quadMin = tcuTexLookupVerifier.minQuad; + var quadMax = tcuTexLookupVerifier.maxQuad; + /** @type {Array<number>} */ var minVal = deMath.subtract(min(quadMin(quad0), quadMin(quad1)), prec.colorThreshold); + /** @type {Array<number>} */ var maxVal = deMath.add(max(quadMax(quad0), quadMax(quad1)), prec.colorThreshold); + return deMath.boolAll( + deMath.logicalOrBool( + deMath.logicalAndBool( + deMath.greaterThanEqual(result, minVal), + deMath.lessThanEqual(result, maxVal)), + deMath.logicalNotBool(prec.colorMask))); + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorLine} line0 + * @param {tcuTexLookupVerifier.ColorLine} line1 + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isInColorBounds_2Line = function(prec, line0, line1, result) { + var min = deMath.min; + var max = deMath.max; + var lineMin = tcuTexLookupVerifier.minLine; + var lineMax = tcuTexLookupVerifier.maxLine; + /** @type {Array<number>} */ var minVal = deMath.subtract(min(lineMin(line0), lineMin(line1)), prec.colorThreshold); + /** @type {Array<number>} */ var maxVal = deMath.add(max(lineMax(line0), lineMax(line1)), prec.colorThreshold); + return deMath.boolAll( + deMath.logicalOrBool( + deMath.logicalAndBool( + deMath.greaterThanEqual(result, minVal), + deMath.lessThanEqual(result, maxVal)), + deMath.logicalNotBool(prec.colorMask))); + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorQuad} quad00 + * @param {tcuTexLookupVerifier.ColorQuad} quad01 + * @param {tcuTexLookupVerifier.ColorQuad} quad10 + * @param {tcuTexLookupVerifier.ColorQuad} quad11 + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isInColorBounds_4Quad = function(prec, quad00, quad01, quad10, quad11, result) { + var min = deMath.min; + var max = deMath.max; + var quadMin = tcuTexLookupVerifier.minQuad; + var quadMax = tcuTexLookupVerifier.maxQuad; + /** @type {Array<number>} */ var minVal = deMath.subtract(min(quadMin(quad00), min(quadMin(quad01), min(quadMin(quad10), quadMin(quad11)))), prec.colorThreshold); + /** @type {Array<number>} */ var maxVal = deMath.add(max(quadMax(quad00), max(quadMax(quad01), max(quadMax(quad10), quadMax(quad11)))), prec.colorThreshold); + return deMath.boolAll( + deMath.logicalOrBool( + deMath.logicalAndBool( + deMath.greaterThanEqual(result, minVal), + deMath.lessThanEqual(result, maxVal)), + deMath.logicalNotBool(prec.colorMask))); + }; + + // Range search utilities + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} c0 + * @param {Array<number>} c1 + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLinearRangeValid = function(prec, c0, c1, fBounds, result) { + // This is basically line segment - AABB test. Valid interpolation line is checked + // against result AABB constructed by applying threshold. + + /** @type {Array<number>} */ var rMin = deMath.subtract(result, prec.colorThreshold); + /** @type {Array<number>} */ var rMax = deMath.add(result, prec.colorThreshold); + + // Algorithm: For each component check whether segment endpoints are inside, or intersect with slab. + // If all intersect or are inside, line segment intersects the whole 4D AABB. + for (var compNdx = 0; compNdx < 4; compNdx++) { + if (!prec.colorMask[compNdx]) + continue; + + /** @type {number} */ var i0 = c0[compNdx] * (1 - fBounds[0]) + c1[compNdx] * fBounds[0]; + /** @type {number} */ var i1 = c0[compNdx] * (1 - fBounds[1]) + c1[compNdx] * fBounds[1]; + if ((i0 > rMax[compNdx] && i1 > rMax[compNdx]) || + (i0 < rMin[compNdx] && i1 < rMin[compNdx])) { + return false; + } + } + + return true; + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorQuad} quad + * @param {Array<number>} xBounds + * @param {Array<number>} yBounds + * @param {number} searchStep + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isBilinearRangeValid = function(prec, quad, xBounds, yBounds, searchStep, result) { + assertMsgOptions(xBounds[0] <= xBounds[1], 'Out of bounds: X direction.', false, true); + assertMsgOptions(yBounds[0] <= yBounds[1], 'Out of bounds: Y direction.', false, true); + + if (!tcuTexLookupVerifier.isInColorBounds_1Quad(prec, quad, result)) + return false; + + for (var x = xBounds[0]; x < xBounds[1] + searchStep; x += searchStep) { + /** @type {number} */ var a = Math.min(x, xBounds[1]); + /** @type {Array<number>} */ var c0 = deMath.add(deMath.scale(quad.p00, (1 - a)), deMath.scale(quad.p10, a)); + /** @type {Array<number>} */ var c1 = deMath.add(deMath.scale(quad.p01, (1 - a)), deMath.scale(quad.p11, a)); + + if (tcuTexLookupVerifier.isLinearRangeValid(prec, c0, c1, yBounds, result)) + return true; + } + + return false; + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorQuad} quad0 + * @param {tcuTexLookupVerifier.ColorQuad} quad1 + * @param {Array<number>} xBounds + * @param {Array<number>} yBounds + * @param {Array<number>} zBounds + * @param {number} searchStep + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isTrilinearRangeValid = function(prec, quad0, quad1, xBounds, yBounds, zBounds, searchStep, result) { + assertMsgOptions(xBounds[0] <= xBounds[1], 'Out of bounds: X direction.', false, true); + assertMsgOptions(yBounds[0] <= yBounds[1], 'Out of bounds: Y direction.', false, true); + assertMsgOptions(zBounds[0] <= zBounds[1], 'Out of bounds: Z direction.', false, true); + + if (!tcuTexLookupVerifier.isInColorBounds_2Quad(prec, quad0, quad1, result)) + return false; + + for (var x = xBounds[0]; x < xBounds[1] + searchStep; x += searchStep) { + for (var y = yBounds[0]; y < yBounds[1] + searchStep; y += searchStep) { + /** @type {number} */ var a = Math.min(x, xBounds[1]); + /** @type {number} */ var b = Math.min(y, yBounds[1]); + /** @type {Array<number>} */ + var c0 = deMath.add( + deMath.add( + deMath.add( + deMath.scale(quad0.p00, (1 - a) * (1 - b)), + deMath.scale(quad0.p10, a * (1 - b))), + deMath.scale(quad0.p01, (1 - a) * b)), + deMath.scale(quad0.p11, a * b)); + /** @type {Array<number>} */ + var c1 = deMath.add( + deMath.add( + deMath.add( + deMath.scale(quad1.p00, (1 - a) * (1 - b)), + deMath.scale(quad1.p10, a * (1 - b))), + deMath.scale(quad1.p01, (1 - a) * b)), + deMath.scale(quad1.p11, a * b)); + + if (tcuTexLookupVerifier.isLinearRangeValid(prec, c0, c1, zBounds, result)) + return true; + } + } + + return false; + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorQuad} quad0 + * @param {tcuTexLookupVerifier.ColorQuad} quad1 + * @param {Array<number>} xBounds0 + * @param {Array<number>} yBounds0 + * @param {Array<number>} xBounds1 + * @param {Array<number>} yBounds1 + * @param {Array<number>} zBounds + * @param {number} searchStep + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.is2DTrilinearFilterResultValid = function(prec, quad0, quad1, xBounds0, yBounds0, xBounds1, yBounds1, zBounds, searchStep, result) { + assertMsgOptions(xBounds0[0] <= xBounds0[1], 'Out of bounds: X direction.', false, true); + assertMsgOptions(yBounds0[0] <= yBounds0[1], 'Out of bounds: Y direction.', false, true); + assertMsgOptions(xBounds1[0] <= xBounds1[1], 'Out of bounds: X direction.', false, true); + assertMsgOptions(yBounds1[0] <= yBounds1[1], 'Out of bounds: Y direction.', false, true); + + if (!tcuTexLookupVerifier.isInColorBounds_2Quad(prec, quad0, quad1, result)) + return false; + + for (var x0 = xBounds0[0]; x0 < xBounds0[1] + searchStep; x0 += searchStep) { + for (var y0 = yBounds0[0]; y0 < yBounds0[1] + searchStep; y0 += searchStep) { + /** @type {number} */ var a0 = Math.min(x0, xBounds0[1]); + /** @type {number} */ var b0 = Math.min(y0, yBounds0[1]); + /** @type {Array<number>} */ + var c0 = deMath.add( + deMath.add( + deMath.add( + deMath.scale(quad0.p00, (1 - a0) * (1 - b0)), + deMath.scale(quad0.p10, a0 * (1 - b0))), + deMath.scale(quad0.p01, (1 - a0) * b0)), + deMath.scale(quad0.p11, a0 * b0)); + + for (var x1 = xBounds1[0]; x1 <= xBounds1[1]; x1 += searchStep) { + for (var y1 = yBounds1[0]; y1 <= yBounds1[1]; y1 += searchStep) { + /** @type {number} */ var a1 = Math.min(x1, xBounds1[1]); + /** @type {number} */ var b1 = Math.min(y1, yBounds1[1]); + /** @type {Array<number>} */ + var c1 = deMath.add( + deMath.add( + deMath.add( + deMath.scale(quad1.p00, (1 - a1) * (1 - b1)), + deMath.scale(quad1.p10, a1 * (1 - b1))), + deMath.scale(quad1.p01, (1 - a1) * b1)), + deMath.scale(quad1.p11, a1 * b1)); + + if (tcuTexLookupVerifier.isLinearRangeValid(prec, c0, c1, zBounds, result)) + return true; + } + } + } + } + + return false; + }; + + /** + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexLookupVerifier.ColorQuad} quad00 + * @param {tcuTexLookupVerifier.ColorQuad} quad01 + * @param {tcuTexLookupVerifier.ColorQuad} quad10 + * @param {tcuTexLookupVerifier.ColorQuad} quad11 + * @param {Array<number>} xBounds0 + * @param {Array<number>} yBounds0 + * @param {Array<number>} zBounds0 + * @param {Array<number>} xBounds1 + * @param {Array<number>} yBounds1 + * @param {Array<number>} zBounds1 + * @param {Array<number>} wBounds + * @param {number} searchStep + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.is3DTrilinearFilterResultValid = function(prec, quad00, quad01, quad10, quad11, xBounds0, yBounds0, zBounds0, xBounds1, yBounds1, zBounds1, wBounds, searchStep, result) { + assertMsgOptions(xBounds0[0] <= xBounds0[1], 'Out of bounds: X direction.', false, true); + assertMsgOptions(yBounds0[0] <= yBounds0[1], 'Out of bounds: Y direction.', false, true); + assertMsgOptions(zBounds0[0] <= zBounds0[1], 'Out of bounds: Z direction.', false, true); + assertMsgOptions(xBounds1[0] <= xBounds1[1], 'Out of bounds: X direction.', false, true); + assertMsgOptions(yBounds1[0] <= yBounds1[1], 'Out of bounds: Y direction.', false, true); + assertMsgOptions(zBounds1[0] <= zBounds1[1], 'Out of bounds: Z direction.', false, true); + + if (!tcuTexLookupVerifier.isInColorBounds_4Quad(prec, quad00, quad01, quad10, quad11, result)) + return false; + + function biInterp(result, p00, p01, p10, p11, s00, s01, s10, s11) { + for (var ii = 0; ii < 4; ++ii) { + result[ii] = p00[ii] * s00 + p10[ii] * s10 + p01[ii] * s01 + p11[ii] * s11; + } + } + + function interp(result, p0, p1, s) { + for (var ii = 0; ii < 4; ++ii) { + result[ii] = p0[ii] * (1 - s) + p1[ii] * s; + } + } + + /** @type {Array<number>} */ var c00 = [0, 0, 0, 0]; + /** @type {Array<number>} */ var c01 = [0, 0, 0, 0]; + /** @type {Array<number>} */ var c10 = [0, 0, 0, 0]; + /** @type {Array<number>} */ var c11 = [0, 0, 0, 0]; + /** @type {Array<number>} */ var cz0 = [0, 0, 0, 0]; + /** @type {Array<number>} */ var cz1 = [0, 0, 0, 0]; + + for (var x0 = xBounds0[0]; x0 < xBounds0[1] + searchStep; x0 += searchStep) { + for (var y0 = yBounds0[0]; y0 < yBounds0[1] + searchStep; y0 += searchStep) { + /** @type {number} */ var a0 = Math.min(x0, xBounds0[1]); + /** @type {number} */ var b0 = Math.min(y0, yBounds0[1]); + + /** @type {number} */ var s00 = (1 - a0) * (1 - b0); + /** @type {number} */ var s01 = (1 - a0) * b0; + /** @type {number} */ var s10 = a0 * (1 - b0); + /** @type {number} */ var s11 = a0 * b0; + + biInterp(c00, quad00.p00, quad00.p01, quad00.p10, quad00.p11, s00, s01, s10, s11); + biInterp(c01, quad01.p00, quad01.p01, quad01.p10, quad01.p11, s00, s01, s10, s11); + + for (var z0 = zBounds0[0]; z0 < zBounds0[1] + searchStep; z0 += searchStep) { + /** @type {number} */ var c0 = Math.min(z0, zBounds0[1]); + interp(cz0, c00, c01, c0); + + for (var x1 = xBounds1[0]; x1 < xBounds1[1] + searchStep; x1 += searchStep) { + for (var y1 = yBounds1[0]; y1 < yBounds1[1] + searchStep; y1 += searchStep) { + /** @type {number} */ var a1 = Math.min(x1, xBounds1[1]); + /** @type {number} */ var b1 = Math.min(y1, yBounds1[1]); + + /** @type {number} */ var t00 = (1 - a1) * (1 - b1); + /** @type {number} */ var t01 = (1 - a1) * b1; + /** @type {number} */ var t10 = a1 * (1 - b1); + /** @type {number} */ var t11 = a1 * b1; + + biInterp(c10, quad10.p00, quad10.p01, quad10.p10, quad10.p11, t00, t01, t10, t11); + biInterp(c11, quad11.p00, quad11.p01, quad11.p10, quad11.p11, t00, t01, t10, t11); + + for (var z1 = zBounds1[0]; z1 < zBounds1[1] + searchStep; z1 += searchStep) { + /** @type {number} */ var c1 = Math.min(z1, zBounds1[1]); + interp(cz1, c10, c11, c1); + + if (tcuTexLookupVerifier.isLinearRangeValid(prec, cz0, cz1, wBounds, result)) + return true; + } + } + } + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {number} coordX + * @param {number} coordY + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isNearestSampleResultValid_CoordXYAsNumber = function(level, sampler, prec, coordX, coordY, result) { + assertMsgOptions(level.getDepth() == 1, 'Depth must be 1.', false, true); + + /** @type {Array<number>} */ + var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getWidth(), coordX, prec.coordBits[0], prec.uvwBits[0]); + + /** @type {number} */ var minI = Math.floor(uBounds[0]); + /** @type {number} */ var maxI = Math.floor(uBounds[1]); + + for (var i = minI; i <= maxI; i++) { + /** @type {number} */ var x = tcuTexVerifierUtil.wrap(sampler.wrapS, i, level.getWidth()); + /** @type {Array<number>} */ var color; + if (tcuTexLookupVerifier.isSRGB(level.getFormat())) { + color = tcuTexLookupVerifier.lookupFloat(level, sampler, x, coordY, 0); + } else { + color = tcuTexLookupVerifier.lookupScalar(level, sampler, x, coordY, 0); + } + + if (tcuTexLookupVerifier.isColorValid(prec, color, result)) + return true; + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord vec2 + * @param {number} coordZ int + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isNearestSampleResultValid_CoordAsVec2AndInt = function(level, sampler, prec, coord, coordZ, result) { + /** @type {Array<number>} */ + var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getWidth(), coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var vBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getHeight(), coord[1], prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinates - without wrap mode + /** @type {number} */ var minI = Math.floor(uBounds[0]); + /** @type {number} */ var maxI = Math.floor(uBounds[1]); + /** @type {number} */ var minJ = Math.floor(vBounds[0]); + /** @type {number} */ var maxJ = Math.floor(vBounds[1]); + + // \todo [2013-07-03 pyry] This could be optimized by first computing ranges based on wrap mode. + + for (var j = minJ; j <= maxJ; j++) + for (var i = minI; i <= maxI; i++) { + /** @type {number} */ var x = tcuTexVerifierUtil.wrap(sampler.wrapS, i, level.getWidth()); + /** @type {number} */ var y = tcuTexVerifierUtil.wrap(sampler.wrapT, j, level.getHeight()); + /** @type {Array<number>} */ var color; + if (tcuTexLookupVerifier.isSRGB(level.getFormat())) { + color = tcuTexLookupVerifier.lookupFloat(level, sampler, x, y, coordZ); + } else { + color = tcuTexLookupVerifier.lookupScalar(level, sampler, x, y, coordZ); + } + + if (tcuTexLookupVerifier.isColorValid(prec, color, result)) + return true; + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord vec3 + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isNearestSampleResultValid_CoordAsVec3 = function(level, sampler, prec, coord, result) { + /** @type {Array<number>} */ + var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getWidth(), coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var vBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getHeight(), coord[1], prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ + var wBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getDepth(), coord[2], prec.coordBits[2], prec.uvwBits[2]); + + // Integer coordinates - without wrap mode + /** @type {number} */ var minI = Math.floor(uBounds[0]); + /** @type {number} */ var maxI = Math.floor(uBounds[1]); + /** @type {number} */ var minJ = Math.floor(vBounds[0]); + /** @type {number} */ var maxJ = Math.floor(vBounds[1]); + /** @type {number} */ var minK = Math.floor(wBounds[0]); + /** @type {number} */ var maxK = Math.floor(wBounds[1]); + + // \todo [2013-07-03 pyry] This could be optimized by first computing ranges based on wrap mode. + + for (var k = minK; k <= maxK; k++) { + for (var j = minJ; j <= maxJ; j++) { + for (var i = minI; i <= maxI; i++) { + /** @type {number} */ var x = tcuTexVerifierUtil.wrap(sampler.wrapS, i, level.getWidth()); + /** @type {number} */ var y = tcuTexVerifierUtil.wrap(sampler.wrapT, j, level.getHeight()); + /** @type {number} */ var z = tcuTexVerifierUtil.wrap(sampler.wrapR, k, level.getDepth()); + /** @type {Array<number>} */ var color; + if (tcuTexLookupVerifier.isSRGB(level.getFormat())) { + color = tcuTexLookupVerifier.lookupFloat(level, sampler, x, y, z); + } else { + color = tcuTexLookupVerifier.lookupScalar(level, sampler, x, y, z); + } + + if (tcuTexLookupVerifier.isColorValid(prec, color, result)) + return true; + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {number} coordX + * @param {number} coordY + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLinearSampleResultValid_CoordXYAsNumber = function(level, sampler, prec, coordX, coordY, result) { + /** @type {Array<number>} */ var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, level.getWidth(), coordX, prec.coordBits[0], prec.uvwBits[0]); + + /** @type {number} */ var minI = Math.floor(uBounds[0] - 0.5); + /** @type {number} */ var maxI = Math.floor(uBounds[1] - 0.5); + + /** @type {number} */ var w = level.getWidth(); + + for (var i = minI; i <= maxI; i++) { + // Wrapped coordinates + /** @type {number} */ var x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i, w); + /** @type {number} */ var x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i + 1, w); + + // Bounds for filtering factors + /** @type {number} */ var minA = deMath.clamp((uBounds[0] - 0.5) - i, 0, 1); + /** @type {number} */ var maxA = deMath.clamp((uBounds[1] - 0.5) - i, 0, 1); + + /** @type {Array<number>} */ var colorA = tcuTexLookupVerifier.lookupFloat(level, sampler, x0, coordY, 0); + /** @type {Array<number>} */ var colorB = tcuTexLookupVerifier.lookupFloat(level, sampler, x1, coordY, 0); + + if (tcuTexLookupVerifier.isLinearRangeValid(prec, colorA, colorB, [minA, maxA], result)) + return true; + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord vec2 + * @param {number} coordZ int + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLinearSampleResultValid_CoordAsVec2AndInt = function(level, sampler, prec, coord, coordZ, result) { + /** @type {Array<number>} */ var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, level.getWidth(), coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ var vBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, level.getHeight(), coord[1], prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinate bounds for (x0,y0) - without wrap mode + /** @type {number} */ var minI = Math.floor(uBounds[0] - 0.5); + /** @type {number} */ var maxI = Math.floor(uBounds[1] - 0.5); + /** @type {number} */ var minJ = Math.floor(vBounds[0] - 0.5); + /** @type {number} */ var maxJ = Math.floor(vBounds[1] - 0.5); + + /** @type {number} */ var w = level.getWidth(); + /** @type {number} */ var h = level.getHeight(); + + /** @type {tcuTexture.TextureChannelClass} */ + var texClass = tcuTexture.getTextureChannelClass(level.getFormat().type); + + /** @type {number} */ + var searchStep = (texClass == tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForUnorm(prec) : + (texClass == tcuTexture.TextureChannelClass.SIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForSnorm(prec) : + 0; // Step is computed for floating-point quads based on texel values. + + // \todo [2013-07-03 pyry] This could be optimized by first computing ranges based on wrap mode. + + for (var j = minJ; j <= maxJ; j++) + for (var i = minI; i <= maxI; i++) { + // Wrapped coordinates + /** @type {number} */ var x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i, w); + /** @type {number} */ var x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i + 1, w); + /** @type {number} */ var y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j, h); + /** @type {number} */ var y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j + 1, h); + + // Bounds for filtering factors + /** @type {number} */ var minA = deMath.clamp((uBounds[0] - 0.5) - i, 0, 1); + /** @type {number} */ var maxA = deMath.clamp((uBounds[1] - 0.5) - i, 0, 1); + /** @type {number} */ var minB = deMath.clamp((vBounds[0] - 0.5) - j, 0, 1); + /** @type {number} */ var maxB = deMath.clamp((vBounds[1] - 0.5) - j, 0, 1); + + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad = tcuTexLookupVerifier.lookupQuad(level, sampler, x0, x1, y0, y1, coordZ); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep = tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad); + + if (tcuTexLookupVerifier.isBilinearRangeValid(prec, quad, [minA, maxA], [minB, maxB], searchStep, result)) + return true; + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord vec3 + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLinearSampleResultValid_CoordAsVec3 = function(level, sampler, prec, coord, result) { + /** @type {Array<number>} */ + var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getWidth(), coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var vBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getHeight(), coord[1], prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ + var wBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, level.getDepth(), coord[2], prec.coordBits[2], prec.uvwBits[2]); + + // Integer coordinate bounds for (x0,y0) - without wrap mode + /** @type {number} */ var minI = Math.floor(uBounds[0] - 0.5); + /** @type {number} */ var maxI = Math.floor(uBounds[1] - 0.5); + /** @type {number} */ var minJ = Math.floor(vBounds[0] - 0.5); + /** @type {number} */ var maxJ = Math.floor(vBounds[1] - 0.5); + /** @type {number} */ var minK = Math.floor(wBounds[0] - 0.5); + /** @type {number} */ var maxK = Math.floor(wBounds[1] - 0.5); + + /** @type {number} */ var w = level.getWidth(); + /** @type {number} */ var h = level.getHeight(); + /** @type {number} */ var d = level.getDepth(); + + /** @type {tcuTexture.TextureChannelClass} */ + var texClass = tcuTexture.getTextureChannelClass(level.getFormat().type); + /** @type {number} */ + var searchStep = (texClass == tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForUnorm(prec) : + (texClass == tcuTexture.TextureChannelClass.SIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForSnorm(prec) : + 0; // Step is computed for floating-point quads based on texel values. + + // \todo [2013-07-03 pyry] This could be optimized by first computing ranges based on wrap mode. + + for (var k = minK; k <= maxK; k++) { + for (var j = minJ; j <= maxJ; j++) { + for (var i = minI; i <= maxI; i++) { + // Wrapped coordinates + /** @type {number} */ var x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i, w); + /** @type {number} */ var x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i + 1, w); + /** @type {number} */ var y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j, h); + /** @type {number} */ var y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j + 1, h); + /** @type {number} */ var z0 = tcuTexVerifierUtil.wrap(sampler.wrapR, k, d); + /** @type {number} */ var z1 = tcuTexVerifierUtil.wrap(sampler.wrapR, k + 1, d); + + // Bounds for filtering factors + /** @type {number} */ var minA = deMath.clamp((uBounds[0] - 0.5) - i, 0, 1); + /** @type {number} */ var maxA = deMath.clamp((uBounds[1] - 0.5) - i, 0, 1); + /** @type {number} */ var minB = deMath.clamp((vBounds[0] - 0.5) - j, 0, 1); + /** @type {number} */ var maxB = deMath.clamp((vBounds[1] - 0.5) - j, 0, 1); + /** @type {number} */ var minC = deMath.clamp((wBounds[0] - 0.5) - k, 0, 1); + /** @type {number} */ var maxC = deMath.clamp((wBounds[1] - 0.5) - k, 0, 1); + + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad0 = tcuTexLookupVerifier.lookupQuad(level, sampler, x0, x1, y0, y1, z0); + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad1 = tcuTexLookupVerifier.lookupQuad(level, sampler, x0, x1, y0, y1, z1); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep = Math.min(tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad0), tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad1)); + + if (tcuTexLookupVerifier.isTrilinearRangeValid(prec, quad0, quad1, [minA, maxA], [minB, maxB], [minC, maxC], searchStep, result)) + return true; + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {number} coord + * @param {number} coordY + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isNearestMipmapLinearSampleResultValid_CoordXYAsNumber = function(level0, level1, sampler, prec, coord, coordY, fBounds, result) { + /** @type {number} */ var w0 = level0.getWidth(); + /** @type {number} */ var w1 = level1.getWidth(); + + /** @type {Array<number>} */ + var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w0, coord, prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w1, coord, prec.coordBits[0], prec.uvwBits[0]); + + // Integer coordinates - without wrap mode + /** @type {number} */ var minI0 = Math.floor(uBounds0[0]); + /** @type {number} */ var maxI0 = Math.floor(uBounds0[1]); + /** @type {number} */ var minI1 = Math.floor(uBounds1[0]); + /** @type {number} */ var maxI1 = Math.floor(uBounds1[1]); + + for (var i0 = minI0; i0 <= maxI0; i0++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + /** @type {Array<number>} */ + var c0 = tcuTexLookupVerifier.lookupFloat(level0, sampler, tcuTexVerifierUtil.wrap(sampler.wrapS, i0, w0), coordY, 0); + /** @type {Array<number>} */ + var c1 = tcuTexLookupVerifier.lookupFloat(level1, sampler, tcuTexVerifierUtil.wrap(sampler.wrapS, i1, w1), coordY, 0); + + if (tcuTexLookupVerifier.isLinearRangeValid(prec, c0, c1, fBounds, result)) + return true; + } + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {number} coordZ + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isNearestMipmapLinearSampleResultValid_CoordAsVec2AndInt = function(level0, level1, sampler, prec, coord, coordZ, fBounds, result) { + /** @type {number} */ var w0 = level0.getWidth(); + /** @type {number} */ var w1 = level1.getWidth(); + /** @type {number} */ var h0 = level0.getHeight(); + /** @type {number} */ var h1 = level1.getHeight(); + + /** @type {Array<number>} */ + var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w0, coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w1, coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var vBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, h0, coord[1], prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ + var vBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, h1, coord[1], prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinates - without wrap mode + /** @type {number} */ var minI0 = Math.floor(uBounds0[0]); + /** @type {number} */ var maxI0 = Math.floor(uBounds0[1]); + /** @type {number} */ var minI1 = Math.floor(uBounds1[0]); + /** @type {number} */ var maxI1 = Math.floor(uBounds1[1]); + /** @type {number} */ var minJ0 = Math.floor(vBounds0[0]); + /** @type {number} */ var maxJ0 = Math.floor(vBounds0[1]); + /** @type {number} */ var minJ1 = Math.floor(vBounds1[0]); + /** @type {number} */ var maxJ1 = Math.floor(vBounds1[1]); + + for (var j0 = minJ0; j0 <= maxJ0; j0++) { + for (var i0 = minI0; i0 <= maxI0; i0++) { + for (var j1 = minJ1; j1 <= maxJ1; j1++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + /** @type {Array<number>} */ var c0 = tcuTexLookupVerifier.lookupFloat(level0, sampler, tcuTexVerifierUtil.wrap(sampler.wrapS, i0, w0), tcuTexVerifierUtil.wrap(sampler.wrapT, j0, h0), coordZ); + /** @type {Array<number>} */ var c1 = tcuTexLookupVerifier.lookupFloat(level1, sampler, tcuTexVerifierUtil.wrap(sampler.wrapS, i1, w1), tcuTexVerifierUtil.wrap(sampler.wrapT, j1, h1), coordZ); + + if (tcuTexLookupVerifier.isLinearRangeValid(prec, c0, c1, fBounds, result)) + return true; + } + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isNearestMipmapLinearSampleResultValid_CoordAsVec3 = function(level0, level1, sampler, prec, coord, fBounds, result) { + /** @type {number} */ var w0 = level0.getWidth(); + /** @type {number} */ var w1 = level1.getWidth(); + /** @type {number} */ var h0 = level0.getHeight(); + /** @type {number} */ var h1 = level1.getHeight(); + /** @type {number} */ var d0 = level0.getDepth(); + /** @type {number} */ var d1 = level1.getDepth(); + + /** @type {Array<number>} */ + var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w0, coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w1, coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var vBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, h0, coord[1], prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ + var vBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, h1, coord[1], prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ + var wBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, d0, coord[2], prec.coordBits[2], prec.uvwBits[2]); + /** @type {Array<number>} */ + var wBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, d1, coord[2], prec.coordBits[2], prec.uvwBits[2]); + + // Integer coordinates - without wrap mode + /** @type {number} */ var minI0 = Math.floor(uBounds0[0]); + /** @type {number} */ var maxI0 = Math.floor(uBounds0[1]); + /** @type {number} */ var minI1 = Math.floor(uBounds1[0]); + /** @type {number} */ var maxI1 = Math.floor(uBounds1[1]); + /** @type {number} */ var minJ0 = Math.floor(vBounds0[0]); + /** @type {number} */ var maxJ0 = Math.floor(vBounds0[1]); + /** @type {number} */ var minJ1 = Math.floor(vBounds1[0]); + /** @type {number} */ var maxJ1 = Math.floor(vBounds1[1]); + /** @type {number} */ var minK0 = Math.floor(wBounds0[0]); + /** @type {number} */ var maxK0 = Math.floor(wBounds0[1]); + /** @type {number} */ var minK1 = Math.floor(wBounds1[0]); + /** @type {number} */ var maxK1 = Math.floor(wBounds1[1]); + + for (var k0 = minK0; k0 <= maxK0; k0++) { + for (var j0 = minJ0; j0 <= maxJ0; j0++) { + for (var i0 = minI0; i0 <= maxI0; i0++) { + for (var k1 = minK1; k1 <= maxK1; k1++) { + for (var j1 = minJ1; j1 <= maxJ1; j1++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + /** @type {Array<number>} */ var c0 = tcuTexLookupVerifier.lookupFloat(level0, sampler, tcuTexVerifierUtil.wrap(sampler.wrapS, i0, w0), tcuTexVerifierUtil.wrap(sampler.wrapT, j0, h0), tcuTexVerifierUtil.wrap(sampler.wrapR, k0, d0)); + /** @type {Array<number>} */ var c1 = tcuTexLookupVerifier.lookupFloat(level1, sampler, tcuTexVerifierUtil.wrap(sampler.wrapS, i1, w1), tcuTexVerifierUtil.wrap(sampler.wrapT, j1, h1), tcuTexVerifierUtil.wrap(sampler.wrapR, k1, d1)); + + if (tcuTexLookupVerifier.isLinearRangeValid(prec, c0, c1, fBounds, result)) + return true; + } + } + } + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {number} coordZ + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLinearMipmapLinearSampleResultValid_CoordAsVec2AndInt = function(level0, level1, sampler, prec, coord, coordZ, fBounds, result) { + // \todo [2013-07-04 pyry] This is strictly not correct as coordinates between levels should be dependent. + // Right now this allows pairing any two valid bilinear quads. + + /** @type {number} */ var w0 = level0.getWidth(); + /** @type {number} */ var w1 = level1.getWidth(); + /** @type {number} */ var h0 = level0.getHeight(); + /** @type {number} */ var h1 = level1.getHeight(); + + /** @type {Array<number>} */ + var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w0, coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w1, coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var vBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, h0, coord[1], prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ + var vBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, h1, coord[1], prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinates - without wrap mode + /** @type {number} */ var minI0 = Math.floor(uBounds0[0] - 0.5); + /** @type {number} */ var maxI0 = Math.floor(uBounds0[1] - 0.5); + /** @type {number} */ var minI1 = Math.floor(uBounds1[0] - 0.5); + /** @type {number} */ var maxI1 = Math.floor(uBounds1[1] - 0.5); + /** @type {number} */ var minJ0 = Math.floor(vBounds0[0] - 0.5); + /** @type {number} */ var maxJ0 = Math.floor(vBounds0[1] - 0.5); + /** @type {number} */ var minJ1 = Math.floor(vBounds1[0] - 0.5); + /** @type {number} */ var maxJ1 = Math.floor(vBounds1[1] - 0.5); + + /** @type {tcuTexture.TextureChannelClass} */ + var texClass = tcuTexture.getTextureChannelClass(level0.getFormat().type); + /** @type {number} */ var cSearchStep = (texClass == tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForUnorm(prec) : + (texClass == tcuTexture.TextureChannelClass.SIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForSnorm(prec) : + 0; // Step is computed for floating-point quads based on texel values. + + /** @type {number} */ var x0; + /** @type {number} */ var x1; + /** @type {number} */ var y0; + /** @type {number} */ var y1; + + for (var j0 = minJ0; j0 <= maxJ0; j0++) { + for (var i0 = minI0; i0 <= maxI0; i0++) { + /** @type {number} */ var searchStep0; + + x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i0, w0); + x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i0 + 1, w0); + y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j0, h0); + y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j0 + 1, h0); + + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad0 = tcuTexLookupVerifier.lookupQuad(level0, sampler, x0, x1, y0, y1, coordZ); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep0 = tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad0); + else + searchStep0 = cSearchStep; + + /** @type {number} */ var minA0 = deMath.clamp((uBounds0[0] - 0.5) - i0, 0, 1); + /** @type {number} */ var maxA0 = deMath.clamp((uBounds0[1] - 0.5) - i0, 0, 1); + /** @type {number} */ var minB0 = deMath.clamp((vBounds0[0] - 0.5) - j0, 0, 1); + /** @type {number} */ var maxB0 = deMath.clamp((vBounds0[1] - 0.5) - j0, 0, 1); + + for (var j1 = minJ1; j1 <= maxJ1; j1++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + /** @type {number} */ var searchStep1; + + x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i1, w1); + x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i1 + 1, w1); + y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j1, h1); + y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j1 + 1, h1); + + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad1 = tcuTexLookupVerifier.lookupQuad(level1, sampler, x0, x1, y0, y1, coordZ); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep1 = tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad1); + else + searchStep1 = cSearchStep; + + /** @type {number} */ var minA1 = deMath.clamp((uBounds1[0] - 0.5) - i1, 0, 1); + /** @type {number} */ var maxA1 = deMath.clamp((uBounds1[1] - 0.5) - i1, 0, 1); + /** @type {number} */ var minB1 = deMath.clamp((vBounds1[0] - 0.5) - j1, 0, 1); + /** @type {number} */ var maxB1 = deMath.clamp((vBounds1[1] - 0.5) - j1, 0, 1); + + if (tcuTexLookupVerifier.is2DTrilinearFilterResultValid(prec, quad0, quad1, [minA0, maxA0], [minB0, maxB0], [minA1, maxA1], [minB1, maxB1], + fBounds, Math.min(searchStep0, searchStep1), result)) + return true; + } + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLinearMipmapLinearSampleResultValid_CoordAsVec3 = function(level0, level1, sampler, prec, coord, fBounds, result) { + // \todo [2013-07-04 pyry] This is strictly not correct as coordinates between levels should be dependent. + // Right now this allows pairing any two valid bilinear quads. + + /** @type {number} */ var w0 = level0.getWidth(); + /** @type {number} */ var w1 = level1.getWidth(); + /** @type {number} */ var h0 = level0.getHeight(); + /** @type {number} */ var h1 = level1.getHeight(); + /** @type {number} */ var d0 = level0.getDepth(); + /** @type {number} */ var d1 = level1.getDepth(); + + /** @type {Array<number>} */ + var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w0, coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, w1, coord[0], prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ + var vBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, h0, coord[1], prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ + var vBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, h1, coord[1], prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ + var wBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, d0, coord[2], prec.coordBits[2], prec.uvwBits[2]); + /** @type {Array<number>} */ + var wBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds( + sampler.normalizedCoords, d1, coord[2], prec.coordBits[2], prec.uvwBits[2]); + + // Integer coordinates - without wrap mode + /** @type {number} */ var minI0 = Math.floor(uBounds0[0] - 0.5); + /** @type {number} */ var maxI0 = Math.floor(uBounds0[1] - 0.5); + /** @type {number} */ var minI1 = Math.floor(uBounds1[0] - 0.5); + /** @type {number} */ var maxI1 = Math.floor(uBounds1[1] - 0.5); + /** @type {number} */ var minJ0 = Math.floor(vBounds0[0] - 0.5); + /** @type {number} */ var maxJ0 = Math.floor(vBounds0[1] - 0.5); + /** @type {number} */ var minJ1 = Math.floor(vBounds1[0] - 0.5); + /** @type {number} */ var maxJ1 = Math.floor(vBounds1[1] - 0.5); + /** @type {number} */ var minK0 = Math.floor(wBounds0[0] - 0.5); + /** @type {number} */ var maxK0 = Math.floor(wBounds0[1] - 0.5); + /** @type {number} */ var minK1 = Math.floor(wBounds1[0] - 0.5); + /** @type {number} */ var maxK1 = Math.floor(wBounds1[1] - 0.5); + + /** @type {tcuTexture.TextureChannelClass} */ + var texClass = tcuTexture.getTextureChannelClass(level0.getFormat().type); + /** @type {number} */ var cSearchStep = texClass == tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT ? tcuTexLookupVerifier.computeBilinearSearchStepForUnorm(prec) : + texClass == tcuTexture.TextureChannelClass.SIGNED_FIXED_POINT ? tcuTexLookupVerifier.computeBilinearSearchStepForSnorm(prec) : + 0; // Step is computed for floating-point quads based on texel values. + + /** @type {number} */ var x0; + /** @type {number} */ var x1; + /** @type {number} */ var y0; + /** @type {number} */ var y1; + /** @type {number} */ var z0; + /** @type {number} */ var z1; + + for (var k0 = minK0; k0 <= maxK0; k0++) { + for (var j0 = minJ0; j0 <= maxJ0; j0++) { + for (var i0 = minI0; i0 <= maxI0; i0++) { + /** @type {number} */ var searchStep0; + + x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i0, w0); + x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i0 + 1, w0); + y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j0, h0); + y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j0 + 1, h0); + z0 = tcuTexVerifierUtil.wrap(sampler.wrapR, k0, d0); + z1 = tcuTexVerifierUtil.wrap(sampler.wrapR, k0 + 1, d0); + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad00 = tcuTexLookupVerifier.lookupQuad(level0, sampler, x0, x1, y0, y1, z0); + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad01 = tcuTexLookupVerifier.lookupQuad(level0, sampler, x0, x1, y0, y1, z1); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep0 = Math.min(tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad00), tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad01)); + else + searchStep0 = cSearchStep; + + /** @type {number} */ var minA0 = deMath.clamp((uBounds0[0] - 0.5) - i0, 0, 1); + /** @type {number} */ var maxA0 = deMath.clamp((uBounds0[1] - 0.5) - i0, 0, 1); + /** @type {number} */ var minB0 = deMath.clamp((vBounds0[0] - 0.5) - j0, 0, 1); + /** @type {number} */ var maxB0 = deMath.clamp((vBounds0[1] - 0.5) - j0, 0, 1); + /** @type {number} */ var minC0 = deMath.clamp((wBounds0[0] - 0.5) - k0, 0, 1); + /** @type {number} */ var maxC0 = deMath.clamp((wBounds0[1] - 0.5) - k0, 0, 1); + + for (var k1 = minK1; k1 <= maxK1; k1++) { + for (var j1 = minJ1; j1 <= maxJ1; j1++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + + /** @type {number} */ var searchStep1; + + x0 = tcuTexVerifierUtil.wrap(sampler.wrapS, i1, w1); + x1 = tcuTexVerifierUtil.wrap(sampler.wrapS, i1 + 1, w1); + y0 = tcuTexVerifierUtil.wrap(sampler.wrapT, j1, h1); + y1 = tcuTexVerifierUtil.wrap(sampler.wrapT, j1 + 1, h1); + z0 = tcuTexVerifierUtil.wrap(sampler.wrapR, k1, d1); + z1 = tcuTexVerifierUtil.wrap(sampler.wrapR, k1 + 1, d1); + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad10 = tcuTexLookupVerifier.lookupQuad(level1, sampler, x0, x1, y0, y1, z0); + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad11 = tcuTexLookupVerifier.lookupQuad(level1, sampler, x0, x1, y0, y1, z1); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep1 = Math.min(tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad10), tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad11)); + else + searchStep1 = cSearchStep; + + /** @type {number} */ var minA1 = deMath.clamp((uBounds1[0] - 0.5) - i1, 0, 1); + /** @type {number} */ var maxA1 = deMath.clamp((uBounds1[1] - 0.5) - i1, 0, 1); + /** @type {number} */ var minB1 = deMath.clamp((vBounds1[0] - 0.5) - j1, 0, 1); + /** @type {number} */ var maxB1 = deMath.clamp((vBounds1[1] - 0.5) - j1, 0, 1); + /** @type {number} */ var minC1 = deMath.clamp((wBounds1[0] - 0.5) - k1, 0, 1); + /** @type {number} */ var maxC1 = deMath.clamp((wBounds1[1] - 0.5) - k1, 0, 1); + + if (tcuTexLookupVerifier.is3DTrilinearFilterResultValid( + prec, quad00, quad01, quad10, quad11, + [minA0, maxA0], [minB0, maxB0], [minC0, maxC0], + [minA1, maxA1], [minB1, maxB1], [minC1, maxC1], + fBounds, Math.min(searchStep0, searchStep1), result)) + return true; + } + } + } + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} filterMode + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {number} coordX + * @param {number} coordY + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLevelSampleResultValid_CoordXYAsNumber = function(level, sampler, filterMode, prec, coordX, coordY, result) { + if (filterMode == tcuTexture.FilterMode.LINEAR) + return tcuTexLookupVerifier.isLinearSampleResultValid_CoordXYAsNumber(level, sampler, prec, coordX, coordY, result); + else + return tcuTexLookupVerifier.isNearestSampleResultValid_CoordXYAsNumber(level, sampler, prec, coordX, coordY, result); + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} filterMode + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {number} coordZ + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec2AndInt = function(level, sampler, filterMode, prec, coord, coordZ, result) { + if (filterMode == tcuTexture.FilterMode.LINEAR) + return tcuTexLookupVerifier.isLinearSampleResultValid_CoordAsVec2AndInt(level, sampler, prec, coord, coordZ, result); + else + return tcuTexLookupVerifier.isNearestSampleResultValid_CoordAsVec2AndInt(level, sampler, prec, coord, coordZ, result); + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} filterMode + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec3 = function(level, sampler, filterMode, prec, coord, result) { + if (filterMode == tcuTexture.FilterMode.LINEAR) + return tcuTexLookupVerifier.isLinearSampleResultValid_CoordAsVec3(level, sampler, prec, coord, result); + else + return tcuTexLookupVerifier.isNearestSampleResultValid_CoordAsVec3(level, sampler, prec, coord, result); + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} levelFilter + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {number} coordZ + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isMipmapLinearSampleResultValid_CoordAsVec2AndInt = function(level0, level1, sampler, levelFilter, prec, coord, coordZ, fBounds, result) { + if (levelFilter == tcuTexture.FilterMode.LINEAR) + return tcuTexLookupVerifier.isLinearMipmapLinearSampleResultValid_CoordAsVec2AndInt(level0, level1, sampler, prec, coord, coordZ, fBounds, result); + else + return tcuTexLookupVerifier.isNearestMipmapLinearSampleResultValid_CoordAsVec2AndInt(level0, level1, sampler, prec, coord, coordZ, fBounds, result); + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} level0 + * @param {tcuTexture.ConstPixelBufferAccess} level1 + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} levelFilter + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isMipmapLinearSampleResultValid_CoordAsVec3 = function(level0, level1, sampler, levelFilter, prec, coord, fBounds, result) { + if (levelFilter == tcuTexture.FilterMode.LINEAR) + return tcuTexLookupVerifier.isLinearMipmapLinearSampleResultValid_CoordAsVec3(level0, level1, sampler, prec, coord, fBounds, result); + else + return tcuTexLookupVerifier.isNearestMipmapLinearSampleResultValid_CoordAsVec3(level0, level1, sampler, prec, coord, fBounds, result); + }; + + /** + * @param {tcuTexture.Texture2DView} texture + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} lodBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLookupResultValid_Texture2DView = function(texture, sampler, prec, coord, lodBounds, result) { + /** @type {number} */ var minLod = lodBounds[0]; + /** @type {number} */ var maxLod = lodBounds[1]; + /** @type {boolean} */ var canBeMagnified = minLod <= sampler.lodThreshold; + /** @type {boolean} */ var canBeMinified = maxLod > sampler.lodThreshold; + + assertMsgOptions(tcuTexLookupVerifier.isSamplerSupported(sampler), 'Sampler not supported.', false, true); + + /** @type {number} */ var minLevel; + /** @type {number} */ var maxLevel; + + if (canBeMagnified) + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec2AndInt(texture.getLevel(0), sampler, sampler.magFilter, prec, coord, 0, result)) + return true; + + if (canBeMinified) { + /** @type {boolean} */ var isNearestMipmap = tcuTexVerifierUtil.isNearestMipmapFilter(sampler.minFilter); + /** @type {boolean} */ var isLinearMipmap = tcuTexVerifierUtil.isLinearMipmapFilter(sampler.minFilter); + /** @type {number} */ var minTexLevel = 0; + /** @type {number} */ var maxTexLevel = texture.getNumLevels() - 1; + + assertMsgOptions(minTexLevel <= maxTexLevel, 'minTexLevel > maxTexLevel', false, true); + + if (isLinearMipmap && minTexLevel < maxTexLevel) { + minLevel = deMath.clamp(Math.floor(minLod), minTexLevel, maxTexLevel - 1); + maxLevel = deMath.clamp(Math.floor(maxLod), minTexLevel, maxTexLevel - 1); + + assertMsgOptions(minLevel <= maxLevel, 'minLevel > maxLevel', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + /** @type {number} */ var minF = deMath.clamp(minLod - level, 0, 1); + /** @type {number} */ var maxF = deMath.clamp(maxLod - level, 0, 1); + + if (tcuTexLookupVerifier.isMipmapLinearSampleResultValid_CoordAsVec2AndInt(texture.getLevel(level), texture.getLevel(level + 1), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, coord, 0, [minF, maxF], result)) + return true; + } + } else if (isNearestMipmap) { + // \note The accurate formula for nearest mipmapping is level = ceil(lod + 0.5) - 1 but Khronos has made + // decision to allow floor(lod + 0.5) as well. + minLevel = deMath.clamp(Math.ceil(minLod + 0.5) - 1, minTexLevel, maxTexLevel); + maxLevel = deMath.clamp(Math.floor(maxLod + 0.5), minTexLevel, maxTexLevel); + + assertMsgOptions(minLevel <= maxLevel, 'minLevel > maxLevel', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec2AndInt(texture.getLevel(level), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, coord, 0, result)) + return true; + } + } else { + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec2AndInt(texture.getLevel(0), sampler, sampler.minFilter, prec, coord, 0, result)) + return true; + } + } + + return false; + }; + + /** + * @param {tcuTexture.TextureCubeView} texture + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} lodBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLookupResultValid_TextureCubeView = function(texture, sampler, prec, coord, lodBounds, result) { + /** @type {number} */ var numPossibleFaces = 0; + + assertMsgOptions(tcuTexLookupVerifier.isSamplerSupported(sampler), 'Sampler not supported.', false, true); + + /** @type {Array<tcuTexture.CubeFace>} */ var possibleFaces = tcuTexVerifierUtil.getPossibleCubeFaces(coord, prec.coordBits); + + /** @type {number} */ var minLevel; + /** @type {number} */ var maxLevel; + + if (!possibleFaces) + return true; // Result is undefined. + + for (var tryFaceNdx = 0; tryFaceNdx < possibleFaces.length; tryFaceNdx++) { + /** @type {tcuTexture.CubeFaceCoords} */ + var faceCoords = new tcuTexture.CubeFaceCoords(possibleFaces[tryFaceNdx], tcuTexture.projectToFace(possibleFaces[tryFaceNdx], coord)); + /** @type {number} */ var minLod = lodBounds[0]; + /** @type {number} */ var maxLod = lodBounds[1]; + /** @type {boolean} */ var canBeMagnified = minLod <= sampler.lodThreshold; + /** @type {boolean} */ var canBeMinified = maxLod > sampler.lodThreshold; + + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faces = []; + if (canBeMagnified) { + tcuTexLookupVerifier.getCubeLevelFaces(texture, 0, faces); + + if (tcuTexLookupVerifier.isCubeLevelSampleResultValid(faces, sampler, sampler.magFilter, prec, faceCoords, result)) + return true; + } + + if (canBeMinified) { + /** @type {boolean} */ var isNearestMipmap = tcuTexVerifierUtil.isNearestMipmapFilter(sampler.minFilter); + /** @type {boolean} */ var isLinearMipmap = tcuTexVerifierUtil.isLinearMipmapFilter(sampler.minFilter); + /** @type {number} */ var minTexLevel = 0; + /** @type {number} */ var maxTexLevel = texture.getNumLevels() - 1; + + assertMsgOptions(minTexLevel <= maxTexLevel, 'minTexLevel > maxTexLevel', false, true); + + if (isLinearMipmap && minTexLevel < maxTexLevel) { + minLevel = deMath.clamp(Math.floor(minLod), minTexLevel, maxTexLevel - 1); + maxLevel = deMath.clamp(Math.floor(maxLod), minTexLevel, maxTexLevel - 1); + + assertMsgOptions(minLevel <= maxLevel, 'minLevel > maxLevel', false, true); + + for (var levelNdx = minLevel; levelNdx <= maxLevel; levelNdx++) { + /** @type {number} */ var minF = deMath.clamp(minLod - levelNdx, 0, 1); + /** @type {number} */ var maxF = deMath.clamp(maxLod - levelNdx, 0, 1); + + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faces0 = []; + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faces1 = []; + + tcuTexLookupVerifier.getCubeLevelFaces(texture, levelNdx, faces0); + tcuTexLookupVerifier.getCubeLevelFaces(texture, levelNdx + 1, faces1); + + if (tcuTexLookupVerifier.isCubeMipmapLinearSampleResultValid(faces0, faces1, sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, faceCoords, [minF, maxF], result)) + return true; + } + } else if (isNearestMipmap) { + // \note The accurate formula for nearest mipmapping is level = ceil(lod + 0.5) - 1 but Khronos has made + // decision to allow floor(lod + 0.5) as well. + minLevel = deMath.clamp(Math.ceil(minLod + 0.5) - 1, minTexLevel, maxTexLevel); + maxLevel = deMath.clamp(Math.floor(maxLod + 0.5), minTexLevel, maxTexLevel); + + assertMsgOptions(minLevel <= maxLevel, 'minLevel > maxLevel', false, true); + + for (var levelNdx = minLevel; levelNdx <= maxLevel; levelNdx++) { + tcuTexLookupVerifier.getCubeLevelFaces(texture, levelNdx, faces); + + if (tcuTexLookupVerifier.isCubeLevelSampleResultValid(faces, sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, faceCoords, result)) + return true; + } + } else { + tcuTexLookupVerifier.getCubeLevelFaces(texture, 0, faces); + + if (tcuTexLookupVerifier.isCubeLevelSampleResultValid(faces, sampler, sampler.minFilter, prec, faceCoords, result)) + return true; + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.Texture2DArrayView} texture + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} lodBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLookupResultValid_Texture2DArrayView = function(texture, sampler, prec, coord, lodBounds, result) { + /** @type {Array<number>} */ var layerRange = tcuTexLookupVerifier.computeLayerRange(texture.getNumLayers(), prec.coordBits[2], coord[2]); + /** @type {Array<number>} */ var coordXY = deMath.swizzle(coord, [0, 1]); + /** @type {number} */ var minLod = lodBounds[0]; + /** @type {number} */ var maxLod = lodBounds[1]; + /** @type {boolean} */ var canBeMagnified = minLod <= sampler.lodThreshold; + /** @type {boolean} */ var canBeMinified = maxLod > sampler.lodThreshold; + + assertMsgOptions(tcuTexLookupVerifier.isSamplerSupported(sampler), 'Sampler not supported.', false, true); + /** @type {number} */ var minLevel; + /** @type {number} */ var maxLevel; + + for (var layer = layerRange[0]; layer <= layerRange[1]; layer++) { + if (canBeMagnified) { + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec2AndInt(texture.getLevel(0), sampler, sampler.magFilter, prec, coordXY, layer, result)) + return true; + } + + if (canBeMinified) { + /** @type {boolean} */ var isNearestMipmap = tcuTexVerifierUtil.isNearestMipmapFilter(sampler.minFilter); + /** @type {boolean} */ var isLinearMipmap = tcuTexVerifierUtil.isLinearMipmapFilter(sampler.minFilter); + /** @type {number} */ var minTexLevel = 0; + /** @type {number} */ var maxTexLevel = texture.getNumLevels() - 1; + + assertMsgOptions(minTexLevel <= maxTexLevel, 'minTexLevel > maxTexLevel', false, true); + + if (isLinearMipmap && minTexLevel < maxTexLevel) { + minLevel = deMath.clamp(Math.floor(minLod), minTexLevel, maxTexLevel - 1); + maxLevel = deMath.clamp(Math.floor(maxLod), minTexLevel, maxTexLevel - 1); + + assertMsgOptions(minLevel <= maxLevel, 'minLevel > maxLevel', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + /** @type {number} */ var minF = deMath.clamp(minLod - level, 0, 1); + /** @type {number} */ var maxF = deMath.clamp(maxLod - level, 0, 1); + + if (tcuTexLookupVerifier.isMipmapLinearSampleResultValid_CoordAsVec2AndInt(texture.getLevel(level), texture.getLevel(level + 1), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, coordXY, layer, [minF, maxF], result)) + return true; + } + } else if (isNearestMipmap) { + // \note The accurate formula for nearest mipmapping is level = ceil(lod + 0.5) - 1 but Khronos has made + // decision to allow floor(lod + 0.5) as well. + minLevel = deMath.clamp(Math.ceil(minLod + 0.5) - 1, minTexLevel, maxTexLevel); + maxLevel = deMath.clamp(Math.floor(maxLod + 0.5), minTexLevel, maxTexLevel); + + assertMsgOptions(minLevel <= maxLevel, 'minLevel > maxLevel', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec2AndInt(texture.getLevel(level), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, coordXY, layer, result)) + return true; + } + } else { + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec2AndInt(texture.getLevel(0), sampler, sampler.minFilter, prec, coordXY, layer, result)) + return true; + } + } + } + + return false; + }; + + /** + * @param {tcuTexture.Texture3DView} texture + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} lodBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLookupResultValid = function(texture, sampler, prec, coord, lodBounds, result) { + /** @type {number} */ var minLod = lodBounds[0]; + /** @type {number} */ var maxLod = lodBounds[1]; + /** @type {boolean} */ var canBeMagnified = minLod <= sampler.lodThreshold; + /** @type {boolean} */ var canBeMinified = maxLod > sampler.lodThreshold; + + assertMsgOptions(tcuTexLookupVerifier.isSamplerSupported(sampler), 'Sampler not supported.', false, true); + + /** @type {number} */ var minLevel; + /** @type {number} */ var maxLevel; + + if (canBeMagnified) + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec3(texture.getLevel(0), sampler, sampler.magFilter, prec, coord, result)) + return true; + + if (canBeMinified) { + /** @type {boolean} */ var isNearestMipmap = tcuTexVerifierUtil.isNearestMipmapFilter(sampler.minFilter); + /** @type {boolean} */ var isLinearMipmap = tcuTexVerifierUtil.isLinearMipmapFilter(sampler.minFilter); + /** @type {number} */ var minTexLevel = 0; + /** @type {number} */ var maxTexLevel = texture.getNumLevels() - 1; + + assertMsgOptions(minTexLevel <= maxTexLevel, 'minTexLevel > maxTexLevel', false, true); + + if (isLinearMipmap && minTexLevel < maxTexLevel) { + minLevel = deMath.clamp(Math.floor(minLod), minTexLevel, maxTexLevel - 1); + maxLevel = deMath.clamp(Math.floor(maxLod), minTexLevel, maxTexLevel - 1); + + assertMsgOptions(minLevel <= maxLevel, 'minLevel > maxLevel', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + /** @type {number} */ var minF = deMath.clamp(minLod - level, 0, 1); + /** @type {number} */ var maxF = deMath.clamp(maxLod - level, 0, 1); + + if (tcuTexLookupVerifier.isMipmapLinearSampleResultValid_CoordAsVec3(texture.getLevel(level), texture.getLevel(level + 1), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, coord, [minF, maxF], result)) + return true; + } + } else if (isNearestMipmap) { + // \note The accurate formula for nearest mipmapping is level = ceil(lod + 0.5) - 1 but Khronos has made + // decision to allow floor(lod + 0.5) as well. + minLevel = deMath.clamp(Math.ceil(minLod + 0.5) - 1, minTexLevel, maxTexLevel); + maxLevel = deMath.clamp(Math.floor(maxLod + 0.5), minTexLevel, maxTexLevel); + + assertMsgOptions(minLevel <= maxLevel, 'minLevel > maxLevel', false, true); + + for (var level = minLevel; level <= maxLevel; level++) { + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec3(texture.getLevel(level), sampler, tcuTexVerifierUtil.getLevelFilter(sampler.minFilter), prec, coord, result)) + return true; + } + } else { + if (tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec3(texture.getLevel(0), sampler, sampler.minFilter, prec, coord, result)) + return true; + } + } + + return false; + }; + + /** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} faces (&faces)[CUBEFACE_LAST] + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexture.CubeFaceCoords} coords + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isSeamlessLinearSampleResultValid = function(faces, sampler, prec, coords, result) { + /** @type {number} */ var size = faces[coords.face].getWidth(); + + /** @type {Array<number>} */ var uBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size, coords.s, prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ var vBounds = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size, coords.t, prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinate bounds for (x0,y0) - without wrap mode + /** @type {number} */ var minI = Math.floor(uBounds[0] - 0.5); + /** @type {number} */ var maxI = Math.floor(uBounds[1] - 0.5); + /** @type {number} */ var minJ = Math.floor(vBounds[0] - 0.5); + /** @type {number} */ var maxJ = Math.floor(vBounds[1] - 0.5); + + /** @type {tcuTexture.TextureChannelClass} */ var texClass = tcuTexture.getTextureChannelClass(faces[coords.face].getFormat().type); + /** @type {number} */ var searchStep = (texClass == tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForUnorm(prec) : + (texClass == tcuTexture.TextureChannelClass.SIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForSnorm(prec) : + 0; // Step is computed for floating-point quads based on texel values. + + for (var j = minJ; j <= maxJ; j++) { + for (var i = minI; i <= maxI; i++) { + /** @type {tcuTexture.CubeFaceCoords} */ var c00 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i + 0, j + 0]), size); + /** @type {tcuTexture.CubeFaceCoords} */ var c10 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i + 1, j + 0]), size); + /** @type {tcuTexture.CubeFaceCoords} */ var c01 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i + 0, j + 1]), size); + /** @type {tcuTexture.CubeFaceCoords} */ var c11 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i + 1, j + 1]), size); + + // If any of samples is out of both edges, implementations can do pretty much anything according to spec. + // \todo [2013-07-08 pyry] Test the special case where all corner pixels have exactly the same color. + if (c00 == null || c01 == null || c10 == null || c11 == null || + c00.face == null || c01.face == null || c10.face == null || c11.face == null) + return true; + + // Bounds for filtering factors + /** @type {number} */ var minA = deMath.clamp((uBounds[0] - 0.5) - i, 0, 1); + /** @type {number} */ var maxA = deMath.clamp((uBounds[1] - 0.5) - i, 0, 1); + /** @type {number} */ var minB = deMath.clamp((vBounds[0] - 0.5) - j, 0, 1); + /** @type {number} */ var maxB = deMath.clamp((vBounds[1] - 0.5) - j, 0, 1); + + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad = new tcuTexLookupVerifier.ColorQuad([], [], [], []); + quad.p00 = tcuTexLookupVerifier.lookupFloat(faces[c00.face], sampler, c00.s, c00.t, 0); + quad.p10 = tcuTexLookupVerifier.lookupFloat(faces[c10.face], sampler, c10.s, c10.t, 0); + quad.p01 = tcuTexLookupVerifier.lookupFloat(faces[c01.face], sampler, c01.s, c01.t, 0); + quad.p11 = tcuTexLookupVerifier.lookupFloat(faces[c11.face], sampler, c11.s, c11.t, 0); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep = tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad); + + if (tcuTexLookupVerifier.isBilinearRangeValid(prec, quad, [minA, maxA], [minB, maxB], searchStep, result)) + return true; + } + } + + return false; + }; + + /** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} faces0 (&faces0)[CUBEFACE_LAST] + * @param {Array<tcuTexture.ConstPixelBufferAccess>} faces1 (&faces1)[CUBEFACE_LAST] + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexture.CubeFaceCoords} coords + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isSeamplessLinearMipmapLinearSampleResultValid = function(faces0, faces1, sampler, prec, coords, fBounds, result) { + // \todo [2013-07-04 pyry] This is strictly not correct as coordinates between levels should be dependent. + // Right now this allows pairing any two valid bilinear quads. + /** @type {number} */ var size0 = faces0[coords.face].getWidth(); + /** @type {number} */ var size1 = faces1[coords.face].getWidth(); + + /** @type {Array<number>} */ var uBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size0, coords.s, prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ var uBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size1, coords.s, prec.coordBits[0], prec.uvwBits[0]); + /** @type {Array<number>} */ var vBounds0 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size0, coords.t, prec.coordBits[1], prec.uvwBits[1]); + /** @type {Array<number>} */ var vBounds1 = tcuTexVerifierUtil.computeNonNormalizedCoordBounds(sampler.normalizedCoords, size1, coords.t, prec.coordBits[1], prec.uvwBits[1]); + + // Integer coordinates - without wrap mode + /** @type {number} */ var minI0 = Math.floor(uBounds0[0] - 0.5); + /** @type {number} */ var maxI0 = Math.floor(uBounds0[1] - 0.5); + /** @type {number} */ var minI1 = Math.floor(uBounds1[0] - 0.5); + /** @type {number} */ var maxI1 = Math.floor(uBounds1[1] - 0.5); + /** @type {number} */ var minJ0 = Math.floor(vBounds0[0] - 0.5); + /** @type {number} */ var maxJ0 = Math.floor(vBounds0[1] - 0.5); + /** @type {number} */ var minJ1 = Math.floor(vBounds1[0] - 0.5); + /** @type {number} */ var maxJ1 = Math.floor(vBounds1[1] - 0.5); + + /** @type {tcuTexture.TextureChannelClass} */ var texClass = tcuTexture.getTextureChannelClass(faces0[coords.face].getFormat().type); + /** @type {number} */ var cSearchStep = (texClass == tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForUnorm(prec) : + (texClass == tcuTexture.TextureChannelClass.SIGNED_FIXED_POINT) ? tcuTexLookupVerifier.computeBilinearSearchStepForSnorm(prec) : + 0; // Step is computed for floating-point quads based on texel values. + + /** @type {tcuTexture.CubeFaceCoords} */ var c00; + /** @type {tcuTexture.CubeFaceCoords} */ var c10; + /** @type {tcuTexture.CubeFaceCoords} */ var c01; + /** @type {tcuTexture.CubeFaceCoords} */ var c11; + + for (var j0 = minJ0; j0 <= maxJ0; j0++) { + for (var i0 = minI0; i0 <= maxI0; i0++) { + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad0 = new tcuTexLookupVerifier.ColorQuad([], [], [], []); + /** @type {number} */ var searchStep0; + + c00 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i0 + 0, j0 + 0]), size0); + c10 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i0 + 1, j0 + 0]), size0); + c01 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i0 + 0, j0 + 1]), size0); + c11 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i0 + 1, j0 + 1]), size0); + + // If any of samples is out of both edges, implementations can do pretty much anything according to spec. + // \todo [2013-07-08 pyry] Test the special case where all corner pixels have exactly the same color. + if (c00 == null || c01 == null || c10 == null || c11 == null || + c00.face == null || c01.face == null || c10.face == null || c11.face == null) + return true; + + quad0.p00 = tcuTexLookupVerifier.lookupFloat(faces0[c00.face], sampler, c00.s, c00.t, 0); + quad0.p10 = tcuTexLookupVerifier.lookupFloat(faces0[c10.face], sampler, c10.s, c10.t, 0); + quad0.p01 = tcuTexLookupVerifier.lookupFloat(faces0[c01.face], sampler, c01.s, c01.t, 0); + quad0.p11 = tcuTexLookupVerifier.lookupFloat(faces0[c11.face], sampler, c11.s, c11.t, 0); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep0 = tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad0); + else + searchStep0 = cSearchStep; + + /** @type {number} */ var minA0 = deMath.clamp((uBounds0[0] - 0.5) - i0, 0, 1); + /** @type {number} */ var maxA0 = deMath.clamp((uBounds0[1] - 0.5) - i0, 0, 1); + /** @type {number} */ var minB0 = deMath.clamp((vBounds0[0] - 0.5) - j0, 0, 1); + /** @type {number} */ var maxB0 = deMath.clamp((vBounds0[1] - 0.5) - j0, 0, 1); + + for (var j1 = minJ1; j1 <= maxJ1; j1++) { + for (var i1 = minI1; i1 <= maxI1; i1++) { + /** @type {tcuTexLookupVerifier.ColorQuad} */ + var quad1 = new tcuTexLookupVerifier.ColorQuad([], [], [], []); + /** @type {number} */ var searchStep1; + + c00 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i1 + 0, j1 + 0]), size1); + c10 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i1 + 1, j1 + 0]), size1); + c01 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i1 + 0, j1 + 1]), size1); + c11 = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(coords.face, [i1 + 1, j1 + 1]), size1); + + if (c00 == null || c01 == null || c10 == null || c11 == null || + c00.face == null || c01.face == null || c10.face == null || c11.face == null) + return true; + + quad1.p00 = tcuTexLookupVerifier.lookupFloat(faces1[c00.face], sampler, c00.s, c00.t, 0); + quad1.p10 = tcuTexLookupVerifier.lookupFloat(faces1[c10.face], sampler, c10.s, c10.t, 0); + quad1.p01 = tcuTexLookupVerifier.lookupFloat(faces1[c01.face], sampler, c01.s, c01.t, 0); + quad1.p11 = tcuTexLookupVerifier.lookupFloat(faces1[c11.face], sampler, c11.s, c11.t, 0); + + if (texClass == tcuTexture.TextureChannelClass.FLOATING_POINT) + searchStep1 = tcuTexLookupVerifier.computeBilinearSearchStepFromFloatQuad(prec, quad1); + else + searchStep1 = cSearchStep; + + /** @type {number} */ var minA1 = deMath.clamp((uBounds1[0] - 0.5) - i1, 0, 1); + /** @type {number} */ var maxA1 = deMath.clamp((uBounds1[1] - 0.5) - i1, 0, 1); + /** @type {number} */ var minB1 = deMath.clamp((vBounds1[0] - 0.5) - j1, 0, 1); + /** @type {number} */ var maxB1 = deMath.clamp((vBounds1[1] - 0.5) - j1, 0, 1); + + if (tcuTexLookupVerifier.is2DTrilinearFilterResultValid(prec, quad0, quad1, [minA0, maxA0], [minB0, maxB0], [minA1, maxA1], [minB1, maxB1], + fBounds, Math.min(searchStep0, searchStep1), result)) + return true; + } + } + } + } + + return false; + }; + + /** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} level (&level)[CUBEFACE_LAST] + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} filterMode + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexture.CubeFaceCoords} coords + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isCubeLevelSampleResultValid = function(level, sampler, filterMode, prec, coords, result) { + if (filterMode == tcuTexture.FilterMode.LINEAR) { + if (sampler.seamlessCubeMap) + return tcuTexLookupVerifier.isSeamlessLinearSampleResultValid(level, sampler, prec, coords, result); + else + return tcuTexLookupVerifier.isLinearSampleResultValid_CoordAsVec2AndInt(level[coords.face], sampler, prec, [coords.s, coords.t], 0, result); + } else + return tcuTexLookupVerifier.isNearestSampleResultValid_CoordAsVec2AndInt(level[coords.face], sampler, prec, [coords.s, coords.t], 0, result); + }; + + /** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} faces0 (&faces0)[CUBEFACE_LAST] + * @param {Array<tcuTexture.ConstPixelBufferAccess>} faces1 (&faces1)[CUBEFACE_LAST] + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} levelFilter + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {tcuTexture.CubeFaceCoords} coords + * @param {Array<number>} fBounds + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isCubeMipmapLinearSampleResultValid = function(faces0, faces1, sampler, levelFilter, prec, coords, fBounds, result) { + if (levelFilter == tcuTexture.FilterMode.LINEAR) { + if (sampler.seamlessCubeMap) + return tcuTexLookupVerifier.isSeamplessLinearMipmapLinearSampleResultValid(faces0, faces1, sampler, prec, coords, fBounds, result); + else + return tcuTexLookupVerifier.isLinearMipmapLinearSampleResultValid_CoordAsVec2AndInt(faces0[coords.face], faces1[coords.face], sampler, prec, [coords.s, coords.t], 0, fBounds, result); + } else + return tcuTexLookupVerifier.isNearestMipmapLinearSampleResultValid_CoordAsVec2AndInt(faces0[coords.face], faces1[coords.face], sampler, prec, [coords.s, coords.t], 0, fBounds, result); + }; + + /** + * @param {tcuTexture.TextureCubeView} texture + * @param {number} levelNdx + * @param {Array<tcuTexture.ConstPixelBufferAccess>} out (&out)[CUBEFACE_LAST] + */ + tcuTexLookupVerifier.getCubeLevelFaces = function(texture, levelNdx, out) { + for (var faceNdx = 0; faceNdx < 6; faceNdx++) + out[faceNdx] = texture.getLevelFace(levelNdx, /** @type {tcuTexture.CubeFace} */ (faceNdx)); + }; + + /** + * @param {number} numLayers + * @param {number} numCoordBits + * @param {number} layerCoord + * @return {Array<number>} + */ + tcuTexLookupVerifier.computeLayerRange = function(numLayers, numCoordBits, layerCoord) { + /** @type {number} */ var err = tcuTexVerifierUtil.computeFloatingPointError(layerCoord, numCoordBits); + /** @type {number} */ var minL = Math.floor(layerCoord - err + 0.5); // Round down + /** @type {number} */ var maxL = Math.ceil(layerCoord + err + 0.5) - 1; // Round up + + assertMsgOptions(minL <= maxL, 'minL > maxL', false, true); + + return [deMath.clamp(minL, 0, numLayers - 1), deMath.clamp(maxL, 0, numLayers - 1)]; + }; + + /** + * @param {Array<number>} bits + * @return {Array<number>} + */ + tcuTexLookupVerifier.computeFixedPointThreshold = function(bits) { + return tcuTexVerifierUtil.computeFixedPointError_Vector(bits); + }; + + /** + * @param {Array<number>} bits + * @param {Array<number>} value + * @return {Array<number>} + */ + tcuTexLookupVerifier.computeFloatingPointThreshold = function(bits, value) { + return tcuTexVerifierUtil.computeFloatingPointError_Vector(value, bits); + }; + + /** + * @param {number} dudx + * @param {number} dvdx + * @param {number} dwdx + * @param {number} dudy + * @param {number} dvdy + * @param {number} dwdy + * @param {tcuTexLookupVerifier.LodPrecision} prec + * @return {Array<number>} + */ + tcuTexLookupVerifier.computeLodBoundsFromDerivates = function(dudx, dvdx, dwdx, dudy, dvdy, dwdy, prec) { + /** @type {number} */ var mu = Math.max(Math.abs(dudx), Math.abs(dudy)); + /** @type {number} */ var mv = Math.max(Math.abs(dvdx), Math.abs(dvdy)); + /** @type {number} */ var mw = Math.max(Math.abs(dwdx), Math.abs(dwdy)); + /** @type {number} */ var minDBound = Math.max(Math.max(mu, mv), mw); + /** @type {number} */ var maxDBound = mu + mv + mw; + /** @type {number} */ var minDErr = tcuTexVerifierUtil.computeFloatingPointError(minDBound, prec.derivateBits); + /** @type {number} */ var maxDErr = tcuTexVerifierUtil.computeFloatingPointError(maxDBound, prec.derivateBits); + /** @type {number} */ var minLod = Math.log2(minDBound - minDErr); + /** @type {number} */ var maxLod = Math.log2(maxDBound + maxDErr); + /** @type {number} */ var lodErr = tcuTexVerifierUtil.computeFixedPointError(prec.lodBits); + + assertMsgOptions(minLod <= maxLod, 'Error: minLod > maxLod', false, true); + return [minLod - lodErr, maxLod + lodErr]; + }; + + /** + * @param {number} dudx + * @param {number} dvdx + * @param {number} dudy + * @param {number} dvdy + * @param {tcuTexLookupVerifier.LodPrecision} prec + * @return {Array<number>} + */ + tcuTexLookupVerifier.computeLodBoundsFromDerivatesUV = function(dudx, dvdx, dudy, dvdy, prec) { + return tcuTexLookupVerifier.computeLodBoundsFromDerivates(dudx, dvdx, 0, dudy, dvdy, 0, prec); + }; + + /** + * @param {number} dudx + * @param {number} dudy + * @param {tcuTexLookupVerifier.LodPrecision} prec + * @return {Array<number>} + */ + tcuTexLookupVerifier.computeLodBoundsFromDerivatesU = function(dudx, dudy, prec) { + return tcuTexLookupVerifier.computeLodBoundsFromDerivates(dudx, 0, 0, dudy, 0, 0, prec); + }; + + /** + * @param {Array<number>} coord + * @param {Array<number>} coordDx + * @param {Array<number>} coordDy + * @param {number} faceSize + * @param {tcuTexLookupVerifier.LodPrecision} prec + * @return {Array<number>} + */ + tcuTexLookupVerifier.computeCubeLodBoundsFromDerivates = function(coord, coordDx, coordDy, faceSize, prec) { + /** @type {boolean} */ var allowBrokenEdgeDerivate = false; + /** @type {tcuTexture.CubeFace} */ var face = tcuTexture.selectCubeFace(coord); + /** @type {number} */ var maNdx = 0; + /** @type {number} */ var sNdx = 0; + /** @type {number} */ var tNdx = 0; + + // \note Derivate signs don't matter when computing lod + switch (face) { + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X: + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_X: maNdx = 0; sNdx = 2; tNdx = 1; break; + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y: + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y: maNdx = 1; sNdx = 0; tNdx = 2; break; + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z: + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z: maNdx = 2; sNdx = 0; tNdx = 1; break; + default: + throw new Error('Invalid CubeFace.'); + } + + /** @type {number} */ var sc = coord[sNdx]; + /** @type {number} */ var tc = coord[tNdx]; + /** @type {number} */ var ma = Math.abs(coord[maNdx]); + /** @type {number} */ var scdx = coordDx[sNdx]; + /** @type {number} */ var tcdx = coordDx[tNdx]; + /** @type {number} */ var madx = Math.abs(coordDx[maNdx]); + /** @type {number} */ var scdy = coordDy[sNdx]; + /** @type {number} */ var tcdy = coordDy[tNdx]; + /** @type {number} */ var mady = Math.abs(coordDy[maNdx]); + /** @type {number} */ var dudx = faceSize * 0.5 * (scdx * ma - sc * madx) / (ma * ma); + /** @type {number} */ var dvdx = faceSize * 0.5 * (tcdx * ma - tc * madx) / (ma * ma); + /** @type {number} */ var dudy = faceSize * 0.5 * (scdy * ma - sc * mady) / (ma * ma); + /** @type {number} */ var dvdy = faceSize * 0.5 * (tcdy * ma - tc * mady) / (ma * ma); + /** @type {Array<number>} */ var bounds = tcuTexLookupVerifier.computeLodBoundsFromDerivatesUV(dudx, dvdx, dudy, dvdy, prec); + + // Implementations may compute derivate from projected (s, t) resulting in incorrect values at edges. + if (allowBrokenEdgeDerivate) { + /** @type {Array<number>} */ var dxErr = tcuTexVerifierUtil.computeFloatingPointError_Vector(coordDx, [prec.derivateBits, prec.derivateBits, prec.derivateBits]); + /** @type {Array<number>} */ var dyErr = tcuTexVerifierUtil.computeFloatingPointError_Vector(coordDy, [prec.derivateBits, prec.derivateBits, prec.derivateBits]); + /** @type {Array<number>} */ var xoffs = deMath.add(deMath.abs(coordDx), dxErr); + /** @type {Array<number>} */ var yoffs = deMath.add(deMath.abs(coordDy), dyErr); + + if (tcuTexture.selectCubeFace(deMath.add(coord, xoffs)) != face || + tcuTexture.selectCubeFace(deMath.subtract(coord, xoffs)) != face || + tcuTexture.selectCubeFace(deMath.add(coord, yoffs)) != face || + tcuTexture.selectCubeFace(deMath.subtract(coord, yoffs)) != face) { + return [bounds[0], 1000]; + } + } + + return bounds; + }; + + /** + * @param {Array<number>} lodBounds + * @param {Array<number>} lodMinMax + * @param {tcuTexLookupVerifier.LodPrecision} prec + * @return {Array<number>} + */ + tcuTexLookupVerifier.clampLodBounds = function(lodBounds, lodMinMax, prec) { + /** @type {number} */ var lodErr = tcuTexVerifierUtil.computeFixedPointError(prec.lodBits); + /** @type {number} */ var a = lodMinMax[0]; + /** @type {number} */ var b = lodMinMax[1]; + return [deMath.clamp(lodBounds[0], a - lodErr, b - lodErr), deMath.clamp(lodBounds[1], a + lodErr, b + lodErr)]; + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.TexLookupScaleMode} scaleMode + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {number} coordZ + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLevel2DLookupResultValid = function(access, sampler, scaleMode, prec, coord, coordZ, result) { + /** @type {tcuTexture.FilterMode} */ + var filterMode = (scaleMode == tcuTexLookupVerifier.TexLookupScaleMode.MAGNIFY) ? sampler.magFilter : sampler.minFilter; + return tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec2AndInt(access, sampler, filterMode, prec, coord, coordZ, result); + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.TexLookupScaleMode} scaleMode + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {number} coordZ + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLevel2DLookupResultValid_Int = function(access, sampler, scaleMode, prec, coord, coordZ, result) { + assertMsgOptions(sampler.minFilter == tcuTexture.FilterMode.NEAREST && sampler.magFilter == tcuTexture.FilterMode.NEAREST, 'minFilter and magFilter must be NEAREST', false, true); + return tcuTexLookupVerifier.isNearestSampleResultValid_CoordAsVec2AndInt(access, sampler, prec, coord, coordZ, result); + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.TexLookupScaleMode} scaleMode + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLevel3DLookupResultValid = function(access, sampler, scaleMode, prec, coord, result) { + /** @type {tcuTexture.FilterMode} */ + var filterMode = (scaleMode == tcuTexLookupVerifier.TexLookupScaleMode.MAGNIFY) ? sampler.magFilter : sampler.minFilter; + return tcuTexLookupVerifier.isLevelSampleResultValid_CoordAsVec3(access, sampler, filterMode, prec, coord, result); + }; + + /** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexLookupVerifier.TexLookupScaleMode} scaleMode + * @param {tcuTexLookupVerifier.LookupPrecision} prec + * @param {Array<number>} coord + * @param {Array<number>} result + * @return {boolean} + */ + tcuTexLookupVerifier.isLevel3DLookupResultValid_Int = function(access, sampler, scaleMode, prec, coord, result) { + assertMsgOptions(sampler.minFilter == tcuTexture.FilterMode.NEAREST && sampler.magFilter == tcuTexture.FilterMode.NEAREST, 'minFilter and magFilter must be NEAREST', false, true); + return tcuTexLookupVerifier.isNearestSampleResultValid_CoordAsVec3(access, sampler, prec, coord, result); + }; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexVerifierUtil.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexVerifierUtil.js new file mode 100644 index 000000000..4c88f4460 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexVerifierUtil.js @@ -0,0 +1,265 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuTexVerifierUtil'); +goog.require('framework.common.tcuFloat'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.delibs.debase.deMath'); +goog.require('framework.delibs.debase.deUtil'); + +goog.scope(function() { + + var tcuTexVerifierUtil = framework.common.tcuTexVerifierUtil; + var deMath = framework.delibs.debase.deMath; + var deUtil = framework.delibs.debase.deUtil; + var tcuFloat = framework.common.tcuFloat; + var tcuTexture = framework.common.tcuTexture; + + /** + * @param {number} value + * @param {number} numAccurateBits + * @return {number} + */ + tcuTexVerifierUtil.computeFloatingPointError = function(value, numAccurateBits) { + /** @type {number} */ var numGarbageBits = 23 - numAccurateBits; + /** @type {number} */ var mask = (1 << numGarbageBits) - 1; + /** @type {number} */ var exp = tcuFloat.newFloat32(value).exponent(); + + /** @type {tcuFloat.deFloat} */ var v1 = new tcuFloat.deFloat(); + /** @type {tcuFloat.deFloat} */ var v2 = new tcuFloat.deFloat(); + return v1.construct(1, exp, 1 << 23 | mask).getValue() - v2.construct(1, exp, 1 << 23).getValue(); + }; + + /** + * @param {number} numAccurateBits + * @return {number} + */ + tcuTexVerifierUtil.computeFixedPointError = function(numAccurateBits) { + return tcuTexVerifierUtil.computeFloatingPointError(1.0, numAccurateBits); + }; + + /** + * @param {Array<number>} numAccurateBits + * @return {Array<number>} + */ + tcuTexVerifierUtil.computeFixedPointError_Vector = function(numAccurateBits) { + /** @type {Array<number>} */ var res = []; + for (var ndx = 0; ndx < numAccurateBits.length; ndx++) + res[ndx] = tcuTexVerifierUtil.computeFixedPointError(numAccurateBits[ndx]); + return res; + }; + + /** + * @param {Array<number>} value + * @param {Array<number>} numAccurateBits + * @return {Array<number>} + */ + tcuTexVerifierUtil.computeFloatingPointError_Vector = function(value, numAccurateBits) { + assertMsgOptions(value.length === numAccurateBits.length, '', false, true); + /** @type {Array<number>} */ var res = []; + for (var ndx = 0; ndx < value.length; ndx++) + res[ndx] = tcuTexVerifierUtil.computeFloatingPointError(value[ndx], numAccurateBits[ndx]); + return res; + }; + + // Sampler introspection + + /** + * @param {tcuTexture.FilterMode} mode + * @return {boolean} + */ + tcuTexVerifierUtil.isNearestMipmapFilter = function(mode) { + return mode == tcuTexture.FilterMode.NEAREST_MIPMAP_NEAREST || mode == tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST; + }; + + /** + * @param {tcuTexture.FilterMode} mode + * @return {boolean} + */ + tcuTexVerifierUtil.isLinearMipmapFilter = function(mode) { + return mode == tcuTexture.FilterMode.NEAREST_MIPMAP_LINEAR || mode == tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR; + }; + + /** + * @param {tcuTexture.FilterMode} mode + * @return {boolean} + */ + tcuTexVerifierUtil.isMipmapFilter = function(mode) { + return tcuTexVerifierUtil.isNearestMipmapFilter(mode) || tcuTexVerifierUtil.isLinearMipmapFilter(mode); + }; + + /** + * @param {tcuTexture.FilterMode} mode + * @return {boolean} + */ + tcuTexVerifierUtil.isLinearFilter = function(mode) { + return mode == tcuTexture.FilterMode.LINEAR || mode == tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST || mode == tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR; + }; + + /** + * @param {tcuTexture.FilterMode} mode + * @return {boolean} + */ + tcuTexVerifierUtil.isNearestFilter = function(mode) { + return !tcuTexVerifierUtil.isLinearFilter(mode); + }; + + /** + * @param {tcuTexture.FilterMode} mode + * @return {tcuTexture.FilterMode} + */ + tcuTexVerifierUtil.getLevelFilter = function(mode) { + return tcuTexVerifierUtil.isLinearFilter(mode) ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + }; + + /** + * @param {tcuTexture.WrapMode} mode + * @return {boolean} + */ + tcuTexVerifierUtil.isWrapModeSupported = function(mode) { + return mode != tcuTexture.WrapMode.MIRRORED_REPEAT_CL && mode != tcuTexture.WrapMode.REPEAT_CL; + }; + + /** + * + * @param {boolean} normalizedCoords + * @param {number} dim + * @param {number} coord + * @param {number} coordBits + * @param {number} uvBits + * @return {Array<number>} + */ + tcuTexVerifierUtil.computeNonNormalizedCoordBounds = function(normalizedCoords, dim, coord, coordBits, uvBits) { + /** @type {number} */ var coordErr = tcuTexVerifierUtil.computeFloatingPointError(coord, coordBits); + /** @type {number} */ var minN = coord - coordErr; + /** @type {number} */ var maxN = coord + coordErr; + /** @type {number} */ var minA = normalizedCoords ? minN * dim : minN; + /** @type {number} */ var maxA = normalizedCoords ? maxN * dim : maxN; + /** @type {number} */ var minC = minA - tcuTexVerifierUtil.computeFixedPointError(uvBits); + /** @type {number} */ var maxC = maxA + tcuTexVerifierUtil.computeFixedPointError(uvBits); + assertMsgOptions(minC <= maxC, '', false, true); + return [minC, maxC]; + }; + + /** + * @param {Array<number>} coord + * @param {Array<number>} bits + * @return {?Array<tcuTexture.CubeFace>} + */ + tcuTexVerifierUtil.getPossibleCubeFaces = function(coord, bits) { + + /** @type {Array<tcuTexture.CubeFace>} */ var faces = []; + + /** @type {number} */ var x = coord[0]; + /** @type {number} */ var y = coord[1]; + /** @type {number} */ var z = coord[2]; + /** @type {number} */ var ax = Math.abs(x); + /** @type {number} */ var ay = Math.abs(y); + /** @type {number} */ var az = Math.abs(z); + /** @type {number} */ var ex = tcuTexVerifierUtil.computeFloatingPointError(x, bits[0]); + /** @type {number} */ var ey = tcuTexVerifierUtil.computeFloatingPointError(y, bits[1]); + /** @type {number} */ var ez = tcuTexVerifierUtil.computeFloatingPointError(z, bits[2]); + /** @type {number} */ var numFaces = 0; + + if (ay + ey < ax - ex && az + ez < ax - ex) { + if (x >= ex) faces.push(tcuTexture.CubeFace.CUBEFACE_POSITIVE_X); + if (x <= ex) faces.push(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X); + } else if (ax + ex < ay - ey && az + ez < ay - ey) { + if (y >= ey) faces.push(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y); + if (y <= ey) faces.push(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y); + } else if (ax + ex < az - ez && ay + ey < az - ez) { + if (z >= ez) faces.push(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z); + if (z <= ez) faces.push(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z); + } else { + // One or more components are equal (or within error bounds). Allow all faces where major axis is not zero. + if (ax > ex) { + faces.push(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X); + faces.push(tcuTexture.CubeFace.CUBEFACE_POSITIVE_X); + } + + if (ay > ey) { + faces.push(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y); + faces.push(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y); + } + + if (az > ez) { + faces.push(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z); + faces.push(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z); + } + } + + return faces.length == 0 ? null : faces; + }; + + /** + * @param {tcuTexture.Sampler} sampler + * @return {tcuTexture.Sampler} + */ + tcuTexVerifierUtil.getUnnormalizedCoordSampler = function(sampler) { + var copy = /** @type {tcuTexture.Sampler} */ (deUtil.clone(sampler)); + copy.normalizedCoords = false; + return copy; + }; + + /** + * @param {number} a + * @param {number} b + * @return {number} + */ + tcuTexVerifierUtil.imod = function(a, b) { + return deMath.imod(a, b); + }; + + /** + * @param {number} a + * @return {number} + */ + tcuTexVerifierUtil.mirror = function(a) { + return deMath.mirror(a); + }; + + /** + * @param {tcuTexture.WrapMode} mode + * @param {number} c + * @param {number} size + * @return {number} + */ + tcuTexVerifierUtil.wrap = function(mode, c, size) { + switch (mode) { + // \note CL and GL modes are handled identically here, as verification process accounts for + // accuracy differences caused by different methods (wrapping vs. denormalizing first). + case tcuTexture.WrapMode.CLAMP_TO_EDGE: + return deMath.clamp(c, 0, size - 1); + + case tcuTexture.WrapMode.REPEAT_GL: + case tcuTexture.WrapMode.REPEAT_CL: + return deMath.imod(c, size); + + case tcuTexture.WrapMode.MIRRORED_REPEAT_GL: + case tcuTexture.WrapMode.MIRRORED_REPEAT_CL: + return (size - 1) - deMath.mirror(deMath.imod(c, 2 * size) - size); + + default: + throw new Error('Wrap mode not supported.'); + } + }; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexture.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexture.js new file mode 100644 index 000000000..8a3a2ed1d --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTexture.js @@ -0,0 +1,3636 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuTexture'); +goog.require('framework.common.tcuFloat'); +goog.require('framework.delibs.debase.deMath'); +goog.require('framework.delibs.debase.deString'); +goog.require('framework.delibs.debase.deUtil'); + +goog.scope(function() { + +var tcuTexture = framework.common.tcuTexture; +var deMath = framework.delibs.debase.deMath; +var tcuFloat = framework.common.tcuFloat; +var deString = framework.delibs.debase.deString; +var deUtil = framework.delibs.debase.deUtil; + +var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); +}; + +/** + * Texture tcuTexture.channel order + * @enum + */ +tcuTexture.ChannelOrder = { + R: 0, + A: 1, + I: 2, + L: 3, + LA: 4, + RG: 5, + RA: 6, + RGB: 7, + RGBA: 8, + ARGB: 9, + BGRA: 10, + + sRGB: 11, + sRGBA: 12, + + D: 13, + S: 14, + DS: 15 +}; + +/** + * Texture tcuTexture.channel type + * @enum + */ +tcuTexture.ChannelType = { + SNORM_INT8: 0, + SNORM_INT16: 1, + SNORM_INT32: 2, + UNORM_INT8: 3, + UNORM_INT16: 4, + UNORM_INT32: 5, + UNORM_SHORT_565: 6, + UNORM_SHORT_555: 7, + UNORM_SHORT_4444: 8, + UNORM_SHORT_5551: 9, + UNORM_INT_101010: 10, + UNORM_INT_1010102_REV: 11, + UNSIGNED_INT_1010102_REV: 12, + UNSIGNED_INT_11F_11F_10F_REV: 13, + UNSIGNED_INT_999_E5_REV: 14, + UNSIGNED_INT_24_8: 15, + SIGNED_INT8: 16, + SIGNED_INT16: 17, + SIGNED_INT32: 18, + UNSIGNED_INT8: 19, + UNSIGNED_INT16: 20, + UNSIGNED_INT32: 21, + HALF_FLOAT: 22, + FLOAT: 23, + FLOAT_UNSIGNED_INT_24_8_REV: 24 +}; + +/** + * Enums for tcuTexture.TextureChannelClass + * @enum {number} + */ +tcuTexture.TextureChannelClass = { + + SIGNED_FIXED_POINT: 0, + UNSIGNED_FIXED_POINT: 1, + SIGNED_INTEGER: 2, + UNSIGNED_INTEGER: 3, + FLOATING_POINT: 4 +}; + +/** + * @param {?tcuTexture.ChannelType} channelType + * @return {tcuTexture.TextureChannelClass} + */ +tcuTexture.getTextureChannelClass = function(channelType) { + + switch (channelType) { + case tcuTexture.ChannelType.SNORM_INT8: return tcuTexture.TextureChannelClass.SIGNED_FIXED_POINT; + case tcuTexture.ChannelType.SNORM_INT16: return tcuTexture.TextureChannelClass.SIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNORM_INT8: return tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNORM_INT16: return tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNORM_SHORT_565: return tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNORM_SHORT_555: return tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNORM_SHORT_4444: return tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNORM_SHORT_5551: return tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNORM_INT_101010: return tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: return tcuTexture.TextureChannelClass.UNSIGNED_FIXED_POINT; + case tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV: return tcuTexture.TextureChannelClass.UNSIGNED_INTEGER; + case tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV: return tcuTexture.TextureChannelClass.FLOATING_POINT; + case tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV: return tcuTexture.TextureChannelClass.FLOATING_POINT; + case tcuTexture.ChannelType.SIGNED_INT8: return tcuTexture.TextureChannelClass.SIGNED_INTEGER; + case tcuTexture.ChannelType.SIGNED_INT16: return tcuTexture.TextureChannelClass.SIGNED_INTEGER; + case tcuTexture.ChannelType.SIGNED_INT32: return tcuTexture.TextureChannelClass.SIGNED_INTEGER; + case tcuTexture.ChannelType.UNSIGNED_INT8: return tcuTexture.TextureChannelClass.UNSIGNED_INTEGER; + case tcuTexture.ChannelType.UNSIGNED_INT16: return tcuTexture.TextureChannelClass.UNSIGNED_INTEGER; + case tcuTexture.ChannelType.UNSIGNED_INT32: return tcuTexture.TextureChannelClass.UNSIGNED_INTEGER; + case tcuTexture.ChannelType.HALF_FLOAT: return tcuTexture.TextureChannelClass.FLOATING_POINT; + case tcuTexture.ChannelType.FLOAT: return tcuTexture.TextureChannelClass.FLOATING_POINT; + default: return /** @type {tcuTexture.TextureChannelClass<number>} */ (Object.keys(tcuTexture.ChannelType).length); + } +}; + +/** + * @param {tcuTexture.TextureFormat} format + */ +tcuTexture.isFixedPointDepthTextureFormat = function(format) { + var channelClass = tcuTexture.getTextureChannelClass(format.type); + + if (format.order == tcuTexture.ChannelOrder.D) { + // depth internal formats cannot be non-normalized integers + return channelClass != tcuTexture.TextureChannelClass.FLOATING_POINT; + } else if (format.order == tcuTexture.ChannelOrder.DS) { + // combined formats have no single channel class, detect format manually + switch (format.type) { + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: return false; + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: return true; + + default: + // unknown format + DE_ASSERT(false); + return true; + } + } + return false; +}; + +/** + * @param {Array<number>} color + * @param {tcuTexture.CompareMode} compare + * @param {number} chanNdx + * @param {number} ref_ + * @param {boolean} isFixedPoint + */ +tcuTexture.execCompare = function(color, compare, chanNdx, ref_, isFixedPoint) { + var clampValues = isFixedPoint; + var cmp = clampValues ? deMath.clamp(color[chanNdx], 0.0, 1.0) : color[chanNdx]; + var ref = clampValues ? deMath.clamp(ref_, 0.0, 1.0) : ref_; + var res = false; + + switch (compare) { + case tcuTexture.CompareMode.COMPAREMODE_LESS: res = ref < cmp; break; + case tcuTexture.CompareMode.COMPAREMODE_LESS_OR_EQUAL: res = ref <= cmp; break; + case tcuTexture.CompareMode.COMPAREMODE_GREATER: res = ref > cmp; break; + case tcuTexture.CompareMode.COMPAREMODE_GREATER_OR_EQUAL: res = ref >= cmp; break; + case tcuTexture.CompareMode.COMPAREMODE_EQUAL: res = ref == cmp; break; + case tcuTexture.CompareMode.COMPAREMODE_NOT_EQUAL: res = ref != cmp; break; + case tcuTexture.CompareMode.COMPAREMODE_ALWAYS: res = true; break; + case tcuTexture.CompareMode.COMPAREMODE_NEVER: res = false; break; + default: + DE_ASSERT(false); + } + + return res ? 1.0 : 0.0; +}; + +/** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} levels + * @param {number} numLevels + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {number} s + * @param {number} t + * @param {number} lod + * @param {Array<number>} offset + */ +tcuTexture.sampleLevelArray2DCompare = function(levels, numLevels, sampler, ref, s, t, lod, offset) { + var magnified = lod <= sampler.lodThreshold; + var filterMode = magnified ? sampler.magFilter : sampler.minFilter; + + switch (filterMode) { + case tcuTexture.FilterMode.NEAREST: return levels[0].sample2DCompare(sampler, filterMode, ref, s, t, offset); + case tcuTexture.FilterMode.LINEAR: return levels[0].sample2DCompare(sampler, filterMode, ref, s, t, offset); + + case tcuTexture.FilterMode.NEAREST_MIPMAP_NEAREST: + case tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST: { + var maxLevel = numLevels - 1; + var level = deMath.clamp(Math.ceil(lod + 0.5) - 1, 0, maxLevel); + var levelFilter = filterMode == tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + + return levels[level].sample2DCompare(sampler, levelFilter, ref, s, t, offset); + } + + case tcuTexture.FilterMode.NEAREST_MIPMAP_LINEAR: + case tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR: { + var maxLevel = numLevels - 1; + var level0 = deMath.clamp(Math.floor(lod), 0, maxLevel); + var level1 = Math.min(maxLevel, level0 + 1); + var levelFilter = filterMode == tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + var f = deMath.deFloatFrac(lod); + var t0 = levels[level0].sample2DCompare(sampler, levelFilter, ref, s, t, offset); + var t1 = levels[level1].sample2DCompare(sampler, levelFilter, ref, s, t, offset); + + return t0 * (1.0 - f) + t1 * f; + } + + default: + DE_ASSERT(false); + return 0.0; + } +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {number} u + * @param {number} v + * @param {Array<number>} offset + * @param {boolean} isFixedPointDepthFormat + * @return {number} + */ +tcuTexture.sampleLinear2DCompare = function(access, sampler, ref, u, v, offset, isFixedPointDepthFormat) { + var w = access.getWidth(); + var h = access.getHeight(); + + var x0 = Math.floor(u - 0.5) + offset[0]; + var x1 = x0 + 1; + var y0 = Math.floor(v - 0.5) + offset[1]; + var y1 = y0 + 1; + + var i0 = tcuTexture.wrap(sampler.wrapS, x0, w); + var i1 = tcuTexture.wrap(sampler.wrapS, x1, w); + var j0 = tcuTexture.wrap(sampler.wrapT, y0, h); + var j1 = tcuTexture.wrap(sampler.wrapT, y1, h); + + var a = deMath.deFloatFrac(u - 0.5); + var b = deMath.deFloatFrac(v - 0.5); + + var i0UseBorder = sampler.wrapS == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(i0, 0, w); + var i1UseBorder = sampler.wrapS == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(i1, 0, w); + var j0UseBorder = sampler.wrapT == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(j0, 0, h); + var j1UseBorder = sampler.wrapT == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(j1, 0, h); + + // Border color for out-of-range coordinates if using CLAMP_TO_BORDER, otherwise execute lookups. + var p00Clr = (i0UseBorder || j0UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i0, j0, offset[2]); + var p10Clr = (i1UseBorder || j0UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i1, j0, offset[2]); + var p01Clr = (i0UseBorder || j1UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i0, j1, offset[2]); + var p11Clr = (i1UseBorder || j1UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i1, j1, offset[2]); + + // Execute comparisons. + var p00 = tcuTexture.execCompare(p00Clr, sampler.compare, sampler.compareChannel, ref, isFixedPointDepthFormat); + var p10 = tcuTexture.execCompare(p10Clr, sampler.compare, sampler.compareChannel, ref, isFixedPointDepthFormat); + var p01 = tcuTexture.execCompare(p01Clr, sampler.compare, sampler.compareChannel, ref, isFixedPointDepthFormat); + var p11 = tcuTexture.execCompare(p11Clr, sampler.compare, sampler.compareChannel, ref, isFixedPointDepthFormat); + + // Interpolate. + return (p00 * (1.0 - a) * (1.0 - b)) + + (p10 * (a) * (1.0 - b)) + + (p01 * (1.0 - a) * (b)) + + (p11 * (a) * (b)); +}; + +/** + * Construct texture format + * @param {?tcuTexture.ChannelOrder} order + * @param {?tcuTexture.ChannelType} type + * + * @constructor + */ +tcuTexture.TextureFormat = function(order, type) { + this.order = order; + this.type = type; +}; + +/** + * Compare two formats + * @param {tcuTexture.TextureFormat} format Format to compare with + * @return {boolean} + */ +tcuTexture.TextureFormat.prototype.isEqual = function(format) { + return this.order === format.order && this.type === format.type; +}; + +tcuTexture.TextureFormat.prototype.toString = function() { + return 'TextureFormat(' + deString.enumToString(tcuTexture.ChannelOrder, this.order) + ', ' + + deString.enumToString(tcuTexture.ChannelType, this.type) + ')'; +}; + +/** + * Is format sRGB? + * @return {boolean} + */ +tcuTexture.TextureFormat.prototype.isSRGB = function() { + return this.order === tcuTexture.ChannelOrder.sRGB || this.order === tcuTexture.ChannelOrder.sRGBA; +}; + +tcuTexture.TextureFormat.prototype.getNumStencilBits = function() { + switch (this.order) { + case tcuTexture.ChannelOrder.S: + switch (this.type) { + case tcuTexture.ChannelType.UNSIGNED_INT8: return 8; + case tcuTexture.ChannelType.UNSIGNED_INT16: return 16; + case tcuTexture.ChannelType.UNSIGNED_INT32: return 32; + default: + throw new Error('Wrong type: ' + this.type); + } + + case tcuTexture.ChannelOrder.DS: + switch (this.type) { + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: return 8; + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: return 8; + default: + throw new Error('Wrong type: ' + this.type); + } + + default: + throw new Error('Wrong order: ' + this.order); + } +}; + +/** + * Get TypedArray type that can be used to access texture. + * @param {?tcuTexture.ChannelType} type + * @return TypedArray that supports the tcuTexture.channel type. + */ +tcuTexture.getTypedArray = function(type) { + switch (type) { + case tcuTexture.ChannelType.SNORM_INT8: return Int8Array; + case tcuTexture.ChannelType.SNORM_INT16: return Int16Array; + case tcuTexture.ChannelType.SNORM_INT32: return Int32Array; + case tcuTexture.ChannelType.UNORM_INT8: return Uint8Array; + case tcuTexture.ChannelType.UNORM_INT16: return Uint16Array; + case tcuTexture.ChannelType.UNORM_INT32: return Uint32Array; + case tcuTexture.ChannelType.UNORM_SHORT_565: return Uint16Array; + case tcuTexture.ChannelType.UNORM_SHORT_555: return Uint16Array; + case tcuTexture.ChannelType.UNORM_SHORT_4444: return Uint16Array; + case tcuTexture.ChannelType.UNORM_SHORT_5551: return Uint16Array; + case tcuTexture.ChannelType.UNORM_INT_101010: return Uint32Array; + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: return Uint32Array; + case tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV: return Uint32Array; + case tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV: return Uint32Array; + case tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV: return Uint32Array; + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: return Uint32Array; + case tcuTexture.ChannelType.FLOAT: return Float32Array; + case tcuTexture.ChannelType.SIGNED_INT8: return Int8Array; + case tcuTexture.ChannelType.SIGNED_INT16: return Int16Array; + case tcuTexture.ChannelType.SIGNED_INT32: return Int32Array; + case tcuTexture.ChannelType.UNSIGNED_INT8: return Uint8Array; + case tcuTexture.ChannelType.UNSIGNED_INT16: return Uint16Array; + case tcuTexture.ChannelType.UNSIGNED_INT32: return Uint32Array; + case tcuTexture.ChannelType.HALF_FLOAT: return Uint16Array; + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: return Float32Array; /* this type is a special case */ + } + + throw new Error('Unrecognized type ' + type); +}; + +/** + * @return {number} pixel size in bytes + */ +tcuTexture.TextureFormat.prototype.getPixelSize = function() { + if (this.type == null || this.order == null) { + // Invalid/empty format. + return 0; + } else if (this.type == tcuTexture.ChannelType.UNORM_SHORT_565 || + this.type == tcuTexture.ChannelType.UNORM_SHORT_555 || + this.type == tcuTexture.ChannelType.UNORM_SHORT_4444 || + this.type == tcuTexture.ChannelType.UNORM_SHORT_5551) { + DE_ASSERT(this.order == tcuTexture.ChannelOrder.RGB || this.order == tcuTexture.ChannelOrder.RGBA); + return 2; + } else if (this.type == tcuTexture.ChannelType.UNORM_INT_101010 || + this.type == tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV || + this.type == tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV) { + DE_ASSERT(this.order == tcuTexture.ChannelOrder.RGB); + return 4; + } else if (this.type == tcuTexture.ChannelType.UNORM_INT_1010102_REV || + this.type == tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV) { + DE_ASSERT(this.order == tcuTexture.ChannelOrder.RGBA); + return 4; + } else if (this.type == tcuTexture.ChannelType.UNSIGNED_INT_24_8) { + DE_ASSERT(this.order == tcuTexture.ChannelOrder.D || this.order == tcuTexture.ChannelOrder.DS); + return 4; + } else if (this.type == tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV) { + DE_ASSERT(this.order == tcuTexture.ChannelOrder.DS); + return 8; + } else { + var numChannels; + var channelSize; + + switch (this.order) { + case tcuTexture.ChannelOrder.R: numChannels = 1; break; + case tcuTexture.ChannelOrder.A: numChannels = 1; break; + case tcuTexture.ChannelOrder.I: numChannels = 1; break; + case tcuTexture.ChannelOrder.L: numChannels = 1; break; + case tcuTexture.ChannelOrder.LA: numChannels = 2; break; + case tcuTexture.ChannelOrder.RG: numChannels = 2; break; + case tcuTexture.ChannelOrder.RA: numChannels = 2; break; + case tcuTexture.ChannelOrder.RGB: numChannels = 3; break; + case tcuTexture.ChannelOrder.RGBA: numChannels = 4; break; + case tcuTexture.ChannelOrder.ARGB: numChannels = 4; break; + case tcuTexture.ChannelOrder.BGRA: numChannels = 4; break; + case tcuTexture.ChannelOrder.sRGB: numChannels = 3; break; + case tcuTexture.ChannelOrder.sRGBA: numChannels = 4; break; + case tcuTexture.ChannelOrder.D: numChannels = 1; break; + case tcuTexture.ChannelOrder.S: numChannels = 1; break; + case tcuTexture.ChannelOrder.DS: numChannels = 2; break; + default: DE_ASSERT(false); + } + + switch (this.type) { + case tcuTexture.ChannelType.SNORM_INT8: channelSize = 1; break; + case tcuTexture.ChannelType.SNORM_INT16: channelSize = 2; break; + case tcuTexture.ChannelType.SNORM_INT32: channelSize = 4; break; + case tcuTexture.ChannelType.UNORM_INT8: channelSize = 1; break; + case tcuTexture.ChannelType.UNORM_INT16: channelSize = 2; break; + case tcuTexture.ChannelType.UNORM_INT32: channelSize = 4; break; + case tcuTexture.ChannelType.SIGNED_INT8: channelSize = 1; break; + case tcuTexture.ChannelType.SIGNED_INT16: channelSize = 2; break; + case tcuTexture.ChannelType.SIGNED_INT32: channelSize = 4; break; + case tcuTexture.ChannelType.UNSIGNED_INT8: channelSize = 1; break; + case tcuTexture.ChannelType.UNSIGNED_INT16: channelSize = 2; break; + case tcuTexture.ChannelType.UNSIGNED_INT32: channelSize = 4; break; + case tcuTexture.ChannelType.HALF_FLOAT: channelSize = 2; break; + case tcuTexture.ChannelType.FLOAT: channelSize = 4; break; + default: DE_ASSERT(false); + } + + return numChannels * channelSize; + } +}; + +/** + * @enum + */ +tcuTexture.CubeFace = { + CUBEFACE_NEGATIVE_X: 0, + CUBEFACE_POSITIVE_X: 1, + CUBEFACE_NEGATIVE_Y: 2, + CUBEFACE_POSITIVE_Y: 3, + CUBEFACE_NEGATIVE_Z: 4, + CUBEFACE_POSITIVE_Z: 5 +}; + +/** + * Renamed from ArrayBuffer due to name clash + * Wraps ArrayBuffer. + * @constructor + * @param {number=} numElements + */ +tcuTexture.DeqpArrayBuffer = function(numElements) { + if (numElements) + this.m_ptr = new ArrayBuffer(numElements); +}; + +/** + * Set array size + * @param {number} numElements Size in bytes + */ +tcuTexture.DeqpArrayBuffer.prototype.setStorage = function(numElements) { + this.m_ptr = new ArrayBuffer(numElements); +}; + +/** + * @return {number} Buffer size + */ +tcuTexture.DeqpArrayBuffer.prototype.size = function() { + if (this.m_ptr) + return this.m_ptr.byteLength; + + return 0; +}; + +/** + * Is the buffer empty (zero size)? + * @return {boolean} + */ +tcuTexture.DeqpArrayBuffer.prototype.empty = function() { + if (!this.m_ptr) + return true; + return this.size() == 0; +}; + +/** + * @enum + * The values are negative to avoid conflict with channels 0 - 3 + */ +tcuTexture.channel = { + ZERO: -1, + ONE: -2 +}; + +/** + * @param {tcuTexture.ChannelOrder} order + * @return {Array<Number|tcuTexture.channel>} + */ +tcuTexture.getChannelReadMap = function(order) { + switch (order) { + /*static const Channel INV[] = { tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, tcuTexture.channel.ONE }; */ + + case tcuTexture.ChannelOrder.R: return [0, tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, tcuTexture.channel.ONE]; + case tcuTexture.ChannelOrder.A: return [tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, 0]; + case tcuTexture.ChannelOrder.I: return [0, 0, 0, 0]; + case tcuTexture.ChannelOrder.L: return [0, 0, 0, tcuTexture.channel.ONE]; + case tcuTexture.ChannelOrder.LA: return [0, 0, 0, 1]; + case tcuTexture.ChannelOrder.RG: return [0, 1, tcuTexture.channel.ZERO, tcuTexture.channel.ONE]; + case tcuTexture.ChannelOrder.RA: return [0, tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, 1]; + case tcuTexture.ChannelOrder.RGB: return [0, 1, 2, tcuTexture.channel.ONE]; + case tcuTexture.ChannelOrder.RGBA: return [0, 1, 2, 3]; + case tcuTexture.ChannelOrder.BGRA: return [2, 1, 0, 3]; + case tcuTexture.ChannelOrder.ARGB: return [1, 2, 3, 0]; + case tcuTexture.ChannelOrder.sRGB: return [0, 1, 2, tcuTexture.channel.ONE]; + case tcuTexture.ChannelOrder.sRGBA: return [0, 1, 2, 3]; + case tcuTexture.ChannelOrder.D: return [0, tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, tcuTexture.channel.ONE]; + case tcuTexture.ChannelOrder.S: return [tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, 0]; + case tcuTexture.ChannelOrder.DS: return [0, tcuTexture.channel.ZERO, tcuTexture.channel.ZERO, 1]; + } + + throw new Error('Unrecognized order ' + order); +}; + +/** + * @param {tcuTexture.ChannelOrder} order + * @return {Array<number>} + */ +tcuTexture.getChannelWriteMap = function(order) { + switch (order) { + case tcuTexture.ChannelOrder.R: return [0]; + case tcuTexture.ChannelOrder.A: return [3]; + case tcuTexture.ChannelOrder.I: return [0]; + case tcuTexture.ChannelOrder.L: return [0]; + case tcuTexture.ChannelOrder.LA: return [0, 3]; + case tcuTexture.ChannelOrder.RG: return [0, 1]; + case tcuTexture.ChannelOrder.RA: return [0, 3]; + case tcuTexture.ChannelOrder.RGB: return [0, 1, 2]; + case tcuTexture.ChannelOrder.RGBA: return [0, 1, 2, 3]; + case tcuTexture.ChannelOrder.ARGB: return [3, 0, 1, 2]; + case tcuTexture.ChannelOrder.BGRA: return [2, 1, 0, 3]; + case tcuTexture.ChannelOrder.sRGB: return [0, 1, 2]; + case tcuTexture.ChannelOrder.sRGBA: return [0, 1, 2, 3]; + case tcuTexture.ChannelOrder.D: return [0]; + case tcuTexture.ChannelOrder.S: return [3]; + case tcuTexture.ChannelOrder.DS: return [0, 3]; + } + throw new Error('Unrecognized order ' + order); +}; + +/** + * @param {tcuTexture.ChannelType} type + * @return {number} + */ +tcuTexture.getChannelSize = function(type) { + switch (type) { + case tcuTexture.ChannelType.SNORM_INT8: return 1; + case tcuTexture.ChannelType.SNORM_INT16: return 2; + case tcuTexture.ChannelType.SNORM_INT32: return 4; + case tcuTexture.ChannelType.UNORM_INT8: return 1; + case tcuTexture.ChannelType.UNORM_INT16: return 2; + case tcuTexture.ChannelType.UNORM_INT32: return 4; + case tcuTexture.ChannelType.SIGNED_INT8: return 1; + case tcuTexture.ChannelType.SIGNED_INT16: return 2; + case tcuTexture.ChannelType.SIGNED_INT32: return 4; + case tcuTexture.ChannelType.UNSIGNED_INT8: return 1; + case tcuTexture.ChannelType.UNSIGNED_INT16: return 2; + case tcuTexture.ChannelType.UNSIGNED_INT32: return 4; + case tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV: return 4; + case tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV: return 4; + case tcuTexture.ChannelType.HALF_FLOAT: return 2; + case tcuTexture.ChannelType.FLOAT: return 4; + + } + throw new Error('Unrecognized type ' + deString.enumToString(tcuTexture.ChannelType, type)); +}; + +/** + * @param {number} src Source value + * @param {number} bits Source value size in bits + * @return {number} Normalized value + */ +tcuTexture.channelToNormFloat = function(src, bits) { + var maxVal = (1 << bits) - 1; + return src / maxVal; +}; + +/** + * @param {number} value Source value + * @param {tcuTexture.ChannelType} type + * @return {number} Source value converted to float + */ +tcuTexture.channelToFloat = function(value, type) { + switch (type) { + case tcuTexture.ChannelType.SNORM_INT8: return Math.max(-1, value / 127); + case tcuTexture.ChannelType.SNORM_INT16: return Math.max(-1, value / 32767); + case tcuTexture.ChannelType.SNORM_INT32: return Math.max(-1, value / 2147483647); + case tcuTexture.ChannelType.UNORM_INT8: return value / 255; + case tcuTexture.ChannelType.UNORM_INT16: return value / 65535; + case tcuTexture.ChannelType.UNORM_INT32: return value / 4294967295; + case tcuTexture.ChannelType.SIGNED_INT8: return value; + case tcuTexture.ChannelType.SIGNED_INT16: return value; + case tcuTexture.ChannelType.SIGNED_INT32: return value; + case tcuTexture.ChannelType.UNSIGNED_INT8: return value; + case tcuTexture.ChannelType.UNSIGNED_INT16: return value; + case tcuTexture.ChannelType.UNSIGNED_INT32: return value; + case tcuTexture.ChannelType.HALF_FLOAT: return tcuFloat.halfFloatToNumber(value); + case tcuTexture.ChannelType.FLOAT: return value; + } + throw new Error('Unrecognized tcuTexture.channel type ' + type); +}; + +/** + * @param {number} value Source value + * @param {tcuTexture.ChannelType} type + * @return {number} Source value converted to int + */ +tcuTexture.channelToInt = function(value, type) { + switch (type) { + case tcuTexture.ChannelType.HALF_FLOAT: return Math.round(tcuFloat.halfFloatToNumber(value)); + case tcuTexture.ChannelType.FLOAT: return Math.round(value); + default: + return value; + } +}; + +/** + * @param {tcuTexture.ChannelOrder} order + * @return {number} + */ +tcuTexture.getNumUsedChannels = function(order) { + switch (order) { + case tcuTexture.ChannelOrder.R: return 1; + case tcuTexture.ChannelOrder.A: return 1; + case tcuTexture.ChannelOrder.I: return 1; + case tcuTexture.ChannelOrder.L: return 1; + case tcuTexture.ChannelOrder.LA: return 2; + case tcuTexture.ChannelOrder.RG: return 2; + case tcuTexture.ChannelOrder.RA: return 2; + case tcuTexture.ChannelOrder.RGB: return 3; + case tcuTexture.ChannelOrder.RGBA: return 4; + case tcuTexture.ChannelOrder.ARGB: return 4; + case tcuTexture.ChannelOrder.BGRA: return 4; + case tcuTexture.ChannelOrder.sRGB: return 3; + case tcuTexture.ChannelOrder.sRGBA: return 4; + case tcuTexture.ChannelOrder.D: return 1; + case tcuTexture.ChannelOrder.S: return 1; + case tcuTexture.ChannelOrder.DS: return 2; + } + throw new Error('Unrecognized tcuTexture.channel order ' + order); +}; + +/** + * @enum + */ +tcuTexture.WrapMode = { + CLAMP_TO_EDGE: 0, //! Clamp to edge + CLAMP_TO_BORDER: 1, //! Use border color at edge + REPEAT_GL: 2, //! Repeat with OpenGL semantics + REPEAT_CL: 3, //! Repeat with OpenCL semantics + MIRRORED_REPEAT_GL: 4, //! Mirrored repeat with OpenGL semantics + MIRRORED_REPEAT_CL: 5 //! Mirrored repeat with OpenCL semantics +}; + +/** + * @enum + */ +tcuTexture.FilterMode = { + NEAREST: 0, + LINEAR: 1, + + NEAREST_MIPMAP_NEAREST: 2, + NEAREST_MIPMAP_LINEAR: 3, + LINEAR_MIPMAP_NEAREST: 4, + LINEAR_MIPMAP_LINEAR: 5 +}; + +/** + * @enum + */ +tcuTexture.CompareMode = { + COMPAREMODE_NONE: 0, + COMPAREMODE_LESS: 1, + COMPAREMODE_LESS_OR_EQUAL: 2, + COMPAREMODE_GREATER: 3, + COMPAREMODE_GREATER_OR_EQUAL: 4, + COMPAREMODE_EQUAL: 5, + COMPAREMODE_NOT_EQUAL: 6, + COMPAREMODE_ALWAYS: 7, + COMPAREMODE_NEVER: 8 +}; + +/** + * @constructor + * @param {!tcuTexture.WrapMode} wrapS + * @param {!tcuTexture.WrapMode} wrapT + * @param {!tcuTexture.WrapMode} wrapR + * @param {!tcuTexture.FilterMode} minFilter + * @param {!tcuTexture.FilterMode} magFilter + * @param {number=} lodThreshold + * @param {boolean=} normalizedCoords + * @param {tcuTexture.CompareMode=} compare + * @param {number=} compareChannel + * @param {Array<number>=} borderColor + * @param {boolean=} seamlessCubeMap + */ +tcuTexture.Sampler = function(wrapS, wrapT, wrapR, minFilter, magFilter, lodThreshold, normalizedCoords, compare, compareChannel, borderColor, seamlessCubeMap) { + /** @type {!tcuTexture.WrapMode} */ this.wrapS = wrapS; + /** @type {!tcuTexture.WrapMode} */ this.wrapT = wrapT; + /** @type {!tcuTexture.WrapMode} */ this.wrapR = wrapR; + /** @type {!tcuTexture.FilterMode} */ this.minFilter = minFilter; + /** @type {!tcuTexture.FilterMode} */ this.magFilter = magFilter; + this.lodThreshold = lodThreshold || 0; + this.normalizedCoords = normalizedCoords === undefined ? true : normalizedCoords; + /** @type {tcuTexture.CompareMode} */ this.compare = compare || tcuTexture.CompareMode.COMPAREMODE_NONE; + this.compareChannel = compareChannel || 0; + this.borderColor = borderColor || [0, 0, 0, 0]; + this.seamlessCubeMap = seamlessCubeMap || false; +}; + +/** + * Special unnormalization for REPEAT_CL and MIRRORED_REPEAT_CL tcuTexture.wrap modes; otherwise ordinary unnormalization. + * @param {tcuTexture.WrapMode} mode + * @param {number} c Value to tcuTexture.unnormalize + * @param {number} size Unnormalized type size (integer) + * @return {number} + */ +tcuTexture.unnormalize = function(mode, c, size) { + switch (mode) { + case tcuTexture.WrapMode.CLAMP_TO_EDGE: + case tcuTexture.WrapMode.CLAMP_TO_BORDER: + case tcuTexture.WrapMode.REPEAT_GL: + case tcuTexture.WrapMode.MIRRORED_REPEAT_GL: // Fall-through (ordinary case). + return size * c; + + case tcuTexture.WrapMode.REPEAT_CL: + return size * (c - Math.floor(c)); + + case tcuTexture.WrapMode.MIRRORED_REPEAT_CL: + return size * Math.abs(c - 2 * deMath.rint(0.5 * c)); + } + throw new Error('Unrecognized tcuTexture.wrap mode ' + mode); +}; + +/** + * @param {tcuTexture.WrapMode} mode + * @param {number} c Source value (integer) + * @param {number} size Type size (integer) + * @return {number} + */ +tcuTexture.wrap = function(mode, c, size) { + switch (mode) { + case tcuTexture.WrapMode.CLAMP_TO_BORDER: + return deMath.clamp(c, -1, size); + + case tcuTexture.WrapMode.CLAMP_TO_EDGE: + return deMath.clamp(c, 0, size - 1); + + case tcuTexture.WrapMode.REPEAT_GL: + return deMath.imod(c, size); + + case tcuTexture.WrapMode.REPEAT_CL: + return deMath.imod(c, size); + + case tcuTexture.WrapMode.MIRRORED_REPEAT_GL: + return (size - 1) - deMath.mirror(deMath.imod(c, 2 * size) - size); + + case tcuTexture.WrapMode.MIRRORED_REPEAT_CL: + return deMath.clamp(c, 0, size - 1); // \note Actual mirroring done already in unnormalization function. + } + throw new Error('Unrecognized tcuTexture.wrap mode ' + mode); +}; + +/** + * @param {number} cs + * @return {number} + */ +tcuTexture.sRGBChannelToLinear = function(cs) { + if (cs <= 0.04045) + return cs / 12.92; + else + return Math.pow((cs + 0.055) / 1.055, 2.4); +}; + +/** + * Convert sRGB to linear colorspace + * @param {Array<number>} cs Vec4 + * @return {Array<number>} Vec4 + */ +tcuTexture.sRGBToLinear = function(cs) { + return [ + tcuTexture.sRGBChannelToLinear(cs[0]), + tcuTexture.sRGBChannelToLinear(cs[1]), + tcuTexture.sRGBChannelToLinear(cs[2]), + cs[3] + ]; +}; + +/** + * Texel tcuTexture.lookup with color conversion. + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {number} i + * @param {number} j + * @param {number} k + * @return {Array<number>} Vec4 pixel color + */ +tcuTexture.lookup = function(access, i, j, k) { + var p = access.getPixel(i, j, k); + return access.getFormat().isSRGB() ? tcuTexture.sRGBToLinear(p) : p; +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {number} u + * @param {number} v + * @param {(number|Array<number>)} depthOrOffset depth (int) or offset (ivec3) + * @return {Array<number>} Vec4 pixel color + */ +tcuTexture.sampleLinear2D = function(access, sampler, u, v, depthOrOffset) { + /** @type {number} */ var xOffset = 0; + /** @type {number} */ var yOffset = 0; + /** @type {number} */ var value; + if (Array.isArray(depthOrOffset)) { + xOffset = depthOrOffset[0]; + yOffset = depthOrOffset[1]; + value = depthOrOffset[2]; + } else { + value = /** @type {number} */ (depthOrOffset); + } + + /** + * @param {Array<number>} p00 + * @param {Array<number>} p10 + * @param {Array<number>} p01 + * @param {Array<number>} p11 + * @param {number} a + * @param {number} b + */ + var interpolateQuad = function(p00, p10, p01, p11, a, b) { + var s00 = (1 - a) * (1 - b); + var s10 = a * (1 - b); + var s01 = (1 - a) * b; + var s11 = a * b; + + return [ + (p00[0] * s00) + (p10[0] * s10) + (p01[0] * s01) + (p11[0] * s11), + (p00[1] * s00) + (p10[1] * s10) + (p01[1] * s01) + (p11[1] * s11), + (p00[2] * s00) + (p10[2] * s10) + (p01[2] * s01) + (p11[2] * s11), + (p00[3] * s00) + (p10[3] * s10) + (p01[3] * s01) + (p11[3] * s11) + ]; + }; + + var w = access.getWidth(); + var h = access.getHeight(); + + var x0 = Math.floor(u - 0.5) + xOffset; + var x1 = x0 + 1; + var y0 = Math.floor(v - 0.5) + yOffset; + var y1 = y0 + 1; + + var i0 = tcuTexture.wrap(sampler.wrapS, x0, w); + var i1 = tcuTexture.wrap(sampler.wrapS, x1, w); + var j0 = tcuTexture.wrap(sampler.wrapT, y0, h); + var j1 = tcuTexture.wrap(sampler.wrapT, y1, h); + + var a = deMath.deFloatFrac(u - 0.5); + var b = deMath.deFloatFrac(v - 0.5); + + var i0UseBorder = sampler.wrapS == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(i0, 0, w); + var i1UseBorder = sampler.wrapS == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(i1, 0, w); + var j0UseBorder = sampler.wrapT == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(j0, 0, h); + var j1UseBorder = sampler.wrapT == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(j1, 0, h); + + // Border color for out-of-range coordinates if using CLAMP_TO_BORDER, otherwise execute lookups. + var p00 = (i0UseBorder || j0UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i0, j0, value); + var p10 = (i1UseBorder || j0UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i1, j0, value); + var p01 = (i0UseBorder || j1UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i0, j1, value); + var p11 = (i1UseBorder || j1UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i1, j1, value); + + // Interpolate. + return interpolateQuad(p00, p10, p01, p11, a, b); +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {number} u + * @param {number} v + * @param {number} w + * @param {Array<number>=} offset + * @return {Array<number>} Vec4 pixel color + */ +tcuTexture.sampleLinear3D = function(access, sampler, u, v, w, offset) { + /** + * @param {Array<number>} p000 + * @param {Array<number>} p100 + * @param {Array<number>} p010 + * @param {Array<number>} p110 + * @param {Array<number>} p001 + * @param {Array<number>} p101 + * @param {Array<number>} p011 + * @param {Array<number>} p111 + * @param {number} a + * @param {number} b + * @param {number} c + */ + var interpolateCube = function(p000, p100, p010, p110, p001, p101, p011, p111, a, b, c) { + var s000 = (1 - a) * (1 - b) * (1 - c); + var s100 = a * (1 - b) * (1 - c); + var s010 = (1 - a) * b * (1 - c); + var s110 = a * b * (1 - c); + var s001 = (1 - a) * (1 - b) * c; + var s101 = a * (1 - b) * c; + var s011 = (1 - a) * b * c; + var s111 = a * b * c; + + return [ + (p000[0] * s000) + (p100[0] * s100) + (p010[0] * s010) + (p110[0] * s110) + (p001[0] * s001) + (p101[0] * s101) + (p011[0] * s011) + (p111[0] * s111), + (p000[1] * s000) + (p100[1] * s100) + (p010[1] * s010) + (p110[1] * s110) + (p001[1] * s001) + (p101[1] * s101) + (p011[1] * s011) + (p111[1] * s111), + (p000[2] * s000) + (p100[2] * s100) + (p010[2] * s010) + (p110[2] * s110) + (p001[2] * s001) + (p101[2] * s101) + (p011[2] * s011) + (p111[2] * s111), + (p000[3] * s000) + (p100[3] * s100) + (p010[3] * s010) + (p110[3] * s110) + (p001[3] * s001) + (p101[3] * s101) + (p011[3] * s011) + (p111[3] * s111) + ]; + }; + + var width = access.getWidth(); + var height = access.getHeight(); + var depth = access.getDepth(); + + /** @type {number} */ var xOffset = 0; + /** @type {number} */ var yOffset = 0; + /** @type {number} */ var zOffset = 0; + + if (offset !== undefined && offset.length === 3) { + xOffset = offset[0]; + yOffset = offset[1]; + zOffset = offset[2]; + } + + var x0 = Math.floor(u - 0.5) + xOffset; + var x1 = x0 + 1; + var y0 = Math.floor(v - 0.5) + yOffset; + var y1 = y0 + 1; + var z0 = Math.floor(w - 0.5) + zOffset; + var z1 = z0 + 1; + + var i0 = tcuTexture.wrap(sampler.wrapS, x0, width); + var i1 = tcuTexture.wrap(sampler.wrapS, x1, width); + var j0 = tcuTexture.wrap(sampler.wrapT, y0, height); + var j1 = tcuTexture.wrap(sampler.wrapT, y1, height); + var k0 = tcuTexture.wrap(sampler.wrapR, z0, depth); + var k1 = tcuTexture.wrap(sampler.wrapR, z1, depth); + + var a = deMath.deFloatFrac(u - 0.5); + var b = deMath.deFloatFrac(v - 0.5); + var c = deMath.deFloatFrac(w - 0.5); + + var i0UseBorder = sampler.wrapS == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(i0, 0, width); + var i1UseBorder = sampler.wrapS == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(i1, 0, width); + var j0UseBorder = sampler.wrapT == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(j0, 0, height); + var j1UseBorder = sampler.wrapT == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(j1, 0, height); + var k0UseBorder = sampler.wrapR == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(k0, 0, depth); + var k1UseBorder = sampler.wrapR == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(k1, 0, depth); + + // Border color for out-of-range coordinates if using CLAMP_TO_BORDER, otherwise execute lookups. + var p000 = (i0UseBorder || j0UseBorder || k0UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i0, j0, k0); + var p100 = (i1UseBorder || j0UseBorder || k0UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i1, j0, k0); + var p010 = (i0UseBorder || j1UseBorder || k0UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i0, j1, k0); + var p110 = (i1UseBorder || j1UseBorder || k0UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i1, j1, k0); + var p001 = (i0UseBorder || j0UseBorder || k1UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i0, j0, k1); + var p101 = (i1UseBorder || j0UseBorder || k1UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i1, j0, k1); + var p011 = (i0UseBorder || j1UseBorder || k1UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i0, j1, k1); + var p111 = (i1UseBorder || j1UseBorder || k1UseBorder) ? sampler.borderColor : tcuTexture.lookup(access, i1, j1, k1); + + // Interpolate. + return interpolateCube(p000, p100, p010, p110, p001, p101, p011, p111, a, b, c); +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {number} u + * @param {number} v + * @param {(number|Array<number>)} depthOrOffset depth (integer) or offset (ivec3) + * @return {Array<number>} Vec4 pixel color + */ +tcuTexture.sampleNearest2D = function(access, sampler, u, v, depthOrOffset) { + /** @type {number} */ var xOffset = 0; + /** @type {number} */ var yOffset = 0; + /** @type {number} */ var value; + if (Array.isArray(depthOrOffset)) { + xOffset = depthOrOffset[0]; + yOffset = depthOrOffset[1]; + value = depthOrOffset[2]; + } else { + value = /** @type {number} */ (depthOrOffset); + } + + var width = access.getWidth(); + var height = access.getHeight(); + + var x = Math.floor(u) + xOffset; + var y = Math.floor(v) + yOffset; + + // Check for CLAMP_TO_BORDER. + if ((sampler.wrapS == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(x, 0, width)) || + (sampler.wrapT == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(y, 0, height))) + return sampler.borderColor; + + var i = tcuTexture.wrap(sampler.wrapS, x, width); + var j = tcuTexture.wrap(sampler.wrapT, y, height); + + return tcuTexture.lookup(access, i, j, value); +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @param {tcuTexture.Sampler} sampler + * @param {number} u + * @param {number} v + * @param {number} w + * @param {Array<number>=} offset + * @return {Array<number>} Vec4 pixel color + */ +tcuTexture.sampleNearest3D = function(access, sampler, u, v, w, offset) { + var width = access.getWidth(); + var height = access.getHeight(); + var depth = access.getDepth(); + /** @type {number} */ var xOffset = 0; + /** @type {number} */ var yOffset = 0; + /** @type {number} */ var zOffset = 0; + + if (offset !== undefined && offset.length === 3) { + xOffset = offset[0]; + yOffset = offset[1]; + zOffset = offset[2]; + } + + var x = Math.floor(u) + xOffset; + var y = Math.floor(v) + yOffset; + var z = Math.floor(w) + zOffset; + + // Check for CLAMP_TO_BORDER. + if ((sampler.wrapS == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(x, 0, width)) || + (sampler.wrapT == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(y, 0, height)) || + (sampler.wrapR == tcuTexture.WrapMode.CLAMP_TO_BORDER && !deMath.deInBounds32(z, 0, depth))) + return sampler.borderColor; + + var i = tcuTexture.wrap(sampler.wrapS, x, width); + var j = tcuTexture.wrap(sampler.wrapT, y, height); + var k = tcuTexture.wrap(sampler.wrapR, z, depth); + + return tcuTexture.lookup(access, i, j, k); +}; + +/** + * @param {Array<number>} color Vec4 color + * @return {number} The color in packed 32 bit format + */ +tcuTexture.packRGB999E5 = function(color) { + /** @const */ var mBits = 9; + /** @const */ var eBits = 5; + /** @const */ var eBias = 15; + /** @const */ var eMax = (1 << eBits) - 1; + /** @const */ var maxVal = (((1 << mBits) - 1) * (1 << (eMax - eBias))) / (1 << mBits); + + var rc = deMath.clamp(color[0], 0, maxVal); + var gc = deMath.clamp(color[1], 0, maxVal); + var bc = deMath.clamp(color[2], 0, maxVal); + var maxc = Math.max(rc, gc, bc); + var expp = Math.max(-eBias - 1, Math.floor(Math.log2(maxc))) + 1 + eBias; + var e = Math.pow(2, expp - eBias - mBits); + var maxs = Math.floor(maxc / e + 0.5); + + var exps = maxs == (1 << mBits) ? expp + 1 : expp; + var rs = deMath.clamp(Math.floor(rc / e + 0.5), 0, (1 << 9) - 1); + var gs = deMath.clamp(Math.floor(gc / e + 0.5), 0, (1 << 9) - 1); + var bs = deMath.clamp(Math.floor(bc / e + 0.5), 0, (1 << 9) - 1); + + DE_ASSERT((exps & ~((1 << 5) - 1)) == 0); + DE_ASSERT((rs & ~((1 << 9) - 1)) == 0); + DE_ASSERT((gs & ~((1 << 9) - 1)) == 0); + DE_ASSERT((bs & ~((1 << 9) - 1)) == 0); + + return rs | (gs << 9) | (bs << 18) | (exps << 27); +}; + +/** + * @param {number} color Color in packed 32 bit format + * @return {Array<number>} The color in unpacked format + */ +tcuTexture.unpackRGB999E5 = function(color) { + var mBits = 9; + var eBias = 15; + + var exp = (color >> 27) & ((1 << 5) - 1); + var bs = (color >> 18) & ((1 << 9) - 1); + var gs = (color >> 9) & ((1 << 9) - 1); + var rs = color & ((1 << 9) - 1); + + var e = Math.pow(2, (exp - eBias - mBits)); + var r = rs * e; + var g = gs * e; + var b = bs * e; + + return [r, g, b, 1]; +}; + +/** + * \brief Read-only pixel data access + * + * tcuTexture.ConstPixelBufferAccess encapsulates pixel data pointer along with + * format and layout information. It can be used for read-only access + * to arbitrary pixel buffers. + * + * Access objects are like iterators or pointers. They can be passed around + * as values and are valid as long as the storage doesn't change. + * @constructor + */ +tcuTexture.ConstPixelBufferAccess = function(descriptor) { + if (descriptor) { + this.m_offset = descriptor.offset || 0; + this.m_format = descriptor.format || new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.FLOAT); + this.m_width = descriptor.width; + this.m_height = descriptor.height; + if (descriptor.depth) + this.m_depth = descriptor.depth; + else + this.m_depth = 1; + this.m_data = descriptor.data; + if (descriptor.rowPitch) + this.m_rowPitch = descriptor.rowPitch; + else + this.m_rowPitch = this.m_width * this.m_format.getPixelSize(); + + if (descriptor.slicePitch) + this.m_slicePitch = descriptor.slicePitch; + else + this.m_slicePitch = this.m_rowPitch * this.m_height; + + if (this.m_format.isEqual(new tcuTexture.TextureFormat( + tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8))) + this.m_rgba8View = new tcuTexture.RGBA8View(this); + else if (this.m_format.isEqual(new tcuTexture.TextureFormat( + tcuTexture.ChannelOrder.RGB, tcuTexture.ChannelType.UNORM_INT8))) + this.m_rgb8View = new tcuTexture.RGBA8View(this); + + } + + this.m_dataPtrType = null; + this.m_dataPtr = null; +}; + +tcuTexture.ConstPixelBufferAccess.prototype.toString = function() { + var str = 'BufferAccess(format: ' + this.m_format + + ', width: ' + this.m_width + + ', height: ' + this.m_height; + if (this.m_depth > 1) + str += ', depth: ' + this.m_depth; + if (this.m_rowPitch != this.m_width * this.m_format.getPixelSize()) + str += ', row pitch: ' + this.m_rowPitch; + if (this.m_slicePitch != this.m_rowPitch * this.m_height) + str += ', slice pitch: ' + this.m_slicePitch; + if (this.m_offset > 0) + str += ', offset: ' + this.m_offset; + str += ')'; + return str; +}; + +/** @return {number} */ +tcuTexture.ConstPixelBufferAccess.prototype.getDataSize = function() { return this.m_depth * this.m_slicePitch; }; +tcuTexture.ConstPixelBufferAccess.prototype.isEmpty = function() { return this.m_width == 0 || this.m_height == 0 || this.m_depth == 0; }; +/** @return {goog.TypedArray} */ +tcuTexture.ConstPixelBufferAccess.prototype.getDataPtr = function() { + if (this.m_dataPtrType != this.m_format.type) { + this.m_dataPtrType = this.m_format.type; + var arrayType = tcuTexture.getTypedArray(this.m_format.type); + this.m_dataPtr = new arrayType(this.m_data, this.m_offset); + } + return this.m_dataPtr; +}; +/** @return {ArrayBuffer} */ +tcuTexture.ConstPixelBufferAccess.prototype.getBuffer = function() { + return this.m_data; +}; +/** @return {number} */ +tcuTexture.ConstPixelBufferAccess.prototype.getRowPitch = function() { return this.m_rowPitch; }; +/** @return {number} */ +tcuTexture.ConstPixelBufferAccess.prototype.getWidth = function() { return this.m_width; }; +/** @return {number} */ +tcuTexture.ConstPixelBufferAccess.prototype.getHeight = function() { return this.m_height; }; +/** @return {number} */ +tcuTexture.ConstPixelBufferAccess.prototype.getDepth = function() { return this.m_depth; }; +/** @return {number} */ +tcuTexture.ConstPixelBufferAccess.prototype.getSlicePitch = function() { return this.m_slicePitch; }; +/** @return {tcuTexture.TextureFormat} */ +tcuTexture.ConstPixelBufferAccess.prototype.getFormat = function() { return this.m_format; }; + +/** + * @param {number} x + * @param {number} y + * @param {number=} z + * @return {number} stencil value + */ +tcuTexture.ConstPixelBufferAccess.prototype.getPixStencil = function(x, y, z) { + z = z || 0; + + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + DE_ASSERT(deMath.deInBounds32(z, 0, this.m_depth)); + + // Make sure that the position is 'integer' + x = Math.round(x); + y = Math.round(y); + z = Math.round(z); + + var pixelSize = this.m_format.getPixelSize(); + var offset = z * this.m_slicePitch + y * this.m_rowPitch + x * pixelSize; + var pixelPtr = this.getDataPtr(); + var pixelPtrOffset = offset / pixelPtr.BYTES_PER_ELEMENT; + + switch (this.m_format.type) { + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + switch (this.m_format.order) { + case tcuTexture.ChannelOrder.S: return (pixelPtr[pixelPtrOffset] >> 8) & 0xff; + case tcuTexture.ChannelOrder.DS: return pixelPtr[pixelPtrOffset] & 0xff; + + default: + DE_ASSERT(false); + return 0; + } + + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.DS); + var u32array = new Uint32Array(this.m_data, offset + this.m_offset + 4, 1); + return u32array[0] & 0xff; + + default: { + if (this.m_format.order == tcuTexture.ChannelOrder.S) + return tcuTexture.channelToInt(pixelPtr[pixelPtrOffset], this.m_format.type); + else { + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.DS); + var stencilChannelIndex = 3; + return tcuTexture.channelToInt(pixelPtr[stencilChannelIndex + pixelPtrOffset], this.m_format.type); + } + } + } +}; + +/** + * @param {number} x + * @param {number} y + * @param {number=} z + * @return {number} + */ +tcuTexture.ConstPixelBufferAccess.prototype.getPixDepth = function(x, y, z) { + if (z == null) + z = 0; + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + DE_ASSERT(deMath.deInBounds32(z, 0, this.m_depth)); + + // Make sure that the position is 'integer' + x = Math.round(x); + y = Math.round(y); + z = Math.round(z); + + var pixelSize = this.m_format.getPixelSize(); + var offset = z * this.m_slicePitch + y * this.m_rowPitch + x * pixelSize; + var pixelPtr = this.getDataPtr(); + var pixelPtrOffset = offset / pixelPtr.BYTES_PER_ELEMENT; + + var ub = function(pixel, offset, count) { + return (pixel >> offset) & ((1 << count) - 1); + }; + var nb = function(pixel, offset, count) { + return tcuTexture.channelToNormFloat(ub(pixel, offset, count), count); + }; + + // Packed formats. + switch (this.m_format.type) { + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + switch (this.m_format.order) { + case tcuTexture.ChannelOrder.D: // fall-through + case tcuTexture.ChannelOrder.DS: + return nb(pixelPtr[pixelPtrOffset], 8, 24); + default: + throw new Error('Unsupported tcuTexture.channel order ' + this.m_format.order); + } + break; + + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: { + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.DS); + return pixelPtr[pixelPtrOffset]; + break; + } + + default: { + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.D || this.m_format.order == tcuTexture.ChannelOrder.DS); + return tcuTexture.channelToFloat(pixelPtr[pixelPtrOffset], this.m_format.type); + } + } +}; + +/** + * @param {number} x + * @param {number} y + * @param {number=} z + * @return {Array<number>} Pixel value as Vec4 + */ +tcuTexture.ConstPixelBufferAccess.prototype.getPixel = function(x, y, z) { + z = z || 0; + + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + DE_ASSERT(deMath.deInBounds32(z, 0, this.m_depth)); + + // Make sure that the position is 'integer' + return this._getPixelInternal(Math.round(x), Math.round(y), Math.round(z)); +}; + +// NOTE: getPixel has been broken into getPixel, _getPixelInternal, and _getPixelPacked +// because having them combined previously was causing V8 depots +tcuTexture.ConstPixelBufferAccess.prototype._getPixelInternal = function(x, y, z) { + // Quick paths + if (z == 0) { + if (this.m_rgba8View) { + var color = this.m_rgba8View.read(x, y, 4); + color[0] /= 255; + color[1] /= 255; + color[2] /= 255; + color[3] /= 255; + return color; + } else if (this.m_rgb8View) { + var color = this.m_rgb8View.read(x, y, 3); + color[0] /= 255; + color[1] /= 255; + color[2] /= 255; + color[3] = 1; + return color; + } + } + + var pixelSize = this.m_format.getPixelSize(); + var offset = z * this.m_slicePitch + y * this.m_rowPitch + x * pixelSize; + + var pixelPtr = this.getDataPtr(); + var pixelPtrOffset = offset / pixelPtr.BYTES_PER_ELEMENT; + + return this._getPixelPacked(pixelPtr, pixelPtrOffset); +}; + +tcuTexture.ConstPixelBufferAccess.prototype._getPixelPacked = (function() { + + var ub = function(pixel, offset, count) { + return (pixel >> offset) & ((1 << count) - 1); + }; + var nb = function(pixel, offset, count) { + var maxVal = (1 << count) - 1; + return ((pixel >> offset) & ((1 << count) - 1)) / maxVal; + }; + var f11 = tcuFloat.float11ToNumber; + var f10 = tcuFloat.float10ToNumber; + + return function tcuTexture_ConstPixelBufferAccess_getPixelPacked(pixelPtr, pixelPtrOffset) { + var pixel = pixelPtr[pixelPtrOffset]; + + // Packed formats. + switch (this.m_format.type) { + case tcuTexture.ChannelType.UNORM_SHORT_565: return [nb(pixel, 11, 5), nb(pixel, 5, 6), nb(pixel, 0, 5), 1]; + case tcuTexture.ChannelType.UNORM_SHORT_555: return [nb(pixel, 10, 5), nb(pixel, 5, 5), nb(pixel, 0, 5), 1]; + case tcuTexture.ChannelType.UNORM_SHORT_4444: return [nb(pixel, 12, 4), nb(pixel, 8, 4), nb(pixel, 4, 4), nb(pixel, 0, 4)]; + case tcuTexture.ChannelType.UNORM_SHORT_5551: return [nb(pixel, 11, 5), nb(pixel, 6, 5), nb(pixel, 1, 5), nb(pixel, 0, 1)]; + case tcuTexture.ChannelType.UNORM_INT_101010: return [nb(pixel, 22, 10), nb(pixel, 12, 10), nb(pixel, 2, 10), 1]; + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: return [nb(pixel, 0, 10), nb(pixel, 10, 10), nb(pixel, 20, 10), nb(pixel, 30, 2)]; + case tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV: return [ub(pixel, 0, 10), ub(pixel, 10, 10), ub(pixel, 20, 10), ub(pixel, 30, 2)]; + case tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV: return tcuTexture.unpackRGB999E5(pixel); + + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + switch (this.m_format.order) { + // \note Stencil is always ignored. + case tcuTexture.ChannelOrder.D: return [nb(pixel, 8, 24), 0, 0, 1]; + case tcuTexture.ChannelOrder.DS: return [nb(pixel, 8, 24), 0, 0, 1 /* (float)ub(0, 8) */]; + default: + DE_ASSERT(false); + } + + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: { + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.DS); + // \note Stencil is ignored. + return [pixel, 0, 0, 1]; + } + + case tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV: { + return [f11(ub(pixel, 0, 11)), f11(ub(pixel, 11, 11)), f10(ub(pixel, 22, 10)), 1]; + } + + default: + break; + } + + // Generic path. + var result = [0, 0, 0, 0]; + var channelMap = tcuTexture.getChannelReadMap(this.m_format.order); + var channelSize = tcuTexture.getChannelSize(this.m_format.type); + + for (var c = 0; c < 4; c++) { + var map = channelMap[c]; + if (map == tcuTexture.channel.ZERO) + result[c] = 0; + else if (map == tcuTexture.channel.ONE) + result[c] = 1; + else + result[c] = tcuTexture.channelToFloat(pixelPtr[map + pixelPtrOffset], this.m_format.type); + } + + return result; + }; +})(); + +/** + * @param {number} x + * @param {number} y + * @param {number=} z + * @return {Array<number>} Pixel value as Vec4 + */ +tcuTexture.ConstPixelBufferAccess.prototype.getPixelInt = function(x, y, z) { + z = z || 0; + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + DE_ASSERT(deMath.deInBounds32(z, 0, this.m_depth)); + + // Make sure that the position is 'integer' + x = Math.round(x); + y = Math.round(y); + z = Math.round(z); + + // Quick paths + if (z == 0) { + if (this.m_rgba8View) + return this.m_rgba8View.read(x, y, 4); + else if (this.m_rgb8View) + return this.m_rgb8View.read(x, y, 3); + } + + var pixelSize = this.m_format.getPixelSize(); + var offset = z * this.m_slicePitch + y * this.m_rowPitch + x * pixelSize; + + var pixelPtr = this.getDataPtr(); + var pixelPtrOffset = offset / pixelPtr.BYTES_PER_ELEMENT; + var pixel = pixelPtr[pixelPtrOffset]; + + var ub = function(pixel, offset, count) { + return (pixel >> offset) & ((1 << count) - 1); + }; + + // Packed formats. + switch (this.m_format.type) { + case tcuTexture.ChannelType.UNORM_SHORT_565: return [ub(pixel, 11, 5), ub(pixel, 5, 6), ub(pixel, 0, 5), 1]; + case tcuTexture.ChannelType.UNORM_SHORT_555: return [ub(pixel, 10, 5), ub(pixel, 5, 5), ub(pixel, 0, 5), 1]; + case tcuTexture.ChannelType.UNORM_SHORT_4444: return [ub(pixel, 12, 4), ub(pixel, 8, 4), ub(pixel, 4, 4), ub(pixel, 0, 4)]; + case tcuTexture.ChannelType.UNORM_SHORT_5551: return [ub(pixel, 11, 5), ub(pixel, 6, 5), ub(pixel, 1, 5), ub(pixel, 0, 1)]; + case tcuTexture.ChannelType.UNORM_INT_101010: return [ub(pixel, 22, 10), ub(pixel, 12, 10), ub(pixel, 2, 10), 1]; + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: return [ub(pixel, 0, 10), ub(pixel, 10, 10), ub(pixel, 20, 10), ub(pixel, 30, 2)]; + case tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV: return [ub(pixel, 0, 10), ub(pixel, 10, 10), ub(pixel, 20, 10), ub(pixel, 30, 2)]; + + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + switch (this.m_format.order) { + case tcuTexture.ChannelOrder.D: return [ub(pixel, 8, 24), 0, 0, 1]; + case tcuTexture.ChannelOrder.S: return [0, 0, 0, ub(pixel, 8, 24)]; + case tcuTexture.ChannelOrder.DS: return [ub(pixel, 8, 24), 0, 0, ub(pixel, 0, 8)]; + default: + DE_ASSERT(false); + } + + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: { + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.DS); + var u32array = new Uint32Array(this.m_data, this.m_offset + offset + 4, 1); + return [pixel, 0, 0, ub(u32array[0], 0, 8)]; + } + + default: + break; + } + + // Generic path. + var result = []; + result.length = 4; + var channelMap = tcuTexture.getChannelReadMap(this.m_format.order); + var channelSize = tcuTexture.getChannelSize(this.m_format.type); + + for (var c = 0; c < 4; c++) { + var map = channelMap[c]; + if (map == tcuTexture.channel.ZERO) + result[c] = 0; + else if (map == tcuTexture.channel.ONE) + result[c] = 1; + else + result[c] = tcuTexture.channelToInt(pixelPtr[map + pixelPtrOffset], this.m_format.type); + } + + return result; +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {?tcuTexture.FilterMode} filter + * @param {number} s + * @param {number} t + * @param {number} depth (integer) + * @return {Array<number>} Sample color + */ +tcuTexture.ConstPixelBufferAccess.prototype.sample2D = function(sampler, filter, s, t, depth) { + DE_ASSERT(deMath.deInBounds32(depth, 0, this.m_depth)); + + // Non-normalized coordinates. + var u = s; + var v = t; + + if (sampler.normalizedCoords) { + u = tcuTexture.unnormalize(sampler.wrapS, s, this.m_width); + v = tcuTexture.unnormalize(sampler.wrapT, t, this.m_height); + } + + switch (filter) { + case tcuTexture.FilterMode.NEAREST: return tcuTexture.sampleNearest2D(this, sampler, u, v, depth); + case tcuTexture.FilterMode.LINEAR: return tcuTexture.sampleLinear2D(this, sampler, u, v, depth); + default: + throw new Error('Invalid filter:' + filter); + } + throw new Error('Unimplemented'); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {?tcuTexture.FilterMode} filter + * @param {number} s + * @param {number} t + * @param {Array<number>} offset + * @return {Array<number>} Sample color + */ +tcuTexture.ConstPixelBufferAccess.prototype.sample2DOffset = function(sampler, filter, s, t, offset) { + DE_ASSERT(deMath.deInBounds32(offset[2], 0, this.m_depth)); + + // Non-normalized coordinates. + var u = s; + var v = t; + + if (sampler.normalizedCoords) { + u = tcuTexture.unnormalize(sampler.wrapS, s, this.m_width); + v = tcuTexture.unnormalize(sampler.wrapT, t, this.m_height); + } + + switch (filter) { + case tcuTexture.FilterMode.NEAREST: return tcuTexture.sampleNearest2D(this, sampler, u, v, offset); + case tcuTexture.FilterMode.LINEAR: return tcuTexture.sampleLinear2D(this, sampler, u, v, offset); + default: + throw new Error('Invalid filter:' + filter); + } +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {?tcuTexture.FilterMode} filter + * @param {number} s + * @param {number} t + * @param {number} r + * @param {Array<number>} offset + * @return {Array<number>} Sample color + */ +tcuTexture.ConstPixelBufferAccess.prototype.sample3DOffset = function(sampler, filter, s, t, r, offset) { + // Non-normalized coordinates. + /** @type {number} */ var u = s; + /** @type {number} */ var v = t; + /** @type {number} */ var w = r; + + if (sampler.normalizedCoords) { + u = tcuTexture.unnormalize(sampler.wrapS, s, this.m_width); + v = tcuTexture.unnormalize(sampler.wrapT, t, this.m_height); + w = tcuTexture.unnormalize(sampler.wrapR, r, this.m_depth); + } + + switch (filter) { + case tcuTexture.FilterMode.NEAREST: return tcuTexture.sampleNearest3D(this, sampler, u, v, w, offset); + case tcuTexture.FilterMode.LINEAR: return tcuTexture.sampleLinear3D(this, sampler, u, v, w, offset); + default: + throw new Error('Invalid filter:' + filter); + } +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} filter + * @param {number} ref + * @param {number} s + * @param {number} t + * @param {Array<number>} offset + * @return {number} + */ +tcuTexture.ConstPixelBufferAccess.prototype.sample2DCompare = function(sampler, filter, ref, s, t, offset) { + DE_ASSERT(deMath.deInBounds32(offset[2], 0, this.m_depth)); + + // Format information for comparison function + var isFixedPointDepth = tcuTexture.isFixedPointDepthTextureFormat(this.m_format); + + // Non-normalized coordinates. + var u = s; + var v = t; + + if (sampler.normalizedCoords) { + u = tcuTexture.unnormalize(sampler.wrapS, s, this.m_width); + v = tcuTexture.unnormalize(sampler.wrapT, t, this.m_height); + } + + switch (filter) { + case tcuTexture.FilterMode.NEAREST: return tcuTexture.execCompare(tcuTexture.sampleNearest2D(this, sampler, u, v, offset), sampler.compare, sampler.compareChannel, ref, isFixedPointDepth); + case tcuTexture.FilterMode.LINEAR: return tcuTexture.sampleLinear2DCompare(this, sampler, ref, u, v, offset, isFixedPointDepth); + default: + DE_ASSERT(false); + return 0.0; + } +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {tcuTexture.FilterMode} filter + * @param {number} s + * @param {number} t + * @param {number} r + * @return {Array<number>} Sample color + */ +tcuTexture.ConstPixelBufferAccess.prototype.sample3D = function(sampler, filter, s, t, r) { + // Non-normalized coordinates. + var u = s; + var v = t; + var w = r; + + if (sampler.normalizedCoords) { + u = tcuTexture.unnormalize(sampler.wrapS, s, this.m_width); + v = tcuTexture.unnormalize(sampler.wrapT, t, this.m_height); + w = tcuTexture.unnormalize(sampler.wrapR, r, this.m_depth); + } + + switch (filter) { + case tcuTexture.FilterMode.NEAREST: return tcuTexture.sampleNearest3D(this, sampler, u, v, w); + case tcuTexture.FilterMode.LINEAR: return tcuTexture.sampleLinear3D(this, sampler, u, v, w); + default: + throw new Error('Invalid filter:' + filter); + } + throw new Error('Unimplemented'); +}; + + /* TODO: do we need any of these? */ { + // template<typename T> + // Vector<T, 4> getPixelT (int x, int y, int z = 0) const; + + // Vec4 sample3D (const tcuTexture.Sampler& sampler, tcuTexture.tcuTexture.Sampler.tcuTexture.FilterMode filter, float s, float t, float r) const; + + // Vec4 sample2DOffset (const tcuTexture.Sampler& sampler, tcuTexture.Sampler::tcuTexture.FilterMode filter, float s, float t, const IVec3& offset) const; + // Vec4 sample3DOffset (const tcuTexture.Sampler& sampler, tcuTexture.Sampler::tcuTexture.FilterMode filter, float s, float t, float r, const IVec3& offset) const; + + // float sample2DCompare (const tcuTexture.Sampler& sampler, tcuTexture.Sampler::tcuTexture.FilterMode filter, float ref, float s, float t, const IVec3& offset) const; + }; + +/** Common type limits + * + */ +tcuTexture.deTypes = { + deInt8: {min: -(1 << 7), max: (1 << 7) - 1}, + deInt16: {min: -(1 << 15), max: (1 << 15) - 1}, + deInt32: {min: -2147483648, max: 2147483647}, + deUint8: {min: 0, max: (1 << 8) - 1}, + deUint16: {min: 0, max: (1 << 16) - 1}, + deUint32: {min: 0, max: 4294967295} +}; + +/** + * Round to even and saturate + * @param {{max: number, min: number}} deType from tcuTexture.deTypes + * @param {number} value + * @return {number} + */ +tcuTexture.convertSatRte = function(deType, value) { + var minVal = deType.min; + var maxVal = deType.max; + var floor = Math.floor(value); + var frac = value - floor; + if (frac == 0.5) { + if (floor % 2 != 0) + floor += 1; + } else if (frac > 0.5) { + floor += 1; + } + + return Math.max(minVal, Math.min(maxVal, floor)); +}; + +/** + * Saturate value to type range + * @param { {max: number, min: number}} deType from tcuTexture.deTypes + * @param {number} src + * @return {number} + */ +tcuTexture.convertSat = function(deType, src) { + var minVal = deType.min; + var maxVal = deType.max; + if (src < minVal) + return minVal; + else if (src > maxVal) + return maxVal; + else + return src; +}; + +/** + * @param {number} src Input integer value + * @param {tcuTexture.ChannelType} type + * @return {number} + */ +tcuTexture.intToChannel = function(src, type) { + var dst; + switch (type) { + case tcuTexture.ChannelType.SNORM_INT8: dst = tcuTexture.convertSat(tcuTexture.deTypes.deInt8, src); break; + case tcuTexture.ChannelType.SNORM_INT16: dst = tcuTexture.convertSat(tcuTexture.deTypes.deInt16, src); break; + case tcuTexture.ChannelType.UNORM_INT8: dst = tcuTexture.convertSat(tcuTexture.deTypes.deUint8, src); break; + case tcuTexture.ChannelType.UNORM_INT16: dst = tcuTexture.convertSat(tcuTexture.deTypes.deUint16, src); break; + case tcuTexture.ChannelType.SIGNED_INT8: dst = tcuTexture.convertSat(tcuTexture.deTypes.deInt8, src); break; + case tcuTexture.ChannelType.SIGNED_INT16: dst = tcuTexture.convertSat(tcuTexture.deTypes.deInt16, src); break; + case tcuTexture.ChannelType.SIGNED_INT32: dst = tcuTexture.convertSat(tcuTexture.deTypes.deInt32, src); break; + case tcuTexture.ChannelType.UNSIGNED_INT8: dst = tcuTexture.convertSat(tcuTexture.deTypes.deUint8, src); break; + case tcuTexture.ChannelType.UNSIGNED_INT16: dst = tcuTexture.convertSat(tcuTexture.deTypes.deUint16, src); break; + case tcuTexture.ChannelType.UNSIGNED_INT32: dst = tcuTexture.convertSat(tcuTexture.deTypes.deUint32, src); break; + case tcuTexture.ChannelType.HALF_FLOAT: dst = tcuFloat.numberToHalfFloat(src); break; + case tcuTexture.ChannelType.FLOAT: dst = src; break; + default: + throw new Error('Unrecognized tcuTexture.channel type: ' + type); + } + return dst; +}; + +/** + * @param {number} src + * @param {number} bits + * @return {number} + */ +tcuTexture.normFloatToChannel = function(src, bits) { + var maxVal = (1 << bits) - 1; + var intVal = tcuTexture.convertSatRte(tcuTexture.deTypes.deUint32, src * maxVal); + return Math.min(maxVal, intVal); +}; + +/** + * @param {number} src + * @param {number} bits + * @return {number} + */ +tcuTexture.uintToChannel = function(src, bits) { + var maxVal = (1 << bits) - 1; + return Math.min(maxVal, src); +}; + +/** + * @param {number} src + * @param {tcuTexture.ChannelType} type + * @return {number} Converted src color value + */ +tcuTexture.floatToChannel = function(src, type) { + switch (type) { + case tcuTexture.ChannelType.SNORM_INT8: return tcuTexture.convertSatRte(tcuTexture.deTypes.deInt8, src * 127); + case tcuTexture.ChannelType.SNORM_INT16: return tcuTexture.convertSatRte(tcuTexture.deTypes.deInt16, src * 32767); + case tcuTexture.ChannelType.SNORM_INT32: return tcuTexture.convertSatRte(tcuTexture.deTypes.deInt32, src * 2147483647); + case tcuTexture.ChannelType.UNORM_INT8: return tcuTexture.convertSatRte(tcuTexture.deTypes.deUint8, src * 255); + case tcuTexture.ChannelType.UNORM_INT16: return tcuTexture.convertSatRte(tcuTexture.deTypes.deUint16, src * 65535); + case tcuTexture.ChannelType.UNORM_INT32: return tcuTexture.convertSatRte(tcuTexture.deTypes.deUint32, src * 4294967295); + case tcuTexture.ChannelType.SIGNED_INT8: return tcuTexture.convertSatRte(tcuTexture.deTypes.deInt8, src); + case tcuTexture.ChannelType.SIGNED_INT16: return tcuTexture.convertSatRte(tcuTexture.deTypes.deInt16, src); + case tcuTexture.ChannelType.SIGNED_INT32: return tcuTexture.convertSatRte(tcuTexture.deTypes.deInt32, src); + case tcuTexture.ChannelType.UNSIGNED_INT8: return tcuTexture.convertSatRte(tcuTexture.deTypes.deUint8, src); + case tcuTexture.ChannelType.UNSIGNED_INT16: return tcuTexture.convertSatRte(tcuTexture.deTypes.deUint16, src); + case tcuTexture.ChannelType.UNSIGNED_INT32: return tcuTexture.convertSatRte(tcuTexture.deTypes.deUint32, src); + case tcuTexture.ChannelType.HALF_FLOAT: return tcuFloat.numberToHalfFloat(src); + case tcuTexture.ChannelType.FLOAT: return src; + } + throw new Error('Unrecognized type ' + type); +}; + +/** + * \brief Read-write pixel data access + * + * This class extends read-only access object by providing write functionality. + * + * \note tcuTexture.PixelBufferAccess may not have any data members nor add any + * virtual functions. It must be possible to reinterpret_cast<> + * tcuTexture.PixelBufferAccess to tcuTexture.ConstPixelBufferAccess. + * @constructor + * @extends {tcuTexture.ConstPixelBufferAccess} + * + */ +tcuTexture.PixelBufferAccess = function(descriptor) { + tcuTexture.ConstPixelBufferAccess.call(this, descriptor); +}; + +tcuTexture.PixelBufferAccess.prototype = Object.create(tcuTexture.ConstPixelBufferAccess.prototype); +tcuTexture.PixelBufferAccess.prototype.constructor = tcuTexture.PixelBufferAccess; + +/** + * @param {Array<number>} color Vec4 color to set + * @param {number} x + * @param {number} y + * @param {number=} z + */ +tcuTexture.PixelBufferAccess.prototype.setPixel = function(color, x, y, z) { + z = z || 0; + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + DE_ASSERT(deMath.deInBounds32(z, 0, this.m_depth)); + + // Make sure that the position is 'integer' + this._setPixelInternal(color, Math.round(x), Math.round(y), Math.round(z)); +}; + +// NOTE: setPixel has been broken into setPixel, _setPixelInternal, and _setPixelPacked +// because having them combined previously was causing V8 depots +tcuTexture.PixelBufferAccess.prototype._setPixelInternal = function(color, x, y, z) { + // Quick paths + if (z == 0) { + if (this.m_rgba8View) { + color = deMath.toIVec(color); + this.m_rgba8View.write(x, y, color, 4); + return; + } else if (this.m_rgb8View) { + color = deMath.toIVec(color); + this.m_rgb8View.write(x, y, color, 3); + return; + } + } + + var pixelSize = this.m_format.getPixelSize(); + var offset = z * this.m_slicePitch + y * this.m_rowPitch + x * pixelSize; + var pixelPtr = this.getDataPtr(); + var pixelPtrOffset = offset / pixelPtr.BYTES_PER_ELEMENT; + + return this._setPixelPacked(color, pixelPtr, pixelPtrOffset); +}; + +tcuTexture.PixelBufferAccess.prototype._setPixelPacked = (function () { + var pn = function(val, offs, bits) { + return tcuTexture.normFloatToChannel(val, bits) << offs; + }; + + var pu = function(val, offs, bits) { + return tcuTexture.uintToChannel(val, bits) << offs; + }; + + return function tcuTexture_PixelBufferAccess_setPixelPacked(color, pixelPtr, pixelPtrOffset) { + // Packed formats. + switch (this.m_format.type) { + case tcuTexture.ChannelType.UNORM_SHORT_565: pixelPtr[pixelPtrOffset] = pn(color[0], 11, 5) | pn(color[1], 5, 6) | pn(color[2], 0, 5); break; + case tcuTexture.ChannelType.UNORM_SHORT_555: pixelPtr[pixelPtrOffset] = pn(color[0], 10, 5) | pn(color[1], 5, 5) | pn(color[2], 0, 5); break; + case tcuTexture.ChannelType.UNORM_SHORT_4444: pixelPtr[pixelPtrOffset] = pn(color[0], 12, 4) | pn(color[1], 8, 4) | pn(color[2], 4, 4) | pn(color[3], 0, 4); break; + case tcuTexture.ChannelType.UNORM_SHORT_5551: pixelPtr[pixelPtrOffset] = pn(color[0], 11, 5) | pn(color[1], 6, 5) | pn(color[2], 1, 5) | pn(color[3], 0, 1); break; + case tcuTexture.ChannelType.UNORM_INT_101010: pixelPtr[pixelPtrOffset] = pn(color[0], 22, 10) | pn(color[1], 12, 10) | pn(color[2], 2, 10); break; + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: pixelPtr[pixelPtrOffset] = pn(color[0], 0, 10) | pn(color[1], 10, 10) | pn(color[2], 20, 10) | pn(color[3], 30, 2); break; + case tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV: pixelPtr[pixelPtrOffset] = pu(color[0], 0, 10) | pu(color[1], 10, 10) | pu(color[2], 20, 10) | pu(color[3], 30, 2); break; + case tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV: pixelPtr[pixelPtrOffset] = tcuTexture.packRGB999E5(color); break; + + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + switch (this.m_format.order) { + // \note Stencil is always ignored. + case tcuTexture.ChannelOrder.D: pixelPtr[pixelPtrOffset] = pn(color[0], 8, 24); break; + case tcuTexture.ChannelOrder.S: pixelPtr[pixelPtrOffset] = pn(color[3], 8, 24); break; + case tcuTexture.ChannelOrder.DS: pixelPtr[pixelPtrOffset] = pn(color[0], 8, 24) | pu(color[3], 0, 8); break; + default: + throw new Error('Unsupported tcuTexture.channel order ' + this.m_format.order); + } + break; + + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: { + pixelPtr[pixelPtrOffset] = color[0]; + var u32array = new Uint32Array(this.m_data, (pixelPtrOffset * pixelPtr.BYTES_PER_ELEMENT) + this.m_offset + 4, 1); + u32array[0] = pu(color[3], 0, 8); + break; + } + + case tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV: { + var f11 = function(value) { + return tcuFloat.numberToFloat11(value); + }; + var f10 = function(value) { + return tcuFloat.numberToFloat10(value); + }; + + pixelPtr[pixelPtrOffset] = f11(color[0]) | (f11(color[1]) << 11) | (f10(color[2]) << 22); + break; + } + case tcuTexture.ChannelType.FLOAT: + if (this.m_format.order == tcuTexture.ChannelOrder.D) { + pixelPtr[pixelPtrOffset] = color[0]; + break; + } + // else fall-through to default case! + + default: { + // Generic path. + var numChannels = tcuTexture.getNumUsedChannels(this.m_format.order); + var map = tcuTexture.getChannelWriteMap(this.m_format.order); + + for (var c = 0; c < numChannels; c++) + pixelPtr[c + pixelPtrOffset] = tcuTexture.floatToChannel(color[map[c]], this.m_format.type); + } + } + }; +})(); + +/** + * @param {Array<number>} color Vec4 color to set (unnormalized) + * @param {number} x + * @param {number} y + * @param {number=} z + */ +tcuTexture.PixelBufferAccess.prototype.setPixelInt = function(color, x, y, z) { + z = z || 0; + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + DE_ASSERT(deMath.deInBounds32(z, 0, this.m_depth)); + + // Make sure that the position is 'integer' + x = Math.round(x); + y = Math.round(y); + z = Math.round(z); + + // Quick paths + if (z == 0) { + if (this.m_rgba8View) { + this.m_rgba8View.write(x, y, color, 4); + return; + } else if (this.m_rgb8View) { + this.m_rgb8View.write(x, y, color, 3); + return; + } + } + + var pixelSize = this.m_format.getPixelSize(); + var offset = z * this.m_slicePitch + y * this.m_rowPitch + x * pixelSize; + var pixelPtr = this.getDataPtr(); + var pixelPtrOffset = offset / pixelPtr.BYTES_PER_ELEMENT; + + var pu = function(val, offs, bits) { + return tcuTexture.uintToChannel(val, bits) << offs; + }; + + // Packed formats. + switch (this.m_format.type) { + case tcuTexture.ChannelType.UNORM_SHORT_565: pixelPtr[pixelPtrOffset] = pu(color[0], 11, 5) | pu(color[1], 5, 6) | pu(color[2], 0, 5); break; + case tcuTexture.ChannelType.UNORM_SHORT_555: pixelPtr[pixelPtrOffset] = pu(color[0], 10, 5) | pu(color[1], 5, 5) | pu(color[2], 0, 5); break; + case tcuTexture.ChannelType.UNORM_SHORT_4444: pixelPtr[pixelPtrOffset] = pu(color[0], 12, 4) | pu(color[1], 8, 4) | pu(color[2], 4, 4) | pu(color[3], 0, 4); break; + case tcuTexture.ChannelType.UNORM_SHORT_5551: pixelPtr[pixelPtrOffset] = pu(color[0], 11, 5) | pu(color[1], 6, 5) | pu(color[2], 1, 5) | pu(color[3], 0, 1); break; + case tcuTexture.ChannelType.UNORM_INT_101010: pixelPtr[pixelPtrOffset] = pu(color[0], 22, 10) | pu(color[1], 12, 10) | pu(color[2], 2, 10); break; + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: pixelPtr[pixelPtrOffset] = pu(color[0], 0, 10) | pu(color[1], 10, 10) | pu(color[2], 20, 10) | pu(color[3], 30, 2); break; + case tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV: pixelPtr[pixelPtrOffset] = pu(color[0], 0, 10) | pu(color[1], 10, 10) | pu(color[2], 20, 10) | pu(color[3], 30, 2); break; + + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + switch (this.m_format.order) { + // \note Stencil is always ignored. + case tcuTexture.ChannelOrder.D: pixelPtr[pixelPtrOffset] = pu(color[0], 8, 24); break; + case tcuTexture.ChannelOrder.S: pixelPtr[pixelPtrOffset] = pu(color[3], 8, 24); break; + case tcuTexture.ChannelOrder.DS: pixelPtr[pixelPtrOffset] = pu(color[0], 8, 24) | pu(color[3], 0, 8); break; + default: + throw new Error('Unsupported tcuTexture.channel order ' + this.m_format.order); + } + break; + + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: { + pixelPtr[pixelPtrOffset] = color[0]; + var u32array = new Uint32Array(this.m_data, offset + this.m_offset + 4, 1); + u32array[pixelPtrOffset] = pu(color[3], 0, 8); + break; + } + + default: { + // Generic path. + var numChannels = tcuTexture.getNumUsedChannels(this.m_format.order); + var map = tcuTexture.getChannelWriteMap(this.m_format.order); + + for (var c = 0; c < numChannels; c++) + pixelPtr[c + pixelPtrOffset] = tcuTexture.intToChannel(color[map[c]], this.m_format.type); + } + } +}; + +/** + * @param {Array<number>=} color Vec4 color to set, optional. + * @param {Array<number>=} x Range in x axis, optional. + * @param {Array<number>=} y Range in y axis, optional. + * @param {Array<number>=} z Range in z axis, optional. + */ +tcuTexture.PixelBufferAccess.prototype.clear = function(color, x, y, z) { + var c = color || [0, 0, 0, 0]; + var arrayType = tcuTexture.getTypedArray(this.m_format.type); + var range_x = x || [0, this.m_width]; + var range_y = y || [0, this.m_height]; + var range_z = z || [0, this.m_depth]; + var pixelSize = this.m_format.getPixelSize(); + var numElements = pixelSize / arrayType.BYTES_PER_ELEMENT; + var width = range_x[1] - range_x[0]; + var height = range_y[1] - range_y[0]; + var depth = range_z[1] - range_z[0]; + if (x === undefined && y === undefined && z === undefined && + c[0] == 0 && c[1] == 0 && c[2] == 0 && c[3] == 0) { + var pixelPtr = new arrayType(this.m_data, this.m_offset); + pixelPtr.fill(0); + return; + } + + //copy first pixel over other pixels in the row + var fillRow = function(pixelPtr, numElements, width) { + for (var i = 1; i < width; i++) + for (var c = 0; c < numElements; c++) + pixelPtr[i * numElements + c] = pixelPtr[c]; + }; + // copy first row to other rows in all planes + var fillPlanes = function(buffer, arrayType, src, offset, rowStride, planeStride, width, height, depth) { + for (var j = 0; j < depth; j++) + for (var i = (j == 0 ? 1 : 0); i < height; i++) { + var dst = new arrayType(buffer, offset + i * rowStride + j * planeStride, width); + dst.set(src); + } + }; + + this.setPixel(c, range_x[0], range_y[0], range_z[0]); + + var offset = range_z[0] * this.m_slicePitch + range_y[0] * this.m_rowPitch + range_x[0] * pixelSize; + var pixelPtr = new arrayType(this.m_data, offset + this.m_offset, width * numElements); + + fillRow(pixelPtr, numElements, width); + fillPlanes(this.m_data, arrayType, pixelPtr, offset + this.m_offset, this.m_rowPitch, this.m_slicePitch, width * numElements, height, depth); +}; + +/** + * @param {number} depth to set + * @param {number} x + * @param {number} y + * @param {number=} z + */ +tcuTexture.PixelBufferAccess.prototype.setPixDepth = function(depth, x, y, z) { + if (z == null) + z = 0; + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + DE_ASSERT(deMath.deInBounds32(z, 0, this.m_depth)); + + // Make sure that the position is 'integer' + x = Math.round(x); + y = Math.round(y); + z = Math.round(z); + + var pixelSize = this.m_format.getPixelSize(); + var offset = z * this.m_slicePitch + y * this.m_rowPitch + x * pixelSize; + var pixelPtr = this.getDataPtr(); + var pixelPtrOffset = offset / pixelPtr.BYTES_PER_ELEMENT; + + var pn = function(val, offs, bits) { + return tcuTexture.normFloatToChannel(val, bits) << offs; + }; + + // Packed formats. + switch (this.m_format.type) { + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + switch (this.m_format.order) { + case tcuTexture.ChannelOrder.D: pixelPtr[pixelPtrOffset] = pn(depth, 8, 24); break; + case tcuTexture.ChannelOrder.DS: pixelPtr[pixelPtrOffset] = pn(depth, 8, 24) | (pixelPtr[pixelPtrOffset] & 0xFF); break; + default: + throw new Error('Unsupported tcuTexture.channel order ' + this.m_format.order); + } + break; + + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: { + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.DS); + pixelPtr[pixelPtrOffset] = depth; + break; + } + + default: { + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.D || this.m_format.order == tcuTexture.ChannelOrder.DS); + pixelPtr[pixelPtrOffset] = tcuTexture.floatToChannel(depth, this.m_format.type); + } + } +}; + +/** + * @param {number} stencil to set + * @param {number} x + * @param {number} y + * @param {number=} z + */ +tcuTexture.PixelBufferAccess.prototype.setPixStencil = function(stencil, x, y, z) { + if (z == null) + z = 0; + DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width)); + DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height)); + DE_ASSERT(deMath.deInBounds32(z, 0, this.m_depth)); + + // Make sure that the position is 'integer' + x = Math.round(x); + y = Math.round(y); + z = Math.round(z); + + var pixelSize = this.m_format.getPixelSize(); + var offset = z * this.m_slicePitch + y * this.m_rowPitch + x * pixelSize; + var pixelPtr = this.getDataPtr(); + var pixelPtrOffset = offset / pixelPtr.BYTES_PER_ELEMENT; + + var pu = function(val, offs, bits) { + return tcuTexture.uintToChannel(val, bits) << offs; + }; + + // Packed formats. + switch (this.m_format.type) { + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + switch (this.m_format.order) { + case tcuTexture.ChannelOrder.S: pixelPtr[pixelPtrOffset] = pu(stencil, 8, 24); break; + case tcuTexture.ChannelOrder.DS: pixelPtr[pixelPtrOffset] = pu(stencil, 0, 8) | (pixelPtr[pixelPtrOffset] & 0xFFFFFF00); break; + default: + throw new Error('Unsupported tcuTexture.channel order ' + this.m_format.order); + } + break; + + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: { + var u32array = new Uint32Array(this.m_data, this.m_offset + offset + 4, 1); + u32array[0] = pu(stencil, 0, 8); + break; + } + + default: { + if (this.m_format.order == tcuTexture.ChannelOrder.S) + pixelPtr[pixelPtrOffset] = tcuTexture.floatToChannel(stencil, this.m_format.type); + else { + DE_ASSERT(this.m_format.order == tcuTexture.ChannelOrder.DS); + pixelPtr[3 + pixelPtrOffset] = tcuTexture.floatToChannel(stencil, this.m_format.type); + } + } + } +}; + +/** + * newFromTextureLevel + * @param {tcuTexture.TextureLevel} level + * @return {tcuTexture.PixelBufferAccess} + */ +tcuTexture.PixelBufferAccess.newFromTextureLevel = function(level) { + var descriptor = new Object(); + descriptor.format = level.getFormat(); + descriptor.width = level.getWidth(); + descriptor.height = level.getHeight(); + descriptor.depth = level.m_depth; + descriptor.data = level.m_data.m_ptr; + + return new tcuTexture.PixelBufferAccess(descriptor); +}; + +/** + * newFromTextureFormat + * @param {tcuTexture.TextureFormat} format + * @param {number} width + * @param {number} height + * @param {number} depth + * @param {number} rowPitch + * @param {number} slicePitch + * @param {ArrayBuffer} data + */ +tcuTexture.PixelBufferAccess.newFromTextureFormat = function(format, width, height, depth, rowPitch, slicePitch, data) { + var descriptor = new Object(); + descriptor.format = format; + descriptor.width = width; + descriptor.height = height; + descriptor.depth = depth; + descriptor.rowPitch = rowPitch; + descriptor.slicePitch = slicePitch; + descriptor.data = data; + + return new tcuTexture.PixelBufferAccess(descriptor); +}; + +/* TODO: Port */ +// { +// public: +// tcuTexture.PixelBufferAccess (void) {} +// tcuTexture.PixelBufferAccess (const tcuTexture.TextureFormat& format, int width, int height, int depth, void* data); + +// void* getDataPtr (void) const { return m_data; } + +// void setPixels (const void* buf, int bufSize) const; +// void setPixel (const tcu::Vec4& color, int x, int y, int z = 0) const; +// void setPixel (const tcu::IVec4& color, int x, int y, int z = 0) const; +// void setPixel (const tcu::UVec4& color, int x, int y, int z = 0) const { setPixel(color.cast<int>(), x, y, z); } + +// void setPixDepth (float depth, int x, int y, int z = 0) const; +// void setPixStencil (int stencil, int x, int y, int z = 0) const; +// }; + +/** + * @constructor + * @param {tcuTexture.TextureFormat} format + * @param {number} numLevels + */ +tcuTexture.TextureLevelPyramid = function(format, numLevels) { + /* tcuTexture.TextureFormat */this.m_format = format; + /* LevelData */ this.m_data = []; + for (var i = 0; i < numLevels; i++) + this.m_data.push(new tcuTexture.DeqpArrayBuffer()); + /* {Array<tcuTexture.PixelBufferAccess>} */ this.m_access = []; + this.m_access.length = numLevels; +}; + +/** @return {boolean} */ +tcuTexture.TextureLevelPyramid.prototype.isLevelEmpty = function(levelNdx) { return this.m_data[levelNdx].empty(); }; +/** @return {tcuTexture.TextureFormat} */ +tcuTexture.TextureLevelPyramid.prototype.getFormat = function() { return this.m_format; }; +/** @return {number} */ +tcuTexture.TextureLevelPyramid.prototype.getNumLevels = function() { return this.m_access.length; }; +/** @return {tcuTexture.PixelBufferAccess} */ +tcuTexture.TextureLevelPyramid.prototype.getLevel = function(ndx) { return this.m_access[ndx]; }; +/** @return {Array<tcuTexture.PixelBufferAccess>} */ +tcuTexture.TextureLevelPyramid.prototype.getLevels = function() { return this.m_access; }; + +/** + * @param {number} levelNdx + * @param {number} width + * @param {number} height + * @param {number} depth + */ +tcuTexture.TextureLevelPyramid.prototype.allocLevel = function(levelNdx, width, height, depth) { + var size = this.m_format.getPixelSize() * width * height * depth; + + DE_ASSERT(this.isLevelEmpty(levelNdx)); + + this.m_data[levelNdx].setStorage(size); + this.m_access[levelNdx] = new tcuTexture.PixelBufferAccess({ + format: this.m_format, + width: width, + height: height, + depth: depth, + data: this.m_data[levelNdx].m_ptr + }); +}; + +tcuTexture.TextureLevelPyramid.prototype.clearLevel = function(levelNdx) { + /* TODO: Implement */ + throw new Error('Not implemented'); +}; + +/** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} levels + * @param {number} numLevels + * @param {tcuTexture.Sampler} sampler + * @param {number} s + * @param {number} t + * @param {number} depth (integer) + * @param {number=} lod + * @return {Array<number>} Vec4 pixel color + */ +tcuTexture.sampleLevelArray2D = function(levels, numLevels, sampler, s, t, depth, lod) { + // z-offset in 2D textures is layer selector + return tcuTexture.sampleLevelArray2DOffset(levels, numLevels, sampler, [s, t], lod, [0, 0, depth]); +}; + +/** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} levels + * @param {number} numLevels + * @param {tcuTexture.Sampler} sampler + * @param {number} s + * @param {number} t + * @param {number} r + * @param {number} lod + * @return {Array<number>} Vec4 pixel color + */ +tcuTexture.sampleLevelArray3D = function(levels, numLevels, sampler, s, t, r, lod) { + return tcuTexture.sampleLevelArray3DOffset(levels, numLevels, sampler, s, t, r, lod, [0, 0, 0]); +}; + +/** + * @constructor + * @param {tcuTexture.CubeFace} face + * @param {Array<number>} coords + */ +tcuTexture.CubeFaceCoords = function(face, coords) { + this.face = face; + this.s = coords[0]; + this.t = coords[1]; +}; + +/** + * \brief 2D Texture View + * @constructor + * @param {number} numLevels + * @param {?Array<tcuTexture.ConstPixelBufferAccess>} levels + */ +tcuTexture.Texture2DView = function(numLevels, levels) { + this.m_numLevels = numLevels; + this.m_levels = levels; +}; + +/** @return {number} */ +tcuTexture.Texture2DView.prototype.getNumLevels = function() { return this.m_numLevels; }; +/** @return {number} */ +tcuTexture.Texture2DView.prototype.getWidth = function() { return this.m_numLevels > 0 ? this.m_levels[0].getWidth() : 0; }; +/** @return {number} */ +tcuTexture.Texture2DView.prototype.getHeight = function() { return this.m_numLevels > 0 ? this.m_levels[0].getHeight() : 0; }; +/** + * @param {number} ndx + * @return {tcuTexture.ConstPixelBufferAccess} + */ +tcuTexture.Texture2DView.prototype.getLevel = function(ndx) { DE_ASSERT(deMath.deInBounds32(ndx, 0, this.m_numLevels)); return this.m_levels[ndx]; }; +/** @return {Array<tcuTexture.ConstPixelBufferAccess>} */ +tcuTexture.Texture2DView.prototype.getLevels = function() { return this.m_levels; }; + +/** + * @param {number} baseLevel + * @param {number} maxLevel + * return {tcuTexture.Texture2DView} + */ +tcuTexture.Texture2DView.prototype.getSubView = function(baseLevel, maxLevel) { + var clampedBase = deMath.clamp(baseLevel, 0, this.m_numLevels - 1); + var clampedMax = deMath.clamp(maxLevel, clampedBase, this.m_numLevels - 1); + var numLevels = clampedMax - clampedBase + 1; + return new tcuTexture.Texture2DView(numLevels, this.m_levels.slice(clampedBase, numLevels)); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number=} lod + * @return {Array<number>} Pixel color + */ +tcuTexture.Texture2DView.prototype.sample = function(sampler, texCoord, lod) { + return tcuTexture.sampleLevelArray2D(this.m_levels, this.m_numLevels, sampler, texCoord[0], texCoord[1], 0 /* depth */, lod); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number} lod + * @param {Array<number>} offset + * @return {Array<number>} Pixel color + */ +tcuTexture.Texture2DView.prototype.sampleOffset = function(sampler, texCoord, lod, offset) { + return tcuTexture.sampleLevelArray2DOffset(this.m_levels, this.m_numLevels, sampler, texCoord, lod, [offset[0], offset[1], 0]); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {Array<number>} texCoord + * @param {number} lod + * @return {number} + */ +tcuTexture.Texture2DView.prototype.sampleCompare = function(sampler, ref, texCoord, lod) { + return tcuTexture.sampleLevelArray2DCompare(this.m_levels, this.m_numLevels, sampler, ref, texCoord[0], texCoord[1], lod, [0, 0, 0]); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {Array<number>} texCoord + * @param {number} lod + * @param {Array<number>} offset + * @return {number} + */ +tcuTexture.Texture2DView.prototype.sampleCompareOffset = function(sampler, ref, texCoord, lod, offset) { + return tcuTexture.sampleLevelArray2DCompare(this.m_levels, this.m_numLevels, sampler, ref, texCoord[0], texCoord[1], lod, [offset[0], offset[1], 0]); +}; + + /* TODO: Port + Vec4 sample (const tcuTexture.Sampler& sampler, float s, float t, float lod) const; + Vec4 sampleOffset (const tcuTexture.Sampler& sampler, float s, float t, float lod, const IVec2& offset) const; + float sampleCompare (const tcuTexture.Sampler& sampler, float ref, float s, float t, float lod) const; + float sampleCompareOffset (const tcuTexture.Sampler& sampler, float ref, float s, float t, float lod, const IVec2& offset) const; + + Vec4 gatherOffsets (const tcuTexture.Sampler& sampler, float s, float t, int componentNdx, const IVec2 (&offsets)[4]) const; + Vec4 gatherOffsetsCompare(const tcuTexture.Sampler& sampler, float ref, float s, float t, const IVec2 (&offsets)[4]) const; + */ + +/** + * @constructor + * @param {number} numLevels + * @param {Array<tcuTexture.ConstPixelBufferAccess>} levels + */ +tcuTexture.Texture2DArrayView = function(numLevels, levels) { + this.m_numLevels = numLevels; + this.m_levels = levels; +}; + +/** @return {number} */ +tcuTexture.Texture2DArrayView.prototype.getNumLevels = function() { return this.m_numLevels; }; +/** @return {number} */ +tcuTexture.Texture2DArrayView.prototype.getWidth = function() { return this.m_numLevels > 0 ? this.m_levels[0].getWidth() : 0; }; +/** @return {number} */ +tcuTexture.Texture2DArrayView.prototype.getHeight = function() { return this.m_numLevels > 0 ? this.m_levels[0].getHeight() : 0; }; +/** @return {number} */ +tcuTexture.Texture2DArrayView.prototype.getNumLayers = function() { return this.m_numLevels > 0 ? this.m_levels[0].getDepth() : 0; }; +/** + * @param {number} ndx + * @return {tcuTexture.ConstPixelBufferAccess} + */ +tcuTexture.Texture2DArrayView.prototype.getLevel = function(ndx) { DE_ASSERT(deMath.deInBounds32(ndx, 0, this.m_numLevels)); return this.m_levels[ndx]; }; +/** @return {Array<tcuTexture.ConstPixelBufferAccess>} */ +tcuTexture.Texture2DArrayView.prototype.getLevels = function() { return this.m_levels; }; + +/** + * @param {number} r + * @return {number} layer corresponding to requested sampling 'r' coordinate + */ +tcuTexture.Texture2DArrayView.prototype.selectLayer = function(r) { + DE_ASSERT(this.m_numLevels > 0 && this.m_levels); + return deMath.clamp(Math.round(r), 0, this.m_levels[0].getDepth() - 1); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number=} lod + * @return {Array<number>} Pixel color + */ +tcuTexture.Texture2DArrayView.prototype.sample = function(sampler, texCoord, lod) { + lod = lod || 0; + return tcuTexture.sampleLevelArray2D(this.m_levels, this.m_numLevels, sampler, texCoord[0], texCoord[1], this.selectLayer(texCoord[2]), lod); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number} lod + * @param {Array<number>} offset + * @return {Array<number>} + */ +tcuTexture.Texture2DArrayView.prototype.sampleOffset = function(sampler, texCoord, lod, offset) { + return tcuTexture.sampleLevelArray2DOffset(this.m_levels, this.m_numLevels, sampler, texCoord, lod, [offset[0], offset[1], this.selectLayer(texCoord[2])]); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {Array<number>} texCoord + * @param {number} lod + * @param {Array<number>} offset + * @return {number} + */ +tcuTexture.Texture2DArrayView.prototype.sampleCompareOffset = function(sampler, ref, texCoord, lod, offset) { + return tcuTexture.sampleLevelArray2DCompare(this.m_levels, this.m_numLevels, sampler, ref, texCoord[0], texCoord[1], lod, [offset[0], offset[1], this.selectLayer(texCoord[2])]); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {Array<number>} texCoord + * @param {number} lod + * @return {number} + */ +tcuTexture.Texture2DArrayView.prototype.sampleCompare = function(sampler, ref, texCoord, lod) { + return tcuTexture.sampleLevelArray2DCompare(this.m_levels, this.m_numLevels, sampler, ref, texCoord[0], texCoord[1], lod, [0, 0, this.selectLayer(texCoord[2])]); +}; + +/** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} levels + * @param {number} numLevels + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number} lod + * @param {Array<number>} offset + * @return {Array<number>} + */ +tcuTexture.sampleLevelArray2DOffset = function(levels, numLevels, sampler, texCoord, lod, offset) { + /** @type {boolean} */ var magnified = lod <= sampler.lodThreshold; + /** @type {tcuTexture.FilterMode} */ var filterMode = magnified ? sampler.magFilter : sampler.minFilter; + /** @type {number} */ var maxLevel; + /** @type {tcuTexture.FilterMode} */ var levelFilter; + switch (filterMode) { + case tcuTexture.FilterMode.NEAREST: return levels[0].sample2DOffset(sampler, filterMode, texCoord[0], texCoord[1], offset); + case tcuTexture.FilterMode.LINEAR: return levels[0].sample2DOffset(sampler, filterMode, texCoord[0], texCoord[1], offset); + + case tcuTexture.FilterMode.NEAREST_MIPMAP_NEAREST: + case tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST: + maxLevel = numLevels - 1; + /** @type {number} */ var level = deMath.clamp(Math.ceil(lod + 0.5) - 1, 0, maxLevel); + levelFilter = (filterMode === tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST) ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + + return levels[level].sample2DOffset(sampler, levelFilter, texCoord[0], texCoord[1], offset); + + case tcuTexture.FilterMode.NEAREST_MIPMAP_LINEAR: + case tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR: + maxLevel = numLevels - 1; + /** @type {number} */ var level0 = deMath.clamp(Math.floor(lod), 0, maxLevel); + /** @type {number} */ var level1 = Math.min(maxLevel, level0 + 1); + levelFilter = (filterMode === tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR) ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + /** @type {number} */ var f = deMath.deFloatFrac(lod); + /** @type {Array<number>} */ var t0 = levels[level0].sample2DOffset(sampler, levelFilter, texCoord[0], texCoord[1], offset); + /** @type {Array<number>} */ var t1 = levels[level1].sample2DOffset(sampler, levelFilter, texCoord[0], texCoord[1], offset); + + return deMath.add(deMath.scale(t0, (1.0 - f)), deMath.scale(t1, f)); + + default: + return [0.0, 0.0, 0.0, 0.0]; + } +}; + +/** + * @constructor + * @param {number} numLevels + * @param {Array<tcuTexture.ConstPixelBufferAccess>} levels + */ +tcuTexture.Texture3DView = function(numLevels, levels) { + this.m_numLevels = numLevels; + this.m_levels = levels; +}; + +/** @return {number} */ +tcuTexture.Texture3DView.prototype.getNumLevels = function() { return this.m_numLevels; }; +/** @return {number} */ +tcuTexture.Texture3DView.prototype.getWidth = function() { return this.m_numLevels > 0 ? this.m_levels[0].getWidth() : 0; }; +/** @return {number} */ +tcuTexture.Texture3DView.prototype.getHeight = function() { return this.m_numLevels > 0 ? this.m_levels[0].getHeight() : 0; }; +/** @return {number} */ +tcuTexture.Texture3DView.prototype.getDepth = function() { return this.m_numLevels > 0 ? this.m_levels[0].getDepth() : 0; }; +/** + * @param {number} ndx + * @return {tcuTexture.ConstPixelBufferAccess} + */ +tcuTexture.Texture3DView.prototype.getLevel = function(ndx) { DE_ASSERT(deMath.deInBounds32(ndx, 0, this.m_numLevels)); return this.m_levels[ndx]; }; +/** @return {Array<tcuTexture.ConstPixelBufferAccess>} */ +tcuTexture.Texture3DView.prototype.getLevels = function() { return this.m_levels; }; + +/** + * @param {number} baseLevel + * @param {number} maxLevel + * return {tcuTexture.Texture3DView} + */ +tcuTexture.Texture3DView.prototype.getSubView = function(baseLevel, maxLevel) { + var clampedBase = deMath.clamp(baseLevel, 0, this.m_numLevels - 1); + var clampedMax = deMath.clamp(maxLevel, clampedBase, this.m_numLevels - 1); + var numLevels = clampedMax - clampedBase + 1; + return new tcuTexture.Texture3DView(numLevels, this.m_levels.slice(clampedBase, numLevels)); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number=} lod + * @return {Array<number>} Pixel color + */ +tcuTexture.Texture3DView.prototype.sample = function(sampler, texCoord, lod) { + lod = lod || 0; + return tcuTexture.sampleLevelArray3D(this.m_levels, this.m_numLevels, sampler, texCoord[0], texCoord[1], texCoord[2], lod); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {Array<number>} texCoord + * @param {number} lod + * @return {number} + */ +tcuTexture.Texture3DView.prototype.sampleCompare = function(sampler, ref, texCoord, lod) { + throw new Error('Unimplemented'); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number} lod + * @param {Array<number>} offset + * @return {Array<number>} + */ +tcuTexture.Texture3DView.prototype.sampleOffset = function(sampler, texCoord, lod, offset) { + return tcuTexture.sampleLevelArray3DOffset(this.m_levels, this.m_numLevels, sampler, texCoord[0], texCoord[1], texCoord[2], lod, offset); +}; + +/* TODO: All view classes are very similar. They should have a common base class */ + +/** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} levels + * @param {number} numLevels + * @param {tcuTexture.Sampler} sampler + * @param {number} s + * @param {number} t + * @param {number} r + * @param {number} lod + * @param {Array<number>} offset + * @return {Array<number>} + */ +tcuTexture.sampleLevelArray3DOffset = function(levels, numLevels, sampler, s, t, r, lod, offset) { + /** @type {boolean} */ var magnified = lod <= sampler.lodThreshold; + /** @type {tcuTexture.FilterMode} */ var filterMode = magnified ? sampler.magFilter : sampler.minFilter; + /** @type {number} */ var maxLevel; + /** @type {tcuTexture.FilterMode} */ var levelFilter; + switch (filterMode) { + case tcuTexture.FilterMode.NEAREST: return levels[0].sample3DOffset(sampler, filterMode, s, t, r, offset); + case tcuTexture.FilterMode.LINEAR: return levels[0].sample3DOffset(sampler, filterMode, s, t, r, offset); + + case tcuTexture.FilterMode.NEAREST_MIPMAP_NEAREST: + case tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST: + maxLevel = numLevels - 1; + /** @type {number} */ var level = deMath.clamp(Math.ceil(lod + 0.5) - 1, 0, maxLevel); + levelFilter = (filterMode === tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST) ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + + return levels[level].sample3DOffset(sampler, levelFilter, s, t, r, offset); + + case tcuTexture.FilterMode.NEAREST_MIPMAP_LINEAR: + case tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR: + maxLevel = numLevels - 1; + /** @type {number} */ var level0 = deMath.clamp(Math.floor(lod), 0, maxLevel); + /** @type {number} */ var level1 = Math.min(maxLevel, level0 + 1); + levelFilter = (filterMode === tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR) ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + /** @type {number} */ var f = deMath.deFloatFrac(lod); + /** @type {Array<number>} */ var t0 = levels[level0].sample3DOffset(sampler, levelFilter, s, t, r, offset); + /** @type {Array<number>} */ var t1 = levels[level1].sample3DOffset(sampler, levelFilter, s, t, r, offset); + + return deMath.add(deMath.scale(t0, (1.0 - f)), deMath.scale(t1, f)); + + default: + throw new Error('Filter mode not supported'); + } +}; + +/** + * @param {number} width + * @param {number=} height + * @param {number=} depth + * @return {number} Number of pyramid levels + */ +tcuTexture.computeMipPyramidLevels = function(width, height, depth) { + if (depth !== undefined) + return Math.floor(Math.log2(Math.max(width, Math.max(height, depth)))) + 1; + else if (height !== undefined) + return Math.floor(Math.log2(Math.max(width, height))) + 1; + else + return Math.floor(Math.log2(width)) + 1; +}; + +/** + * @param {number} baseLevelSize + * @param {number} levelNdx + */ +tcuTexture.getMipPyramidLevelSize = function(baseLevelSize, levelNdx) { + return Math.max(baseLevelSize >> levelNdx, 1); +}; + +/** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} faceAccesses + * @param {tcuTexture.CubeFace} baseFace + * @param {number} u + * @param {number} v + * @param {number} depth + * @return {Array<Array<number>>} + */ +tcuTexture.getCubeLinearSamples = function(faceAccesses, baseFace, u, v, depth) { + DE_ASSERT(faceAccesses[0].getWidth() == faceAccesses[0].getHeight()); + /** @type {Array<Array<number>>} */ var dst = []; + var size = faceAccesses[0].getWidth(); + var x0 = Math.floor(u - 0.5); + var x1 = x0 + 1; + var y0 = Math.floor(v - 0.5); + var y1 = y0 + 1; + var baseSampleCoords = + [ + [x0, y0], + [x1, y0], + [x0, y1], + [x1, y1] + ]; + /** @type {Array<Array<number>>} */ var sampleColors = []; + /** @type {Array<boolean>} */ var hasBothCoordsOutOfBounds = []; //!< Whether correctCubeFace() returns CUBEFACE_LAST, i.e. both u and v are out of bounds. + + // Find correct faces and coordinates for out-of-bounds sample coordinates. + + for (var i = 0; i < 4; i++) { + /** @type {tcuTexture.CubeFaceCoords} */ var coords = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(baseFace, baseSampleCoords[i]), size); + hasBothCoordsOutOfBounds[i] = coords == null; + if (!hasBothCoordsOutOfBounds[i]) + sampleColors[i] = tcuTexture.lookup(faceAccesses[coords.face], coords.s, coords.t, depth); + } + + // If a sample was out of bounds in both u and v, we get its color from the average of the three other samples. + // \note This averaging behavior is not required by the GLES3 spec (though it is recommended). GLES3 spec only + // requires that if the three other samples all have the same color, then the doubly-out-of-bounds sample + // must have this color as well. + + var bothOutOfBoundsNdx = -1; + for (var i = 0; i < 4; i++) { + if (hasBothCoordsOutOfBounds[i]) { + DE_ASSERT(bothOutOfBoundsNdx < 0); // Only one sample can be out of bounds in both u and v. + bothOutOfBoundsNdx = i; + } + } + if (bothOutOfBoundsNdx != -1) { + sampleColors[bothOutOfBoundsNdx] = [0, 0, 0, 0]; + for (var i = 0; i < 4; i++) + if (i != bothOutOfBoundsNdx) + sampleColors[bothOutOfBoundsNdx] = deMath.add(sampleColors[bothOutOfBoundsNdx], sampleColors[i]); + + sampleColors[bothOutOfBoundsNdx] = deMath.scale(sampleColors[bothOutOfBoundsNdx], (1.0 / 3.0)); + } + + for (var i = 0; i < sampleColors.length; i++) + dst[i] = sampleColors[i]; + + return dst; +}; + +// \todo [2014-02-19 pyry] Optimize faceAccesses +/** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} faceAccesses + * @param {tcuTexture.CubeFace} baseFace + * @param {tcuTexture.Sampler} sampler + * @param {number} s + * @param {number} t + * @param {number} depth + * @return {Array<number>} + */ +tcuTexture.sampleCubeSeamlessLinear = function(faceAccesses, baseFace, sampler, s, t, depth) { + DE_ASSERT(faceAccesses[0].getWidth() == faceAccesses[0].getHeight()); + + var size = faceAccesses[0].getWidth(); + // Non-normalized coordinates. + var u = s; + var v = t; + + if (sampler.normalizedCoords) { + u = tcuTexture.unnormalize(sampler.wrapS, s, size); + v = tcuTexture.unnormalize(sampler.wrapT, t, size); + } + + // Get sample colors. + + /** @type {Array<Array<number>>} */ var sampleColors = tcuTexture.getCubeLinearSamples(faceAccesses, baseFace, u, v, depth); + + // Interpolate. + + var a = deMath.deFloatFrac(u - 0.5); + var b = deMath.deFloatFrac(v - 0.5); + + return deMath.add((deMath.scale(deMath.scale(sampleColors[0], (1.0 - a)), (1.0 - b))), + deMath.add((deMath.scale(deMath.scale(sampleColors[1], (a)), (1.0 - b))), + deMath.add((deMath.scale(deMath.scale(sampleColors[2], (1.0 - a)), (b))), + (deMath.scale(deMath.scale(sampleColors[3], (a)), (b)))))); +}; + +/** + * @param {Array<Array<tcuTexture.ConstPixelBufferAccess>>} faces + * @param {number} numLevels + * @param {tcuTexture.CubeFace} face + * @param {tcuTexture.Sampler} sampler + * @param {number} s + * @param {number} t + * @param {number} depth + * @param {number=} lod + * @return {Array<number>} + */ +tcuTexture.sampleLevelArrayCubeSeamless = function(faces, numLevels, face, sampler, s, t, depth, lod) { + lod = lod || 0; + var magnified = lod <= sampler.lodThreshold; + /** @type {tcuTexture.FilterMode} */ var filterMode = magnified ? sampler.magFilter : sampler.minFilter; + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faceAccesses = []; + /** @type {tcuTexture.FilterMode}*/ var levelFilter; + + switch (filterMode) { + case tcuTexture.FilterMode.NEAREST: + return tcuTexture.sampleCubeSeamlessNearest(faces[face][0], sampler, s, t, depth); + + case tcuTexture.FilterMode.LINEAR: { + faceAccesses = []; + for (var i = 0; i < Object.keys(tcuTexture.CubeFace).length; i++) + faceAccesses[i] = faces[i][0]; + + return tcuTexture.sampleCubeSeamlessLinear(faceAccesses, face, sampler, s, t, depth); + } + + case tcuTexture.FilterMode.NEAREST_MIPMAP_NEAREST: + case tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST: { + var maxLevel = numLevels - 1; + var level = deMath.clamp(Math.ceil(lod + 0.5) - 1, 0, maxLevel); + levelFilter = (filterMode == tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST) ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + + if (levelFilter == tcuTexture.FilterMode.NEAREST) + return tcuTexture.sampleCubeSeamlessNearest(faces[face][level], sampler, s, t, depth); + else { + DE_ASSERT(levelFilter == tcuTexture.FilterMode.LINEAR); + + faceAccesses = []; + for (var i = 0; i < Object.keys(tcuTexture.CubeFace).length; i++) + faceAccesses[i] = faces[i][level]; + + return tcuTexture.sampleCubeSeamlessLinear(faceAccesses, face, sampler, s, t, depth); + } + } + + case tcuTexture.FilterMode.NEAREST_MIPMAP_LINEAR: + case tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR: { + var maxLevel = numLevels - 1; + var level0 = deMath.clamp(Math.floor(lod), 0, maxLevel); + var level1 = Math.min(maxLevel, level0 + 1); + levelFilter = (filterMode == tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR) ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + var f = deMath.deFloatFrac(lod); + var t0 = []; + var t1 = []; + + if (levelFilter == tcuTexture.FilterMode.NEAREST) { + t0 = tcuTexture.sampleCubeSeamlessNearest(faces[face][level0], sampler, s, t, depth); + t1 = tcuTexture.sampleCubeSeamlessNearest(faces[face][level1], sampler, s, t, depth); + } else { + DE_ASSERT(levelFilter == tcuTexture.FilterMode.LINEAR); + + /** @type {Array<tcuTexture.ConstPixelBufferAccess>}*/ var faceAccesses0 = []; + /** @type {Array<tcuTexture.ConstPixelBufferAccess>}*/ var faceAccesses1 = []; + for (var i = 0; i < Object.keys(tcuTexture.CubeFace).length; i++) { + faceAccesses0[i] = faces[i][level0]; + faceAccesses1[i] = faces[i][level1]; + } + + t0 = tcuTexture.sampleCubeSeamlessLinear(faceAccesses0, face, sampler, s, t, depth); + t1 = tcuTexture.sampleCubeSeamlessLinear(faceAccesses1, face, sampler, s, t, depth); + } + + return deMath.add(deMath.scale(t0, (1.0 - f)), deMath.scale(t1, f)); + } + + default: + throw new Error('Unsupported filter mode'); + } +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} faceAccess + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {number} s + * @param {number} t + * @param {number=} depth + * @return {number} + */ +tcuTexture.sampleCubeSeamlessNearestCompare = function(faceAccess, sampler, ref, s, t, depth) { + depth = depth ? depth : 0; + /** @type {tcuTexture.Sampler} */ var clampingSampler = deUtil.clone(sampler); + clampingSampler.wrapS = tcuTexture.WrapMode.CLAMP_TO_EDGE; + clampingSampler.wrapT = tcuTexture.WrapMode.CLAMP_TO_EDGE; + return faceAccess.sample2DCompare(clampingSampler, tcuTexture.FilterMode.NEAREST, ref, s, t, [0, 0, depth]); +}; + +/** + * @param {Array<tcuTexture.ConstPixelBufferAccess>} faceAccesses + * @param {tcuTexture.CubeFace} baseFace + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {number} s + * @param {number} t + * @return {number} + */ +tcuTexture.sampleCubeSeamlessLinearCompare = function(faceAccesses, baseFace, sampler, ref, s, t) { + DE_ASSERT(faceAccesses[0].getWidth() == faceAccesses[0].getHeight()); + + var size = faceAccesses[0].getWidth(); + // Non-normalized coordinates. + var u = s; + var v = t; + + if (sampler.normalizedCoords) { + u = tcuTexture.unnormalize(sampler.wrapS, s, size); + v = tcuTexture.unnormalize(sampler.wrapT, t, size); + } + + var x0 = Math.floor(u - 0.5); + var x1 = x0 + 1; + var y0 = Math.floor(v - 0.5); + var y1 = y0 + 1; + var baseSampleCoords = [ + [x0, y0], + [x1, y0], + [x0, y1], + [x1, y1] + ]; + var sampleRes = []; + var hasBothCoordsOutOfBounds = []; //!< Whether correctCubeFace() returns CUBEFACE_LAST, i.e. both u and v are out of bounds. + + // Find correct faces and coordinates for out-of-bounds sample coordinates. + + for (var i = 0; i < 4; i++) { + /** @type {tcuTexture.CubeFaceCoords} */ var coords = tcuTexture.remapCubeEdgeCoords(new tcuTexture.CubeFaceCoords(baseFace, baseSampleCoords[i]), size); + hasBothCoordsOutOfBounds[i] = coords == null; + + if (!hasBothCoordsOutOfBounds[i]) { + var isFixedPointDepth = tcuTexture.isFixedPointDepthTextureFormat(faceAccesses[coords.face].getFormat()); + + sampleRes[i] = tcuTexture.execCompare(faceAccesses[coords.face].getPixel(coords.s, coords.t), sampler.compare, sampler.compareChannel, ref, isFixedPointDepth); + } + } + + // If a sample was out of bounds in both u and v, we get its color from the average of the three other samples. + // \note This averaging behavior is not required by the GLES3 spec (though it is recommended). GLES3 spec only + // requires that if the three other samples all have the same color, then the doubly-out-of-bounds sample + // must have this color as well. + + var bothOutOfBoundsNdx = -1; + for (var i = 0; i < 4; i++) { + if (hasBothCoordsOutOfBounds[i]) { + DE_ASSERT(bothOutOfBoundsNdx < 0); // Only one sample can be out of bounds in both u and v. + bothOutOfBoundsNdx = i; + } + } + if (bothOutOfBoundsNdx != -1) { + sampleRes[bothOutOfBoundsNdx] = 0.0; + for (var i = 0; i < 4; i++) + if (i != bothOutOfBoundsNdx) + sampleRes[bothOutOfBoundsNdx] += sampleRes[i]; + + sampleRes[bothOutOfBoundsNdx] = sampleRes[bothOutOfBoundsNdx] * (1.0 / 3.0); + } + + // Interpolate. + + var a = deMath.deFloatFrac(u - 0.5); + var b = deMath.deFloatFrac(v - 0.5); + + return (sampleRes[0] * (1.0 - a) * (1.0 - b)) + + (sampleRes[1] * (a) * (1.0 - b)) + + (sampleRes[2] * (1.0 - a) * (b)) + + (sampleRes[3] * (a) * (b)); +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} faceAccess + * @param {tcuTexture.Sampler} sampler + * @param {number} s + * @param {number} t + * @param {number} depth + * @return {Array<number>} + */ +tcuTexture.sampleCubeSeamlessNearest = function(faceAccess, sampler, s, t, depth) { + /** @type {tcuTexture.Sampler} */ var clampingSampler = sampler; + clampingSampler.wrapS = tcuTexture.WrapMode.CLAMP_TO_EDGE; + clampingSampler.wrapT = tcuTexture.WrapMode.CLAMP_TO_EDGE; + return faceAccess.sample2D(clampingSampler, tcuTexture.FilterMode.NEAREST, s, t, depth); +}; + +/** + * @param {Array<number>} coords Vec3 cube coordinates + * @return {tcuTexture.CubeFaceCoords} + */ +tcuTexture.getCubeFaceCoords = function(coords) { + var face = tcuTexture.selectCubeFace(coords); + return new tcuTexture.CubeFaceCoords(face, tcuTexture.projectToFace(face, coords)); +}; + +/** + * @param {Array<Array<tcuTexture.ConstPixelBufferAccess>>} faces + * @param {number} numLevels + * @param {tcuTexture.CubeFace} face + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {number} s + * @param {number} t + * @param {number} lod + * @return {number} + */ +tcuTexture.sampleLevelArrayCubeSeamlessCompare = function(faces, numLevels, face, sampler, ref, s, t, lod) { + var magnified = lod <= sampler.lodThreshold; + /** @type {tcuTexture.FilterMode}*/ var filterMode = magnified ? sampler.magFilter : sampler.minFilter; + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faceAccesses = []; + /** @type {tcuTexture.FilterMode} */ var levelFilter; + + switch (filterMode) { + case tcuTexture.FilterMode.NEAREST: + return tcuTexture.sampleCubeSeamlessNearestCompare(faces[face][0], sampler, ref, s, t); + + case tcuTexture.FilterMode.LINEAR: { + faceAccesses = []; + for (var i = 0; i < Object.keys(tcuTexture.CubeFace).length; i++) + faceAccesses[i] = faces[i][0]; + + return tcuTexture.sampleCubeSeamlessLinearCompare(faceAccesses, face, sampler, ref, s, t); + } + + case tcuTexture.FilterMode.NEAREST_MIPMAP_NEAREST: + case tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST: { + var maxLevel = numLevels - 1; + var level = deMath.clamp(Math.ceil(lod + 0.5) - 1, 0, maxLevel); + levelFilter = filterMode == tcuTexture.FilterMode.LINEAR_MIPMAP_NEAREST ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + + if (levelFilter == tcuTexture.FilterMode.NEAREST) + return tcuTexture.sampleCubeSeamlessNearestCompare(faces[face][level], sampler, ref, s, t); + else { + DE_ASSERT(levelFilter == tcuTexture.FilterMode.LINEAR); + + faceAccesses = []; + for (var i = 0; i < Object.keys(tcuTexture.CubeFace).length; i++) + faceAccesses[i] = faces[i][level]; + + return tcuTexture.sampleCubeSeamlessLinearCompare(faceAccesses, face, sampler, ref, s, t); + } + } + + case tcuTexture.FilterMode.NEAREST_MIPMAP_LINEAR: + case tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR: { + var maxLevel = numLevels - 1; + var level0 = deMath.clamp(Math.floor(lod), 0, maxLevel); + var level1 = Math.min(maxLevel, level0 + 1); + levelFilter = (filterMode == tcuTexture.FilterMode.LINEAR_MIPMAP_LINEAR) ? tcuTexture.FilterMode.LINEAR : tcuTexture.FilterMode.NEAREST; + var f = deMath.deFloatFrac(lod); + var t0; + var t1; + + if (levelFilter == tcuTexture.FilterMode.NEAREST) { + t0 = tcuTexture.sampleCubeSeamlessNearestCompare(faces[face][level0], sampler, ref, s, t); + t1 = tcuTexture.sampleCubeSeamlessNearestCompare(faces[face][level1], sampler, ref, s, t); + } else { + DE_ASSERT(levelFilter == tcuTexture.FilterMode.LINEAR); + + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faceAccesses0 = []; + /** @type {Array<tcuTexture.ConstPixelBufferAccess>} */ var faceAccesses1 = []; + for (var i = 0; i < Object.keys(tcuTexture.CubeFace).length; i++) { + faceAccesses0[i] = faces[i][level0]; + faceAccesses1[i] = faces[i][level1]; + } + + t0 = tcuTexture.sampleCubeSeamlessLinearCompare(faceAccesses0, face, sampler, ref, s, t); + t1 = tcuTexture.sampleCubeSeamlessLinearCompare(faceAccesses1, face, sampler, ref, s, t); + } + + return t0 * (1.0 - f) + t1 * f; + } + + default: + throw new Error('Unsupported filter mode'); + } +}; + +/** + * @constructor + * @extends {tcuTexture.TextureLevelPyramid} + * @param {tcuTexture.TextureFormat} format + * @param {number} width + * @param {number} height + */ +tcuTexture.Texture2D = function(format, width, height) { + tcuTexture.TextureLevelPyramid.call(this, format, tcuTexture.computeMipPyramidLevels(width, height)); + this.m_width = width; + this.m_height = height; + this.m_view = new tcuTexture.Texture2DView(this.getNumLevels(), this.getLevels()); +}; + +tcuTexture.Texture2D.prototype = Object.create(tcuTexture.TextureLevelPyramid.prototype); +tcuTexture.Texture2D.prototype.constructor = tcuTexture.Texture2D; + +tcuTexture.Texture2D.prototype.getWidth = function() { return this.m_width; }; +tcuTexture.Texture2D.prototype.getHeight = function() { return this.m_height; }; +/** @return {tcuTexture.Texture2DView} */ +tcuTexture.Texture2D.prototype.getView = function() { return this.m_view; }; + +/** + * @param {number} baseLevel + * @param {number} maxLevel + * @return {tcuTexture.Texture2DView} + */ +tcuTexture.Texture2D.prototype.getSubView = function(baseLevel, maxLevel) { return this.m_view.getSubView(baseLevel, maxLevel); }; + +/** + * @param {number} levelNdx + */ +tcuTexture.Texture2D.prototype.allocLevel = function(levelNdx) { + DE_ASSERT(deMath.deInBounds32(levelNdx, 0, this.getNumLevels())); + + var width = tcuTexture.getMipPyramidLevelSize(this.m_width, levelNdx); + var height = tcuTexture.getMipPyramidLevelSize(this.m_height, levelNdx); + + tcuTexture.TextureLevelPyramid.prototype.allocLevel.call(this, levelNdx, width, height, 1); +}; + +/** + * @constructor + * @extends {tcuTexture.TextureLevelPyramid} + * @param {tcuTexture.TextureFormat} format + * @param {number} width + * @param {number} height + * @param {number} numLayers + */ +tcuTexture.Texture2DArray = function(format, width, height, numLayers) { + tcuTexture.TextureLevelPyramid.call(this, format, tcuTexture.computeMipPyramidLevels(width, height)); + this.m_width = width; + this.m_height = height; + this.m_numLayers = numLayers; + this.m_view = new tcuTexture.Texture2DArrayView(this.getNumLevels(), this.getLevels()); +}; + +tcuTexture.Texture2DArray.prototype = Object.create(tcuTexture.TextureLevelPyramid.prototype); +tcuTexture.Texture2DArray.prototype.constructor = tcuTexture.Texture2DArray; +/** @return {tcuTexture.Texture2DArrayView} */ +tcuTexture.Texture2DArray.prototype.getView = function() { return this.m_view; }; + +/** @return {number} */ +tcuTexture.Texture2DArray.prototype.getWidth = function() { return this.m_width; }; + +/** @return {number} */ +tcuTexture.Texture2DArray.prototype.getHeight = function() { return this.m_height; }; + +/** + * @param {number} levelNdx + */ +tcuTexture.Texture2DArray.prototype.allocLevel = function(levelNdx) { + DE_ASSERT(deMath.deInBounds32(levelNdx, 0, this.getNumLevels())); + + var width = tcuTexture.getMipPyramidLevelSize(this.m_width, levelNdx); + var height = tcuTexture.getMipPyramidLevelSize(this.m_height, levelNdx); + + tcuTexture.TextureLevelPyramid.prototype.allocLevel.call(this, levelNdx, width, height, this.m_numLayers); +}; + +/** + * @constructor + * @extends {tcuTexture.TextureLevelPyramid} + * @param {tcuTexture.TextureFormat} format + * @param {number} width + * @param {number} height + * @param {number} depth + */ +tcuTexture.Texture3D = function(format, width, height, depth) { + tcuTexture.TextureLevelPyramid.call(this, format, tcuTexture.computeMipPyramidLevels(width, height, depth)); + this.m_width = width; + this.m_height = height; + this.m_depth = depth; + this.m_view = new tcuTexture.Texture3DView(this.getNumLevels(), this.getLevels()); +}; + +tcuTexture.Texture3D.prototype = Object.create(tcuTexture.TextureLevelPyramid.prototype); +tcuTexture.Texture3D.prototype.constructor = tcuTexture.Texture3D; + +tcuTexture.Texture3D.prototype.getWidth = function() { return this.m_width; }; +tcuTexture.Texture3D.prototype.getHeight = function() { return this.m_height; }; +tcuTexture.Texture3D.prototype.getDepth = function() { return this.m_depth; }; +tcuTexture.Texture3D.prototype.getView = function() { return this.m_view; }; +/** + * @param {number} baseLevel + * @param {number} maxLevel + * @return {tcuTexture.Texture3DView} + */ +tcuTexture.Texture3D.prototype.getSubView = function(baseLevel, maxLevel) { return this.m_view.getSubView(baseLevel, maxLevel); }; + +/** + * @param {number} levelNdx + */ +tcuTexture.Texture3D.prototype.allocLevel = function(levelNdx) { + DE_ASSERT(deMath.deInBounds32(levelNdx, 0, this.getNumLevels())); + + var width = tcuTexture.getMipPyramidLevelSize(this.m_width, levelNdx); + var height = tcuTexture.getMipPyramidLevelSize(this.m_height, levelNdx); + var depth = tcuTexture.getMipPyramidLevelSize(this.m_depth, levelNdx); + + tcuTexture.TextureLevelPyramid.prototype.allocLevel.call(this, levelNdx, width, height, depth); +}; + +/** + * @constructor + * @param {number} numLevels + * @param {Array<Array<tcuTexture.ConstPixelBufferAccess>>} levels + */ +tcuTexture.TextureCubeView = function(numLevels, levels) { + this.m_numLevels = numLevels; + this.m_levels = levels; +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number=} lod + * @return {Array<number>} Pixel color + */ +tcuTexture.TextureCubeView.prototype.sample = function(sampler, texCoord, lod) { + DE_ASSERT(sampler.compare == tcuTexture.CompareMode.COMPAREMODE_NONE); + + // Computes (face, s, t). + var coords = tcuTexture.getCubeFaceCoords(texCoord); + if (sampler.seamlessCubeMap) + return tcuTexture.sampleLevelArrayCubeSeamless(this.m_levels, this.m_numLevels, coords.face, sampler, coords.s, coords.t, 0 /* depth */, lod); + else + return tcuTexture.sampleLevelArray2D(this.m_levels[coords.face], this.m_numLevels, sampler, coords.s, coords.t, 0 /* depth */, lod); +}; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {number} ref + * @param {Array<number>} texCoord + * @param {number} lod + * @return {number} + */ +tcuTexture.TextureCubeView.prototype.sampleCompare = function(sampler, ref, texCoord, lod) { + DE_ASSERT(sampler.compare != tcuTexture.CompareMode.COMPAREMODE_NONE); + + // Computes (face, s, t). + var coords = tcuTexture.getCubeFaceCoords(texCoord); + if (sampler.seamlessCubeMap) + return tcuTexture.sampleLevelArrayCubeSeamlessCompare(this.m_levels, this.m_numLevels, coords.face, sampler, ref, coords.s, coords.t, lod); + else + return tcuTexture.sampleLevelArray2DCompare(this.m_levels[coords.face], this.m_numLevels, sampler, ref, coords.s, coords.t, lod, [0, 0, 0]); +}; + +/** + * @param {tcuTexture.CubeFace} face + * @return {Array<tcuTexture.ConstPixelBufferAccess>} + */ +tcuTexture.TextureCubeView.prototype.getFaceLevels = function(face) { return this.m_levels[face]; }; +/** @return {number} */ +tcuTexture.TextureCubeView.prototype.getSize = function() { return this.m_numLevels > 0 ? this.m_levels[0][0].getWidth() : 0; }; + +/** @return {number} */ +tcuTexture.TextureCubeView.prototype.getNumLevels = function() { return this.m_numLevels; }; + +/** + * @param {number} ndx + * @param {tcuTexture.CubeFace} face + * @return {tcuTexture.ConstPixelBufferAccess} + */ +tcuTexture.TextureCubeView.prototype.getLevelFace = function(ndx, face) { + assertMsgOptions(0 <= ndx && ndx < this.m_numLevels, '', false, true); + return this.m_levels[face][ndx]; +}; + +/** + * @param {number} baseLevel + * @param {number} maxLevel + * @return {tcuTexture.TextureCubeView} + */ +tcuTexture.TextureCubeView.prototype.getSubView = function(baseLevel, maxLevel) { + var clampedBase = deMath.clamp(baseLevel, 0, this.m_numLevels - 1); + var clampedMax = deMath.clamp(maxLevel, clampedBase, this.m_numLevels - 1); + var numLevels = clampedMax - clampedBase + 1; + var levels = []; + for (var face in tcuTexture.CubeFace) + levels.push(this.getFaceLevels(tcuTexture.CubeFace[face]).slice(clampedBase, numLevels)); + + return new tcuTexture.TextureCubeView(numLevels, levels); +}; + +/** + * @constructor + * @param {tcuTexture.TextureFormat} format + * @param {number} size + */ +tcuTexture.TextureCube = function(format, size) { + this.m_format = format; + this.m_size = size; + this.m_data = []; + this.m_data.length = Object.keys(tcuTexture.CubeFace).length; + this.m_access = []; + this.m_access.length = Object.keys(tcuTexture.CubeFace).length; + + var numLevels = tcuTexture.computeMipPyramidLevels(this.m_size); + var levels = []; + levels.length = Object.keys(tcuTexture.CubeFace).length; + + for (var face in tcuTexture.CubeFace) { + this.m_data[tcuTexture.CubeFace[face]] = []; + for (var i = 0; i < numLevels; i++) + this.m_data[tcuTexture.CubeFace[face]].push(new tcuTexture.DeqpArrayBuffer()); + this.m_access[tcuTexture.CubeFace[face]] = []; + this.m_access[tcuTexture.CubeFace[face]].length = numLevels; + levels[tcuTexture.CubeFace[face]] = this.m_access[tcuTexture.CubeFace[face]]; + } + + this.m_view = new tcuTexture.TextureCubeView(numLevels, levels); +}; + +/** @return {tcuTexture.TextureFormat} */ +tcuTexture.TextureCube.prototype.getFormat = function() { return this.m_format; }; +/** @return {number} */ +tcuTexture.TextureCube.prototype.getSize = function() { return this.m_size; }; +/** @return {tcuTexture.TextureCubeView} */ +tcuTexture.TextureCube.prototype.getView = function() { return this.m_view; }; +/** + * @param {number} ndx Level index + * @param {tcuTexture.CubeFace} face + * @return {tcuTexture.PixelBufferAccess} + */ +tcuTexture.TextureCube.prototype.getLevelFace = function(ndx, face) { return this.m_access[face][ndx]; }; +/** @return {number} */ +tcuTexture.TextureCube.prototype.getNumLevels = function() { return this.m_access[0].length; }; + +/** + * @param {tcuTexture.Sampler} sampler + * @param {Array<number>} texCoord + * @param {number} lod + * @return {Array<number>} Pixel color + */ +tcuTexture.TextureCube.prototype.sample = function(sampler, texCoord, lod) { + return this.m_view.sample(sampler, texCoord, lod); +}; + +/** + * @param {number} baseLevel + * @param {number} maxLevel + * @return {tcuTexture.TextureCubeView} + */ +tcuTexture.TextureCube.prototype.getSubView = function(baseLevel, maxLevel) { return this.m_view.getSubView(baseLevel, maxLevel); }; + +/** + * @param {tcuTexture.CubeFace} face + * @param {number} levelNdx + * @return {boolean} + */ +tcuTexture.TextureCube.prototype.isLevelEmpty = function(face, levelNdx) { + return this.m_data[face][levelNdx].empty(); +}; + +/** + * @param {tcuTexture.CubeFace} face + * @param {number} levelNdx + */ +tcuTexture.TextureCube.prototype.allocLevel = function(face, levelNdx) { + /** @const */ var size = tcuTexture.getMipPyramidLevelSize(this.m_size, levelNdx); + /** @const*/ var dataSize = this.m_format.getPixelSize() * size * size; + DE_ASSERT(this.isLevelEmpty(face, levelNdx)); + + this.m_data[face][levelNdx].setStorage(dataSize); + this.m_access[face][levelNdx] = new tcuTexture.PixelBufferAccess({ + format: this.m_format, + width: size, + height: size, + depth: 1, + data: this.m_data[face][levelNdx].m_ptr + }); +}; + +/** + * @param {Array<number>} coords Cube coordinates + * @return {tcuTexture.CubeFace} + */ +tcuTexture.selectCubeFace = function(coords) { + var x = coords[0]; + var y = coords[1]; + var z = coords[2]; + var ax = Math.abs(x); + var ay = Math.abs(y); + var az = Math.abs(z); + + if (ay < ax && az < ax) + return x >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_X : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X; + else if (ax < ay && az < ay) + return y >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y; + else if (ax < az && ay < az) + return z >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z; + else { + // Some of the components are equal. Use tie-breaking rule. + if (ax == ay) { + if (ax < az) + return z >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z; + else + return x >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_X : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X; + } else if (ax == az) { + if (az < ay) + return y >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y; + else + return z >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z; + } else if (ay == az) { + if (ay < ax) + return x >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_X : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X; + else + return y >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y; + } else + return x >= 0 ? tcuTexture.CubeFace.CUBEFACE_POSITIVE_X : tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X; + } +}; + +/** + * @param {tcuTexture.CubeFace} face + * @param {Array<number>} coord Cube coordinates (Vec3) + * @return {Array<number>} face coordinates (Vec2) + */ +tcuTexture.projectToFace = function(face, coord) { + var rx = coord[0]; + var ry = coord[1]; + var rz = coord[2]; + var sc = 0; + var tc = 0; + var ma = 0; + + switch (face) { + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X: sc = +rz; tc = -ry; ma = -rx; break; + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_X: sc = -rz; tc = -ry; ma = +rx; break; + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y: sc = +rx; tc = -rz; ma = -ry; break; + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y: sc = +rx; tc = +rz; ma = +ry; break; + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z: sc = -rx; tc = -ry; ma = -rz; break; + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z: sc = +rx; tc = -ry; ma = +rz; break; + default: + throw new Error('Unrecognized face ' + face); + } + + // Compute s, t + var s = ((sc / ma) + 1) / 2; + var t = ((tc / ma) + 1) / 2; + + return [s, t]; +}; + +/** + * @constructor + * @param {tcuTexture.TextureFormat} format + * @param {number=} width + * @param {number=} height + * @param {number=} depth + */ +tcuTexture.TextureLevel = function(format, width, height, depth) { + this.m_format = format; + this.m_width = width || 0; + this.m_height = height || 0; + this.m_depth = depth === undefined ? 1 : depth; + this.m_data = new tcuTexture.DeqpArrayBuffer(); + this.setSize(this.m_width, this.m_height, this.m_depth); +}; + +tcuTexture.TextureLevel.prototype.constructor = tcuTexture.TextureLevel; + +/** + * @param {tcuTexture.TextureFormat} format + * @param {number=} width + * @param {number=} height + * @param {number=} depth + */ +tcuTexture.TextureLevel.prototype.setStorage = function(format, width, height, depth) { + this.m_format = format; + this.setSize(width, height, depth); +}; + +/** + * @param {number=} width + * @param {number=} height + * @param {number=} depth + */ +tcuTexture.TextureLevel.prototype.setSize = function(width, height, depth) { + var pixelSize = this.m_format.getPixelSize(); + + this.m_width = width || 0; + this.m_height = height || 0; + this.m_depth = depth === undefined ? 1 : depth; + + this.m_data.setStorage(this.m_width * this.m_height * this.m_depth * pixelSize); +}; + +/** + * @return {tcuTexture.PixelBufferAccess} + */ +tcuTexture.TextureLevel.prototype.getAccess = function() { + return new tcuTexture.PixelBufferAccess({ + format: this.m_format, + width: this.m_width, + height: this.m_height, + depth: this.m_depth, + data: this.m_data.m_ptr + }); + +}; + +/** + * @return {number} + */ +tcuTexture.TextureLevel.prototype.getWidth = function() { + return this.m_width; +}; + +/** + * @return {number} + */ +tcuTexture.TextureLevel.prototype.getHeight = function() { + return this.m_height; +}; + +/** + * @return {number} + */ +tcuTexture.TextureLevel.prototype.getDepth = function() { + return this.m_depth; +}; + +/** + * @return {?tcuTexture.TextureFormat} + */ +tcuTexture.TextureLevel.prototype.getFormat = function() { + return this.m_format; +}; + +/** + * Checks if origCoords.coords is in bounds defined by size; if not, return a CubeFaceCoords with face set to the appropriate neighboring face and coords transformed accordingly. + * \note If both x and y in origCoords.coords are out of bounds, this returns with face CUBEFACE_LAST, signifying that there is no unique neighboring face. + * @param {tcuTexture.CubeFaceCoords} origCoords + * @param {number} size + * @return {tcuTexture.CubeFaceCoords} + */ +tcuTexture.remapCubeEdgeCoords = function(origCoords, size) { + var uInBounds = deMath.deInBounds32(origCoords.s, 0, size); + var vInBounds = deMath.deInBounds32(origCoords.t, 0, size); + + if (uInBounds && vInBounds) + return origCoords; + + if (!uInBounds && !vInBounds) + return null; + + var coords = [ + tcuTexture.wrap(tcuTexture.WrapMode.CLAMP_TO_BORDER, origCoords.s, size), + tcuTexture.wrap(tcuTexture.WrapMode.CLAMP_TO_BORDER, origCoords.t, size)]; + var canonizedCoords = []; + + // Map the uv coordinates to canonized 3d coordinates. + + switch (origCoords.face) { + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X: canonizedCoords = [0, size - 1 - coords[1], coords[0]]; break; + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_X: canonizedCoords = [size - 1, size - 1 - coords[1], size - 1 - coords[0]]; break; + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y: canonizedCoords = [coords[0], 0, size - 1 - coords[1]]; break; + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y: canonizedCoords = [coords[0], size - 1, coords[1]]; break; + case tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z: canonizedCoords = [size - 1 - coords[0], size - 1 - coords[1], 0]; break; + case tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z: canonizedCoords = [coords[0], size - 1 - coords[1], size - 1]; break; + default: throw new Error('Invalid cube face:' + origCoords.face); + } + + // Find an appropriate face to re-map the coordinates to. + + if (canonizedCoords[0] == -1) + return new tcuTexture.CubeFaceCoords(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X, [canonizedCoords[2], size - 1 - canonizedCoords[1]]); + + if (canonizedCoords[0] == size) + return new tcuTexture.CubeFaceCoords(tcuTexture.CubeFace.CUBEFACE_POSITIVE_X, [size - 1 - canonizedCoords[2], size - 1 - canonizedCoords[1]]); + + if (canonizedCoords[1] == -1) + return new tcuTexture.CubeFaceCoords(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y, [canonizedCoords[0], size - 1 - canonizedCoords[2]]); + + if (canonizedCoords[1] == size) + return new tcuTexture.CubeFaceCoords(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y, [canonizedCoords[0], canonizedCoords[2]]); + + if (canonizedCoords[2] == -1) + return new tcuTexture.CubeFaceCoords(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z, [size - 1 - canonizedCoords[0], size - 1 - canonizedCoords[1]]); + + if (canonizedCoords[2] == size) + return new tcuTexture.CubeFaceCoords(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z, [canonizedCoords[0], size - 1 - canonizedCoords[1]]); + + throw new Error('Cannot remap cube coordinates'); +}; + +/** + * @constructor + * @param {tcuTexture.ConstPixelBufferAccess} src + */ +tcuTexture.RGBA8View = function(src) { + this.src = src; + this.data = new Uint8Array(src.getBuffer(), src.m_offset); + this.stride = src.getRowPitch(); + this.width = src.getWidth(); + this.height = src.getHeight(); + this.pixelSize = src.getFormat().getPixelSize(); +}; + +/** + * @return {tcuTexture.TextureFormat} + */ +tcuTexture.RGBA8View.prototype.getFormat = function() { return this.src.getFormat(); }; + +/** + * Read a pixel + * @param {number} x + * @param {number} y + * @param {number=} numChannels + * @return {Array<number>} + */ +tcuTexture.RGBA8View.prototype.read = function(x, y, numChannels) { + numChannels = numChannels || 4; + var offset = y * this.stride + x * this.pixelSize; + /* Always return a vec4 */ + var result = [0, 0, 0, 255]; + for (var i = 0; i < numChannels; i++) + result[i] = this.data[offset + i]; + return result; +}; + +/** + * Read a pixel into a Uint32 + * @param {number} x + * @param {number} y + * @return {number} + */ +tcuTexture.RGBA8View.prototype.readUintRGBA8 = function(x, y) { + var offset = y * this.stride + x * this.pixelSize; + return ((this.data[offset] & 0xff) << 24) + + ((this.data[offset + 1] & 0xff) << 16) + + ((this.data[offset + 2] & 0xff) << 8) + + (this.data[offset + 3] & 0xff); +}; + +/** + * Write a pixel + * @param {number} x + * @param {number} y + * @param {Array<number>} value + * @param {number=} numChannels + */ +tcuTexture.RGBA8View.prototype.write = function(x, y, value, numChannels) { + numChannels = numChannels || 4; + var offset = y * this.stride + x * this.pixelSize; + for (var i = 0; i < numChannels; i++) + this.data[offset + i] = value[i]; +}; + +tcuTexture.RGBA8View.prototype.getWidth = function() { return this.width; }; + +tcuTexture.RGBA8View.prototype.getHeight = function() { return this.height; }; + +}); diff --git a/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTextureUtil.js b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTextureUtil.js new file mode 100644 index 000000000..40450ab38 --- /dev/null +++ b/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuTextureUtil.js @@ -0,0 +1,725 @@ +/*------------------------------------------------------------------------- + * drawElements Quality Program OpenGL ES Utilities + * ------------------------------------------------ + * + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a tcuTextureUtil.copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +'use strict'; +goog.provide('framework.common.tcuTextureUtil'); +goog.require('framework.common.tcuTexture'); +goog.require('framework.delibs.debase.deMath'); +goog.require('framework.delibs.debase.deRandom'); + +goog.scope(function() { + +var tcuTextureUtil = framework.common.tcuTextureUtil; +var tcuTexture = framework.common.tcuTexture; +var deMath = framework.delibs.debase.deMath; +var deRandom = framework.delibs.debase.deRandom; + +var DE_ASSERT = function(x) { + if (!x) + throw new Error('Assert failed'); +}; + +/** + * @param {number} t + * @param {number} minVal + * @param {number} maxVal + * @return {number} + */ +tcuTextureUtil.linearInterpolate = function(t, minVal, maxVal) { + return minVal + (maxVal - minVal) * t; +}; + +/** tcuTextureUtil.linearChannelToSRGB + * @param {number} cl + * @return {number} + */ +tcuTextureUtil.linearChannelToSRGB = function(cl) { + if (cl <= 0.0) + return 0.0; + else if (cl < 0.0031308) + return 12.92 * cl; + else if (cl < 1.0) + return 1.055 * Math.pow(cl, 0.41666) - 0.055; + else + return 1.0; +}; + +/** + * Convert sRGB to linear colorspace + * @param {Array<number>} cs + * @return {Array<number>} + */ +tcuTextureUtil.sRGBToLinear = function(cs) { + return [tcuTextureUtil.sRGBChannelToLinear(cs[0]), + tcuTextureUtil.sRGBChannelToLinear(cs[1]), + tcuTextureUtil.sRGBChannelToLinear(cs[2]), + cs[3]]; +}; + +/** + * @param {number} cs + * @return {number} + */ + tcuTextureUtil.sRGBChannelToLinear = function(cs) { + if (cs <= 0.04045) + return cs / 12.92; + else + return Math.pow((cs + 0.055) / 1.055, 2.4); +}; + +/** tcuTextureUtil.linearToSRGB + * @param {Array<number>} cl + * @return {Array<number>} + */ +tcuTextureUtil.linearToSRGB = function(cl) { + return [tcuTextureUtil.linearChannelToSRGB(cl[0]), + tcuTextureUtil.linearChannelToSRGB(cl[1]), + tcuTextureUtil.linearChannelToSRGB(cl[2]), + cl[3] + ]; +}; + +/** + * tcuTextureUtil.getSubregion + * @param {tcuTexture.PixelBufferAccess} access + * @param {number} x + * @param {number} y + * @param {number} z + * @param {number} width + * @param {number} height + * @param {number} depth + * @return {tcuTexture.PixelBufferAccess} + */ +tcuTextureUtil.getSubregion = function(access, x, y, z, width, height, depth) { + + DE_ASSERT(deMath.deInBounds32(x, 0, access.getWidth()) && deMath.deInRange32(x + width, x, access.getWidth())); + DE_ASSERT(deMath.deInBounds32(y, 0, access.getHeight()) && deMath.deInRange32(y + height, y, access.getHeight())); + DE_ASSERT(deMath.deInBounds32(z, 0, access.getDepth()) && deMath.deInRange32(z + depth, z, access.getDepth())); + + return new tcuTexture.PixelBufferAccess({ + format: access.getFormat(), + width: width, + height: height, + depth: depth, + rowPitch: access.getRowPitch(), + slicePitch: access.getSlicePitch(), + offset: access.m_offset + access.getFormat().getPixelSize() * x + access.getRowPitch() * y + access.getSlicePitch() * z, + data: access.getBuffer() + }); +}; + +/** + * @param {tcuTexture.PixelBufferAccess} access + * @param {Array<number>} minVal + * @param {Array<number>} maxVal + */ +tcuTextureUtil.fillWithComponentGradients1D = function(access, minVal, maxVal) { + DE_ASSERT(access.getHeight() == 1); + for (var x = 0; x < access.getWidth(); x++) { + var s = (x + 0.5) / access.getWidth(); + + var r = tcuTextureUtil.linearInterpolate(s, minVal[0], maxVal[0]); + var g = tcuTextureUtil.linearInterpolate(s, minVal[1], maxVal[1]); + var b = tcuTextureUtil.linearInterpolate(s, minVal[2], maxVal[2]); + var a = tcuTextureUtil.linearInterpolate(s, minVal[3], maxVal[3]); + + access.setPixel([r, g, b, a], x, 0); + } +}; + +/** + * @param {tcuTexture.PixelBufferAccess} access + * @param {Array<number>} minVal + * @param {Array<number>} maxVal + */ +tcuTextureUtil.fillWithComponentGradients2D = function(access, minVal, maxVal) { + for (var y = 0; y < access.getHeight(); y++) { + var t = (y + 0.5) / access.getHeight(); + for (var x = 0; x < access.getWidth(); x++) { + var s = (x + 0.5) / access.getWidth(); + + var r = tcuTextureUtil.linearInterpolate((s + t) * 0.5, minVal[0], maxVal[0]); + var g = tcuTextureUtil.linearInterpolate((s + (1 - t)) * 0.5, minVal[1], maxVal[1]); + var b = tcuTextureUtil.linearInterpolate(((1 - s) + t) * 0.5, minVal[2], maxVal[2]); + var a = tcuTextureUtil.linearInterpolate(((1 - s) + (1 - t)) * 0.5, minVal[3], maxVal[3]); + + access.setPixel([r, g, b, a], x, y); + } + } +}; + +/** + * @param {tcuTexture.PixelBufferAccess} dst + * @param {Array<number>} minVal + * @param {Array<number>} maxVal + */ +tcuTextureUtil.fillWithComponentGradients3D = function(dst, minVal, maxVal) { + for (var z = 0; z < dst.getDepth(); z++) { + var p = (z + 0.5) / dst.getDepth(); + var b = tcuTextureUtil.linearInterpolate(p, minVal[2], maxVal[2]); + for (var y = 0; y < dst.getHeight(); y++) { + var t = (y + 0.5) / dst.getHeight(); + var g = tcuTextureUtil.linearInterpolate(t, minVal[1], maxVal[1]); + for (var x = 0; x < dst.getWidth(); x++) { + var s = (x + 0.5) / dst.getWidth(); + var r = tcuTextureUtil.linearInterpolate(s, minVal[0], maxVal[0]); + var a = tcuTextureUtil.linearInterpolate(1 - (s + t + p) / 3, minVal[3], maxVal[3]); + dst.setPixel([r, g, b, a], x, y, z); + } + } + } +}; + +/** + * @param {tcuTexture.PixelBufferAccess} access + * @param {Array<number>} minVal + * @param {Array<number>} maxVal + */ +tcuTextureUtil.fillWithComponentGradients = function(access, minVal, maxVal) { + if (access.getHeight() == 1 && access.getDepth() == 1) + tcuTextureUtil.fillWithComponentGradients1D(access, minVal, maxVal); + else if (access.getDepth() == 1) + tcuTextureUtil.fillWithComponentGradients2D(access, minVal, maxVal); + else + tcuTextureUtil.fillWithComponentGradients3D(access, minVal, maxVal); +}; + +/** + * @param {tcuTexture.PixelBufferAccess} dst + */ +tcuTextureUtil.fillWithRGBAQuads = function(dst) { + checkMessage(dst.getDepth() == 1, 'Depth must be 1'); + var width = dst.getWidth(); + var height = dst.getHeight(); + var left = width / 2; + var top = height / 2; + + tcuTextureUtil.getSubregion(dst, 0, 0, 0, left, top, 1).clear([1.0, 0.0, 0.0, 1.0]); + tcuTextureUtil.getSubregion(dst, left, 0, 0, width - left, top, 1).clear([0.0, 1.0, 0.0, 1.0]); + tcuTextureUtil.getSubregion(dst, 0, top, 0, left, height - top, 1).clear([0.0, 0.0, 1.0, 0.0]); + tcuTextureUtil.getSubregion(dst, left, top, 0, width - left, height - top, 1).clear([0.5, 0.5, 0.5, 1.0]); +}; + +// \todo [2012-11-13 pyry] There is much better metaballs code in CL SIR value generators. +/** + * @param {tcuTexture.PixelBufferAccess} dst + * @param {number} numBalls + * @param {number} seed + */ +tcuTextureUtil.fillWithMetaballs = function(dst, numBalls, seed) { + checkMessage(dst.getDepth() == 1, 'Depth must be 1'); + var points = []; + var rnd = new deRandom.Random(seed); + + for (var i = 0; i < numBalls; i++) { + var x = rnd.getFloat(); + var y = rnd.getFloat(); + points[i] = [x, y]; + } + + for (var y = 0; y < dst.getHeight(); y++) + for (var x = 0; x < dst.getWidth(); x++) { + var p = [x / dst.getWidth(), y / dst.getHeight()]; + + var sum = 0.0; + for (var pointNdx = 0; pointNdx < points.length; pointNdx++) { + var d = deMath.subtract(p, points[pointNdx]); + var f = 0.01 / (d[0] * d[0] + d[1] * d[1]); + + sum += f; + } + + dst.setPixel([sum, sum, sum, sum], x, y); + } +}; + +/** + * Create tcuTextureUtil.TextureFormatInfo. + * @constructor + * @param {Array<number>} valueMin + * @param {Array<number>} valueMax + * @param {Array<number>} lookupScale + * @param {Array<number>} lookupBias + */ +tcuTextureUtil.TextureFormatInfo = function(valueMin, valueMax, lookupScale, lookupBias) { + /** @type {Array<number>} */ this.valueMin = valueMin; + /** @type {Array<number>} */ this.valueMax = valueMax; + /** @type {Array<number>} */ this.lookupScale = lookupScale; + /** @type {Array<number>} */ this.lookupBias = lookupBias; +}; + +/** + * @param {?tcuTexture.ChannelType} channelType + * @return {Array<number>} + */ +tcuTextureUtil.getChannelValueRange = function(channelType) { + var cMin = 0; + var cMax = 0; + + switch (channelType) { + // Signed normalized formats. + case tcuTexture.ChannelType.SNORM_INT8: + case tcuTexture.ChannelType.SNORM_INT16: cMin = -1; cMax = 1; break; + + // Unsigned normalized formats. + case tcuTexture.ChannelType.UNORM_INT8: + case tcuTexture.ChannelType.UNORM_INT16: + case tcuTexture.ChannelType.UNORM_SHORT_565: + case tcuTexture.ChannelType.UNORM_SHORT_4444: + case tcuTexture.ChannelType.UNORM_INT_101010: + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: cMin = 0; cMax = 1; break; + + // Misc formats. + case tcuTexture.ChannelType.SIGNED_INT8: cMin = -128; cMax = 127; break; + case tcuTexture.ChannelType.SIGNED_INT16: cMin = -32768; cMax = 32767; break; + case tcuTexture.ChannelType.SIGNED_INT32: cMin = -2147483648; cMax = 2147483647; break; + case tcuTexture.ChannelType.UNSIGNED_INT8: cMin = 0; cMax = 255; break; + case tcuTexture.ChannelType.UNSIGNED_INT16: cMin = 0; cMax = 65535; break; + case tcuTexture.ChannelType.UNSIGNED_INT32: cMin = 0; cMax = 4294967295; break; + case tcuTexture.ChannelType.HALF_FLOAT: cMin = -1e3; cMax = 1e3; break; + case tcuTexture.ChannelType.FLOAT: cMin = -1e5; cMax = 1e5; break; + case tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV: cMin = 0; cMax = 1e4; break; + case tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV: cMin = 0; cMax = 1e5; break; + + default: + DE_ASSERT(false); + } + + return [cMin, cMax]; +}; + +/** + * Creates an array by choosing between 'a' and 'b' based on 'cond' array. + * @param {Array | number} a + * @param {Array | number} b + * @param {Array<boolean>} cond Condtions + * @return {Array} + */ +tcuTextureUtil.select = function(a, b, cond) { + + /*DE_ASSERT(!(a.length && !b.length) + || !(!a.length && b.length) + || !((a.length && b.length) && ((a.length != b.length) || (b.length != cond.length) || (a.length != cond.length))));*/ + + if (a.length && !b.length) throw new Error('second input parameter is not a vector'); + if (!a.length && b.length) throw new Error('first input parameter is not a vector'); + if ((a.length && b.length) && ((a.length != b.length) || (b.length != cond.length) || (a.length != cond.length))) throw new Error('different size vectors'); + + var dst = []; + for (var i = 0; i < cond.length; i++) + if (cond[i]) { + if (a.length) dst.push(a[i]); + else dst.push(a); + } else { + if (b.length) dst.push(b[i]); + else dst.push(b); + } + return dst; +}; + +/** + * Get standard parameters for testing texture format + * + * Returns tcuTextureUtil.TextureFormatInfo that describes good parameters for exercising + * given TextureFormat. Parameters include value ranges per channel and + * suitable lookup scaling and bias in order to reduce result back to + * 0..1 range. + * + * @param {tcuTexture.TextureFormat} format + * @return {tcuTextureUtil.TextureFormatInfo} + */ +tcuTextureUtil.getTextureFormatInfo = function(format) { + // Special cases. + if (format.isEqual(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV))) + return new tcuTextureUtil.TextureFormatInfo([0, 0, 0, 0], + [1023, 1023, 1023, 3], + [1 / 1023, 1 / 1023, 1 / 1023, 1 / 3], + [0, 0, 0, 0]); + else if (format.order == tcuTexture.ChannelOrder.D || format.order == tcuTexture.ChannelOrder.DS) + return new tcuTextureUtil.TextureFormatInfo([0, 0, 0, 0], + [1, 1, 1, 0], + [1, 1, 1, 1], + [0, 0, 0, 0]); // Depth / stencil formats. + else if (format.isEqual(new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_SHORT_5551))) + return new tcuTextureUtil.TextureFormatInfo([0, 0, 0, 0.5], + [1, 1, 1, 1.5], + [1, 1, 1, 1], + [0, 0, 0, 0]); + + var cRange = tcuTextureUtil.getChannelValueRange(format.type); + var chnMask = null; + + switch (format.order) { + case tcuTexture.ChannelOrder.R: chnMask = [true, false, false, false]; break; + case tcuTexture.ChannelOrder.A: chnMask = [false, false, false, true]; break; + case tcuTexture.ChannelOrder.L: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.LA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.RG: chnMask = [true, true, false, false]; break; + case tcuTexture.ChannelOrder.RGB: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.RGBA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.sRGB: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.sRGBA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.D: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.DS: chnMask = [true, true, true, true]; break; + default: + DE_ASSERT(false); + } + + var scale = 1 / (cRange[1] - cRange[0]); + var bias = -cRange[0] * scale; + + return new tcuTextureUtil.TextureFormatInfo(tcuTextureUtil.select(cRange[0], 0, chnMask), + tcuTextureUtil.select(cRange[1], 0, chnMask), + tcuTextureUtil.select(scale, 1, chnMask), + tcuTextureUtil.select(bias, 0, chnMask)); +}; + +/** tcuTextureUtil.getChannelBitDepth + * @param {?tcuTexture.ChannelType} channelType + * @return {Array<number>} + */ +tcuTextureUtil.getChannelBitDepth = function(channelType) { + + switch (channelType) { + case tcuTexture.ChannelType.SNORM_INT8: return [8, 8, 8, 8]; + case tcuTexture.ChannelType.SNORM_INT16: return [16, 16, 16, 16]; + case tcuTexture.ChannelType.SNORM_INT32: return [32, 32, 32, 32]; + case tcuTexture.ChannelType.UNORM_INT8: return [8, 8, 8, 8]; + case tcuTexture.ChannelType.UNORM_INT16: return [16, 16, 16, 16]; + case tcuTexture.ChannelType.UNORM_INT32: return [32, 32, 32, 32]; + case tcuTexture.ChannelType.UNORM_SHORT_565: return [5, 6, 5, 0]; + case tcuTexture.ChannelType.UNORM_SHORT_4444: return [4, 4, 4, 4]; + case tcuTexture.ChannelType.UNORM_SHORT_555: return [5, 5, 5, 0]; + case tcuTexture.ChannelType.UNORM_SHORT_5551: return [5, 5, 5, 1]; + case tcuTexture.ChannelType.UNORM_INT_101010: return [10, 10, 10, 0]; + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: return [10, 10, 10, 2]; + case tcuTexture.ChannelType.SIGNED_INT8: return [8, 8, 8, 8]; + case tcuTexture.ChannelType.SIGNED_INT16: return [16, 16, 16, 16]; + case tcuTexture.ChannelType.SIGNED_INT32: return [32, 32, 32, 32]; + case tcuTexture.ChannelType.UNSIGNED_INT8: return [8, 8, 8, 8]; + case tcuTexture.ChannelType.UNSIGNED_INT16: return [16, 16, 16, 16]; + case tcuTexture.ChannelType.UNSIGNED_INT32: return [32, 32, 32, 32]; + case tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV: return [10, 10, 10, 2]; + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: return [24, 0, 0, 8]; + case tcuTexture.ChannelType.HALF_FLOAT: return [16, 16, 16, 16]; + case tcuTexture.ChannelType.FLOAT: return [32, 32, 32, 32]; + case tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV: return [11, 11, 10, 0]; + case tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV: return [9, 9, 9, 0]; + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: return [32, 0, 0, 8]; + default: + DE_ASSERT(false); + return [0, 0, 0, 0]; + } +}; + +/** tcuTextureUtil.getTextureFormatBitDepth + * @param {tcuTexture.TextureFormat} format + * @return {Array<number>} + */ +tcuTextureUtil.getTextureFormatBitDepth = function(format) { + + /** @type {Array<number>} */ var chnBits = tcuTextureUtil.getChannelBitDepth(format.type); // IVec4 + /** @type {Array<boolean>} */ var chnMask = [false, false, false, false]; // BVec4 + /** @type {Array<number>} */ var chnSwz = [0, 1, 2, 3]; // IVec4 + + switch (format.order) { + case tcuTexture.ChannelOrder.R: chnMask = [true, false, false, false]; break; + case tcuTexture.ChannelOrder.A: chnMask = [false, false, false, true]; break; + case tcuTexture.ChannelOrder.RA: chnMask = [true, false, false, true]; break; + case tcuTexture.ChannelOrder.L: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.I: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.LA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.RG: chnMask = [true, true, false, false]; break; + case tcuTexture.ChannelOrder.RGB: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.RGBA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.BGRA: chnMask = [true, true, true, true]; chnSwz = [2, 1, 0, 3]; break; + case tcuTexture.ChannelOrder.ARGB: chnMask = [true, true, true, true]; chnSwz = [1, 2, 3, 0]; break; + case tcuTexture.ChannelOrder.sRGB: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.sRGBA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.D: chnMask = [true, false, false, false]; break; + case tcuTexture.ChannelOrder.DS: chnMask = [true, false, false, true]; break; + case tcuTexture.ChannelOrder.S: chnMask = [false, false, false, true]; break; + default: + DE_ASSERT(false); + } + + return tcuTextureUtil.select(deMath.swizzle(chnBits, [chnSwz[0], chnSwz[1], chnSwz[2], chnSwz[3]]), [0, 0, 0, 0], chnMask); + +}; + +/** tcuTextureUtil.fillWithGrid + * @const @param {tcuTexture.PixelBufferAccess} access + * @param {number} cellSize + * @param {Array<number>} colorA + * @param {Array<number>} colorB + */ +tcuTextureUtil.fillWithGrid = function(access, cellSize, colorA, colorB) { + if (access.getHeight() == 1 && access.getDepth() == 1) + tcuTextureUtil.fillWithGrid1D(access, cellSize, colorA, colorB); + else if (access.getDepth() == 1) + tcuTextureUtil.fillWithGrid2D(access, cellSize, colorA, colorB); + else + tcuTextureUtil.fillWithGrid3D(access, cellSize, colorA, colorB); +}; + +/** tcuTextureUtil.fillWithGrid1D + * @const @param {tcuTexture.PixelBufferAccess} access + * @param {number} cellSize + * @param {Array<number>} colorA + * @param {Array<number>} colorB + */ +tcuTextureUtil.fillWithGrid1D = function(access, cellSize, colorA, colorB) { + for (var x = 0; x < access.getWidth(); x++) { + var mx = Math.floor(x / cellSize) % 2; + + if (mx) + access.setPixel(colorB, x, 0); + else + access.setPixel(colorA, x, 0); + } +}; + +/** tcuTextureUtil.fillWithGrid2D + * @const @param {tcuTexture.PixelBufferAccess} access + * @param {number} cellSize + * @param {Array<number>} colorA + * @param {Array<number>} colorB + */ +tcuTextureUtil.fillWithGrid2D = function(access, cellSize, colorA, colorB) { + for (var y = 0; y < access.getHeight(); y++) + for (var x = 0; x < access.getWidth(); x++) { + var mx = Math.floor(x / cellSize) % 2; + var my = Math.floor(y / cellSize) % 2; + + if (mx ^ my) + access.setPixel(colorB, x, y); + else + access.setPixel(colorA, x, y); + } +}; + +/** tcuTextureUtil.fillWithGrid3D + * @const @param {tcuTexture.PixelBufferAccess} access + * @param {number} cellSize + * @param {Array<number>} colorA + * @param {Array<number>} colorB + */ +tcuTextureUtil.fillWithGrid3D = function(access, cellSize, colorA, colorB) { + for (var z = 0; z < access.getDepth(); z++) + for (var y = 0; y < access.getHeight(); y++) + for (var x = 0; x < access.getWidth(); x++) { + var mx = Math.floor(x / cellSize) % 2; + var my = Math.floor(y / cellSize) % 2; + var mz = Math.floor(z / cellSize) % 2; + + if (mx ^ my ^ mz) + access.setPixel(colorB, x, y, z); + else + access.setPixel(colorA, x, y, z); + } +}; + +/** + * @const @param {tcuTexture.TextureFormat} format + * @return {Array<number>} + */ +tcuTextureUtil.getTextureFormatMantissaBitDepth = function(format) { + /** @type {Array<number>} */ var chnBits = tcuTextureUtil.getChannelMantissaBitDepth(format.type); + /** @type {Array<boolean>} */ var chnMask = [false, false, false, false]; + /** @type {Array<number>} */ var chnSwz = [0, 1, 2, 3]; + + switch (format.order) { + case tcuTexture.ChannelOrder.R: chnMask = [true, false, false, false]; break; + case tcuTexture.ChannelOrder.A: chnMask = [false, false, false, true]; break; + case tcuTexture.ChannelOrder.RA: chnMask = [true, false, false, true]; break; + case tcuTexture.ChannelOrder.L: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.I: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.LA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.RG: chnMask = [true, true, false, false]; break; + case tcuTexture.ChannelOrder.RGB: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.RGBA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.BGRA: chnMask = [true, true, true, true]; chnSwz = [2, 1, 0, 3]; break; + case tcuTexture.ChannelOrder.ARGB: chnMask = [true, true, true, true]; chnSwz = [1, 2, 3, 0]; break; + case tcuTexture.ChannelOrder.sRGB: chnMask = [true, true, true, false]; break; + case tcuTexture.ChannelOrder.sRGBA: chnMask = [true, true, true, true]; break; + case tcuTexture.ChannelOrder.D: chnMask = [true, false, false, false]; break; + case tcuTexture.ChannelOrder.DS: chnMask = [true, false, false, true]; break; + case tcuTexture.ChannelOrder.S: chnMask = [false, false, false, true]; break; + default: + DE_ASSERT(false); + } + return tcuTextureUtil.select(deMath.swizzle(chnBits, [chnSwz[0], chnSwz[1], chnSwz[2], chnSwz[3]]), [0, 0, 0, 0], chnMask); +}; + +/** + * @param {?tcuTexture.ChannelType} channelType + * @return {Array<number>} + */ +tcuTextureUtil.getChannelMantissaBitDepth = function(channelType) { + switch (channelType) { + case tcuTexture.ChannelType.SNORM_INT8: + case tcuTexture.ChannelType.SNORM_INT16: + case tcuTexture.ChannelType.SNORM_INT32: + case tcuTexture.ChannelType.UNORM_INT8: + case tcuTexture.ChannelType.UNORM_INT16: + case tcuTexture.ChannelType.UNORM_INT32: + case tcuTexture.ChannelType.UNORM_SHORT_565: + case tcuTexture.ChannelType.UNORM_SHORT_4444: + case tcuTexture.ChannelType.UNORM_SHORT_555: + case tcuTexture.ChannelType.UNORM_SHORT_5551: + case tcuTexture.ChannelType.UNORM_INT_101010: + case tcuTexture.ChannelType.UNORM_INT_1010102_REV: + case tcuTexture.ChannelType.SIGNED_INT8: + case tcuTexture.ChannelType.SIGNED_INT16: + case tcuTexture.ChannelType.SIGNED_INT32: + case tcuTexture.ChannelType.UNSIGNED_INT8: + case tcuTexture.ChannelType.UNSIGNED_INT16: + case tcuTexture.ChannelType.UNSIGNED_INT32: + case tcuTexture.ChannelType.UNSIGNED_INT_1010102_REV: + case tcuTexture.ChannelType.UNSIGNED_INT_24_8: + case tcuTexture.ChannelType.UNSIGNED_INT_999_E5_REV: + return tcuTextureUtil.getChannelBitDepth(channelType); + case tcuTexture.ChannelType.HALF_FLOAT: return [10, 10, 10, 10]; + case tcuTexture.ChannelType.FLOAT: return [23, 23, 23, 23]; + case tcuTexture.ChannelType.UNSIGNED_INT_11F_11F_10F_REV: return [6, 6, 5, 0]; + case tcuTexture.ChannelType.FLOAT_UNSIGNED_INT_24_8_REV: return [23, 0, 0, 8]; + default: + throw new Error('Invalid channelType: ' + channelType); + } +}; + +/** + * @param {tcuTexture.PixelBufferAccess} dst + * @param {tcuTexture.ConstPixelBufferAccess} src + */ +tcuTextureUtil.copy = function(dst, src) { + var width = dst.getWidth(); + var height = dst.getHeight(); + var depth = dst.getDepth(); + + DE_ASSERT(src.getWidth() == width && src.getHeight() == height && src.getDepth() == depth); + + if (src.getFormat().isEqual(dst.getFormat())) { + var srcData = src.getDataPtr(); + var dstData = dst.getDataPtr(); + + if (srcData.length == dstData.length) { + dstData.set(srcData); + return; + } + } + var srcClass = tcuTexture.getTextureChannelClass(src.getFormat().type); + var dstClass = tcuTexture.getTextureChannelClass(dst.getFormat().type); + var srcIsInt = srcClass == tcuTexture.TextureChannelClass.SIGNED_INTEGER || srcClass == tcuTexture.TextureChannelClass.UNSIGNED_INTEGER; + var dstIsInt = dstClass == tcuTexture.TextureChannelClass.SIGNED_INTEGER || dstClass == tcuTexture.TextureChannelClass.UNSIGNED_INTEGER; + + if (srcIsInt && dstIsInt) { + for (var z = 0; z < depth; z++) + for (var y = 0; y < height; y++) + for (var x = 0; x < width; x++) + dst.setPixelInt(src.getPixelInt(x, y, z), x, y, z); + } else { + for (var z = 0; z < depth; z++) + for (var y = 0; y < height; y++) + for (var x = 0; x < width; x++) + dst.setPixel(src.getPixel(x, y, z), x, y, z); + } +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} access + */ +tcuTextureUtil.estimatePixelValueRange = function(access) { + var format = access.getFormat(); + + switch (format.type) { + case tcuTexture.ChannelType.UNORM_INT8: + case tcuTexture.ChannelType.UNORM_INT16: + // Normalized unsigned formats. + return [ + [0, 0, 0, 0], + [1, 1, 1, 1] + ]; + + case tcuTexture.ChannelType.SNORM_INT8: + case tcuTexture.ChannelType.SNORM_INT16: + // Normalized signed formats. + return [ + [-1, -1, -1, -1], + [1, 1, 1, 1] + ]; + + default: + // \note Samples every 4/8th pixel. + var minVal = [Infinity, Infinity, Infinity, Infinity]; + var maxVal = [-Infinity, -Infinity, -Infinity, -Infinity]; + + for (var z = 0; z < access.getDepth(); z += 2) { + for (var y = 0; y < access.getHeight(); y += 2) { + for (var x = 0; x < access.getWidth(); x += 2) { + var p = access.getPixel(x, y, z); + + minVal[0] = Math.min(minVal[0], p[0]); + minVal[1] = Math.min(minVal[1], p[1]); + minVal[2] = Math.min(minVal[2], p[2]); + minVal[3] = Math.min(minVal[3], p[3]); + + maxVal[0] = Math.max(maxVal[0], p[0]); + maxVal[1] = Math.max(maxVal[1], p[1]); + maxVal[2] = Math.max(maxVal[2], p[2]); + maxVal[3] = Math.max(maxVal[3], p[3]); + } + } + } + return [minVal, maxVal]; + } +}; + +/** + * @param {tcuTexture.ConstPixelBufferAccess} access + * @return {{scale: Array<number>, bias: Array<number>}} + */ +tcuTextureUtil.computePixelScaleBias = function(access) { + var limits = tcuTextureUtil.estimatePixelValueRange(access); + var minVal = limits[0]; + var maxVal = limits[1]; + + var scale = [1, 1, 1, 1]; + var bias = [0, 0, 0, 0]; + + var eps = 0.0001; + + for (var c = 0; c < 4; c++) { + if (maxVal[c] - minVal[c] < eps) { + scale[c] = (maxVal[c] < eps) ? 1 : (1 / maxVal[c]); + bias[c] = (c == 3) ? (1 - maxVal[c] * scale[c]) : (0 - minVal[c] * scale[c]); + } else { + scale[c] = 1 / (maxVal[c] - minVal[c]); + bias[c] = 0 - minVal[c] * scale[c]; + } + } + + return { + scale: scale, + bias: bias + }; +}; + +}); |