Using SIMD in Mono, or using the GPU

Discussion in 'Unity Support' started by _-_, May 23, 2013.

  1. _-_

    _-_

    New Member

    Joined:
    May 23, 2013
    Messages:
    7
    Currently I'm working on procedural terrain generation, and for that I'm relying heavily on various noises (Perlin, Simplex, Worley and so forth). The problem is now that they start to become to slow, since I'm using them millions of times per second. So, I need something to speed it up and for that I'm looking at either the GPU or SIMD. Both are perfect for noise, since both of them are aimed at vectors and matrices, which are the foundation of most noises. My question is, how do I use them?

    SIMD
    Mono now supports SIMD, but for that I need their dll, which is called something like mono.simd.dll. Then I need to link it to MonoDevelop so I can include it (by "using Mono.Simd"). Problem is, I've downloaded almost all recent Mono-"thingies" (MonoDevelop, Mono SDK, Mono for end-users), but none of them seems to have that dll.
    TL;DR, how do I get Mono.Simd in the MonoDevelop unity is using (apparently Unity's MonoDevelop is a fork of the original Mono).

    GPU
    How can I create a shader which alters the heightmap of the built-in terrain in Unity? I've looked for tutorials, but can't seems to find one. I have seen shadercode, and I'm pretty sure I can learn to write it, so my only problem is, how can I call the shader from my code (which is in C#)? The output from the shader does not need to be anything fancy, just a 2-dimensional array with the calculated values will suffice. Also, I have the normal Unity, so apparently I can't render to texture (which is only included in Pro).

    Thanks in advance for any help.
  2. kaiyum

    kaiyum

    Member

    Joined:
    Nov 25, 2012
    Messages:
    325
    So, you want some cpu calculation performed in gpu? I do not think it is nicely possible in free version. RenderTexture is necessary for these kind of tasks.
  3. _-_

    _-_

    New Member

    Joined:
    May 23, 2013
    Messages:
    7
    Thanks for your feedback Kaiyum.
    I was afraid that I would need render to texture, but can anyone confirm that there is absolutely NO (reasonable) way I can use the GPU to do some calculations and let it output a (huge) array/matrix of numbers? Because a simple array would already be a big help.

    Also, does anyone know anything about the SIMD yet? I did run some tests on the built-in vectors (Vector2, Vector3 and Vector4) and they seem to give a small boost in speed, so I think they are using SIMD. I am not 100% per cent sure about this though, and it is still quite limited. So if anyone would know about the SIMD I would be very, very grateful.
  4. kaiyum

    kaiyum

    Member

    Joined:
    Nov 25, 2012
    Messages:
    325
    If you go through compute shader documentation,may be you can find some way. However,I smell it will be seriously limited by nature(performance wise). On side of SMID, I think unity engine's core functionality take care of that(sse for intels,neon for arm,AltiVec for RISC based system such as xbox360,wii etc,weird SPU instruction sets for playstaion 3). However if you want to implement your own way with different SMID instruction set of x86 family, I think you can do it in pro. I have never wrote a plugin before(as I do not have pro). I think it will work on the way below:

    Code some assembly subroutines and be very careful with utilizing those instruction set;with optimization in mind,you can screw up it and go performance hit. Those subroutine can then be compiled to somesort of object file/run time module. Then you can write some plugin and call you desired function through this tunnel. Making subroutine part seems clear to me,though I have no idea how do you make any connection of these subroutines with unity's plugin. I also want to know what kind of limitation unity's plugin feature has,just for information purposes;you know when I get pro I could do some test,why not :p :p

    However,you(with all free user like me) seems to be screwed in this situation as you can not make native plugin in free version :(:(

    Between I also want to know if unity utilizes other SMID instruction sets such as FMA,AVX,SSE4 etc for x86 platform. :p
    May be for particle effect or skinning?
  5. Arowx

    Arowx

    Member

    Joined:
    Nov 12, 2009
    Messages:
    2,304
  6. kaiyum

    kaiyum

    Member

    Joined:
    Nov 25, 2012
    Messages:
    325
    Thats good! But it is using only SSE. :(:(
  7. _-_

    _-_

    New Member

    Joined:
    May 23, 2013
    Messages:
    7
    I don't really see why using SSE would be a problem?


    and, @Arowx, that was indeed what I've found on the internet, but I can't find the DLL (it's supposed to be included with the newer releases of Mono, except that it isn't or I can't find it). So, where can I find it, or how do I include it to my version of MonoDevelop so I can use it in my code?

    Also, about the GPU, I know how to program and the article is certainly interesting, but how do I call the GPU code/shader from my code, so I get something I can use (for example a matrix or array of calculated numbers)?
  8. Arowx

    Arowx

    Member

    Joined:
    Nov 12, 2009
    Messages:
    2,304
    You got me there I thought Mono.SIMD was built into Mono and just a matter of using the namespace.

    It was out there but it appears to have dropped off the radar at least as far as my google search shows, lots of old stuff.

    If you want super high performance then the other option is to write a C/C++ dll that you can link to and have it do all the heavy lifting for you.

    Although I suspect that as you will be using the terrain on the GPU probably best to built it where you use it.

    But if you do need to bring some of the data back then you would probably use the new DX11 DirectCompute interface, which is not very well documented (at least for dummies guide types like me).

    It looks like your pushing at the boundaries of what Unity can do, or definitely beyond what I can do with Unity.

    Try @aras_p on twitter he's the Unity graphics guru!

    Good Luck
  9. kaiyum

    kaiyum

    Member

    Joined:
    Nov 25, 2012
    Messages:
    325
    I thought it would offer AVX,FMA etc too. You know,those are the latest things from intel-amd.
  10. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    MMX and SSE is as far as cpu accelerated maths go. All recent additions intel and AMD did are part of SSE too, namely SSE 4 and 4.1, though many of them are absolutely useless for general maths. all required for that was in MMX and SSE2


    All the rest is gpu based acceleration if you want to get even faster (cuda, opencl, directcompute in dx11)
  11. alexzzzz

    alexzzzz

    Member

    Joined:
    Nov 20, 2010
    Messages:
    799
    It is located here: \Unity\Editor\Data\Mono\lib\mono\2.0\Mono.Simd.dll

    Just copy-paste it somewhere into your assets folder.
  12. Brainswitch

    Brainswitch

    Member

    Joined:
    Apr 24, 2013
    Messages:
    116
    Hmm... When I tried using Mono.Simd vectors vs Unitys, the Mono.Simd's were quite a bit slower... I might be doing something wrong though. Anyone else tried using the library?
  13. alexzzzz

    alexzzzz

    Member

    Joined:
    Nov 20, 2010
    Messages:
    799