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 5y ago · link · parent

Yes: don't. It's very hard.

More seriously. There is very good reason to be encouraged about wgpu as a cross-platform low-level GPU drawing interface for UI work. There are already successful UI toolkits using it, of which probably the most impressive is iced.

For 2D graphics in general, a good starting point is Diego Nehab's 2D graphics course. It has links to a bunch of resources including papers on GPU rendering.

Of course, I'm working on my own stuff, which in time I hope will become a very strong foundation for building things such as UI toolkits, but it's not ready yet, and of course you're already aware of it.

[1]: http://w3.impa.br/~diego/teaching/vg/

comment 5y ago · link · parent

This is still in transition, and the full story is complicated.

The official shader language for WebGPU is WGSL, and that will almost certainly be the only language supported for web deployment. Originally this was going to be a textual representation that had exactly the same semantics ("bijective") as SPIR-V, but that's been diverging a bit[1]. Only very recently have atomics been added to the spec, and implementations are still catching up.

Because WGSL is still under construction, for native deployment, wgpu supports SPIR-V shaders as well. On Vulkan, those are passed pretty much straight through to the GPU driver (with some validation). On DX12 and Metal, those are translated into HLSL and MSL, respectively. Until recently, in wgpu, all those translations were handled by spirv-cross. More recently, the new naga crate is doing those translations. Again, this is under construction and not everything works yet.

[1]: https://kvark.github.io/spirv/2021/05/01/spirv-horrors.html

comment 5y ago · link · parent

It could be as simple as timing, the polar stroking paper probably hadn't come out at the time this paper was written.

comment 5y ago · link · parent

I've been doing something of a deep dive into the suitability of WebGPU and WGSL for "serious" compute tasks (ie potentially competing with CUDA). Here's my take on the current state.

The upside is that it's very pleasant to program compared with existing APIs, and is portable. Even when the GPU doesn't quite meet the requirements, it's possible to polyfill compute shaders with SwiftShader. As this standard becomes more mature, it will be reasonable to expect that it will be correct and performant pretty much everywhere.

Note that both web and non-web implementations are viable.

The biggest drawback is that the "MVP" covers the basics of compute shaders but not the advanced features. It's roughly equivalent to DX11 / Shader Model 5, which is of course good for compatibility on pre-Windows 10 devices. It's missing subgroup operations, scalar types other than 32 bits, an explicit memory model, and some other goodies. This potentially leaves quite a bit of performance on the table, but that will depend on the exact workload. For example, machine learning inference workloads will probably suffer considerably due to the lack of f16. Thus, if you want to ship something based on compute shaders today, it makes sense to build your own portability layer on top of existing APIs such as Vulkan, DX12, and Metal.

I do think WebGPU is an excellent on-ramp to learning compute shader programming, as it will get stronger over time, and concepts will transfer to other APIs as listed above.

Watch my Twitter feed for more depth on the points I listed above.

comment 5y ago · link · parent

Also see Polar Stroking[1] which covers similar ground.

[1]: https://arxiv.org/abs/2007.00308

comment 5y ago · link · parent

I am not in exactly the same place, but respect your comment as being at least logically self-consistent. It reminds me of Jody Lanard's thread[1] about how the new CDC guidelines are "adultifying" in that they transfer the decision back to individuals rather than paternalistic guidelines. The way I think about it is that until recently, the risk calculus was analogous to drunk driving, but is now more similar to riding a motorcycle without a helmet.

[1]: https://twitter.com/EIDGeek/status/1393987801260204034

comment 5y ago · link · parent

One error is that the citations only support the claim within the US, while the context of the original story and the parent comment are the world. That makes rather a large difference.

comment 5y ago · link · parent

https://www.medrxiv.org/content/10.1101/2020.05.03.20089854v... estimates it at 0.68% (0.53-0.82%). Boiling it down to one number is tricky though, as it is hugely correlated with age, so looking at any particular demographic is likely to give you pretty different numbers.

comment 5y ago · link · parent

Just testing on random parens gives you an expected stack depth of O(sqrt(N)). I've also (just now) tried biasing the sequence generation with more pushes than pops, and it doesn't substantially affect running time.

The case where the stack depth is greater than the partition size is tricky for sure, and this is what I call the "backlink" mechanism. The tl;dr is that it follows the deepest link (furthest from top of stack) that it can, and continues the look-back process, until the slice is complete. You can search for "backlink" in the code - it's actually not a huge amount of logic, though it certainly was subtle to get right.

comment 5y ago · link · parent

I think that's a good thing. People do complain about the overly high rate of evolution of Rust, but I think that was much more true in previous years than today (not least because a lot of projects needed to depend on nightly).

Also, most of these changes enable new things, and are only in the edition because of some low chance of breaking existing code. The 2018 edition had some major changes, so 2018 code looks pretty different - the ? operator, big changes to module use, impl/dyn Trait, and more. I suspect that most code bases you won't be able to tell whether it's 2018 or 2021 without looking more closely.

