Search Unity

Get the results of the GPU skin into a Compute Shader?

Discussion in 'General Graphics' started by sebj, Sep 24, 2016.

  1. sebj

    sebj

    Joined:
    Dec 4, 2013
    Posts:
    70
    Hello,

    I have a component which takes in a baked skinned mesh and produces some data from it in a compute shader.

    Right now, I do the bake on the CPU and copy it into a compute buffer. I can see from the profiler that Unity does the GPU skinning in some sort of separate pass.

    Is there any way to get this into my compute shader entirely on the GPU?

    I'm pretty sure I know the answer as this is far from the first time I've looked at it, but this is a last ditch attempt to see if there's a way, as its so galling to think about re-computing all the matrices, copying them to the GPU then writing my own skinning compute shader, when I know that Unity is doing *all of that already* and I just can't touch it!
     
    richardkettlewell likes this.
  2. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    I would like to know this as well!
     
  3. windwolfx_23

    windwolfx_23

    Joined:
    Jan 15, 2015
    Posts:
    11
    same here, try to get skinned mesh point data to compute shader to emit particles.
     
  4. c6burnz

    c6burnz

    Joined:
    Jun 22, 2014
    Posts:
    5
    I just somehow bumped into this post while trying to find information on how to access a generic avatar's bone matrices when it's been optimized. Infuriatingly, that is not currently possible .... BUT .... I do happen to have an answer to this question! Although the answer, as you have all likely surmised, is: do the skinning yourself!

    As a better answer, I can elaborate on this. No you cannot access the output of Unity's GPU skinning. Please do correct me if I am wrong, but I don't see ANY mechanism to access vertex buffers in Unity's pipeline directly. Somewhat hilariously, you can get native pointers to vertex buffers to access them in plugins (eg. D3D11Buffer*, GLuint, whatever), but you can't actually access it from C# within Unity. It's annoying to say the least.

    OK, enough describing the pain. What can be done about skinning in particular to keep everything on the GPU? Well on platforms with compute shader support, Unity does skinning via compute shader. You can find the internalskinning.compute shader in the built-in shader download: https://unity3d.com/get-unity/download/archive

    It's actually fairly simple once you dig in. Also I found this github repo that makes it even more so: https://github.com/sugi-cho/Unity-InternalSkinningTest

    This project modifies the internalskinning.compute shader to remove a number of defines and simplify it. If it helps you to have a working example to analyze then there you go. There is also an example of utilizing the output in a surface shader, and doing the transforms in the vertex shader. My one warning is that he does a few things you wouldn't want to do in production code (eg. access properties of UnityEngine.Mesh in MonoBehaviour Update instead of caching) so beware and profile :)