-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Rust-GPU support #5634
Comments
We've been following these efforts actively. What exactly would you envision this looking like? I suspect that this is ultimately a question for |
Doesn't wgpu already support SPIR-V as input? |
People have already played rust-gpu in Bevy on platforms that supports SPIR-V by using passthrough in Bevy. Since, Naga frontend for SPIR-V has improved, so maybe it could work on most platform. But it was somewhat painful to use due to the need of doing two compilation passes or having a build script handling that for you |
@alice-i-cecile @bjorn3 According to the
Since Now, if all the gobbledygook were implemented in the engine itself, it could enhance the user experience greatly by unifying the graphics programming with everything else under one simple and powerful system. This seems perfect for the model of "Rust supremacy" that Bevy proudly displays. (Which I totally agree with). Also, it enhances user experience and promotes indie game development by only requiring the developer to learn a single language. |
Bevy consider shaders as assets. Once we have asset preprocessing and baking, shader compilation will just be one of those and shouldn't need anything extra |
Rust-GPU requires a specific nightly compiler. I don't think we can handle this transparently. At least not when rustup isn't used. |
@mockersf I am not sure what you mean. There are plenty of cases, especially in 3D games where a developer would need to write custom shaders to achieve his vision. What are you trying to say? I'm worried I've completely misunderstood you. |
@bjorn3 Notice how I specifically pointed out in the issue post that |
Kind of. Yes it is probably best to wait, but I don't think Rust-GPU will ever ship with rustc and as such it will always require nightly rustc due to depending on unstable rustc apis by definition as alternative codegen backend. |
@bjorn3 Those APIs won't always be unstable, will they? or do you mean unsafe? Also, you mentioned using rustup. What's wrong with that? (forgive me, I haven't used rust in over a year. I'm a little... low on practice. don't really remember how the compiler system works.) |
They will always be unstable. Alternative codegen backends depend on the internal crates that make up rustc which regularily change. Stabilizing these interfaces would effectively require stopping development of rustc I think. Many changes are necessery either to implement new features or to fix bugs. The only thing that may allow codegen backends to work on stable would be to implement a new interface just for codegen backends which can stay the same even if the rustc internals change. The stable-mir initiative may result in such an interface, or it may be deemed infeasible to use this interface for codegen backends due to abstracting too much away. |
I'm not so sure about that. Rustc relies on LLVM, and as it happens, they started to upstream SPIR-V as a backend for LLVM 15. So, it may be that rustc at some point supports SPIR-V as a regular target and by extension rust-gpu will become stable. source: https://www.phoronix.com/news/LLVM-15-SPIR-V-Backend |
This is a very promising idea! The real challenge here is with the build process. Since we're dealing with nightly anyway, we might as well take advantage of metabuild, another unstable feature. Here's the gist: Bevy would export a function called Why use metabuild? Because you can already use The In case you were wondering, here's what a Bevy app's cargo-features = ["metabuild"] # Enable metabuild
[package]
name = "mygame"
version = "0.1"
metabuild = ["bevy"] # Use Bevy's metabuild()
[dependencies]
bevy = {version = "0.8", features = ["rust-gpu"]}
[build-dependencies]
bevy = {version = "0.8", features = ["rust-gpu"]} # Bevy is now a build dependency as well
# Not strictly necessary, but it speeds up shader compilation a lot
[profile.release.build-override]
opt-level = 3
codegen-units = 16
[profile.dev.build-override]
opt-level = 3 Bevy already requires messing around with I should point out that metabuild and |
There is precedent for this: |
@x-52 That does sound promising! Maybe we could set up a sort of experimental branch of bevy that requires nightly rustc, instead of adding |
Also, on a slightly unrelated note, I was wondering how I would go about using SPIR-V with Bevy's |
@thedocruby It is very similar. If your SPIR-V is stored in a file, then you can use it just like any other WGSL or GLSL shader. On the other hand, If you're using the aforementioned |
Also, It may turn out that implementing
So, Once bevy gets to a more stable place in development, there might be enough incentive and opportunity to implement support for |
|
There isn't anything to add in Bevy for @thedocruby you would be more than welcome to do an example or using the form you prefer on how this would currently work and what could be improved, but as for "rust-gpu support", it's already supported 👍 |
As others have mentioned, we'd like to support We've considered rust-gpu in the past for Bevy's "recommended shader language" (ex: what we use for built in bevy crates, what we document / support, etc). However I'm personally largely against that for a number of reasons:
|
Edit: Cleaned up my thoughts into an issue (#7771) and pull request (#7772). A few points I've noted in recent
Code sharing between Bevy's WGSL implementation and Rust shaders is - naturally - nonexistent, so if you want to leverage the lighting system or PBR in a Rust shader, you have to implement from scratch. That's quite doable (see below), but raises the question of how viable using rust-gpu with bevy would be in the long term (i.e. if-and-when-rust-gpu-is-stable) for someone who also wants to use the PBR support code. For other users interested in using |
Some further thoughts re. @cart's commentary on runtime shader compilation: In theory baking rustc into a game is feasible, since it can be imported as a crate and driven programatically, though as far as i know is in a "never stable" state where you have to pick a nightly version and deal with possible API breakage. I suppose that's less an issue if rust-gpu also requires it, but there are probably more issues to hack through w.r.t. making it work this way since all of its use case doco is cargo-focused. Needs investigating, but would doubtless end up being project-domain instead of engine-domain one way or the other due to touching nightly. On the flipside, there's something to be said for compiling statically in cases where it's a good fit. Recent high-profile PC releases have taken a lot of flak for stutter introduced by on-demand shader compilation, some of which introduced a precompilation stage to frontload the compile time as a workaround. Arguably that's not an ideal fix, since the user has to wait on it. I expect there are certain cases where on-site compilation is a boon (vendor or card -specific extensions and optimizations that need to be conditional over the user's hardware, for example), but I think more common-or-garden cases could dodge the problem with appropriately-designed static compilation machinery. Ex. By enumerating the shader variants required by a given game at edit-time, using that data to precompile a single SPIR-V module with appropriately-named entrypoints, which are then looked up during game runtime based on a hash of bevy's shader defs. I'll hack on it and see if I can come up with an example. |
I return bearing workflow! As expected, trying to brute-force permutate a complex shader (i.e. one with more than 8-or-so binary compile-time parameters) is non-viable; not only does it take an extremely long time, it also crashes the SPIR-V codegen backend with an ID out-of-range error. However, hot-recompiling requested entry points on demand has proven quite viable: To wit, my example workspace has evolved into Bevy Rust-GPU; a suite of crates designed to support the workflow pictured above. It remains subject to the issue linked above, as well as lack of read-only storage buffer support, but is otherwise quite usable, and able to support on-demand permutation of shaders as complex as |
The Bevy Rust-GPU suite has been upgraded to bevy 0.10. |
Any updates on this? |
What problem does this solve or what need does it fill?
rust-gpu
is an admirable open-source effort to implement arustc
compiler backend for generating SPIR-V intermediate code. Embark Studios is already actively using this project to generate Vulkan shaders from Rust for an in-house project. Seeing that WebGPU also uses SPIR-V as an intermediate, support for WebGPU is a likely eventuality. This would allow a single language to be used for an entire project, with support for compiling to desktop, mobile, and web platforms. And not just any language, but Rust, the king of languages.In the words of the developers:
rust-gpu
seems to fit perfectly with Bevy's mission and values. It would be terribly disheartening if Bevy never supports the project.What solution would you like?
Rust-GPU might be a bit too early in development for Bevy to fully support it, but even if you decide not to implement it anytime soon, It is definitely something you'll want to do at some point. So, any restructuring of the Bevy internals that would need to be done to support the project should be done slowly and early-on, to reduce the amount of major changes needed to add support down the line.
Or, if you feel like
rust-gpu
support could be easily implemented starting now, without concerns for stability issues, then by all means please do so. I don't really know the structure of Bevy internals atm, so y'all probably are a better judge of that than meWhat alternative(s) have you considered?
There is always the option to explicitly NOT support the project, but in my opinion,
rust-gpu
really looks like it is the perfect fit for the bevy project, and a great addition to the still-being-worked-on graphics and shader system. Of course, I'm not a Bevy developer, so I could be wrong.Additional context
You may have heard of RLSL, an archived and abandoned project that had a similar mission. I'll have you know that the creator of RLSL is one of the minds behind
rust-gpu
, and has taken everything he learned from the previous project and applied it to this new incarnation of his original mission.The text was updated successfully, but these errors were encountered: