samplerFor method

GPUSampler samplerFor(
  1. SamplerOptions options
)

The sampler object for options, made once per distinct description.

This is where all of the WebGL2 backend's sampler trouble disappears. In GL the filter and the wrap modes are properties of the texture, so that backend sets four texParameteri on every bind and one image bound twice with two descriptions keeps whichever came last. WebGPU has real sampler objects compared by value, SamplerOptions already is a value, and so a map is the whole of it.

SamplerOptions.anisotropy is clamped to maxAnisotropy rather than refused, which is the contract's own rule: a caller may ask for sixteen without asking first.

Implementation

GPUSampler samplerFor(SamplerOptions options) =>
    _samplers[options] ??= gpuDevice.createSampler(
      GPUSamplerDescriptor(
        addressModeU: gpuAddressMode(options.widthAddressMode),
        addressModeV: gpuAddressMode(options.heightAddressMode),
        // Nothing in this engine samples a 3D texture, so the third axis is
        // the second's — a value is required and any of the three is legal.
        addressModeW: gpuAddressMode(options.heightAddressMode),
        magFilter: gpuFilterMode(options.magFilter),
        minFilter: gpuFilterMode(options.minFilter),
        mipmapFilter: gpuMipmapFilterMode(options.mipFilter),
        lodMinClamp: 0.0,
        // The whole chain. A texture with one level ignores it, and one built
        // with a chain on purpose wants every level of it.
        lodMaxClamp: 32.0,
        maxAnisotropy: options.anisotropy > maxAnisotropy
            ? maxAnisotropy
            : options.anisotropy,
        label: options.toString(),
      ),
    );