7 ms·
Writing a winning 4K intro in Rust
- doctor_eval 6y agoI learned programming on the TRS80, and didn’t know about the demo scene until I was already too busy with other parts of my life; I think I first saw a demo on the Amiga. I always feel like I missed out on something transcendental. I think I would have leaned a huge amount. Instead I had to settle with hacking NewDOS/80 binaries. I have a huge amount of technical respect for the people who do this stuff.
- mroche 6y agoI didn't know it existed until ~October(?) last year. My $lastEmployer (animation studio) had a demo day where in-house people would demo some personal projects they were working as well as exhibit entrants' works to the actual demo scene. It was insane, I would never dream of being able to produce most of the things I saw. If I remember correctly we watched demos from the 4K category to the 1 or 4M category. Procedural graphics, audio, emulation, it truly is an art form.
- nkozyra 6y agoI grew up on BBS (started dialing at age ~6) There were only two types of programming that intimidated me so much I wouldn't even try: people writing languages from scratch and the demo scene people. I just sat back and said "nope, I'll never get there."
- doctor_eval 6y agoHaha! I wrote a logo inspired language to do graphics programming. It was an interpreter written in BASIC. You didn’t miss much :) I also wrote a BBS, so go figure...
- bane 6y agoIt's never too late! Demoscene competitions happen all year round, around the globe. Some of the competitions are super focused and aren't a ton of work to do. The competitions (called "parties" in the scene) are a great way to spend a weekend with a bunch of like-minded nerds from many different disciplines: programming, graphics, music, ANSI/ASCII art, retrocomputing, etc.
- RobLach 6y agoThis is important art.
- Recurecur 6y agoI really enjoyed the artistic part. I wonder if the author was trying to evoke a Martian atmosphere with the color choices. The contrast of the spheres with the columns was very nice as well...the perfect, beautiful spheres ascending into the sky... I'm not sure if the author is reading this thread, but thanks! :-)
- int_19h 6y agoKinda surprised about iterators not getting optimized so well. Even in higher-level languages, this is a commonly utilized optimization opportunity - e.g. the C# compiler will replace a foreach-loop over an array with an identical (but faster) for-loop.
- dpc_pw 6y agoCompiler optimizers are full of surprises going both ways: good and bad. And details like this can probably change from version to version and get affected by compiling options. I would also expect that performance is higher priority than code size, generally, so missed opportunities like this might go unnoticed and unaddressed a long time if they don't show up on performance benchmarks.
- eru 6y agoThe classic example of surprising optimization is perhaps matrix multiplication in Fortran: At some point in history, when you wrote the straightforward, primitive three-nested-loops version of matrix multiplication your code ran much faster than when you tried to implement one of the more sophisticated algorithms. That was because the Fortran compiler recognised the primitive version and replaced it with a special purpose highly optimized matrix multiplication. The compiler couldn't do that with the more sophisticated approaches.
- mmarx 6y ago> The classic example of surprising optimization is perhaps matrix multiplication in Fortran: > At some point in history, when you wrote the straightforward, primitive three-nested-loops version of matrix multiplication your code ran much faster than when you tried to implement one of the more sophisticated algorithms. It might also have been due to Fortran using column-major matrix layout; if you try to implement a sophisticated multiplication algorithm that is optimised for row-major matrices, then that will be slower (indeed, also the row-major three-nested-loops version is already quite a bit slower). Another thing is that the sophisticated algorithms usually have some constant overhead that makes them shine only on sufficiently large matrices.
- fernly 6y agoTIL: about the Demoscene[1], "an international computer art subculture focused on producing demos: self-contained, sometimes extremely small, computer programs that produce audio-visual presentations. The purpose of a demo is to show off programming, visual art, and musical skills. Demos and other demoscene productions are shared at festivals known as demoparties, voted on by those who attend, and released online." [1] https://en.wikipedia.org/wiki/Demoscene https://en.wikipedia.org/wiki/Demoscene
- the_af 6y agoThere is also an active demoscene for microcomputers of the 80s, such as the Commodore 64 and the ZX Spectrum. And also people coding new games for them. I'm glad these venerable computers live on.
- balls187 6y agoOh yeah, the Cracktro.
- 40four 6y agoI never get tired of watching these things. Such a cool art form.
- Polylactic_acid 6y agoSometimes they can be a lot of filler though. Yes you managed to create a 40 minute video but its 90% the same thing moving very slowly.
- joan_kode 6y agoI'm surprised someone on HN since 2012 would learn about the demoscene today! Then again, it's true that the demoscene "keeps to itself", it's always been quite the bubble. I'm not sure how that community could do some outreach, but I believe a lot more people would find it interesting.
- saagarjha 6y ago
- Torkel 6y agoI tried to share the link to this via fb messenger, and it blocked it because it "goes against our Community Standards". A while back the same happened to pouet (https://twitter.com/pouetdotnet/status/1249453524758528000 https://twitter.com/pouetdotnet/status/1249453524758528000). Demoscene stuff seems to be good at triggering the filters :)
- justinclift 6y agoLooks nifty. Seems to only compile on Win though: $ xargo run For more information about this error, try `rustc --explain E0463`. error: could not compile `winapi`. Still, probably good for interest/learning from. :)
- Yuioup 6y agoHm so it's leveraging the Windows API. I thought demos were supposed to be bare-metal.
- fb03 6y agoThe only way to achieve those kind of graphics in 4k is leveraging the Windows platform api. That is, unless you can drive a gpu and a soundcard from bare metal in 4k, while implementing the drivers and stuff for the hardware.
- slashink 6y agoMost 4K demos on Pouet the last years has been Windows only. Demoscene parties usually specifies exactly what computer they will run the demos on in advance so groups can optimize for that setup. The code sometimes only run on that particular setup.
- dbrgn 6y agoUsing the OS is allowed. As far as I know, one of the reasons why there are more Windows demos than for example Linux, is that Windows provides more graphics/sound APIs out-of-the-box while on Linux the OS itself does less you'd have to utilize libraries (which is kind of cheating when doing a 4k demo). I think it could be summarized as "write a demo for the target platform as efficiently as possible". Doesn't necessarily need to be bare metal, there are JS demos that run in the browser as well. In that case the browser (with all its APIs) is the target platform.
- raverbashing 6y agoThere's no such thing as bare metal today, unless you want to go from real to protected mode and fit a graphics driver in 4k. Basically impossible
- readams 6y agoHow do they view the use of libraries in this competition? Do the libraries count toward your 4K?
- Polylactic_acid 6y agoIts only the output binary size that matters. If the libraries are in there then it counts. Where it gets a bit funny is graphics libraries and gpu features. You wouldn't include vulkan in your size but you get a bunch of features that weren't available on older hardware.
- eru 6y agoLooking at the article, OpenGL seems to be allowed in that competition. No clue about other libraries.
- bane 6y agoBasically it has to run on a fairly clean Windows machine. The competition will usually publish the specifications of the machine ahead of time and what's on it. What you enter has to fit entirely within the bounds of the competition, but can use anything else on the machine if it's available. So you could enter say, a 4kb entry, and use a bunch of Windows .dlls for icons or something as artwork, but you couldn't enter a 4kb executable and 100MB of libraries and other stuff. For some languages where this is really hard to achieve, they'll carve out a separate category for a language if they think there will be enough entries. e.g. a Java category where the entry fits with the space limit, but can use any other library in the default Java classpath, even though that's very very big. Likewise for different operating systems (Linux, MacOS competitions). There's been a fantastic resurgence in retrohardware and a category called "old school/skool" for entries that have to run in MS-DOS -- which can be ultra hard as you have to code up a lot more of the runtime libraries like music, graphics rendering pipelines, etc.
- Snelius 6y agoYea, every rust programming is a challenge ;)
- mmastrac 6y agoCool projects Some Rust macro magic could probably clean up some of that bounds checking.
- dwaltrip 6y agoThat intro was very cool! Does anyone have pointers for a fullstack web dev who is interested in the demoscene (or computer generated art in general)? I've actually played around a bit with rust [1] (I understand it probably isn't ideal for demoscence), but I haven't really done any graphics programming before. I get the feeling it is a pretty deep rabbit hole. [1] https://github.com/dwaltrip/advent-of-code-2019-rust https://github.com/dwaltrip/advent-of-code-2019-rust
- Reelin 6y ago> but I haven't really done any graphics programming before I've never written any demos but do enjoy playing with GPUs. Regarding anything and everything graphics related, try not to get overwhelmed by modern APIs; it's very easy to lose the forest for the trees. Shadertoy is a really good starting point. My impression of the demoscene can be loosely summarized as extreme code golf for graphics and sound.
- dwaltrip 6y agoThat all makes sense. Thanks for the advice.
- atupis 6y agoJavascript demos is one option https://www.dwitter.net/ https://www.dwitter.net/
- dwaltrip 6y agoThat site looks pretty cool! Some very nice animations on there.
- laurentlb 6y agoIn this intro and in almost every modern 4kB intro, the graphics are made in a shader. To play with shaders, I'd recommend you to look at https://www.shadertoy.com/ https://www.shadertoy.com/. Check also Inigo Quilez's website (https://www.iquilezles.org/ https://www.iquilezles.org/), it has a lot of fascinating content.
- smabie 6y agoI love the demo scene, but the culture is kind of strange. Like I've been fooling around with the C64, and all the tools (emulators, debuggers, and etc) are hosted on source forge, or sometimes this random ftp. The author of the debugger I'm using proudly proclaims that he only works on it when he's fucked up on drugs after parties. The documentation of some tools has lewd ascii art, and etc etc. I appreciate the anti-corporate vibe and at the same time wish they were a little more professional. Whatever you think, they're true 80s style cyberpunk hackers!
- drxzcl 6y ago"On the one hand, I found a program that does exactly what we need. On the other hand, not-quite-dressed anime characters..." I've had this exact conversation with coworkers before.
- saagarjha 6y agoWhich side were you on?
- tibet_13 6y agoAsking the real questions.
- nonbirithm 6y agoWhich program?
- vorpalhex 6y ago"For research purposes.. of course..."
- deleted 6y ago[deleted]
- unfunco 6y agoI recently explained to someone in an interview that I chose Puppet over Ansible circa 2014 because by default, Ansible used to output success or failure messages using cowsay.
- pjmlp 6y agoIt looks just great! Congratulations on winning it.
- zerr 6y agoAs author mentions, it is non-idiomatic Rust with lots of unsafe's, basically writing C code with Rust compiler.
- Polylactic_acid 6y agoThis is not true. Unsafe only disables a few safety features. There are still a bunch left on so unsafe rust is still more safety checked than C.
- Jhsto 6y agoCare to elaborate? What are such safety features?
- pizza234 6y agoThe only unsafe functionality the author describes is the unchecked array access. Based on that (which may be incomplete, of course), the borrow checker is a safety feature that is active.
- Ygg2 6y agoThe grandparent is wrong. Unsafe doesn't disable safety functions. From the big book of Rust Unsafety: The only things that are different in Unsafe Rust are that you can: - Dereference raw pointers - Call unsafe functions (including C functions, compiler intrinsics, and the raw allocator) - Implement unsafe traits - Mutate statics - Access fields of unions https://doc.rust-lang.org/stable/nomicon/what-unsafe-does.html https://doc.rust-lang.org/stable/nomicon/what-unsafe-does.ht... The point is, unsafely calling `get_unchecked` is what's bypassing the range check feature. Not Unsafe itself.
- hellofunk 6y agoI'm a little confused about what's happening here... Rust can run in an OpenGL shader ?!
- ohazi 6y agoThis is old and may not even compile anymore, but it's short enough to give you an idea of what's going on. https://github.com/ohazi/opengl-demo-rust https://github.com/ohazi/opengl-demo-rust GLSL shaders are usually either loaded from files or stored as strings in your programming language of choice. The OpenGL library or bindings will usually have some facility to compile those programs and then send them to the GPU, along with textures, geometry, etc.
- pan69 6y agoAlways amazed by what people are capable of doing in 4K. The music vibe reminds a bit of Brian Eno's Under Stars track from the Apollo album: https://www.youtube.com/watch?v=dcWwtQmAt7E https://www.youtube.com/watch?v=dcWwtQmAt7E
- classics2 6y agoIt’s hard to rationalize calling it a “4K demo” when you’re allowed to call OpenGL.
- albertzeyer 6y agoDoes someone know what the problem is with the intrinsics? Or is there a problem? The author states that this breaks his debug build for some reason.
- lasagnaphil 6y agoI think the author called _mm_load_ps() on a pointer that isn’t 16-bit aligned. Either change that to _mm_loadu_ps() (which also works on unaligned pointers but incurs a performance penalty), or make sure your object is aligned when doing the heap allocation (there’s a STL function called aligned_alloc() in C++, probably there’s something similar in Rust)
- Twirrim 6y agoWhy would that only fail on debug builds and not release builds, though? That seems really strange to me
- cptroot 6y agoDebug and release builds probably call slightly different sets of methods, so they might end up with slightly different heaps. That might be enough to cause a difference in offset between the builds.