onnxruntime/js/web/lib/onnxjs/backends/webgl/utils.ts
Yulong Wang abdc31de40
[js] change default formatter for JavaScript/TypeScript from clang-format to Prettier (#21728)
### Description

See
454996d496
for manual changes (excluded auto-generated formatting changes)

### Why

Because the toolsets for old clang-format is out-of-date. This reduces
the development efficiency.

- The NPM package `clang-format` is already in maintenance mode. not
updated since 2 years ago.
- The VSCode extension for clang-format is not maintained for a while,
and a recent Node.js security update made it not working at all in
Windows.

No one in community seems interested in fixing those.

Choose Prettier as it is the most popular TS/JS formatter.

### How to merge

It's easy to break the build:
- Be careful of any new commits on main not included in this PR.
- Be careful that after this PR is merged, other PRs that already passed
CI can merge.

So, make sure there is no new commits before merging this one, and
invalidate js PRs that already passed CI, force them to merge to latest.
2024-08-14 16:51:22 -07:00

97 lines
3 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { assert } from '../../util';
/**
* Given a non RGBA shape calculate the R version
* It is assumed that the dimensions are multiples of given channels
* NOTE: it is always the last dim that gets packed.
* @param unpackedShape original shape to create a packed version from
*/
export function getPackedShape(unpackedShape: readonly number[]): readonly number[] {
const len = unpackedShape.length;
return unpackedShape.slice(0, len - 1).concat(unpackedShape[len - 1] / 4);
}
export async function repeatedTry(
checkFn: () => boolean,
delayFn = (_counter: number) => 0,
maxCounter?: number,
): Promise<void> {
return new Promise<void>((resolve, reject) => {
let tryCount = 0;
const tryFn = () => {
if (checkFn()) {
resolve();
return;
}
tryCount++;
const nextBackoff = delayFn(tryCount);
if (maxCounter != null && tryCount >= maxCounter) {
reject();
return;
}
setTimeout(tryFn, nextBackoff);
};
tryFn();
});
}
/**
* Generates the function name from an input sampler name.
* @param samplerName Name of the sampler.
*/
export function generateShaderFuncNameFromInputSamplerName(samplerName: string): string {
assert(typeof samplerName !== 'undefined' && samplerName.length !== 0, () => 'empty string found for sampler name');
return 'get' + samplerName.charAt(0).toUpperCase() + samplerName.slice(1);
}
/**
* Generates the function name from an input sampler name at output coordinates.
* @param samplerName Name of the sampler.
*/
export function generateShaderFuncNameFromInputSamplerNameAtOutCoords(samplerName: string): string {
assert(typeof samplerName !== 'undefined' && samplerName.length !== 0, () => 'empty string found for sampler name');
return 'get' + samplerName.charAt(0).toUpperCase() + samplerName.slice(1) + 'AtOutCoords';
}
/** Returns a new input shape (a copy) that has a squeezed logical shape. */
export function squeezeInputShape(inputShape: readonly number[], squeezedShape: number[]): number[] {
// Deep copy.
let newInputShape: number[] = JSON.parse(JSON.stringify(inputShape));
newInputShape = squeezedShape;
return newInputShape;
}
/** Returns a list of squeezed parameters for shader functions */
export function getSqueezedParams(params: string[], keptDims: number[]): string {
return keptDims.map((d) => params[d]).join(', ');
}
/** Returns the data type for different ranks. */
export function getCoordsDataType(rank: number): string {
if (rank <= 1) {
return 'int';
} else if (rank === 2) {
return 'ivec2';
} else if (rank === 3) {
return 'ivec3';
} else if (rank === 4) {
return 'ivec4';
} else if (rank === 5) {
return 'ivec5';
} else if (rank === 6) {
return 'ivec6';
} else {
throw Error(`GPU for rank ${rank} is not yet supported`);
}
}
export function getGlChannels(rank = 6): string[] {
return ['x', 'y', 'z', 'w', 'u', 'v'].slice(0, rank);
}