render_stencil
StencilMap
Stencil buffer target for a view. If a bitmap is assigned to this pointer, the
content of the stencil buffer is copied into this bitmap after rendering, instead
of drawn over the screen. This way postprocessing effects can be applied to the
stencil buffer, such as blurring
stencil shadows. The stencil target texture
is available to shaders through the texture variable
StencilMap.
Type:
BMAP* (lite-c)
texture (shader code)
Remarks:
- The bitmap assigned to render_stencil must have at least the same
size as the screen buffer, i.e. screen_size.x and screen_size.y.
When changing the video resolution, take care that the render_stencil bitmap
does not have a smaller size than the screen buffer. Assign a bitmap that
is large enough for the maximum resolution.
- Assigning a bitmap to render_stencil causes
the stencil buffer content to be drawn into the bitmap regardless if
stencil shadows are used or not. This affects the frame rate, so don't
set this pointer unnecessarily.
-
Set the PROCESS_SCREEN
view
flag for a shader that blends the stencil buffer over the screen buffer. If the view uses a render target or a postprocessing stage, the shader can not blend the stencil buffer over the screen buffer (because there is none); instead the shader must blend the render_stencil over the render_target of the previous view. This is a little more expensive because two samplers are required.
- A7.66
If a shader is assigned to mat_shadow, the render_stencil bitmap is automatically generated and adapts automatically to the screen size (just as render_target). Other than with PROCESS_SCREEN, the stencil buffer is here rendered over the screen buffer immediately after the shadow rendering pass. This has the advantage that the render process is faster and does not affect non-self-shadow entities (see shadow_stencil). An example for a poisson blur shader can be found in code\st_stencilblur.fx.
-
A7.75 If a render_stencil bitmap and/or a mat_shadow shader is assigned, but stencil shadows are inactive, the bitmap is rendered over alll entities of the scene. This way it can be used for the final shadow map resulting from user-defined shadow algorithms, such as PSSM. All predefined stencil shadow postprocessing shaders, such as the poisson filter, can then be applied to the user defined shadow algorithm.
-
When using the render_stencil bitmap, make sure to set the NOSHADOW view flag for all views that should generate, but not receive shadows - otherwise the bitmap is rendered into the scene and interferes with shadow generation.
Edition:
A7.10 C
LC
Example:
function main()
{
...
shadow_stencil = 4; // activate z-fail accelerated stencil shadows
effect_load(mat_shadow,"st_stencilblur.fx"); // autogenerate render_stencil by assigning a stencil shader
...
// st_stencilblur.fx - Poisson blur shader
texture StencilMap; // stencil buffer image in the alpha channel
sampler smpStencil = sampler_state { texture = <StencilMap> };
float4 vecViewPort; // contains viewport pixel size in zw components
static const float fDist = 4.0; // blur factor
static const float fStrength = 0.15; // shadow darkness
static const float2 fTaps_Poisson[12] = {
{-.326,-.406},{-.840,-.074},{-.696, .457},{-.203, .621},
{ .962,-.195},{ .473,-.480},{ .519, .767},{ .185,-.893},
{ .507, .064},{ .896, .412},{-.322,-.933},{-.792,-.598}
};
float4 blurPoissonPS( float2 Tex : TEXCOORD0) : COLOR0
{
float4 Stencil = 0;
for (int i=0; i < NUM_TAPS; i++)
Stencil += tex2D(smpStencil,Tex.xy + fTaps_Poisson[i]*fDist*vecViewPort.zw);
return Stencil * fStrength;
}
technique stencilblur {
pass one {
PixelShader = compile ps_2_0 blurPoissonPS();
}
}
See also:
VIEW, BMAP, view.bmap, view.stage,
view.material,
render_target,
render_zbuffer, PROCESS_SCREEN,
shadow_stencil
► latest
version online