Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question CommandBuffer.Blit and BlitFullscreenTriangle

Discussion in 'Shaders' started by WayneJP, Dec 3, 2022.

  1. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    Currently I was writing custom blur post-processing effect. I follow the official doc and write following code, but only get a white triangle and black background:

    Code (CSharp):
    1.  
    2. ...
    3.  
    4. public override void Render(PostProcessRenderContext context)
    5. {
    6.    var cmd = context.command;
    7.  
    8.    ...
    9.  
    10.    var sheet = context.propertySheets.Get(Shader.Find("Custom Blur"));
    11.    context.GetScreenSpaceTemporaryRT(cmd, downSample, 0, context.sourceFormat, RenderTextureReadWrite.Default, FilterMode.Bilinear, rtW, rtH);
    12.    cmd.BlitFullscreenTriangle(context.source, downSample, sheet, 0);
    13.  
    14.    for (int i = 0; i < settings.blurIterations; i++)
    15.    {
    16.        float iterationOffs = (i * 1.0f);
    17.        sheet.properties.SetVector("_Parameter", new Vector4(settings.blurSize * widthMod + iterationOffs, -settings.blurSize * widthMod - iterationOffs, 0.0f, 0.0f));
    18.        // vertical blur
    19.        context.GetScreenSpaceTemporaryRT(cmd, rt, 0, context.sourceFormat, RenderTextureReadWrite.Default, FilterMode.Bilinear, rtW, rtH);
    20.        cmd.BlitFullscreenTriangle(downSample, rt, sheet, 1);
    21.        cmd.ReleaseTemporaryRT(downSample);
    22.        // horizontal blur
    23.        context.GetScreenSpaceTemporaryRT(cmd, rt2, 0, context.sourceFormat, RenderTextureReadWrite.Default, FilterMode.Bilinear, rtW, rtH);
    24.        cmd.BlitFullscreenTriangle(rt, rt2, sheet, 2);
    25.        cmd.ReleaseTemporaryRT(rt);
    26.    }
    27.  
    28.    cmd.BuiltinBlit(rt2, context.destination);
    29.    cmd.ReleaseTemporaryRT(rt2);
    30. }
    31.  
    I change the code to following:
    Code (CSharp):
    1.  
    2. public override void Render(PostProcessRenderContext context)
    3. {
    4.    var cmd = context.command;
    5.    ...
    6.  
    7.    context.GetScreenSpaceTemporaryRT(cmd, rt, 0, context.sourceFormat,      RenderTextureReadWrite.Default, FilterMode.Bilinear, rtW, rtH);
    8.    context.GetScreenSpaceTemporaryRT(cmd, rt2, 0, context.sourceFormat,      RenderTextureReadWrite.Default, FilterMode.Bilinear, rtW, rtH);
    9.    context.command.Blit(context.source, rt, new Material(Shader.Find("Custom Blur")), 0);
    10.    context.command.Blit(rt, rt2, new Material(Shader.Find("Custom Blur")), 1);
    11.    context.command.Blit(rt2, context.destination, new Material(Shader.Find("Custom Blur")), 2);
    12. }
    13.  
    And it works ok. I don't know why BlitFullscreenTriangle command doesn't work expected. Do I miss something? The doc seems sparse.
     
    Zebadiah likes this.