Engine Main Loop
This article describes in detail the steps taken by the UNIGINE engine when the Engine::update(), Engine::render(), and Engine::swap() functions are called. For other steps and general information about execution sequence, see the Execution Sequence article.
The total time the main loop has taken is displayed by the Total counter in the Performance Profiler.
Update#
The update part of the execution sequence includes the following:
The FPS value of the application starts to be calculated.
NoticeCalculation of FPS starts only with the second rendered frame, while the very first one is skipped.- All pending console commands that were called during the previous frame are executed. The commands are executed in the beginning of the update() cycle, but before the scripts are updated, because otherwise they may violate the current process of rendering or physical calculations.
- If a world has been unloaded during the previous frame, another world is loaded: WorldLogic::init().
If the restart_main_window console command was executed previously (not in the current update stage), world shaders are created.
- Properties, input, and controls are updated.
- Update of streamed textures, sound, and landscape.
- Game FPS and materials are updated.
- Ambient sound sources timers are updated.
- The World Logic init() function for the newly loaded world is called.
- The update() function of all plugins is called. What happens during it, solely depends on the content of this custom function.
- The Editor-related Logic is updated by calling the Editor update() function.
- The System Logic is updated. The default system script update() function performs the following:
- The system script handles the mouse. It controls whether the mouse is grabbed when clicked (by default), the mouse cursor disappears when not moved for some time (set by the MOUSE_SOFT definition), or not handled by the system (set by the MOUSE_USER definition, which allows input handling by some custom module).
- The main menu logic is updated (if the MENU_USER definition is not set).
- Other system-related user input is handled. For example, the world state is saved or restored, or a screenshot of the current UNIGINE window contents is made, if required.
If the GPU Monitor plugin is initialized (the HAS_GPU_MONITOR definition is set), the plugin output is displayed in the application window and additional console commands become available.
If the world is loaded (it can be done from the console or via the System Logic), the World Logic gets updated. In the World Logic update() function, you can code frame-by-frame behavior of your application (see details here).
NoticePhysics, if any, and continuous operations (pushing a car forward depending on current motor's RPM, simulating wind blowing constantly, performing immediate collision response, etc.), can be implemented separately in the updatePhysics() function. This function is called with a fixed frame rate (while the update() function is called each frame).The world and its world logic are updated in the following order:
- The World Logic updateAsyncThread() function is executed. It is designed to perform logic functions that should be called every frame independently of the rendering thread. This function doesn't block the Main Thread.
- The World Logic updateSyncThread() function is executed: all parallel logic functions that should be executed before update(). This function blocks the Main Thread until all calls are completed.
- The World Logic update() function is executed: node parameters are updated, transformations for non-physical nodes are set and so on.
- The state of nodes existing in the world is updated (mostly for visible nodes): skinned animation is played, particle systems spawn new particles, players are repositioned, and so on. Triggered world callbacks are added to a stack (they will be executed later).
- World Expressions are updated. Code in World Expressions can be written via UnigineEditor.
- GUI is updated.
- The World Logic postUpdate() function is called.
- The System Logic postUpdate() function is executed, if necessary (see details here). It can access the updated data on node states and correct the behavior accordingly in the same frame.
- The Editor Logic postUpdate() function is executed.
- The postUpdate() function of all plugins is called.
- The world spatial tree is updated.
- The file system is updated.
At the end of the update stage, physics and pathfinding start to be updated in their separate threads. Then they perform their tasks on all available threads in parallel to rendering.
The total time of update stage is displayed by the Update counter in the Performance Profiler.
Rendering#
As soon as the update stage is completed, UNIGINE can start rendering the world. In parallel, the pathfinding is performed in a separate thread. Depending on the physics update mode, parts of the physics calculations are performed in the main thread or in parallel to it. This approach enables to effectively balance the load between CPU and GPU, and thus allows for higher framerate in the UNIGINE-based application.
Here is what happens during the render stage:
Render | Physics | Pathfinding |
---|---|---|
|
|
|
Swap#
The swap stage is the last one in the main loop. It includes the following:
- If the screenshot console command is executed previously, on this stage, the taken screenshot is saved in folder where all application data is stored.
Synchronization of physics and pathfinding with the rendered world, i.e. waiting for all additional threads to finish their tasks. Results of the physical calculations are applied to the world. That is, on the previous step we have calculated how physical bodies with collision shapes have changed their position and orientation (due to our update-based logic or interaction). Now these transformations can be finally applied to nodes, i.e. rendered meshes.
NoticeAs the synchronization of physics follows the rendering stage (in default Async Rendering physics mode), applied physical transformations will be visible on the screen only in the next frame. To calculate physics before the rendering enable the Before Rendering physics mode.- The World Logic swap() function is executed. It operates with the results of the updateAsyncThread() function.
- The plugin swap() function is called, if it exists.
- Unloading of the world, if another world is going to be loaded in the next frame.
- Deletion of all objects that are planned to be deleted.
- Values shown in the Performance Profiler are updated.
After the swap stage is completed, the application window initiates GPU buffers swapping as described here.