Jump to content

  • Log In with Google      Sign In   
  • Create Account


Unreal Engine... How did they do it?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#1 Telios   Members   -  Reputation: 398

Like
1Likes
Like

Posted 07 August 2012 - 08:16 AM

Hiya,

I've recently been shown a demo reel of the Unreal Engine 4 (clicky).

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 Posted Image) 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?

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12345

Like
3Likes
Like

Posted 07 August 2012 - 08:29 AM

My guess is they are changing and recompiling a shared library, which can be dynamically loaded and unloaded. The details are OS specific.

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

#3 Telios   Members   -  Reputation: 398

Like
0Likes
Like

Posted 07 August 2012 - 08:47 AM

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.

#4 Radikalizm   Crossbones+   -  Reputation: 2791

Like
10Likes
Like

Posted 07 August 2012 - 09:03 AM

I stumbled upon this blog a while back which might be of interest: http://runtimecompiledcplusplus.blogspot.be/
They even mention the similarities between their technique and the technique used by UE4

I gets all your texture budgets!


#5 Hodgman   Moderators   -  Reputation: 28409

Like
7Likes
Like

Posted 07 August 2012 - 09:14 AM

N.B. visual studio has contained an "edit and continue" mode built into it for a long time. Edit your project properties to include "edit and continue" debugging information (on top of the regular debug info), and when you hit a breakpoint in the debugger, you can make some changes and press continue.

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.

Most objects aren't allocated "in a DLL", they're allocated within the address space of your program.
As long as you don't change the layout of those object's classes when reloading the code, they'll continue to be valid.

If you pare back all the abstractions, all our game code is, is a bunch of data-structure definitions, and procedures that can transform those data-structures. There's no reason why you shouldn't be able to make your transforms re-loadable (and it's a huge productivity boost if you do so!).

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!

Edited by Hodgman, 07 August 2012 - 09:30 AM.


#6 Telios   Members   -  Reputation: 398

Like
0Likes
Like

Posted 07 August 2012 - 09:42 AM

I stumbled upon this blog a while back which might be of interest: http://runtimecompil...us.blogspot.be/
They even mention the similarities between their technique and the technique used by UE4

Amazing!

...

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 Posted Image

All the best.

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

Edited by Telios, 07 August 2012 - 09:49 AM.


#7 Radikalizm   Crossbones+   -  Reputation: 2791

Like
0Likes
Like

Posted 07 August 2012 - 09:51 AM

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!


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.

I gets all your texture budgets!


#8 dougbinks   Members   -  Reputation: 480

Like
9Likes
Like

Posted 07 August 2012 - 10:47 AM

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.

#9 Radikalizm   Crossbones+   -  Reputation: 2791

Like
0Likes
Like

Posted 07 August 2012 - 12:22 PM

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.


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 Posted Image
Needless to say I'll be following your project closely

I gets all your texture budgets!


#10 Hodgman   Moderators   -  Reputation: 28409

Like
1Likes
Like

Posted 07 August 2012 - 09:27 PM

N.B. Not trying to put a damper on these awesome ideas here, just pointing out some pitfalls to be aware of:
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 usually 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 (so they become little bubbles in your address space), 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 (until you free the used-space around them, probably at the end of the level), 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.e. new/malloc) 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 (e.g. perhaps bulk-deallocation of the whole area is used).

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 (which there might not be space for). Alternatively, it means serializing the entire stack and re-allocating the whole thing from the bottom-up to avoid these holes (and then patching any other objects that held pointers into this stack).

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, 07 August 2012 - 09:41 PM.


#11 Radikalizm   Crossbones+   -  Reputation: 2791

Like
0Likes
Like

Posted 08 August 2012 - 01:26 AM

<snip>


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, 08 August 2012 - 01:33 AM.

I gets all your texture budgets!


#12 Hodgman   Moderators   -  Reputation: 28409

Like
1Likes
Like

Posted 08 August 2012 - 01:57 AM

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.

Lots of companies do this already, but only for procedures, not class-layouts. The PS3 encourages a "job based" 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 (equivalent) and changing some function-pointers is simple. It's only the reloading of class-layouts (and the implied serialization/deserialization/pointer-patching) that complicates matters.

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

Edited by Hodgman, 08 August 2012 - 01:59 AM.


#13 Radikalizm   Crossbones+   -  Reputation: 2791

Like
0Likes
Like

Posted 08 August 2012 - 02:09 AM

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.

Lots of companies do this already, but only for procedures, not class-layouts. The PS3 encourages a "job based" 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 (equivalent) and changing some function-pointers is simple. It's only the reloading of class-layouts (and the implied serialization/deserialization/pointer-patching) that complicates matters.

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


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.

I gets all your texture budgets!


#14 NightCreature83   Crossbones+   -  Reputation: 2686

Like
0Likes
Like

Posted 08 August 2012 - 03:57 AM


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.

Lots of companies do this already, but only for procedures, not class-layouts. The PS3 encourages a "job based" 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 (equivalent) and changing some function-pointers is simple. It's only the reloading of class-layouts (and the implied serialization/deserialization/pointer-patching) that complicates matters.

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


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 Posted Image 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.

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.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#15 Lightness1024   Members   -  Reputation: 702

Like
0Likes
Like

Posted 08 August 2012 - 06:42 AM

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.

#16 Radikalizm   Crossbones+   -  Reputation: 2791

Like
0Likes
Like

Posted 08 August 2012 - 09:33 AM

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.


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 ;)

I gets all your texture budgets!


#17 dougbinks   Members   -  Reputation: 480

Like
4Likes
Like

Posted 08 August 2012 - 09:53 AM

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 blog). 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.

#18 Lightness1024   Members   -  Reputation: 702

Like
0Likes
Like

Posted 09 August 2012 - 06:44 AM

:) 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 ?

#19 dougbinks   Members   -  Reputation: 480

Like
3Likes
Like

Posted 09 August 2012 - 08:26 AM

It's actually a lot easier than you'd think to use, and we have an error handling mechanism 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.

#20 dougbinks   Members   -  Reputation: 480

Like
3Likes
Like

Posted 24 October 2012 - 03:42 AM

If anyone is following this thread still and using OS X, they might like to know we've recently added OS X support (only tested 10.8 and XCode 4 Clang++ so far). Would appreciate any feedback.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS