Search Unity

Bug Material forgets ComputeBuffer when VFXGraph window is active

Discussion in 'Visual Effect Graph' started by cecarlsen, Nov 26, 2021.

  1. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    864
    VFX Graph breaks my custom DrawProcedural based renderers, just by existing.

    When a VFX Graph editor window is open, my materials forgets their buffers. VFX Graph dosen't even have to be active in the scene. It is the editor window that is responsible for this crime.

    To reproduce, put this behaviour on an object in an empty scene:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestDrawProcedural : MonoBehaviour
    4. {
    5.     public Shader shader;
    6.  
    7.     Material _material;
    8.     ComputeBuffer _colorBuffer;
    9.  
    10.     const int quadCount = 3;
    11.  
    12.  
    13.     void Awake()
    14.     {
    15.         _material = new Material( shader  );
    16.         _colorBuffer = new ComputeBuffer( quadCount, sizeof( float ) * 4 );
    17.         _colorBuffer.SetData( new Color[]{ Color.red, Color.green, Color.blue } );
    18.         _material.SetBuffer( "_ColorBuffer", _colorBuffer );
    19.     }
    20.  
    21.  
    22.     void OnDestroy()
    23.     {
    24.         Destroy( _material );
    25.     }
    26.  
    27.  
    28.     void Update()
    29.     {
    30.         //_material.SetBuffer( "_ColorBuffer", _colorBuffer ); // WORKAROUND: if you set the buffer every update, then it can co-exist with VFX Graph.
    31.         Graphics.DrawProcedural( _material, new Bounds( Vector3.zero, Vector3.one * 1000 ), MeshTopology.Quads, 4, quadCount );
    32.     }
    33. }
    Add a reference to this shader:
    Code (CSharp):
    1. Shader "Test/TestDrawProcedural"
    2. {
    3.     CGINCLUDE
    4.  
    5.         #include "UnityCG.cginc"
    6.  
    7.         struct ToVert
    8.         {
    9.             float4 vertex : POSITION;
    10.         };
    11.  
    12.         struct ToFrag
    13.         {
    14.             float4 vertex : SV_POSITION;
    15.             float4 color : COLOR;
    16.         };
    17.  
    18.         static float2 quadLookup[ 4 ] = { float2( -1.0, -1.0 ), float2( -1.0, 1.0 ), float2( 1.0, 1.0 ), float2( 1.0, -1.0 ) };
    19.  
    20.         StructuredBuffer<float4> _ColorBuffer;
    21.  
    22.  
    23.         ToFrag Vert( uint vi : SV_VertexID, uint ii : SV_InstanceID )
    24.         {
    25.             float4 pos = float4( quadLookup[ vi ] * 0.45, 0, 1 );
    26.             pos.x += ii;
    27.  
    28.             float4 color = _ColorBuffer[ ii ];
    29.  
    30.             ToFrag o;
    31.             o.vertex = UnityObjectToClipPos( pos );
    32.             o.color = color;
    33.             return o;
    34.         }
    35.  
    36.         fixed4 Frag( ToFrag i ) : SV_Target
    37.         {
    38.             return i.color;
    39.         }
    40.  
    41.     ENDCG
    42.  
    43.  
    44.     SubShader
    45.     {
    46.         Tags{ "RenderType" = "Opaque" }
    47.  
    48.         Pass
    49.         {
    50.             CGPROGRAM
    51.             #pragma vertex Vert
    52.             #pragma fragment Frag
    53.             ENDCG
    54.         }
    55.     }
    56. }
    Hit Play and you will see three quads in red, green and blue.

    Open a VFX Graph window, and the quads instantly goes black. This happens because the material that draws the quads forgets a reference to a compute buffer that holds the colors. If I set the buffer every frame, just before drawing, it works as expected again. So VFX Graph does something that makes the material forget. It is not related to the name of the buffer. I can name it "_Whatever" and it still fails.

    Why?

    HDRP v 12.1, Unity v. 2021.2.1f1.

    EDIT: I also tried setting the buffer again OnApplicationFocus as suggested in this thread, but it made no difference. Makes sense, I guess VFX Graph is not refocusing Unity.
    https://forum.unity.com/threads/is-this-a-bug-in-unity.710510/#post-4749881


    WorksFine.PNG NotSoMuch.jpg
     
    Last edited: Nov 26, 2021