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 a way to index into array pointers with custom element alignment #5873

Open
maxime-modulopi opened this issue Dec 15, 2024 · 5 comments
Open
Labels

Comments

@maxime-modulopi
Copy link

In GLSL, buffer_reference_align can be used to specify a custom alignment:

  • For GLSL_EXT_buffer_reference, pointers are assumed to point to a single element T, whose address is aligned to buffer_reference_align.
  • For GLSL_EXT_buffer_reference2, pointers can point to an unsized array of elements T, where each element is individually aligned to buffer_reference_align.

I believe this is not currently possible in Slang.

I see two ways in which this use case could be implemented:

  1. GLSL style (buffer_reference_align), where we can specify the element alignment/stride at the pointer level
  2. C++ style (alignas()) where we can specify the alignment at the type level
@csyonghe
Copy link
Collaborator

This isn't possible right now. Before we are able to add support for this feature, the work around is to wrap the element type in a wrapper struct that pads the size to alignment boundary, and then use loadAligned/storeAligned to ensure aligned load stores are generated.

@bmillsNV
Copy link
Collaborator

Thank you for filing this issue. This is worth resolving, but we don’t have current available bandwidth – moving to the backlog for now. Please do feel welcome to propose a solution if you’re able to before it makes it out of the backlog!

@csyonghe
Copy link
Collaborator

The easiest path to allow this in the Slang compiler is to add a [Align(x)] attribute that works on struct types, so you can do:

[Align(16)]
struct Element
{
     float2 vec;
}

uniform Element* ptr; // element will have stride 16 and pointer access is aligned to 16 bytes.

If this works for you, we should be able to implement this relatively quickly.

@maxime-modulopi
Copy link
Author

This is the C++ style I mentioned so yes this would work perfectly!

Wouldn't this also completely replace the need for loadAligned and storeAligned?

@csyonghe
Copy link
Collaborator

LoadAligned/storeAligned is simply a lower level concept so you can do aligned access without any type wrappers.

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

No branches or pull requests

3 participants