Search Unity

Question Replace OnRenderImage with CommandBuffer

Discussion in 'General Graphics' started by CGDever, Feb 25, 2023.

  1. CGDever

    CGDever

    Joined:
    Dec 17, 2014
    Posts:
    156
    Right now my custom post processing effect works at OnRenderImage (...).
    But I need to execute this effect after specific camera event. So, that's why I have to use CommandBuffer:
    Code (CSharp):
    1. buffer = new CommandBuffer();
    2. buffer.name = "Light Adding";
    3. buffer.Blit ( camera.activeTexture, null as RenderTexture, camMaterial, 0 );
    4. camera.AddCommandBuffer(event, buffer);
    I suppose camera.activeTexture is an equivalent of RenderTexture source of OnRenderImage.
    I'm not sure about null as RenderTexture. Is it an equivalent of RenderTexture destination of OnRenderImage?

    Right now post effect is not applied and its influence on final image is invisible.
    My basic question is: how can I reproduce OnRenderImage with CommandBuffer?
     
  2. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    985
    I've been playing around with command buffers for the first time over the last week and just wanted to chime in with some things that caught me off guard just so you don't go down the same hole as me.

    First. You only add the command once at activation of the effect. NOT every frame. If you add it every frame it will stack up new commands and eventually bring your machine to a screeching halt as it starts to perform millions upon millions of additional passes.

    Second, and this really messed with me for a few days, are you using SRP or Built-In? If it's SRP you are wasting your time as you cannot inject command buffers into the camera anymore in that system. To make things even more complicated HDRP and URP use different methods to achieve the same effect. For URP you'll want to look into Render Featrues. For HDRP you'll want to look into Custom Render Passes (I think, I don't actually have any experience with HDRP so I can't say for sure. Just a term that has been popping up for me over the last week or so).
     
    c0d3_m0nk3y likes this.
  3. CGDever

    CGDever

    Joined:
    Dec 17, 2014
    Posts:
    156
    Thank you very much! Yes, I understand a little how the command buffer works.
    I use Built-In pipeline.
    I don't know the reason, but FrameDebugger shows completelly black Deferred Depth texture. Looks like this texture doesn't exist for CameraEvents: AfterGBuffer \ AfterReflections. I'm a bit confused, why my depth texture is black?
    In my Postprocessing script I used cam.depthTextureMode = DepthTextureMode.Depth; Also I added a dynamic direction light with hard shadows mode "ON".
     
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
  5. CGDever

    CGDever

    Joined:
    Dec 17, 2014
    Posts:
    156
    Thank you for your help. I looked precisely and realised _CameraDepthTexture is correct. My fault.
    Well, the problem came from _MainTex of postprocessing shader. It looks completely black, even in FrameDebugger.

    My Blit comand is buffer.Blit( null as RenderTexture, BuiltinRenderTextureType.CameraTarget, material);
    I'm not sure about source texture, is "null as RenderTexture" a correct option of transfering source texture ?

    Update
    I don't know why, but adding this to shader property helped:
    Code (CSharp):
    1. Properties {
    2.         [HideInInspector] _MainTex ("Base (RGB)", 2D) = "black" {}
    3. }
     
    Last edited: Feb 25, 2023