Skip to content

WGSL: describe memory layout rules for buffers - #1215

Merged
dneto0 merged 4 commits into
gpuweb:mainfrom
dneto0:buffer-layout
Nov 19, 2020
Merged

dneto0 merged 4 commits into
gpuweb:mainfrom
dneto0:buffer-layout

Conversation

@dneto0

@dneto0 dneto0 commented Nov 9, 2020

Copy link
Copy Markdown
Contributor

This builds on #1214 (describe resource interface of a shader)

WGSL: describe memory layout rules for buffers

- Add notations for floor, ceiling, roundUp
- define 'stride' and 'offset' attributes
- describe byte-by-byte layout of values, based on storage class, and
stride and offset
- describe constraints on stride and offset, yielding 'uniform buffer
layout' and 'storage buffer layout'

@dneto0
dneto0 requested review from dj2 and kvark November 9, 2020 15:50
@dneto0 dneto0 added wgsl WebGPU Shading Language Issues for wgsl meeting labels Nov 9, 2020
@dneto0 dneto0 added this to the MVP milestone Nov 9, 2020
@github-actions

github-actions Bot commented Nov 9, 2020

Copy link
Copy Markdown
Contributor

Previews, as seen at the time of posting this comment:
WebGPU | IDL
WGSL
0349457

@github-actions

github-actions Bot commented Nov 9, 2020

Copy link
Copy Markdown
Contributor

Previews, as seen at the time of posting this comment:
WebGPU | IDL
WGSL
55288ea

Comment thread wgsl/index.bs Outdated
<tr><td><dfn noexport>`stride`</dfn>
<td>positive i32 literal
<td>Applied to an array type.<br>
The number of bytes between successive elements of the array,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first reading, I take this to mean the space between the elements, not element + space. The number of bytes from the start of one element to the start of the next element of the array?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9603cf

Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Outdated
The memory layout of a type is significant only when referring to a value in those storage classes.
This affects evaluation of a variable in one of those storage classes, or of a pointer into one of those storage classes.
This affects evaluation of a variable in one of those storage classes,
or of a pointer expression pointing into one of those storage classes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the 'This' in this sentence, the buffer variable?

This sentence has too many one of those storage classes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing those last two sentences with:

The memory layout of a type is significant only when evaluating an expression with:

  • a variable in the uniform or storage storage class, or
  • a pointer into the uniform or storage storage class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL at 9603cf

Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Outdated
not support the `scalarBlockLayout` feature.
Therefore, a data value must not be placed in the padding at the end of a structure or matrix,
nor in the padding at the last element of an array.
Counting the such padding as part of the allocation extent allows [SHORTNAME] to capture this constraint.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Counting the padding or Counting such padding

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9603cf

Comment thread wgsl/index.bs Outdated
</table>

If |B| is a [=uniform buffer=] variable in a resource interface,
and |WB| is the WebGPU GPUBuffer bound to |B|, then:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we link WebGPU GPUBuffer back to the spec?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9603cf

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen at the time of posting this comment:
WebGPU | IDL
WGSL
41555bd

@dneto0

dneto0 commented Nov 10, 2020

Copy link
Copy Markdown
Contributor Author

Rebased, and added 9603cf to reply to most of @dj2's feedback.

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen at the time of posting this comment:
WebGPU | IDL
WGSL
9603cf6

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen at the time of posting this comment:
WebGPU | IDL
WGSL
01bce53

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen at the time of posting this comment:
WebGPU | IDL
WGSL
5b29556

@dneto0

dneto0 commented Nov 10, 2020

Copy link
Copy Markdown
Contributor Author

The parent PR was updated, so I rebased again.

Comment thread wgsl/index.bs Resolved
@grorg

grorg commented Nov 17, 2020

Copy link
Copy Markdown
Contributor

Discussed at 2020-11-17 meeting.

@dneto0

dneto0 commented Nov 17, 2020

Copy link
Copy Markdown
Contributor Author

From the meeting:

  • add a non-normative statement about how std140 and std430 from OpenGL (and GLSL) map to these concepts
  • add non-normative statement about what Vulkan concepts map to these

@dneto0

dneto0 commented Nov 17, 2020

Copy link
Copy Markdown
Contributor Author

Hey @kvark please take a look.

@kvark kvark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is some grand work, thank you for writing it down!

