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

Typed buffer #16

Merged
merged 3 commits into from
Sep 23, 2024
Merged

Typed buffer #16

merged 3 commits into from
Sep 23, 2024

Conversation

Firestar99
Copy link
Contributor

@Firestar99 Firestar99 commented Sep 23, 2024

Adds TypedBuffer<T> to explicitly declare an input as a buffer, instead of using the implicit declarations that have been used previously. The explicit declaration also enables you to declare an "array of buffer descriptors containing something" as is common in bindless.

Examples:

/// Examples (for an `#[spirv(storage_buffer)]`-annotated entry-point parameter):
single_u32: &u32, // implicit, 1 buffer of a single u32
many_u32s: &[u32], // implicit, 1 buffer of many u32s
single_u32: &TypedBuffer<u32>, // explicit, 1 buffer of a single u32
many_u32s: &TypedBuffer<[u32]>, // explicit, 1 buffer of many u32s
// Now possible to declare:
many_buffers_single_u32: &RuntimeArray<TypedBuffer<u32>>, // explicit, many buffers of a single u32.
many_buffers_many_u32s: &RuntimeArray<TypedBuffer<[u32]>>, // explicit, many buffers of many u32s.

glsl buffer declaration as a comparison:

// glsl does not accept structs directly and instead requires an "interface block" to be declared.
// It's looks similar to a struct (but is not a struct) that allows unsized arrays at the end.
layout(std430, binding = 0) buffer BufferType0
{
    int value;
} single_u32; 
layout(std430, binding = 1) buffer BufferType1
{
    int value[];
} many_u32; 

layout(std430, binding = 2) buffer BufferType2
{
    int value;
} many_buffers_single_u32[]; // !!!
layout(std430, binding = 3) buffer BufferType3
{
    int value[];
} many_buffers_many_u32s[]; // !!!
// Note the array declaration at the variable name (marked with !!!) declares the interface block 
// as many buffers, in rust-gpu you can now do this with `RuntimeArray<TypedBuffer<_>>`.

Originally made by @eddyb with tests and fixups by me.

Original embark PR's description

This PR adds a TypedBuffer<T> "handle" type to spirv-std (comparable to Image types in some respects). While not a perfect representation of the Vulkan/SPIR-V buffer model, we can't do much better for now.

With TypedBuffer<T>, we can now redefine the existing usage patterns:

#[spirv(fragment)]
fn main_fs(
    #[spirv(uniform,        descriptor_set = 0, binding = 0)]
    uniform_buffer: &MyConstData,
    #[spirv(storage_buffer, descriptor_set = 0, binding = 1)]
    storage_buffer: &mut [u32],
) {...}

as sugar1 for: 1 (which we will continue to support for the foreseeable future, presumably)

#[spirv(fragment)]
fn main_fs(
    #[spirv(uniform,        descriptor_set = 0, binding = 0)]
    uniform_buffer: &TypedBuffer<MyConstData>,
    #[spirv(storage_buffer, descriptor_set = 0, binding = 1)]
    storage_buffer: &mut TypedBuffer<[u32]>,
) {...}

(comparable to buffer { uint data[]; } storage_buffer; in GLSL)

That is, plain Rust references are used to describe a single buffer, with the Rust type (MyConstData or [u32]) being for the buffer contents, used to interpret the raw bytes (provided to the host, e.g. Vulkan, API).

Because the "buffer handle" is made explicit, specifying multiple buffers is unambiguous:

#[spirv(fragment)]
fn main_fs(
    #[spirv(uniform,        descriptor_set = 0, binding = 0)]
    uniform_buffers: &RuntimeArray<TypedBuffer<MyConstData>>,
    #[spirv(storage_buffer, descriptor_set = 0, binding = 1)]
    storage_buffers: &mut RuntimeArray<TypedBuffer<[u32]>>,
) {...}

(comparable to buffer { uint data[]; } storage_buffers[]; in GLSL)

In other words, each TypedBuffer Rust value corresponds exactly to one buffer descriptor (e.g. VkBuffer).
EmbarkStudios/rust-gpu#1014

Changelog

### Changed 🛠
- [PR#16](https://github.com/Rust-GPU/rust-gpu/pull/16) add `TypedBuffer<T>` to explicitly declare buffers, allows declaring an "array of buffer descriptors"

@LegNeato LegNeato merged commit 83c7e8d into Rust-GPU:main Sep 23, 2024
7 checks passed
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.

3 participants