Search Unity

CommandBuffer.SetRenderTarget doesn't works on Android

Discussion in 'General Graphics' started by arty_unity, May 19, 2021.

  1. arty_unity

    arty_unity

    Joined:
    May 7, 2019
    Posts:
    2
    The goal:

    In custom SRP I need to render offscreen lowres buffer. It would be used by some shader on scene.

    The idea:
    1. Create temporary render texture with lower size
    2. Set it as render target
    3. Render something
    4. Switch render target back to camera target
    5. Render shaders, that needs this render texture
    6. Release render texture
    To simplify, on step 3 I just call ClearRenderTarget() to fill texture with some color.

    The code:

    Pipeline code is very basic with just Renderer.Render() call.
    Code (CSharp):
    1. private readonly CameraRenderer cameraRenderer = new CameraRenderer();
    2.  
    3. protected override void Render(ScriptableRenderContext context, Camera[] cameras) {
    4.     var pipelineAsset = GraphicsSettings.currentRenderPipeline as CustomRenderPipelineAsset;
    5.     if (pipelineAsset == null) return;
    6.     foreach (var camera in cameras) {
    7.         cameraRenderer.Render(context, camera, pipelineAsset);
    8.     }
    9. }

    Renderer draws the offscreen buffer first, than sets render target to cameraTarget and draws scene content.
    Code (CSharp):
    1. public void Render(ScriptableRenderContext context, Camera camera, CustomRenderPipelineAsset pipelineAsset)
    2. {
    3.     // Setup camera, buffer, etc.
    4.     DrawOffscreenBuffer(pipelineAsset);
    5.     DrawVisibleGeometry();
    6.     Cleanup();
    7.     // Execute buffer and submit context.
    8. }
    9.  
    10. private void DrawOffscreenBuffer(CustomRenderPipelineAsset pipelineAsset)
    11. {
    12.     var renderScale = pipelineAsset.renderScale;
    13.     buffer.GetTemporaryRT(renderBufferID,
    14.         (int) (camera.pixelWidth * renderScale),
    15.         (int) (camera.pixelHeight * renderScale), 24);
    16.     buffer.SetRenderTarget(renderBufferID,
    17.         RenderBufferLoadAction.DontCare,
    18.         RenderBufferStoreAction.DontCare);
    19.  
    20.     buffer.ClearRenderTarget(true, true, Color.cyan);
    21.  
    22.     buffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
    23.     ExecuteBuffer();
    24. }
    25.  
    26. private void DrawVisibleGeometry()
    27. {
    28.     // Draw scene content.
    29. }
    30.  
    31. private void Cleanup()
    32. {
    33.     buffer.ReleaseTemporaryRT(renderBufferID);
    34. }

    Shader inverts the color
    Code (CSharp):
    1. sampler2D _RenderBuffer;
    2. half4 _RenderBuffer_TexelSize;
    3.  
    4. interpolators vert(vertexInput v)
    5. {
    6.    interpolators o;
    7.    o.clipPosition = UnityObjectToClipPos(v.objectPosition);
    8.    o.backgroundPosition = ComputeGrabScreenPos(o.clipPosition);
    9.    return o;
    10. }
    11.  
    12. half3 frag(interpolators i) : SV_TARGET
    13. {
    14.    half3 outputColor;
    15.    outputColor.rgb = 1 - tex2Dproj(_RenderBuffer, i.backgroundPosition);
    16.    return outputColor;
    17. }

    The result:

    UnityEditor on Windows directX - how it should look like.
    2021-05-19_0001.png

    Frame debugger info.
    2021-05-19_0002.png


    Android OpenGLES 3 - for some reason render texture is black (shader inverts the color).
    Screenshot_20210519-143020_RenderTargetTest.jpg

    But in the frame debugger it looks the same as in the first case.
    2021-05-19_0003.png

    So, questions:
    1. Why SetRenderTarget on OpenGLES3 doesn't do what it should do?
    2. Is the problem really in SetRenderTarget, or it can be somwhere else?
     
  2. unityl2

    unityl2

    Joined:
    Feb 2, 2022
    Posts:
    6
    Did you find any solution to this problem?
     
  3. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Have you tried setting the "RenderBufferLoadAction" to load and "RenderBufferStoreAction" to store?

    You should explicitly set the load & store actions on mobile platforms, or it'll be set to don't care.
     
  4. unityl2

    unityl2

    Joined:
    Feb 2, 2022
    Posts:
    6
  5. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Perhaps it's not the same issue. Have you tried testing your effect on desktop OpenGL?
     
  6. unityl2

    unityl2

    Joined:
    Feb 2, 2022
    Posts:
    6
    It works in editor.
     
  7. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    I suggest making a bug report with a project that can reproduce the issue.