All files / src/capabilities HardwareAccelerator.ts

94.73% Statements 18/19
100% Branches 8/8
100% Functions 3/3
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                    5x     5x 3x 3x 2x   1x               5x     5x 2x   2x 2x                       3x 2x   1x      
import { ICapabilities } from "../types.js";
 
/**
 * 2026 Zenith Tier: ハードウェアアクセラレーション(WebNN / WebGPU)の管理と診断を行うユーティリティ。
 */
export class HardwareAccelerator {
  /**
   * WebGPU が利用可能かチェックします。
   */
  public static async checkWebGPU(): Promise<boolean> {
    const g = globalThis as unknown as {
      navigator: { gpu?: { requestAdapter: () => Promise<unknown> } };
    };
    if (!g.navigator.gpu) return false;
    try {
      const adapter = await g.navigator.gpu.requestAdapter();
      return !!adapter;
    } catch {
      return false;
    }
  }
 
  /**
   * WebNN が利用可能かチェックします。
   */
  public static async checkWebNN(): Promise<boolean> {
    const g = globalThis as unknown as {
      navigator: { ml?: { createContext: () => Promise<unknown> } };
    };
    if (!g.navigator.ml) return false;
    try {
      // 2026: 実際にコンテキストを作成できるか検証
      const context = await g.navigator.ml.createContext();
      return !!context;
    } catch {
      return false;
    }
  }
 
  /**
   * 現在の環境で利用可能な最速のアクセラレーション手法を取得します。
   */
  public static async getBestAcceleration(): Promise<
    ICapabilities["acceleration"]
  > {
    if (await this.checkWebNN()) return "webnn";
    if (await this.checkWebGPU()) return "webgpu";
    // SIMD チェックは EnvironmentDetector に依存
    return "none";
  }
}