-
Notifications
You must be signed in to change notification settings - Fork 370
Allow creating mappable buffer with more usages as optional features #5108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
2b68626
8675390
7765b87
3d39151
cad2e84
44266f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
GPU-writable buffer usages
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3595,23 +3595,22 @@ The {{GPUMapMode}} flags determine how a {{GPUBuffer}} is mapped when calling | |
| :: | ||
| Only valid with buffers created with the {{GPUBufferUsage/MAP_WRITE}} usage. | ||
|
|
||
| When the buffer is created without GPU-writable ({{GPUBufferUsage/STORAGE}}, | ||
| {{GPUBufferUsage/COPY_DST}}, {{GPUBufferUsage/QUERY_RESOLVE}}) usages: | ||
| <dfn dfn>GPU-writable buffer usages</dfn> includes {{GPUBufferUsage/STORAGE}}, | ||
| {{GPUBufferUsage/COPY_DST}} and {{GPUBufferUsage/QUERY_RESOLVE}}. | ||
|
|
||
| When the buffer is created without [=GPU-writable buffer usages=]: | ||
| Once the buffer is mapped, calls to {{GPUBuffer/getMappedRange()}} will return an | ||
| {{ArrayBuffer}} containing the buffer’s current values. | ||
|
|
||
| When the buffer is created with GPU-writable ({{GPUBufferUsage/STORAGE}}, | ||
| {{GPUBufferUsage/COPY_DST}}, {{GPUBufferUsage/QUERY_RESOLVE}}) usages: | ||
| When the buffer is created with [=GPU-writable buffer usages=]: | ||
| Once the buffer is mapped, calls to {{GPUBuffer/getMappedRange()}} will return an | ||
| {{ArrayBuffer}} containing the default initialized data (zeros) or data written by the | ||
| webpage during a previous mapping. | ||
|
|
||
| Changes to the returned ArrayBuffer will be stored in the GPUBuffer after | ||
| Changes to the returned {{ArrayBuffer}} will be stored in the buffer after | ||
| {{GPUBuffer/unmap()}} is called. | ||
|
|
||
| Note: Write-only mapping will never return values produced by the GPU, and the returned | ||
| {{ArrayBuffer}} will only ever contain the default initialized data (zeros) or data written | ||
| by the webpage during a previous mapping. | ||
| Note: Write-only mapping will never return values produced by the GPU. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this restriction necessary? Won't it result in an extra copy of the data for write only mappings on UMA systems? I think it would be preferable to state that reading the contents of a write only mappings are undefined (or zero-ed?) in this case.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is mainly because currently in Chromium a copy is always needed because we cannot access the mapped pointer from GPU resources directly in the JS side. We can discuss more about this issue in the WG meeting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have the same restriction in WebKit too, perhaps this is a non-issue.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We couldn't make it fully undefined, at most we could add a third option for what gets returned. Hard to say if the performance would be any better better if we allowed it. It would require cache flushes or something to make sure you don't get unsafe undefined values. And of course for portability reasons we really don't want anyone to rely on data getting read back on write-only mappings. But that's probably not a huge concern. |
||
|
|
||
| : <dfn>READ|WRITE</dfn> | ||
| :: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be clear, this is allowing either of two behaviors?
For better portability we could clear the map region every time. I wonder, would that be too expensive? (How much of the benefit would we lose from avoiding the copyB2B in today's map-write-then-copy pattern?) The region could even be cleared before mapping by the GPU (which might have higher memory bandwidth than the CPU).
Maybe if there are really cases where effectively
READ|WRITEwould be more efficient thanWRITEwe could have the browser provide a hint about which one to use.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comes from a note in the current SPEC. Here I mean for the first time
buffer.getMappedData()is called after a write-only mapping, the data in the returned array buffer should be all zeros, and since the second time the array buffer will contain the datawritten by the webpage during a previous mapping.Obviously clearing the map region every time is expensive and unnecessary. With the mentioned behavior we don't need to either clear or read the data back from the GPU when non-triply mapping is used.
When triply mapping is supported on CPU-cached UMA (e.g. on Intel iGPUs), we can directly get the GPU data through
buffer.getMappedData()without any other operations. So I add the feature"buffer-map-write-with-extended-usages-and-gpu-data"for the best performance of data uploading on this architecture.The feature
"buffer-map-write-with-extended-usages"also works for CPU-cached UMA with non-triply mapping, and it is for the best performance of data uploading on non-triply mapping on non-CPU-cached UMA and ReBAR, where only write in sequence or memcpy is much more performant compared with randomly write.I feel it strange to use
READtogether withWRITEbecauseMAP_READshould keep data on GPU unchanged. For such use case I decide to use "MAP_WRITEwith current GPU data in the array buffer" instead.