2019-10-13 07:02:07 +01:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
using Ryujinx.Graphics.GAL;
|
2022-05-15 12:30:15 +01:00
|
|
|
using Ryujinx.Graphics.OpenGL.Helper;
|
2020-05-23 10:46:09 +01:00
|
|
|
using Ryujinx.Graphics.OpenGL.Image;
|
2019-10-13 07:02:07 +01:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.OpenGL
|
|
|
|
{
|
2019-12-31 22:09:49 +00:00
|
|
|
class Window : IWindow, IDisposable
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2022-05-15 12:30:15 +01:00
|
|
|
private const int TextureCount = 3;
|
2020-03-29 04:02:58 +01:00
|
|
|
private readonly Renderer _renderer;
|
|
|
|
|
2019-11-01 00:32:43 +00:00
|
|
|
private int _width;
|
|
|
|
private int _height;
|
2022-05-15 12:30:15 +01:00
|
|
|
private bool _sizeChanged;
|
2019-10-13 07:02:07 +01:00
|
|
|
private int _copyFramebufferHandle;
|
2022-05-15 12:30:15 +01:00
|
|
|
private int _stagingFrameBuffer;
|
|
|
|
private int[] _stagingTextures;
|
|
|
|
private int _currentTexture;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 21:18:35 +01:00
|
|
|
internal BackgroundContextWorker BackgroundContext { get; private set; }
|
|
|
|
|
2021-06-28 21:09:43 +01:00
|
|
|
internal bool ScreenCaptureRequested { get; set; }
|
|
|
|
|
2020-03-29 04:02:58 +01:00
|
|
|
public Window(Renderer renderer)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2020-03-29 04:02:58 +01:00
|
|
|
_renderer = renderer;
|
2022-05-15 12:30:15 +01:00
|
|
|
_stagingTextures = new int[TextureCount];
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
public void Present(ITexture texture, ImageCrop crop, Action<object> swapBuffersCallback)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
2019-11-24 02:24:03 +00:00
|
|
|
GL.Disable(EnableCap.FramebufferSrgb);
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
if (_sizeChanged)
|
|
|
|
{
|
|
|
|
if (_stagingFrameBuffer != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteTextures(_stagingTextures.Length, _stagingTextures);
|
|
|
|
GL.DeleteFramebuffer(_stagingFrameBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateStagingFramebuffer();
|
|
|
|
_sizeChanged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
(int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();
|
|
|
|
|
|
|
|
CopyTextureToFrameBufferRGB(_stagingFrameBuffer, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop, swapBuffersCallback);
|
|
|
|
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);
|
2020-02-13 17:43:29 +00:00
|
|
|
|
|
|
|
GL.Enable(EnableCap.FramebufferSrgb);
|
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-26 23:31:29 +01:00
|
|
|
|
Fix OpenGL issues with RTSS overlays and OBS Game Capture (#3217)
OpenGL game overlays and hooks tend to make a lot of assumptions about how games present frames to the screen, since presentation in OpenGL kind of sucks and they would like to have info such as the size of the screen, or if the contents are SRGB rather than linear.
There are two ways of getting this. OBS hooks swap buffers to get a frame for video capture, but it actually checks the bound framebuffer at the time. I made sure that this matches the output framebuffer (the window) so that the output matches the size. RTSS checks the viewport size by default, but this was actually set to the last used viewport by the game, causing the OSD to fly all across the screen depending on how it was used (or res scale). The viewport is now manually set to match the output framebuffer size.
In the case of RTSS, it also loads its resources by destructively setting a pixel pack parameter without regard to what it was set to by the guest application. OpenGL state can be set for a long period of time and is not expected to be set before each call to a method, so randomly changing it isn't great practice. To fix this, I've added a line to set the pixel unpack alignment back to 4 after presentation, which should cover RTSS loading its incredibly ugly font.
- RTSS and overlays that use it should no longer cause certain textures to load incorrectly. (mario kart 8, pokemon legends arceus)
- OBS Game Capture should no longer crop the game output incorrectly, flicker randomly, or capture with incorrect gamma.
This doesn't fix issues with how RTSS reports our frame timings.
2022-03-20 16:37:45 +00:00
|
|
|
// Restore unpack alignment to 4, as performance overlays such as RTSS may change this to load their resources.
|
|
|
|
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
|
2020-02-13 17:43:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
private void CreateStagingFramebuffer()
|
|
|
|
{
|
|
|
|
_stagingFrameBuffer = GL.GenFramebuffer();
|
|
|
|
GL.GenTextures(_stagingTextures.Length, _stagingTextures);
|
|
|
|
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _stagingFrameBuffer);
|
|
|
|
|
|
|
|
foreach (var stagingTexture in _stagingTextures)
|
|
|
|
{
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, stagingTexture);
|
|
|
|
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, _width, _height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
|
|
|
|
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
|
|
|
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
|
|
|
|
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, stagingTexture, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, 0);
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:43:29 +00:00
|
|
|
public void SetSize(int width, int height)
|
|
|
|
{
|
2022-05-15 12:30:15 +01:00
|
|
|
_width = width;
|
2020-02-13 17:43:29 +00:00
|
|
|
_height = height;
|
2022-05-15 12:30:15 +01:00
|
|
|
_sizeChanged = true;
|
2020-02-13 17:43:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop, Action<object> swapBuffersCallback)
|
2020-02-13 17:43:29 +00:00
|
|
|
{
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, _stagingTextures[_currentTexture], 0);
|
|
|
|
|
2021-08-20 22:26:25 +01:00
|
|
|
TextureView viewConverted = view.Format.IsBgr() ? _renderer.TextureCopy.BgraSwap(view) : view;
|
2020-07-26 04:03:40 +01:00
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
GL.FramebufferTexture(
|
|
|
|
FramebufferTarget.ReadFramebuffer,
|
|
|
|
FramebufferAttachment.ColorAttachment0,
|
2020-07-26 04:03:40 +01:00
|
|
|
viewConverted.Handle,
|
2019-10-13 07:02:07 +01:00
|
|
|
0);
|
|
|
|
|
|
|
|
GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
|
|
|
|
|
2020-04-07 10:19:45 +01:00
|
|
|
GL.Disable(EnableCap.RasterizerDiscard);
|
|
|
|
GL.Disable(IndexedEnableCap.ScissorTest, 0);
|
2020-03-29 04:02:58 +01:00
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
|
|
|
|
|
|
|
int srcX0, srcX1, srcY0, srcY1;
|
2020-07-07 03:41:07 +01:00
|
|
|
float scale = view.ScaleFactor;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
if (crop.Left == 0 && crop.Right == 0)
|
|
|
|
{
|
|
|
|
srcX0 = 0;
|
2020-07-07 03:41:07 +01:00
|
|
|
srcX1 = (int)(view.Width / scale);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
srcX0 = crop.Left;
|
|
|
|
srcX1 = crop.Right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crop.Top == 0 && crop.Bottom == 0)
|
|
|
|
{
|
|
|
|
srcY0 = 0;
|
2020-07-07 03:41:07 +01:00
|
|
|
srcY1 = (int)(view.Height / scale);
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
srcY0 = crop.Top;
|
|
|
|
srcY1 = crop.Bottom;
|
|
|
|
}
|
|
|
|
|
2020-07-07 03:41:07 +01:00
|
|
|
if (scale != 1f)
|
|
|
|
{
|
|
|
|
srcX0 = (int)(srcX0 * scale);
|
|
|
|
srcY0 = (int)(srcY0 * scale);
|
|
|
|
srcX1 = (int)Math.Ceiling(srcX1 * scale);
|
|
|
|
srcY1 = (int)Math.Ceiling(srcY1 * scale);
|
|
|
|
}
|
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
|
|
|
|
float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));
|
2019-10-13 07:02:07 +01:00
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
int dstWidth = (int)(_width * ratioX);
|
2019-10-13 07:02:07 +01:00
|
|
|
int dstHeight = (int)(_height * ratioY);
|
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
int dstPaddingX = (_width - dstWidth) / 2;
|
2019-10-13 07:02:07 +01:00
|
|
|
int dstPaddingY = (_height - dstHeight) / 2;
|
|
|
|
|
|
|
|
int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
|
|
|
|
int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;
|
|
|
|
|
|
|
|
int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
|
|
|
|
int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;
|
|
|
|
|
2021-06-28 21:09:43 +01:00
|
|
|
if (ScreenCaptureRequested)
|
|
|
|
{
|
2021-08-20 22:26:25 +01:00
|
|
|
CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY);
|
2021-06-28 21:09:43 +01:00
|
|
|
|
|
|
|
ScreenCaptureRequested = false;
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
GL.BlitFramebuffer(
|
|
|
|
srcX0,
|
|
|
|
srcY0,
|
|
|
|
srcX1,
|
|
|
|
srcY1,
|
|
|
|
dstX0,
|
|
|
|
dstY0,
|
|
|
|
dstX1,
|
|
|
|
dstY1,
|
|
|
|
ClearBufferMask.ColorBufferBit,
|
|
|
|
BlitFramebufferFilter.Linear);
|
|
|
|
|
2020-02-13 17:43:29 +00:00
|
|
|
// Remove Alpha channel
|
2021-06-01 08:29:01 +01:00
|
|
|
GL.ColorMask(false, false, false, true);
|
|
|
|
GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
|
|
|
|
|
|
|
for (int i = 0; i < Constants.MaxRenderTargets; i++)
|
|
|
|
{
|
|
|
|
((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
|
|
|
|
}
|
2020-02-13 17:43:29 +00:00
|
|
|
|
Fix OpenGL issues with RTSS overlays and OBS Game Capture (#3217)
OpenGL game overlays and hooks tend to make a lot of assumptions about how games present frames to the screen, since presentation in OpenGL kind of sucks and they would like to have info such as the size of the screen, or if the contents are SRGB rather than linear.
There are two ways of getting this. OBS hooks swap buffers to get a frame for video capture, but it actually checks the bound framebuffer at the time. I made sure that this matches the output framebuffer (the window) so that the output matches the size. RTSS checks the viewport size by default, but this was actually set to the last used viewport by the game, causing the OSD to fly all across the screen depending on how it was used (or res scale). The viewport is now manually set to match the output framebuffer size.
In the case of RTSS, it also loads its resources by destructively setting a pixel pack parameter without regard to what it was set to by the guest application. OpenGL state can be set for a long period of time and is not expected to be set before each call to a method, so randomly changing it isn't great practice. To fix this, I've added a line to set the pixel unpack alignment back to 4 after presentation, which should cover RTSS loading its incredibly ugly font.
- RTSS and overlays that use it should no longer cause certain textures to load incorrectly. (mario kart 8, pokemon legends arceus)
- OBS Game Capture should no longer crop the game output incorrectly, flicker randomly, or capture with incorrect gamma.
This doesn't fix issues with how RTSS reports our frame timings.
2022-03-20 16:37:45 +00:00
|
|
|
// Set clip control, viewport and the framebuffer to the output to placate overlays and OBS capture.
|
|
|
|
GL.ClipControl(ClipOrigin.LowerLeft, ClipDepthMode.NegativeOneToOne);
|
|
|
|
GL.Viewport(0, 0, _width, _height);
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.Framebuffer, drawFramebuffer);
|
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, _stagingFrameBuffer);
|
Fix OpenGL issues with RTSS overlays and OBS Game Capture (#3217)
OpenGL game overlays and hooks tend to make a lot of assumptions about how games present frames to the screen, since presentation in OpenGL kind of sucks and they would like to have info such as the size of the screen, or if the contents are SRGB rather than linear.
There are two ways of getting this. OBS hooks swap buffers to get a frame for video capture, but it actually checks the bound framebuffer at the time. I made sure that this matches the output framebuffer (the window) so that the output matches the size. RTSS checks the viewport size by default, but this was actually set to the last used viewport by the game, causing the OSD to fly all across the screen depending on how it was used (or res scale). The viewport is now manually set to match the output framebuffer size.
In the case of RTSS, it also loads its resources by destructively setting a pixel pack parameter without regard to what it was set to by the guest application. OpenGL state can be set for a long period of time and is not expected to be set before each call to a method, so randomly changing it isn't great practice. To fix this, I've added a line to set the pixel unpack alignment back to 4 after presentation, which should cover RTSS loading its incredibly ugly font.
- RTSS and overlays that use it should no longer cause certain textures to load incorrectly. (mario kart 8, pokemon legends arceus)
- OBS Game Capture should no longer crop the game output incorrectly, flicker randomly, or capture with incorrect gamma.
This doesn't fix issues with how RTSS reports our frame timings.
2022-03-20 16:37:45 +00:00
|
|
|
|
2022-05-15 12:30:15 +01:00
|
|
|
swapBuffersCallback((object)_stagingTextures[_currentTexture]);
|
|
|
|
_currentTexture = ++_currentTexture % _stagingTextures.Length;
|
2020-03-29 04:02:58 +01:00
|
|
|
|
Fix OpenGL issues with RTSS overlays and OBS Game Capture (#3217)
OpenGL game overlays and hooks tend to make a lot of assumptions about how games present frames to the screen, since presentation in OpenGL kind of sucks and they would like to have info such as the size of the screen, or if the contents are SRGB rather than linear.
There are two ways of getting this. OBS hooks swap buffers to get a frame for video capture, but it actually checks the bound framebuffer at the time. I made sure that this matches the output framebuffer (the window) so that the output matches the size. RTSS checks the viewport size by default, but this was actually set to the last used viewport by the game, causing the OSD to fly all across the screen depending on how it was used (or res scale). The viewport is now manually set to match the output framebuffer size.
In the case of RTSS, it also loads its resources by destructively setting a pixel pack parameter without regard to what it was set to by the guest application. OpenGL state can be set for a long period of time and is not expected to be set before each call to a method, so randomly changing it isn't great practice. To fix this, I've added a line to set the pixel unpack alignment back to 4 after presentation, which should cover RTSS loading its incredibly ugly font.
- RTSS and overlays that use it should no longer cause certain textures to load incorrectly. (mario kart 8, pokemon legends arceus)
- OBS Game Capture should no longer crop the game output incorrectly, flicker randomly, or capture with incorrect gamma.
This doesn't fix issues with how RTSS reports our frame timings.
2022-03-20 16:37:45 +00:00
|
|
|
((Pipeline)_renderer.Pipeline).RestoreClipControl();
|
2020-04-07 10:19:45 +01:00
|
|
|
((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
|
|
|
|
((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
|
Fix OpenGL issues with RTSS overlays and OBS Game Capture (#3217)
OpenGL game overlays and hooks tend to make a lot of assumptions about how games present frames to the screen, since presentation in OpenGL kind of sucks and they would like to have info such as the size of the screen, or if the contents are SRGB rather than linear.
There are two ways of getting this. OBS hooks swap buffers to get a frame for video capture, but it actually checks the bound framebuffer at the time. I made sure that this matches the output framebuffer (the window) so that the output matches the size. RTSS checks the viewport size by default, but this was actually set to the last used viewport by the game, causing the OSD to fly all across the screen depending on how it was used (or res scale). The viewport is now manually set to match the output framebuffer size.
In the case of RTSS, it also loads its resources by destructively setting a pixel pack parameter without regard to what it was set to by the guest application. OpenGL state can be set for a long period of time and is not expected to be set before each call to a method, so randomly changing it isn't great practice. To fix this, I've added a line to set the pixel unpack alignment back to 4 after presentation, which should cover RTSS loading its incredibly ugly font.
- RTSS and overlays that use it should no longer cause certain textures to load incorrectly. (mario kart 8, pokemon legends arceus)
- OBS Game Capture should no longer crop the game output incorrectly, flicker randomly, or capture with incorrect gamma.
This doesn't fix issues with how RTSS reports our frame timings.
2022-03-20 16:37:45 +00:00
|
|
|
((Pipeline)_renderer.Pipeline).RestoreViewport0();
|
2020-07-26 04:03:40 +01:00
|
|
|
|
|
|
|
if (viewConverted != view)
|
|
|
|
{
|
|
|
|
viewConverted.Dispose();
|
|
|
|
}
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private int GetCopyFramebufferHandleLazy()
|
|
|
|
{
|
|
|
|
int handle = _copyFramebufferHandle;
|
|
|
|
|
2019-11-01 00:32:43 +00:00
|
|
|
if (handle == 0)
|
|
|
|
{
|
|
|
|
handle = GL.GenFramebuffer();
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
_copyFramebufferHandle = handle;
|
2019-11-01 00:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
2019-12-31 22:09:49 +00:00
|
|
|
|
2021-04-14 11:28:43 +01:00
|
|
|
public void InitializeBackgroundContext(IOpenGLContext baseContext)
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 21:18:35 +01:00
|
|
|
{
|
|
|
|
BackgroundContext = new BackgroundContextWorker(baseContext);
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:09:43 +01:00
|
|
|
public void CaptureFrame(int x, int y, int width, int height, bool isBgra, bool flipX, bool flipY)
|
|
|
|
{
|
|
|
|
long size = Math.Abs(4 * width * height);
|
|
|
|
byte[] bitmap = new byte[size];
|
|
|
|
|
|
|
|
GL.ReadPixels(x, y, width, height, isBgra ? PixelFormat.Bgra : PixelFormat.Rgba, PixelType.UnsignedByte, bitmap);
|
2021-08-20 22:26:25 +01:00
|
|
|
|
2021-06-28 21:09:43 +01:00
|
|
|
_renderer.OnScreenCaptured(new ScreenCaptureImageInfo(width, height, isBgra, bitmap, flipX, flipY));
|
|
|
|
}
|
|
|
|
|
2019-12-31 22:09:49 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 21:18:35 +01:00
|
|
|
BackgroundContext.Dispose();
|
|
|
|
|
2019-12-31 22:09:49 +00:00
|
|
|
if (_copyFramebufferHandle != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteFramebuffer(_copyFramebufferHandle);
|
|
|
|
|
|
|
|
_copyFramebufferHandle = 0;
|
|
|
|
}
|
2022-05-15 12:30:15 +01:00
|
|
|
|
|
|
|
if (_stagingFrameBuffer != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteTextures(_stagingTextures.Length, _stagingTextures);
|
|
|
|
GL.DeleteFramebuffer(_stagingFrameBuffer);
|
|
|
|
_stagingFrameBuffer = 0;
|
|
|
|
_stagingTextures = null;
|
|
|
|
}
|
2019-12-31 22:09:49 +00:00
|
|
|
}
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
}
|