texture_ok.spec.ts (5464B)
1 export const description = ` 2 Test for texture_ok utils. 3 `; 4 5 import { makeTestGroup } from '../common/framework/test_group.js'; 6 import { typedArrayFromParam, typedArrayParam } from '../common/util/util.js'; 7 import { TexelView } from '../webgpu/util/texture/texel_view.js'; 8 import { findFailedPixels } from '../webgpu/util/texture/texture_ok.js'; 9 10 import { UnitTest } from './unit_test.js'; 11 12 class F extends UnitTest { 13 test(act: string, exp: string): void { 14 this.expect(act === exp, 'got: ' + act.replace('\n', '⏎')); 15 } 16 } 17 18 export const g = makeTestGroup(F); 19 g.test('findFailedPixels') 20 .desc( 21 ` 22 Test findFailedPixels passes what is expected to pass and fails what is expected 23 to fail. For example NaN === NaN should be true in a texture that allows NaN. 24 2 different representations of the same rgb9e5ufloat should compare as equal. 25 etc... 26 ` 27 ) 28 .params(u => 29 u.combineWithParams([ 30 // Sanity Check 31 { 32 format: 'rgba8unorm', 33 actual: typedArrayParam('Uint8Array', [0x00, 0x40, 0x80, 0xff]), 34 expected: typedArrayParam('Uint8Array', [0x00, 0x40, 0x80, 0xff]), 35 isSame: true, 36 }, 37 // Slightly different values 38 { 39 format: 'rgba8unorm', 40 actual: typedArrayParam('Uint8Array', [0x00, 0x40, 0x80, 0xff]), 41 expected: typedArrayParam('Uint8Array', [0x00, 0x40, 0x81, 0xff]), 42 isSame: false, 43 }, 44 // Different representations of the same value 45 { 46 format: 'rgb9e5ufloat', 47 actual: typedArrayParam('Uint8Array', [0x78, 0x56, 0x34, 0x12]), 48 expected: typedArrayParam('Uint8Array', [0xf0, 0xac, 0x68, 0x0c]), 49 isSame: true, 50 }, 51 // Slightly different values 52 { 53 format: 'rgb9e5ufloat', 54 actual: typedArrayParam('Uint8Array', [0x78, 0x56, 0x34, 0x12]), 55 expected: typedArrayParam('Uint8Array', [0xf1, 0xac, 0x68, 0x0c]), 56 isSame: false, 57 }, 58 // Test NaN === NaN 59 { 60 format: 'r32float', 61 actual: typedArrayParam('Float32Array', [parseFloat('abc')]), 62 expected: typedArrayParam('Float32Array', [parseFloat('def')]), 63 isSame: true, 64 }, 65 // Sanity Check 66 { 67 format: 'r32float', 68 actual: typedArrayParam('Float32Array', [1.23]), 69 expected: typedArrayParam('Float32Array', [1.23]), 70 isSame: true, 71 }, 72 // Slightly different values. 73 { 74 format: 'r32float', 75 actual: typedArrayParam('Uint32Array', [0x3f9d70a4]), 76 expected: typedArrayParam('Uint32Array', [0x3f9d70a5]), 77 isSame: false, 78 }, 79 // Slightly different 80 { 81 format: 'rg11b10ufloat', 82 actual: typedArrayParam('Uint32Array', [0x3ce]), 83 expected: typedArrayParam('Uint32Array', [0x3cf]), 84 isSame: false, 85 }, 86 // Positive.Infinity === Positive.Infinity (red) 87 { 88 format: 'rg11b10ufloat', 89 actual: typedArrayParam('Uint32Array', [0b11111000000]), 90 expected: typedArrayParam('Uint32Array', [0b11111000000]), 91 isSame: true, 92 }, 93 // Positive.Infinity === Positive.Infinity (green) 94 { 95 format: 'rg11b10ufloat', 96 actual: typedArrayParam('Uint32Array', [0b11111000000_00000000000]), 97 expected: typedArrayParam('Uint32Array', [0b11111000000_00000000000]), 98 isSame: true, 99 }, 100 // Positive.Infinity === Positive.Infinity (blue) 101 { 102 format: 'rg11b10ufloat', 103 actual: typedArrayParam('Uint32Array', [0b1111100000_00000000000_00000000000]), 104 expected: typedArrayParam('Uint32Array', [0b1111100000_00000000000_00000000000]), 105 isSame: true, 106 }, 107 // NaN === NaN (red) 108 { 109 format: 'rg11b10ufloat', 110 actual: typedArrayParam('Uint32Array', [0b11111000001]), 111 expected: typedArrayParam('Uint32Array', [0b11111000010]), 112 isSame: true, 113 }, 114 // NaN === NaN (green) 115 { 116 format: 'rg11b10ufloat', 117 actual: typedArrayParam('Uint32Array', [0b11111000100_00000000000]), 118 expected: typedArrayParam('Uint32Array', [0b11111001000_00000000000]), 119 isSame: true, 120 }, 121 // NaN === NaN (blue) 122 { 123 format: 'rg11b10ufloat', 124 actual: typedArrayParam('Uint32Array', [0b1111110000_00000000000_00000000000]), 125 expected: typedArrayParam('Uint32Array', [0b1111101000_00000000000_00000000000]), 126 isSame: true, 127 }, 128 ] as const) 129 ) 130 .fn(t => { 131 const { format, actual, expected, isSame } = t.params; 132 const actualData = new Uint8Array(typedArrayFromParam(actual).buffer); 133 const expectedData = new Uint8Array(typedArrayFromParam(expected).buffer); 134 135 const actTexelView = TexelView.fromTextureDataByReference(format, actualData, { 136 bytesPerRow: actualData.byteLength, 137 rowsPerImage: 1, 138 subrectOrigin: [0, 0, 0], 139 subrectSize: [1, 1, 1], 140 }); 141 const expTexelView = TexelView.fromTextureDataByReference(format, expectedData, { 142 bytesPerRow: expectedData.byteLength, 143 rowsPerImage: 1, 144 subrectOrigin: [0, 0, 0], 145 subrectSize: [1, 1, 1], 146 }); 147 148 const zero = { x: 0, y: 0, z: 0 }; 149 const failedPixelsMessage = findFailedPixels( 150 format, 151 zero, 152 { width: 1, height: 1, depthOrArrayLayers: 1 }, 153 { actTexelView, expTexelView }, 154 { 155 maxFractionalDiff: 0, 156 } 157 ); 158 159 t.expect(isSame === !failedPixelsMessage, failedPixelsMessage); 160 });