Material Optimization

From Valve Developer Community
Jump to navigation Jump to search
English (en)Español (es)Translate (Translate)
Optimization (level design)

Shader Fallbacks

(This section is only relevant if you are creating new materials. Skip it?)

Todo: Document $fallbackmaterial.

It goes without saying that not all PCs are equal. Your materials will need to look good on a wide range of specifications, and the best way of achieving acceptable performance at the low-end without sacrificing fidelity at the high-end is with shader fallbacks.

The good news is that Valve's stock materials are already set up with fallbacks, where appropriate, and in most cases you won't need to do any work in this area at all. The even better news is that if you do need to create a fallback, all of the hard work of benchmarking different hardware and graphics settings has already been done by Valve and all you need do is choose between a handful of coarse DirectX levels and other miscellaneous graphics settings.

Usage

Shader fallbacks are parameters of the material like any other, and should go inside the material's original pair of curly braces ({ and }).

Fallback Shaders

(in all games since Source 2007)
"<=dx90"
{
	<VMT Parameters to set>
}
Warning.pngWarning:You must wrap conditional statements in quotes if an equals sign = is used in it like the example above, or it will create a parsing error.

The parameters within a fallback are only applied if its opening statement is true. Accepted values include:

Name Condition Notes
<DX95 Below dxlevel 95
<DX90 dxlevel 90
<=DX90
>=DX90
>DX90
>=DX90_20b dxlevel 92
<DX90_20b
dx9 Ditto of >=DX90 for legacy compatibility
hdr
hdr_dx9 Ditto of hdr for legacy compatibility
ldr
360 Game is running on the Xbox 360
srgb Game is using correct sRGB blending
GPU<1 Comparison of gpu_level ConVar (in all games since Left 4 Dead) (not in Source 2013)
GPU<2
GPU>=1
GPU>=2

Fallback Shaders (Legacy)

Fallback Shaders (removed since Left 4 Dead)
Note.pngNote:The following material, created before conditionals were added to the engine, is actually a bad example. It is not future-proof: systems running HDR_DX9 will not see normal mapping because it is only defined for DX9 and DX8 mode. To avoid this issue in your own materials, use the conditional statements above or target high-end computers in the main body of the material, and use fallbacks only to remove/replace/tweak features for the benefit of older machines.
LightmappedGeneric_DX8
{
	<DX8 parameters>
}

While a "top-level" shader is called in the first line of a material, the actual job of rendering is passed down by the engine to a specific 'family member'. You can pass commands that only apply to a single child shader by providing its name. There is a full list of fallback shaders here.

Example

You can clear a value out by typing "" (two quotes, nothing between).

LightmappedGeneric
{
	$basetexture metal/metalwall078a
	$surfaceprop metal

	$envmap	env_cubemap
	$envmaptint "[.56 .56 .75]"
	$envmapcontrast 1
	$envmapsaturation 1

	LightmappedGeneric_DX9
	{
		$bumpmap metal/metalwall078a_normal
		$normalmapalphaenvmapmask 1
		$envmaptint "[.09 .1 .12]"
	}

	LightmappedGeneric_DX8
	{
		$bumpmap metal/metalwall078a_normal
		$nodiffusebumplighting 1
		$normalmapalphaenvmapmask 1
	}

	LightmappedGeneric_NoBump_DX8
	{
		$basetexture metal/citadel_metalwall078a
		$basealphaenvmapmask 1
	}
}

Conditional Tests

(in all games since Source 2007)
"srgb?$color" "[1.2 1.2 1.2]"
"!srgb?$color" "[0.8 0.8 0.8]"

There are also conditional tests that can also be applied directly to variables and inverted with a preceding exclamation point !.

Name Condition Notes
hdr
ldr
360 Game is running on the Xbox 360
srgb Game is using correct sRGB blending
srgb_gameconsole (in all games since Counter-Strike: Global Offensive)
srgb_pc
lowfill mat_reduceparticles ConVar is true (in all games since Left 4 Dead) (also in Source 2013) Has nothing to do with mat_reducefillrate
SonyPS3 Game is running on the PlayStation 3 (in all games since Left 4 Dead) (not in Source 2013)
gameconsole
GPU<1 Comparison of gpu_level ConVar
GPU<2
GPU<3
GPU>=1
GPU>=2
GPU>=3
HighQualityCSM CSM quality
LowQualityCSM

Overdraw

Using mat_fillrate to investigate overdraw. Problem areas end up bright red.

Fillrate refers to the number of pixels that a video card can shove into its memory per second, independently of the time needed to decide what color they should be. It becomes an issue when the user tries to run a game at a higher resolution than the card is capable of, or more commonly when a given pixel is being processed and re-processed many times.

This latter effect is known as "overdraw" and occurs when a material is translucent (particles are dreadful), uses normal mapping, uses specular reflections, and/or is refractive. Dynamic lights and effects like motion blur and HDR (but not color correction) are also culprits. You can spot overdraw by checking showbudget for an unusually large "Swap Buffer" bar, and examine its effect precisely with mat_fillrate.

The only true solution to overdraw is reducing the number of pixels that needs to be re-processed. This can be done with a shader fallback, by creating fewer objects, or by reducing screen resolution. There are also a few tricks that can be employed, albeit to limited effect, which follow.

Tip.pngTip:To temporarily work around fillrate issues, use mat_viewportscale.

Particles

See Particle Performance.

Refraction

Any object that crosses the plane of a refractive material will be rendered twice: once for the direct view, and again for the refracted view (it's also rendered for reflection, but that can't be helped). This becomes avoidable when the water is obscured—for instance, if the object is embedded in a displacement sand drift. By raising the model or cutting the water brush so that it doesn't extend beneath the displacement, a whole rendering pass can be avoided.

You can examine what is being rendered for refraction with mat_showwatertextures 1.

Normal Mapping

LightmappedGeneric materials are the prime suspects here. To help a little, you can add $nodiffusebumplighting 1 to the material in question, but as ever, the only true solution is a fallback.

Creating SSBumps instead of generic normalmaps may save on performance slightly.

Tip.pngTip:When creating DirectX 8 fallbacks, use LightmappedGeneric_NoBump_DX8 to differentiate between video cards that have and haven't been deemed suitable for normal mapping by Valve.

Water

With Water, overdraw is caused by both refraction and reflection, often over a large area. You can disable either or both if needed:

$forcecheap 1
Disables both reflection and refraction.
$refracttexture
$reflecttexture
Using these commands without passing a texture bypasses one or both.

If you've got fillrate and CPU to burn, you can forcibly make the water do local reflections regardless of video config settings by specifying $forceexpensive 1, and you can forcibly make the water reflect entities by specifying $reflectentities 1.

Special Materials

Nodraw

Main article:  Nodraw
tools\toolsnodraw

tools\toolsnodraw prevents a surface from being drawn at all, prevents lightmaps from being compiled for it, and prevents it from reflecting light (it still casts shadows). Use it on surfaces that will never be seen by the player but can't be sealed off by another brush, such as the tops of roofs and ledges or the far side of building in inaccessible areas, particularly those in background maps.

Black

tools/toolsblack is a material that behaves like nodraw but has a color value. It's useful at the bottom of deep pits, in inaccessible caves, etc.

Skybox 2D

A 3D Skybox is drawn in full whenever the tools\toolsskybox material is being drawn. You can avoid this by using tools\toolsskybox2d in its place. Obviously, this only has an effect when the normal skybox material isn't visible, nor does it have any effect when there is no 3D skybox.