size: virtualMipSize()

in src/webgpu/shader/execution/expression/call/builtin/texture_utils.ts [1331:1420]


        size: virtualMipSize(dimension, info.size, i),
        mipLevel: i,
        sampleCount: info.sampleCount ?? 1,
      },
      options
    )
  );
}

export type vec1 = [number]; // Because it's easy to deal with if these types are all array of number
export type vec2 = [number, number];
export type vec3 = [number, number, number];
export type vec4 = [number, number, number, number];
export type Dimensionality = vec1 | vec2 | vec3;

type TextureCallArgKeys = keyof TextureCallArgs<vec1>;
const kTextureCallArgNames: readonly TextureCallArgKeys[] = [
  'component',
  'coords',
  'derivativeMult', // NOTE: derivativeMult not an argument but is used with coords for implicit derivatives.
  'arrayIndex',
  'bias',
  'sampleIndex',
  'mipLevel',
  'ddx',
  'ddy',
  'depthRef',
  'offset',
] as const;

export interface TextureCallArgs<T extends Dimensionality> {
  component?: number; // Used by textureGather
  coords?: T; // The coord passed
  derivativeMult?: T;
  mipLevel?: number;
  arrayIndex?: number;
  bias?: number;
  sampleIndex?: number;
  depthRef?: number;
  ddx?: T;
  ddy?: T;
  offset?: T;
}

export type TextureBuiltin =
  | 'textureGather'
  | 'textureGatherCompare'
  | 'textureLoad'
  | 'textureSample'
  | 'textureSampleBaseClampToEdge'
  | 'textureSampleBias'
  | 'textureSampleCompare'
  | 'textureSampleCompareLevel'
  | 'textureSampleGrad'
  | 'textureSampleLevel';

export interface TextureCall<T extends Dimensionality> extends TextureCallArgs<T> {
  builtin: TextureBuiltin;
  coordType: 'f' | 'i' | 'u';
  levelType?: 'i' | 'u' | 'f';
  arrayIndexType?: 'i' | 'u';
  sampleIndexType?: 'i' | 'u';
  componentType?: 'i' | 'u';
}

const isBuiltinComparison = (builtin: TextureBuiltin) =>
  builtin === 'textureGatherCompare' ||
  builtin === 'textureSampleCompare' ||
  builtin === 'textureSampleCompareLevel';
const isBuiltinGather = (builtin: TextureBuiltin | undefined) =>
  builtin === 'textureGather' || builtin === 'textureGatherCompare';
const builtinNeedsSampler = (builtin: TextureBuiltin) =>
  builtin.startsWith('textureSample') || builtin.startsWith('textureGather');
const builtinNeedsDerivatives = (builtin: TextureBuiltin) =>
  builtin === 'textureSample' ||
  builtin === 'textureSampleBias' ||
  builtin === 'textureSampleCompare';

const isCubeViewDimension = (viewDescriptor?: GPUTextureViewDescriptor) =>
  viewDescriptor?.dimension === 'cube' || viewDescriptor?.dimension === 'cube-array';

const isViewDimensionCubeOrCubeArray = (viewDimension: GPUTextureViewDimension) =>
  viewDimension === 'cube' || viewDimension === 'cube-array';

const s_u32 = new Uint32Array(1);
const s_f32 = new Float32Array(s_u32.buffer);
const s_i32 = new Int32Array(s_u32.buffer);

const kBitCastFunctions = {
  f: (v: number) => {