Garry's Mod Wiki

Render Reference - Min/Mag Texture Filters

Render Library References

These pages seek to provide helpful insight into the groups of functions provided by the Render Library.

The major function groups are:

What are Min(ification) and Mag(nification) Filters?

Minification and Magnification filters are a feature of DirectX 9, which the Source Engine (and Garry's Mod, by extension) uses for its graphics rendering.

These two filters handle the very common situation where an image of some kind (a Texture or Material in the Source Engine) needs to be drawn at either a larger or smaller size than its natural size. In either case, there has to be some logic to tell the graphics system how it should stretch (Magnify) or compress (Minify) the image to make it fit the desired size.

In some situations like, for example, a pixel-art game, you may want low resolution pixel-art to be drawn at a much higher resolution on a modern display without the graphics system attempting to smooth the pixels as it stretches them, because smoothing would ruin the pixel-art look of the textures.

In other cases you might have a high resolution image, like a logo, that needs to be minified so that it can be drawn much smaller for something like a business card, or a HUD element without looking like it's missing key details of the design.

While it's not always necessary to make a specific minification or magnification filter choice for everything drawn to the screen, it can make a huge difference in the situations where it is needed.

What Functions are There?

Magnification

Minification

What Filters are There?

There are four (4) filters available via the Texture Filtering (TEXFILTER) enums:

  1. None - This produces a pixelated appearance, similar to Point filtering.
  2. Point - This produces a pixelated look. For more detailed info, see this page.
  3. Linear - This produces a smoothed look. For more detailed info, see this page.
  4. Anisotropic - This filter smooths textures based in part on their angle relative to the camera. For more detailed info, see this page

For a demonstration of how each filter looks, see the example output below.

Usage Example

Example

The following example provides a simple demonstration of each filter on both a magnified and minified version of an image.

-- Calling Material() every frame is quite expensive -- So we call it once, outside of any hooks, and cache the result in a local variable local ourMat = Material( "entities/sent_ball.png" ) local sizeBig = 500 local sizeSmall = 100 local pos = 100 local filters = {} filters[ TEXFILTER.NONE ] = "NONE" filters[ TEXFILTER.POINT ] = "POINT" filters[ TEXFILTER.LINEAR ] = "LINEAR" filters[ TEXFILTER.ANISOTROPIC ] = "ANISOTROPIC" hook.Add( "HUDPaint", "PutAUniqueHookNameHere", function() surface.SetDrawColor( 255, 255, 255, 255 ) -- Set the drawing color surface.SetMaterial( ourMat ) -- Use our cached material -- Show each filter for 1 second each -- (TEXFILTER enums correspond to integer numbers) local filter = math.floor( CurTime() % 4 ) render.PushFilterMag( filter ) render.PushFilterMin( filter ) -- Actually draw the rectangle with a magnification filter surface.DrawTexturedRect( pos, pos, sizeBig, sizeBig ) -- Actually draw the rectangle with a minification filter surface.DrawTexturedRect( pos + sizeBig + 1, pos, sizeSmall, sizeSmall ) render.PopFilterMin() render.PopFilterMag() draw.SimpleText( filters[ filter ], "ChatFont", pos + 1, pos + 1, color_white ) end )
Output: