Skip to content

Commit

Permalink
Add Barracuda GPU worker supports (derenlei#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ROBYER1 authored Jan 22, 2021
1 parent 23351d3 commit 9a64408
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
6 changes: 2 additions & 4 deletions Assets/Scripts/DetectorYolo2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class DetectorYolo2 : MonoBehaviour, Detector
// 1.08F, 1.19F, 3.42F, 4.41F, 6.63F, 11.38F, 9.42F, 5.11F, 16.62F, 10.52F // yolov2-tiny-voc
//0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov2-tiny
0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov2-tiny-food
//0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov3
};


Expand All @@ -58,9 +57,8 @@ public void Start()
.Where(s => !String.IsNullOrEmpty(s)).ToArray();
var model = ModelLoader.Load(this.modelFile);
// https://docs.unity3d.com/Packages/[email protected]/manual/Worker.html
// var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
this.worker = WorkerFactory.CreateWorker(workerType, model);
//These checks all check for GPU before CPU as GPU is preferred if the platform + rendering pipeline support it
this.worker = GraphicsWorker.GetWorker(model);
}


Expand Down
9 changes: 2 additions & 7 deletions Assets/Scripts/DetectorYolo3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ public class DetectorYolo3 : MonoBehaviour, Detector

private float[] anchors = new float[]
{
// 1.08F, 1.19F, 3.42F, 4.41F, 6.63F, 11.38F, 9.42F, 5.11F, 16.62F, 10.52F // yolov2-tiny-voc
//0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov2-tiny
//0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov2-tiny-food
//0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov3
10F, 14F, 23F, 27F, 37F, 58F, 81F, 82F, 135F, 169F, 344F, 319F // yolov3-tiny
};

Expand All @@ -65,9 +61,8 @@ public void Start()
.Where(s => !String.IsNullOrEmpty(s)).ToArray();
var model = ModelLoader.Load(this.modelFile);
// https://docs.unity3d.com/Packages/[email protected]/manual/Worker.html
//var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
this.worker = WorkerFactory.CreateWorker(workerType, model);
//These checks all check for GPU before CPU as GPU is preferred if the platform + rendering pipeline support it
this.worker = GraphicsWorker.GetWorker(model);
}


Expand Down
59 changes: 59 additions & 0 deletions Assets/Scripts/GraphicsWorker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

using UnityEngine;
using System.Collections;
using Unity.Barracuda;

public class GraphicsWorker: MonoBehaviour
{

public static IWorker GetWorker(Model model)
{
IWorker worker;
#if UNITY_IOS //Only IOS
Debug.Log("Graphics API: " + SystemInfo.graphicsDeviceType);
if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
{
//IOS 11 needed for ARKit, IOS 11 has Metal support only, therefore GPU can run
var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
worker = WorkerFactory.CreateWorker(workerType, model);
}
else
{
//If Metal support is dropped for some reason, fall back to CPU
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
worker = WorkerFactory.CreateWorker(workerType, model);
}

#elif UNITY_ANDROID //Only Android
Debug.Log("Graphics API: " + SystemInfo.graphicsDeviceType);
if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Vulkan)
{
//Vulkan on Android supports GPU
//However, ARCore does not currently support Vulkan, when it does, this line will work
var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
worker = WorkerFactory.CreateWorker(workerType, model);
}
else
{
//If not vulkan, fall back to CPU
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
worker = WorkerFactory.CreateWorker(workerType, model);
}

#elif UNITY_WEBGL //Only WebGL
Debug.Log("Graphics API: " + SystemInfo.graphicsDeviceType);
//WebGL only supports CPU
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
worker = WorkerFactory.CreateWorker(workerType, model);

#else //Any other platform
Debug.Log("Graphics API: " + SystemInfo.graphicsDeviceType);
// https://docs.unity3d.com/Packages/[email protected]/manual/SupportedPlatforms.html
//var workerType = WorkerFactory.Type.CSharpBurst; // CPU
var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
worker = WorkerFactory.CreateWorker(workerType, model);
#endif

return worker;
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/GraphicsWorker.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9a64408

Please sign in to comment.