Last active
April 21, 2021 13:13
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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