mgpuOwnDeviceRequirements function

(Set<String>, Map<String, int>) mgpuOwnDeviceRequirements(
  1. JSObject device
)

Capture actual device features/limits for browser-selected replacement. These are compatibility requirements, never a physical adapter identity.

Implementation

(Set<String>, Map<String, int>) mgpuOwnDeviceRequirements(JSObject device) {
  final array = globalContext.getProperty<JSObject>('Array'.toJS);
  final object = globalContext.getProperty<JSObject>('Object'.toJS);
  final features = device.getProperty<JSObject>('features'.toJS);
  final names = array.callMethod<JSArray<JSString>>('from'.toJS, features);
  final limits = device.getProperty<JSObject>('limits'.toJS);
  final values = <String, int>{};
  JSObject? source = limits;
  for (var depth = 0; source != null && depth < 3; depth++) {
    final keys = object.callMethod<JSArray<JSString>>(
        'getOwnPropertyNames'.toJS, source);
    for (final key in keys.toDart) {
      final name = key.toDart;
      if (!name.startsWith('max') && name != 'minUniformBufferOffsetAlignment' &&
          name != 'minStorageBufferOffsetAlignment') {
        continue;
      }
      final raw = limits.getProperty<JSAny?>(key);
      if (raw == null || !raw.isA<JSNumber>()) continue;
      final value = (raw as JSNumber).toDartDouble;
      if (!value.isFinite || value < 0 || value > 9007199254740991 ||
          value != value.truncateToDouble()) {
        throw StateError('WebGPU returned an invalid $name limit');
      }
      values[name] = value.toInt();
    }
    source = object.callMethod<JSObject?>('getPrototypeOf'.toJS, source);
  }
  if (!values.containsKey('maxBufferSize') ||
      !values.containsKey('maxTextureDimension2D')) {
    throw StateError('WebGPU replacement limits are unavailable');
  }
  return ({for (final name in names.toDart) name.toDart}, values);
}