Search Unity

What does blend mode do that makes it faster than a grabpass {"TextureName"}

Discussion in 'Shaders' started by Gogeric, Oct 22, 2014.

  1. Gogeric

    Gogeric

    Joined:
    Apr 5, 2013
    Posts:
    7
    ...aaand what can i do if i want to control blending my own way inside CGPROGRAM/ENDCG ?

    the intended use here is to have a single shader for all my specialFX and be able to use multicompile to set the blending : Alpha blended/Additive/Multiply

    That way the artist can create the material set his textures use the same shader everywhere and just select the blending he wants

    the reason for me to have a single shader is purely practical, if a change need to be made i make it only in a single place, if i want to apply a filter to all my textures i can do just that in a single shader (instead of 3/4)

    and finally what would be the downside of having a multicompile taking care of the blending (without a grabpass if anyone knows a cheaper way)
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Blending occurs on the OUTPUT from your shader AFTER you have finished your shader running. It is performed by the blending hardware, to mix your output with the existing contents of the backbuffer. You set up what is GOING to happen using the blend mode, blend func, etc... then once your shader is done and it's given an output color RGBA to the rest of the pipeline, it will then blend it with the backbuffer. You cannot do this inside the shader itself without somehow getting the backbuffer into a texture and feeding that texture into the shader.

    Grab Pass is a whole different thing, whereby it's going to grab the backbuffer into a texture first and pass it into the shader for you.
     
  3. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    The issue with blending is that it's technically very hard to read and write to the same buffer in a massively parallel environment such as the GPU. That's why the job is left only to non-programmable hardware processors, ROP units. There are cases when you need to do your own blending, though, or something even funkier. For that, as a workaround, Unity provides grab passes, which tell the renderer to make a copy of the frame buffer you're rendering into and pass it as an input to your next shader passes. Since it's just a copy, you can read from it in your shader and do whatever you please. You can see why that is so slow, though.. making a copy of the whole frame buffer, tens of megabytes in size, simply takes a while.

    I suppose you know that in Unity's shaders, more often than not, you write something like include "UnityCG.cginc", which allows you to access many functions unity devs have premade for you. Great thing about this is that you can make your own .cginc files, which are just text files with pure shader code in them (What you would normally write between CGPROGRAM and ENDCG). You can them include them in all your other shaders in the same directory and use the functions defined there. It's the ultimate solution for code duplication :)
     
  4. Gogeric

    Gogeric

    Joined:
    Apr 5, 2013
    Posts:
    7
    Thanks a lot both of you i think that by adding your answers i get the full answer to what i was asking,
    Actually i think that if it was possible it would be nice to be able to do multi-compilation for the blending modes also