Skip to content

Commit

Permalink
feat(gpuu): new package added
Browse files Browse the repository at this point in the history
Signed-off-by: Neko Ayaka <neko@ayaka.moe>
  • Loading branch information
nekomeowww committed Mar 4, 2025
1 parent 4e1870d commit 8344c0d
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ words:
- gcornut
- giteeai
- gltf
- gpuu
- grammyjs
- groq
- guiiai
Expand Down
50 changes: 50 additions & 0 deletions packages/gpuu/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@proj-airi/gpuu",
"type": "module",
"private": true,
"description": "A collection of utilities for working with GPUs, especially for WebGPU.",
"author": {
"name": "Neko Ayaka",
"email": "neko@ayaka.moe",
"url": "https://github.com/nekomeowww"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/moeru-ai/airi.git",
"directory": "packages/gpuu"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./webgpu": {
"types": "./dist/webgpu/index.d.ts",
"import": "./dist/webgpu/index.mjs",
"require": "./dist/webgpu/index.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"README.md",
"dist",
"package.json"
],
"scripts": {
"dev": "pnpm run stub",
"stub": "unbuild --stub",
"build": "unbuild",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"defu": "^6.1.4"
},
"devDependencies": {
"@types/node": "^22.13.8",
"@webgpu/types": "^0.1.54"
}
}
1 change: 1 addition & 0 deletions packages/gpuu/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.warn('Please import \'@proj-airi/gpuu\' instead.')
36 changes: 36 additions & 0 deletions packages/gpuu/src/webgpu/checker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export interface WebGPUCheckResult {
supported: boolean
fp16Supported: boolean
isNode: boolean
reason: string
adapter?: GPUAdapter
}

export async function check(): Promise<WebGPUCheckResult> {
try {
if (isInNodejsRuntime())
return { supported: false, isNode: true, reason: '', fp16Supported: false }

if (typeof navigator === 'undefined' || !navigator.gpu)
return { supported: false, isNode: false, reason: 'WebGPU is not available (navigator.gpu is undefined)', fp16Supported: false }

const adapter = await navigator.gpu.requestAdapter()
if (!adapter)
return { supported: false, isNode: false, reason: 'WebGPU is not supported (no adapter found)', fp16Supported: false }

return { supported: true, isNode: false, reason: '', adapter, fp16Supported: adapter.features.has('shader-f16') }
}
catch (error) {
const errorMessage = error instanceof Error ? error.toString() : String(error)
return { supported: false, isNode: false, reason: errorMessage, fp16Supported: false }
}
}

function isInNodejsRuntime() {
// eslint-disable-next-line node/prefer-global/process
return typeof process !== 'undefined'
// eslint-disable-next-line node/prefer-global/process
&& 'versions' in process && process.versions != null && typeof process.versions === 'object'
// eslint-disable-next-line node/prefer-global/process
&& 'node' in process.versions && process.versions.node != null
}
2 changes: 2 additions & 0 deletions packages/gpuu/src/webgpu/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { check } from './checker'
export type { WebGPUCheckResult } from './checker'
29 changes: 29 additions & 0 deletions packages/gpuu/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": [
"ESNext",
"DOM",
"DOM.Iterable",
"WebWorker"
],
"module": "ESNext",
"moduleResolution": "bundler",
"types": [
"node",
// @webgpu/types
// https://www.npmjs.com/package/@webgpu/types
"@webgpu/types"
],
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
},
"include": [
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.mts"
]
}
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8344c0d

Please sign in to comment.