Implement changes from gdkchan/buffer-sharing-rebased
Co-authored-by: gdkchan <gab.dark.100@gmail.com> Co-authored-by: Alula <6276139+alula@users.noreply.github.com>
This commit is contained in:
@@ -9,11 +9,18 @@ using System.Threading;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu
|
||||
{
|
||||
using Texture = Image.Texture;
|
||||
|
||||
public record TextureData(int Width, int Height, byte[] Data);
|
||||
|
||||
/// <summary>
|
||||
/// GPU image presentation window.
|
||||
/// </summary>
|
||||
public class Window
|
||||
{
|
||||
private const int CaptureTextureWidth = 1280;
|
||||
private const int CaptureTextureHeight = 720;
|
||||
|
||||
private readonly GpuContext _context;
|
||||
|
||||
/// <summary>
|
||||
@@ -85,7 +92,21 @@ namespace Ryujinx.Graphics.Gpu
|
||||
}
|
||||
}
|
||||
|
||||
private class PresentedTexture
|
||||
{
|
||||
public readonly Texture Texture;
|
||||
public readonly ImageCrop Crop;
|
||||
|
||||
public PresentedTexture(Texture texture, ImageCrop crop)
|
||||
{
|
||||
Texture = texture;
|
||||
Crop = crop;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly ConcurrentQueue<PresentationTexture> _frameQueue;
|
||||
private PresentedTexture _lastPresentedTexture;
|
||||
private ITexture _captureTexture;
|
||||
|
||||
private int _framesAvailable;
|
||||
|
||||
@@ -188,6 +209,51 @@ namespace Ryujinx.Graphics.Gpu
|
||||
return true;
|
||||
}
|
||||
|
||||
public TextureData GetLastPresentedData()
|
||||
{
|
||||
PresentedTexture pt = Volatile.Read(ref _lastPresentedTexture);
|
||||
|
||||
if (pt != null)
|
||||
{
|
||||
byte[] inputData = CaptureLastFrame(pt.Texture.HostTexture, pt.Crop);
|
||||
|
||||
int size = SizeCalculator.GetBlockLinearTextureSize(
|
||||
CaptureTextureWidth,
|
||||
CaptureTextureHeight,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
4,
|
||||
16,
|
||||
1,
|
||||
1).TotalSize;
|
||||
|
||||
byte[] data = new byte[size];
|
||||
|
||||
LayoutConverter.ConvertLinearToBlockLinear(data, CaptureTextureWidth, CaptureTextureHeight, CaptureTextureWidth * 4, 4, 16, inputData);
|
||||
|
||||
return new TextureData(CaptureTextureWidth, CaptureTextureHeight, data);
|
||||
}
|
||||
|
||||
return new TextureData(0, 0, Array.Empty<byte>());
|
||||
}
|
||||
|
||||
public TextureData GetLastPresentedDataLinear()
|
||||
{
|
||||
PresentedTexture pt = Volatile.Read(ref _lastPresentedTexture);
|
||||
|
||||
if (pt != null)
|
||||
{
|
||||
byte[] inputData = CaptureLastFrame(pt.Texture.HostTexture, new ImageCrop());
|
||||
|
||||
return new TextureData(pt.Texture.Info.Width, pt.Texture.Info.Height, inputData);
|
||||
}
|
||||
|
||||
return new TextureData(0, 0, Array.Empty<byte>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Presents a texture on the queue.
|
||||
/// If the queue is empty, then no texture is presented.
|
||||
@@ -205,6 +271,10 @@ namespace Ryujinx.Graphics.Gpu
|
||||
|
||||
pt.Cache.Tick();
|
||||
|
||||
EnsureCaptureTexture();
|
||||
|
||||
Volatile.Write(ref _lastPresentedTexture, new PresentedTexture(texture, pt.Crop));
|
||||
|
||||
texture.SynchronizeMemory();
|
||||
|
||||
ImageCrop crop = new(
|
||||
@@ -244,6 +314,96 @@ namespace Ryujinx.Graphics.Gpu
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureCaptureTexture()
|
||||
{
|
||||
if (_captureTexture == null)
|
||||
{
|
||||
_captureTexture = _context.Renderer.CreateTexture(new TextureCreateInfo(
|
||||
1280,
|
||||
720,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
4,
|
||||
Format.R8G8B8A8Unorm,
|
||||
DepthStencilMode.Depth,
|
||||
Target.Texture2D,
|
||||
SwizzleComponent.Red,
|
||||
SwizzleComponent.Green,
|
||||
SwizzleComponent.Blue,
|
||||
SwizzleComponent.Alpha));
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] CaptureLastFrame(ITexture lastFrame, ImageCrop crop)
|
||||
{
|
||||
int cropLeft, cropRight, cropTop, cropBottom;
|
||||
|
||||
if (crop.Left == 0 && crop.Right == 0)
|
||||
{
|
||||
cropLeft = 0;
|
||||
cropRight = lastFrame.Width;
|
||||
}
|
||||
else
|
||||
{
|
||||
cropLeft = crop.Left;
|
||||
cropRight = crop.Right;
|
||||
}
|
||||
|
||||
if (crop.Top == 0 && crop.Bottom == 0)
|
||||
{
|
||||
cropTop = 0;
|
||||
cropBottom = lastFrame.Height;
|
||||
}
|
||||
else
|
||||
{
|
||||
cropTop = crop.Top;
|
||||
cropBottom = crop.Bottom;
|
||||
}
|
||||
|
||||
int x1, y1, x2, y2;
|
||||
|
||||
if (crop.FlipX)
|
||||
{
|
||||
x1 = cropRight;
|
||||
x2 = cropLeft;
|
||||
}
|
||||
else
|
||||
{
|
||||
x1 = cropLeft;
|
||||
x2 = cropRight;
|
||||
}
|
||||
|
||||
if (crop.FlipY)
|
||||
{
|
||||
y1 = cropBottom;
|
||||
y2 = cropTop;
|
||||
}
|
||||
else
|
||||
{
|
||||
y1 = cropTop;
|
||||
y2 = cropBottom;
|
||||
}
|
||||
|
||||
Extents2D srcRegion = new(x1, y1, x2, y2);
|
||||
Extents2D dstRegion = new(0, 0, CaptureTextureWidth, CaptureTextureHeight);
|
||||
|
||||
byte[] outputData = null;
|
||||
|
||||
_context.Renderer.BackgroundContextAction(() =>
|
||||
{
|
||||
lastFrame.CopyTo(_captureTexture, srcRegion, dstRegion, true);
|
||||
|
||||
using var data = _captureTexture.GetData();
|
||||
|
||||
outputData = data.Get().ToArray();
|
||||
});
|
||||
|
||||
return outputData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicate that a frame on the queue is ready to be acquired.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user