Hey everyone!
Documentation is rather sparse on those topics, greatly lacking colorful real world examples (especially for the topics on their own and for C# jobs combined with ECS) and some forum entries are rather vague and show considerable uncertainty.
So I’ve got some questions about C# jobs, ECS, Burst and compute shaders. Maybe some of you know some answers to those or some talks that actually show useful ways of integrating things like that. I am using LTS Unity 2018.3. I am new to intentional multi threaded programming and still trying to understand the usefulness and implications of that.
If I understand correctly, there is a manual way of implementing multi threading for CPUs. Are there any advantages of the basic, manual way over C# jobs or any restrictions when using C# jobs?
When using C# jobs for calculations on different cores, do those cores behave just like the core that uses the main thread?
For example, can I use a number of jobs running per frame for different usages on a number of cores?
Can I use a singular C# job to completely move certain ongoing calculations onto a different core (any core that currently isn’t busy)? For example, combat system calculations?
Can a C# job be a coroutine?
Does the ECS encourage not using GameObjects? I am greatly dependant on the GameObject workflow and I can’t imagine a useful use case for not using a GameObject or just use raw data, without any scene or component references at all.
Or are entities just a more efficient way to interpret and use GameObject data via script?
If I understand correctly, Burst is only useful when making use of the ECS, right?
How do compute shaders compare to C# jobs in terms of use cases and being able to implement those features easily? I understand compute shaders use a GPU, which likely uses quite a number of cores making it useful for graphics calculations.
When writing a custom AI system using a number of raycasts, would C# jobs make sense? Would compute shaders even work?
I have read about C# jobs taking quite some time to implement. Are C# jobs worth the development time or should I use .cpp for math heavy calculations?
I’ve also got two examples:
E1 (not tested if compiles, but it’s very similar to what I am working on):
```csharp
*public class Statics {
public static Transform Transform_GetClosest (Transform origin, Transform references) {int referencesLength = references.Length;
float currentDistance = 0.0f;
Transform closest = references[0];
float closestDistance = (references[0].position - origin.position).sqrMagnitude;
for (int i = 0; i < referencesLength; i++) {
currentDistance = (references.position - origin.position).sqrMagnitude;
if (currentDistance < closestDistance) {
closest = references;
closestDistance = currentDistance;
}
} return closest;
}
}
_```_
10.
The for loop of that could run on different cores simultaneously, right?
11.
Is there a way and would it make sense to use a compute shader for that, using float3s for the world positions of the transforms, getting the index and therefore the index of the closest transform?
E2, 12.
I am currently using color arrays, populated by .GetPixels from textures and use those to calculate resulting textures, e.g. adding, subtracting, multiplying those colors. Right now, I am doing that on CPU, which is very slow. I am not intending to change that, unless it’s very easy to implement a more performant solution, but it would be interesting if, in such a case, for example, the usage of a compute shader would make sense.
EDIT:
13.
How about platform support? Are those features supported on consoles? (Nintendo Switch? PS4?)
*
Best wishes,
Shu