Search Unity

CommandBuffer.Blit(source, dest, material) Source Texture is incorrect

Discussion in 'Shaders' started by Ewanuk, Feb 2, 2017.

  1. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    I have a shader that I use through a command buffer to perform a screen-space operation on a Render Texture.

    Previously, I was lazy and had a source render texture assigned to a global shader property. Now I'm cleaning up my code and instead of referencing the global property in the shader, I'm trying to read it through _MainTex directly. However, what I'm getting in _MainTex is not the same as the global shader property:

    Code (CSharp):
    1. //////C Sharp Code Snippit
    2. string globalSrcTexPropertyAsString = "_GlobalSrcTexture";
    3. string globalDstTexPropertyAsString = "_GlobalDstTexture";
    4.  
    5. RenderTexture globalSrcTexture = new RenderTexture(Screen.height, Screen.height, 0, RenderTextureFormat.ARGBFloat);
    6. RenderTexture globalDstTexture = new RenderTexture(Screen.height, Screen.height, 0, RenderTextureFormat.ARGBFloat);
    7.  
    8. globalSrcTexture.SetGlobalShaderProperty(globalSrcTexPropertyAsString);
    9. globalDstTexture.SetGlobalShaderProperty(globalDstTexPropertyAsString);
    10.  
    11. CommandBuffer cmdBuffer = new CommandBuffer();
    12.  
    13. //Some things happen here that write to globalSrcTexture
    14.  
    15. cmdBuffer.SetRenderTarget(globalDstTexture);
    16. effect.ClearRenderTarget(false, true, new Color(0, 0, 0, 1));
    17. effect.Blit((Texture)globalSrcTexture, globalDstTexture, blitMaterial);
    18.  
    19.  
    20. //////Shader code snippit
    21.  
    22. sampler2D_float _MainTex;
    23.  
    24. //The output of these two methods should be identical, but they're not
    25. return tex2D(_GlobalSrcTexture, i.uv);
    26. return tex2D(_MainTex, i.uv);
    Why are the two fragment outputs in the shader not identical?
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I ran into that same issue before. _MainTex doesn't seem to get set correctly when using Blit in a CommandBuffer. Using a global does work. I think I just added:
    Code (csharp):
    1.  
    2. cmdBuffer.SetGlobalTexture("_MainTex", sourceIdentifier);
    3.  
    Not the greatest way, but it does the trick.
     
  3. steven3Dim

    steven3Dim

    Joined:
    Feb 21, 2017
    Posts:
    12