Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Sentry.Unity/SentryScreenshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading.Tasks;
using Sentry.Extensibility;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;

namespace Sentry.Unity;
Expand Down Expand Up @@ -56,11 +55,13 @@ internal static void CaptureAsync(SentryUnityOptions options, Action<byte[]?> on
{
var renderTexture = CaptureIntoRenderTexture(options, Screen.width, Screen.height);
var (width, height) = ((uint)renderTexture.width, (uint)renderTexture.height);
var format = renderTexture.graphicsFormat;

try
{
// The texture has to stay out of the pool until the readback is done with it
AsyncGPUReadback.Request(renderTexture, 0, TextureFormat.RGBA32, readback =>
// The texture has to stay out of the pool until the readback is done with it. Reading it back in its own
// format, as converting fails for formats the platform can't ReadPixels from, e.g. BGRA on Metal.
AsyncGPUReadback.Request(renderTexture, 0, readback =>
{
RenderTexture.ReleaseTemporary(renderTexture);

Expand All @@ -77,7 +78,7 @@ internal static void CaptureAsync(SentryUnityOptions options, Action<byte[]?> on
byte[]? bytes = null;
try
{
bytes = ImageConversion.EncodeArrayToJPG(pixels, GraphicsFormat.R8G8B8A8_UNorm, width, height,
bytes = ImageConversion.EncodeArrayToJPG(pixels, format, width, height,
0, options.ScreenshotCompression);
}
catch (Exception e)
Expand Down
20 changes: 20 additions & 0 deletions test/Sentry.Unity.Tests/SentryScreenshotTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections;
using System.Threading.Tasks;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace Sentry.Unity.Tests;

Expand Down Expand Up @@ -61,4 +64,21 @@ public void CaptureScreenshot_RestoresActiveRenderTexture()
Object.Destroy(gameTarget);
}
}

// Unity logs an error when the readback has to convert from a format it can't ReadPixels from, e.g. BGRA on Metal
[UnityTest]
public IEnumerator CaptureAsync_ReadsBackScreenshot()
{
if (!SystemInfo.supportsAsyncGPUReadback)
{
Assert.Ignore("Async GPU readback is not supported.");
}

var screenshot = new TaskCompletionSource<byte[]?>();

SentryScreenshot.CaptureAsync(new SentryUnityOptions(), bytes => screenshot.SetResult(bytes));
yield return new WaitUntil(() => screenshot.Task.IsCompleted);

Assert.IsNotEmpty(screenshot.Task.Result);
}
}
Loading