|
| 1 | +import { EXECUTION_MODES, MIME_TYPES, RENDERING_BACKENDS } from "./constants"; |
| 2 | +import { createBlobURL } from "./blobURL"; |
| 3 | +import { stripLeadingDotSlash } from "./stringOps"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Validate provided config |
| 7 | + * @param {object} config |
| 8 | + */ |
| 9 | +export function validateConfig(config) { |
| 10 | + const validRenderings = Object.values(RENDERING_BACKENDS); |
| 11 | + const validExecModes = Object.values(EXECUTION_MODES); |
| 12 | + if (config.rendering !== undefined && !validRenderings.includes(config.rendering)) { |
| 13 | + throw new Error(`Invalid rendering backend: ${config.rendering}. Valid options are: ${validRenderings.join(', ')}`); |
| 14 | + } |
| 15 | + if (config.exec !== undefined && !validExecModes.includes(config.exec)) { |
| 16 | + throw new Error(`Invalid execution mode: ${config.exec}. Valid options are: ${validExecModes.join(', ')}`); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Normalize provided config based on constraints. WebGPU requires async execution. |
| 22 | + * @param {object} config |
| 23 | + * @returns {object} |
| 24 | + */ |
| 25 | +export function normalizeConfig(config) { |
| 26 | + if (config?.rendering === RENDERING_BACKENDS.WEBGPU) { |
| 27 | + if (config?.exec !== EXECUTION_MODES.ASYNC) { |
| 28 | + console.warn('WebGPU rendering requires async execution mode. Switching exec to "async".'); |
| 29 | + return { ...config, exec: EXECUTION_MODES.ASYNC }; |
| 30 | + } |
| 31 | + } |
| 32 | + return config; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Check if two configs are the same. Only rendering and exec are compared. |
| 37 | + * @param {object} config1 |
| 38 | + * @param {object} config2 |
| 39 | + * @returns {boolean} |
| 40 | + */ |
| 41 | +export function isSameConfig(config1, config2) { |
| 42 | + return config1.rendering === config2.rendering && config1.exec === config2.exec; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Create Emscripten config from provided config |
| 47 | + * @param {object} config |
| 48 | + * @param {{name: string, buffer: ArrayBufferLike}} wasmFile |
| 49 | + * @returns {object} |
| 50 | + */ |
| 51 | +export function createEmscriptenConfig(config, wasmFile) { |
| 52 | + const emscriptenConfig = {}; |
| 53 | + let wasmURL = null; |
| 54 | + if (wasmFile !== null && wasmFile !== undefined) { |
| 55 | + emscriptenConfig.locateFile = (fileName) => { |
| 56 | + const normalizedFileName = stripLeadingDotSlash(fileName); |
| 57 | + const normalizedTargetName = stripLeadingDotSlash(wasmFile.name); |
| 58 | + if (normalizedFileName === normalizedTargetName) { |
| 59 | + wasmURL = createBlobURL(wasmFile.buffer, MIME_TYPES.WASM); |
| 60 | + return wasmURL; |
| 61 | + } |
| 62 | + // Resolve other files relative to this module's URL, not the page URL. |
| 63 | + return new URL(fileName, import.meta.url).href; |
| 64 | + }; |
| 65 | + } |
| 66 | + let userOnRuntimeInitialized = config?.onRuntimeInitialized; |
| 67 | + emscriptenConfig.onRuntimeInitialized = () => { |
| 68 | + if (typeof userOnRuntimeInitialized === 'function') { |
| 69 | + userOnRuntimeInitialized(); |
| 70 | + } |
| 71 | + // Free the object URL after runtime is initialized |
| 72 | + if (wasmURL !== null) { |
| 73 | + URL.revokeObjectURL(wasmURL); |
| 74 | + } |
| 75 | + }; |
| 76 | + if (config?.rendering === RENDERING_BACKENDS.WEBGPU) { |
| 77 | + const userPreRun = Array.isArray(config?.preRun) |
| 78 | + ? config.preRun |
| 79 | + : (config?.preRun ? [config.preRun] : []); |
| 80 | + emscriptenConfig.preRun = [(module) => { |
| 81 | + module.ENV.VTK_GRAPHICS_BACKEND = 'WEBGPU'; |
| 82 | + }, |
| 83 | + ...userPreRun]; |
| 84 | + } |
| 85 | + return emscriptenConfig; |
| 86 | +} |
0 commit comments