EmbarkStudios / rust-gpu

🐉 Making Rust a first-class language and ecosystem for GPU shaders 🚧
https://shader.rs
Apache License 2.0
7.34k stars 245 forks source link

Investigate custom `repr` for GPU memory #11

Closed Jasper-Bekkers closed 1 year ago

Jasper-Bekkers commented 4 years ago

A few options here:

repi commented 4 years ago
* Support `#[repr(Rust)]` 

This is the richest one and most ideal one if/when we have Rust both on the CPU and GPU side.

But also this representation has explicitly no guarantees about ABI compatability or stability between compiler version, so to be safe one would have to have exactly the same rustc compiler for the CPU code and the GPU code to have this match, which is likely unfeasible for a long time.

Need a representation that has a stable ABI, do still hope that Rust in the future will have a subset that can give ABI stability guarantees.

repr(C) is likely the only choice on the Rust CPU side for now and until then.

Jasper-Bekkers commented 4 years ago

From @h3r2tic #[repr(Rust)] would probably also make the SPIR-V code platform specific.

khyperia commented 4 years ago

What exactly does this affect? Things that I can think of:

  1. Calling spir-v compiled by other languages from Rust
  2. Binding layout?
  3. Accessing binary buffer blobs passed into shader (e.g. array of structs)

1 is not on the roadmap. If 2, pointing out explicitly where and what cases it can happen would help. If 3, I wasn't aware that shaders could access binary buffer blobs (I don't think you can in glsl?) - I'd appreciate a pointer in the spec.

h3r2tic commented 4 years ago

If you mean repr(Rust), then it's not very compatible with anything; even two identical structs can be incompatible with each other: https://doc.rust-lang.org/nomicon/repr-rust.html

khyperia commented 4 years ago

Yes, but does that actually affect anything? What ABI boundary are we going through where that will be a problem?

h3r2tic commented 4 years ago

It's probably something for @Jasper-Bekkers :P What was the intended use case? The issue lists possible options, but not motivations.

I was going under the assumption that this would be for buffers (constant, uniform) that want to share types between the CPU and GPU, and access them on the GPU via something similar to Shader Storage Buffer Objects, but with Rust idioms/semantics. On the codegen side, this could end up generating typed loads/stores, or map to simple DWORD fetches like Buffer Textures.

For example, here I have a shader that does a ton of manual unpacking of ray-tracing acceleration data and mesh geometry via bindless buffer textures. Automating that would be great -- but again, I'm not sure if this is the intended motivation behind this issue.