[webkit-changes] [WebKit/WebKit] 0f12e7: [WGSL] Don't pack user-defined structs

Tadeu Zagallo noreply at github.com
Mon Jun 5 02:38:24 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: 0f12e79a9bd41fdb635555d0a08db893d4670801
      https://github.com/WebKit/WebKit/commit/0f12e79a9bd41fdb635555d0a08db893d4670801
  Author: Tadeu Zagallo <tzagallo at apple.com>
  Date:   2023-06-05 (Mon, 05 Jun 2023)

  Changed paths:
    M Source/WebGPU/WGSL/AST/ASTStructure.h
    M Source/WebGPU/WGSL/GlobalVariableRewriter.cpp
    M Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp
    A Source/WebGPU/WGSL/tests/valid/packing.wgsl

  Log Message:
  -----------
  [WGSL] Don't pack user-defined structs
https://bugs.webkit.org/show_bug.cgi?id=257653
rdar://110175536

Reviewed by Myles C. Maxfield.

WGSL distinguishes from Metal in the representation of `vec3`. In WGSL, the size
of `vec3<T>` is `3 * sizeof(T)` and the alignment is `4 * sizeof(T)`. In Metal,
we translate `vec3<T>` to `vec<T, 3>`, which has both size and alignment equal
to `4 * sizeof(T)`. This difference is observable through buffers.

The implementation to pack structs was first introduced in 264243 at main, by using
packed types (e.g. `packed_float3`). Originally, all structs were packed, but
packed types aren't allowed everywhere, for example on the vertex output. To fix
that, in 264277 at main, I changed it so that we only pack user-defined structs, but
that is not correct either, since not all operations work on packed types. An
example is matrix multiplication: we can only multiply a matrix by `vec<float, 3>`,
not `packed_float3`.

To implement this correctly, we need to distinguish between a packed and unpacked
struct, and we can't modify the original struct. Packed structs are only necessary
for resources, and in that case we need to create a duplicate, packed version of the
struct. This is the first of a series of patches that implement the proper behavior.
For now, we merely set up the duplicate struct and introduce 2 new struct roles to
give us more information about packed structs. This information will be used in the
following patches to determine where we need to explicitly pack and upack values.

* Source/WebGPU/WGSL/AST/ASTStructure.h:
* Source/WebGPU/WGSL/GlobalVariableRewriter.cpp:
(WGSL::RewriteGlobalVariables::collectGlobals):
(WGSL::RewriteGlobalVariables::packResourceStruct):
* Source/WebGPU/WGSL/Metal/MetalFunctionWriter.cpp:
(WGSL::Metal::FunctionDefinitionWriter::visit):
* Source/WebGPU/WGSL/tests/valid/packing.wgsl: Added.

Canonical link: https://commits.webkit.org/264864@main




More information about the webkit-changes mailing list