diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index d3c8e9f14875..53144304bb99 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -18,13 +18,17 @@ jobs: persist-credentials: false - uses: actions/setup-node@v3 with: - node-version: "16.x" + node-version: '16.x' - run: npm ci + - name: validating cache + run: npx grunt run:validate-cache - run: npm test - name: copy out-wpt to wpt tree run: | git clone --depth 2 https://github.com/web-platform-tests/wpt.git rsync -av out-wpt/ wpt/webgpu + - name: adding wpt lint ignore rule for *.bin + run: 'echo "TRAILING WHITESPACE, INDENT TABS, CR AT EOL: *.bin" >> wpt/lint.ignore' - name: test wpt lint run: ./wpt lint working-directory: ./wpt diff --git a/Gruntfile.js b/Gruntfile.js index 8c2402b5f2cd..2ab90a00f634 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -30,9 +30,14 @@ module.exports = function (grunt) { cmd: 'node', args: ['tools/validate', ...kAllSuites.map(s => 'src/' + s)], }, + 'generate-cache': { + // Note this generates files into the src/ directory (not the gen/ directory). + cmd: 'node', + args: ['tools/gen_cache', 'src/webgpu'], + }, 'validate-cache': { cmd: 'node', - args: ['tools/gen_cache', 'out', 'src/webgpu', '--validate'], + args: ['tools/gen_cache', 'src/webgpu', '--validate'], }, 'write-out-wpt-cts-html': { // Note this generates directly into the out-wpt/ directory rather than the gen/ directory. @@ -44,11 +49,6 @@ module.exports = function (grunt) { cmd: 'node', args: ['tools/gen_wpt_cts_html', 'tools/gen_wpt_cfg_chunked2sec.json'], }, - 'generate-cache': { - // Note this generates directly into the out/ directory rather than the gen/ directory. - cmd: 'node', - args: ['tools/gen_cache', 'out', 'src/webgpu'], - }, unittest: { cmd: 'node', args: ['tools/run_node', 'unittests:*'], @@ -115,6 +115,15 @@ module.exports = function (grunt) { '--copy-files' ], }, + 'copy-assets-node': { + cmd: 'node', + args: [ + 'node_modules/@babel/cli/bin/babel', + 'src/resources/', + '--out-dir=out-node/resources/', + '--copy-files' + ], + }, lint: { cmd: 'node', args: ['node_modules/eslint/bin/eslint', 'src/**/*.ts', '--max-warnings=0'], @@ -223,9 +232,10 @@ module.exports = function (grunt) { helpMessageTasks.push({ name, desc }); } - grunt.registerTask('generate-common', 'Generate files into gen/', [ + grunt.registerTask('generate-common', 'Generate files into gen/ and src/', [ 'run:generate-version', 'run:generate-listings', + 'run:generate-cache', ]); grunt.registerTask('build-standalone', 'Build out/ (no checks; run after generate-common)', [ 'run:build-out', @@ -240,6 +250,10 @@ module.exports = function (grunt) { 'concurrent:write-out-wpt-cts-html-all', 'run:autoformat-out-wpt', ]); + grunt.registerTask('build-node', 'Build out-node/ (no checks; run after generate-common)', [ + 'run:build-out-node', + 'run:copy-assets-node', + ]); grunt.registerTask('build-all', 'Build out*/ (no checks; run after generate-common)', [ 'concurrent:all-builds', 'build-done-message', @@ -268,6 +282,11 @@ module.exports = function (grunt) { 'build-wpt', 'build-done-message', ]); + registerTaskAndAddToHelp('node', 'Build node (out-node/) (no checks)', [ + 'generate-common', + 'build-node', + 'build-done-message', + ]); registerTaskAndAddToHelp('checks', 'Run all checks (and build tsdoc)', [ 'concurrent:all-checks', ]); diff --git a/src/common/framework/data_cache.ts b/src/common/framework/data_cache.ts index c1e3a889beb3..fea2bd203fae 100644 --- a/src/common/framework/data_cache.ts +++ b/src/common/framework/data_cache.ts @@ -187,11 +187,11 @@ export interface Cacheable { /** * serialize() encodes `data` to a binary representation so that it can be stored in a cache file. */ - serialize(data: Data): Uint8Array; + serialize(data: Data): Promise; /** * deserialize() is the inverse of serialize(), decoding the binary representation back to a Data * object. */ - deserialize(binary: Uint8Array): Data; + deserialize(binary: Uint8Array): Promise; } diff --git a/src/common/runtime/cmdline.ts b/src/common/runtime/cmdline.ts index 44a73fb38b34..860a06bfe9c4 100644 --- a/src/common/runtime/cmdline.ts +++ b/src/common/runtime/cmdline.ts @@ -3,6 +3,7 @@ import * as fs from 'fs'; import { dataCache } from '../framework/data_cache.js'; +import { getResourcePath, setBaseResourcePath } from '../framework/resources.js'; import { globalTestConfig } from '../framework/test_config.js'; import { DefaultTestFileLoader } from '../internal/file_loader.js'; import { prettyPrintLog } from '../internal/logging/log_message.js'; @@ -37,6 +38,12 @@ Options: return sys.exit(rc); } +if (!sys.existsSync('src/common/runtime/cmdline.ts')) { + console.log('Must be run from repository root'); + usage(1); +} +setBaseResourcePath('out-node/resources'); + // The interface that exposes creation of the GPU, and optional interface to code coverage. interface GPUProviderModule { // @returns a GPU with the given flags @@ -65,7 +72,6 @@ let printJSON = false; let quiet = false; let loadWebGPUExpectations: Promise | undefined = undefined; let gpuProviderModule: GPUProviderModule | undefined = undefined; -let dataPath: string | undefined = undefined; const queries: string[] = []; const gpuProviderFlags: string[] = []; @@ -84,8 +90,6 @@ for (let i = 0; i < sys.args.length; ++i) { listMode = 'unimplemented'; } else if (a === '--debug') { debug = true; - } else if (a === '--data') { - dataPath = sys.args[++i]; } else if (a === '--print-json') { printJSON = true; } else if (a === '--expectations') { @@ -132,21 +136,20 @@ Did you remember to build with code coverage instrumentation enabled?` } } -if (dataPath !== undefined) { - dataCache.setStore({ - load: (path: string) => { - return new Promise((resolve, reject) => { - fs.readFile(`${dataPath}/${path}`, (err, data) => { - if (err !== null) { - reject(err.message); - } else { - resolve(data); - } - }); +dataCache.setStore({ + load: (path: string) => { + return new Promise((resolve, reject) => { + fs.readFile(getResourcePath(`cache/${path}`), (err, data) => { + if (err !== null) { + reject(err.message); + } else { + resolve(data); + } }); - }, - }); -} + }); + }, +}); + if (verbose) { dataCache.setDebugLogger(console.log); } diff --git a/src/common/runtime/server.ts b/src/common/runtime/server.ts index 8310784e3a2c..ac1af61949dd 100644 --- a/src/common/runtime/server.ts +++ b/src/common/runtime/server.ts @@ -5,6 +5,7 @@ import * as http from 'http'; import { AddressInfo } from 'net'; import { dataCache } from '../framework/data_cache.js'; +import { getResourcePath, setBaseResourcePath } from '../framework/resources.js'; import { globalTestConfig } from '../framework/test_config.js'; import { DefaultTestFileLoader } from '../internal/file_loader.js'; import { prettyPrintLog } from '../internal/logging/log_message.js'; @@ -25,7 +26,6 @@ Options: --colors Enable ANSI colors in output. --compat Run tests in compatibility mode. --coverage Add coverage data to each result. - --data Path to the data cache directory. --verbose Print result/log of every test as it runs. --gpu-provider Path to node module that provides the GPU implementation. --gpu-provider-flag Flag to set on the gpu-provider as = @@ -71,13 +71,13 @@ if (!sys.existsSync('src/common/runtime/cmdline.ts')) { console.log('Must be run from repository root'); usage(1); } +setBaseResourcePath('out-node/resources'); Colors.enabled = false; let emitCoverage = false; let verbose = false; let gpuProviderModule: GPUProviderModule | undefined = undefined; -let dataPath: string | undefined = undefined; const gpuProviderFlags: string[] = []; for (let i = 0; i < sys.args.length; ++i) { @@ -89,8 +89,6 @@ for (let i = 0; i < sys.args.length; ++i) { globalTestConfig.compatibility = true; } else if (a === '--coverage') { emitCoverage = true; - } else if (a === '--data') { - dataPath = sys.args[++i]; } else if (a === '--gpu-provider') { const modulePath = sys.args[++i]; gpuProviderModule = require(modulePath); @@ -130,21 +128,20 @@ Did you remember to build with code coverage instrumentation enabled?` } } -if (dataPath !== undefined) { - dataCache.setStore({ - load: (path: string) => { - return new Promise((resolve, reject) => { - fs.readFile(`${dataPath}/${path}`, (err, data) => { - if (err !== null) { - reject(err.message); - } else { - resolve(data); - } - }); +dataCache.setStore({ + load: (path: string) => { + return new Promise((resolve, reject) => { + fs.readFile(getResourcePath(`cache/${path}`), (err, data) => { + if (err !== null) { + reject(err.message); + } else { + resolve(data); + } }); - }, - }); -} + }); + }, +}); + if (verbose) { dataCache.setDebugLogger(console.log); } diff --git a/src/common/runtime/standalone.ts b/src/common/runtime/standalone.ts index 0376f92dda63..17500fff6748 100644 --- a/src/common/runtime/standalone.ts +++ b/src/common/runtime/standalone.ts @@ -2,7 +2,7 @@ /* eslint no-console: "off" */ import { dataCache } from '../framework/data_cache.js'; -import { setBaseResourcePath } from '../framework/resources.js'; +import { getResourcePath, setBaseResourcePath } from '../framework/resources.js'; import { globalTestConfig } from '../framework/test_config.js'; import { DefaultTestFileLoader } from '../internal/file_loader.js'; import { Logger } from '../internal/logging/logger.js'; @@ -80,7 +80,7 @@ if (powerPreference || compatibility) { dataCache.setStore({ load: async (path: string) => { - const response = await fetch(`data/${path}`); + const response = await fetch(getResourcePath(`cache/${path}`)); if (!response.ok) { return Promise.reject(response.statusText); } diff --git a/src/common/tools/gen_cache.ts b/src/common/tools/gen_cache.ts index ce0854aa2046..0ffbb373a0aa 100644 --- a/src/common/tools/gen_cache.ts +++ b/src/common/tools/gen_cache.ts @@ -3,32 +3,41 @@ import * as path from 'path'; import * as process from 'process'; import { Cacheable, dataCache, setIsBuildingDataCache } from '../framework/data_cache.js'; +import { crc32, toHexString } from '../util/crc32.js'; +import { parseImports } from '../util/parse_imports.js'; function usage(rc: number): void { - console.error(`Usage: tools/gen_cache [options] [OUT_DIR] [SUITE_DIRS...] + console.error(`Usage: tools/gen_cache [options] [SUITE_DIRS...] For each suite in SUITE_DIRS, pre-compute data that is expensive to generate -at runtime and store it under OUT_DIR. If the data file is found then the -DataCache will load this instead of building the expensive data at CTS runtime. +at runtime and store it under 'src/resources/cache'. If the data file is found +then the DataCache will load this instead of building the expensive data at CTS +runtime. +Note: Due to differences in gzip compression, different versions of node can +produce radically different binary cache files. gen_cache uses the hashes of the +source files to determine whether a cache file is 'up to date'. This is faster +and does not depend on the compressed output. Options: --help Print this message and exit. --list Print the list of output files without writing them. - --nth i/n Only process every file where (file_index % n == i) - --validate Check that cache should build (Tests for collisions). + --force Rebuild cache even if they're up to date + --validate Check the cache is up to date --verbose Print each action taken. `); process.exit(rc); } +// Where the cache is generated +const outDir = 'src/resources/cache'; + +let forceRebuild = false; let mode: 'emit' | 'list' | 'validate' = 'emit'; -let nth = { i: 0, n: 1 }; let verbose = false; const nonFlagsArgs: string[] = []; -for (let i = 0; i < process.argv.length; i++) { - const arg = process.argv[i]; +for (const arg of process.argv) { if (arg.startsWith('-')) { switch (arg) { case '--list': { @@ -39,6 +48,10 @@ for (let i = 0; i < process.argv.length; i++) { usage(0); break; } + case '--force': { + forceRebuild = true; + break; + } case '--verbose': { verbose = true; break; @@ -47,28 +60,6 @@ for (let i = 0; i < process.argv.length; i++) { mode = 'validate'; break; } - case '--nth': { - const err = () => { - console.error( - `--nth requires a value of the form 'i/n', where i and n are positive integers and i < n` - ); - process.exit(1); - }; - i++; - if (i >= process.argv.length) { - err(); - } - const value = process.argv[i]; - const parts = value.split('/'); - if (parts.length !== 2) { - err(); - } - nth = { i: parseInt(parts[0]), n: parseInt(parts[1]) }; - if (nth.i < 0 || nth.n < 1 || nth.i > nth.n) { - err(); - } - break; - } default: { console.log('unrecognized flag: ', arg); usage(1); @@ -79,12 +70,10 @@ for (let i = 0; i < process.argv.length; i++) { } } -if (nonFlagsArgs.length < 4) { +if (nonFlagsArgs.length < 3) { usage(0); } -const outRootDir = nonFlagsArgs[2]; - dataCache.setStore({ load: (path: string) => { return new Promise((resolve, reject) => { @@ -100,57 +89,108 @@ dataCache.setStore({ }); setIsBuildingDataCache(); -void (async () => { - for (const suiteDir of nonFlagsArgs.slice(3)) { - await build(suiteDir); +const specFileSuffix = __filename.endsWith('.ts') ? '.spec.ts' : '.spec.js'; + +/** + * @returns a list of all the files under 'dir' that has the given extension + * @param dir the directory to search + * @param ext the extension of the files to find + */ +function glob(dir: string, ext: string) { + const files: string[] = []; + for (const file of fs.readdirSync(dir)) { + const path = `${dir}/${file}`; + if (fs.statSync(path).isDirectory()) { + for (const child of glob(path, ext)) { + files.push(`${file}/${child}`); + } + } + if (path.endsWith(ext) && fs.statSync(path).isFile()) { + files.push(file); + } } -})(); + return files; +} -const specFileSuffix = __filename.endsWith('.ts') ? '.spec.ts' : '.spec.js'; +/** + * SourceHasher is a utility for producing a hash of a source .ts file and its imported source files. + */ +class SourceHasher { + /** + * @param path the source file path + * @returns a hash of the source file and all of its imported dependencies. + */ + public hashOf(path: string) { + this.u32Array[0] = this.hashFile(path); + return this.u32Array[0].toString(16); + } + + hashFile(path: string): number { + if (!fs.existsSync(path) && path.endsWith('.js')) { + path = path.substring(0, path.length - 2) + 'ts'; + } + + const cached = this.hashes.get(path); + if (cached !== undefined) { + return cached; + } + + this.hashes.set(path, 0); // Store a zero hash to handle cyclic imports + + const content = fs.readFileSync(path, { encoding: 'utf-8' }); + const normalized = content.replace('\r\n', '\n'); + let hash = crc32(normalized); + for (const importPath of parseImports(path, normalized)) { + const importHash = this.hashFile(importPath); + hash = this.hashCombine(hash, importHash); + } + + this.hashes.set(path, hash); + return hash; + } -async function crawlFilesRecursively(dir: string): Promise { - const subpathInfo = await Promise.all( - (await fs.promises.readdir(dir)).map(async d => { - const p = path.join(dir, d); - const stats = await fs.promises.stat(p); - return { - path: p, - isDirectory: stats.isDirectory(), - isFile: stats.isFile(), - }; - }) - ); - - const files = subpathInfo - .filter(i => i.isFile && i.path.endsWith(specFileSuffix)) - .map(i => i.path); - - return files.concat( - await subpathInfo - .filter(i => i.isDirectory) - .map(i => crawlFilesRecursively(i.path)) - .reduce(async (a, b) => (await a).concat(await b), Promise.resolve([])) - ); + /** Simple non-cryptographic hash combiner */ + hashCombine(a: number, b: number): number { + return crc32(`${toHexString(a)} ${toHexString(b)}`); + } + + private hashes = new Map(); + private u32Array = new Uint32Array(1); } +void (async () => { + const suiteDirs = nonFlagsArgs.slice(2); // skip + for (const suiteDir of suiteDirs) { + await build(suiteDir); + } +})(); + async function build(suiteDir: string) { if (!fs.existsSync(suiteDir)) { console.error(`Could not find ${suiteDir}`); process.exit(1); } - // Crawl files and convert paths to be POSIX-style, relative to suiteDir. - let filesToEnumerate = (await crawlFilesRecursively(suiteDir)).sort(); + // Load hashes.json + const fileHashJsonPath = `${outDir}/hashes.json`; + let fileHashes: Record = {}; + if (fs.existsSync(fileHashJsonPath)) { + const json = fs.readFileSync(fileHashJsonPath, { encoding: 'utf8' }); + fileHashes = JSON.parse(json); + } - // Filter out non-spec files - filesToEnumerate = filesToEnumerate.filter(f => f.endsWith(specFileSuffix)); + // Crawl files and convert paths to be POSIX-style, relative to suiteDir. + const filesToEnumerate = glob(suiteDir, specFileSuffix) + .map(p => `${suiteDir}/${p}`) + .sort(); + const fileHasher = new SourceHasher(); const cacheablePathToTS = new Map(); + const errors: Array = []; - let fileIndex = 0; for (const file of filesToEnumerate) { - const pathWithoutExtension = file.substring(0, file.length - specFileSuffix.length); - const mod = await import(`../../../${pathWithoutExtension}.spec.js`); + const pathWithoutExtension = file.substring(0, file.length - 3); + const mod = await import(`../../../${pathWithoutExtension}.js`); if (mod.d?.serialize !== undefined) { const cacheable = mod.d as Cacheable; @@ -158,41 +198,78 @@ async function build(suiteDir: string) { // Check for collisions const existing = cacheablePathToTS.get(cacheable.path); if (existing !== undefined) { - console.error( - `error: Cacheable '${cacheable.path}' is emitted by both: + errors.push( + `'${cacheable.path}' is emitted by both: '${existing}' and '${file}'` ); - process.exit(1); } cacheablePathToTS.set(cacheable.path, file); } - const outPath = `${outRootDir}/data/${cacheable.path}`; + const outPath = `${outDir}/${cacheable.path}`; + const fileHash = fileHasher.hashOf(file); - if (fileIndex++ % nth.n === nth.i) { - switch (mode) { - case 'emit': { + switch (mode) { + case 'emit': { + if (!forceRebuild && fileHashes[cacheable.path] === fileHash) { if (verbose) { - console.log(`building '${outPath}'`); + console.log(`'${outPath}' is up to date`); } - const data = await cacheable.build(); - const serialized = cacheable.serialize(data); - fs.mkdirSync(path.dirname(outPath), { recursive: true }); - fs.writeFileSync(outPath, serialized, 'binary'); - break; - } - case 'list': { - console.log(outPath); - break; + continue; } - case 'validate': { - // Only check currently performed is the collision detection above - break; + console.log(`building '${outPath}'`); + const data = await cacheable.build(); + const serialized = await cacheable.serialize(data); + fs.mkdirSync(path.dirname(outPath), { recursive: true }); + fs.writeFileSync(outPath, serialized, 'binary'); + fileHashes[cacheable.path] = fileHash; + break; + } + case 'list': { + console.log(outPath); + break; + } + case 'validate': { + if (fileHashes[cacheable.path] !== fileHash) { + errors.push( + `'${outPath}' needs rebuilding. Generate with 'npx grunt run:generate-cache'` + ); + } else if (verbose) { + console.log(`'${outPath}' is up to date`); } } } } } + + // Check that there aren't stale files in the cache directory + for (const file of glob(outDir, '.bin')) { + if (cacheablePathToTS.get(file) === undefined) { + switch (mode) { + case 'emit': + fs.rmSync(file); + break; + case 'validate': + errors.push( + `cache file '${outDir}/${file}' is no longer generated. Remove with 'npx grunt run:generate-cache'` + ); + break; + } + } + } + + // Update hashes.json + if (mode === 'emit') { + const json = JSON.stringify(fileHashes, undefined, ' '); + fs.writeFileSync(fileHashJsonPath, json, { encoding: 'utf8' }); + } + + if (errors.length > 0) { + for (const error of errors) { + console.error(error); + } + process.exit(1); + } } diff --git a/src/common/util/crc32.ts b/src/common/util/crc32.ts new file mode 100644 index 000000000000..5f74b4662efd --- /dev/null +++ b/src/common/util/crc32.ts @@ -0,0 +1,57 @@ +/// CRC32 immutable lookup table data. +const kCRC32LUT = [ + 0, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, + 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, + 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, + 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, + 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, + 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, + 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, + 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, + 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, + 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, + 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, + 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, + 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, + 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, + 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, + 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, + 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, + 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, + 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, + 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, + 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, + 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, + 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, + 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, + 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, + 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, + 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, + 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, + 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, + 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, + 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, + 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d, +]; + +/** + * @param str the input string + * @returns the CRC32 of the input string + * @see https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-32_algorithm + */ +export function crc32(str: string): number { + const utf8 = new TextEncoder().encode(str); + const u32 = new Uint32Array(1); + + u32[0] = 0xffffffff; + for (const c of utf8) { + u32[0] = (u32[0] >>> 8) ^ kCRC32LUT[(u32[0] & 0xff) ^ c]; + } + u32[0] = u32[0] ^ 0xffffffff; + return u32[0]; +} + +/** @returns the input number has a 8-character hex string */ +export function toHexString(number: number): string { + return ('00000000' + number.toString(16)).slice(-8); +} diff --git a/src/common/util/parse_imports.ts b/src/common/util/parse_imports.ts new file mode 100644 index 000000000000..4b5604b8972b --- /dev/null +++ b/src/common/util/parse_imports.ts @@ -0,0 +1,36 @@ +/** + * Parses all the paths of the typescript `import` statements from content + * @param path the current path of the file + * @param content the file content + * @returns the list of import paths + */ +export function parseImports(path: string, content: string): string[] { + const out: string[] = []; + const importRE = /^import\s[^'"]*(['"])([./\w]*)(\1);/gm; + let importMatch: RegExpMatchArray | null; + while ((importMatch = importRE.exec(content))) { + const importPath = importMatch[2].replace(`'`, '').replace(`"`, ''); + out.push(joinPath(path, importPath)); + } + return out; +} + +function joinPath(a: string, b: string): string { + const aParts = a.split('/'); + const bParts = b.split('/'); + aParts.pop(); // remove file + let bStart = 0; + while (aParts.length > 0) { + switch (bParts[bStart]) { + case '.': + bStart++; + continue; + case '..': + aParts.pop(); + bStart++; + continue; + } + break; + } + return [...aParts, ...bParts.slice(bStart)].join('/'); +} diff --git a/src/resources/cache/hashes.json b/src/resources/cache/hashes.json new file mode 100644 index 000000000000..aac63261c6fe --- /dev/null +++ b/src/resources/cache/hashes.json @@ -0,0 +1,106 @@ +{ + "webgpu/shader/execution/binary/af_addition.bin": "a56b8e5b", + "webgpu/shader/execution/binary/af_logical.bin": "ae4d0245", + "webgpu/shader/execution/binary/af_division.bin": "272d8801", + "webgpu/shader/execution/binary/af_matrix_addition.bin": "808bf815", + "webgpu/shader/execution/binary/af_matrix_subtraction.bin": "1d2c3ef5", + "webgpu/shader/execution/binary/af_multiplication.bin": "51bcd4fa", + "webgpu/shader/execution/binary/af_remainder.bin": "4cf24b9a", + "webgpu/shader/execution/binary/af_subtraction.bin": "320f8bec", + "webgpu/shader/execution/binary/f16_addition.bin": "2158527", + "webgpu/shader/execution/binary/f16_logical.bin": "8b97670c", + "webgpu/shader/execution/binary/f16_division.bin": "ab5a1978", + "webgpu/shader/execution/binary/f16_matrix_addition.bin": "281dfb13", + "webgpu/shader/execution/binary/f16_matrix_matrix_multiplication.bin": "afb1ba5", + "webgpu/shader/execution/binary/f16_matrix_scalar_multiplication.bin": "9f0b30b8", + "webgpu/shader/execution/binary/f16_matrix_subtraction.bin": "a0d7171c", + "webgpu/shader/execution/binary/f16_matrix_vector_multiplication.bin": "99abdf10", + "webgpu/shader/execution/binary/f16_multiplication.bin": "c9b3a1f7", + "webgpu/shader/execution/binary/f16_remainder.bin": "37d766fb", + "webgpu/shader/execution/binary/f16_subtraction.bin": "569714bb", + "webgpu/shader/execution/binary/f32_addition.bin": "4036bb6c", + "webgpu/shader/execution/binary/f32_logical.bin": "c761067f", + "webgpu/shader/execution/binary/f32_division.bin": "cd7ff99f", + "webgpu/shader/execution/binary/f32_matrix_addition.bin": "9dca27e2", + "webgpu/shader/execution/binary/f32_matrix_matrix_multiplication.bin": "6f1a3cee", + "webgpu/shader/execution/binary/f32_matrix_scalar_multiplication.bin": "faa9f9b6", + "webgpu/shader/execution/binary/f32_matrix_subtraction.bin": "2d8bce4f", + "webgpu/shader/execution/binary/f32_matrix_vector_multiplication.bin": "16935a68", + "webgpu/shader/execution/binary/f32_multiplication.bin": "3b4bd117", + "webgpu/shader/execution/binary/f32_remainder.bin": "323866c9", + "webgpu/shader/execution/binary/f32_subtraction.bin": "e7850558", + "webgpu/shader/execution/binary/i32_arithmetic.bin": "d385729b", + "webgpu/shader/execution/binary/i32_comparison.bin": "a212a9e5", + "webgpu/shader/execution/binary/u32_arithmetic.bin": "d9387c9a", + "webgpu/shader/execution/binary/u32_comparison.bin": "cb609171", + "webgpu/shader/execution/abs.bin": "49f7faf5", + "webgpu/shader/execution/acos.bin": "866f09e1", + "webgpu/shader/execution/acosh.bin": "4560e02a", + "webgpu/shader/execution/asin.bin": "cc81b07b", + "webgpu/shader/execution/asinh.bin": "9725f077", + "webgpu/shader/execution/atan.bin": "609df34a", + "webgpu/shader/execution/atan2.bin": "fe507972", + "webgpu/shader/execution/atanh.bin": "57f6b8a3", + "webgpu/shader/execution/bitcast.bin": "1f24ff3a", + "webgpu/shader/execution/ceil.bin": "4ed0c45b", + "webgpu/shader/execution/clamp.bin": "ad417174", + "webgpu/shader/execution/cos.bin": "96beac42", + "webgpu/shader/execution/cosh.bin": "f804dcd", + "webgpu/shader/execution/cross.bin": "afc0eb76", + "webgpu/shader/execution/degrees.bin": "91f06824", + "webgpu/shader/execution/determinant.bin": "eceba086", + "webgpu/shader/execution/distance.bin": "35e14924", + "webgpu/shader/execution/dot.bin": "57b17ace", + "webgpu/shader/execution/exp.bin": "1df2ddb6", + "webgpu/shader/execution/exp2.bin": "1bd38dd", + "webgpu/shader/execution/faceForward.bin": "6f4b9a73", + "webgpu/shader/execution/floor.bin": "f8c9993f", + "webgpu/shader/execution/fma.bin": "8f076f30", + "webgpu/shader/execution/fract.bin": "2b18f48f", + "webgpu/shader/execution/frexp.bin": "93592d00", + "webgpu/shader/execution/inverseSqrt.bin": "5cef7c26", + "webgpu/shader/execution/ldexp.bin": "71954769", + "webgpu/shader/execution/length.bin": "f0871818", + "webgpu/shader/execution/log.bin": "365b395d", + "webgpu/shader/execution/log2.bin": "6d844fc7", + "webgpu/shader/execution/max.bin": "850c61e6", + "webgpu/shader/execution/min.bin": "dd9dedde", + "webgpu/shader/execution/mix.bin": "25b0bc44", + "webgpu/shader/execution/modf.bin": "3995f421", + "webgpu/shader/execution/normalize.bin": "40d115ec", + "webgpu/shader/execution/pack2x16float.bin": "acd2d4cc", + "webgpu/shader/execution/pow.bin": "ba4aa9d5", + "webgpu/shader/execution/quantizeToF16.bin": "5eaa050", + "webgpu/shader/execution/radians.bin": "ad3b9408", + "webgpu/shader/execution/reflect.bin": "371ab49a", + "webgpu/shader/execution/refract.bin": "127389a3", + "webgpu/shader/execution/round.bin": "cb947f1d", + "webgpu/shader/execution/saturate.bin": "84a77008", + "webgpu/shader/execution/sign.bin": "694483cb", + "webgpu/shader/execution/sin.bin": "588f5708", + "webgpu/shader/execution/sinh.bin": "55e2f80a", + "webgpu/shader/execution/smoothstep.bin": "f532f7ee", + "webgpu/shader/execution/sqrt.bin": "a12f423b", + "webgpu/shader/execution/step.bin": "56327d5", + "webgpu/shader/execution/tan.bin": "8d9ea9af", + "webgpu/shader/execution/tanh.bin": "96d01d58", + "webgpu/shader/execution/transpose.bin": "22e1a807", + "webgpu/shader/execution/trunc.bin": "eb14c04d", + "webgpu/shader/execution/unpack2x16float.bin": "cc1e0067", + "webgpu/shader/execution/unpack2x16snorm.bin": "85f1c3c5", + "webgpu/shader/execution/unpack2x16unorm.bin": "2f956a85", + "webgpu/shader/execution/unpack4x8snorm.bin": "404052b4", + "webgpu/shader/execution/unpack4x8unorm.bin": "b7bdf499", + "webgpu/shader/execution/unary/af_arithmetic.bin": "24315037", + "webgpu/shader/execution/unary/af_assignment.bin": "405dcc78", + "webgpu/shader/execution/unary/bool_conversion.bin": "8916fbb", + "webgpu/shader/execution/unary/f16_arithmetic.bin": "d59a9bec", + "webgpu/shader/execution/unary/f16_conversion.bin": "70096394", + "webgpu/shader/execution/unary/f32_arithmetic.bin": "b9430cfe", + "webgpu/shader/execution/unary/f32_conversion.bin": "c03cad58", + "webgpu/shader/execution/unary/i32_arithmetic.bin": "2bba8fe4", + "webgpu/shader/execution/unary/i32_complement.bin": "caa954b2", + "webgpu/shader/execution/unary/i32_conversion.bin": "22a36f9e", + "webgpu/shader/execution/unary/u32_complement.bin": "fabf96c0", + "webgpu/shader/execution/unary/u32_conversion.bin": "ec57b500" +} \ No newline at end of file diff --git a/src/resources/cache/webgpu/shader/execution/abs.bin b/src/resources/cache/webgpu/shader/execution/abs.bin new file mode 100644 index 000000000000..17e2229752b1 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/abs.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/acos.bin b/src/resources/cache/webgpu/shader/execution/acos.bin new file mode 100644 index 000000000000..34406ee6ff36 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/acos.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/acosh.bin b/src/resources/cache/webgpu/shader/execution/acosh.bin new file mode 100644 index 000000000000..eceb0fcd69a1 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/acosh.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/asin.bin b/src/resources/cache/webgpu/shader/execution/asin.bin new file mode 100644 index 000000000000..718a766f80a2 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/asin.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/asinh.bin b/src/resources/cache/webgpu/shader/execution/asinh.bin new file mode 100644 index 000000000000..620167622d34 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/asinh.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/atan.bin b/src/resources/cache/webgpu/shader/execution/atan.bin new file mode 100644 index 000000000000..ca9b4104fa4c Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/atan.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/atan2.bin b/src/resources/cache/webgpu/shader/execution/atan2.bin new file mode 100644 index 000000000000..0fca2d1ad0f6 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/atan2.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/atanh.bin b/src/resources/cache/webgpu/shader/execution/atanh.bin new file mode 100644 index 000000000000..e5a1e37a594b Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/atanh.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/af_addition.bin b/src/resources/cache/webgpu/shader/execution/binary/af_addition.bin new file mode 100644 index 000000000000..703707f13979 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/af_addition.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/af_division.bin b/src/resources/cache/webgpu/shader/execution/binary/af_division.bin new file mode 100644 index 000000000000..d7967e87879c Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/af_division.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/af_logical.bin b/src/resources/cache/webgpu/shader/execution/binary/af_logical.bin new file mode 100644 index 000000000000..da7a412aa503 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/af_logical.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/af_matrix_addition.bin b/src/resources/cache/webgpu/shader/execution/binary/af_matrix_addition.bin new file mode 100644 index 000000000000..3c666bdf7b50 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/af_matrix_addition.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/af_matrix_subtraction.bin b/src/resources/cache/webgpu/shader/execution/binary/af_matrix_subtraction.bin new file mode 100644 index 000000000000..cb19969f9202 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/af_matrix_subtraction.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/af_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/af_multiplication.bin new file mode 100644 index 000000000000..35474be82ff2 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/af_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/af_remainder.bin b/src/resources/cache/webgpu/shader/execution/binary/af_remainder.bin new file mode 100644 index 000000000000..3df87ee1a7b0 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/af_remainder.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/af_subtraction.bin b/src/resources/cache/webgpu/shader/execution/binary/af_subtraction.bin new file mode 100644 index 000000000000..61e6da6a4d83 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/af_subtraction.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_addition.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_addition.bin new file mode 100644 index 000000000000..5542654a2240 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_addition.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_division.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_division.bin new file mode 100644 index 000000000000..cb1a05e05437 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_division.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_logical.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_logical.bin new file mode 100644 index 000000000000..5659be99762d Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_logical.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_addition.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_addition.bin new file mode 100644 index 000000000000..2876edf54f52 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_addition.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_matrix_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_matrix_multiplication.bin new file mode 100644 index 000000000000..81342f521bf8 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_matrix_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_scalar_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_scalar_multiplication.bin new file mode 100644 index 000000000000..dc2d43e235d5 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_scalar_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_subtraction.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_subtraction.bin new file mode 100644 index 000000000000..a2d8ac6c072a Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_subtraction.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_vector_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_vector_multiplication.bin new file mode 100644 index 000000000000..2b8bbc058c64 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_matrix_vector_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_multiplication.bin new file mode 100644 index 000000000000..b834ed8acf2c Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_remainder.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_remainder.bin new file mode 100644 index 000000000000..ef3e80fb2b80 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_remainder.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f16_subtraction.bin b/src/resources/cache/webgpu/shader/execution/binary/f16_subtraction.bin new file mode 100644 index 000000000000..10be022dbb52 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f16_subtraction.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_addition.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_addition.bin new file mode 100644 index 000000000000..e55501474584 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_addition.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_division.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_division.bin new file mode 100644 index 000000000000..54333f2a6dfd Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_division.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_logical.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_logical.bin new file mode 100644 index 000000000000..5ed0ed1e0576 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_logical.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_addition.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_addition.bin new file mode 100644 index 000000000000..ba087465e09b Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_addition.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_matrix_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_matrix_multiplication.bin new file mode 100644 index 000000000000..474e82fb5952 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_matrix_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_scalar_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_scalar_multiplication.bin new file mode 100644 index 000000000000..90849e89f4a7 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_scalar_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_subtraction.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_subtraction.bin new file mode 100644 index 000000000000..d548944f510e Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_subtraction.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_vector_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_vector_multiplication.bin new file mode 100644 index 000000000000..5cdc4d28a110 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_matrix_vector_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_multiplication.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_multiplication.bin new file mode 100644 index 000000000000..6f5c7a1a7a19 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_multiplication.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_remainder.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_remainder.bin new file mode 100644 index 000000000000..d8e19a526860 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_remainder.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/f32_subtraction.bin b/src/resources/cache/webgpu/shader/execution/binary/f32_subtraction.bin new file mode 100644 index 000000000000..ea54e54398f4 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/f32_subtraction.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/i32_arithmetic.bin b/src/resources/cache/webgpu/shader/execution/binary/i32_arithmetic.bin new file mode 100644 index 000000000000..b1b925a9cb7a Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/i32_arithmetic.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/i32_comparison.bin b/src/resources/cache/webgpu/shader/execution/binary/i32_comparison.bin new file mode 100644 index 000000000000..c14b31a89e5d Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/i32_comparison.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/u32_arithmetic.bin b/src/resources/cache/webgpu/shader/execution/binary/u32_arithmetic.bin new file mode 100644 index 000000000000..8cdce057ddb8 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/u32_arithmetic.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/binary/u32_comparison.bin b/src/resources/cache/webgpu/shader/execution/binary/u32_comparison.bin new file mode 100644 index 000000000000..26a6dd6b79c7 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/binary/u32_comparison.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/bitcast.bin b/src/resources/cache/webgpu/shader/execution/bitcast.bin new file mode 100644 index 000000000000..1f171337d745 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/bitcast.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/ceil.bin b/src/resources/cache/webgpu/shader/execution/ceil.bin new file mode 100644 index 000000000000..68580a51846d Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/ceil.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/clamp.bin b/src/resources/cache/webgpu/shader/execution/clamp.bin new file mode 100644 index 000000000000..7f508d97c3be Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/clamp.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/cos.bin b/src/resources/cache/webgpu/shader/execution/cos.bin new file mode 100644 index 000000000000..c7b020f58125 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/cos.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/cosh.bin b/src/resources/cache/webgpu/shader/execution/cosh.bin new file mode 100644 index 000000000000..010adce0d93a Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/cosh.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/cross.bin b/src/resources/cache/webgpu/shader/execution/cross.bin new file mode 100644 index 000000000000..fd4959f2ba74 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/cross.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/degrees.bin b/src/resources/cache/webgpu/shader/execution/degrees.bin new file mode 100644 index 000000000000..d46206988166 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/degrees.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/determinant.bin b/src/resources/cache/webgpu/shader/execution/determinant.bin new file mode 100644 index 000000000000..f69fc55e483b Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/determinant.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/distance.bin b/src/resources/cache/webgpu/shader/execution/distance.bin new file mode 100644 index 000000000000..74edf7c2876c Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/distance.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/dot.bin b/src/resources/cache/webgpu/shader/execution/dot.bin new file mode 100644 index 000000000000..231b144d953a Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/dot.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/exp.bin b/src/resources/cache/webgpu/shader/execution/exp.bin new file mode 100644 index 000000000000..5e5dbab586c8 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/exp.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/exp2.bin b/src/resources/cache/webgpu/shader/execution/exp2.bin new file mode 100644 index 000000000000..7012bac15948 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/exp2.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/faceForward.bin b/src/resources/cache/webgpu/shader/execution/faceForward.bin new file mode 100644 index 000000000000..301b017b9cfb Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/faceForward.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/floor.bin b/src/resources/cache/webgpu/shader/execution/floor.bin new file mode 100644 index 000000000000..2228293b7547 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/floor.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/fma.bin b/src/resources/cache/webgpu/shader/execution/fma.bin new file mode 100644 index 000000000000..c73ee5a614ca Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/fma.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/fract.bin b/src/resources/cache/webgpu/shader/execution/fract.bin new file mode 100644 index 000000000000..3400c36d85a1 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/fract.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/frexp.bin b/src/resources/cache/webgpu/shader/execution/frexp.bin new file mode 100644 index 000000000000..2c5b0c0f2bf5 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/frexp.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/inverseSqrt.bin b/src/resources/cache/webgpu/shader/execution/inverseSqrt.bin new file mode 100644 index 000000000000..2e757f360fa8 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/inverseSqrt.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/ldexp.bin b/src/resources/cache/webgpu/shader/execution/ldexp.bin new file mode 100644 index 000000000000..2aa23b335dce Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/ldexp.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/length.bin b/src/resources/cache/webgpu/shader/execution/length.bin new file mode 100644 index 000000000000..1a611bfbb3d2 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/length.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/log.bin b/src/resources/cache/webgpu/shader/execution/log.bin new file mode 100644 index 000000000000..9872214a8202 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/log.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/log2.bin b/src/resources/cache/webgpu/shader/execution/log2.bin new file mode 100644 index 000000000000..703834636b55 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/log2.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/max.bin b/src/resources/cache/webgpu/shader/execution/max.bin new file mode 100644 index 000000000000..bf829d7524ab Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/max.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/min.bin b/src/resources/cache/webgpu/shader/execution/min.bin new file mode 100644 index 000000000000..a1e8629785c1 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/min.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/mix.bin b/src/resources/cache/webgpu/shader/execution/mix.bin new file mode 100644 index 000000000000..46bf392731f7 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/mix.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/modf.bin b/src/resources/cache/webgpu/shader/execution/modf.bin new file mode 100644 index 000000000000..f08ad4c8ce04 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/modf.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/normalize.bin b/src/resources/cache/webgpu/shader/execution/normalize.bin new file mode 100644 index 000000000000..501aadf5a20a Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/normalize.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/pack2x16float.bin b/src/resources/cache/webgpu/shader/execution/pack2x16float.bin new file mode 100644 index 000000000000..602706e9b2c8 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/pack2x16float.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/pow.bin b/src/resources/cache/webgpu/shader/execution/pow.bin new file mode 100644 index 000000000000..cfa77298b626 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/pow.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/quantizeToF16.bin b/src/resources/cache/webgpu/shader/execution/quantizeToF16.bin new file mode 100644 index 000000000000..0018c258c0db Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/quantizeToF16.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/radians.bin b/src/resources/cache/webgpu/shader/execution/radians.bin new file mode 100644 index 000000000000..d410fe26ad4d Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/radians.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/reflect.bin b/src/resources/cache/webgpu/shader/execution/reflect.bin new file mode 100644 index 000000000000..dfa6e2dca169 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/reflect.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/refract.bin b/src/resources/cache/webgpu/shader/execution/refract.bin new file mode 100644 index 000000000000..39a9a79e872f Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/refract.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/round.bin b/src/resources/cache/webgpu/shader/execution/round.bin new file mode 100644 index 000000000000..6a815b386c01 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/round.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/saturate.bin b/src/resources/cache/webgpu/shader/execution/saturate.bin new file mode 100644 index 000000000000..08cc351e9000 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/saturate.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/sign.bin b/src/resources/cache/webgpu/shader/execution/sign.bin new file mode 100644 index 000000000000..94e68bcdfa8b Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/sign.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/sin.bin b/src/resources/cache/webgpu/shader/execution/sin.bin new file mode 100644 index 000000000000..4f52e1adb384 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/sin.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/sinh.bin b/src/resources/cache/webgpu/shader/execution/sinh.bin new file mode 100644 index 000000000000..b3f7f01b356f Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/sinh.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/smoothstep.bin b/src/resources/cache/webgpu/shader/execution/smoothstep.bin new file mode 100644 index 000000000000..86f73362dd04 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/smoothstep.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/sqrt.bin b/src/resources/cache/webgpu/shader/execution/sqrt.bin new file mode 100644 index 000000000000..0dd7a2ac5f72 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/sqrt.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/step.bin b/src/resources/cache/webgpu/shader/execution/step.bin new file mode 100644 index 000000000000..783bb91cf9ec Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/step.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/tan.bin b/src/resources/cache/webgpu/shader/execution/tan.bin new file mode 100644 index 000000000000..937d7e78883f Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/tan.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/tanh.bin b/src/resources/cache/webgpu/shader/execution/tanh.bin new file mode 100644 index 000000000000..3c180bcba4b8 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/tanh.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/transpose.bin b/src/resources/cache/webgpu/shader/execution/transpose.bin new file mode 100644 index 000000000000..e63e0c314b30 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/transpose.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/trunc.bin b/src/resources/cache/webgpu/shader/execution/trunc.bin new file mode 100644 index 000000000000..193a90a78d65 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/trunc.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/af_arithmetic.bin b/src/resources/cache/webgpu/shader/execution/unary/af_arithmetic.bin new file mode 100644 index 000000000000..fc5bf8dc2fd7 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/af_arithmetic.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/af_assignment.bin b/src/resources/cache/webgpu/shader/execution/unary/af_assignment.bin new file mode 100644 index 000000000000..4fca4df4de82 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/af_assignment.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/bool_conversion.bin b/src/resources/cache/webgpu/shader/execution/unary/bool_conversion.bin new file mode 100644 index 000000000000..bf0d864abcbe Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/bool_conversion.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/f16_arithmetic.bin b/src/resources/cache/webgpu/shader/execution/unary/f16_arithmetic.bin new file mode 100644 index 000000000000..5603e4b1f70a Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/f16_arithmetic.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/f16_conversion.bin b/src/resources/cache/webgpu/shader/execution/unary/f16_conversion.bin new file mode 100644 index 000000000000..9f6c86c14880 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/f16_conversion.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/f32_arithmetic.bin b/src/resources/cache/webgpu/shader/execution/unary/f32_arithmetic.bin new file mode 100644 index 000000000000..10d979f8d465 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/f32_arithmetic.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/f32_conversion.bin b/src/resources/cache/webgpu/shader/execution/unary/f32_conversion.bin new file mode 100644 index 000000000000..0511d67c357a Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/f32_conversion.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/i32_arithmetic.bin b/src/resources/cache/webgpu/shader/execution/unary/i32_arithmetic.bin new file mode 100644 index 000000000000..a7c3688700a4 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/i32_arithmetic.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/i32_complement.bin b/src/resources/cache/webgpu/shader/execution/unary/i32_complement.bin new file mode 100644 index 000000000000..1163ab7347c3 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/i32_complement.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/i32_conversion.bin b/src/resources/cache/webgpu/shader/execution/unary/i32_conversion.bin new file mode 100644 index 000000000000..f3c700f1148c Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/i32_conversion.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/u32_complement.bin b/src/resources/cache/webgpu/shader/execution/unary/u32_complement.bin new file mode 100644 index 000000000000..778a8c9d5a81 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/u32_complement.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unary/u32_conversion.bin b/src/resources/cache/webgpu/shader/execution/unary/u32_conversion.bin new file mode 100644 index 000000000000..f6668b3f9693 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unary/u32_conversion.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unpack2x16float.bin b/src/resources/cache/webgpu/shader/execution/unpack2x16float.bin new file mode 100644 index 000000000000..869f74f9b42c Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unpack2x16float.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unpack2x16snorm.bin b/src/resources/cache/webgpu/shader/execution/unpack2x16snorm.bin new file mode 100644 index 000000000000..ada02252247f Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unpack2x16snorm.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unpack2x16unorm.bin b/src/resources/cache/webgpu/shader/execution/unpack2x16unorm.bin new file mode 100644 index 000000000000..95de6e1222a7 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unpack2x16unorm.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unpack4x8snorm.bin b/src/resources/cache/webgpu/shader/execution/unpack4x8snorm.bin new file mode 100644 index 000000000000..3e3eceae1320 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unpack4x8snorm.bin differ diff --git a/src/resources/cache/webgpu/shader/execution/unpack4x8unorm.bin b/src/resources/cache/webgpu/shader/execution/unpack4x8unorm.bin new file mode 100644 index 000000000000..84fb6cb077d9 Binary files /dev/null and b/src/resources/cache/webgpu/shader/execution/unpack4x8unorm.bin differ diff --git a/src/unittests/crc32.spec.ts b/src/unittests/crc32.spec.ts new file mode 100644 index 000000000000..5986823c8a3b --- /dev/null +++ b/src/unittests/crc32.spec.ts @@ -0,0 +1,28 @@ +export const description = ` +Test for crc32 utility functions. +`; + +import { makeTestGroup } from '../common/framework/test_group.js'; +import { crc32, toHexString } from '../common/util/crc32.js'; + +import { UnitTest } from './unit_test.js'; + +class F extends UnitTest { + test(content: string, expect: string): void { + const got = toHexString(crc32(content)); + this.expect( + expect === got, + ` +expected: ${expect} +got: ${got}` + ); + } +} + +export const g = makeTestGroup(F); + +g.test('strings').fn(t => { + t.test('', '00000000'); + t.test('hello world', '0d4a1185'); + t.test('123456789', 'cbf43926'); +}); diff --git a/src/unittests/parse_imports.spec.ts b/src/unittests/parse_imports.spec.ts new file mode 100644 index 000000000000..0efdc0d17105 --- /dev/null +++ b/src/unittests/parse_imports.spec.ts @@ -0,0 +1,79 @@ +export const description = ` +Test for "parseImports" utility. +`; + +import { makeTestGroup } from '../common/framework/test_group.js'; +import { parseImports } from '../common/util/parse_imports.js'; + +import { UnitTest } from './unit_test.js'; + +class F extends UnitTest { + test(content: string, expect: string[]): void { + const got = parseImports('a/b/c.js', content); + const expectJoined = expect.join('\n'); + const gotJoined = got.join('\n'); + this.expect( + expectJoined === gotJoined, + ` +expected: ${expectJoined} +got: ${gotJoined}` + ); + } +} + +export const g = makeTestGroup(F); + +g.test('empty').fn(t => { + t.test(``, []); + t.test(`\n`, []); + t.test(`\n\n`, []); +}); + +g.test('simple').fn(t => { + t.test(`import 'x/y/z.js';`, ['a/b/x/y/z.js']); + t.test(`import * as blah from 'x/y/z.js';`, ['a/b/x/y/z.js']); + t.test(`import { blah } from 'x/y/z.js';`, ['a/b/x/y/z.js']); +}); + +g.test('multiple').fn(t => { + t.test( + ` +blah blah blah +import 'x/y/z.js'; +more blah +import * as blah from 'm/n/o.js'; +extra blah +import { blah } from '../h.js'; +ending with blah +`, + ['a/b/x/y/z.js', 'a/b/m/n/o.js', 'a/h.js'] + ); +}); + +g.test('multiline').fn(t => { + t.test( + `import { + blah +} from 'x/y/z.js';`, + ['a/b/x/y/z.js'] + ); + t.test( + `import { + blahA, + blahB, +} from 'x/y/z.js';`, + ['a/b/x/y/z.js'] + ); +}); + +g.test('file_characters').fn(t => { + t.test(`import '01234_56789.js';`, ['a/b/01234_56789.js']); +}); + +g.test('relative_paths').fn(t => { + t.test(`import '../x.js';`, ['a/x.js']); + t.test(`import '../x/y.js';`, ['a/x/y.js']); + t.test(`import '../../x.js';`, ['x.js']); + t.test(`import '../../../x.js';`, ['../x.js']); + t.test(`import '../../../../x.js';`, ['../../x.js']); +}); diff --git a/src/webgpu/shader/execution/expression/case_cache.ts b/src/webgpu/shader/execution/expression/case_cache.ts index ff82792d647d..f9bfd3c00b21 100644 --- a/src/webgpu/shader/execution/expression/case_cache.ts +++ b/src/webgpu/shader/execution/expression/case_cache.ts @@ -137,7 +137,7 @@ export class CaseCache implements Cacheable> { * @param builders a Record of case-list name to case-list builder. */ constructor(name: string, builders: Record) { - this.path = `webgpu/shader/execution/case-cache/${name}.bin`; + this.path = `webgpu/shader/execution/${name}.bin`; this.builders = builders; } @@ -164,7 +164,7 @@ export class CaseCache implements Cacheable> { * serialize() implements the Cacheable.serialize interface. * @returns the serialized data. */ - serialize(data: Record): Uint8Array { + async serialize(data: Record): Promise { const maxSize = 32 << 20; // 32MB - max size for a file const stream = new BinaryStream(new ArrayBuffer(maxSize)); stream.writeU32(Object.keys(data).length); @@ -172,15 +172,17 @@ export class CaseCache implements Cacheable> { stream.writeString(name); stream.writeArray(data[name], serializeCase); } - return stream.buffer(); + const compressed = this.compress('gzip', stream.buffer()); + return compressed; } /** * deserialize() implements the Cacheable.deserialize interface. * @returns the deserialize data. */ - deserialize(array: Uint8Array): Record { - const s = new BinaryStream(array.buffer); + async deserialize(array: Uint8Array): Promise> { + const decompressed = await this.decompress('gzip', array); + const s = new BinaryStream(decompressed); const casesByName: Record = {}; const numRecords = s.readU32(); for (let i = 0; i < numRecords; i++) { @@ -191,6 +193,26 @@ export class CaseCache implements Cacheable> { return casesByName; } + /** + * Compresses a Uint8Array using using the given CompressionFormat + */ + private async compress(format: CompressionFormat, data: Uint8Array): Promise { + const stream = new Blob([data]).stream(); + const compressedStream = stream.pipeThrough(new CompressionStream(format)); + const blob = await new Response(compressedStream).blob(); + return new Uint8Array(await blob.arrayBuffer()); + } + + /** + * Decompresses a Uint8Array using using gzip + */ + private async decompress(format: CompressionFormat, data: Uint8Array): Promise { + const stream = new Blob([data]).stream(); + const decompressedStream = stream.pipeThrough(new DecompressionStream(format)); + const blob = await new Response(decompressedStream).blob(); + return await blob.arrayBuffer(); + } + public readonly path: string; private readonly builders: Record; }