Search Unity

ShaderGraph with premultiply blending is not really premultiplied

Discussion in 'Universal Render Pipeline' started by SampsaPlaysome, Jun 23, 2020.

  1. SampsaPlaysome

    SampsaPlaysome

    Joined:
    Oct 20, 2019
    Posts:
    34
    Hello,

    We have been struggling to get premultiplied effects done in ShaderGraph to work, and it turns out the problem is in Universal Render Pipeline package.

    I looked into the com.unity.render-pipelines.universal@7.4.1\Editor\ShaderGraph\Includes\UnlitPass.hlsl, and found this:

    Code (CSharp):
    1. half4 frag(PackedVaryings packedInput) : SV_TARGET
    2. {  
    3.     Varyings unpacked = UnpackVaryings(packedInput);
    4.     UNITY_SETUP_INSTANCE_ID(unpacked);
    5.     UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
    6.  
    7.     SurfaceDescriptionInputs surfaceDescriptionInputs = BuildSurfaceDescriptionInputs(unpacked);
    8.     SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
    9.  
    10. #if _AlphaClip
    11.     clip(surfaceDescription.Alpha - surfaceDescription.AlphaClipThreshold);
    12. #endif
    13.  
    14. #ifdef _ALPHAPREMULTIPLY_ON
    15.     surfaceDescription.Color *= surfaceDescription.Alpha;
    16. #endif
    17.  
    18.     return half4(surfaceDescription.Color, surfaceDescription.Alpha);
    19. }
    Apparently when in premultiply mode, the generated shader does the multiplying for us. This is practically same as alpha-blended mode. Only difference is that blending mode in the shader is different, but from the user's perspective the ShaderGraph shader acts like alpha-blended shader.

    If I comment out this:

    Code (CSharp):
    1. #ifdef _ALPHAPREMULTIPLY_ON
    2.     surfaceDescription.Color *= surfaceDescription.Alpha;
    3. #endif
    it works as expected.

    This is clearly a bug, and should be fixed.

    However, as this has been broken for a long time now, makes me wonder if there is some fundamental issue to be solved...
     
    SolidAlloy likes this.
  2. SampsaPlaysome

    SampsaPlaysome

    Joined:
    Oct 20, 2019
    Posts:
    34
    We solved this as clean as we could by creating a forked copy of URP package with the fix, and switching that in place in manifest.json

    But it would be great to have an official confirmation that the thing is broken.