DOTS
What does DOTS consist of?
DOTS consists of:
* Entity Component Systems: ECS implementation in Unity
* C# Job System: fast, safe and easy multi-threading
* Burst: Compiler for HPC#
* Collections: NativeCollections for use with jobs
* Mathematics: SIMD friendly math library for jobs
DOTS: Jobs
What is a Job?
To put it simply, it’s the logic on the main thread that is being split into smaller tasks called “jobs”.
In Unity specifically, the Unity Job System puts the jobs into a queue. A pool of worker threads can then draw from that queue and process the jobs.
Jobs have to Scheduled on the main thread. When scheduling a job, the job will not necessarily finish within the same frame unless said so.
DOTS: ECS
What is Monobehaviour?
Monobehaviour is the standard Unity component that you get without downloading any packages.
Monobehaviour always exists as a Component of a GameObject and can be instantiated with GameObjects.
DOTS: ECS
What is a GameObject?
GamObjects are essential objects in Unity. They are objects that does not do much themselves but acts as containers for Components.
Components can bring the objects to life depending on the implementation.
GameObjects can be a child of another and through that has access to different callbacks and other methods.
DOTS: ECS
What are the drawbacks of GameObjects?
GameObjects are managed objects and cannot be used in burst-compiled code.
The managed objects are garbage collected but also are scattered around memory.
DOTS: ECS
What is ECS?
In bigger picture ECS stands for Entity, Component and Systems.
Entity is the ID
Component is the Data
Systems is the logic
DOTS: ECS
What are Sub Scenes?
Sub scenes are a different scene type that essentially is a container that is converted into entity data. The GameObjects placed here are baked to DOTS data.
DOTS: ECS
What are Entities?
Entities should just be thought of as indexes. They are uniquely identified by a combination of index and version number.
An entity can have any number of components as long as it doesn’t have more than one of each component type.
An entities set of components is its Archetype.
DOTS: ECS
What are Entity Archetype?
Entities that a share the same unique set of components are set to belong to the same Archetype.
In example:
Entity A: Position, Rotation, Renderer & Speed
Entity B: Position, Rotation, Renderer & Speed
Entity C: Position, Rotation & Renderer
Entity A and B belong to the same Archetype.
Entity C belong to one Archetype.
Entities with the same Archetype are stored together in memory in chunks.
When the user creates entities and gives them components; Unity manages the chunk in native memory*.
*native memory is the memory managed by OS for process
DOTS: ECS
What is a EntityManager?
EntityMananger is the central API for:
* Creating and Deleting entities
* Add and Remove Components
All of these are called structural changes and has performance implications.
Managed vs Unmanaged Code
What is Managed code?
Managed code is essentially a programming language that handles memory management, exception handling and other tasks on the low level systems.
Managed vs Unmanaged Code
What is Unmanaged Code?
Unmanaged Code is code that runs directly on the OS - basically the opposite of Managed Code.
No handling of memory management, exception handling and other low level system tasks.
Blittable and Non-blittable types
What is a Blittable type and can you name a few?
A Blittable data type are types that do not need to be marshalled when transferred between the two environments*.
Common Blittable types:
* float
* int
* double
* char
* bool
* structure (with blittable types)
*managed and unmanaged code environment
Blittable and Non-blittable types
What is Marshalling data?
Marshalling data is a process that converts data between managed and unmanaged environment.
Blittable and Non-blittable types
What is Non-blittable types and can you name a few?
Non-blittable data types are the types that do not have a consistent memory layout between the two environments*.
They need to be marshalled if you need to pass them between e.g. C# and C++.
*managed and unmanaged code enviroment
DOTS: ECS
What are Components?
Components are structs though there are different types of Components but mainly it is structs. They do not contain any logic but only holds data, blittable data.
The types of Components are:
* General Purpose Component: contains only blittable data types but can hold multiple of them
* Tag Component: this component helps to specify archetype and the struct is often empty for optimization purposes
* Class Component: this component type is made with a class instead of struct. The reason why you may use this is in order to reference managed objects, though it has performance limitations and can only be used on main thread
* Dynamic Buffers: essentially stores data that are dynamic - the arrays are not shared between entities
Baking
What is Baking?
Baking is the process of serializing data from the editor down to binary format.
We can use it to add additional data for when gameobjects in subscenes gets converted to entity data. We can also create components and entities in runtime via baking.
DOTS: ECS
What is a System?
A System is a set of Data Transformation. Systems contain pure logic and little to no state. Each system has a system entity that processes all entities matching its queries.
Systems can be compared to the Start(), Update() and OnDestroy() in Monobehaviour.
Uses queries to only target specific set of entities.
DOTS: ECS
What is a Query in ECS?
ECS introduces something called Idiomatic Foreach (IFE) that takes care of main thread code - but also has IJobEntity which does jobified multithreaded code.
IFE is important since since IFE will run and execute its logic on any Archetype that matches with the EntityQuery specified. EntityQuery checks all entities with a specified set of components (Archetype).
Mathematics
What is MathF and math and the difference between them?
MathF is deal for non-bursted singlethreaded code or regular Monobehaviour code. It operate on single-precision float types and is mostly scalar-based (float, double, int). They do not support matrix operations or anything beyond 3D vectors directly.
math on the otherhand supports both scalar (float, double, int) and vectorized data types (float3, int4, quaternion). math is designed for high performance and efficiency, especially with Burst compilers which can auto vectorize operations (using SIMD). math is optimized to better auto-vectorisation and works great with burst compiler.