onnxruntime/js/web/lib/onnxjs/backends/backend-webgl.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
2.8 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { env } from 'onnxruntime-common';
import { Backend, SessionHandler } from '../backend';
import { Logger } from '../instrument';
import { Session } from '../session';
import { WebGLSessionHandler } from './webgl/session-handler';
import { WebGLContext } from './webgl/webgl-context';
import { createWebGLContext } from './webgl/webgl-context-factory';
/**
* WebGLBackend is the entry point for all WebGL opeartions
* When it starts it created the WebGLRenderingContext
* and other main framework components such as Program and Texture Managers
*/
export class WebGLBackend implements Backend {
glContext: WebGLContext;
get contextId(): 'webgl' | 'webgl2' | undefined {
return env.webgl.contextId;
}
set contextId(value: 'webgl' | 'webgl2' | undefined) {
env.webgl.contextId = value;
}
get matmulMaxBatchSize(): number | undefined {
return env.webgl.matmulMaxBatchSize;
}
set matmulMaxBatchSize(value: number | undefined) {
env.webgl.matmulMaxBatchSize = value;
}
get textureCacheMode(): 'initializerOnly' | 'full' | undefined {
return env.webgl.textureCacheMode;
}
set textureCacheMode(value: 'initializerOnly' | 'full' | undefined) {
env.webgl.textureCacheMode = value;
}
get pack(): boolean | undefined {
return env.webgl.pack;
}
set pack(value: boolean | undefined) {
env.webgl.pack = value;
}
get async(): boolean | undefined {
return env.webgl.async;
}
set async(value: boolean | undefined) {
env.webgl.async = value;
}
initialize(): boolean {
try {
this.glContext = createWebGLContext(this.contextId);
if (typeof this.matmulMaxBatchSize !== 'number') {
this.matmulMaxBatchSize = 16;
}
if (typeof this.textureCacheMode !== 'string') {
this.textureCacheMode = 'full';
}
if (typeof this.pack !== 'boolean') {
this.pack = false;
}
if (typeof this.async !== 'boolean') {
this.async = false;
}
Logger.setWithEnv(env);
if (!env.webgl.context) {
Object.defineProperty(env.webgl, 'context', { value: this.glContext.gl });
}
Logger.verbose(
'WebGLBackend',
`Created WebGLContext: ${typeof this.glContext} with matmulMaxBatchSize: ${
this.matmulMaxBatchSize
}; textureCacheMode: ${this.textureCacheMode}; pack: ${this.pack}; async: ${this.async}.`,
);
return true;
} catch (e) {
Logger.warning('WebGLBackend', `Unable to initialize WebGLBackend. ${e}`);
return false;
}
}
createSessionHandler(context: Session.Context): SessionHandler {
return new WebGLSessionHandler(this, context);
}
dispose(): void {
this.glContext.dispose();
}
}