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

Add buffer pointer support for WGSL (SPV_EXT_physical_storage_buffer) #2457

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

felipeagc
Copy link

This will add support for the PhysicalStorage SPIR-V storage class, which will enable use of the VK_KHR_buffer_device_address Vulkan extension.

Here's some example WGSL code for what this looks like:

struct Buffer {
    data: vec4<f32>,
}
struct PushConstants {
    buf: ptr<buffer, Buffer, 16>, // Pointed-to type must have 16-byte alignment
}
var<push_constant> pc: PushConstants;

// Accessing the buffer
var data = (*pc.buf).data;

The name of the WGSL address space is buffer and the third parameter for the pointer is the alignment of the pointed-to Buffer, which affects load operations on the buffer. It is optional and defaults to 16, as per the GLSL spec.

The size and alignment of pointers in this storage class is 8 bytes, so I also had to change that.

One thing this would allow for and that I did not implement is recursive pointers:

struct Buffer {
    data: f32,
    next: ptr<buffer, Buffer>,
}

@felipeagc felipeagc force-pushed the master branch 2 times, most recently from ea5fb5c to 0d8cb28 Compare August 25, 2023 20:34
Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

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

Thanks - this looks very interesting.

src/lib.rs Outdated Show resolved Hide resolved
src/proc/layouter.rs Outdated Show resolved Hide resolved
src/proc/mod.rs Outdated Show resolved Hide resolved
src/valid/mod.rs Show resolved Hide resolved
src/valid/type.rs Outdated Show resolved Hide resolved
src/valid/type.rs Outdated Show resolved Hide resolved
src/valid/type.rs Show resolved Hide resolved
@jimblandy
Copy link
Member

@felipeagc Just out of curiosity - what are you using this new Capability for?

@felipeagc
Copy link
Author

@felipeagc Just out of curiosity - what are you using this new Capability for?

I'm writing a Vulkan renderer and wanted to use buffer pointers to simplify the design a bit. But since I was using naga to compile shaders, I needed to add the feature to the compiler! :)

I don't know if this kind of feature is too out of scope for naga, as far as WebGPU is concerned, but since I implemented it for myself I figured I should try to upstream it so other people can use it.

Allows compilation of this kind of access:
```
struct OtherStruct {
    data: float,
}
struct MyBuffer {
    sub_buf: ptr<buffer, OtherStruct>
}
struct PushConstants {
    buf: ptr<buffer, MyBuffer>,
}
var<push_constant> pc: PushConstants;

var d = (*(*pc.buf).sub_buf).data;
```

Before you would have to use intermediate variables:
```
var b = (*pc.buf);
var d = (*b.sub_buf).data;
```
@cwfitzgerald
Copy link
Member

Hello, thank you for your PR against Naga!

As part of gfx-rs/wgpu#4231, we have moved development of Naga into the wgpu repository in the Naga subfolder. We have transferred all issues, but we are unable to automatically transfer PRs.

As such, please recreate your PR against the wgpu repository. We apologize for the inconvenience this causes, but will make contributing to both projects more streamlined going forward.

We are leaving PRs open, but once they are transferred, please close the original Naga PR.

@exrook
Copy link
Contributor

exrook commented Nov 2, 2023

@felipeagc As I'm also interested in using these features, I've rebased your PR branch on the post-merge wgpu repo here: https://github.com/exrook/wgpu/tree/buffer_pointer

@junglie85
Copy link

Hi. Just wondering what the status of this PR is as I’d like to use this feature too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants