Better HN
topnewbestaskshowjobs
Log inSign up

raphlinus

14,081 karma · joined Mar 7, 2014 (12y ago) · 1,887 submissions

I do research on fundamental UI technology and 2D graphics, with a focus on Rust and fonts. @raph@mastodon.online
Submissions

comment 4y ago · link · parent

I agree with you that dynamically instantiating shader permutations is bad news. That said, I think the author is probably onto something regarding linking and closures. The existing "binding model" for shaders is incredibly tedious and low-level (not to mention frustratingly not-quite-portable), and I think it's likely that there's a better way to do it.

comment 5y ago · link · parent

I don't really agree. I think it's completely valid to explore a GPU architecture in which rasterization is done in software, with a perhaps a bit of support in the ISA. That's what they've done here, and they do demonstrate running OpenGL ES.

The value of this approach depends on the workload. If it's mostly rasterizing large triangles with simple shaders, then a hardware rasterizer buys you a lot. However, as triangles get smaller, a pure software rasterizer can win (as demonstrated by Nanite). And as you spend more time in shaders, the relative amount of overhead from software rasterization decreases; this was shown in the cudaraster paper[1].

Overall, if we can get simpler hardware with more of a focus on compute power, I think that's a good thing, and I think it's completely fine to call that a GPU.

[1]: https://research.nvidia.com/publication/high-performance-sof...

comment 5y ago · link · parent

This is a research project from Georgia Tech. There's a homepage at [0] and a paper at [1]. It is specialized to run OpenCL, but with a bit of support for the graphics pipeline, mostly a texture fetch instruction. It appears to be fairly vanilla RISC-V overall, with a small number of additional instructions to support GPU. I'm very happy to see this kind of thing, as I think there's a lot of design space to explore, and it's great that some of that is happening in academic spaces.

[0]: https://vortex.cc.gatech.edu/

[1]: https://vortex.cc.gatech.edu/publications/vortex_micro21_fin...

comment 5y ago · link · parent

The specific thing I've found least documented, yet most important for the distinctive percussive attacks of the DX7, is a random variation of the pitch envelope for the first few milliseconds of the note. That's almost short enough it could be done in the firmware, but I believe it might be in the hardware. It's not present in the msfa source, but might have been recovered by later Dexed authors (I haven't carefully looked at their code).

If you get to the envelope hardware, you'll find it's just as clever as the exponential and sine generators. There's some info at [1], but it doesn't capture every single thing I found - there are cases where there is a slight amount of additional noise in the amplitude, I'm not sure whether intentional to give more character or an unintentional artifact. That's also missing from the msfa source.

[1]: https://github.com/google/music-synthesizer-for-android/blob...

ETA: Also see https://levien.com/dx7-envelope.html for a somewhat interactive JavaScript implementation of the envelope algorithm. This accurately emulates the envelope shape and quantization of the DX7 (ie it uses the same reduced number of bits to drive its state machine), but I do not claim it is bit-perfect.

comment 5y ago · link · parent

I believe you will find two main sources of differences in tone between the various DX7 emulators. One is that there are fairly major differences between the original DX7 and the DX7 II (I used the latter for the original engine now adapted in Dexed). The other is the analog filter on output, approximately a 16kHz lowpass filter designed to reduce artifacts from the DAC (this is replaced by a more general Moog-style filter in the Android implementation but present as an accurate emulation in Dexed).

I think Dexed is quite accurate, but this work will allow the authors to take it to the next level. I suspect most people won't be able to hear the difference, however.

comment 5y ago · link · parent

The Open CL 1.2 barrier() function is threadgroup scope, same as threadgroup_barrier on Metal. OpenCL 2.0 introduced a proper barrier function (work_group_barrier), which takes a memory scope parameter, which can be memory_scope_device (all this is pretty similar to the Vulkan memory model, and at least some of the same people worked on both). I know of no way to reliably support those semantics on Metal.

Claiming OpenCL bitcode is "forward compatible" is a pretty strong claim considering that OpenCL has been deprecated for over 3 years, and the main thing you get when searching OpenCL docs on the Apple site is an exhortation to migrate to Metal. To the extent there's a forward compatibility guarantee for AIR, I'm sure it only applies to output generated by official Apple tools, and I'm pretty sure by now there's no way to get those to output a device-scope barrier.

comment 5y ago · link · parent

Thanks very much for this detailed information, it's very helpful. I've filed https://github.com/gfx-rs/metal-rs/issues/222 to track it in the Rust ecosystem side.

comment 5y ago · link · parent

This is done in rust-gpu[1] by the `spirv(workgroup)` attribute on the kernel function signature.

[1]: https://github.com/EmbarkStudios/rust-gpu/blob/46c9ea0c9c7b7...

comment 5y ago · link · parent

While this might work on your specific device, it is an undocumented interface and there is no guarantee at all for future compatibility.

If you do get a definitive answer from Apple you can share, please follow up here, as I would like to be able to cite it. I would be quite shocked if it's different than what I just said, though.

comment 5y ago · link · parent

