0
\$\begingroup\$

I'm a beginner in game programming and I want someone to confirm my understanding.

Let's say there's a 3D model for a character in the game. This character is made up of triangles. Each vertex in these triangles has a position. When I tell the GPU to display this character (like Mario on the screen)...

  1. the GPU will first pass through the vertex shader stage to place every vertex of Mario's model in the correct 2D position on the screen.
  2. After that comes the rasterization stage, which figures out which pixels fall inside each triangle.
  3. Then the fragment shader (or pixel shader) colors each pixel that came out of rasterization.

And that's how Mario appears on the screen.

When I press, say, an arrow key to move the character, all of Mario's vertices get recalculated again by the vertex shader, and the whole process repeats.

This pipeline happens, for example, 60 times per second. That means even if I’m not pressing any key, it still has to redraw Mario 60 times per second.

Everything I just said above: does it have to happen in every game?

\$\endgroup\$
3
  • \$\begingroup\$ Usually, the shaders are going to run every frame whether Mario is moving or not. Some games may pause rendering when nothing is moving, but this isn't practical during gameplay for most games. \$\endgroup\$ Commented Nov 14 at 0:03
  • \$\begingroup\$ so what i said is right? \$\endgroup\$ Commented Nov 14 at 0:32
  • 1
    \$\begingroup\$ You suggested that the shaders only run when you press an arrow key to move Mario. My point was that usually the shaders are running every frame whether you're moving him or not. \$\endgroup\$ Commented Nov 14 at 22:26

2 Answers 2

5
\$\begingroup\$

What you've said is how most games work, at least at a logical level. (In practice, these stages might not happen exactly sequentially - a GPU might be simultaneously drawing some fragments while rasterizing others and transforming vertices for the wave of triangles after that, and a mobile tile-based GPU might work on one tile at a time rather than rasterizing all triangles of the model at once. But the net effect is the same as if those stages happened in exactly that order.)

It's entirely possible for a game to implement on-demand rendering, where the code waits until something changes, and then asks the GPU to draw a new frame only when a change happens (or, to be even more selective, only re-drawing the part of the screen where something changed).

In practice though, lots of games have things changing all the time, even if it's something as simple as background/idle animations, so there's little benefit to implementing this kind of selective rendering. This is especially true in 3D games, where anytime the camera is moving, just about every pixel on the screen will change. The camera tends to move a lot during gameplay, so we need the process of re-drawing every pixel to be fast enough to happen every frame if we want to keep that motion smooth. If we've already made it fast enough to do every frame, then we might as well do it every frame, even if it's not strictly necessary, rather than add extra complications into our code to save that work. It's already part of our frame budget, so we might as well spend it.

The only real downside is when running off of battery, where those redundant frames will drain the battery faster than caching one image until it needs to change. So, if you're making a game targeted for mobile devices where lots of time is spent on screens where little to nothing changes, then in that case it might pay off to implement that logic to re-use frames when nothing is changing.

\$\endgroup\$
3
\$\begingroup\$

For most modern 3D games running on modern hardware, you've described the basics of how rendering usually works. However, that's not the only option. Other options include:

  • Using Mesh Shaders instead of vertex shaders.
  • Doing the vertex processing on the CPU.
  • Fixed function hardware.
  • Not using meshes that consist of vertices and polygons (e.g. voxels, implict surfaces).

So, to answer your final question: No. It doesn't happen the way you described in every game. As an example Doom doesn't work that way.

EDIT: For reference, the main differences in Doom from what you described are:

  1. It's all software rendered (at least in the original PC version), so there aren't really any vertex shaders or pixel shaders.
  2. The characters are all rendered as billboards (i.e. 2D sprites). This is a very common technique, for example it's also used to render the characters in Mario Kart 64.
\$\endgroup\$
1
  • 3
    \$\begingroup\$ If you're going to give Doom as a counter-example, it might be a good idea to explain what it does differently - although I think it's probably better just not to mention Doom at all, rather than confusing a beginning developer with how things were done more than 30 years ago. \$\endgroup\$ Commented Nov 14 at 22:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.