webgpuCreateCubeRenderTarget function

TextureHandle? webgpuCreateCubeRenderTarget(
  1. GPUDevice gpu,
  2. List<WebGpuTexture> tracked, {
  3. required int size,
  4. required TextureFormat format,
  5. int mipLevels = 1,
})

An empty cube a pass may draw into, one face and one level at a time. See GraphicsDevice.createCubeRenderTarget.

Six array layers and RENDER_ATTACHMENT, and that is the whole of it. Where WebGL2 needs a face target constant in framebufferTexture2D and Impeller needs a slice on the attachment, here a face is baseArrayLayer and a level is baseMipLevel on an ordinary 2D view — WebGpuTexture.attachmentView already makes exactly that pair, because a cube face and a mip level are the same mechanism in this API.

mipLevels is a chain a pass may draw into, not one it may only upload to, and for a while this said the opposite. The levels were allocated from the day the cube was and supportsRenderToMip went on answering false beside them, so a caller reading the capability was told the chain was out of reach while the allocation quietly held it. Nothing had to be built to lift that: the level is baseMipLevel on the attachment view, and a reflection probe fills the chain with its own passes rather than asking this API for a generateMipmap it does not have.

Null for a format this device has no spelling for, and for a block-compressed one: those have spellings now that the compression features are asked for, and a compressed render target is a validation error rather than a slow path.

Implementation

TextureHandle? webgpuCreateCubeRenderTarget(
  GPUDevice gpu,
  List<WebGpuTexture> tracked, {
  required int size,
  required TextureFormat format,
  int mipLevels = 1,
}) {
  final spelling = gpuTextureFormat(format);
  if (spelling == null || format.isCompressed) return null;
  final texture = gpu.createTexture(
    GPUTextureDescriptor(
      size: GPUExtent3DDict(width: size, height: size, depthOrArrayLayers: 6),
      format: spelling,
      usage:
          GpuTextureUsage.renderAttachment |
          GpuTextureUsage.textureBinding |
          GpuTextureUsage.copyDst |
          GpuTextureUsage.copySrc,
      sampleCount: 1,
      mipLevelCount: mipLevels,
      dimension: '2d',
      label: 'cube target ${size}x$size $spelling',
    ),
  );
  final backend = WebGpuTexture(
    texture: texture,
    dimension: WebGpuTextureDimension.cube,
    sampleable: true,
  );
  tracked.add(backend);
  return TextureHandle(
    backend: backend,
    width: size,
    height: size,
    format: format,
    type: TextureType.textureCube,
  );
}