Search Unity

Can you reuse calculation from a vert shader from one pass to another?

Discussion in 'Shaders' started by RibsNGibs, Apr 11, 2017.

  1. RibsNGibs

    RibsNGibs

    Joined:
    Jun 17, 2016
    Posts:
    15
    ...or maybe I should be doing this differently...

    I have a custom vert/frag shader that's doing some a bunch of billboarding kinds of calculations to make sure my polygons are facing the camera (but is more complex the standard billboard shader orientation) in the vert shader
    "ForwardBase" pass.

    And then apparently I need a "ShadowCaster" pass to write to the camera depth texture, so I have the entire vert shader from the "ForwardBase" pass copied to that ShadowCaster pass essentially line for line (except for some minor variable passthrough stuff to the frag shader).

    Is there any way I can save on the computation on this second pass (basically, the work that went into figuring out the projection space position)?

    It's probably not that big of a deal, but it makes me feel dirty that it's doing the same thing twice and that I've got duplicated code...
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Short answer: No.

    Long Answer: Yes, by not having either shader pass be what does the work. The most obvious solution is do all the calculations on the CPU and upload the mesh each frame, but that's not super efficient (though it is what Unity's particle system does!). The real trick is to calculate your reoriented mesh with a compute shader and use that to fill out a buffer with vertex positions that your rendered mesh reads. However unless you're trying to do this with millions of vertices it's questionable if this would be faster than just letting the GPU do what it's good at, which is doing a ton of math over and over.
     
  3. RibsNGibs

    RibsNGibs

    Joined:
    Jun 17, 2016
    Posts:
    15
    Ah, both of those make sense, but probably not worth trying out unless and if I actually have a framerate problem. I'm not doing it on millions of verts, but a fair number (grass billboard quads).Thanks!