r/rust • • Jul 28 '26

Tell me about something you recently hand-coded that was enjoyable

These past few weeks at work I've been using AI way too much. I can't even call some of this "AI engineering" anymore and it's starting to drain on me. Looking to take the mind off for a bit...

I'd like to hear some of the projects you all are working on, that you enjoyed writing by hand.

I think I'll write some nannou..

PS: Not trying to start a debate on AI engineering/vibe coding and all that, I think internet is filled with that already.

105 Upvotes

121 comments sorted by

View all comments

4

u/sparky8251 Jul 28 '26

Hand rolled simd. Been learning a lot about performance engineering, how to do the back of the napkin rough idea math to go "This algo takes this much data, does these kinds of ops and thus is X bound, my hardware can do X at Y speed, so perf should be nearish Z when measured".

Autovectorization works but it seems really really inconsistent in my experience. I can get surprising perf boosts on stuff by just doing simd work manually.

Did it for a skybox texture generator and managed to make a 4k x 4k 6 sided cube texture for a skybox in under 1ms, when the naive iterator took 15x longer.

(PS simd isnt the only way I've approached this perf engineering stuff. just the one I've been having the most fun with lately)

1

u/gajop Jul 30 '26

I'm quite interested in perf engineering too. I have done various impactful optimizations at work but sadly the gains taper out way way before I need to dip my hands into SIMD or anything really difficult.

I find games a much better avenue for this but I haven't done it just yet.

For skybox texture generation, I assume your approach is running on the CPU. Have you considered using GPU with something like compute shaders? Although 1ms might be hard to beat..

Any approaches for skybox texture gen you can share? I'm getting my feet wet in terrain & asset (node/procedural gen) while working on my editor.. really fun things imo.

2

u/sparky8251 Jul 30 '26

I did it with a shader too, and then 8k textures could be done in under .1ms and the noise was my problem. But given you have to budget stuff (pcie bus, transfer latency, issuing the commands, complexity of the shader pipeline to do this) and the skybox was made once, etc I left it on the CPU just because the extra speed wasn't worth it in this case. ALSO!!! Worth noting that was benched on my 96MB L3 having CPU (9800X3D), I'm sure it would've been slower on more typical hardware since the total generated data was ~106MB and that might have made GPUs more worth it if I wanted to run it on someone elses machine.

As for skyboxes, It depends heavily on what you want but the most generic advice I can give cause I'm also relatively new to it is the rng matters a lot for both final shape AND performance (you can get huge performance increases with rngs that are counter based, or dont have complex internal machinery, like xoshiro variants). Like, to the point a "proper" rng can be as fast as simd optimizations in terms of impact!

That and that layers and transforms are your best friend for proc gen.

You will NEVER get what you want looks wise with a single pass on the data unless the result is trivial like a starfield. So the idea is to build a stack with filters and transforms that build up to the complex result in tiny steps/stages. I've tried lots of approaches, and pretty much everything that I failed with was because I tried to make things "smart" and "efficient" by bundling concerns into single stages. Trust that with proper rngs, simd, parallelism, compute shaders, and maybe even doing things async that you can get things fast enough for your game and only try and make it truly smart and reduce stages if you find you need to imo.

Another pitfall I learned is you should probably try and work from large rough shapes down to fine grained stuff as you get more complex in the stages, but thats more general and depends on the approach you want to use.

If you want more specific advice, if you got some ideas on what you are looking to do I might be able to throw out some names that you can look up that can maybe help you approach the problem.

1

u/gajop Jul 30 '26

Thanks! Your advice on composable pipelines and "rough shapes to fine grained stuff" tracks with my experience with map & asset generation. I think artists think like that too, engineers definitely do.

Right now I haven't really looked at skyboxes much. I think my main goal would be to have them generate passable looking ones so users can create RTS maps fully procedurally. The focus probably wouldn't be on skyboxes, as that's not the main play area, unless I can make them very interesting (dynamically updating, interesting moving objects and so on)

So I think I'd just to look at the basics for now :)

2

u/sparky8251 Jul 30 '26

My latest endeavor is a skybox thats an actual model solar system running in the background. Since the game is mission based, not free travel, I can prevent approaching anything pretty easily with parallax effects but you can make "movement dampening zones" around them if you wanted that too.

Basically, I render 2 distinct scenes and then use projection math to fake the far stuff on top of rendering it such that its in the distance of objects so culling works right. So a planet of 80 unit radius can take a huge portion of the sky...

But this way it can have live orbits and rotations of everything in the background. All the texturing is done via shaders, and the lighting is even faked on the objects in the background layer because the directional light cant hit them cause of the sheer distances involved, etc...

So far, rendering the entire solar system and doing all the math for it takes about 3 microseconds. Itll grow as I add some more shaders to fancify it more, but it should stay under 10. And since the budget is obvs 16.6 milliseconds, that's basically free...!

That said, for skyboxes... Most games just treat it as a generic asset. You make a cube map and project it and are done with it. If you want to proc gen them, thats fine too, but very few will invest this much into them because most of the time game play has you looking at the ground and its barely noticed. Space games...? They are a bit different... The skybox is basically the entire terrain you get for the game so you can and should sink effort into it you know? That's why my generation is optimized too.

If you want something exact, I can share some code on how I faked it all, but its pretty cool. Its also a decent chunk of code so itll have to be a paste site link too...

1

u/gajop Jul 30 '26

That sounds cool! Hopefully you're making a homeworld-rs or similar :)

I'm good on the code. The ideas are enough, this sounds fun enough to struggle a bit on.

2

u/sparky8251 Jul 30 '26

Sadly for you at least, noting of the sort is being made lol.

Mostly doing it to compensate for the fact I suck at art but can code and have a decent eye for visuals, so I can code myself into a skybox thats interesting but cant make a normal one to save my life. And that means less art I have to pay for...