Been a while.
This update started out with trying to pin down some performance issues on the Steam Deck. To that end, I made a test scene wherein one can add as many asteroids and lightning bolts to the screen as one likes, then press a button to have all the asteroids blow up and the lightning go into I Actually Hit Something Mode. It also displays FPS, if you're not running with the Steam Deck performance overlay enabled. From the main menu, hit the tiny controller button you don't use to pause. There's no keyboard equivalent right now, because I didn't write this for other people to play with and I have a controller on my dev machine. But take a look, if you want.
This was a bit eye-opening, and highlights just how important it is to make actual measurements. As Adam Savage once said, the only difference between science and screwing around is writing stuff down.
Explosions
The asteroid explosions were still taking more of the FPS budget than I wanted, so I dove into the WraparoundSprite2D and WraparoundAnimatedSprite2D classes to institute visibility culling. No longer does each one draw nine sprites every time. Each of those nine sprites gets a visibility test (just checking to see if there's any overlap between the viewport and its bounding box) before drawing it. Most sprites will then only get drawn once, with a maximum of four times.
Particles
Same thing goes for particles, though those are harder to deal with. A particle system's footprint expands over time, and must be calculated rather than directly read. As mentioned in the previous update, GPU particle systems exist only on the GPU, so they're inaccessible to normal game code. Fortunately, most of the particle systems in the game run with fairly simple parameters that lend themselves to algorithmic calculation of a maximum bounding box. This was less simple than it seems, mostly because I suck at figuring out all the different transforms required to convert things from screen space to object space to viewport space to Widdershins Minkowsky Space. Shit's complicated, yo.
Distortion
I noticed while profiling the frame draw time in the Godot editor that the frame draw time doubled as soon as the player ship was added to the scene. Doubled. Turns out it was the always-on even when inactive repulsor distortion shader.
The repulsor ability uses a space-warp shader that samples the screen, then distorts it based on some parameters passed in. Sampling the screen is a) kinda expensive, and b) not something that can be done in parallel with any other rendering. In order to make the best use of the Steam Deck hardware, I need things to be as parallelizable as possible.
There's no getting around the screen sampling part without rewriting Godot's entire render engine, so I just have to make it as efficient as possible. When I wrote the WraparoundSprite2D, I converted the distortion shader effect into one so that the distortion would wrap around the edges of the screen like all the other effects. This effectively meant running 9 copies of that shader, most of which would never affect any viewable pixels. I rewrote the shader to always be a single shader, but to have the distortion appear in more than one place if necessary. I also calculated a bounding box within the shader to eliminate processing pixels for which distortion would not be necessary, and moved all of the distance calculations into a vertex shader so that they wouldn't need to run for every pixel.
Lightning
This is where I found a lot of savings.
Later on in the game, the lightning gun is excellent for clearing thickets of small asteroids from whole regions of the screen. As long as you don't mind a slide show while it's happening, that is.
The lightning shader as I originally wrote it was a masterpiece of inefficiency. I was generating 6 7-octave and 6 12-octave FBM textures for each bolt. My benchmark scene indicated that I could, if I asked really nicely, have six of these on screen at a time before I started dropping frames on the Deck. And that's if I wasn't doing anything else.
But there's no reason to calculate what you can cache, so I hopped over into GIMP and made a 1024x1024 tiling texture with different scales of noise on each channel. A single texture read now gives me four channels of FBM. I can animate this just by sliding it around, at least for the lightning shader.
Result: 114 FBM folds -> 1 texture read. Hundreds of lightning bolts on the screen at the same time and no frame rate impact.
Mouse Input
After watching the Nextfest trailer, a few people pointed out that the game really doesn't play like that with a keyboard. I admit, I'd been neglecting the non-controller input for too long even though this game started out as a way for a friend with a broken hand to have something to play. I haven't really played it with just the keyboard in a while, and the default keyboard configuration sucks now. I'm literally the best Asterisk player in the world (not as impressive as it seems, but I'm still pretty damn good at a game I made) and I struggled to get past stage 8 without my hand cramping up from the unnatural position the keyboard inputs forced.
Fine.
Let's implement mouse inputs.
It's a game design decision that the maximum rotation rate of the ship be fixed, which raises a problem. Joysticks and gamepads are almost always used as differential controls, that is to say the input indicates the rate at which something happens. For something where the maximum rate is fixed, a differential control works beautifully and doesn't cause any cognitive load in the user.
The mouse is usually used as a direct control, where the position of the mouse is directly* correlated to some position or angle in game. Think playing a FPS with mouse and keyboard. The player isn't limited on how fast they can turn around, so it's all about how well the player is trained to know how far to move the mouse to quickly land on a target. This mirrors how the mouse is used in normal desktop computing. (*modulo mouse acceleration).
So now I'm put in the position of needing to use a direct control device to control a differential input. There are three possibilities:
Read the mouse position when the game starts, and use the distance on the x axis as a proportional input. Without a reference for where zero is, the player is left hunting for it while the ship always turns one way or another.
Read the distance on the x axis the mouse has moved since the last frame and use that as a proportional input. This is the one I've implemented. Upsides: it feels natural-ish. Downsides: a player used to FPS games will expect the ship to turn at a rate proportional to the speed of the mouse, and may be frustrated or confused when that doesn't happen.
Make the mouse cursor visible, and have the ship turn at maximum speed to point towards the cursor. Upsides: fairly easy to understand, intuitive. Downsides: trivializes aiming for mouse and keyboard players.
There's one more possibility: removing the design decision to impose a maximum turn rate, give gamepad/Steam Deck players a settings option to change the sensitivity of the joystick, and let mouse and keyboard players pull off 360 no scopes to their hearts' content. I'm open to feedback here.
Bokeh
I've always been planning to have the asteroid fade in effect be more like a camera focus than a simple alpha/brightness fade. There are a few problems with that.
Blurring a sprite involves averaging each pixel's value with the values of its neighbors, weighted appropriately. The larger the blur, the more neighbors each pixel needs to sample and since we're dealing in 2D, that neighbor count increases on both the x and y axes. Blurring lots of sprites by more than a very small amount is something that programs like AfterEffects and DaVinci Resolve will tell you to go make a cup of coffee while they take care of it. It's just not something you want to try to do in real time.
A weighted average is a series of multiplication and addition operations, both of which are commutative (i.e. order doesn't matter, as long as you're doing the multiplication before the addition). So it's possible to do a blur in two passes, one of which only considers the horizontal neighbors, and the other of which only considers the vertical neighbors. The second pass reads the result of the first pass, so each vertical neighbor already has all the horizontal neighbor values baked in.
It's better! For a 64x64 blur kernel, you end up doing only 128 texture reads instead of 4096. Per pixel.
Still far too slow. Texture reads, while optimized in modern hardware, still involve pulling data from memory instead of dealing with registers within the computation unit. That's a lot slower, and reading hundreds or thousands of textures per pixel means you might have been better off writing the game in PowerPoint.
Even if we could do a pixel perfect blur, it would lack something that an actual defocus effect needs: bokeh. With a real camera, objects don't just get blurry when they are out of focus. The overall effect looks that way when dealing with objects that have dimension, but point light sources keep their hard edges and get bigger. In actuality, every "pixel" in the image just gets bigger, but it's most noticeable with high contrast points like lights. It's a minor detail--very minor in the grand scheme of the game--but if I can manage it I'd like to. Unfortunately, properly calculating bokeh is even more involved than doing a single pass blur, so doing that as a shader is right out.
Since the asteroids are all rendered in Blender, it would be quite simple to create an animation of each asteroid where it slowly comes into focus. The animation would need to be 180 frames long, since the game runs at 60FPS and the next stage fade-in time is three seconds. There are ten asteroids of each size, three sizes, and currently two asteroid types. That's over 10,000 animation frames for this effect, over two billion pixels. Setting aside for the moment the limitations of the steam deck, I don't want to have to upgrade the hardware my gitlab server runs on.
But wait! Video cards have been dealing with needing to blur textures for ages. Enter mipmaps.
Mipmaps are a texture optimization that the game engine can take advantage of when a texture is square and a perfect power of two on each dimension. Rather than store just the texture in VRAM, it stores the texture, plus the texture scaled to 1/2, plus the texture scaled to 1/4, and so on until it's down to a single pixel. The engine will calculate which of these textures is actually needed for what will be displayed on screen, pre-caching some of the scaling work that the GPU would have to do otherwise. And if trilinear filtering is turned on, the GPU will smoothly interpolate between two different mipmap levels so that there's no "popping" when a texture crosses a power of two size threshold. This gives a nearly perfect blur for damn near free. The shader to draw a certain mipmap level is a single line of code (plus boilerplate).
Mipmaps are normally automatically generated by the game engine during image import, but they don't have to be. It's possible to create a set of custom mipmaps so you can have effects like a texture slowly changing color as it approaches. Or one could, say, render sucessively smaller and more out of focus versions of each asteroid and assemble them as custom mipmaps. Which is exactly what I did.
It took me a while to get everything tuned how I wanted it, but I've greatly simplified the exploding asteroids from three sprites into a single sprite with two textures (albedo and emissive) and I can cross-fade between the mipmap levels for a pretty convincing focus rack. It still pops a bit compared to a physically accurate effect, but by speeding up the focus part (and having it over-focus, then compensate during the last second of the animation) and adding a brightness increase at the same time I think it actually works quite well. I know exactly where the flaws are and most of the time I don't notice them. Good enough for now.
This was a long detour into performance optimizations, but the sluggishness on the Deck in later stages was really starting to bother me. At the suggestion of several people, I'm going to start working on a hint or tutorial system now.
Sidenote: I have a MacBook on the way, should arrive by the end of next week. This will allow me to test and publish builds for OSX, so the two of you out there who only use Apple products can breathe a sign of relief. You were born for this too.
