Sign in to follow this  

Unity [.net] Faster game with NetAsm, a JIT Native Code Injection Library

This topic is 3461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I would like to share with the .NET community, the NetAsm opensource project available on codeplex.
NetAsm provides a hook to the .NET JIT compiler and enables to inject your own native code in replacement of the default CLR JIT compilation. With this library, it is possible, at runtime, to inject x86 assembler code in CLR methods with the speed of a pure CLR method call and without the cost of Interop/PInvoke calls. With this library, it is possible for example to inject highly optimized code (using SSE, MMX) inside a method, in replacement of the default generated native code by the JIT compiler.
This library could be particularly useful for game using .NET platform (XNA for example), as it provides a way to integrate fast native code for methods that need highly optimized native code. The current features of NetAsm are:

  • Runs on x86 32bit Microsoft .NET platform with 2.0+ CLR runtime (x64 may be supported in the future
  • Provides three different native code injection techniques: Static, DLL, and Dynamic
    • Static code injection: The native code is stored in an attribute of the method.
    • Dll code injection: this method is similar to the DllImport mechanism but CLR methods are directly bind to the DLL function, without going through the interop layers.
    • Dynamic code injection: you can generate native code dynamically with a callback interface that is called by the JIT when compilation of a method is occurring. It means that you can compile a method "on the fly". You have also access to the IL code of the method being compiled.
  • Supports for debugging static and dynamic code injection.
  • Supports for different calling conventions: StdCall, FastCall, ThisCall, Cdecl. Default calling convention is CLRCall
  • NetAsm can be used inside any .NET language (you can run assembler code inside VB!)
  • Very small library <100Ko.

For additional information and documentation, please visit the NetAsm codeplex site.

Alexandre

Share this post


Link to post
Share on other sites
Odd question, but is there any chance that the approach could be modified to work on the Xbox 360, which runs a version of the .net compact framework and omits the emit instruction, or does the approach rely on something x86 specific? I realize its a very different architecture, but the CPU is fairly well understood and if it were possible it would go a long way towards getting the most out of the 360 (such as the VMX128 units).

I'm not sure if there are other protections in place, in fact, there probably are, but it would definitely be something to look into.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravyne
Odd question, but is there any chance that the approach could be modified to work on the Xbox 360, which runs a version of the .net compact framework and omits the emit instruction, or does the approach rely on something x86 specific? I realize its a very different architecture, but the CPU is fairly well understood and if it were possible it would go a long way towards getting the most out of the 360 (such as the VMX128 units).

I'm not sure if there are other protections in place, in fact, there probably are, but it would definitely be something to look into.
Well, currently, NetAsm is supporting only x86 32bit .NET platform. NetAsm is not able to run on .NET CF on XBox360, as the Xbox CF doesn't allow the use of mixed library or native library. Although, technically, if the .NET CF on XBox360 was more open, NetAsm should be able to run with few minor changes...

Share this post


Link to post
Share on other sites
Very interesting.
Quote:
Original post by AlexandreMutelDll code injection: this method is similar to the DllImport mechanism but CLR methods are directly bind to the DLL function, without going through the interop layers.

Would it be possible also something like this with delegates? Like calling delegate would directly bind to given address of some native function without interop layers. I'm thinking here about usage of OpenGL extensions and wglGetProcAddress returned function pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by bubu LV
Would it be possible also something like this with delegates? Like calling delegate would directly bind to given address of some native function without interop layers. I'm thinking here about usage of OpenGL extensions and wglGetProcAddress returned function pointer.

If you think about something similar to Marshal.GetDelegateForFunctionPointer this is currently possible in NetAsm through Dynamic code injection (see TestDynamicCodeInjection.cs in NetAsmDemo sample on how to do dynamic code injection). I could add this delegate features if enough people are interested in (add this request to the Issue Tracker on codeplex).
However, NetAsm should not be considered as a replacement for Interop. There are some issues :
  1. The native code injected is considered as CLR code and not as unmanaged, so there are some restrictions when using external functions that could call OS functions (for example, even if NetAsm could call printf from msvcrt.dll, it prints nothing because the CLR is supposed to be in CLR mode and not unmanaged) : Interop has a different behavior as you leave the CLR runtime to the unmanaged runtime (the VM does a couple of things to insure the transition, thread contexts...etc.).
  2. Also, NetAsm doesn't fully support other calling conventions (StdCall, FastCall, ThisCall, Cdecl). The default supported calling convention is ClrCall - which doesn't do any conversion call between the CLR call and the code injected-

Currently, native code injection should be preferred when using CPU intensive calculation (for small routines for example).

Moreover, if you look at some benchmarks on NetAsm, you will see that when a method is CPU intensive and is running for a medium-time, the cost of calling through Interop is negligible to the cost of the method itself (See Matrix Multiplication sample using SSE2 : in this example, NetAsm is only 10% faster than fast interop - without security checking).

Share this post


Link to post
Share on other sites
This is extremely interesting!

A couple of questions:

Is there any chance that this could work on the Mono JIT? What would it take for that to happen?

How easy is it to add support for different platforms (e.g. x86_64)?

"The native code injected is considered as CLR code and not as unmanaged" - would this restrict calling e.g. OpenGL functions?

I am mainly trying to understand how/if this project could be used to speed up wrappers like Tao or OpenTK.

Keep up the good work!

Share this post


Link to post
Share on other sites
Quote:
Original post by Fiddler
Is there any chance that this could work on the Mono JIT? What would it take for that to happen?

We have to wait... i have just asked this to Miguel de Icaza. It would be nice but i'm not sure it's possible or even it would be made possible by Mono team dev...
Quote:
How easy is it to add support for different platforms (e.g. x86_64)?

I don't have currently any way to test such platform, although the port of NetAsm to x64 shouldn't be hard. If anyone is interested in helping me on this, i would appreciate.
Quote:
"The native code injected is considered as CLR code and not as unmanaged" - would this restrict calling e.g. OpenGL functions?

You have to test it. I'm not sure it's going to work.

Share this post


Link to post
Share on other sites
Quote:
Original post by AlexandreMutel
Quote:
Original post by Fiddler
Is there any chance that this could work on the Mono JIT? What would it take for that to happen?

We have to wait... i have just asked this to Miguel de Icaza. It would be nice but i'm not sure it's possible or even it would be made possible by Mono team dev...

Great! Going from posts on the mono-dev mailing for the debugging and profiler APIs, I suspect that it might be possible, but I'm nowhere proficient enough on the Mono internals to say for sure.

I also don't know how Mono plays with C++/CLI. There was some work on this front recently, but I don't think it's really supported at this point. (By supported I mean running C++/CLI assemblies, not compiling).

Going slightly off-topic now, but Mono takes a smaller hit than .Net when calling unmanaged code (about 6ns overhead versus 12ns for a simple function with one parameter and one return value, measured on x64 mode on a Core 2 @2.66GHz). The capability for dynamic injection through NetAsm is still awesome, though!

Quote:

Quote:
How easy is it to add support for different platforms (e.g. x86_64)?

I don't have currently any way to test such platform, although the port of NetAsm to x64 shouldn't be hard. If anyone is interested in helping me on this, i would appreciate.

If you point me to the correct direction, I could help with this. Is this as simple as adding a new calling to the CallingConventionHelper.h? How would the JIT hooks need to change?

Quote:
Quote:
"The native code injected is considered as CLR code and not as unmanaged" - would this restrict calling e.g. OpenGL functions?

You have to test it. I'm not sure it's going to work.
[/quote]
Fair enough :D I'll write a simple test to check this out.

Share this post


Link to post
Share on other sites

This topic is 3461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Redlime
      I am a game development student working on a 2.5D beat 'em up brawler game in Unity as my final year project and I've been working on the ai for the enemies in the game. However, I've been unable to get the enemies to working properly as the enemies would be constantly stuttering and jerking while trying to follow the player. I've created a foreach statement and added some conditions that call for the robots to spread out and stay away from the player if it is not in an engaging enum state. This has been an issue plaguing the ai for a while now, analysing the animator, it seems like the robot's movement is instantaneously stopping and going which might be the reason causing this stuttering. But I'm not sure how to prevent the robot's movement from stopping and going. If anyone knows the reason why this is happening to the robots it would be much appreciated as my project is due soon and I'm still unable to fix such a game breaking bug! I've included a few videos below showing what is happening to my robots and also the stuttering shown inside the animator. I've also included the full script for my robots.
       
      Robots Stuttering
      Robots Stuttering Animator
      Robot.cs
    • By red1m3n
      Hi I'm a hobbyist getting together a team to pursue game creation, we'll do both 2D and 3D games, we would definitely build different types of games, and hopefully have a great time doing it. you dont have to have much experience, just enough that you think you can fill the position. I have plenty of ideas for games that I'd love your help with, and am completely open to fulfilling your game ideas . As of right now this is a for fun project thing, but if me and my team are satisfied with our work it can be published to steam or other platforms. 
      The positions I'm currently looking to fill are:
      2D Pixel artist
      3D Modeler 
      C# programmer
      Musician/Sound engineer
      I am currently working on a project it is a 2D Merchant adventure game with survival elements. It does not have a name yet, that's because I'm bad with names. 
       
      If interested please feel free to leave me a PM or email at dealtavu@gmail.com so we can talk more about it..
    • By Aggrojag
      Hello!
      I'm working on a game that is quite personal to me. It touches on my own depression, obsessions, phobias, etc. It's a narrative driven dark comedy with some small aspects of platforming and puzzle solving. The project is rather small as well. It touches on topics such as suicide, mental illness, family, corruption, free-will, and redemption.
      First, I'm looking for a 2D animator that can compliment the style of the attached image. The player character's animations will be a bit more extensive than a typical platformer. There will be many death animations, and some other strange dealings with the character that will need animation. Other items will include some objects in the world that are in need of animation. All static pieces of art will be handled by the artist that drew the attached image.
      Second, I'm looking for a male voice actor. There will be at minimum two characters that will need to be brought to life through vocals. They are a very core part of the game, though they will never be shown on screen. As a reminder, this is a rather dark comedy, so be prepared for some insanity in your portrayal of these characters. The first voice is that of a game show host's voice, while the other will be a devilish voice (without SFX, I think it cheapens it, does not necessarily need to be a low voice, this is more about the portrayal than how the voice sounds).
      Last, I would love to have an editor on board. More than just avoiding disaster in the writing, I'm looking for someone that can elevate it. There is not a ton of writing that will be in the final project, but it's important enough of an aspect to be trying to bring someone in to help with it.
      To those interested:
      I have a crappy prototype that exists, along with a couple game design docs. More than anything, I'd like to present the project through a voice chat via discord to any interested. Discord - Bobdul Thundercuck#4590
      I do plan to post this on itch.io and wherever else I can put it that makes sense. I fully expect to make nothing off this project, as should you. However, in the case that money is made, a rev share model will be in place.

       
      Edit: Editor position has been filled, title has been changed to reflect this, and crossed out the section detailing this.
    • By William Edmeades
      Hey guys and girls,   Have you ever found yourself trying to apply for a job to be a game developer or a game artist? Maybe a sound engineer or a script writer? Only to not even get an interview due to lack of ‘experience’, teamwork projects or released game titles you have worked on? I have for sure. The game industry is hard and competitive to get into for someone new, especially when you might have a full time job or even have to support a family. So I have come up with an idea and I wanted to throw something out there to see if people could be interested in such an idea.   What if you could be part of a virtual game development company? A place where you can work on projects with a team of people from the internet, soon to be known as your colleagues, which could be from anywhere in the world all working at your own pace when you have time to do as little or as much as you want.   The idea is we can create the power of a company and release titles together. Not only do you then have a released game title you have worked on under your resume, but also work experience, team work skills, and the power of helping each other and learning from each other.    My dream idea is to create a solid place for people to join a party and develop ideas and create games and level up it experience points.   As far as money is concerned, the games would either be released for free or any money would go towards charity, it’s not about making money, it’s about building a career or justness having fun building games and socialising with like minded people.    I want to make something amazing, I think it would benefit me, and I think others who are serious would benefit greatly too.   I await your critasism on the ‘prototype’ idea, I just wanted to see how many people would be interested in such a concept. And if you are very much into the idea, let’s make it happen.   Thanks,   Will. www.feelingterrible.com
    • By NDraskovic
      Hey guys,
      Like the title says, I'm trying to find out if it's possible to automatically set which scenes will be built for different platforms. For instance, I have scenes that I want to include when building for PC, but exclude them for mobile platforms. Is it possible to do this automatically (by some settings file, or by coding some editor extension), or do I have to manually (de)select them each time I switch platforms?
      Google just keeps sending me to the documentation sites about the regular publishing, but I can't find information about this problem.
      Thanks in advance
  • Popular Now