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

Camera.SetTargetBuffers - Correct usage for MRTs?

Discussion in 'Editor & General Support' started by Geenz, Dec 31, 2013.

  1. Geenz

    Geenz

    Joined:
    Sep 1, 2010
    Posts:
    99
    Hello Unity Community! It's been a while.

    So as of late, I'm working on a deferred renderer for a small project, and I can't quite seem to get Camera.SetTargetBuffers working correctly! Every time I attempt to run the script, I get the following in the console along with the editor's GUI deciding to go blank shortly there after:
    Code (csharp):
    1. Releasing render texture that is set to be RenderTexture.active!
    2. RenderTexture warning: Destroying active render texture. Switching to main context.
    I'm not entirely sure why this is!

    Here's some code to give an idea of what I'm doing to trigger this:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Camera))]
    5. public class DeferredCamera : MonoBehaviour {
    6.     private RenderTexture[] mrtTex = null;
    7.     private RenderBuffer[] mrtRB = new RenderBuffer[3];
    8.     private GameObject dummyCam = null;
    9.     public Shader geometryBufferShader = null;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         mrtTex = new RenderTexture[3] {
    14.             new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32),
    15.             new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32),
    16.             new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32)
    17.         };
    18.         mrtRB[0] = mrtTex[0].colorBuffer;
    19.         mrtRB[1] = mrtTex[1].colorBuffer;
    20.         mrtRB[2] = mrtTex[2].colorBuffer;
    21.         dummyCam = new GameObject("Dummy Camera", typeof(Camera));
    22.         dummyCam.hideFlags = HideFlags.HideAndDontSave;
    23.     }
    24.  
    25.     void OnPreRender()
    26.     {
    27.         dummyCam.camera.SetTargetBuffers(mrtRB, mrtTex[0].depthBuffer);
    28.         dummyCam.camera.clearFlags = CameraClearFlags.SolidColor;
    29.         dummyCam.camera.RenderWithShader(geometryBufferShader, "RenderType");
    30.     }
    31. }
    32.  
    I feel it's worth noting that it doesn't matter if I have a separate camera that I'm invoking RenderWithShader on or not. Using Graphics.SetRenderTarget does make a difference with a bit of manual work to ensure that the previous render texture is set as the active render texture again, but that results in nothing being rendered beyond the clear color.

    It may also be worth mentioning that I'm doing this on OS X 10.9.1 as well. Any ideas what could be going wrong here?

    Edit: Also, modifying OnPreRender to this:
    Code (csharp):
    1.  
    2.         RenderTexture rt = RenderTexture.active;
    3.         dummyCam.camera.SetTargetBuffers(mrtRB, mrtTex[0].depthBuffer);
    4.         dummyCam.camera.clearFlags = CameraClearFlags.SolidColor;
    5.         dummyCam.camera.RenderWithShader(geometryBufferShader, "RenderType");
    6.         RenderTexture.active = rt;
    7.  
    has no effect what so ever.

    Edit #2: To generally clarify why this is under Support instead of ShaderLab, well, I'm pretty sure that this shouldn't be happening when I run the above code:
    $Screen Shot 2013-12-31 at 5.48.58 AM.png
     
    Last edited: Dec 31, 2013
  2. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Did you ever get SetTargetBuffers to work with 4.3?
     
  3. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    can you please create a small repro project and bug report it? We have a special test internally just for that kind of usage (MRT through SetTargetBuffers) so it should work