Sign in to follow this  

Unreal Engine... How did they do it?

This topic is 1880 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

Hiya,

I've recently been shown a demo reel of the Unreal Engine 4 ([url="http://www.youtube.com/watch?v=acR4n6lJEdQ"]clicky[/url]).

At the very end, while running around in the game world, the demonstrator changes some of the source code in Visual Studio. While he continues to run around the code is recompiled, and suddenly the changes take effect in the game world - without even restarting the simulation.

This struck me as pretty amazing (more so than the fancy graphics [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]) and I have no idea how they do it. Somehow they are able to replace running binary code in-memory, without the machine exploding. If anyone is interested, it starts around 9:45.

It's far beyond me to try and make anything like this but I'm fascinated - does anyone have an inkling how it works?

Share this post


Link to post
Share on other sites
My guess is they are changing and recompiling a [url="http://en.wikipedia.org/wiki/Library_%28computing%29#Shared_libraries"]shared library[/url], which can be dynamically loaded and unloaded. The details are OS specific.

In Windows, shared libraries are called "dynamic-link libraries" (DLLs).

Share this post


Link to post
Share on other sites
That was my guess, but I'm not sure it would be easy.

If you pull the rug from under a DLL like that surely you'll invalidate any pointers to objects within it, the state of those objects, etc. They must have designed the DLL very carefully, and/or serialise all the objects to another part of memory and then deserialise them back to the new library when it's loaded. It sounds terribly complicated to me.

Thanks! Interesting stuff.

Share this post


Link to post
Share on other sites
[quote name='Radikalizm' timestamp='1344351790' post='4967032']
I stumbled upon this blog a while back which might be of interest: [url="http://runtimecompiledcplusplus.blogspot.be/"]http://runtimecompil...us.blogspot.be/[/url]
They even mention the similarities between their technique and the technique used by UE4
[/quote]
Amazing!
[quote name='Hodgman' timestamp='1344352494' post='4967036']
...
[/quote]
Helpful explanation, thanks.

That library does look identical to Epic's implementation - they might be one and the same. I've just given their test code a run through and it works as advertised, I'll definitely be having a closer look into how exactly they're doing it. They've even geared the sample code up for integrating into an existing project.

I've definitely learnt some useful and interesting things today, thanks for your replies [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]

All the best.

[Edit] It looks like they do indeed serialise objects back into the recompiled module. Edited by Telios

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1344352494' post='4967036']
Making run-time changes to your structures is more complicated, and VS "edit and continue" simply doesn't support this. Radikalizm's awesome link seems to serialize objects who's structures have changed, so they can be re-created after re-loading the code. I'm definitely going to have a good look at how that project works!
[/quote]

When I first read about their technique I was quite skeptical about it and I assumed they weren't telling the entire truth, but right now this seems to be the real deal.
By design the technique itself is quite elegant too, and I can see a massive productivity boost in a lot of programming fields once this gets more widespread

I still have to make some time to play with their demo code myself though, but if this works without any major issues I'll probably be integrating this into my own development code - if possible.

Share this post


Link to post
Share on other sites
[quote name='dougbinks' timestamp='1344358025' post='4967058']
Glad you've found our project useful!

We don't know if the Unreal implementation is related to ours - it's possible that after our presentation in 2011 at the AI GameDev conference in Paris they found out about it but it's also possible they came up with a similar solution themselves.

On the subject of modules and memory, you do need to ensure you either have virtual destructors or something similar as objects are allocated on each modules heap.
[/quote]

Ahh perfect, now I can say this in person: Awesome work!

