Skip to content

Commit

Permalink
refactor: setup gpu resources only once
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Jan 31, 2021
1 parent 683b88f commit 4a65687
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions Assets/MediaPipe/Examples/Scripts/SceneDirector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ void StartGraph() {
return;
}

if (useGPU) {
SetupGpuResources();
}
graphRunner = StartCoroutine(RunGraph());
}

Expand All @@ -128,6 +131,24 @@ void StopGraph() {
}
}

void SetupGpuResources() {
if (gpuResources != null) {
Debug.Log("Gpu resources are already initialized");
return;
}

// TODO: have to wait for currentContext to be initialized.
if (currentContext == IntPtr.Zero) {
Debug.LogWarning("No EGL Context Found");
} else {
Debug.Log($"EGL Context Found ({currentContext})");
}

gpuResources = GpuResources.Create(currentContext).ConsumeValueOrDie();
gpuHelper = new GlCalculatorHelper();
gpuHelper.InitializeForTest(gpuResources);
}

IEnumerator RunGraph() {
yield return WaitForAssets();

Expand Down Expand Up @@ -155,17 +176,6 @@ IEnumerator RunGraph() {
var graph = graphContainer.GetComponent<IDemoGraph<TextureFrame>>();

if (useGPU) {
// TODO: have to wait for currentContext to be initialized.
if (currentContext == IntPtr.Zero) {
Debug.LogWarning("No EGL Context Found");
} else {
Debug.Log($"EGL Context Found ({currentContext})");
}

gpuResources = GpuResources.Create(currentContext).ConsumeValueOrDie();
gpuHelper = new GlCalculatorHelper();
gpuHelper.InitializeForTest(gpuResources);

graph.Initialize(gpuResources, gpuHelper);
} else {
graph.Initialize();
Expand Down

0 comments on commit 4a65687

Please sign in to comment.