Garry's Mod Wiki

Revision Difference

render_stencils#561351

<cat>Don'tListMePlease Dev</cat> <title>Render Reference - Stencils</title> # Render Library References These pages seek to provide helpful insight into the groups of functions provided by the <page text="Render Library">render</page>. The major function groups are: * <page text="Beams">render_beams</page> * <page text="Minification and Magnification Texture Filters">render_min_mag_filters</page> * <page text="Render Targets">render_rendertargets</page> * **Stencils** # A Brief Preamble It's going to be very helpful to you if you already have a grasp on <page text="Render Targets">render_rendertargets</page> before learning about Stencils. It's not necessarily a hard requirement, but you will have a much easier time mentally "Connecting the dots." To avoid repetition, the shorthand term `byte` is used throughout this page to mean "An <page>Number</page> with an integer value between `0` and `255`, representing 8 bits of data." While this is not a term used in Lua, it is commmon in programming. # What are Stencils? **Stencils are a system to constrain drawing to <page text="Render Targets">render_rendertargets</page> on a per-pixel basis.** Whenever anything is drawn to a Render Target, including the main game view, the Stencil performs logical checks (More on these later) for each pixel that is about to be drawn to and determines whether or not that pixel should be allowed to change. Stencils are a powerful tool that gives fine-grained control over where and when 2D and 3D elements are drawn. Whenever anything is drawn to a Render Target, including the main game view, the Stencil performs logical checks (More on these later) for each pixel that is about to be affected by the draw operation and determines whether or not that pixel should be allowed to change from its current values. ⤶ # What are the Parts of the Stencil System?⤶ ⤶ * The **Stencil Buffer** holds a `byte` for every individual pixel on the Render Target it belongs to. ⤶ * By default, every pixel's Stencil value is `0`.⤶ * The **Reference Value** is a `byte` that Stencil Buffer values are compared against.⤶ ⤶ When a draw operation is performed, each of the affected pixels has its Stencil Buffer value compared to the Reference Value. ⤶ ⤶ * The **Compare Function** determines which of the possible relationships (Greater than, less than, equal to, etc.) between the Stencil Buffer value for a pixel and the current Reference Value should be considered a "**Pass**" and which should be considered a "**Fail**".⤶ ⤶ Depending on if a pixel Passed or Failed the Compare Function, its value is then potentially modified, depending on the current Stencil configuration.⤶ ⤶ * **Operations** modify the Stencil Buffer value for a given pixel depending on if it has Passed or Failed.⤶ Operations include incrementing the current value by one (1), replacing the current value with the Reference Value, making no change, and others. ⤶ <page text="The full list of possible Operations can be found here">Enums/STENCILOPERATION</page>⤶ ⤶ If a pixel Fails the Compare Function, then the ⤶ * The **Fail Operation** will be performed if ⤶ ⤶ ⤶ ⤶ * Masks⤶ ⤶ ⤶ ```⤶ get a list of all pixels affected by this drawing operation⤶ for each affected pixel do⤶ compare the Stencil Buffer value for that pixel against the Reference Value⤶ if the Stencil Buffer value has the same relationship to the Reference Value as the Compare Function then⤶ compare the existing Depth Buffer value for this pixel against the drawing operation's new Depth Buffer value for the same pixel⤶ if the new Depth Buffer value is larger (further away) than the existing Depth Buffer's value then⤶ -- The new value for this pixel is behind the current value, so we should keep the current value⤶ Perform the Z Fail Operation on that pixel⤶ else⤶ -- The new value for this pixel is in front of the old value, so it should be drawn instead⤶ Perform the Pass Operation on that pixel⤶ end⤶ else⤶ Perform the Fail Operation on that pixel⤶ end⤶ end⤶ ```⤶ When performing draw operations, the exact same Stencil logic is used for every individual pixel affected by the draw operation. ⤶ Inbetween draw operations, the Stencil logic can be re-configured to achieve different results.⤶ **Stencils are a multi-part system.** ⤶ **Stencils are a multi-part system.** The **Stencil Buffer** holds a `byte` for every individual pixel on the Render Target it belongs to. By default, every pixel's Stencil value is `0`. The **Reference Value** is a `byte` that all Stencil Buffer values are compared against during draw operations. The **Compare Function** The **Fail Operation** The **Pass Operation** The **Z Fail Operation** The **Test Mask** The **Write Mask**