comment 5y ago · link · parent

Another curve to add to this (very nice) collection is the parallel curve of an Euler spiral. It's mathematically very similar to a circle involute, but with some nice and interesting properties of its own.

comment 5y ago · link · parent

Yes, I was assuming it was char *, as in the OP, which can be signed. And any left shift of a negative quantity is UB in C (I'm not sure if this is fixed in recent C++), it doesn't have to be what's commonly thought of as overflow.

comment 5y ago · link · parent

Why thank you Vitali. Coming from you, that is high praise indeed.

comment 5y ago · link · parent

This one wasn't specifically "betrayal by compiler," but it was a confusion between signed and unsigned quantities for a size field, which is very similar to the UB exhibited in OP.

Also, the fact that you can't see the problem is actually evidence of how insidious these problems are :)

The rules for this are arcane, and, while the solution suggested in OP is correct, it skates close to the edge, in that there are many similar idioms that are not ok. In particular, (p[1] << 8) & 0xff00, which is code I've written, is potentially UB (hence "mask, and then shift" as a mantra). I'd be surprised if anyone other than jart or someone who's been part of the C or C++ standards process can say why.

comment 5y ago · link · parent

I agree with the bulk of this post.

Re the anecdata at the end. Have you ever run your code through the sanitizers? I have. CVE-2016-2414 is one of my battle scars, and I consider myself a pretty good programmer who is aware of security implications.

comment 5y ago · link · parent

That's just constant folding. Here's what it looks like when you actually need to go to memory:

https://gcc.godbolt.org/z/9qGqh6M1E

And I think we're on the same page, it should be possible to get similar results in Rust.

comment 5y ago · link · parent

In Rust, [u8; N] and &[u8] are both primitive types, and not abstractions. It's possible to create an abstraction around either (the former even more so now with const generics), but that's not necessary. It's also possible to use "extension traits" to add methods, even to existing and built-in types[1].

I'm not sure about a 3 byte big endian integer. I mean, that's going to compile down to some combination of shifting and masking operations anyway, isn't it? I suspect that if you have some oddball binary format that needs, this it will be possible to write some code to marshal it, that compiles down to the best possible asm. Godbolt is your friend here :)

[1]: https://rust-lang.github.io/rfcs/0445-extension-trait-conven...

comment 5y ago · link · parent

The idiomatic way to do this in Rust is to use functions like .to_le_bytes(), so you have the u32 (or whatever) on one end and raw bytes (something like [u8; 4]) on the other. It can get slightly tedious if you're doing it by hand, but it's impossible to accidentally forget. If you're doing this kind of thing at scale, like dealing with TrueType fonts (another bastion of big-endian), it's common to reach for derive macros, which automate a great deal of the tedium.

comment 5y ago · link · parent

I have no idea who you're talking about, but curiously I ran across this random example of a company that seems to have a particularly onerous process for cancelling a subscription:

https://www.reddit.com/r/assholedesign/comments/gcz2dg/mansc...

comment 5y ago · link · parent

Commenting out lines, rerunning, and trying to get meaningful performance counters is pretty much my workflow, and, from what I've observed, not very uncommon. A tool like AGI should give at least somewhat better insight than just FPS though, as that is a crude measure that can conflate a lot of different things.

There's a fair amount of academic work on GPU simulators, but a lot of the activity there seemed to be a few years ago. And in any case, I'm not sure how usable these are for tuning real workloads. If you have good hardware performance counters, those can give you almost as good information as you'd get out of a simulator.

I think there's tremendous potential to do better tools in this space. I'm not quite sure what the business model is, though.

891.

The case of the curiously slow shader(raphlinus.github.io ↗)

77 points · by raphlinus · 5y ago · 12 comments

comment 5y ago · link · parent

I think there's something important here, potentially obscured by imprecise terminology.

The Rust safety invariants are, at heart, global properties. The central one is that mutable references are unique. Another way of saying that is: if you hold a mutable reference, then all other references held by all other objects in the system do not conflict with it (either because it is included in the "stacked borrow" or because it doesn't reference the data at all). That kind of property is ordinarily quite difficult to prove.

Rust does it though, by encoding these invariants in the types of functions. Most importantly, these types compose, meaning if you've got one function that respects these invariants calling into another, then the whole thing will also be sound. You can compile them separately and still be confident.

By contrast, achieving similar goals in the C family of languages does require a global analysis. The classic example is alias analysis. It's implemented in many compilers, tons of PhD ink has been spilled, but long story short it doesn't work. You get okay results sometimes on small programs[1], but as systems scale up, basically there always becomes a way for one pointer to alias another.

Thus, the claim I would make is the following: it will not be practical to retrofit Rust-style borrow checking onto an existing unsafe language, because the type system has to be rich enough to express the types of invariants required. The Rust type system has been carefully crafted to be powerful enough, at considerable cost: the complexity of the type system is one of the biggest complaints about the language, and slow compile times, one of its correlates, is another.

I would also claim that trying to scale up static analysis (based on alias analysis and other similar global analysis techniques), while perhaps somewhat helpful, is not going to give comparable results as Rust. In order to get anywhere near the level of confidence that all possible safety problems have been caught, the false positive rate would be unacceptably high.

I think this is one of the enduring achievements of Rust, and one likely to be carried forward in future programming language designs, but extremely unlikely to be successfully retrofitted to existing languages.

[1]: https://digitalcommons.calpoly.edu/cgi/viewcontent.cgi?artic...

comment 5y ago · link · parent

Another game that I think really belongs in this collection is Gorogoa by Jason Roberts. It's a small game, not particularly difficult, but one I enjoyed.

comment 5y ago · link · parent

For the most part, yes. It is possible to do runtime detection of these features and have compatibility paths, but that further adds to the burden of what's required of tooling and infrastructure. Depending on exactly what you're doing, it's probably best to assume recent drivers. For this type of application (a renderer for a creative app on desktop), I think it's a perfectly reasonable requirement.

comment 5y ago · link · parent

Thanks, appreciate the shout-out.

I'll give a little background how that came about. At the time, I was promoting the use of Rust within Fuchsia. The team was largely composed of badass C++ programmers, who of course were very familiar with reasoning about programs in terms of exactly how things were laid out in memory. The official Rust documentation was at a more abstract level with respect to a lot of this stuff, in many case careful not to overspecify because a lot of the details of memory layout are not normative and may in fact change. In explaining the most common and important Rust data types, I found these kinds of diagrams useful, and it grew into the page you see there.

comment 5y ago · link · parent

There's a lot here, let me try to clarify a bit.

Most of the time, when people say OpenCL, they mean that an OpenCL driver is provided by the GPU vendor. That's what's in a particularly sorry state. Many vendors ship OpenCL but have deprecated it. AMD's ROCm is based on OpenCL, but they don't support it on all cards and there are problems.

The other thing that's picking up steam lately is using a lower level API such as Vulkan as the interface to the graphics hardware, and having a layer that runs the compute workload (whether OpenCL or something else) on top of that. In my opinion, this actually has a pretty good future. On Linux, Vulkan is obviously the way to go, on Windows it's supported by all major cards, and on mac it's possible to fake it with MoltenVK. This is what clspv and clvk are about, but from the article you posted these are not in usable shape yet. I think that might say more about the level of interest around OpenCL than anything else though. It's entirely possible that running compute on Vulkan becomes mainstream through other efforts like IREE than by porting OpenCL workloads.

In a couple years or so, there's a good chance the landscape will shift again, as WebGPU might be capable of running compute workloads well, and is likely to be supported by all vendors. Note that despite "web" in the name, it doesn't require a browser or other web technology. You can run prototypes of it today, but there are still huge chunks of the ecosystem missing.

comment 5y ago · link · parent

That post is 4 years old. Since then, Vulkan compute has made enormous strides forward, while OpenCL has stagnated. Examples of the things Vulkan has now are subgroup size control, explicit float16 and int8, pointers (previously you had to fake them with arrays), and a memory model. The memory model alone is a pretty big advance.

What is still a mess is the tool situation. You have to write your shaders in GLSL or HLSL (the latter is still missing some subgroup operation) and compile to SPIR-V. You also have to have CPU-side code to manage all resources, including memory allocation and putting in explicit pipeline barriers and other ways to manage the asynchrony. For someone who just wants to get their code running, it's pretty painful.

My own application is 2D rendering, but a lot of the motivation for improving Vulkan Compute is machine learning workloads. One of the projects to watch is IREE (https://google.github.io/iree/).

I've got some blog posts and tutorials in the pipeline, stay tuned.

comment 5y ago · link · parent

Vulkan supports integer operations just fine, but tool support for just about everything is extremely primitive. Sizes other than 32 bits are available, but generally as an option. You can check https://vulkan.gpuinfo.org/listfeaturescore10.php to see the fraction of drivers that support the various integer sizes (and lots other optional features).

comment 5y ago · link · parent

Speaking for myself (team J&J), single dose is definitely more convenient. Anecdotally, reactions seem to be less severe than for the mRNA vaccines. And, while it's only a minor speed bump to our gold-plated healthcare delivery system, the fact that it doesn't need an extreme cold chain means it's logistically much easier to handle.

comment 5y ago · link · parent

My perspective is that OpenCL is indeed in that bad shape, though it does have defenders. Both AMD (ROCm) and Intel (oneAPI) have ways to run workloads originally written to run on CUDA, but they're nowhere near the level of polish as CUDA.

I believe an open stack can and will emerge, but it will take time and effort on all levels of the stack. It's possible to do pretty amazing things with Vulkan compute shaders, but the programming model is different than CUDA (it's not single-source), and the tooling support is not quite there.

In time, I am hopeful that WebGPU will gather more momentum, and be officially supported even in places where Vulkan requires janky adapter layers. But in its current form, it's very immature and far from being usable for real workloads.

1
293031
63