Skip to content

Instantly share code, notes, and snippets.

@olokobayusuf
Last active April 21, 2021 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olokobayusuf/9c9c40d9ad053124c4d0d41a48e05ae6 to your computer and use it in GitHub Desktop.
Save olokobayusuf/9c9c40d9ad053124c4d0d41a48e05ae6 to your computer and use it in GitHub Desktop.
Texture input that uses asynchronous readbacks, but commits frames on a background thread.
using NatSuite.Recorders.Inputs;
/// <summary>
/// Recorder input for recording video frames from textures.
/// </summary>
public class ThreadedAsyncTextureInput : ITextureInput {
#region --Client API--
/// <summary>
/// Create a texture input which performs asynchronous readbacks.
/// </summary>
/// <param name="recorder">Media recorder to receive video frames.</param>
public ThreadedAsyncTextureInput (IMediaRecorder recorder) => this.recorder = recorder;
/// <summary>
/// Commit a video frame from a texture.
/// </summary>
/// <param name="texture">Source texture.</param>
/// <param name="timestamp">Frame timestamp in nanoseconds.</param>
public unsafe void CommitFrame (Texture texture, long timestamp) {
// Blit
var (width, height) = recorder.frameSize;
var renderTexture = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32);
Graphics.Blit(texture, renderTexture);
// Readback
AsyncGPUReadback.Request(renderTexture, 0, request => recorder?.CommitFrame(
NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(request.GetData<byte>()),
timestamp
));
RenderTexture.ReleaseTemporary(renderTexture);
}
/// <summary>
/// Stop recorder input and release resources.
/// </summary>
public void Dispose () => recorder = null;
#endregion
#region --Operations--
private IMediaRecorder recorder;
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment