Draw call

From Valve Developer Community
Jump to navigation Jump to search

To draw geometry on the screen, the game issues draw calls to the graphics API. A draw call tells the graphics API what to draw and how to draw it. Each draw call contains all the information the graphics API needs to draw on the screen, such as information about textures, shaders, and buffers. Draw calls can be resource intensive, but often the preparation for a draw call is more resource intensive than the draw call itself.

Todo: Describe how draw calls affect brush faces and displacements due to lightmap assignment pages. Could this be improved with custom map compilers to allocate lightmap pages with draw calls in mind?
Todo: Investigate draw call behavior sprites, particles, water, VGUI, RTT shadows and flashlight.

Measurement

To measure draw calls, the engine has a built-in counter under VProf. To enable it, set sv_cheats 1 then set vprof_counters to 1. DrawIndexedPrimitive in the top right represents the draw call count.

Icon-Bug.pngBug:FPS should be capped to 30 (via fps_max 30) for stable results, as the counter doesn't seem to refresh correctly at higher frame rates for unknown reasons.

Various commands can be used to hide categories of geometry to understand the count better. Quick list of some available commands:

  • r_drawworld - Toggle world brushes
  • r_drawdisp - Toggle displacements
  • r_drawstaticprops - Toggle static props
  • r_drawentities - Toggle entities
  • r_drawdecals - Toggle decals
  • r_drawdetailprops - Toggle detail props
  • r_drawsprite - Toggle sprites
  • r_drawparticles - Toggle particles
  • r_drawropes - Toggle ropes
  • r_drawviewmodel - Toggle viewmodel
  • r_shadows - Toggle shadows
  • r_3dsky - Toggle 3D sky
  • r_renderoverlayfragment - Toggle overlays
  • cl_drawhud - Toggle VGUI

Optimizing

Merging materials

Models, such as from static props or entities are the most frequent source of draw calls. Every model in the game will issue a new draw call. If there are multiple materials in a prop, a new draw call is issued for each material. By combining materials together into one, these draw calls can be reduced. Most props authored by Valve consist of only one material. In later games like Counter-Strike: Global Offensive, different props share the same material for the purpose of static prop combine (see below).

The engine automatically batches materials for decals together.

Todo: Check if this is true for overlays

Static prop combining

Combining static props is another tactic to reduce draw calls. The engine does not support automatic batching of models by the same material, so this process has to be done at compile time. Counter-Strike: Global Offensive supports static prop combine in VBSP. Valve claimed that by employing static prop combine in their CS:GO maps (which are very heavy on static prop usage), they gained a 40% performance improvement. However, this process has been noted for being difficult to use by the community as it requires the source files for all props organized in a specific structure on disk. Alternatively, Hammer++ Hammer++ can merge models together inside the editor using its built-in Propper++ tool.

Sharing cubemaps

Cubemaps can be an unexpected source of draw call count. Cubemaps that are assigned to the world on brushes/displacements require a copy of that material to be generated that references this cubemap. VBSP does optimize this by sharing copies of a material that reference the same cubemap. If there is a lot of big displacements spread out with the same material where the cubemap is subtly visible, it may be better to assign a singular cubemap to all these faces rather than having automatic selection of cubemaps.

See also

Unity documentation - More general, detailed information on draw calls