If I can get this to work nicely with my development code you might have saved me quite some production time [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
Needless to say I'll be following your project closely

Share this post


Link to post
Share on other sites
[i]N.B. Not trying to put a damper on these awesome ideas here, just pointing out some pitfalls to be aware of:[/i]
Two issues with supporting changes to data-structures (which implies serialization/deserializaiton) is that many game engines are (1) very specific about how their utilize their RAM, and (2) also very limited in the amount of RAM they can use.

(2) isn't an issue on PCs, and console dev-kits [i]usually[/i] have double the retail-version's amount of RAM, so it's likely only a minor problem affecting some of your SKUs.

(1) complicates matters though. Even with a general-purpose allocator, when freeing 10,000 objects and then allocating 10,000 slightly larger objects, it's possible that the original allocations aren't contiguous ([i]so they become little bubbles in your address space[/i]), and the new allocations are too big to fit in those "bubbles". This fragmentation of RAM can have the same effect as if you'd simply leaked the original allocations ([i]until you free the used-space around them, probably at the end of the level[/i]), which can make it much more likely to hit the RAM limits from (2).

Further, optimised engines don't use global, general-purpose global allocators ([i]i.e. new/malloc[/i]) for everything. To construct a certain kind of object, you might need to specify which allocation area to construct it in, and you might not be able to free that allocation at a random time ([i]e.g. perhaps bulk-deallocation of the whole area is used[/i]).

For example, when allocating an object in our renderer, depending on which systems are likely to access it and it's required lifetime, the user will specify 1 allocator out of a possible 4 different stacks and 4 different heaps that could be used. i.e. the same object could be constructed within 8 different areas of RAM, but choosing the right one is important based on usage.
If the object is allocated in one of the stack areas, it's can't be freed at will; it's lifetime is bound to the lifetime of the stack. Reallocating with a larger size means 'leaking' the original and adding a new object on the top of the stack ([i]which there might not be space for[/i]). Alternatively, it means serializing the entire stack and re-allocating the whole thing from the bottom-up to avoid these holes ([i]and then patching any other objects that held pointers into this stack[/i]).

These aren't un-solvable problems, but a class-layout-patching solution that's applicable for every engine would have to be very flexible in regards to allocations, and may end up having to serialize/deserialze hundreds of megs of data per 'reload'.

That said, gameplay code is usually less performance-critical than engine code, so maybe you just don't support this feature on your critical engine systems ;) Edited by Hodgman

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1344396469' post='4967232']
[i]<snip>[/i]
[/quote]

You hit the nail right on the head, this is exactly the thing I wanted to experiment with as I too am using specialized allocation schemes in my engine code which may not work too well with this system, and I really don't want to give them up to implement this feature.
I can see the destroying and reallocating resources in memory becoming a problem since I mostly use a contiguous allocation scheme so I can easily group resources of one type into one contiguous chunk of memory (no fragmentation + data-oriented design = happy cache + happy programmer). Cleanup is done in chunks too as it pretty much comes down to rolling back to a base point in memory and calling the appropriate destructors in order, so selective destruction of data is not really recommended - I'm not sure whether this will actually pose a problem, but it's definitely something I need to keep in mind

I would be interested to see this run on consoles, I can imagine it being a major productivity boost if developers can make on-the-fly changes to code running on a console. Edited by Radikalizm

Share this post


Link to post
Share on other sites
[quote name='Radikalizm' timestamp='1344410766' post='4967284']I would be interested to see this run on consoles, I can imagine it being a major productivity boost if developers can make on-the-fly changes to code running on a console.[/quote]Lots of companies do this already, but only for procedures, not class-layouts. The PS3 encourages a "[i]job based[/i]" programming model, where your procedures are more separate from your data-layouts than in traditional OOP, so it's not too hard to shoe-horn in code-reloading to systems written this way.
Reloading a DLL ([i]equivalent[/i]) and changing some function-pointers is simple. It's only the reloading of class-layouts ([i]and the implied serialization/deserialization/pointer-patching[/i]) that complicates matters.

As far as I'm concerned, every engine should already be doing this for their ([i]HLSL or equivalent[/i]) shader code and any "script" code ([i]e.g. Lua[/i]) too ;) Edited by Hodgman

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1344412668' post='4967294']
[quote name='Radikalizm' timestamp='1344410766' post='4967284']I would be interested to see this run on consoles, I can imagine it being a major productivity boost if developers can make on-the-fly changes to code running on a console.[/quote]Lots of companies do this already, but only for procedures, not class-layouts. The PS3 encourages a "[i]job based[/i]" programming model, where your procedures are more separate from your data-layouts than in traditional OOP, so it's not too hard to shoe-horn in code-reloading to systems written this way.
Reloading a DLL ([i]equivalent[/i]) and changing some function-pointers is simple. It's only the reloading of class-layouts ([i]and the implied serialization/deserialization/pointer-patching[/i]) that complicates matters.

As far as I'm concerned, every engine should already be doing this for their ([i]HLSL or equivalent[/i]) shader code and any "script" code ([i]e.g. Lua[/i]) too ;)
[/quote]

I see, interesting. I don't have any experience whatsoever with console development, unless you count XNA + X360 development as console experience, so this is new to me :) Sadly console development is one of those things you can't experiment with on your own...

As for shader and script code my system does in fact allow for runtime editing and recompilation in development builds, just like it allows for on-the-fly editing and reloading of any other type of resource, and you're absolutely right about it being a crucial technique if you ever want to get anything major done.

Share this post


Link to post
Share on other sites
[quote name='Radikalizm' timestamp='1344413395' post='4967296']
[quote name='Hodgman' timestamp='1344412668' post='4967294']
[quote name='Radikalizm' timestamp='1344410766' post='4967284']I would be interested to see this run on consoles, I can imagine it being a major productivity boost if developers can make on-the-fly changes to code running on a console.[/quote]Lots of companies do this already, but only for procedures, not class-layouts. The PS3 encourages a "[i]job based[/i]" programming model, where your procedures are more separate from your data-layouts than in traditional OOP, so it's not too hard to shoe-horn in code-reloading to systems written this way.
Reloading a DLL ([i]equivalent[/i]) and changing some function-pointers is simple. It's only the reloading of class-layouts ([i]and the implied serialization/deserialization/pointer-patching[/i]) that complicates matters.

As far as I'm concerned, every engine should already be doing this for their ([i]HLSL or equivalent[/i]) shader code and any "script" code ([i]e.g. Lua[/i]) too ;)
[/quote]

I see, interesting. I don't have any experience whatsoever with console development, unless you count XNA + X360 development as console experience, so this is new to me [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Sadly console development is one of those things you can't experiment with on your own...

As for shader and script code my system does in fact allow for runtime editing and recompilation in development builds, just like it allows for on-the-fly editing and reloading of any other type of resource, and you're absolutely right about it being a crucial technique if you ever want to get anything major done.
[/quote]
I have not really used XNA for 360 but I think there will be a few features the runtime might not expose to keep the writing of game code simple. I am thinking of manually setting which tiles to clear on the backbuffer and setting up tiled based rendering in particular, but I don't know this for sure to be honest.

Share this post


Link to post
Share on other sites
I think this feature has nothing to do with visual studio. VS is only here bound to their system to allow for nice editor features. but compilation/link/relaunch is handled by them.
Surely, it cannot work for everything, you cannot modify core code, it must be within precise "plugin" architecture, and surely, like android, there must be a lot of constraints. You mentioned before something about serialization, this is exactly what android requires of all application because they can be killed at any time.
it is easier to rebuild a game logic that says "when space hit then jump" which requires no data, than rebuild the code of the deferred pipeline because it would require rebinding all uniforms, rebuild all render targets etc etc.
But of course, it is still amazing.

Share this post


Link to post
Share on other sites
[quote name='Lightness1024' timestamp='1344429724' post='4967370']
I think this feature has nothing to do with visual studio. VS is only here bound to their system to allow for nice editor features. but compilation/link/relaunch is handled by them.
Surely, it cannot work for everything, you cannot modify core code, it must be within precise "plugin" architecture, and surely, like android, there must be a lot of constraints. You mentioned before something about serialization, this is exactly what android requires of all application because they can be killed at any time.
it is easier to rebuild a game logic that says "when space hit then jump" which requires no data, than rebuild the code of the deferred pipeline because it would require rebinding all uniforms, rebuild all render targets etc etc.
But of course, it is still amazing.
[/quote]

You might want to read the slides of their presentation (it's posted at the link I provided), it'll clear some things up about the inner workings ;)

Share this post


Link to post
Share on other sites
Some great commentary and questions here, here's some responses which I hope clarify things:

@Lightness1024 and Radikalizm:

We're using Visual Studio as our compiler, since that's the most common compiler used for games, and there's a freely available Express version which we also support. Other compilers and OSes could be supported but would require a little extra work. We don't use any specific features of VS for the base implementation, though the pragma comment library feature is an easy way to get libraries added - but we intend to supply a cross platform version eventually. In particular I'll likely port to Mac OS X and Linux at some point, though that may not be for a few months due to other work.

@Hodgman

Myself and the other authors are all professional game developers with console experience, and a similar implementation at Crytek is working on the XBox 360 (see our develop conference slides linked from the [url="http://runtimecompiledcplusplus.blogspot.com/"]blog[/url]). Memory is certainly an issue for console development in general and this technique would add some overhead, though there are development consoles with more memory to aid with this.

Many changes could be done with in-place serialization or even serialize to disk/network etc. and back so as to be able to free up memory prior to recreating the objects and thus also handle fragmentation issues. Additionally, though in principal all data types could be runtime changed (for example I've used the technique for graphics coding), most of the larger asset types are likely to have a very stable memory format and so not need runtime serialization.

We may make a client/server style runtime compiler for systems like iOS, Android and consoles at some point, but we'd not be able to open source the console work due to licensing conditions.

Hope this helps, I'll try and monitor this thread and try to answer questions that come up.

Share this post


Link to post
Share on other sites
:) thanks for replies.
but all that seems to me a little scary. trying to maintain heap while rebuilding code in a non-managed/non-interpreted language :s
what happens if a plugin creates a bunch of instances of structure S and we change the content of structure S to a new layout of data we will name S2, then after rebuild, if the runtime tries to access those old instances the code will think to see S2 structures but actually those are S structures in memory. This will corrupt the content and crash if it involves pointers.
how would that be handled ?

Share this post


Link to post
Share on other sites
It's actually a lot easier than you'd think to use, and we have an [url="http://runtimecompiledcplusplus.blogspot.ch/2011/09/crash-protection.html"]error handling mechanism[/url] to enable you to correct these problems and keep working. Even if the game crashes in a completely fatal way, you're no worse off than if you'd made an error in your code, compiled and ran it as normal. If data gets corrupted, you could re-load a save point (currently our demo doesn't create save points when loading new code, but it would be trivial to add).

Best way to find out if it works for you is to give the demo a try.

Note that our primary target is programmers - who are used to dealing with bugs and crashes and the state they create is code which doesn't get lost when the program crashes. If you want to use this for designers (and we believe it can be, see latest post on the console for one example), further error protection methods such as the above mentioned save points should be used.

Share this post


Link to post
Share on other sites
If anyone is following this thread still and using OS X, they might like to know [url="http://runtimecompiledcplusplus.blogspot.fr/2012/10/mac-os-x-and-simultaneous-multiple.html"]we've recently added OS X support[/url] (only tested 10.8 and XCode 4 Clang++ so far). Would appreciate any feedback.

Share this post


Link to post
Share on other sites
My quick and easy way to achieve something similar is for development purposes, split the game into two applications and have those two applications communicate over a local network connection.

The game application does most of the game stuff, but asset loading & use is separated out to the second application.
The second application does the asset loading, rendering, audio and other things that use large amounts of memory.

Game loads up, runs as it would, calls it's APIs to load in textures/audio/etc. Some of those loads are routed to the second application to load that data.
The game renders, sending a render command buffer to the second application which does the rendering.

This deals with a vast amount of the game data that has to be loaded from disk and keeps it in memory.

The next step is to hold the game data that is needed for game logic in the second application's memory. And then the next step after that is to give the second application a representation of the game's state so that when the game is reloaded it returns to the point in game play it was at when it was terminated.

Share this post


Link to post
Share on other sites
[quote name='Kyall' timestamp='1351125984' post='4993612']
My quick and easy way to achieve something similar is for development purposes, split the game into two applications and have those two applications communicate over a local network connection.
[/quote]

That's a pretty good way to do things, especially if you're going to have an multi-player since you likely need the infrastructure anyway. I know a few game developers who've used this technique, and it's worked for them quite well.

Share this post


Link to post
Share on other sites

This topic is 1880 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