Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question A way to reference texture across CommandBuffers?

Discussion in 'General Graphics' started by INeatFreak, May 28, 2021.

  1. INeatFreak

    INeatFreak

    Joined:
    Sep 12, 2018
    Posts:
    46
    Hello,
    I want to set texture from commandBufferA and get that texture from commandBufferB that runs after the A. It is possible to cmd.SetGlobalTexture but there's no GetGlobalTexture where I can access it.

    I've tried creating RenderTargetIdentifier with global texture name (tried with both texture and property names) and using that, but doesn't work.

    Unity provided some way to do these kind of things like BuiltinRenderTextureType enum that has Depth, RenderTexture options but no Shadowmap & Screenspace Shadowmap.
    There's also BuiltinRenderTextureType.PropertyName option but not sure if it's what I am looking for and don't know how to use it (Unity Documentation don't have any samples).
     

    Attached Files:

    Last edited: Jun 15, 2021
  2. INeatFreak

    INeatFreak

    Joined:
    Sep 12, 2018
    Posts:
    46
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    The way I've worked around this is by creating a render texture outside of the command buffers and assigning both command buffers to use that. Temporary render textures created inside a command buffer may be released immediately after the command buffer finishes, so you can't really use them across multiple command buffers or with objects in the scene.
     
  4. INeatFreak

    INeatFreak

    Joined:
    Sep 12, 2018
    Posts:
    46
    My current approach is getting it from previous frame:

    Code (CSharp):
    1. tex_shadowmap = Shader.GetGlobalTextute("get_ShadowMap");
    2. cmdLight.SetGlobalShader("get_ShadowMap", BuiltinRenderTextureType.CurrentActive);
    3.  
    But it isn't perfect, it has some issues:
    1) Texture will always be 1 frame behind everything else.
    2) The very first frame texture will be null.

    It's really sad that to see that unity never implemented such simple command (GetGlobalXXX for CommandBuffers) that can be really useful in many cases.
     
  5. INeatFreak

    INeatFreak

    Joined:
    Sep 12, 2018
    Posts:
    46
    But I do not have access to other CommandBuffer. Just need to acces global texture within CommandBuffer.
    Something similar to this: (this is not a working solution)
    Code (CSharp):
    1. cmdA.SetGlobalTexture("get_ShadowMap", BuiltinRenderTextureType.CurrentActuve(;
    2. light.AddCommandBuffer(cmdA);
    3.  
    4. cmdB.SetGlobalTexture("_ShadowMap", "get_ShadowMap");
    5. cmdB.DrawMesh... // with shadowmap set
    6. camera.AddCommandBuffer(cmdB);
    But the ones I want to access aren't immediately released since they're being referenced in further passes.
     
    Last edited: Jun 15, 2021