Garry's Mod Wiki

Revision Difference

render_stencils#561358

<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 have a grasp on <page text="Render Targets">render_rendertargets</page> before learning about Stencils. It's not necessarily a hard requirement and you don't need to be an expert, but you will have a much easier time mentally "connecting the dots" if you're familiar with the terms defined there. To avoid repetition, the shorthand term `byte` is used throughout this page to mean "A <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 operations on <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 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 <page text="Reference Value">render.SetStencilReferenceValue</page> 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. `This is the start of a loop over pixels affected by the draw operation` * The <page text="Compare Function">render.SetStencilCompareFunction</page> 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**". <page text="The full list of possible Compare Functions can be found here">Enums/STENCILCOMPARISONFUNCTION</page> * The <page text="The Test Mask">render.SetStencilTestMask</page> is a `byte` that will be <page text="bitwise ANDed">bit.band</page> with all values as they are read from the Stencil Buffer. * By default, the Read Mask is set to make no change to read values. 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> * The <page text="The Write Mask">render.SetStencilWriteMask</page> is a `byte` that will be <page text="bitwise ANDed">bit.band</page> with all values before they are written to the Stencil Buffer. * By default, the Write Mask is set to make no change to written values. * The <page text="Fail Operation">render.SetStencilFailOperation</page> is performed on pixels that Fail the Compare Function. `This pixel is done and the next one can be processed.` For the pixels that Pass the Compare Function, there is one additional check they must Pass before they are allowed to be modified by the draw operation. * The **Depth Test** (or "Z test") compares the pixel's current Depth Buffer value against the Depth value of the draw operation. Unlike the Compare Function, this comparison is fixed and cannot be configured. * The <page text="Z Fail Operation">render.SetStencilZFailOperation</page> is performed on the Stencil Buffer for pixels where the current Depth Buffer value is less than the draw operation's proposed depth value. This indicates that the draw operation should not be visible on this pixel because it is drawing behind the current pixel. `This pixel is done and the next one can be processed.` If a pixel has passed both the Compare Function as well as the Depth Test, it is considered Passed and will have its contents replaced/modified by the draw operation. This will affect the Color, Depth, and Stencil values for the pixel. * The <page text="Pass Operation">render.SetStencilPassOperation</page> is performed on the Stencil Buffer for pixels that have passed both the Compare Function and the Depth Test and are being updated by the draw operation. `This pixel is done and the next one can be processed.` The following flowchart outlines the same information as the text above:⤶ The following flowchart outlines the same information as the text above to aid in understanding.⤶ <image src="b2b4c/8dc453bdd5b621c.png" size="52831" name="StencilSystemFlow.png" /> # How Can Those Parts Be Used to Make Things? Man fucked if I know