Specifically, I suspect one main reason they've kept it as long as they have is for WebGL support in Safari. Now they are investing in Angle[1], so going forward I expect that to be the primary path to WebGL compatibility.

[1]: https://docs.google.com/presentation/d/1ilUE_r5WNGlRhfGx71XH...

comment 5y ago · link · parent

By "swift-style refcounting" I mean the object is reliably deallocated exactly when the last release is called. Following up on the response to comex, I would say I would get these semantics if I called objc_retainAutoreleasedReturnValue on the allocating method's return value, and it actually worked.

comment 5y ago · link · parent

Thanks, this is helpful. The specific method that's causing me trouble right at the moment is "computeCommandEncoder"[1], which is a method off MTLCommandBuffer, and I think not in the "new" class. In the Rust bindings[2], this is just a msg_send! and from what I can tell is getting an autoreleased reference, not a retained one.

It looks like objc_retainAutoreleasedReturnValue might be exactly what I'm looking for, even if it isn't 100% guaranteed; if I'm understanding, it would be safe, and wouldn't actually leak as long as you had an autorelease pool somewhere in the chain. However, I'm not seeing a binding to it in the objc crate[3]. Sounds like maybe I should file an issue?

Also, given that such a method seems obviously useful, I wonder why it's not being called from these C++ bindings?

[1]: https://developer.apple.com/documentation/metal/mtlcommandbu...

[2]: https://github.com/gfx-rs/metal-rs/blob/master/src/commandbu...

[3]: https://docs.rs/objc/0.2.7/objc/runtime/index.html

comment 5y ago · link · parent

I totally get where you're coming from, Metal is much easier to program than raw Vulkan. I think a Metal-like API over "modern" APIs could be a good thing. There are things like the Vulkan Memory Allocator that help, but among other things that makes integration of different modules harder; this is especially nice on Metal because you can just allocate your own stuff off MTLDevice.

That said, there would be challenges, and big ones, the more you tried to make it actually Metal compatible. You'd need a C++ compiler for the shaders, and certain idioms (pointers, scalar types other than 32 bits) would be difficult to translate portably.

And as adamnemecek points out, the role of a friendly cross-platform GPU layer is largely being occupied by WebGPU (wgpu in particular in Rust-land). That'll be very comfortable for people used to Metal, as there's been lots of input from Apple folks. However, WGSL is much lower level than Metal Shading Language, and there are lots of important capabilities (subgroups, etc) missing that Metal and other advanced (>DX11) APIs can do.

comment 5y ago · link · parent

Right. Would you happen to know if there's a way to make a retained object actually deallocate on last "release," other than putting an autorelease pool around it? Would you also happen to know if the Swift bindings have this same wonky autorelease behavior, or more straightforward Swift-style refcounting?

comment 5y ago · link · parent

"Subset" is not the right word here. Vulkan has pointers now, but (as was discussed in a recent thread), there are serious limitations compared with "real" C++. At the same time, Metal has its own limitations, not least of which it's lacking acquire/release semantics on atomics and a device-scoped barrier.

OpenCL is a little strange because older variants don't have advanced atomics (or subgroups), but does have pointers. I'd be curious to know what specific thing is not available on DX12 and Metal but missing in Vulkan, especially because I'm not aware of any DX12 feature on the critical path for OpenCL that's missing from Vulkan (at least as an extension).

comment 5y ago · link · parent

