ProwlEngine / Prowl

An Open Source C# 3D Game Engine under MIT license, inspired by Unity and featuring a complete editor and built on Silk.NET
MIT License
360 stars 30 forks source link

[Engine] Job System #69

Closed michaelsakharov closed 9 months ago

michaelsakharov commented 9 months ago

A Efficient Multi-threaded Job System

Will be handy to have especially to offload things like Asset Importing onto.

michaelsakharov commented 9 months ago

Forgot c# has a lot of Thread related helper classes, instead of building a Job System it makes more sense to just use those.

PaperPrototype commented 9 months ago

What about using System.Threading.Tasks

This is how I utilized Tasks in Unity for multi threading voxel terrain

private IEnumerator RunMeshTask(byte[] voxels, Action<Mesh> callback) {
    var vertices = new();
    var triangles = new();

    var task = Task.Factory.StartNew(delegate {
       // compute mesh
    });

    // wait until task is completed
    yield return new WaitUntil(() => {
        return task.IsCompleted;
    });

    var mesh = new Mesh();
    mesh.vertices = vertices;
    mesh.triangles = triangles;

    callback(mesh);
}

// usage
StartCoroutine(RunMeshTask(voxels, (_mesh) => {
    // this callback gets called once the task has finished and gives a voxel mesh
    meshFilter.mesh = _mesh;
}));
michaelsakharov commented 9 months ago

That's what I meant by c# has existing solutions :P