Hacker News new | ask | show | jobs
by ninetenfour 1744 days ago
> The GPU’s hardware-implemented tessellation is (a) not compatible enough.

I am unsure why we are talking about GPU-based tesselation again. I said do it on the CPU in my comment you are referencing.

> Counter-intuitively, stroked paths are harder to render than filled ones.

I would if it could be rendered via a 2D SDF? Similar to how you can do resolution independent fonts/decals via SDFs... basically use a cutoff distance from stroke midline or similar.

> Also, the code in general is slow compared to C++, C# and many other statically typed languages. It’s incredibly hard to generate fast code from very dynamic languages like JS or Python, where everything is a hash map.

The above is 100% incorrect. The trick is to not use slow hashmaps and then your code is near the speed of C++. Use ArrayBuffers instead for everything. I said this in my earlier comment.

1 comments

> if it could be rendered via a 2D SDF?

I don’t think so. For one, computing SDF is a computationally hard problem.

Another thing, SDF will result in round joins, look at these pictures for available options: https://docs.microsoft.com/en-us/windows/win32/api/d2d1/ne-d... That particular spec is for MS Direct2D, but the rest of them offer the same features. Vector graphic formats are old and very stable. Similar story with line caps, SDF will get you round caps but sometimes you need different ones: https://docs.microsoft.com/en-us/windows/win32/api/d2d1/ne-d...

> The trick is to not use slow hashmaps

Every JavaScript object has such a slow hashmap, for the properties of that object.

> and then your code is near the speed of C++

JavaScript can only approach the speed of C++ for I/O bound things. For crunching numbers on CPU, the difference is rather large. Here’s some benchmark across many programming languages: https://benchmarksgame-team.pages.debian.net/benchmarksgame/ The average difference between C++ and JavaScript is 4x, for some problems it’s almost an order of magnitude.

Two reasons.

1. It’s hard to produce fast code from dynamically typed languages. CPU instructions are typed, different data types need different instructions.

2. C++ compilers work offline. In release builds, C++ optimizer can spend more time optimizing code. JavaScript JIT simply doesn’t have time to do these optimizations.

Thanks a lot Const-me for all your very accurate responses! I basically have nothing more to add.

Oh, and indeed, SDF is just completely irrelevant to the problem at hand here.

I would kindly suggest ninetenfour to avoid making bold statements without more domain experience. JS is great for many things. It's just not the tool for the job here.

SDF 2D fields are fun though.

Give you can create them from any cubic curve and it gives you offsets from it.

https://www.iquilezles.org/www/articles/distfunctions2d/dist...

If you offset in both direction from a curve, you can pick a start and end threshold to fill an outline. This only gives you rounded outline strokes. If you had separable tangential versus normal SDFs you could vary your end cap types.

Now how do you handle an arbitrary set of knots/control points in one shader? That is a separate problems for which there are a bunch of solutions.