Thus, I'm curious to see how Rust will tackle it. Will it try to enforce the portability and safety guarantees with a type system, or will it specifically lower the guarantees compared to native code (thus keeping the syntax of Rust but not the philosophy).
Anyhow, amazing work by Embark, and a stellar example how to collaborate in open source!
Everyone was yelling this at you since the beginning. Ever since Apple proposed "safe HLSL", and then it morphed into WHLSL and then WLSL and then the competing Tint proposal which is now WGSL, everyone (Adobe, Valve, Epic, Unity, NVIDIA) was saying "none of this will do what you think it does, we've all been here before and it's very very hard, at the very least, get some IHVs or at least a team that does port work / emulation tech on board".
The committee chose not to listen, instead forcing WebGPU down this hell that nobody really wants or appreciates. The whole WebGPU is three years late and still has no users, despite being one of the most complicated specs to land in years. And yeah, I'm still a bit bitter about it.
I am also skeptical of rust-gpu's approach, because I don't think it's possible to take a CPU-inspired language design and blindly adapt it to GPUs without some difficult questions to answer (they still don't have flow control, but I'm told that's coming).
But at the very least, I think repi and Embark know what they're doing and know about the tough problems, rather than feigning being blindsighted.
We need to stop trying to make software mitigations and start putting pressure on hardware designers.
That is to say, WebGPU is working with IHVs indirectly via Khronos, Microsoft, etc, and is putting pressure to introduce the safety features at the driver/hardware level. We've been arguing for more defined UB in Vulkan specs, initialization of workgroup memory, device memory ownership, and other things.
From my understanding of GLSL, it seemed like the hardware was in control of scheduling, and the driver that compiled the shaders was the one inserting locks, fences, etc.
GPUs seem like a bad fit for run-time exceptions and return value checking (? operator?) since they are so poor at branching, being SIMD. The more you can check ahead of time, the better, and Rust isn't such a bad fit there.
But given that Rust's main advantages seem to be the ability to safely control multiple execution threads, and its handling of edge cases, I wonder if the first is going to be used at all, and if the second isn't going to lead to extra instructions?
Rust is different enough from GLSL and HLSL (most game engines use HLSL as "native" and cross compile to GLSL, MSL or use header defines to build PSSL) that introducing a new shading language compatible with Rust is probably worth the effort for them.
For instance it's possible to share data structure between the CPU and GPU side directly from the same source code, or you can run the same code on the CPU or GPU for whatever reason (debugging, unit testing etc...).
The Metal API uses C++ (with some extensions) as shader language, which IMHO is complete overkill too, but one nice side effect is that you can put shared data structures into headers, and then include the same shared headers on the CPU and GPU side.
Ideally it should be possible to mix GPU and CPU code in the same source file, and only (for instance) annotate GPU functions with an attribute which causes it to be compiled as shader code.
Focus on Vulkan graphics shaders first, then after Vulkan compute shaders
So no compute support yet. Also, Rust-GPU's focus is writing shaders in Rust, in order to support a game engine, so the focus will be real-time graphics and simulation. It seems like it will never evolve into a CUDA like heterogeneous environment that's more common in scientific computing and ML.
I don't think anyone would be opposed to evolving the project into a strong alternative for scientific computing and machine learning!
That’s not a very compelling value proposition. You don’t need to switch languages to use a package manager.
Plus, not all engines have the same data layout, so an “effect crate” would either require your engine to work a very specific way, or would require integration work that would probably defeat the purpose of using a package manager in the first place.
And what does Rust itself add that GLSL/HLSL don’t offer? It can’t be worth the headaches, especially when you consider how a TON of devices still only support OpenGL, which not only requires runtime compilation of shaders (so only GLSL unless your app ships a cross compiler), but different drivers behave differently, requiring ugly hacks for the sack of performance in some cases.
You could say the same thing about nodes in shader graph systems, but in practice they work quite well.
> It can’t be worth the headaches, especially when you consider how a TON of devices still only support OpenGL, which not only requires runtime compilation of shaders (so only GLSL unless your app ships a cross compiler)
You can AOT compile SPIR-V to GLSL with SPIRV-Cross.
what does the amd64 assembly look like for "telling the GPU to do something"?
Is there an amd64 instruction that says "Tell the GPU to start executing here?"
Or is it at the OS layer instead of the assembly layer, with an OS syscall that defers to the GPU?
I don't know enough about PCIe to be an authoritative source, but I'll just go along and describe the way most microcontrollers and peripherals work. I doubt x86 has instructions dedicated to PCIe. There will likely be a PCIe controller memory-mapped to some hardware address, that the OS will write to as if it was regular memory (that address is physically wired to the controller, be it on the CPU die or on the motherboard). The address is generally specified by the BIOS/UEFI (devicetrees on most embedded platforms).
Of course, what you will write will depend on the controller itself, but the OS will have a driver that contains that information. You have a back-and-forth to list peripherals on the bus, identify the one you want to communicate to, set-up access mode (direct memory access where you map an area of your memory to the peripheral, or just sequential access trough the controller). Once you can communicate with the GPU, you start the same dance to give it a program you compiled ahead of time, have it run, and fetch the result.
I don't have low-level experience with that specific part of the system, but that kind of stuff is generic enough that you find the same patterns everywhere, at least for turing machines, which x86 CPUs are.
https://compas.cs.stonybrook.edu/~nhonarmand/courses/sp17/cs...
The actual details of the communication is documented by some IHVs, either through PDFs or source code. Normally, the CPU doesn't wait for programs to complete, but instead, the GPU driver has a list of tasks to run (a "command buffer") that it itself can schedule across many different pieces of hardware, and a combination of the driver and GPU itself determine scheduling, execution, and so on. Note that there's still a lot of work for the driver to do, including managing the device's memory (textures, buffers, so on), compiling any programs into machine language (similar to a JIT), and coordinating multiple different programs trying to use the GPU.
Yes. Modern OSes don’t allow userspace code to directly access peripheral devices, GPUs included. Due to the enormous complexity of modern GPUs (GPUs typically have more transistors, consume more electricity, and have comparable amount of memory), the API surface is huge.
On Windows, the native API surface is Direct3D. It has two large pieces, user-mode and kernel mode. The vendor-provided GPU driver is similarly split in two.
User-mode parts implement HLSL compiler (Microsoft), another downstream compiler to compile DXBC into proprietary byte code GPUs actually run (vendors), implement other higher-level stuff like mipmap generation (vendors, at least for nVidia), and expose user-facing APIs, i.e. multiple versions of D3D (Microsoft).
Kernel mode driver interfaces with actual hardware, the Microsoft’s vendor-agnostic part of that is in dxgkrnl.sys.
Various GPU drivers often expose extra user-facing APIs in addition to Direct3D (CUDA, Vulkan, OpenGL, OpenCL), but all of them are optional.
This depends a lot on the OS and on the peripheral, but this is certainly not true across the board.
RDMA verbs, for example, definitely involve userspace directly communicating with the NIC, with no kernel involvement.
At least some of the AMD GPU graphics stacks support userspace submission to the GPU also, see http://www.hsafoundation.com/html/Content/Runtime/Topics/02_....
This requires hardware support, like an MMU and ensuring bad submissions from userspace can't do bad things to other processes, but lots of hardware supports these things.
The windows graphics stack does require going into the kernel to submit work to the GPU, but that isn't the case for all operating systems and all peripherals.
somewhat related: GPUs started getting support for virtual memory in ~2006 or so [1] but the windows graphics stack was improved to actually use that capability in Windows 10, ~10 years later [2].
[1] https://www.anandtech.com/show/2116/2 [2] https://en.wikipedia.org/wiki/Windows_Display_Driver_Model#W...
Interesting.
> but the windows graphics stack was improved to actually use that capability in Windows 10
No, it happened much earlier, in WDDM 1.0 i.e. Windows Vista: https://en.wikipedia.org/wiki/Windows_Display_Driver_Model#V...
I was under the impression that user-space drivers (like Linux UIO) were given an address that they can mmap for direct reads/writes from/to the peripherals address space. Is this not "direct"?
Kernel-mode drivers indeed do that under the hood, but the majority of GPU I/O bandwidth doesn’t go that way.
With DMA, GPUs have full access to system memory. They have specialized piece of hardware (exposed to programmers as copy command queue in D3D12, or transfer queue in Vulcan) to move large blocks of data in either direction between system RAM and VRAM.
That is, from a x86-64 assembly point of view, it's just writing to memory. Usually, only the final write to the special register is privileged, so that write will need an OS syscall into the graphics driver; the rest can be written directly by the userspace driver (which used beforehand another OS syscall to get access to some of that memory shared with the GPU). The userspace driver also compiles the SPIR-V programs into native GPU code, which can be loaded and executed as instructed by the list of commands.
------------
When it comes to I/O, there are two strategies: memory mapped I/O, and I/O ports (the "in" and "out" assembly instructions). I'm pretty sure most modern hardware is memory-mapped type, where you just read to (or write from) certain "memory" locations.
Its not really memory (it doesn't go to RAM), but instead to some other component, like PCIe root complex for further processing.
------
Something to note: the whole OSI level exists on the motherboard. You've got physical connections, you've got link-layer connections, and even a network similar to TCP/IP. In fact, there are multiple networks: SATA networks, PCIe networks, and USB Networks, that have their own addressing rules and physical protocols. Different USB Ports, different PCIe addresses, and more!
From the perspective of the CPU, I expect any "GPU Command" to simply be a link-level call to the PCIe root complex. You send data to the PCIe controller, which then forwards the data to the GPU. Or NVMe RAM, or your network card.
To fully make a GPU Command get to the GPU, you need to know the PCIe address, and route the message appropriately (there could be PCIe switches in between). Finally, there's a protocol, similar to application-level code (HTTP) where applications talk to the GPU.
Given the similarities between DirectX, OpenCL, Vulkan, Metal, and CUDA/HIP, I expect that GPUs all have roughly the same "application" interface. Shaders are compiled into machine code. Machine code is loaded into GPU VRAM. Command Queues issue remote-function calls with an event-driven dependency graph that the GPU selects kernels from. Etc. etc.
That HLSL/GLSL code is tightly coupled to format and bindings of the input and output data (textures, samplers, constant/uniform buffers, render targets, etc.) These are defined by the combination of non-shader assets, and the CPU-running code which implements the rest of the graphics engine.
I’m not sure how different shader language is going to help?
Can Rust be used also for compute? It would be a great way to dislodge CUDA lock-in, since something in Rust could be a lot better option than either CUDA or OpenCL.
It can, but it's worth noticing that the programming paradigm is different. For this reason, I would assume you are still writing a DSL within Rust which can use Rust's type checker to help you figure out what to write and how, but Rust in itself cannot represent the same semantics as-is that is required to write efficient parallel code. So, you still need to be a domain expert. But, automating the bytecode writing is alleviated by Rust's ability to provide packages etc.
I refactored some OpenMP code to run on SPIR-V using the Vulkan API the OP project uses, and I must say that I cannot see myself doing it in any other language than Rust.
calling it Accelerated Massive Parallelism (AMP)
https://en.wikipedia.org/wiki/C%2B%2B_AMP
It generated a lot of buzz in the Windows world around that time, but as far as I can tell never went anywhere.