webgpu-utils

    Function getSizeAndAlignmentOfUnsizedArrayElement

    • Returns the size, align, and unalignedSize of "the" unsized array element. Unsized arrays are only allowed at the outer most level or the last member of a top level struct.

      Example:

      const code = `
      struct Foo {
      a: u32,
      b: array<vec3f>,
      };
      @group(0) @binding(0) var<storage> f: Foo;
      `;
      const defs = makeShaderDataDefinitions(code);
      const { size, align, unalignedSize } = getSizeAndAlignmentOfUnsizedArrayElement(
      defs.storages.f);
      // size = 16 (since you need to allocate 16 bytes per element)
      // align = 16 (since vec3f needs to be aligned to 16 bytes)
      // unalignedSize = 12 (since only 12 bytes are used for a vec3f)

      Generally you only need size. Example:

      const code = `
      struct Foo {
      a: u32,
      b: array<vec3f>,
      };
      @group(0) @binding(0) var<storage> f: Foo;
      `;
      const defs = makeShaderDataDefinitions(code);
      const { size } = getSizeAndAlignmentOfUnsizedArrayElement(defs.storages.f);
      const numElements = 10;
      const views = makeStructuredViews(
      defs.storages.f,
      new ArrayBuffer(defs.storages.f.size + size * numElements));

      Parameters

      Returns { align: number; size: number }

      the size, align, and unalignedSize in bytes of the unsized array element in this type definition. If there is no unsized array, size = 0.

    MMNEPVFCICPMFPCPTTAAATR