Skip to content

Commit 9a64408

Browse files
author
ROBYER1
authored
Add Barracuda GPU worker supports (#14)
1 parent 23351d3 commit 9a64408

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

Assets/Scripts/DetectorYolo2.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public class DetectorYolo2 : MonoBehaviour, Detector
4848
// 1.08F, 1.19F, 3.42F, 4.41F, 6.63F, 11.38F, 9.42F, 5.11F, 16.62F, 10.52F // yolov2-tiny-voc
4949
//0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov2-tiny
5050
0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov2-tiny-food
51-
//0.57273F, 0.677385F, 1.87446F, 2.06253F, 3.33843F, 5.47434F, 7.88282F, 3.52778F, 9.77052F, 9.16828F // yolov3
5251
};
5352

5453

@@ -58,9 +57,8 @@ public void Start()
5857
.Where(s => !String.IsNullOrEmpty(s)).ToArray();
5958
var model = ModelLoader.Load(this.modelFile);
6059
// https://docs.unity3d.com/Packages/com.unity.barracuda@1.0/manual/Worker.html
61-
// var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
62-
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
63-
this.worker = WorkerFactory.CreateWorker(workerType, model);
60+
//These checks all check for GPU before CPU as GPU is preferred if the platform + rendering pipeline support it
61+
this.worker = GraphicsWorker.GetWorker(model);
6462
}
6563

6664

Assets/Scripts/DetectorYolo3.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ public class DetectorYolo3 : MonoBehaviour, Detector
5151

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

@@ -65,9 +61,8 @@ public void Start()
6561
.Where(s => !String.IsNullOrEmpty(s)).ToArray();
6662
var model = ModelLoader.Load(this.modelFile);
6763
// https://docs.unity3d.com/Packages/com.unity.barracuda@1.0/manual/Worker.html
68-
//var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
69-
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
70-
this.worker = WorkerFactory.CreateWorker(workerType, model);
64+
//These checks all check for GPU before CPU as GPU is preferred if the platform + rendering pipeline support it
65+
this.worker = GraphicsWorker.GetWorker(model);
7166
}
7267

7368

Assets/Scripts/GraphicsWorker.cs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
using UnityEngine;
3+
using System.Collections;
4+
using Unity.Barracuda;
5+
6+
public class GraphicsWorker: MonoBehaviour
7+
{
8+
9+
public static IWorker GetWorker(Model model)
10+
{
11+
IWorker worker;
12+
#if UNITY_IOS //Only IOS
13+
Debug.Log("Graphics API: " + SystemInfo.graphicsDeviceType);
14+
if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
15+
{
16+
//IOS 11 needed for ARKit, IOS 11 has Metal support only, therefore GPU can run
17+
var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
18+
worker = WorkerFactory.CreateWorker(workerType, model);
19+
}
20+
else
21+
{
22+
//If Metal support is dropped for some reason, fall back to CPU
23+
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
24+
worker = WorkerFactory.CreateWorker(workerType, model);
25+
}
26+
27+
#elif UNITY_ANDROID //Only Android
28+
Debug.Log("Graphics API: " + SystemInfo.graphicsDeviceType);
29+
if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Vulkan)
30+
{
31+
//Vulkan on Android supports GPU
32+
//However, ARCore does not currently support Vulkan, when it does, this line will work
33+
var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
34+
worker = WorkerFactory.CreateWorker(workerType, model);
35+
}
36+
else
37+
{
38+
//If not vulkan, fall back to CPU
39+
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
40+
worker = WorkerFactory.CreateWorker(workerType, model);
41+
}
42+
43+
#elif UNITY_WEBGL //Only WebGL
44+
Debug.Log("Graphics API: " + SystemInfo.graphicsDeviceType);
45+
//WebGL only supports CPU
46+
var workerType = WorkerFactory.Type.CSharpBurst; // CPU
47+
worker = WorkerFactory.CreateWorker(workerType, model);
48+
49+
#else //Any other platform
50+
Debug.Log("Graphics API: " + SystemInfo.graphicsDeviceType);
51+
// https://docs.unity3d.com/Packages/com.unity.barracuda@1.0/manual/SupportedPlatforms.html
52+
//var workerType = WorkerFactory.Type.CSharpBurst; // CPU
53+
var workerType = WorkerFactory.Type.ComputePrecompiled; // GPU
54+
worker = WorkerFactory.CreateWorker(workerType, model);
55+
#endif
56+
57+
return worker;
58+
}
59+
}

Assets/Scripts/GraphicsWorker.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)