Comment thread wgsl/index.bs Outdated
the host and the GPU, or copied between host and GPU without format translation.
When used for this purpose, the type must be additionally decorated with layout attributes
as described in [[#memory-layouts]].
In particular, any variable using the `uniform` or `storage` [=storage classes=] must have

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other types that need to be host-shareable? If not, then "In particular" sounds a bit misleading. Perhaps, we should more precise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here to provide context. Really it's a forward reference.

I will change to:

We will see in [[#module-scope-variables]] that the [=store type=] of [=uniform buffer=] and [=storage buffer=]
variables must be host-shareable.

Comment thread wgsl/index.bs Outdated
<td>Applied to a member of a structure type.<br>
The number of bytes between the start of the structure and the location
of this member, when a value of this structure is stored in the
`uniform` or `storage` [=storage classes=].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary? We are describing the attributes of host-shareable types, so the fact about uniform/storage is specified/implied at the higher level

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will remove for both 'stride' and 'offset'.

Comment thread wgsl/index.bs Outdated
See [[#resource-layout-compatibility]].

Compared to OpenGL:
* OpenGL `std140` layout satisfies [SHORTNAME] [=uniform buffer layout=].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it "satisfies", or is it "exactly matches"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Satisfies. WGSL uniform buffer layout can give strides and offsets that are multiples of the ones specified by std140.

Example:

  uint arr[3];   // will have stride of 16 in std140. 

But

  [[stride(32)]] array<u32,3>; // This is perfectly fine to use inside a uniform buffer in WGSL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead say something like

For any type |S|, the tightest offset and stride assignments would exactly match both [SHORTNAME] [=uniform buffer layout=] and std140

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unresolved, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

  • For any type |S|, OpenGL std140 layout is the same as using the tightest offset and stride assignments in [SHORTNAME] [=uniform buffer layout=].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or remove the quantification by |S|:

  • For any type, OpenGL std140 layout is the same as using the tightest offset and stride assignments in WGSL [=uniform buffer layout=].

Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Outdated
= |k| &times; <var ignore>Align</var>(|T|<sub>|i|</sub>,<var ignore>C</var>),
for some non-negative integer |k|
</p>
* For the `uniform` storage class, members are aligned to 16 byte boundaries:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof. I think this is wrong. Will remove it.

@dneto0 dneto0 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've addressed all feedback from @kvark

Comment thread wgsl/index.bs Outdated
<td>Applied to a member of a structure type.<br>
The number of bytes between the start of the structure and the location
of this member, when a value of this structure is stored in the
`uniform` or `storage` [=storage classes=].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will remove for both 'stride' and 'offset'.

Comment thread wgsl/index.bs Outdated
the host and the GPU, or copied between host and GPU without format translation.
When used for this purpose, the type must be additionally decorated with layout attributes
as described in [[#memory-layouts]].
In particular, any variable using the `uniform` or `storage` [=storage classes=] must have

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here to provide context. Really it's a forward reference.

I will change to:

We will see in [[#module-scope-variables]] that the [=store type=] of [=uniform buffer=] and [=storage buffer=]
variables must be host-shareable.

Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Outdated
See [[#resource-layout-compatibility]].

Compared to OpenGL:
* OpenGL `std140` layout satisfies [SHORTNAME] [=uniform buffer layout=].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unresolved, right?

- define 'block' attribute on structure type
- text from 'uniform' and 'storage' rows of storage classes table
  out to resource interface section
- define 'set' and 'binding' attribute on structure type
- relate resources in WGSL to WebGPU GPUBindingType

- Add notations for floor, ceiling, roundUp
- define 'stride' and 'offset' attributes
- describe byte-by-byte layout of values, based on storage class, and
stride and offset
- describe constraints on stride and offset, yielding 'uniform buffer
layout' and 'storage buffer layout'

- Fix alignment of structure in uniform storage class

- Uniform buffer layout: 16byte alignment of array elements, and of struct

* Clarify array stride as counting from the start of one element
  to the start of the next
* Simplify description of when layout applies, using bullet points.
* Remove stray "the"
* Add crossreference for WebGPU GPUBuffer
* Add internal links to runtime-sized

Add notes about relation to OpenGL and Vulkan layout

Even in uniform buffers, struct members aren't automatically aligned to 16

Simplify description of stride and offset

Clarify forward reference of uniform/storage buffer variables as host-shareable

Fix one WGSL -> [SHORTNAME]

Clarify that a type can be used in both uniform and storage storage classes
@dneto0

dneto0 commented Nov 19, 2020

Copy link
Copy Markdown
Contributor Author

Resolved @kvark's issue with std140, std430 (I hope).

Then I rebased against main, and fixed up the cross-references for storage classes as mentioned in the new text.

@dneto0
dneto0 merged commit e9df811 into gpuweb:main Nov 19, 2020
ben-clayton added a commit to ben-clayton/gpuweb that referenced this pull request Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wgsl WebGPU Shading Language Issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants