How does game optimization work, particularly for PCs where you can never know what combination of specs the player has? Is it interwoven to every stage of game development or is it only done at the very end right before the game is published?
Optimization generally comes in two flavors: speed and size. Since we have finite time to do things, we want things to run faster in order to do more things in the same amount of time. Similarly, we have finite memory to hold data so we want to make as much use of the limited memory we have as possible by keeping the things we load as small as possible. Most of the time optimizing for speed trumps size because disk space and memory is easier to add than computing speed… but we always want to pack as much as we can into both, so that we can deliver the most game that we can.
At its core, optimization is a question of avoiding unnecessary work. For example, let’s say that we’re working on a first person shooter and the player turns to change the camera. We have a list of all of the entities that exist in the world. We need to figure out whether each entity needs to be drawn. We can simply go through the list and ask “Is there anything in between you and me to stop me from seeing you?” and get an answer, but let’s say that doing the specific check takes a while. If we have to do it for every entity in the game every frame, this can eat up a lot of resources.
But let’s say that we also have a pretty fast condition we can check - let’s say we can ask the entity “Are you behind the player character?” and it will have to answer “yes” or “no”. Suddenly, we can start ignoring a lot of these world entities. After all, if our player can only see what’s in front of her, there is no way that she could see something behind her, right? This means that we should ask this question to every entity that we might see, because it will avoid doing the more expensive “check whether anything is in between us” operation when we don’t have to do it.
Let’s extrapolate on this concept further. One common optimization is using “LoDs” - levels of detail. The idea is that an object that’s very far away from the viewer is hard to see and is only a speck on screen, so we don’t have to render it at full detail because the player can’t see those details anyway. Therefore, we can get away with rendering fewer polygons on the far-away models, and a lot more on the up-close ones. This means that we have to load multiple models for the same entity (each level of detail needs one), but it also means that we are cutting down on the number of polygons the system needs to draw at any given time.
Let’s take this further. Another way of optimization is by minimizing the number of loads we have to do from disk. Hard drives hold a lot of data, but it takes time for the cpu to load the data from them. The reader in the hard drive needs to seek the right position, magnetic disk needs to spin to the right place to be read, then the data needs to be read from the spinning disk and written into memory. This can take a relatively long time. Let’s say that we have our LODs, but we need to load textures for them. Low detail models only need low detail textures, after all. But we want to avoid the set amount of time it takes to load a file. How do we speed it up? What we can do is take all of the textures for the all of the different LOD models and put them all into one big texture. Since we know we’re probably going to use all of the different LOD models, we might as well load all of their textures at the same time too. This means we don’t have to wait for multiple separate loads. These are called Mipmaps.
If you haven’t noticed, these kinds of optimizations apply to all platforms because they are all computers and generally behave the same way. Whether it’s a PC, mobile device, or console, it still has to load stuff from disk, it still has a CPU that runs at a certain speed, and it still uses main memory to load stuff. Don’t get me wrong, there are optimizations we do that are specific to the platform as well, but those tend to be more specific. Most dev teams start optimizing the general stuff as soon as they can, because optimization is part of the planning process too. The goal of optimization is to minimize the resources we use in an intelligent manner. That way things can run faster. Sometimes it means being smart about where to spend our time, and avoiding the expensive operations if we don’t need to do them. Sometimes it’s about using up resources we can spare (such as memory) to save time from loading. Sometimes it’s about trading resources we can spare (like memory) for resource we need (like lowering the number of polygons we have to draw each frame). But it’s all to make things perform more efficiently and that’s always a worthy goal.
- [How do you render a sphere? And other graphics questions]
- [Bump Maps, Normal Maps, Specular Maps, and Ambient Occlusion]
- [The Game Optimization tag on this blog]
Got a burning question you want answered?