Skip to content

Commit

Permalink
Merge branch 'Ryujinx:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
zandm7 authored Apr 30, 2022
2 parents ce6e0ed + d64594e commit 3d8b840
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
6 changes: 6 additions & 0 deletions Ryujinx.Graphics.Gpu/Image/TextureGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,12 @@ public void ClearModified(MultiRange range, TextureGroup ignore = null)
/// <param name="size">The size of the flushing memory access</param>
public void FlushAction(TextureGroupHandle handle, ulong address, ulong size)
{
// There is a small gap here where the action is removed but _actionRegistered is still 1.
// In this case it will skip registering the action, but here we are already handling it,
// so there shouldn't be any issue as it's the same handler for all actions.

handle.ClearActionRegistered();

if (!handle.Modified)
{
return;
Expand Down
35 changes: 25 additions & 10 deletions Ryujinx.Graphics.Gpu/Image/TextureGroupHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Ryujinx.Graphics.Gpu.Image
{
Expand Down Expand Up @@ -32,9 +33,9 @@ class TextureGroupHandle : IDisposable
private ulong _modifiedSync;

/// <summary>
/// Whether a tracking action is currently registered or not.
/// Whether a tracking action is currently registered or not. (0/1)
/// </summary>
private bool _actionRegistered;
private int _actionRegistered;

/// <summary>
/// Whether a sync action is currently registered or not.
Expand Down Expand Up @@ -171,11 +172,9 @@ private void RegisterSync(GpuContext context)
_syncActionRegistered = true;
}

if (!_actionRegistered)
if (Interlocked.Exchange(ref _actionRegistered, 1) == 0)
{
_group.RegisterAction(this);

_actionRegistered = true;
}
}

Expand Down Expand Up @@ -233,8 +232,6 @@ public void SynchronizeDependents()
/// <param name="context">The GPU context used to wait for sync</param>
public void Sync(GpuContext context)
{
_actionRegistered = false;

bool needsSync = !context.IsGpuThread();

if (needsSync)
Expand Down Expand Up @@ -263,21 +260,39 @@ public void Sync(GpuContext context)
}
}

/// <summary>
/// Clears the action registered variable, indicating that the tracking action should be
/// re-registered on the next modification.
/// </summary>
public void ClearActionRegistered()
{
Interlocked.Exchange(ref _actionRegistered, 0);
}

/// <summary>
/// Action to perform when a sync number is registered after modification.
/// This action will register a read tracking action on the memory tracking handle so that a flush from CPU can happen.
/// </summary>
private void SyncAction()
{
// The storage will need to signal modified again to update the sync number in future.
_group.Storage.SignalModifiedDirty();

lock (Overlaps)
{
foreach (Texture texture in Overlaps)
{
texture.SignalModifiedDirty();
}
}

// Register region tracking for CPU? (again)
_registeredSync = _modifiedSync;
_syncActionRegistered = false;

if (!_actionRegistered)
if (Interlocked.Exchange(ref _actionRegistered, 1) == 0)
{
_group.RegisterAction(this);

_actionRegistered = true;
}
}

Expand Down
7 changes: 3 additions & 4 deletions Ryujinx.Memory/Tracking/RegionHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ internal void Signal(ulong address, ulong size, bool write, ref IList<RegionHand
{
lock (_preActionLock)
{
_preAction?.Invoke(address, size);
RegionSignal action = Interlocked.Exchange(ref _preAction, null);

_preAction = null;
action?.Invoke(address, size);
}
}
finally
Expand Down Expand Up @@ -252,8 +252,7 @@ public void RegisterAction(RegionSignal action)

lock (_preActionLock)
{
RegionSignal lastAction = _preAction;
_preAction = action;
RegionSignal lastAction = Interlocked.Exchange(ref _preAction, action);

if (lastAction == null && action != lastAction)
{
Expand Down

0 comments on commit 3d8b840

Please sign in to comment.