2020-08-29 21:42:34 +02:00
|
|
|
using Ryujinx.Common;
|
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 22:18:35 +02:00
|
|
|
using Ryujinx.Cpu.Tracking;
|
2019-10-26 19:50:52 +02:00
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2019-10-13 08:02:07 +02:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Image
|
|
|
|
{
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Represents a pool of GPU resources, such as samplers or textures.
|
|
|
|
/// </summary>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// <typeparam name="T">Type of the GPU resource</typeparam>
|
2019-10-13 08:02:07 +02:00
|
|
|
abstract class Pool<T> : IDisposable
|
|
|
|
{
|
|
|
|
protected const int DescriptorSize = 0x20;
|
|
|
|
|
|
|
|
protected GpuContext Context;
|
|
|
|
|
|
|
|
protected T[] Items;
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// The maximum ID value of resources on the pool (inclusive).
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// The maximum amount of resources on the pool is equal to this value plus one.
|
|
|
|
/// </remarks>
|
2019-12-07 00:19:12 +01:00
|
|
|
public int MaximumId { get; }
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// The address of the pool in guest memory.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
public ulong Address { get; }
|
2019-12-30 00:26:37 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The size of the pool in bytes.
|
|
|
|
/// </summary>
|
|
|
|
public ulong Size { get; }
|
2019-10-13 08:02:07 +02: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 22:18:35 +02:00
|
|
|
private readonly CpuMultiRegionHandle _memoryTracking;
|
2020-12-03 19:34:32 +01:00
|
|
|
private readonly Action<ulong, ulong> _modifiedDelegate;
|
2020-05-04 00:54:50 +02:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
public Pool(GpuContext context, ulong address, int maximumId)
|
|
|
|
{
|
2019-12-07 00:19:12 +01:00
|
|
|
Context = context;
|
|
|
|
MaximumId = maximumId;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
int count = maximumId + 1;
|
|
|
|
|
2020-12-03 19:34:32 +01:00
|
|
|
ulong size = (ulong)(uint)count * DescriptorSize;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
Items = new T[count];
|
|
|
|
|
|
|
|
Address = address;
|
|
|
|
Size = size;
|
2020-05-04 00:54:50 +02: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 22:18:35 +02:00
|
|
|
_memoryTracking = context.PhysicalMemory.BeginGranularTracking(address, size);
|
2020-12-03 19:34:32 +01:00
|
|
|
_modifiedDelegate = RegionModified;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the GPU resource with the given ID.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">ID of the resource. This is effectively a zero-based index</param>
|
|
|
|
/// <returns>The GPU resource with the given ID</returns>
|
2019-10-13 08:02:07 +02:00
|
|
|
public abstract T Get(int id);
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Synchronizes host memory with guest memory.
|
2020-01-02 00:14:18 +01:00
|
|
|
/// This causes invalidation of pool entries,
|
2019-12-30 00:26:37 +01:00
|
|
|
/// if a modification of entries by the CPU is detected.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void SynchronizeMemory()
|
|
|
|
{
|
2020-12-03 19:34:32 +01:00
|
|
|
_memoryTracking.QueryModified(_modifiedDelegate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Indicate that a region of the pool was modified, and must be loaded from memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mAddress">Start address of the modified region</param>
|
|
|
|
/// <param name="mSize">Size of the modified region</param>
|
|
|
|
private void RegionModified(ulong mAddress, ulong mSize)
|
|
|
|
{
|
|
|
|
if (mAddress < Address)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-12-03 19:34:32 +01:00
|
|
|
mAddress = Address;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-12-03 19:34:32 +01:00
|
|
|
ulong maxSize = Address + Size - mAddress;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-12-03 19:34:32 +01:00
|
|
|
if (mSize > maxSize)
|
|
|
|
{
|
|
|
|
mSize = maxSize;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-12-03 19:34:32 +01:00
|
|
|
InvalidateRangeImpl(mAddress, mSize);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void InvalidateRangeImpl(ulong address, ulong size);
|
|
|
|
|
|
|
|
protected abstract void Delete(T item);
|
|
|
|
|
2019-12-30 00:26:37 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Performs the disposal of all resources stored on the pool.
|
|
|
|
/// It's an error to try using the pool after disposal.
|
|
|
|
/// </summary>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (Items != null)
|
|
|
|
{
|
|
|
|
for (int index = 0; index < Items.Length; index++)
|
|
|
|
{
|
|
|
|
Delete(Items[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Items = null;
|
|
|
|
}
|
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 22:18:35 +02:00
|
|
|
_memoryTracking.Dispose();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|