Search Unity

[Solved]Cancel instance in instanced shader

Discussion in 'Shaders' started by Guedez, Aug 31, 2017.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Is there any way to cancel the drawing of a instance in an instanced shader?
    I am currently drawing 26 million blades of grass, but that reduces my fps to 20~
    What I want to do is to 'cancel/not draw' some of those based on distance.
    Is there any way to cancel the drawing of a instance after it went to the shader? I tried degenerating all triangles, although it did make the blades of grass disappear, it did nothing to help with the framerate
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Once you've gotten to the point of drawing it with a shader, the expensive parts were already done. Having the shader not draw isn't going to change too much, especially if the object being drawn is small on screen.

    You can, as you said, make it a degenerate triangle, or move it behind the camera, but you have to do that for every vertex anyway, and the cost of rendering the final pixels is likely negligible.

    You need to cull them before they're even sent to the GPU, or handle instancing more manually with a compute shader.
     
  3. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    I was worried about that, and started experimenting with 'halving the draw count' by distance, but now it looks ugly as hell (although it does run faster).

    I can't seem to find anything about using compute shaders to draw instances on google, can you elaborate on that?
    Currently I do use compute shaders to build the position buffer (lots of raycasting on triangles) to know where should I draw the grass blades, but I sure hope that you don't mean to 'rebuild the position buffer every frame, and only adding those that will be drawn on that frame'
     
  4. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Well, I've managed to get something done, not perfect, but I guess it counts as 'solved'
    Result: https://webmshare.com/yYvv1

    It's not ideal, since I pay full cost for about 30% of the blades of grass that are not drawn, see comparison webm below: https://webmshare.com/QPaaJ

    The squarey circle is the actual savings, the rounded circle is the blades that are removed by the shader (which I pay full cost for them)