Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is there a way to send compute shader data directly to a material shader and not via CPU?

Discussion in 'Shaders' started by TheCelt, Sep 4, 2019.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    I am looking to implement the iFFT algorithm for ocean rendering. I want to run the algorithm on the compute shader and pass that data to my material for vertex/frag shader to do its magic based on the data from the compute shader.

    The only current way i can see to do this is to return the data from the compute shader to the cpu, then from the CPU to the material shader via properties.

    But is there a way to skip the CPU and send data directly to the material shader? I can't find much info on this in the documentation.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Do you mean using:
    mat.SetBuffer("_MyBuffer", myBuffer);

    Because that doesn't copy any data back to the CPU, that's just telling the GPU to use the same buffer data that's already on the GPU. Effectively the only thing the CPU has a reference pointer (or, perhaps more accurately, a reference to a reference pointer).
     
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    If both the compute shader and material use the same buffer. Does it make sure the compute shader runs first before the material shader? So that the material shader when reading the data is actually reading the data after the compute shader has done its magic?

    Basically i want the compute shader to generate my maps and normals for the iFFT algorithm and the material to use those maps for displacement and what not. Though my maps are just buffer arrays not actual
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    No, that's up to you. Make sure you Dispatch your compute shader early in the c# Update loop so it has time to run before the frame starts to render. You may even want to ping pong between two buffers to make sure you're not stepping on the previous frame's rendering if you plan to disable vsync.
     
    SugoiDev and TheCelt like this.