Search Unity

DrawProcedural from Compute Shader only updates when Unity is paused/unpaused

Discussion in 'Shaders' started by Dreamback, Aug 17, 2017.

  1. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    I'm using a Compute Shader to create a bunch of positions, and then using Graphics.DrawProcedural to render them to the camera. And it works! ...once. It only uses data from the Compute Shader once, and after that it only gets updated data if I pause and unpause the Editor. Anyone know what's up? It's kinda like the material getting the SetBuffer doesn't think it needs updating.

    I tried changing it to a CommandBuffer.DrawProcedural, but that didn't help.

    Code (CSharp):
    1.     public void Start ()
    2.     {
    3.         pointCloudBuffer_ = new ComputeBuffer(131072, 12, ComputeBufferType.Append);
    4.         pointCloudBuffer_.SetCounterValue (0);
    5.  
    6.         material.SetBuffer ("_PointCloudBuffer", pointCloudBuffer_);
    7.         shader.SetBuffer(kernelHandle_, "_PointCloudBuffer", pointCloudBuffer_);
    8.  
    9.         commandBuf = new CommandBuffer ();
    10.         commandBuf.name = "PointCloud";
    11.         commandBuf.DrawProcedural(camera.cameraToWorldMatrix, material, -1, MeshTopology.Points, 131072);
    12.         camera.AddCommandBuffer(CameraEvent.AfterFinalPass, commandBuf);
    13.      }
    14.  
    In the Compute Shader, I'm using
    AppendStructuredBuffer<float3> _PointCloudBuffer;

    and in the fragment shader, I'm using
    StructuredBuffer<float3> _PointCloudBuffer;
     
  2. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    Clarification: from more testing, the shader updates from the structured buffer every time the camera is disabled, then enabled. But not if we constantly toggle the camera on and off every update.

    Here's my regular shader, that isn't getting (or using maybe) an updated buffer, see anything off about it that could cause this?

    Code (CSharp):
    1.         Cull Back ZWrite Off ZTest Lequal
    2.  
    3.         Pass
    4.         {
    5.             CGPROGRAM
    6.             #pragma target 5.0
    7.             #pragma vertex vert
    8.             #pragma fragment frag
    9.  
    10.  
    11.             #include "UnityCG.cginc"
    12.             //The buffer containing the points we want to draw.
    13.             StructuredBuffer<float3> _PointCloudBuffer;
    14.             struct ps_input {
    15.                 float4 pos : SV_POSITION;
    16.             };
    17.             ps_input vert (uint id : SV_VertexID)
    18.             {
    19.                 ps_input o;
    20.                 float3 worldPos = _PointCloudBuffer[id];                // Read the world position of this point
    21.                 o.pos = mul (UNITY_MATRIX_VP, float4(worldPos,1.0f));    // Convert it to View Position
    22.                 return o;
    23.             }
    24.             float4 frag (ps_input i) : COLOR
    25.             {
    26.                 return float4(1,0,0,1);            // highlight the pixel in red
    27.             }
    28.             ENDCG
    29.         }
    30.  
     
    Last edited: Aug 17, 2017
  3. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    220
    I figured out what was causing the problem - I'm using the Depth Texture for the camera in my ComputeShader. But the Depth Texture won't have any data if the camera is culling everything. But I wanted the camera to show black except for the procedural mesh I was generating, so I culled everything, so it didn't update...except briefly when other cameras rendered something.