Adam Frańczak / Playsoft
Techniki optymalizacyjne w UDK/UE3.
Prezentacja z konferencji infoShare 2013 w Gdańsku.
Presented at infoShare 2013 conference in Gdańsk, Poland.
2. OVERVIEWOVERVIEW
●
About UEAbout UE
●
Application examplesApplication examples
● OptimizationOptimization
● Tools for optimization in UE3Tools for optimization in UE3
●
OptimizationOptimization in UE3in UE3
●
OptimizationOptimization:: examplesexamples
3. ABOUT UNREAL ENGINEABOUT UNREAL ENGINE
● Developer – Epic GamesDeveloper – Epic Games
● First version – 1998First version – 1998
● Operating system – cross-platformOperating system – cross-platform
● Stable release – February/2013 Unreal Engine3Stable release – February/2013 Unreal Engine3
● Unreal Engine 4 ? -Unreal Engine 4 ? - stillstill inin progressprogress
10. OPTIMIZATIONOPTIMIZATION
Optimization - is the process of modifying a software system to makeOptimization - is the process of modifying a software system to make
some aspect of it work more efficiently or to use less resources.some aspect of it work more efficiently or to use less resources.
Levels of optimization:Levels of optimization:
● DesignDesign
● Source CodeSource Code
● CompileCompile
● AssemblyAssembly
● Run timeRun time
11. OPTIMIZATION IN UEOPTIMIZATION IN UE
„„UE3 has a large number of tools and techniques to help you win theUE3 has a large number of tools and techniques to help you win the
battle with an optimization!”battle with an optimization!”
● STATS COMMANDSTATS COMMAND
● GAME PROFILERGAME PROFILER
● MEMLEAK-CHECKMEMLEAK-CHECK
● SHADER ANALIZERSHADER ANALIZER
andand moremore
12. STAT COMMANDSSTAT COMMANDS
STATSTAT COMMANDS in Unreal Engine 3 provides the ability to view a lotCOMMANDS in Unreal Engine 3 provides the ability to view a lot
of statistics about various aspects of the game and engine.of statistics about various aspects of the game and engine.
MostMost importantimportant STAT COMMANDS:STAT COMMANDS:
●
STAT FPS – just FPSSTAT FPS – just FPS
●
STAT UNIT – show information about time spent for the currentSTAT UNIT – show information about time spent for the current
frame on the CPU, the time spent in the game thread and time spentframe on the CPU, the time spent in the game thread and time spent
in the render threadin the render thread
●STAT MEMORY – display information about memorySTAT MEMORY – display information about memory
●STAT ENGINESTAT ENGINE
●STAT GAME -STAT GAME -
●STAT OBJECT – show information about object creation andSTAT OBJECT – show information about object creation and
destructiondestruction
14. MEMLEAKCHECKMEMLEAKCHECK
Help to understand where and when memory is being used in ourHelp to understand where and when memory is being used in our
game.game.
Output Sections:Output Sections:
●
headerheader
● class listclass list
● specific object listspecific object list
●
platform memoryplatform memory
● game specific memorygame specific memory
●
rendering memoryrendering memory
●
package memorypackage memory
● assets listassets list
●
actor listactor list
● level listlevel list
15. MEMLEAKCHECKMEMLEAKCHECK
Provides to use and understand where and when memory is being useProvides to use and understand where and when memory is being use
in our game.in our game.
Output Sections:Output Sections:
●
headerheader
● class listclass list
● specific object listspecific object list
●
platform memoryplatform memory
● game specific memorygame specific memory
●
rendering memoryrendering memory
●
package memorypackage memory
● assets listassets list
●
actor listactor list
● level listlevel list
17. GAMEPLAYPROFILERGAMEPLAYPROFILER
The gameplay profiler is a tool that makes use of data captured duringThe gameplay profiler is a tool that makes use of data captured during
profiling sessions and it allows you to dig deeper into gameplay codeprofiling sessions and it allows you to dig deeper into gameplay code
to analyze time spent on performing functionsto analyze time spent on performing functions.
22. GENERAL RULESGENERAL RULES
● Identify your budget (for assets , textures, sound etc.)Identify your budget (for assets , textures, sound etc.)
● Instill the mindset of making certain that people are using theInstill the mindset of making certain that people are using the
minimal amount of data / cpu cycles to create what they wantminimal amount of data / cpu cycles to create what they want
● Remember that the game has to run within the constraints of theRemember that the game has to run within the constraints of the
target platforms – this process requires constant monitoringtarget platforms – this process requires constant monitoring.
23. RULESRULES
● Identify slow but low visual impact casesIdentify slow but low visual impact cases
● Ideally find enough potential optimizations that if they were allIdeally find enough potential optimizations that if they were all
implemented, the scene would be < 33msimplemented, the scene would be < 33ms
● Use detailed draw event information to quickly identify what eachUse detailed draw event information to quickly identify what each
draw call is in the editordraw call is in the editor
● Record Actor name, Resource name (static mesh, particle emitter,Record Actor name, Resource name (static mesh, particle emitter,
etc), Material nameetc), Material name
24. RULESRULES
● Provide LD's with suggestions on what to change, with clearProvide LD's with suggestions on what to change, with clear
directions and screenshotsdirections and screenshots
● LD's and artists decide which optimizations are worth while, makeLD's and artists decide which optimizations are worth while, make
content changescontent changes
● This ensures that art is responsible for quality decisionsThis ensures that art is responsible for quality decisions
● Repeat steps 1-3 if needed.Repeat steps 1-3 if needed.
25. DRAW CALLSDRAW CALLS
Number of draw call is very important. Each material is a separateNumber of draw call is very important. Each material is a separate
draw call, so if you have 1 mesh with 4 materials, it is 4 draw calls.draw call, so if you have 1 mesh with 4 materials, it is 4 draw calls.
Remember:Remember: keep draw calls downkeep draw calls down
● Combine meshes that use the same texture and shaderCombine meshes that use the same texture and shader
● Aggressive cullingAggressive culling
27. TEXTURESTEXTURES
The first thing to look at is the "Over Budget" value in STATThe first thing to look at is the "Over Budget" value in STAT
STREAMING. If it's „0” you're ok. If it's non-zero, you're using moreSTREAMING. If it's „0” you're ok. If it's non-zero, you're using more
textures than can fit in the texture pool and you should reducetextures than can fit in the texture pool and you should reduce
memory usage and/or increase the texture pool size.memory usage and/or increase the texture pool size.
Use only square and require power of two for mobile devices.Use only square and require power of two for mobile devices.
If it is possible just turn off alpha channelIf it is possible just turn off alpha channel
28. TEXTURESTEXTURES
Textures Groups: groups of textures which are handled specially.Textures Groups: groups of textures which are handled specially.
●
TEXTUREGROUP_SkyboxTEXTUREGROUP_Skybox (textures are always fully streamed in,(textures are always fully streamed in,
they're automatically marked Forced).they're automatically marked Forced).
●
TEXTUREGROUP_UI (TEXTUREGROUP_UI (textures have all their mip-levels removedtextures have all their mip-levels removed
during cooking)during cooking)
● TEXTUREGROUP_Lightmapse (TEXTUREGROUP_Lightmapse (extra boost factors from .iniextra boost factors from .ini
settings)settings)
● TEXTUREGROUP_ShadowmapTEXTUREGROUP_Shadowmap
● TEXTUREGROUP_CharacterTEXTUREGROUP_Character (groups has a slight preference in(groups has a slight preference in
that they can be the first ones to stream in when loading a mapthat they can be the first ones to stream in when loading a map
and will be the last textures to stream out in a panic stream out)and will be the last textures to stream out in a panic stream out)
29. MEMORYMEMORY
Memory usage is always a concern for video games, especially thoseMemory usage is always a concern for video games, especially those
destined for consoles or mobile devices where memory is limited. Thisdestined for consoles or mobile devices where memory is limited. This
extends from the amount of space taken up by content assets on diskextends from the amount of space taken up by content assets on disk
to memory usage of different systems during runtime. These are allto memory usage of different systems during runtime. These are all
extremely important pieces of information that make it possible toextremely important pieces of information that make it possible to
constrain memory usage within acceptable limitsconstrain memory usage within acceptable limits.
30. MEMORYMEMORY
Important rules:Important rules:
●
Budget: Decide what to use memory on.Budget: Decide what to use memory on.
●
Make sure all assets are optimal and don't have unnecessaryMake sure all assets are optimal and don't have unnecessary
references.references.
● Make sure you have enough buffer (for fragmentation) to keepMake sure you have enough buffer (for fragmentation) to keep
your game runningyour game running
31. MEMORYMEMORY
Possibilities include:Possibilities include:
● The level that has too many Static MeshesThe level that has too many Static Meshes
● AI that create too many projectiles and particlesAI that create too many projectiles and particles
● Code that allocates too much memoryCode that allocates too much memory
32. SPAWN VS POOLING OBJECTSSPAWN VS POOLING OBJECTS
Using object pooling for ephemeral objects is faster than creating andUsing object pooling for ephemeral objects is faster than creating and
destroying them.destroying them.
However:However: if you keep allocating memory on top of the pools you justif you keep allocating memory on top of the pools you just
created, you might trigger garbage collection even more oftencreated, you might trigger garbage collection even more often
Remember: performance will suffer if you allocate pools that are tooRemember: performance will suffer if you allocate pools that are too
large or keep them active when the objects they contain will not belarge or keep them active when the objects they contain will not be
needed for some timeneeded for some time
33. CODE – GOOD PRACTICECODE – GOOD PRACTICE
Use the kismet and sequences if it is possible:Use the kismet and sequences if it is possible:
●
Fast and easy toggle on or toggle off all objectsFast and easy toggle on or toggle off all objects
● Precisely run scriptsPrecisely run scripts
● Precisely run movies/cinematicsPrecisely run movies/cinematics
● Acces to matineAcces to matine
● Level streamingLevel streaming
34. CODE – GOOD PRACTICECODE – GOOD PRACTICE
ExampleExample::
36. CODE – GOOD PRACTICECODE – GOOD PRACTICE
● Native functionality instead of UnrealScript codeNative functionality instead of UnrealScript code
● Execute code only as often as neededExecute code only as often as needed
●
Find bottlenecks by measuring execution timeFind bottlenecks by measuring execution time
function MyFunction() {function MyFunction() {
local float ExecutionTime;local float ExecutionTime;
Clock(ExecutionTime);Clock(ExecutionTime);
UnClock(ExecutionTime);UnClock(ExecutionTime);
Log("Time spent executing something:" @ ExecutionTime @ "ms");Log("Time spent executing something:" @ ExecutionTime @ "ms");
}}
37. CODE – GOOD PRACTICECODE – GOOD PRACTICE
Speed comparison of loops: (different loops have differentSpeed comparison of loops: (different loops have different
overhead times)overhead times)
while( i!=n ){while( i!=n ){
stuff[i++] = vect(1,0,0)*FRand() + vect(0,1,0)*FRand() + vect(0,0,1)*FRand();stuff[i++] = vect(1,0,0)*FRand() + vect(0,1,0)*FRand() + vect(0,0,1)*FRand();
} //} // While loop takes 0.93 time unitWhile loop takes 0.93 time unit
if( n!=0 ){if( n!=0 ){
do{do{
stuff[i++] = vect(1,0,0)*FRand() + vect(0,1,0)*FRand() + vect(0,0,1)*FRand();stuff[i++] = vect(1,0,0)*FRand() + vect(0,1,0)*FRand() + vect(0,0,1)*FRand();
}until( i==n )}until( i==n )
} //} //Until loop takes 0.90 time unitUntil loop takes 0.90 time unit
38. CODE – GOOD PRACTICECODE – GOOD PRACTICE
Disable engine events when you don't need themDisable engine events when you don't need them
event PostBeginPlay{event PostBeginPlay{
// all events are enabled by default, so disable Tick event to start with// all events are enabled by default, so disable Tick event to start with
Disable('Tick');Disable('Tick');
}}
event Trigger(Actor Sender, Pawn Instigator){event Trigger(Actor Sender, Pawn Instigator){
// tell engine to call Tick event from now on// tell engine to call Tick event from now on
Enable('Tick');Enable('Tick');
}}
event Tick(float DeltaTime) {event Tick(float DeltaTime) {
// do something -- executed only after the actor has been triggered// do something -- executed only after the actor has been triggered
}}
39. GOOD IDEASGOOD IDEAS
Remember:Remember:
●
particle systems are not cheapparticle systems are not cheap
- automated flipbook texture creation from expensive animated- automated flipbook texture creation from expensive animated
materialsmaterials
● Considerations for different devicesConsiderations for different devices
- screen resolution vs memory- screen resolution vs memory
- per-pixel rendering performance- per-pixel rendering performance
40. CODE – GOOD PRACTICECODE – GOOD PRACTICE
and remember at the endand remember at the end REFINE YOUR ALGORITHMS.REFINE YOUR ALGORITHMS.