export function concreteTypeOf()

in src/webgpu/util/conversion.ts [1188:1234]


export function concreteTypeOf(ty: Type, allowedScalarTypes?: Type[]): Type {
  if (allowedScalarTypes && allowedScalarTypes.length > 0) {
    // https://www.w3.org/TR/WGSL/#conversion-rank
    switch (ty) {
      case Type.abstractInt:
        if (allowedScalarTypes.includes(Type.i32)) {
          return Type.i32;
        }
        if (allowedScalarTypes.includes(Type.u32)) {
          return Type.u32;
        }
      // fallthrough.
      case Type.abstractFloat:
        if (allowedScalarTypes.includes(Type.f32)) {
          return Type.f32;
        }
        if (allowedScalarTypes.includes(Type.f16)) {
          return Type.f16;
        }
        throw new Error(`no ${ty}`);
    }
  } else {
    switch (ty) {
      case Type.abstractInt:
        return Type.i32;
      case Type.abstractFloat:
        return Type.f32;
    }
  }
  if (ty instanceof ScalarType) {
    return ty;
  }
  if (ty instanceof VectorType) {
    return Type.vec(ty.width, concreteTypeOf(ty.elementType, allowedScalarTypes) as ScalarType);
  }
  if (ty instanceof MatrixType) {
    return Type.mat(
      ty.cols,
      ty.rows,
      concreteTypeOf(ty.elementType, allowedScalarTypes) as ScalarType
    );
  }
  if (ty instanceof ArrayType) {
    return Type.array(ty.count, concreteTypeOf(ty.elementType, allowedScalarTypes));
  }
  throw new Error(`unhandled type ${ty}`);
}