2019-11-24 03:24:03 +01:00
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
2021-01-17 19:44:34 +01:00
|
|
|
using Ryujinx.Graphics.Texture;
|
|
|
|
using Ryujinx.Memory.Range;
|
2019-11-24 03:24:03 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
2020-12-17 19:39:52 +01:00
|
|
|
using System.Threading;
|
2019-11-24 03:24:03 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu
|
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
using Texture = Image.Texture;
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// GPU image presentation window.
|
|
|
|
/// </summary>
|
2019-11-24 03:24:03 +01:00
|
|
|
public class Window
|
|
|
|
{
|
2019-12-29 18:41:50 +01:00
|
|
|
private readonly GpuContext _context;
|
2019-11-24 03:24:03 +01:00
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Texture presented on the window.
|
|
|
|
/// </summary>
|
2019-11-24 03:24:03 +01:00
|
|
|
private struct PresentationTexture
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Texture cache where the texture might be located.
|
|
|
|
/// </summary>
|
|
|
|
public TextureCache Cache { get; }
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Texture information.
|
|
|
|
/// </summary>
|
|
|
|
public TextureInfo Info { get; }
|
|
|
|
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Physical memory locations where the texture data is located.
|
|
|
|
/// </summary>
|
|
|
|
public MultiRange Range { get; }
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Texture crop region.
|
|
|
|
/// </summary>
|
|
|
|
public ImageCrop Crop { get; }
|
|
|
|
|
2020-04-19 03:25:57 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Texture acquire callback.
|
|
|
|
/// </summary>
|
|
|
|
public Action<GpuContext, object> AcquireCallback { get; }
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Texture release callback.
|
|
|
|
/// </summary>
|
2020-04-19 03:25:57 +02:00
|
|
|
public Action<object> ReleaseCallback { get; }
|
2019-11-24 03:24:03 +01:00
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
2020-04-19 03:25:57 +02:00
|
|
|
/// User defined object, passed to the various callbacks.
|
2019-12-31 21:08:20 +01:00
|
|
|
/// </summary>
|
|
|
|
public object UserObj { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the presentation texture.
|
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <param name="cache">Texture cache used to look for the texture to be presented</param>
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <param name="info">Information of the texture to be presented</param>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="range">Physical memory locations where the texture data is located</param>
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <param name="crop">Texture crop region</param>
|
2020-04-19 03:25:57 +02:00
|
|
|
/// <param name="acquireCallback">Texture acquire callback</param>
|
|
|
|
/// <param name="releaseCallback">Texture release callback</param>
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <param name="userObj">User defined object passed to the release callback, can be used to identify the texture</param>
|
2019-11-24 03:24:03 +01:00
|
|
|
public PresentationTexture(
|
2021-06-29 19:32:02 +02:00
|
|
|
TextureCache cache,
|
2020-04-19 03:25:57 +02:00
|
|
|
TextureInfo info,
|
2021-01-17 19:44:34 +01:00
|
|
|
MultiRange range,
|
2020-04-19 03:25:57 +02:00
|
|
|
ImageCrop crop,
|
|
|
|
Action<GpuContext, object> acquireCallback,
|
|
|
|
Action<object> releaseCallback,
|
|
|
|
object userObj)
|
2019-11-24 03:24:03 +01:00
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
Cache = cache;
|
2020-04-19 03:25:57 +02:00
|
|
|
Info = info;
|
2021-01-17 19:44:34 +01:00
|
|
|
Range = range;
|
2020-04-19 03:25:57 +02:00
|
|
|
Crop = crop;
|
|
|
|
AcquireCallback = acquireCallback;
|
|
|
|
ReleaseCallback = releaseCallback;
|
|
|
|
UserObj = userObj;
|
2019-11-24 03:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 18:41:50 +01:00
|
|
|
private readonly ConcurrentQueue<PresentationTexture> _frameQueue;
|
2019-11-24 03:24:03 +01:00
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
private int _framesAvailable;
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU presentation window.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">GPU emulation context</param>
|
2019-11-24 03:24:03 +01:00
|
|
|
public Window(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
|
|
|
_frameQueue = new ConcurrentQueue<PresentationTexture>();
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Enqueues a frame for presentation.
|
|
|
|
/// This method is thread safe and can be called from any thread.
|
|
|
|
/// When the texture is presented and not needed anymore, the release callback is called.
|
|
|
|
/// It's an error to modify the texture after calling this method, before the release callback is called.
|
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <param name="pid">Process ID of the process that owns the texture pointed to by <paramref name="address"/></param>
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <param name="address">CPU virtual address of the texture data</param>
|
|
|
|
/// <param name="width">Texture width</param>
|
|
|
|
/// <param name="height">Texture height</param>
|
|
|
|
/// <param name="stride">Texture stride for linear texture, should be zero otherwise</param>
|
|
|
|
/// <param name="isLinear">Indicates if the texture is linear, normally false</param>
|
|
|
|
/// <param name="gobBlocksInY">GOB blocks in the Y direction, for block linear textures</param>
|
|
|
|
/// <param name="format">Texture format</param>
|
|
|
|
/// <param name="bytesPerPixel">Texture format bytes per pixel (must match the format)</param>
|
|
|
|
/// <param name="crop">Texture crop region</param>
|
2020-04-19 03:25:57 +02:00
|
|
|
/// <param name="acquireCallback">Texture acquire callback</param>
|
|
|
|
/// <param name="releaseCallback">Texture release callback</param>
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <param name="userObj">User defined object passed to the release callback</param>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <exception cref="ArgumentException">Thrown when <paramref name="pid"/> is invalid</exception>
|
2019-11-24 03:24:03 +01:00
|
|
|
public void EnqueueFrameThreadSafe(
|
2021-06-29 19:32:02 +02:00
|
|
|
long pid,
|
2020-04-19 03:25:57 +02:00
|
|
|
ulong address,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int stride,
|
|
|
|
bool isLinear,
|
|
|
|
int gobBlocksInY,
|
|
|
|
Format format,
|
|
|
|
int bytesPerPixel,
|
|
|
|
ImageCrop crop,
|
|
|
|
Action<GpuContext, object> acquireCallback,
|
|
|
|
Action<object> releaseCallback,
|
|
|
|
object userObj)
|
2019-11-24 03:24:03 +01:00
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
if (!_context.PhysicalMemoryRegistry.TryGetValue(pid, out var physicalMemory))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("The PID is invalid or the process was not registered", nameof(pid));
|
|
|
|
}
|
|
|
|
|
2020-09-11 01:48:48 +02:00
|
|
|
FormatInfo formatInfo = new FormatInfo(format, 1, 1, bytesPerPixel, 4);
|
2019-11-24 03:24:03 +01:00
|
|
|
|
|
|
|
TextureInfo info = new TextureInfo(
|
2021-01-17 19:44:34 +01:00
|
|
|
0UL,
|
2019-11-24 03:24:03 +01:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
stride,
|
|
|
|
isLinear,
|
|
|
|
gobBlocksInY,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
Target.Texture2D,
|
|
|
|
formatInfo);
|
|
|
|
|
2021-01-17 19:44:34 +01:00
|
|
|
int size = SizeCalculator.GetBlockLinearTextureSize(
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
bytesPerPixel,
|
|
|
|
gobBlocksInY,
|
|
|
|
1,
|
|
|
|
1).TotalSize;
|
|
|
|
|
|
|
|
MultiRange range = new MultiRange(address, (ulong)size);
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
_frameQueue.Enqueue(new PresentationTexture(
|
|
|
|
physicalMemory.TextureCache,
|
|
|
|
info,
|
|
|
|
range,
|
|
|
|
crop,
|
|
|
|
acquireCallback,
|
|
|
|
releaseCallback,
|
|
|
|
userObj));
|
2019-11-24 03:24:03 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Presents a texture on the queue.
|
|
|
|
/// If the queue is empty, then no texture is presented.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="swapBuffersCallback">Callback method to call when a new texture should be presented on the screen</param>
|
2019-11-24 03:24:03 +01:00
|
|
|
public void Present(Action swapBuffersCallback)
|
|
|
|
{
|
|
|
|
_context.AdvanceSequence();
|
|
|
|
|
|
|
|
if (_frameQueue.TryDequeue(out PresentationTexture pt))
|
|
|
|
{
|
2020-04-19 03:25:57 +02:00
|
|
|
pt.AcquireCallback(_context, pt.UserObj);
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
Texture texture = pt.Cache.FindOrCreateTexture(null, TextureSearchFlags.WithUpscale, pt.Info, 0, null, pt.Range);
|
2019-11-24 03:24:03 +01:00
|
|
|
|
|
|
|
texture.SynchronizeMemory();
|
|
|
|
|
Add a Multithreading layer for the GAL, multi-thread shader compilation at runtime (#2501)
* Initial Implementation
About as fast as nvidia GL multithreading, can be improved with faster command queuing.
* Struct based command list
Speeds up a bit. Still a lot of time lost to resource copy.
* Do shader init while the render thread is active.
* Introduce circular span pool V1
Ideally should be able to use structs instead of references for storing these spans on commands. Will try that next.
* Refactor SpanRef some more
Use a struct to represent SpanRef, rather than a reference.
* Flush buffers on background thread
* Use a span for UpdateRenderScale.
Much faster than copying the array.
* Calculate command size using reflection
* WIP parallel shaders
* Some minor optimisation
* Only 2 max refs per command now.
The command with 3 refs is gone. :relieved:
* Don't cast on the GPU side
* Remove redundant casts, force sync on window present
* Fix Shader Cache
* Fix host shader save.
* Fixup to work with new renderer stuff
* Make command Run static, use array of delegates as lookup
Profile says this takes less time than the previous way.
* Bring up to date
* Add settings toggle. Fix Muiltithreading Off mode.
* Fix warning.
* Release tracking lock for flushes
* Fix Conditional Render fast path with threaded gal
* Make handle iteration safe when releasing the lock
This is mostly temporary.
* Attempt to set backend threading on driver
Only really works on nvidia before launching a game.
* Fix race condition with BufferModifiedRangeList, exceptions in tracking actions
* Update buffer set commands
* Some cleanup
* Only use stutter workaround when using opengl renderer non-threaded
* Add host-conditional reservation of counter events
There has always been the possibility that conditional rendering could use a query object just as it is disposed by the counter queue. This change makes it so that when the host decides to use host conditional rendering, the query object is reserved so that it cannot be deleted. Counter events can optionally start reserved, as the threaded implementation can reserve them before the backend creates them, and there would otherwise be a short amount of time where the counter queue could dispose the event before a call to reserve it could be made.
* Address Feedback
* Make counter flush tracked again.
Hopefully does not cause any issues this time.
* Wait for FlushTo on the main queue thread.
Currently assumes only one thread will want to FlushTo (in this case, the GPU thread)
* Add SDL2 headless integration
* Add HLE macro commands.
Co-authored-by: Mary <mary@mary.zone>
2021-08-27 00:31:29 +02:00
|
|
|
_context.Renderer.Window.Present(texture.HostTexture, pt.Crop, swapBuffersCallback);
|
2019-11-24 03:24:03 +01:00
|
|
|
|
2020-04-19 03:25:57 +02:00
|
|
|
pt.ReleaseCallback(pt.UserObj);
|
2019-11-24 03:24:03 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 19:39:52 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Indicate that a frame on the queue is ready to be acquired.
|
|
|
|
/// </summary>
|
|
|
|
public void SignalFrameReady()
|
|
|
|
{
|
|
|
|
Interlocked.Increment(ref _framesAvailable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Determine if any frames are available, and decrement the available count if there are.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>True if a frame is available, false otherwise</returns>
|
|
|
|
public bool ConsumeFrameAvailable()
|
|
|
|
{
|
|
|
|
if (Interlocked.CompareExchange(ref _framesAvailable, 0, 0) != 0)
|
|
|
|
{
|
|
|
|
Interlocked.Decrement(ref _framesAvailable);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-24 03:24:03 +01:00
|
|
|
}
|
|
|
|
}
|