How does game “optimization” work? Are people sitting there changing lines of code to more “optimal” ones? What is “optimized”?

2.11K views

The recent The Last of Us for PC made me realize I had no idea what’s meant by “optimizing” a game.

Same with optifine in Minecraft improving performance. How do these things work to just make games use fewer resources?

In: 158

105 Answers

Anonymous 0 Comments

It really depends on the game/engine and how it is designed from a technical perspective. Most game engines have something like a profiler (you can just google unity profiler to see an image of what it looks like). While you are running your game in the editor you can use the profiler and it will show how many ms different operations take. That includes executing all the code for gameplay, as well as drawing everything on the screen.

For 3d games usually the most common optimizations come from limiting complex draw calls. [This blog has a really good breakdown](http://simonschreibt.de/gat/renderhell/) of how the graphics pipeline typically works. But basically each draw call is sending data to the GPU and saying “hey I need you to render all this stuff”. The goal is to minimize the number of draw calls you need while still maintaining the visual fidelity and stability of the game.

Some of the most common and easy performance boosts come from culling, basically cutting down what you want the GPU to do to the bare minimum. There are 3 super common types of culling that almost every game uses (unless they are poorly made tbh).

The first and most obvious one is view-frustrum culling, it basically says “if an object is not within the view frame of the camera, do not send it to be rendered”. Most engines do this by default but some actually don’t and you have to make sure it’s enabled.

The second pretty obvious one is called backface culling, which says “if a mesh has faces that are on the back side of the object don’t render them”. There are a few cases where you actually don’t want backface culling for certain special effects (think like xray effects) but for the most part this is another one that almost every game uses.

The last one is a lot more complicated and can be implemented A LOT of different ways, and that’s occlusion culling. Occlusion culling basically says “there is an object blocking this other object, so don’t render it”. That sounds simple in practice but the actual process of doing that can be fairly complicated. Doing it on a per-pixel basis is extremely inefficient and acutally causes more overhead a lot of the time. I mainly have experience with unity, and other engines do things differently, but in unity typically what you do is create an occlusion map, which is basically a voxelized representation of the scene (think turning objects into different sized cubes instead of their normal shape). When the camera looks out into the scene it will cast rays and see what parts of the baked occlusion it collides with. If it collides with one of the occlusion voxels it will basically tell the GPU “OK this space is blocked so anything directly behind it do not render”. It’s a little more complicated than that because you essentially have to tell the engine how small of a gap between those voxels is ok, and what isnt. This is a bit of a balancing act because if your occlusion is too dense it will not run optimized, but if its too sparse you will get things like objects disappearing behind gaps in stair cases for example. With unity occlusion culling applies to an entire mesh, not just certain polygons, other engines work differently, but if you’ve ever played a game where sometimes you look through a narrow gap at a certain angle and objects behind the gap disappear that is occlusion culling happening, just not tuned perfectly.

Occlusion culling gets even more advanced with things called portals, which typically deal with more dynamic spaces and gaps. For instance you might have doorways in your game, and you obviously don’t want to render what is behind the door, but as soon as the door opens you don’t want to see stuff popping in as it slides open. So typically stuff like that is flagged as an occlusion portal, where it can essentially be toggled on and off in the code based on a number of different things.

It’s also not uncommon to have custom occlusion coding set up for smaller objects in games, where they might fade in as you get close enough even though they would technically be visible from further away. Part of optimizing this can also come from “baking meshes” where you take multiple small objects and essentially bake them down into one large object that only contributes once to the draw call instead of a dozen times.

All of this is just scratching the surface of the basics of rendering. There is a lot of other stuff that is done for optimization. A big one is mixed baked/realtime lighting with light probes vs full realtime global illumination (this is the reason why in a game like Warzone for example, the night time map and the day time map are two completely separate files each weighing in at nearly 40gb, because the engine does not use full realtime global illumination, and lightmaps are BIG but efficient and look good). LOD’s are another huge one (level of detail). Especially in big open world games you might have 2-4 different versions of an object. The lowest level is what displays when the camera is right next to the object, and the highest level is what you are looking at from across the map. In some games the highest level LODs go from 3d objects to just flat 2d images of the object. RDR2 has some of the best implementations of distant objects in a recent game to me, and they have a fairly advanced system for fading between LODs so that you never really see the pop-in between different LODs.

I could go on, but yeah i think you get the picture. Shadows are another big one, limiting the number of shadows, which objects get shadows, what lights actually cast shadows, how far away the shadows are drawn. And all this is basically just on the rendering side, this isn’t even touching the optimizing “code” or game logic part of it. At least from almost all the projects i’ve worked on the main optimization comes from graphics optimization and reduction of draw calls. Only really complex games or big multiplayer games need a lot of coding optimization on modern machines (take that with a grain of salt, just speaking from my experience).

edit: just realized this is ELI5 not r/gamedev lmao

You are viewing 1 out of 105 answers, click here to view all answers.