Search Unity

Asynchronously getting data from the GPU (DirectX 11 with RenderTexture or ComputeBuffer)

Discussion in 'Shaders' started by jimhug, Nov 21, 2014.

  1. tntfoz

    tntfoz

    Joined:
    Sep 29, 2016
    Posts:
    129
    Hi all, just found this thread after a few hours of Googling ways to try and speed up the texture2D.ReadPixels call.

    Unfortunately the info in here is way over my head! :/

    Can anyone confirm if @Michal_ 's tool can replace the ReadPixels call, and if so, how would RetrieveTextureData be utilised for this case?

    Here's the current code I have (in a simple Update() function):

    Code (CSharp):
    1.  
    2.            var rect = new Rect(0, 0, (int)TextureSize.x, (int)TextureSize.y);
    3.  
    4.             RenderTexture.active = this.renderTexture;
    5.  
    6.             GL.Clear(false, true, BackgroundColor);
    7.  
    8.             targetCamera.Render();
    9.  
    10.             this.texture2D.ReadPixels(rect, 0, 0);          
    11.             this.texture2D.Apply();
    12.  
    Thanks in advance for any help, and apologies if I've misunderstood all of this completely!
     
  2. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    I'll add "take screenshot" example sometime next week if I have time.
     
    tntfoz likes this.
  3. R_RT

    R_RT

    Joined:
    Jan 22, 2017
    Posts:
    7
    Hi tntfoz, this is possible with the native plugin by Michal_. If you want to take only one screenshot once in a while the unity-example in github should be good.

    However if you would like to capture frames in a higher fps-count, using multiple textures seems necessary, because the texture will need some time to be ready to read. But it seems that the textures are not necessarily received in the order that you actually requested it (at least that's the case in my scene until now).

    Coming back to your question: Capturing a single screenshot seems to be working. Capturing a video-like raw image sequence seems not to work (or I haven't figured out how)
     
    tntfoz likes this.
  4. tntfoz

    tntfoz

    Joined:
    Sep 29, 2016
    Posts:
    129
    Thank you both!
     
  5. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    @tntfoz sorry, I still don't have time. Too much work. But I still plan to do it one day.
     
    tntfoz and tinyant like this.
  6. tntfoz

    tntfoz

    Joined:
    Sep 29, 2016
    Posts:
    129
  7. Hakazaba

    Hakazaba

    Joined:
    Jul 1, 2015
    Posts:
    119
    Hey @Michal_ Ive been testing out your plugin which is extremely useful for what I'm working on.
    Unfortunately its also causing unity crashes. I'm probably doing something wrong but i'm really not sure how to debug it correctly.
     
  8. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    There's probably a bug in the plugin if it crashes unity. Or a bug in unity. Probably the plugin though. Can you make a small repro project? I won't have much time in the next few weeks but I will take a look eventually...
     
  9. joergzdarsky

    joergzdarsky

    Joined:
    Sep 25, 2013
    Posts:
    56
    Hi Michal_,

    do you know if Unity did in any way consider add your plugin to the native repository / Unity code or at least implement a similar feature themselfs?

    That plugin is so usefull and it is such a pain that Unity3D has such a big functional gap when it comes to the GPU->CPU communication, I really wonder why they don't pick that topic up??
     
  10. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    I have no such information. I even asked unity guys how exactly native plugin callbacks work internally so that I could make this more efficient but I never got any response.

    Making the plugin part of Unity doesn't make much sense. They could implement it more efficiently directly in the engine. The plugin API is pretty restrictive. I doubt they will do it though. They would have to implement it for a lot of platforms and I doubt it is something your average unity user will ever need.

    I would personally say Unity has far bigger problems than gpu-cpu communication but I'm sure that is subjective.
     
  11. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    I think Joachim said in the unite talk in Austin that there would be an async readback in Unity 2018.1
     
    GoGoGadget likes this.
  12. GoGoGadget

    GoGoGadget

    Joined:
    Sep 23, 2013
    Posts:
    864
    Just before I go ahead and plug this into my own project, does anyone know if the current version works on 5.6.3?

    Also curious to hear if anyone has tried leveraging this async compute for any post-processing, or any general post-pro use cases for Async compute. IE. could you potentially speed up something like Eye Adaptation by using Async compute?
     
  13. Ilandria

    Ilandria

    Joined:
    Mar 8, 2014
    Posts:
    13
    Any chance you could find a reference/link to the source of that for me? That would be amazing news for my current project.

    I just decided to try switching over from DX11 (where this plugin works amazingly) to OpenGL 4.5 without thinking about it, and obviously this plugin doesn't run on OpenGL.
     
  14. R_RT

    R_RT

    Joined:
    Jan 22, 2017
    Posts:
    7
    This would be the talk you are searching for:

     
    SugoiDev likes this.
  15. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    They talk about it at 93 minutes+
     
  16. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,362
    18.1 has a beautiful named experimental called AsyncGPUReadBack which might work.

    17.3 has CreateGPUFence, maybe this GPUFence can be used to peek at the texture?
     
    julienkay, Zolden and GoGoGadget like this.
  17. julienkay

    julienkay

    Joined:
    Nov 12, 2013
    Posts:
    170
  18. joergzdarsky

    joergzdarsky

    Joined:
    Sep 25, 2013
    Posts:
    56
    Wow!! I think that is the best update coming with Unity3D ever in a new version. Looking forward for the final 2018.1 version!! Will bring procedural terrain creating with Unity3D to another level!
     
  19. Tohrak

    Tohrak

    Joined:
    Nov 2, 2015
    Posts:
    12
    Hi, I'm trying to use the plugin made by Michal_ (AsyncTextureReader) for Android, it is working very well on the Editor, but I get the DllNotFoundException on Android Build. What could I do to make it work ?
    I googled it and I believe I have to make a specific library for Android out of it , like a .so file. Is that correct ?
    If so, how could I do that ?

    Sorry, I'm not really a pro with Plugins. Any help will be appreciated, thank you. =)
     
  20. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    What unity version are you using? Also, although I'm not sure it matters, what device and os did you test on for android?

    Edit: I may have misread, did you try to use the given examples or did you try to build the plugin dll yourself?
     
  21. Tohrak

    Tohrak

    Joined:
    Nov 2, 2015
    Posts:
    12
    I'm using Unity 5.6.4. I tried to use the DLL found in the UnityProject there : https://github.com/SlightlyMad/AsyncTextureReader
    It works in the Editor but not in Android api lvl 24, with the error DLL not found. And so I guess I have to build it from the source code. Maybe I could made an aar. file out of it in Android Studio ?

    Thank you for the help.
     
  22. donov

    donov

    Joined:
    Apr 15, 2013
    Posts:
    55
    Did AsyncGPUReadback ever got OpenGL support?
     
    futurlab_xbox likes this.