Search Unity

Bug Spawn a variable amount of particles from graphics buffer

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

  1. felbj694

    felbj694

    Joined:
    Oct 23, 2016
    Posts:
    35
    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:
    154
    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. felbj694

    felbj694

    Joined:
    Oct 23, 2016
    Posts:
    35
    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:
    154
    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.