Skip to content

Commit 2e6cf29

Browse files
committed
feat: Read the input Texture on GPU
1 parent efb103a commit 2e6cf29

File tree

1 file changed

+12
-27
lines changed
  • Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Experimental

1 file changed

+12
-27
lines changed

Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/Experimental/TextureFrame.cs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,41 +108,28 @@ public void Dispose()
108108
/// If <paramref name="src" /> format is different from <see cref="format" />, it converts the format.
109109
/// </summary>
110110
/// <remarks>
111-
/// After calling it, pixel data can't be read from CPU safely.
111+
/// After calling it, pixel data can't be read on CPU safely.
112+
/// To read pixel data on CPU, use <see cref="ReadTextureAsync" />.
112113
/// </remarks>
113-
public bool ReadTextureOnGPU(Texture src)
114+
public void ReadTextureOnGPU(Texture src, bool flipHorizontally, bool flipVertically)
114115
{
115-
if (GetTextureFormat(src) != format)
116-
{
117-
return Graphics.ConvertTexture(src, _texture);
118-
}
119-
Graphics.CopyTexture(src, _texture);
120-
return true;
121-
}
116+
ReadTextureInternal(src, flipHorizontally, flipVertically);
122117

123-
public AsyncGPUReadbackRequest ReadTextureAsync(Texture src)
124-
{
125-
if (!ReadTextureOnGPU(src))
126-
{
127-
throw new InvalidOperationException("Failed to read texture on GPU");
128-
}
129-
130-
return AsyncGPUReadback.Request(_texture, 0, (req) =>
131-
{
132-
if (_texture == null)
133-
{
134-
return;
135-
}
136-
_texture.LoadRawTextureData(req.GetData<byte>());
137-
_texture.Apply();
138-
});
118+
Graphics.CopyTexture(_tmpRenderTexture, _texture);
119+
RenderTexture.ReleaseTemporary(_tmpRenderTexture);
139120
}
140121

141122
/// <remarks>
142123
/// This method is not thread-safe.
143124
/// Avoid calling this method again before the returned <see cref="AsyncGPUReadbackRequest.done"/> is <see langword="true"/>.
144125
/// </remarks>
145126
public AsyncGPUReadbackRequest ReadTextureAsync(Texture src, bool flipHorizontally, bool flipVertically)
127+
{
128+
ReadTextureInternal(src, flipHorizontally, flipVertically);
129+
return AsyncGPUReadback.Request(_tmpRenderTexture, 0, _onReadBackRenderTexture);
130+
}
131+
132+
private void ReadTextureInternal(Texture src, bool flipHorizontally, bool flipVertically)
146133
{
147134
var graphicsFormat = GraphicsFormatUtility.GetGraphicsFormat(format, true);
148135
_tmpRenderTexture = RenderTexture.GetTemporary(src.width, src.height, 32, graphicsFormat);
@@ -163,8 +150,6 @@ public AsyncGPUReadbackRequest ReadTextureAsync(Texture src, bool flipHorizontal
163150
}
164151
Graphics.Blit(src, _tmpRenderTexture, scale, offset);
165152
RenderTexture.active = currentRenderTexture;
166-
167-
return AsyncGPUReadback.Request(_tmpRenderTexture, 0, _onReadBackRenderTexture);
168153
}
169154

170155
private readonly Action<AsyncGPUReadbackRequest> _onReadBackRenderTexture;

0 commit comments

Comments
 (0)