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.

Bug Spawn a variable amount of particles from graphics buffer

Discussion in 'Visual Effect Graph' started by felibj694, Nov 3, 2022.

  1. felibj694

    felibj694

    Joined:
    Oct 23, 2016
    Posts:
    26
    I have a system that populates an append buffer with float3 positions and I want to spawn particles at these positions. The problem is that the amount of positions change.
    I thought I could create a solution similar how DrawProceduralIndirect is used.
    So I created another buffer (as GraphicsBuffer.Target.IndirectArguments) and copied the append buffer counter (using GraphicsBuffer.CopyCount) and tried to read this value in the vfx shader.
    upload_2022-11-3_11-23-29.png
    This gives the following compile error:
    "New VFX : Exception while compiling expression graph: System.InvalidOperationException: The expression UnityEditor.VFX.VFXExpressionCombine is not valid as it have the invalid flag: InvalidOnCPU at UnityEditor.VFX.VFXExpressionGraph.BuildMapper (UnityEditor.VFX.VFXContext context..."

    I want to avoid having to read the counter to cpu and set an uniform variable using SetInt()..

    (vfx graph version 12.1.6)
     
    Last edited: Nov 3, 2022
    Qriva likes this.
  2. PaulDemeulenaere

    PaulDemeulenaere

    Unity Technologies

    Joined:
    Sep 29, 2016
    Posts:
    105
    Hello,
    The Spawn System is fully processed on CPU and can't read directly from a Graphics Buffer on CPU.
    In an ideal word, we would like to be able to process an indirect compute dispatch to feed a particles system with your custom amount of data but it isn't possible with our current system.

    We are going to improve error feedback in that case, there are other cases where you can encounter this issue (for instance, the submesh mask on an output particle mesh can rely on GPU processing).

    Alternatively, I can suggest to try executing an Initialize with always the same amount of particles and apply a rejection based on your counter buffer:
    upload_2022-11-7_10-37-39.png

    To be honest, I didn't try this graph in a real world scenario with a compute shader processing and outputting data, let me know if it solves your problem, I can go further trying to build a basic sample.
     
    Vita- likes this.
  3. felibj694

    felibj694

    Joined:
    Oct 23, 2016
    Posts:
    26
    Ok thank you for responding. Yes that workaround will probably work, I will have to redo some things so that the capacity is not completely wasted.
    BTW, I saw in you example that you chose capacity to 2^16. Is there any advantage to choose a power of 2?
     
  4. PaulDemeulenaere

    PaulDemeulenaere

    Unity Technologies

    Joined:
    Sep 29, 2016
    Posts:
    105
    This isn't really relevant in that case but the thread group count in Visual Effect dispatch is always 64, it is often wise to simulate multiple of 64 particles to have the best GPU occupancy.
     
    Qriva likes this.