Skip to content
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

Draft: dealing with data too large for a single buffer #6138

Draft
wants to merge 27 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e076832
init files, dir structure
alphastrata Aug 19, 2024
d6a2f5d
wip: it's working need to suss out the readme and some of the consts...
alphastrata Aug 20, 2024
218ad7f
ok that's probably good enough for a first pass...
alphastrata Aug 20, 2024
b3218bd
chore: spelling
alphastrata Aug 20, 2024
f72ebb0
chore: readme tweaks
alphastrata Aug 20, 2024
ac41f98
Merge branch 'gfx-rs:trunk' into trunk
alphastrata Aug 20, 2024
87ed862
chore: clippy and fmt
alphastrata Aug 20, 2024
126a996
chore: add self and changes to changelog.md
alphastrata Aug 20, 2024
50600c3
Merge branch 'trunk' into trunk
alphastrata Aug 23, 2024
4517673
fix: typo and remove env_logger via cfg flag for wasm builds (which t…
alphastrata Aug 31, 2024
c8328be
Merge branch 'trunk' into trunk
alphastrata Aug 31, 2024
8ec5d28
Merge branch 'trunk' into trunk
alphastrata Sep 8, 2024
e8bb012
Merge branch 'gfx-rs:trunk' into trunk
alphastrata Sep 17, 2024
207f8d6
Merge branch 'trunk' into trunk
alphastrata Sep 18, 2024
28fbd9c
Merge branch 'gfx-rs:trunk' into trunk
alphastrata Sep 24, 2024
7e8e42a
Merge branch 'trunk' into trunk
alphastrata Sep 29, 2024
85f5633
refactor: bring inline with newer wgpu
alphastrata Sep 29, 2024
451047b
refactor: bring inline with newer wgpu
alphastrata Sep 29, 2024
2d58b9c
chore: work on the readme a bit...
alphastrata Sep 29, 2024
38a44df
refactor: remove a bunch of everything, be simple
alphastrata Sep 29, 2024
164374b
wip: get a test going
alphastrata Oct 1, 2024
95a34e4
wip: remove unrequired pub(s)...
alphastrata Oct 1, 2024
67a0606
refactor: remove a bunch of everything, be simple
alphastrata Sep 29, 2024
a7d306e
wip: remove unrequired pub(s)...
alphastrata Oct 1, 2024
7d54d5f
Merge branch 'tmp' into trunk
alphastrata Oct 2, 2024
6c8e193
Merge branch 'gfx-rs:trunk' into trunk
alphastrata Oct 2, 2024
64f0697
chore: cleanups, typos, simplifying
alphastrata Oct 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ By @bradwerth [#6216](https://github.com/gfx-rs/wgpu/pull/6216).

#### General

- Added an example that splits up large data across multiple buffers and accesses them as a single contiguous array in the shader, demonstrating the naga/wgsl only `binding_array`, and some of the, less documented `wgpu::Features`. By @alphastrata in [#6130](https://github.com/gfx-rs/wgpu/pull/6138)

- If GL context creation fails retry with GLES. By @Rapdorian in [#5996](https://github.com/gfx-rs/wgpu/pull/5996)
- Fix profiling with `tracy`. By @waywardmonkeys in [#5988](https://github.com/gfx-rs/wgpu/pull/5988)
- As a workaround for [issue #4905](https://github.com/gfx-rs/wgpu/issues/4905), `wgpu-core` is undocumented unless `--cfg wgpu_core_doc` feature is enabled. By @kpreid in [#5987](https://github.com/gfx-rs/wgpu/pull/5987)
Expand Down
98 changes: 98 additions & 0 deletions examples/src/big_compute_buffers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# big-compute-buffers

This example assumes you're familiar with the other GP-GPU compute examples in this repository, if you're not you should go look at those first.

Showcases how to split larger datasets (things too big to fit into a single buffer), across multiple buffers whilst treating them as a single, contiguous buffer on the GPU.

- Creates a large set of buffers, by default 1GB, full of `0.0`s.
- Increments each element in said large buffer by `1.0`.
- Returns those `1.0` values as a back to the HOST.

The aim of this example is to demonstrate taking a large input dataset, splitting it into chunks for the purpose of moving it onto the GPU, but then treating it as a single contiguous data structure once on the DEVICE.

## To Run
As the maximum supported buffer size varies wildly per system, when you try to run this, then when it will likely fail, in-which-case read the error and update these `const`s accordingly:
>`src/big_compute_buffers/mod.rs`
```rust
const MAX_BUFFER_SIZE: u64 = 1 << 27; // 134_217_728 // 134MB
const MAX_DISPATCH_SIZE: u32 = (1 << 16) - 1; // 65_535
alphastrata marked this conversation as resolved.
Show resolved Hide resolved
```

It is recommended you enable the logger to see the code explain what it's doing.
```
RUST_LOG=wgpu_examples::big_compute_buffers cargo run -r --bin wgpu-examples big_compute_buffers
```

## Example Output

<details>
<summary>Output</summary>

```sh

[DEBUG wgpu_examples::big_compute_buffers] Size of input 1_073_741_824b
[WARN wgpu_examples::big_compute_buffers] Supplied input is too large for a single staging buffer, splitting...
[DEBUG wgpu_examples::big_compute_buffers] num_chunks: 8
[DEBUG wgpu_examples::big_compute_buffers] creating staging buffer 1 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating staging buffer 2 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating staging buffer 3 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating staging buffer 4 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating staging buffer 5 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating staging buffer 6 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating staging buffer 7 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating staging buffer 8 of 8
[DEBUG wgpu_examples::big_compute_buffers] Created staging_buffer
[WARN wgpu_examples::big_compute_buffers] Supplied input is too large for a single storage buffer, splitting...
[DEBUG wgpu_examples::big_compute_buffers] creating Storage Buffer 1 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating Storage Buffer 2 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating Storage Buffer 3 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating Storage Buffer 4 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating Storage Buffer 5 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating Storage Buffer 6 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating Storage Buffer 7 of 8
[DEBUG wgpu_examples::big_compute_buffers] creating Storage Buffer 8 of 8
[DEBUG wgpu_examples::big_compute_buffers] Created storage_buffer
[DEBUG wgpu_examples::big_compute_buffers] bind_idx:0 buffer is 134_217_728b
[DEBUG wgpu_examples::big_compute_buffers] bind_idx:1 buffer is 134_217_728b
[DEBUG wgpu_examples::big_compute_buffers] bind_idx:2 buffer is 134_217_728b
[DEBUG wgpu_examples::big_compute_buffers] bind_idx:3 buffer is 134_217_728b
[DEBUG wgpu_examples::big_compute_buffers] bind_idx:4 buffer is 134_217_728b
[DEBUG wgpu_examples::big_compute_buffers] bind_idx:5 buffer is 134_217_728b
[DEBUG wgpu_examples::big_compute_buffers] bind_idx:6 buffer is 134_217_728b
[DEBUG wgpu_examples::big_compute_buffers] bind_idx:7 buffer is 134_217_728b
[DEBUG wgpu_examples::big_compute_buffers] created 8 BindGroupEntries with 8 corresponding BindGroupEntryLayouts.
[DEBUG wgpu_examples::big_compute_buffers] set_pipeline complete
[DEBUG wgpu_examples::big_compute_buffers] set_bind_group complete
[DEBUG wgpu_examples::big_compute_buffers] buffers created, submitting job to GPU
[DEBUG wgpu_examples::big_compute_buffers] Job submission complete.
[DEBUG wgpu_examples::big_compute_buffers] Getting results...
[DEBUG wgpu_examples::big_compute_buffers] GPU RUNTIME: 2522ms
[DEBUG wgpu_examples::big_compute_buffers] All numbers checked, previously 0.0 elements are now 1.0s

```

</details>
w
## FAQ

### How do I ascertain the max_*buffer_binding_size?
You could write some code, or just run this example and it'll fail then see [below](#how-will-i-know-if-the-provided-default-is-too-high-for-my-gpu).
```rust
// get yourself a wgpu::Device
device.limits().max_buffer_size
````

### How will I know if the provided default is too high for my gpu?
You'll see this error:
```sh
thread 'main' panicked at wgpu/src/backend/wgpu_core.rs:3433:5:
wgpu error: Validation Error

Caused by:
In Device::create_bind_group, label = 'Combined Storage Bind Group'
Buffer binding 0 range 268435456 exceeds `max_*_buffer_binding_size` limit 134217728
```


### What's going on with the MAX_DISPATCH_SIZE, and the OFFSET etc in the shader?
There is a limit to the maximum number of invocations you can spool up in `workgroup_size(x,y,z)`, this means there's a limit, on how 'high' a number we can get a global_index.xyz value to count up to, if this limit is less than the number of `0.0`s in our buffers we need a way to count 'higher'.
Loading
Loading