Search Unity

Modifying texture and then getting back

Discussion in 'Shaders' started by Arrovs, Jan 29, 2015.

  1. Arrovs

    Arrovs

    Joined:
    Sep 23, 2014
    Posts:
    24
    Hello.
    I wish to know if its possible to put 2 textures in videocard and get after then back resulting one. I really dont need to draw it on screen.
    It just could be much more faster than on pc.
    I already know code for this, but i want to know how to move it to graphic card and get back.
    Also i do not have pro license - just for case.
    Could be glad if anybody could post links or samples.
    Thanks already.
     
  2. TechnoCraft

    TechnoCraft

    Joined:
    Apr 6, 2012
    Posts:
    28
    You can do it with Compute Shader but you are limited to 1024 threads in Unity Free Edition. And you can not use SetTexture, GetTexture. You can use SetBuffer, GetBuffer. Using this approach the performance overhead of getting and setting a texture to buffer of bytes will kill any realtime ambitions. 1024 threads are nothing compare to what graphic cards are able to do nowdays. Try milions of threads. Not in Unity free.
     
    Last edited: Jan 31, 2015
  3. Arrovs

    Arrovs

    Joined:
    Sep 23, 2014
    Posts:
    24
    Thanks. I already thought about compute shader, but didnt knew if it will work on mobiles and now knowing limitations good i went with threading(on cpu). Of course that is slow stuff too, but for my needs works really well.
     
  4. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    Huh? I have never heard about such a limit nor can I find info about it anywhere... Perhaps you mean the dx11 limit of 1024 threads PER dimension PER group?
     
  5. TechnoCraft

    TechnoCraft

    Joined:
    Apr 6, 2012
    Posts:
    28
    See attached project (Unity Free 4.61). Features ComputeShader, Texture Computation, GameOfLife (source site)

    Shader error in 'shCustomComputeShader.compute': the product of the arguments of numthreads(1024,1024,1024) must be less than or equal to 1024 at line 25.
    [numthreads(1024,1,1)] works.
     

    Attached Files:

    Last edited: Feb 4, 2015
  6. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    Oh, my bad.. turns the limit is not per dimension but per group as a whole. It's still a limitation of the hardware, not Unity... it does the same in Unity pro as well. The thing is, a single group shouldn't be doing all the work anyways. Since a group is executed on a single shader processor unit, you're losing huge amounts of parallelism.

    Consider this.. you want to run a compute shader on a 1024x1024 buffer, or something over a million threads. Let's say you define your group size as [numthreads(16, 16, 1)], which is just 256 threads per group. You can then do ComputeShader.Dispatch(index, 64, 64, 1) to launch 4096 of these groups.. that should launch enough threads to cover the whole buffer just fine, even on the free version of unity :)
     
    reefwirrax likes this.
  7. TechnoCraft

    TechnoCraft

    Joined:
    Apr 6, 2012
    Posts:
    28
    Thanks for pointing out the difference of dispatch and thread groups.

    In attached project above (Unity Free), the performance cost of manual data transfer between texture and buffer is higher than the time needed to compute the texture in a shader (so I didn't bother with parallel threading). Using RenderTexture resolves this problem but it is a Unity Pro feature.

    Example for 256x256:
    //Use the same code as above, just change two lines in shCustomComputeShader.compute:
    [numthreads(8,8,4)]
    uint y = id.x + (id.y * 8) + (id.z * 64);