Along similar lines, from my skim of the README, it also seems like deallocated things are not actually released until the return from the nearest autorelease block, which is not exactly convenient for Rust (and it's not properly safe either without at least an extra retain). I always imagined that when you call through Swift it's a more native memory management interface, but perhaps it's also the same Obj-C layer under the hood.

comment 5y ago · link · parent

+1 to GPGPU specifically. It feels like there is enormous opportunity here, as existing languages have serious limitations (CUDA is effectively Nvidia-only, choices for compute shaders are extremely low level), and at the same time I think there's a lot of very cool stuff you can do, largely held back right now by poor tooling.

I suspect that progress on this front will overlap a bit with the previous category, as more often than not you run machine learning workloads on GPU anyway.

comment 5y ago · link · parent

This is consistent with my experience as well, as a researcher in industry with a fairly high open source profile. I get a trickle of emails along the lines of, "your work is awesome, can I be your apprentice?" This has never panned out. But if you come to me with specific questions demonstrating that you've read what I've written and are passionate about the ideas, you'll get a mentoring call and an openness to consider collaboration. I've had two such in the last two days, in fact, one through an introduction, the other from a cold IM. For me, academic status is not a factor, but then, I'm not actually in academia.

comment 5y ago · link · parent

Let's talk if and when you want to build something. I make no promises that piet-gpu-hal is suitable for other workloads (right now I would say it barely meets the needs for piet-gpu), but on the other hand I am interested in the question of what it lacks and what it would take to run, eg, machine learning workloads on top of it.

comment 5y ago · link · parent

Yeah, sometimes atomics perform way better than you expect them to. Check out the linkedlist benchmark in my suite, 12.1 G elements/s on AMD 5700 XT using DX12. That's a respectable fraction of raw memory bandwidth. Carrying over intuition from CPU land, you'd expect it to be very slow.

Looking at the ISA[2] you can get a glimpse of the magic that happens under the hood to make that happen. (Note: this test case is slightly simplified from what's in the repo for pedagogical reasons).

[1]: https://github.com/linebender/piet-gpu/blob/master/tests/sha...

[2]: https://shader-playground.timjones.io/da907f46d8bace9e5db7bd...

comment 5y ago · link · parent

Vulkan can't reliably do parallel dispatches, certainly not with any kind of scheduling fairness guarantee. CUDA has cooperative groups, which is a huge advantage.

Okay, I see your point about dev environments. It's like cameras, the best dev toolchain is the one you already have installed on your machine. I'll fix this but want to think about the best way to say it. I still believe there's a case to be made that CUDA is a heavyweight dependency.

comment 5y ago · link · parent

> They want to walk a LoD/culling tree

DAG, my friend, not tree. Seriously, I recommend people watch the talk, it's one of the more impressive demonstrations of how to use GPU compute power I've ever seen, and the results speak for themselves.

comment 5y ago · link · parent

Workgroup in Vulkan/WebGPU lingo is equivalent to "thread block" in CUDA speak; see [1] for a decoder ring.

> Using atomics to solve this is rarely a good idea, atomics will make things go slowly, and there is often a way to restructure the problem so that you can let threads read data from a previous dispatch, and break your pipeline into more dispatches if necessary.

This depends on the exact workload, but I disagree. A multiple dispatch solution to prefix sum requires reading the input at least twice, while decoupled look-back is single pass. That's a 1.5x difference if you're memory saturated, which is a good assumption here.

The Nanite talk (which I linked) showed a very similar result, for very similar reasons. They have a multi-dispatch approach to their adaptive LOD resolver, and it's about 25% slower than the one that uses atomics to manage the job queue.

Thus, I think we can solidly conclud that atomics are an essential part of the toolkit for GPU compute.

You do make an important distinction between runtime and development environment, and I should fix that, but there's still a point to be made. Most people doing machine learning work need a dev environment (or use Colab), even if they're theoretically just consuming GPU code that other people wrote. And if you do distribute a CUDA binary, it only runs on Nvidia. By contrast, my stuff is a 20-second "cargo build" and you can write your own GPU code with very minimal additional setup.

[1]: https://github.com/googlefonts/compute-shader-101/blob/main/...

654.

Prefix sum on portable compute shaders(raphlinus.github.io ↗)

116 points · by raphlinus · 5y ago · 15 comments

comment 5y ago · link · parent

Thanks for the detailed explanation, that makes sense and is useful information. My understanding was that they were more like real pointers, but I see that is not the case.

comment 5y ago · link · parent

So it depends a lot on the goals. CUDA is a very good developer experience, and Vulkan compute shaders is (at the moment) a very bad one. But if the goal is to ship real compute on a wide variety of devices, Vulkan is pretty close to the only game in town.

I also expect the language support and tooling to get a lot better. The fact that it's based on SPIR-V means it doesn't lock you into any one language, it's possible to develop good tools that run on top of it. That's happening to a large extent in the machine learning space with IREE, much less so here though.

I should also say, I'm not disagreeing with the strategy Blender has chosen, specifically getting vendors to support tuned ports to their hardware. I'm just saying that these specific criticisms of Vulkan (like lacking pointers) aren't really valid.

comment 5y ago · link · parent

This doesn't sound right to me. Vulkan 1.2 has support for pointers through an extension[1], and that's getting more widely available (certainly a lot more cards than can run ROCm). There's also support coming down the pike for buffer device address[2], which basically lets you use pointers to refer to resources when submitting work to the GPU instead of having to create bindings.

If an open source project of this scope came to me for advice, I would absolutely 100% recommend they base their stuff on Vulkan. It's getting very good quite quickly; there's basically nothing holding you back any more if you're willing to use the latest extensions. There is a tendency in open source to move more slowly though, and I understand that.

I agree with you that GLSL/HLSL is a bad programming language, but you can still get your work done in it. I'm personally excited about rust-gpu, but that is not ready yet.

[1]: https://www.khronos.org/registry/vulkan/specs/1.2-extensions...

[2]: https://community.arm.com/arm-community-blogs/b/graphics-gam...

comment 5y ago · link · parent

There's a lot of good stuff to be had from disassembling the ROM as well, don't ask me how I know.

comment 5y ago · link · parent

Gah thanks, I will fix this. Originally I had an SGI Indigo, but I switched away from it because there isn't anywhere nearly as good a description of its architecture as copetti's on the classic consoles.

comment 5y ago · link · parent

What would you suggest instead, as a well-documented system? I considered a few, but went with PS2 because of the excellent analysis. My understanding is that it's asynchronous from a programming model, but close to synchronous when it comes to timing, because the queues are pretty small. I could have this wrong though.
1
212223
63