gpuweb / gpuweb

Where the GPU for the Web work happens!
https://webgpu.io
Other
4.78k stars 316 forks source link

Provide stronger control over physical alignments of buffers and bindings #3021

Open kainino0x opened 2 years ago

kainino0x commented 2 years ago

In #2999 and #2505 it was decided that author-provided alignments in WGSL shaders do not incur alignment restrictions on the buffer binding attached to the shader. However, for performance reasons, a shader author may want to guarantee the physical alignment of a WGSL buffer access (constraint 2 in https://github.com/gpuweb/gpuweb/issues/2999#issuecomment-1148913857). @dneto0 describes some examples:

  • I pointed out that a system author can have a legitimate desire to constrain the divisor on the machine address that goes beyond things known by a WebGPU implementation about primitive shader data types:
    • E.g. system cache line size (want to avoid false sharing)
    • E.g. system page size
    • E.g. interfacing with an external accelerator (e.g. DMA, another hardware block)

In order to guarantee the physical alignment of a WGSL load, we need to guarantee the physical alignment of a binding, that is, the (physical address of the GPUBuffer + static offset + dynamic offset) must be aligned. All of this is currently in the control of the author except for the alignment of the GPUBuffer, but we also don't provide any help with maintaining the guarantee.


Some rough options follow. Please point out if anything doesn't make sense.

Decision A: Alignment of GPUBuffers

  1. Add an alignment to GPUBufferDescriptor.
  2. If there is some smallish maximum alignment that an application "would ever" need, say 1024, we could simply always align GPUBuffer allocations to this alignment. I don't understand the requirements fully enough to know if this is an option.

Decision B: Alignment of bindings

  1. Add no extra validation for this. Applications must simply "get it right" so their loads are aligned the way they want.
  2. Add a new GPUBufferBindingLayout.alignment member. All three of the GPUBuffer alignment, static GPUBufferBinding.offset, and dynamic offset must be multiples of this.
  3. 2, plus add a new WGSL attribute like @binding_alignment(1024) @group(0) @binding(0) var<storage> buffer: B;. GPUBufferBindingLayout.alignment must be a multiple of this. Its value is independent of AlignOf(B) (may be smaller or larger).
  4. Expose some kind of shader (and binding?) reflection to let applications do this validation themselves. (https://github.com/gpuweb/gpuweb/issues/2505#issuecomment-1144131942)

Tentatively marking v1 for initial surfacing, but this is a definite candidate for post-v1.

litherum commented 2 years ago

resolved today: we can add this in later if necessary. not necessary for v1.