» Tutorial Effects - Multiple Passes
This site relies heavily on Javascript. You should enable it if you want the full experience. Learn more.

Tutorial Effects - Multiple Passes

Italian | Mandarin | Japanese

TOC: Of Effects and Shaders
Back: Multiple Textures
Next: Vertexshader Preparations


While effects can have multiple passes per technique as outlined in EX9.Effect.File there is no way (in vvvv) to access the color returned in the pixelshader of pass 1 in pass 2. For such kind of multipass effects we need to go the round via Renderer (EX9) and using its output as a texture via DXTexture (EX9.Texture) as an input to another effect.

Effect Chain

As promised in Neighbouring Pixels here is how to chain the horizonal and vertical blurs in a way to achieve a perfectly smooth blur in both directions.

The ouput of renderer Pass 1 is taken as a texture whose resolution is set to correspond to the original image. This texture is taken as input to the vertical blurring effect in Pass 2. Like this you can chain any number of effects.

Feedback

Using this technique you can of course also create effects that take the output they create back via a texture input, thus creating a feedback.

An example for such an application would be Cellular Automata the most popular of which is John Horton Conway's "Game of Life".

Rather than a game it is actually more a simulation based on a grid of cells (much like pixels of a texture) that can either be dead (black) or alive (white). The simulations result is essentially a black texture with some white pixels. Using the result as a starting point for the next generation of the simulation creats a feedback and lets the simulation run forever. Of course this needs an initial state to start from which is realized in this example using the mouse to add living cells, see the according patch in of-effects-and-shaders

The Game of Life has 3 simple rules:

  • A dead cell with exactly three live neighbors becomes a live cell (birth).
  • A live cell with two or three live neighbors stays alive (survival).
  • In all other cases, a cell dies or remains dead (overcrowding or loneliness).

So in the pixelshader for every pixel we have to count its living neighbours and decide according to the rules if the pixel will live or be dead in the next generation.

float2 PixelSize;
float4 PS(vs2ps In): COLOR
{
    //sample the current pixel
    float4 center = tex2D(Samp, In.TexCd);
 
    //prepare pixel offsets for x and y
    float2 offX = float2 (PixelSize.x, 0);
    float2 offY = float2 (0, PixelSize.y);
 
    //sample neighbours and add up their states
    float alive = 0;
    alive += tex2D(Samp, In.TexCd - offY).r;
    alive += tex2D(Samp, In.TexCd - offY - offX).r;
    alive += tex2D(Samp, In.TexCd - offY + offX).r;
    alive += tex2D(Samp, In.TexCd + offY).r;
    alive += tex2D(Samp, In.TexCd + offY - offX).r;
    alive += tex2D(Samp, In.TexCd + offY + offX).r;
    alive += tex2D(Samp, In.TexCd - offX).r;
    alive += tex2D(Samp, In.TexCd + offX).r;
 
    //a dead cell with exactly three live neighbors becomes a live cell (birth).
    if (center.r == 0 && alive == 3) 
    {
      center.rgb = 1;
    } 
    //a live cell with two or three live neighbors stays alive (survival).  
    else if (center.r == 1 && (alive == 3 || alive == 2)) 
    {
      center.rgb = 1;
    } 
    //in all other cases, a cell dies or remains dead (overcrowding or loneliness).
    else 
    {
      center.rgb = 0;
    }
 
    return center;
}

Another important thing to notice in the effect are the sampler state settings:

sampler Samp = sampler_state    //sampler for doing the texture-lookup
{
    Texture   = (Tex);          //apply a texture to the sampler
    MipFilter = NONE;         //sampler states
    MinFilter = POINT;
    MagFilter = POINT;
};

Note how all the filtering is disabled. This allows the truely working with unique pixels and not letting the graphiccard interpolate between pixels which would make no sense in this application where a pixel must only be black or white, but no shade gray.


Next: Vertexshader Preparations
Back: Multiple Textures
TOC: Of Effects and Shaders

anonymous user login

Shoutbox

~2d ago

cznickesz: @tonfilm thanks for the info. Can´t wait to give it a try!

~3d ago

tonfilm: @cznickesz #vvvv #vl #xenko integration is not ready for the public yet, it's still under heavy development... more updates soon.

~3d ago

cznickesz: Possibly dumb question: is there any release containing xenko-integration right now?

~4d ago

~5d ago

~6d ago

joreg: looking for latest #vvvv gamma preview? it is here: vvvv-gamma-2019.1-preview

~6d ago

udo2013: I just want to know why the camera does not work. should be easy to be answered.

~6d ago

udo2013: an answer, please. also there was a 4d julia set in dx9. that must have programmed one. haalllooo!!

~6d ago

udo2013: where are the people who have translated shadertoy's mandelbulb in hlsl ?? for vvvv