Catch-up: For campaign mode, SnwScf needs bigger arenas/levels. This means (1) much wider voxel snow coverage (e.g. across mountains) which mandates LOD levels (level of detail) and (2) removing/replacing snow as the player(s) move. All in all, lots of optimizations, etc. Some of the funnest work 😀
This one’s all about fixing then optimizing the replay of actions (dig, build, etc). This is used when voxels are (a) tidied due to being further away than we wish to keep or (b) when we switch their LOD levels = a lot more common!
At the start, it didn’t work. This was mostly due to other work I’d done on the codebase while not worrying about this functionality since I hadn’t been using it. With small arenas I never let the snow be tidied — I just kept it all! Obviously this meant larger load on CPU, Memory and GPU! In past posts (1, 2 & 3) I talked about improving with LOD, etc. Now the voxels can be swapped-out to lower LOD levels so we definitely need it!
Here’s out test patch of snow. To keep things consistent, I saved the snow so I can reload it every time. This is a slight over-complication — it actually stores all of the generation settings and simply loads them then does generation and gets the same result every time!
Here are the numbers:
|Total time (ms)||Percentage improvement||Num replay calls||Num DoOperation() calls||Comments|
|(didn’t work)||Original (didn’t work)|
|752||-83%||78||4992||(1) Switched to Chunk-per-call (rather than whole height each time)|
|(not-timed)||6||4144||(2) Optimized what was considered actually modifying a ChunkData.|
|53||-99%||6||1120||(3) Bounded start & end bounds by original sizes|
|8||Blocks to a side in a Chunk|
|8||Height of this operation (Chunks)|
|4||World height (Chunks)|
And some comments on what I did:
- The original code found all ActionData instances that had affected a Chunk then re-ran it for every column… but it didn’t bound the height of the column so it was affecting each Chunk as many times as the number of vertical Chunks it had affected!! (e.g. ActionData touched 3 Chunks high, all 3 Chunks would be done 3 times!?) This seems a bug in the original code.
- The original ActionData code considered a Chunk touched if it was within certain bounds even if it didn’t change the isovalue. This might have been for painting? Even that guess is a bit of a stretch — I can’t really see any reason for it so I ‘fixed’ it. I don’t use it anyway so meh. It’s easy enough to revert if I offer these changes back to the die-hard users of TerraVol (if there are any).
- So previously we’d operated on *every* Block within the Chunk. For the edge ones, that was superfluous. Instead, I bounded the Blocks affected by the original size affected. Great improvement — albeit most useful on edge Chunks which this test-case has lots of. A larger operation will be more inner ones but I have ideas for that!
(care to guess along at home … or write in on a stamped self-addressed envelope 😉 )
Here are a couple of pictures of the operation. The white boxes are Chunk boundaries and the purple boxes are the reduced bounds that are now operated upon 🙂 If you’re wondering why the purple boxes extend outside the green capsule, it’s because the isovalues are smoothed over that range from no-effect to full-effect to produce a smooth result at about the threshold where the green capsule is drawn.
53ms is actually still a long time and needs reducing / parallelizing but it’s a lot better. To give you an idea — this is a tiny operation. Most are much larger. This one’s equivalent to rolling a small snowman 1 meter. A carrocket hit at smallest level would be about 100 times larger.
Now this is all done off the main thread so it’s not so bad however it’s all done on a Generation thread (which has responsibility for getting the data ready before the Builder threads turn that into a mesh). Sadly, the way things are structured at the moment, the Builder can time-out if the Generation takes too long and won’t notice a change until the camera moves sufficiently far. Also generally I’ve tended to only need 1 or 2 Generation threads whereas what we’re really doing here is ActionData application — which I have tended to have 4 threads for since it’s done a lot! It would feel a lot cleaner to move this re-application of ActionData to the ActionDataThread then get the BuilderThread to be informed when the Chunks are ready for meshing. So that’s next!
Onwards and faster-wards 😉