Compare commits

...

12 Commits

Author SHA1 Message Date
gdkchan
efb135b74c Clear CPU side data on GPU buffer clears (#4125)
* Clear CPU side data on GPU buffer clears

* Implement tracked fill operation that can signal other resource types except buffer

* Fix tests, add missing XML doc

* PR feedback
2023-02-16 18:28:49 -03:00
gdkchan
a707842e14 Validate dimensions before creating texture (#4430) 2023-02-16 11:16:31 -03:00
TSRBerry
a5a9b9bc8b GUI: Small Updater refactor & Set correct permissions on Linux when extracting files (#4315)
* ava: Refactor Updater.cs

Fix typos
Remove unused usings
Rename variables to follow naming scheme

* ava: Set file permissions when extracting update files

* gtk: Apply the same refactor to Updater.cs

* updater: Replace assert with if statement

* updater: Remove await usings again
2023-02-15 22:36:35 +00:00
Mary
17078ad929 vulkan: Respect VK_KHR_portability_subset vertex stride alignment (#4419)
* vulkan: Respect VK_KHR_portability_subset vertex stride alignment

We were hardcoding alignment to 4, but by specs it can be any values that
is a power of 2.

This also enable VK_KHR_portability_subset if present as per specs
requirements.

* address gdkchan's comment

* Make NeedsVertexBufferAlignment internal
2023-02-15 08:41:48 +00:00
Mary
32450d45de vulkan: Clean up MemoryAllocator (#4418)
This started as an attempt to remove vkGetPhysicalDeviceMemoryProperties
in FindSuitableMemoryTypeIndex (As this could have some overhead and
shouldn't change at runtime) and turned in a little bigger cleanup.
2023-02-15 07:50:26 +01:00
Ac_K
ed7a0474c6 Infra: Issues template cleanup (#4421)
* Infra: Issues template cleanup

* applied
2023-02-14 15:58:57 +01:00
Mary
fe9c49949a vulkan: Enforce Vulkan 1.2+ at instance API level and 1.1+ at device level (#4408)
* vulkan: Enforce Vulkan 1.2+ at instance API level and 1.1+ at device level

This ensure we don't end up trying to initialize with anything currently incompatible.

* Address riperiperi's comment
2023-02-13 22:04:55 +00:00
Mary
052b23c83c vulkan: Do not call vkCmdSetViewport when viewportCount is 0 (#4406)
This fix validation error "VUID-vkCmdSetViewport-viewportCount-arraylength".
2023-02-13 20:32:20 +00:00
riperiperi
e4f68592c3 Fix partial updates for textures. (#4401)
I was forcing some types of texture to partially update when investigating performance with games that stream in data, and noticed that partially loading texture data was really broken on both backends.

Fixes Vulkan texture set by getting the correct expected size for the texture. Fixes partial upload on both backends for both Texture 2D Array and Cubemap using the wrong offset and uploading to the first layer/level for a handle. 3D might also be affected.

This might fix textures randomly having incorrect data in games that render to it - jumbled in the case of OpenGL, and outdated/black in the case of Vulkan. This case typically happens in UE4 games.
2023-02-12 10:30:26 +01:00
Logan Stromberg
1dcd44b94f Treat NpadIdType < 0 as invalid. Filter invalid SupportedPlayers inside IHidServer.SetSupportedNpadIdType(). (#4377)
Co-authored-by: Logan Stromberg <lostromb@microsoft.com>
2023-02-10 12:37:20 -03:00
gdkchan
61b1ce252f Allow partially mapped textures with unmapped start (#4394) 2023-02-10 11:47:59 -03:00
gdkchan
5f38086f94 Fix SPIR-V when all inputs/outputs are indexed (#4389) 2023-02-09 04:48:25 +01:00
46 changed files with 628 additions and 497 deletions

View File

@@ -1,26 +1,27 @@
name: Bug Report name: Bug Report
description: File a bug report description: File a bug report
title: "[Bug] <title>" title: "[Bug]"
labels: bug labels: bug
body: body:
- type: textarea - type: textarea
id: issue id: issue
attributes: attributes:
label: Description of Issue label: Description of the issue
description: What's the issue you encountered? description: What's the issue you encountered?
validations: validations:
required: true required: true
- type: textarea - type: textarea
id: repro id: repro
attributes: attributes:
label: Reproduction Steps label: Reproduction steps
description: How can the issue be reproduced? description: How can the issue be reproduced?
placeholder: Describe each step as precisely as possible
validations: validations:
required: true required: true
- type: textarea - type: textarea
id: log id: log
attributes: attributes:
label: Log File label: Log file
description: A log file will help our developers to better diagnose and fix the issue. description: A log file will help our developers to better diagnose and fix the issue.
placeholder: Logs files can be found under "Logs" folder in Ryujinx program folder. You can drag and drop the log on to the text area placeholder: Logs files can be found under "Logs" folder in Ryujinx program folder. You can drag and drop the log on to the text area
validations: validations:
@@ -29,55 +30,44 @@ body:
id: os id: os
attributes: attributes:
label: OS label: OS
placeholder: "Example: Windows 10" placeholder: "e.g. Windows 10"
validations: validations:
required: true required: true
- type: input - type: input
id: ryujinx-version id: ryujinx-version
attributes: attributes:
label: Ryujinx version label: Ryujinx version
placeholder: | placeholder: "e.g. 1.0.470"
- *(e.g. 1.0.470)*
validations: validations:
required: true required: true
- type: input - type: input
id: game-version id: game-version
attributes: attributes:
label: Game version label: Game version
placeholder: | placeholder: "e.g. 1.1.1"
- *(e.g. 1.1.1)*
validations: validations:
required: false required: false
- type: input - type: input
id: cpu id: cpu
attributes: attributes:
label: CPU label: CPU
placeholder: | placeholder: "e.g. i7-6700"
- *(e.g. i7-6700)*
validations: validations:
required: false required: false
- type: input - type: input
id: gpu id: gpu
attributes: attributes:
label: GPU label: GPU
placeholder: | placeholder: "e.g. NVIDIA RTX 2070"
- *(e.g. NVIDIA RTX 2070)*
validations: validations:
required: false required: false
- type: input - type: input
id: ram id: ram
attributes: attributes:
label: RAM label: RAM
placeholder: | placeholder: "e.g. 16GB"
- *(e.g. 16GB)*
validations: validations:
required: false required: false
- type: checkboxes
attributes:
label: Applied Mods?
options:
- label: "Yes"
required: false
- type: textarea - type: textarea
id: mods id: mods
attributes: attributes:
@@ -93,4 +83,4 @@ body:
- Additional info about your environment: - Additional info about your environment:
- Any other information relevant to your issue. - Any other information relevant to your issue.
validations: validations:
required: false required: false

View File

@@ -1,6 +1,6 @@
name: Feature Request name: Feature Request
description: Suggest a new feature for Ryujinx. description: Suggest a new feature for Ryujinx.
title: "[Feature Request] <title>" title: "[Feature Request]"
body: body:
- type: textarea - type: textarea
id: overview id: overview
@@ -12,14 +12,14 @@ body:
- type: textarea - type: textarea
id: details id: details
attributes: attributes:
label: Smaller Details label: Smaller details
description: These may include specific methods of implementation etc. description: These may include specific methods of implementation etc.
validations: validations:
required: true required: true
- type: textarea - type: textarea
id: request id: request
attributes: attributes:
label: Nature of Request label: Nature of request
validations: validations:
required: true required: true
- type: textarea - type: textarea

View File

@@ -1,6 +1,6 @@
name: Missing CPU Instruction name: Missing CPU Instruction
description: CPU Instruction is missing in Ryujinx. description: CPU Instruction is missing in Ryujinx.
title: "[CPU] <title>" title: "[CPU]"
labels: [cpu, not-implemented] labels: [cpu, not-implemented]
body: body:
- type: textarea - type: textarea

View File

@@ -5,7 +5,7 @@ body:
- type: textarea - type: textarea
id: instruction id: instruction
attributes: attributes:
label: Service Call label: Service call
description: What service call is missing? description: What service call is missing?
validations: validations:
required: true required: true

View File

@@ -71,6 +71,7 @@ namespace ARMeilleure.Memory
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="write">True if the region was written, false if read</param> /// <param name="write">True if the region was written, false if read</param>
/// <param name="precise">True if the access is precise, false otherwise</param> /// <param name="precise">True if the access is precise, false otherwise</param>
void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false); /// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null);
} }
} }

View File

@@ -222,7 +222,7 @@ namespace ARMeilleure.Signal
// Tracking action should be non-null to call it, otherwise assume false return. // Tracking action should be non-null to call it, otherwise assume false return.
context.BranchIfFalse(skipActionLabel, trackingActionPtr); context.BranchIfFalse(skipActionLabel, trackingActionPtr);
Operand result = context.Call(trackingActionPtr, OperandType.I32, offset, Const(_pageSize), isWrite, Const(0)); Operand result = context.Call(trackingActionPtr, OperandType.I32, offset, Const(_pageSize), isWrite);
context.Copy(inRegionLocal, result); context.Copy(inRegionLocal, result);
context.MarkLabel(skipActionLabel); context.MarkLabel(skipActionLabel);

View File

@@ -132,8 +132,8 @@ namespace Ryujinx.Modules
} }
} }
// If build not done, assume no new update are availaible. // If build not done, assume no new update are available.
if (_buildUrl == null) if (_buildUrl is null)
{ {
if (showVersionUpToDate) if (showVersionUpToDate)
{ {
@@ -240,13 +240,13 @@ namespace Ryujinx.Modules
{ {
HttpClient result = new(); HttpClient result = new();
// Required by GitHub to interract with APIs. // Required by GitHub to interact with APIs.
result.DefaultRequestHeaders.Add("User-Agent", "Ryujinx-Updater/1.0.0"); result.DefaultRequestHeaders.Add("User-Agent", "Ryujinx-Updater/1.0.0");
return result; return result;
} }
public static async void UpdateRyujinx(Window parent, string downloadUrl) private static async void UpdateRyujinx(Window parent, string downloadUrl)
{ {
_updateSuccessful = false; _updateSuccessful = false;
@@ -300,8 +300,6 @@ namespace Ryujinx.Modules
ryuExe = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, OperatingSystem.IsWindows() ? "Ryujinx.exe" : "Ryujinx"); ryuExe = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, OperatingSystem.IsWindows() ? "Ryujinx.exe" : "Ryujinx");
} }
SetFileExecutable(ryuExe);
Process.Start(ryuExe, CommandLineState.Arguments); Process.Start(ryuExe, CommandLineState.Arguments);
Environment.Exit(0); Environment.Exit(0);
@@ -408,9 +406,9 @@ namespace Ryujinx.Modules
Logger.Warning?.Print(LogClass.Application, ex.Message); Logger.Warning?.Print(LogClass.Application, ex.Message);
Logger.Warning?.Print(LogClass.Application, "Multi-Threaded update failed, falling back to single-threaded updater."); Logger.Warning?.Print(LogClass.Application, "Multi-Threaded update failed, falling back to single-threaded updater.");
for (int j = 0; j < webClients.Count; j++) foreach (WebClient webClient in webClients)
{ {
webClients[j].CancelAsync(); webClient.CancelAsync();
} }
DoUpdateWithSingleThread(taskDialog, downloadUrl, updateFile); DoUpdateWithSingleThread(taskDialog, downloadUrl, updateFile);
@@ -472,22 +470,6 @@ namespace Ryujinx.Modules
worker.Start(); worker.Start();
} }
private static void SetFileExecutable(string path)
{
const UnixFileMode ExecutableFileMode = UnixFileMode.UserExecute |
UnixFileMode.UserWrite |
UnixFileMode.UserRead |
UnixFileMode.GroupRead |
UnixFileMode.GroupWrite |
UnixFileMode.OtherRead |
UnixFileMode.OtherWrite;
if (!OperatingSystem.IsWindows() && File.Exists(path))
{
File.SetUnixFileMode(path, ExecutableFileMode);
}
}
private static async void InstallUpdate(TaskDialog taskDialog, string updateFile) private static async void InstallUpdate(TaskDialog taskDialog, string updateFile)
{ {
// Extract Update // Extract Update
@@ -503,27 +485,30 @@ namespace Ryujinx.Modules
await Task.Run(() => await Task.Run(() =>
{ {
TarEntry tarEntry; TarEntry tarEntry;
while ((tarEntry = tarStream.GetNextEntry()) != null)
if (!OperatingSystem.IsWindows())
{ {
if (tarEntry.IsDirectory) continue; while ((tarEntry = tarStream.GetNextEntry()) is not null)
string outPath = Path.Combine(UpdateDir, tarEntry.Name);
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
using (FileStream outStream = File.OpenWrite(outPath))
{ {
tarStream.CopyEntryContents(outStream); if (tarEntry.IsDirectory) continue;
string outPath = Path.Combine(UpdateDir, tarEntry.Name);
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
using (FileStream outStream = File.OpenWrite(outPath))
{
tarStream.CopyEntryContents(outStream);
}
File.SetUnixFileMode(outPath, (UnixFileMode)tarEntry.TarHeader.Mode);
File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));
Dispatcher.UIThread.Post(() =>
{
taskDialog.SetProgressBarState(GetPercentage(tarEntry.Size, inStream.Length), TaskDialogProgressState.Normal);
});
} }
File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));
TarEntry entry = tarEntry;
Dispatcher.UIThread.Post(() =>
{
taskDialog.SetProgressBarState(GetPercentage(entry.Size, inStream.Length), TaskDialogProgressState.Normal);
});
} }
}); });
@@ -603,8 +588,6 @@ namespace Ryujinx.Modules
Directory.Delete(UpdateDir, true); Directory.Delete(UpdateDir, true);
SetFileExecutable(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ryujinx"));
_updateSuccessful = true; _updateSuccessful = true;
taskDialog.Hide(); taskDialog.Hide();

View File

@@ -634,13 +634,13 @@ namespace Ryujinx.Cpu.AppleHv
/// <remarks> /// <remarks>
/// This function also validates that the given range is both valid and mapped, and will throw if it is not. /// This function also validates that the given range is both valid and mapped, and will throw if it is not.
/// </remarks> /// </remarks>
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false) public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
{ {
AssertValidAddressAndSize(va, size); AssertValidAddressAndSize(va, size);
if (precise) if (precise)
{ {
Tracking.VirtualMemoryEvent(va, size, write, precise: true); Tracking.VirtualMemoryEvent(va, size, write, precise: true, exemptId);
return; return;
} }
@@ -663,7 +663,7 @@ namespace Ryujinx.Cpu.AppleHv
if (state >= tag) if (state >= tag)
{ {
Tracking.VirtualMemoryEvent(va, size, write); Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
return; return;
} }
else if (state == 0) else if (state == 0)
@@ -706,7 +706,7 @@ namespace Ryujinx.Cpu.AppleHv
// Only trigger tracking from reads if both bits are set on any page. // Only trigger tracking from reads if both bits are set on any page.
if (write || (pte & (pte >> 1) & BlockMappedMask) != 0) if (write || (pte & (pte >> 1) & BlockMappedMask) != 0)
{ {
Tracking.VirtualMemoryEvent(va, size, write); Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
break; break;
} }
} }
@@ -822,21 +822,21 @@ namespace Ryujinx.Cpu.AppleHv
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuRegionHandle BeginTracking(ulong address, ulong size) public CpuRegionHandle BeginTracking(ulong address, ulong size, int id)
{ {
return new CpuRegionHandle(Tracking.BeginTracking(address, size)); return new CpuRegionHandle(Tracking.BeginTracking(address, size, id));
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity) public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
{ {
return new CpuMultiRegionHandle(Tracking.BeginGranularTracking(address, size, handles, granularity)); return new CpuMultiRegionHandle(Tracking.BeginGranularTracking(address, size, handles, granularity, id));
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity) public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity, int id)
{ {
return new CpuSmartMultiRegionHandle(Tracking.BeginSmartGranularTracking(address, size, granularity)); return new CpuSmartMultiRegionHandle(Tracking.BeginSmartGranularTracking(address, size, granularity, id));
} }
/// <summary> /// <summary>

View File

@@ -28,8 +28,9 @@ namespace Ryujinx.Cpu
/// </summary> /// </summary>
/// <param name="address">CPU virtual address of the region</param> /// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="id">Handle ID</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
CpuRegionHandle BeginTracking(ulong address, ulong size); CpuRegionHandle BeginTracking(ulong address, ulong size, int id);
/// <summary> /// <summary>
/// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with. /// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
@@ -38,8 +39,9 @@ namespace Ryujinx.Cpu
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="handles">Handles to inherit state from or reuse. When none are present, provide null</param> /// <param name="handles">Handles to inherit state from or reuse. When none are present, provide null</param>
/// <param name="granularity">Desired granularity of write tracking</param> /// <param name="granularity">Desired granularity of write tracking</param>
/// <param name="id">Handle ID</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity); CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id);
/// <summary> /// <summary>
/// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with. /// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
@@ -47,7 +49,8 @@ namespace Ryujinx.Cpu
/// <param name="address">CPU virtual address of the region</param> /// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="granularity">Desired granularity of write tracking</param> /// <param name="granularity">Desired granularity of write tracking</param>
/// <param name="id">Handle ID</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity); CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity, int id);
} }
} }

View File

@@ -629,31 +629,31 @@ namespace Ryujinx.Cpu.Jit
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuRegionHandle BeginTracking(ulong address, ulong size) public CpuRegionHandle BeginTracking(ulong address, ulong size, int id)
{ {
return new CpuRegionHandle(Tracking.BeginTracking(address, size)); return new CpuRegionHandle(Tracking.BeginTracking(address, size, id));
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity) public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
{ {
return new CpuMultiRegionHandle(Tracking.BeginGranularTracking(address, size, handles, granularity)); return new CpuMultiRegionHandle(Tracking.BeginGranularTracking(address, size, handles, granularity, id));
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity) public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity, int id)
{ {
return new CpuSmartMultiRegionHandle(Tracking.BeginSmartGranularTracking(address, size, granularity)); return new CpuSmartMultiRegionHandle(Tracking.BeginSmartGranularTracking(address, size, granularity, id));
} }
/// <inheritdoc/> /// <inheritdoc/>
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false) public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
{ {
AssertValidAddressAndSize(va, size); AssertValidAddressAndSize(va, size);
if (precise) if (precise)
{ {
Tracking.VirtualMemoryEvent(va, size, write, precise: true); Tracking.VirtualMemoryEvent(va, size, write, precise: true, exemptId);
return; return;
} }
@@ -676,7 +676,7 @@ namespace Ryujinx.Cpu.Jit
if ((pte & tag) != 0) if ((pte & tag) != 0)
{ {
Tracking.VirtualMemoryEvent(va, size, write); Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
break; break;
} }

View File

@@ -518,13 +518,13 @@ namespace Ryujinx.Cpu.Jit
/// <remarks> /// <remarks>
/// This function also validates that the given range is both valid and mapped, and will throw if it is not. /// This function also validates that the given range is both valid and mapped, and will throw if it is not.
/// </remarks> /// </remarks>
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false) public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
{ {
AssertValidAddressAndSize(va, size); AssertValidAddressAndSize(va, size);
if (precise) if (precise)
{ {
Tracking.VirtualMemoryEvent(va, size, write, precise: true); Tracking.VirtualMemoryEvent(va, size, write, precise: true, exemptId);
return; return;
} }
@@ -547,7 +547,7 @@ namespace Ryujinx.Cpu.Jit
if (state >= tag) if (state >= tag)
{ {
Tracking.VirtualMemoryEvent(va, size, write); Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
return; return;
} }
else if (state == 0) else if (state == 0)
@@ -590,7 +590,7 @@ namespace Ryujinx.Cpu.Jit
// Only trigger tracking from reads if both bits are set on any page. // Only trigger tracking from reads if both bits are set on any page.
if (write || (pte & (pte >> 1) & BlockMappedMask) != 0) if (write || (pte & (pte >> 1) & BlockMappedMask) != 0)
{ {
Tracking.VirtualMemoryEvent(va, size, write); Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
break; break;
} }
} }
@@ -706,21 +706,21 @@ namespace Ryujinx.Cpu.Jit
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuRegionHandle BeginTracking(ulong address, ulong size) public CpuRegionHandle BeginTracking(ulong address, ulong size, int id)
{ {
return new CpuRegionHandle(Tracking.BeginTracking(address, size)); return new CpuRegionHandle(Tracking.BeginTracking(address, size, id));
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity) public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
{ {
return new CpuMultiRegionHandle(Tracking.BeginGranularTracking(address, size, handles, granularity)); return new CpuMultiRegionHandle(Tracking.BeginGranularTracking(address, size, handles, granularity, id));
} }
/// <inheritdoc/> /// <inheritdoc/>
public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity) public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity, int id)
{ {
return new CpuSmartMultiRegionHandle(Tracking.BeginSmartGranularTracking(address, size, granularity)); return new CpuSmartMultiRegionHandle(Tracking.BeginSmartGranularTracking(address, size, granularity, id));
} }
/// <summary> /// <summary>

View File

@@ -8,7 +8,7 @@ namespace Ryujinx.Cpu
{ {
public class MemoryEhMeilleure : IDisposable public class MemoryEhMeilleure : IDisposable
{ {
private delegate bool TrackingEventDelegate(ulong address, ulong size, bool write, bool precise = false); private delegate bool TrackingEventDelegate(ulong address, ulong size, bool write);
private readonly MemoryTracking _tracking; private readonly MemoryTracking _tracking;
private readonly TrackingEventDelegate _trackingEvent; private readonly TrackingEventDelegate _trackingEvent;

View File

@@ -69,7 +69,7 @@ namespace Ryujinx.Graphics.Gpu.Image
Address = address; Address = address;
Size = size; Size = size;
_memoryTracking = physicalMemory.BeginGranularTracking(address, size); _memoryTracking = physicalMemory.BeginGranularTracking(address, size, ResourceKind.Pool);
_memoryTracking.RegisterPreciseAction(address, size, PreciseAction); _memoryTracking.RegisterPreciseAction(address, size, PreciseAction);
_modifiedDelegate = RegionModified; _modifiedDelegate = RegionModified;
} }

View File

@@ -474,6 +474,29 @@ namespace Ryujinx.Graphics.Gpu.Image
{ {
address = memoryManager.Translate(info.GpuAddress); address = memoryManager.Translate(info.GpuAddress);
// If the start address is unmapped, let's try to find a page of memory that is mapped.
if (address == MemoryManager.PteUnmapped)
{
// Make sure that the dimensions are valid before calculating the texture size.
if (info.Width < 1 || info.Height < 1 || info.Levels < 1)
{
return null;
}
if ((info.Target == Target.Texture3D ||
info.Target == Target.Texture2DArray ||
info.Target == Target.Texture2DMultisampleArray ||
info.Target == Target.CubemapArray) && info.DepthOrLayers < 1)
{
return null;
}
ulong dataSize = (ulong)info.CalculateSizeInfo(layerSize).TotalSize;
address = memoryManager.TranslateFirstMapped(info.GpuAddress, dataSize);
}
// If address is still invalid, the texture is fully unmapped, so it has no data, just return null.
if (address == MemoryManager.PteUnmapped) if (address == MemoryManager.PteUnmapped)
{ {
return null; return null;

View File

@@ -336,24 +336,23 @@ namespace Ryujinx.Graphics.Gpu.Image
if (_loadNeeded[baseHandle + i]) if (_loadNeeded[baseHandle + i])
{ {
var info = GetHandleInformation(baseHandle + i); var info = GetHandleInformation(baseHandle + i);
int offsetIndex = info.Index;
// Only one of these will be greater than 1, as partial sync is only called when there are sub-image views. // Only one of these will be greater than 1, as partial sync is only called when there are sub-image views.
for (int layer = 0; layer < info.Layers; layer++) for (int layer = 0; layer < info.Layers; layer++)
{ {
for (int level = 0; level < info.Levels; level++) for (int level = 0; level < info.Levels; level++)
{ {
int offsetIndex = GetOffsetIndex(info.BaseLayer + layer, info.BaseLevel + level);
int offset = _allOffsets[offsetIndex]; int offset = _allOffsets[offsetIndex];
int endOffset = Math.Min(offset + _sliceSizes[info.BaseLevel + level], (int)Storage.Size); int endOffset = Math.Min(offset + _sliceSizes[info.BaseLevel + level], (int)Storage.Size);
int size = endOffset - offset; int size = endOffset - offset;
ReadOnlySpan<byte> data = _physicalMemory.GetSpan(Storage.Range.GetSlice((ulong)offset, (ulong)size)); ReadOnlySpan<byte> data = _physicalMemory.GetSpan(Storage.Range.GetSlice((ulong)offset, (ulong)size));
SpanOrArray<byte> result = Storage.ConvertToHostCompatibleFormat(data, info.BaseLevel, true); SpanOrArray<byte> result = Storage.ConvertToHostCompatibleFormat(data, info.BaseLevel + level, true);
Storage.SetData(result, info.BaseLayer, info.BaseLevel); Storage.SetData(result, info.BaseLayer + layer, info.BaseLevel + level);
offsetIndex++;
} }
} }
} }
@@ -855,7 +854,7 @@ namespace Ryujinx.Graphics.Gpu.Image
/// <returns>A CpuRegionHandle covering the given range</returns> /// <returns>A CpuRegionHandle covering the given range</returns>
private CpuRegionHandle GenerateHandle(ulong address, ulong size) private CpuRegionHandle GenerateHandle(ulong address, ulong size)
{ {
return _physicalMemory.BeginTracking(address, size); return _physicalMemory.BeginTracking(address, size, ResourceKind.Texture);
} }
/// <summary> /// <summary>

View File

@@ -105,13 +105,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
if (_useGranular) if (_useGranular)
{ {
_memoryTrackingGranular = physicalMemory.BeginGranularTracking(address, size, baseHandles); _memoryTrackingGranular = physicalMemory.BeginGranularTracking(address, size, ResourceKind.Buffer, baseHandles);
_memoryTrackingGranular.RegisterPreciseAction(address, size, PreciseAction); _memoryTrackingGranular.RegisterPreciseAction(address, size, PreciseAction);
} }
else else
{ {
_memoryTracking = physicalMemory.BeginTracking(address, size); _memoryTracking = physicalMemory.BeginTracking(address, size, ResourceKind.Buffer);
if (baseHandles != null) if (baseHandles != null)
{ {

View File

@@ -368,7 +368,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
_context.Renderer.Pipeline.ClearBuffer(buffer.Handle, offset, (int)size, value); _context.Renderer.Pipeline.ClearBuffer(buffer.Handle, offset, (int)size, value);
buffer.SignalModified(address, size); memoryManager.Physical.FillTrackedResource(address, size, value, ResourceKind.Buffer);
} }
/// <summary> /// <summary>

View File

@@ -583,6 +583,38 @@ namespace Ryujinx.Graphics.Gpu.Memory
return UnpackPaFromPte(pte) + (va & PageMask); return UnpackPaFromPte(pte) + (va & PageMask);
} }
/// <summary>
/// Translates a GPU virtual address to a CPU virtual address on the first mapped page of memory
/// on the specified region.
/// If no page is mapped on the specified region, <see cref="PteUnmapped"/> is returned.
/// </summary>
/// <param name="va">GPU virtual address to be translated</param>
/// <param name="size">Size of the range to be translated</param>
/// <returns>CPU virtual address, or <see cref="PteUnmapped"/> if unmapped</returns>
public ulong TranslateFirstMapped(ulong va, ulong size)
{
if (!ValidateAddress(va))
{
return PteUnmapped;
}
ulong endVa = va + size;
ulong pte = GetPte(va);
for (; va < endVa && pte == PteUnmapped; va += PageSize - (va & PageMask))
{
pte = GetPte(va);
}
if (pte == PteUnmapped)
{
return PteUnmapped;
}
return UnpackPaFromPte(pte) + (va & PageMask);
}
/// <summary> /// <summary>
/// Gets the kind of a given memory page. /// Gets the kind of a given memory page.
/// This might indicate the type of resource that can be allocated on the page, and also texture tiling. /// This might indicate the type of resource that can be allocated on the page, and also texture tiling.

View File

@@ -7,6 +7,7 @@ using Ryujinx.Memory.Range;
using Ryujinx.Memory.Tracking; using Ryujinx.Memory.Tracking;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
namespace Ryujinx.Graphics.Gpu.Memory namespace Ryujinx.Graphics.Gpu.Memory
@@ -295,23 +296,41 @@ namespace Ryujinx.Graphics.Gpu.Memory
} }
} }
/// <summary>
/// Fills the specified memory region with a 32-bit integer value.
/// </summary>
/// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size of the region</param>
/// <param name="value">Value to fill the region with</param>
/// <param name="kind">Kind of the resource being filled, which will not be signalled as CPU modified</param>
public void FillTrackedResource(ulong address, ulong size, uint value, ResourceKind kind)
{
_cpuMemory.SignalMemoryTracking(address, size, write: true, precise: true, (int)kind);
using WritableRegion region = _cpuMemory.GetWritableRegion(address, (int)size);
MemoryMarshal.Cast<byte, uint>(region.Memory.Span).Fill(value);
}
/// <summary> /// <summary>
/// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with. /// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
/// </summary> /// </summary>
/// <param name="address">CPU virtual address of the region</param> /// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="kind">Kind of the resource being tracked</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
public CpuRegionHandle BeginTracking(ulong address, ulong size) public CpuRegionHandle BeginTracking(ulong address, ulong size, ResourceKind kind)
{ {
return _cpuMemory.BeginTracking(address, size); return _cpuMemory.BeginTracking(address, size, (int)kind);
} }
/// <summary> /// <summary>
/// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with. /// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
/// </summary> /// </summary>
/// <param name="range">Ranges of physical memory where the data is located</param> /// <param name="range">Ranges of physical memory where the data is located</param>
/// <param name="kind">Kind of the resource being tracked</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
public GpuRegionHandle BeginTracking(MultiRange range) public GpuRegionHandle BeginTracking(MultiRange range, ResourceKind kind)
{ {
var cpuRegionHandles = new CpuRegionHandle[range.Count]; var cpuRegionHandles = new CpuRegionHandle[range.Count];
int count = 0; int count = 0;
@@ -321,7 +340,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
var currentRange = range.GetSubRange(i); var currentRange = range.GetSubRange(i);
if (currentRange.Address != MemoryManager.PteUnmapped) if (currentRange.Address != MemoryManager.PteUnmapped)
{ {
cpuRegionHandles[count++] = _cpuMemory.BeginTracking(currentRange.Address, currentRange.Size); cpuRegionHandles[count++] = _cpuMemory.BeginTracking(currentRange.Address, currentRange.Size, (int)kind);
} }
} }
@@ -338,12 +357,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// </summary> /// </summary>
/// <param name="address">CPU virtual address of the region</param> /// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="kind">Kind of the resource being tracked</param>
/// <param name="handles">Handles to inherit state from or reuse</param> /// <param name="handles">Handles to inherit state from or reuse</param>
/// <param name="granularity">Desired granularity of write tracking</param> /// <param name="granularity">Desired granularity of write tracking</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles = null, ulong granularity = 4096) public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, ResourceKind kind, IEnumerable<IRegionHandle> handles = null, ulong granularity = 4096)
{ {
return _cpuMemory.BeginGranularTracking(address, size, handles, granularity); return _cpuMemory.BeginGranularTracking(address, size, handles, granularity, (int)kind);
} }
/// <summary> /// <summary>
@@ -351,11 +371,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// </summary> /// </summary>
/// <param name="address">CPU virtual address of the region</param> /// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="kind">Kind of the resource being tracked</param>
/// <param name="granularity">Desired granularity of write tracking</param> /// <param name="granularity">Desired granularity of write tracking</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity = 4096) public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ResourceKind kind, ulong granularity = 4096)
{ {
return _cpuMemory.BeginSmartGranularTracking(address, size, granularity); return _cpuMemory.BeginSmartGranularTracking(address, size, granularity, (int)kind);
} }
/// <summary> /// <summary>

View File

@@ -0,0 +1,13 @@
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// Kind of a GPU resource.
/// </summary>
enum ResourceKind
{
None,
Buffer,
Texture,
Pool
}
}

View File

@@ -397,6 +397,31 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info, bool perPatch) private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info, bool perPatch)
{ {
bool iaIndexing = context.Config.UsedFeatures.HasFlag(FeatureFlags.IaIndexing); bool iaIndexing = context.Config.UsedFeatures.HasFlag(FeatureFlags.IaIndexing);
if (iaIndexing && !perPatch)
{
var attrType = context.TypeVector(context.TypeFP32(), (LiteralInteger)4);
attrType = context.TypeArray(attrType, context.Constant(context.TypeU32(), (LiteralInteger)MaxAttributes));
if (context.Config.Stage == ShaderStage.Geometry)
{
attrType = context.TypeArray(attrType, context.Constant(context.TypeU32(), (LiteralInteger)context.InputVertices));
}
var spvType = context.TypePointer(StorageClass.Input, attrType);
var spvVar = context.Variable(spvType, StorageClass.Input);
if (context.Config.PassthroughAttributes != 0 && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough())
{
context.Decorate(spvVar, Decoration.PassthroughNV);
}
context.Decorate(spvVar, Decoration.Location, (LiteralInteger)0);
context.AddGlobalVariable(spvVar);
context.InputsArray = spvVar;
}
var inputs = perPatch ? info.InputsPerPatch : info.Inputs; var inputs = perPatch ? info.InputsPerPatch : info.Inputs;
foreach (int attr in inputs) foreach (int attr in inputs)
@@ -410,60 +435,56 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
if (iaIndexing && isUserAttr && !perPatch) if (iaIndexing && isUserAttr && !perPatch)
{ {
if (context.InputsArray == null) continue;
{
var attrType = context.TypeVector(context.TypeFP32(), (LiteralInteger)4);
attrType = context.TypeArray(attrType, context.Constant(context.TypeU32(), (LiteralInteger)MaxAttributes));
if (context.Config.Stage == ShaderStage.Geometry)
{
attrType = context.TypeArray(attrType, context.Constant(context.TypeU32(), (LiteralInteger)context.InputVertices));
}
var spvType = context.TypePointer(StorageClass.Input, attrType);
var spvVar = context.Variable(spvType, StorageClass.Input);
if (context.Config.PassthroughAttributes != 0 && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough())
{
context.Decorate(spvVar, Decoration.PassthroughNV);
}
context.Decorate(spvVar, Decoration.Location, (LiteralInteger)0);
context.AddGlobalVariable(spvVar);
context.InputsArray = spvVar;
}
} }
else
PixelImap iq = PixelImap.Unused;
if (context.Config.Stage == ShaderStage.Fragment)
{ {
PixelImap iq = PixelImap.Unused; if (attr >= AttributeConsts.UserAttributeBase && attr < AttributeConsts.UserAttributeEnd)
if (context.Config.Stage == ShaderStage.Fragment)
{ {
if (attr >= AttributeConsts.UserAttributeBase && attr < AttributeConsts.UserAttributeEnd) iq = context.Config.ImapTypes[(attr - AttributeConsts.UserAttributeBase) / 16].GetFirstUsedType();
{ }
iq = context.Config.ImapTypes[(attr - AttributeConsts.UserAttributeBase) / 16].GetFirstUsedType(); else
} {
else AttributeInfo attrInfo = AttributeInfo.From(context.Config, attr, isOutAttr: false);
{ AggregateType elemType = attrInfo.Type & AggregateType.ElementTypeMask;
AttributeInfo attrInfo = AttributeInfo.From(context.Config, attr, isOutAttr: false);
AggregateType elemType = attrInfo.Type & AggregateType.ElementTypeMask;
if (attrInfo.IsBuiltin && (elemType == AggregateType.S32 || elemType == AggregateType.U32)) if (attrInfo.IsBuiltin && (elemType == AggregateType.S32 || elemType == AggregateType.U32))
{ {
iq = PixelImap.Constant; iq = PixelImap.Constant;
}
} }
} }
DeclareInputOrOutput(context, attr, perPatch, isOutAttr: false, iq);
} }
DeclareInputOrOutput(context, attr, perPatch, isOutAttr: false, iq);
} }
} }
private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info, bool perPatch) private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info, bool perPatch)
{ {
bool oaIndexing = context.Config.UsedFeatures.HasFlag(FeatureFlags.OaIndexing); bool oaIndexing = context.Config.UsedFeatures.HasFlag(FeatureFlags.OaIndexing);
if (oaIndexing && !perPatch)
{
var attrType = context.TypeVector(context.TypeFP32(), (LiteralInteger)4);
attrType = context.TypeArray(attrType, context.Constant(context.TypeU32(), (LiteralInteger)MaxAttributes));
if (context.Config.Stage == ShaderStage.TessellationControl)
{
attrType = context.TypeArray(attrType, context.Constant(context.TypeU32(), context.Config.ThreadsPerInputPrimitive));
}
var spvType = context.TypePointer(StorageClass.Output, attrType);
var spvVar = context.Variable(spvType, StorageClass.Output);
context.Decorate(spvVar, Decoration.Location, (LiteralInteger)0);
context.AddGlobalVariable(spvVar);
context.OutputsArray = spvVar;
}
var outputs = perPatch ? info.OutputsPerPatch : info.Outputs; var outputs = perPatch ? info.OutputsPerPatch : info.Outputs;
foreach (int attr in outputs) foreach (int attr in outputs)
@@ -477,29 +498,10 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
if (oaIndexing && isUserAttr && !perPatch) if (oaIndexing && isUserAttr && !perPatch)
{ {
if (context.OutputsArray == null) continue;
{
var attrType = context.TypeVector(context.TypeFP32(), (LiteralInteger)4);
attrType = context.TypeArray(attrType, context.Constant(context.TypeU32(), (LiteralInteger)MaxAttributes));
if (context.Config.Stage == ShaderStage.TessellationControl)
{
attrType = context.TypeArray(attrType, context.Constant(context.TypeU32(), context.Config.ThreadsPerInputPrimitive));
}
var spvType = context.TypePointer(StorageClass.Output, attrType);
var spvVar = context.Variable(spvType, StorageClass.Output);
context.Decorate(spvVar, Decoration.Location, (LiteralInteger)0);
context.AddGlobalVariable(spvVar);
context.OutputsArray = spvVar;
}
}
else
{
DeclareOutputAttribute(context, attr, perPatch);
} }
DeclareOutputAttribute(context, attr, perPatch);
} }
if (context.Config.Stage == ShaderStage.Vertex) if (context.Config.Stage == ShaderStage.Vertex)

View File

@@ -39,7 +39,6 @@ namespace Ryujinx.Graphics.Vulkan
BufferUsageFlags.VertexBufferBit | BufferUsageFlags.VertexBufferBit |
BufferUsageFlags.TransformFeedbackBufferBitExt; BufferUsageFlags.TransformFeedbackBufferBitExt;
private readonly PhysicalDevice _physicalDevice;
private readonly Device _device; private readonly Device _device;
private readonly IdList<BufferHolder> _buffers; private readonly IdList<BufferHolder> _buffers;
@@ -48,9 +47,8 @@ namespace Ryujinx.Graphics.Vulkan
public StagingBuffer StagingBuffer { get; } public StagingBuffer StagingBuffer { get; }
public BufferManager(VulkanRenderer gd, PhysicalDevice physicalDevice, Device device) public BufferManager(VulkanRenderer gd, Device device)
{ {
_physicalDevice = physicalDevice;
_device = device; _device = device;
_buffers = new IdList<BufferHolder>(); _buffers = new IdList<BufferHolder>();
StagingBuffer = new StagingBuffer(gd, this); StagingBuffer = new StagingBuffer(gd, this);
@@ -114,7 +112,7 @@ namespace Ryujinx.Graphics.Vulkan
allocateFlagsAlt = DefaultBufferMemoryAltFlags; allocateFlagsAlt = DefaultBufferMemoryAltFlags;
} }
var allocation = gd.MemoryAllocator.AllocateDeviceMemory(_physicalDevice, requirements, allocateFlags, allocateFlagsAlt); var allocation = gd.MemoryAllocator.AllocateDeviceMemory(requirements, allocateFlags, allocateFlagsAlt);
if (allocation.Memory.Handle == 0UL) if (allocation.Memory.Handle == 0UL)
{ {

View File

@@ -8,11 +8,10 @@ namespace Ryujinx.Graphics.Vulkan
{ {
None = 0, None = 0,
VertexBufferAlignment4B = 1, NoTriangleFans = 1,
NoTriangleFans = 1 << 1, NoPointMode = 1 << 1,
NoPointMode = 1 << 2, No3DImageView = 1 << 2,
No3DImageView = 1 << 3, NoLodBias = 1 << 3
NoLodBias = 1 << 4
} }
readonly struct HardwareCapabilities readonly struct HardwareCapabilities
@@ -40,6 +39,7 @@ namespace Ryujinx.Graphics.Vulkan
public readonly ShaderStageFlags RequiredSubgroupSizeStages; public readonly ShaderStageFlags RequiredSubgroupSizeStages;
public readonly SampleCountFlags SupportedSampleCounts; public readonly SampleCountFlags SupportedSampleCounts;
public readonly PortabilitySubsetFlags PortabilitySubset; public readonly PortabilitySubsetFlags PortabilitySubset;
public readonly uint VertexBufferAlignment;
public HardwareCapabilities( public HardwareCapabilities(
bool supportsIndexTypeUint8, bool supportsIndexTypeUint8,
@@ -64,7 +64,8 @@ namespace Ryujinx.Graphics.Vulkan
uint maxSubgroupSize, uint maxSubgroupSize,
ShaderStageFlags requiredSubgroupSizeStages, ShaderStageFlags requiredSubgroupSizeStages,
SampleCountFlags supportedSampleCounts, SampleCountFlags supportedSampleCounts,
PortabilitySubsetFlags portabilitySubset) PortabilitySubsetFlags portabilitySubset,
uint vertexBufferAlignment)
{ {
SupportsIndexTypeUint8 = supportsIndexTypeUint8; SupportsIndexTypeUint8 = supportsIndexTypeUint8;
SupportsCustomBorderColor = supportsCustomBorderColor; SupportsCustomBorderColor = supportsCustomBorderColor;
@@ -89,6 +90,7 @@ namespace Ryujinx.Graphics.Vulkan
RequiredSubgroupSizeStages = requiredSubgroupSizeStages; RequiredSubgroupSizeStages = requiredSubgroupSizeStages;
SupportedSampleCounts = supportedSampleCounts; SupportedSampleCounts = supportedSampleCounts;
PortabilitySubset = portabilitySubset; PortabilitySubset = portabilitySubset;
VertexBufferAlignment = vertexBufferAlignment;
} }
} }
} }

View File

@@ -9,34 +9,36 @@ namespace Ryujinx.Graphics.Vulkan
private ulong MaxDeviceMemoryUsageEstimate = 16UL * 1024 * 1024 * 1024; private ulong MaxDeviceMemoryUsageEstimate = 16UL * 1024 * 1024 * 1024;
private readonly Vk _api; private readonly Vk _api;
private readonly PhysicalDevice _physicalDevice;
private readonly Device _device; private readonly Device _device;
private readonly List<MemoryAllocatorBlockList> _blockLists; private readonly List<MemoryAllocatorBlockList> _blockLists;
private readonly int _blockAlignment;
private readonly PhysicalDeviceMemoryProperties _physicalDeviceMemoryProperties;
private int _blockAlignment; public MemoryAllocator(Vk api, PhysicalDevice physicalDevice, Device device, uint maxMemoryAllocationCount)
public MemoryAllocator(Vk api, Device device, uint maxMemoryAllocationCount)
{ {
_api = api; _api = api;
_physicalDevice = physicalDevice;
_device = device; _device = device;
_blockLists = new List<MemoryAllocatorBlockList>(); _blockLists = new List<MemoryAllocatorBlockList>();
_blockAlignment = (int)Math.Min(int.MaxValue, MaxDeviceMemoryUsageEstimate / (ulong)maxMemoryAllocationCount); _blockAlignment = (int)Math.Min(int.MaxValue, MaxDeviceMemoryUsageEstimate / (ulong)maxMemoryAllocationCount);
_api.GetPhysicalDeviceMemoryProperties(_physicalDevice, out _physicalDeviceMemoryProperties);
} }
public MemoryAllocation AllocateDeviceMemory( public MemoryAllocation AllocateDeviceMemory(
PhysicalDevice physicalDevice,
MemoryRequirements requirements, MemoryRequirements requirements,
MemoryPropertyFlags flags = 0) MemoryPropertyFlags flags = 0)
{ {
return AllocateDeviceMemory(physicalDevice, requirements, flags, flags); return AllocateDeviceMemory(requirements, flags, flags);
} }
public MemoryAllocation AllocateDeviceMemory( public MemoryAllocation AllocateDeviceMemory(
PhysicalDevice physicalDevice,
MemoryRequirements requirements, MemoryRequirements requirements,
MemoryPropertyFlags flags, MemoryPropertyFlags flags,
MemoryPropertyFlags alternativeFlags) MemoryPropertyFlags alternativeFlags)
{ {
int memoryTypeIndex = FindSuitableMemoryTypeIndex(_api, physicalDevice, requirements.MemoryTypeBits, flags, alternativeFlags); int memoryTypeIndex = FindSuitableMemoryTypeIndex(requirements.MemoryTypeBits, flags, alternativeFlags);
if (memoryTypeIndex < 0) if (memoryTypeIndex < 0)
{ {
return default; return default;
@@ -65,20 +67,16 @@ namespace Ryujinx.Graphics.Vulkan
return newBl.Allocate(size, alignment, map); return newBl.Allocate(size, alignment, map);
} }
private static int FindSuitableMemoryTypeIndex( private int FindSuitableMemoryTypeIndex(
Vk api,
PhysicalDevice physicalDevice,
uint memoryTypeBits, uint memoryTypeBits,
MemoryPropertyFlags flags, MemoryPropertyFlags flags,
MemoryPropertyFlags alternativeFlags) MemoryPropertyFlags alternativeFlags)
{ {
int bestCandidateIndex = -1; int bestCandidateIndex = -1;
api.GetPhysicalDeviceMemoryProperties(physicalDevice, out var properties); for (int i = 0; i < _physicalDeviceMemoryProperties.MemoryTypeCount; i++)
for (int i = 0; i < properties.MemoryTypeCount; i++)
{ {
var type = properties.MemoryTypes[i]; var type = _physicalDeviceMemoryProperties.MemoryTypes[i];
if ((memoryTypeBits & (1 << i)) != 0) if ((memoryTypeBits & (1 << i)) != 0)
{ {

View File

@@ -1,4 +1,5 @@
using Ryujinx.Graphics.GAL; using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Shader; using Ryujinx.Graphics.Shader;
using Silk.NET.Vulkan; using Silk.NET.Vulkan;
using System; using System;
@@ -650,9 +651,7 @@ namespace Ryujinx.Graphics.Vulkan
_newState.DepthWriteEnable = oldDepthWriteEnable; _newState.DepthWriteEnable = oldDepthWriteEnable;
_newState.Topology = oldTopology; _newState.Topology = oldTopology;
DynamicState.Viewports = oldViewports; DynamicState.SetViewports(ref oldViewports, oldViewportsCount);
DynamicState.ViewportsCount = (int)oldViewportsCount;
DynamicState.SetViewportsDirty();
_newState.ViewportsCount = oldViewportsCount; _newState.ViewportsCount = oldViewportsCount;
SignalStateChange(); SignalStateChange();
@@ -1138,7 +1137,7 @@ namespace Ryujinx.Graphics.Vulkan
buffer.Dispose(); buffer.Dispose();
if (!Gd.Capabilities.PortabilitySubset.HasFlag(PortabilitySubsetFlags.VertexBufferAlignment4B) && if (Gd.Capabilities.VertexBufferAlignment < 2 &&
(vertexBuffer.Stride % FormatExtensions.MaxBufferFormatScalarSize) == 0) (vertexBuffer.Stride % FormatExtensions.MaxBufferFormatScalarSize) == 0)
{ {
buffer = new VertexBufferState( buffer = new VertexBufferState(
@@ -1183,6 +1182,8 @@ namespace Ryujinx.Graphics.Vulkan
return Math.Clamp(value, 0f, 1f); return Math.Clamp(value, 0f, 1f);
} }
DynamicState.ViewportsCount = (uint)count;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
var viewport = viewports[i]; var viewport = viewports[i];
@@ -1196,8 +1197,6 @@ namespace Ryujinx.Graphics.Vulkan
Clamp(viewport.DepthFar))); Clamp(viewport.DepthFar)));
} }
DynamicState.ViewportsCount = count;
float disableTransformF = disableTransform ? 1.0f : 0.0f; float disableTransformF = disableTransform ? 1.0f : 0.0f;
if (SupportBufferUpdater.Data.ViewportInverse.W != disableTransformF || disableTransform) if (SupportBufferUpdater.Data.ViewportInverse.W != disableTransformF || disableTransform)
{ {

View File

@@ -1,4 +1,5 @@
using Ryujinx.Graphics.GAL; using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using Silk.NET.Vulkan; using Silk.NET.Vulkan;
using System; using System;
@@ -253,7 +254,7 @@ namespace Ryujinx.Graphics.Vulkan
if (gd.NeedsVertexBufferAlignment(vbScalarSizes[i], out int alignment)) if (gd.NeedsVertexBufferAlignment(vbScalarSizes[i], out int alignment))
{ {
alignedStride = (vertexBuffer.Stride + (alignment - 1)) & -alignment; alignedStride = BitUtils.AlignUp(vertexBuffer.Stride, alignment);
} }
// TODO: Support divisor > 1 // TODO: Support divisor > 1

View File

@@ -21,7 +21,7 @@ namespace Ryujinx.Graphics.Vulkan
private Array4<float> _blendConstants; private Array4<float> _blendConstants;
public int ViewportsCount; public uint ViewportsCount;
public Array16<Viewport> Viewports; public Array16<Viewport> Viewports;
private enum DirtyFlags private enum DirtyFlags
@@ -88,9 +88,15 @@ namespace Ryujinx.Graphics.Vulkan
_dirty |= DirtyFlags.Viewport; _dirty |= DirtyFlags.Viewport;
} }
public void SetViewportsDirty() public void SetViewports(ref Array16<Viewport> viewports, uint viewportsCount)
{ {
_dirty |= DirtyFlags.Viewport; Viewports = viewports;
ViewportsCount = viewportsCount;
if (ViewportsCount != 0)
{
_dirty |= DirtyFlags.Viewport;
}
} }
public void ForceAllDirty() public void ForceAllDirty()
@@ -155,7 +161,10 @@ namespace Ryujinx.Graphics.Vulkan
private void RecordViewport(Vk api, CommandBuffer commandBuffer) private void RecordViewport(Vk api, CommandBuffer commandBuffer)
{ {
api.CmdSetViewport(commandBuffer, 0, (uint)ViewportsCount, Viewports.AsSpan()); if (ViewportsCount != 0)
{
api.CmdSetViewport(commandBuffer, 0, ViewportsCount, Viewports.AsSpan());
}
} }
} }
} }

View File

@@ -55,7 +55,6 @@ namespace Ryujinx.Graphics.Vulkan
public unsafe TextureStorage( public unsafe TextureStorage(
VulkanRenderer gd, VulkanRenderer gd,
PhysicalDevice physicalDevice,
Device device, Device device,
TextureCreateInfo info, TextureCreateInfo info,
float scaleFactor, float scaleFactor,
@@ -118,7 +117,7 @@ namespace Ryujinx.Graphics.Vulkan
if (foreignAllocation == null) if (foreignAllocation == null)
{ {
gd.Api.GetImageMemoryRequirements(device, _image, out var requirements); gd.Api.GetImageMemoryRequirements(device, _image, out var requirements);
var allocation = gd.MemoryAllocator.AllocateDeviceMemory(physicalDevice, requirements, DefaultImageMemoryFlags); var allocation = gd.MemoryAllocator.AllocateDeviceMemory(requirements, DefaultImageMemoryFlags);
if (allocation.Memory.Handle == 0UL) if (allocation.Memory.Handle == 0UL)
{ {
@@ -173,7 +172,7 @@ namespace Ryujinx.Graphics.Vulkan
var info = NewCreateInfoWith(ref _info, format, _info.BytesPerPixel); var info = NewCreateInfoWith(ref _info, format, _info.BytesPerPixel);
storage = new TextureStorage(_gd, default, _device, info, ScaleFactor, _allocationAuto); storage = new TextureStorage(_gd, _device, info, ScaleFactor, _allocationAuto);
_aliasedStorages.Add(format, storage); _aliasedStorages.Add(format, storage);
} }

View File

@@ -712,7 +712,7 @@ namespace Ryujinx.Graphics.Vulkan
for (int level = 0; level < levels; level++) for (int level = 0; level < levels; level++)
{ {
int mipSize = GetBufferDataLength(Info.GetMipSize(dstLevel + level)); int mipSize = GetBufferDataLength(Info.GetMipSize2D(dstLevel + level) * dstLayers);
int endOffset = offset + mipSize; int endOffset = offset + mipSize;

View File

@@ -14,6 +14,9 @@ namespace Ryujinx.Graphics.Vulkan
public unsafe static class VulkanInitialization public unsafe static class VulkanInitialization
{ {
private const uint InvalidIndex = uint.MaxValue; private const uint InvalidIndex = uint.MaxValue;
private static uint MinimalVulkanVersion = Vk.Version11.Value;
private static uint MinimalInstanceVulkanVersion = Vk.Version12.Value;
private static uint MaximumVulkanVersion = Vk.Version12.Value;
private const string AppName = "Ryujinx.Graphics.Vulkan"; private const string AppName = "Ryujinx.Graphics.Vulkan";
private const int QueuesCount = 2; private const int QueuesCount = 2;
@@ -33,7 +36,8 @@ namespace Ryujinx.Graphics.Vulkan
"VK_KHR_shader_float16_int8", "VK_KHR_shader_float16_int8",
"VK_EXT_shader_subgroup_ballot", "VK_EXT_shader_subgroup_ballot",
"VK_EXT_subgroup_size_control", "VK_EXT_subgroup_size_control",
"VK_NV_geometry_shader_passthrough" "VK_NV_geometry_shader_passthrough",
"VK_KHR_portability_subset", // By spec, we should enable this if present.
}; };
public static string[] RequiredExtensions { get; } = new string[] public static string[] RequiredExtensions { get; } = new string[]
@@ -99,7 +103,7 @@ namespace Ryujinx.Graphics.Vulkan
ApplicationVersion = 1, ApplicationVersion = 1,
PEngineName = (byte*)appName, PEngineName = (byte*)appName,
EngineVersion = 1, EngineVersion = 1,
ApiVersion = Vk.Version12.Value ApiVersion = MaximumVulkanVersion
}; };
IntPtr* ppEnabledExtensions = stackalloc IntPtr[enabledExtensions.Length]; IntPtr* ppEnabledExtensions = stackalloc IntPtr[enabledExtensions.Length];
@@ -224,7 +228,7 @@ namespace Ryujinx.Graphics.Vulkan
ApplicationVersion = 1, ApplicationVersion = 1,
PEngineName = (byte*)appName, PEngineName = (byte*)appName,
EngineVersion = 1, EngineVersion = 1,
ApiVersion = Vk.Version12.Value ApiVersion = MaximumVulkanVersion
}; };
var instanceCreateInfo = new InstanceCreateInfo var instanceCreateInfo = new InstanceCreateInfo
@@ -239,6 +243,27 @@ namespace Ryujinx.Graphics.Vulkan
api.CreateInstance(in instanceCreateInfo, null, out var instance).ThrowOnError(); api.CreateInstance(in instanceCreateInfo, null, out var instance).ThrowOnError();
// We ensure that vkEnumerateInstanceVersion is present (added in 1.1).
// If the instance doesn't support it, no device is going to be 1.1 compatible.
if (api.GetInstanceProcAddr(instance, "vkEnumerateInstanceVersion") == IntPtr.Zero)
{
api.DestroyInstance(instance, null);
return Array.Empty<DeviceInfo>();
}
// We currently assume that the instance is compatible with Vulkan 1.2
// TODO: Remove this once we relax our initialization codepaths.
uint instanceApiVerison = 0;
api.EnumerateInstanceVersion(ref instanceApiVerison).ThrowOnError();
if (instanceApiVerison < MinimalInstanceVulkanVersion)
{
api.DestroyInstance(instance, null);
return Array.Empty<DeviceInfo>();
}
Marshal.FreeHGlobal(appName); Marshal.FreeHGlobal(appName);
uint physicalDeviceCount; uint physicalDeviceCount;
@@ -259,6 +284,11 @@ namespace Ryujinx.Graphics.Vulkan
var physicalDevice = physicalDevices[i]; var physicalDevice = physicalDevices[i];
api.GetPhysicalDeviceProperties(physicalDevice, out var properties); api.GetPhysicalDeviceProperties(physicalDevice, out var properties);
if (properties.ApiVersion < MinimalVulkanVersion)
{
continue;
}
devices[i] = new DeviceInfo( devices[i] = new DeviceInfo(
StringFromIdPair(properties.VendorID, properties.DeviceID), StringFromIdPair(properties.VendorID, properties.DeviceID),
VendorUtils.GetNameFromId(properties.VendorID), VendorUtils.GetNameFromId(properties.VendorID),

View File

@@ -234,10 +234,12 @@ namespace Ryujinx.Graphics.Vulkan
Api.GetPhysicalDeviceFeatures2(_physicalDevice, &features2); Api.GetPhysicalDeviceFeatures2(_physicalDevice, &features2);
var portabilityFlags = PortabilitySubsetFlags.None; var portabilityFlags = PortabilitySubsetFlags.None;
uint vertexBufferAlignment = 1;
if (usePortability) if (usePortability)
{ {
portabilityFlags |= propertiesPortabilitySubset.MinVertexInputBindingStrideAlignment > 1 ? PortabilitySubsetFlags.VertexBufferAlignment4B : 0; vertexBufferAlignment = propertiesPortabilitySubset.MinVertexInputBindingStrideAlignment;
portabilityFlags |= featuresPortabilitySubset.TriangleFans ? 0 : PortabilitySubsetFlags.NoTriangleFans; portabilityFlags |= featuresPortabilitySubset.TriangleFans ? 0 : PortabilitySubsetFlags.NoTriangleFans;
portabilityFlags |= featuresPortabilitySubset.PointPolygons ? 0 : PortabilitySubsetFlags.NoPointMode; portabilityFlags |= featuresPortabilitySubset.PointPolygons ? 0 : PortabilitySubsetFlags.NoPointMode;
portabilityFlags |= featuresPortabilitySubset.ImageView2DOn3DImage ? 0 : PortabilitySubsetFlags.No3DImageView; portabilityFlags |= featuresPortabilitySubset.ImageView2DOn3DImage ? 0 : PortabilitySubsetFlags.No3DImageView;
@@ -278,9 +280,10 @@ namespace Ryujinx.Graphics.Vulkan
propertiesSubgroupSizeControl.MaxSubgroupSize, propertiesSubgroupSizeControl.MaxSubgroupSize,
propertiesSubgroupSizeControl.RequiredSubgroupSizeStages, propertiesSubgroupSizeControl.RequiredSubgroupSizeStages,
supportedSampleCounts, supportedSampleCounts,
portabilityFlags); portabilityFlags,
vertexBufferAlignment);
MemoryAllocator = new MemoryAllocator(Api, _device, properties.Limits.MaxMemoryAllocationCount); MemoryAllocator = new MemoryAllocator(Api, _physicalDevice, _device, properties.Limits.MaxMemoryAllocationCount);
CommandBufferPool = VulkanInitialization.CreateCommandBufferPool(Api, _device, Queue, QueueLock, queueFamilyIndex); CommandBufferPool = VulkanInitialization.CreateCommandBufferPool(Api, _device, Queue, QueueLock, queueFamilyIndex);
@@ -290,7 +293,7 @@ namespace Ryujinx.Graphics.Vulkan
BackgroundResources = new BackgroundResources(this, _device); BackgroundResources = new BackgroundResources(this, _device);
BufferManager = new BufferManager(this, _physicalDevice, _device); BufferManager = new BufferManager(this, _device);
_syncManager = new SyncManager(this, _device); _syncManager = new SyncManager(this, _device);
_pipeline = new PipelineFull(this, _device); _pipeline = new PipelineFull(this, _device);
@@ -388,7 +391,7 @@ namespace Ryujinx.Graphics.Vulkan
internal TextureStorage CreateTextureStorage(TextureCreateInfo info, float scale) internal TextureStorage CreateTextureStorage(TextureCreateInfo info, float scale)
{ {
return new TextureStorage(this, _physicalDevice, _device, info, scale); return new TextureStorage(this, _device, info, scale);
} }
public void DeleteBuffer(BufferHandle buffer) public void DeleteBuffer(BufferHandle buffer)
@@ -636,11 +639,11 @@ namespace Ryujinx.Graphics.Vulkan
PrintGpuInformation(); PrintGpuInformation();
} }
public bool NeedsVertexBufferAlignment(int attrScalarAlignment, out int alignment) internal bool NeedsVertexBufferAlignment(int attrScalarAlignment, out int alignment)
{ {
if (Capabilities.PortabilitySubset.HasFlag(PortabilitySubsetFlags.VertexBufferAlignment4B)) if (Capabilities.VertexBufferAlignment > 1)
{ {
alignment = 4; alignment = (int)Capabilities.VertexBufferAlignment;
return true; return true;
} }

View File

@@ -38,7 +38,9 @@ namespace Ryujinx.HLE.HOS.Services.Hid.HidServer
public static bool IsValidNpadIdType(NpadIdType npadIdType) public static bool IsValidNpadIdType(NpadIdType npadIdType)
{ {
return npadIdType <= NpadIdType.Player8 || npadIdType == NpadIdType.Handheld || npadIdType == NpadIdType.Unknown; return (npadIdType >= NpadIdType.Player1 && npadIdType <= NpadIdType.Player8) ||
npadIdType == NpadIdType.Handheld ||
npadIdType == NpadIdType.Unknown;
} }
} }
} }

View File

@@ -722,7 +722,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid
for (int i = 0; i < supportedPlayerIds.Length; ++i) for (int i = 0; i < supportedPlayerIds.Length; ++i)
{ {
if (supportedPlayerIds[i] >= 0) if (HidUtils.IsValidNpadIdType(supportedPlayerIds[i]))
{ {
context.Device.Hid.Npads.SetSupportedPlayer(HidUtils.GetIndexFromNpadIdType(supportedPlayerIds[i])); context.Device.Hid.Npads.SetSupportedPlayer(HidUtils.GetIndexFromNpadIdType(supportedPlayerIds[i]));
} }
@@ -1101,7 +1101,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid
if (deviceType < NpadStyleIndex.System || deviceType >= NpadStyleIndex.FullKey) if (deviceType < NpadStyleIndex.System || deviceType >= NpadStyleIndex.FullKey)
{ {
if (npadIdType >= (NpadIdType.Player8 + 1) && npadIdType != NpadIdType.Handheld && npadIdType != NpadIdType.Unknown) if (!HidUtils.IsValidNpadIdType(npadIdType))
{ {
return ResultCode.InvalidNpadIdType; return ResultCode.InvalidNpadIdType;
} }

View File

@@ -96,7 +96,7 @@ namespace Ryujinx.Memory.Tests
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false) public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@@ -34,8 +34,8 @@ namespace Ryujinx.Memory.Tests
private IMultiRegionHandle GetGranular(bool smart, ulong address, ulong size, ulong granularity) private IMultiRegionHandle GetGranular(bool smart, ulong address, ulong size, ulong granularity)
{ {
return smart ? return smart ?
_tracking.BeginSmartGranularTracking(address, size, granularity) : _tracking.BeginSmartGranularTracking(address, size, granularity, 0) :
(IMultiRegionHandle)_tracking.BeginGranularTracking(address, size, null, granularity); (IMultiRegionHandle)_tracking.BeginGranularTracking(address, size, null, granularity, 0);
} }
private void RandomOrder(Random random, List<int> indices, Action<int> action) private void RandomOrder(Random random, List<int> indices, Action<int> action)
@@ -216,7 +216,7 @@ namespace Ryujinx.Memory.Tests
{ {
int region = regionSizes[i]; int region = regionSizes[i];
handle.QueryModified(address, (ulong)(PageSize * region), (address, size) => { }); handle.QueryModified(address, (ulong)(PageSize * region), (address, size) => { });
// There should be a gap between regions, // There should be a gap between regions,
// So that they don't combine and we can see the full effects. // So that they don't combine and we can see the full effects.
address += (ulong)(PageSize * (region + 1)); address += (ulong)(PageSize * (region + 1));
@@ -294,7 +294,7 @@ namespace Ryujinx.Memory.Tests
bool[] actionsTriggered = new bool[3]; bool[] actionsTriggered = new bool[3];
MultiRegionHandle granular = _tracking.BeginGranularTracking(PageSize * 3, PageSize * 3, null, PageSize); MultiRegionHandle granular = _tracking.BeginGranularTracking(PageSize * 3, PageSize * 3, null, PageSize, 0);
PreparePages(granular, 3, PageSize * 3); PreparePages(granular, 3, PageSize * 3);
// Write to the second handle in the multiregion. // Write to the second handle in the multiregion.
@@ -307,7 +307,7 @@ namespace Ryujinx.Memory.Tests
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
singlePages[i] = _tracking.BeginTracking(PageSize * (8 + (ulong)i), PageSize); singlePages[i] = _tracking.BeginTracking(PageSize * (8 + (ulong)i), PageSize, 0);
singlePages[i].Reprotect(); singlePages[i].Reprotect();
} }
@@ -321,7 +321,7 @@ namespace Ryujinx.Memory.Tests
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
doublePages[i] = _tracking.BeginTracking(PageSize * (11 + (ulong)i * 2), PageSize * 2); doublePages[i] = _tracking.BeginTracking(PageSize * (11 + (ulong)i * 2), PageSize * 2, 0);
doublePages[i].Reprotect(); doublePages[i].Reprotect();
} }
@@ -340,7 +340,7 @@ namespace Ryujinx.Memory.Tests
doublePages doublePages
}; };
MultiRegionHandle combined = _tracking.BeginGranularTracking(0, PageSize * 18, handleGroups.SelectMany((handles) => handles), PageSize); MultiRegionHandle combined = _tracking.BeginGranularTracking(0, PageSize * 18, handleGroups.SelectMany((handles) => handles), PageSize, 0);
bool[] expectedDirty = new bool[] bool[] expectedDirty = new bool[]
{ {
@@ -405,7 +405,7 @@ namespace Ryujinx.Memory.Tests
{ {
bool actionTriggered = false; bool actionTriggered = false;
MultiRegionHandle granular = _tracking.BeginGranularTracking(PageSize * 3, PageSize * 3, null, PageSize); MultiRegionHandle granular = _tracking.BeginGranularTracking(PageSize * 3, PageSize * 3, null, PageSize, 0);
PreparePages(granular, 3, PageSize * 3); PreparePages(granular, 3, PageSize * 3);
// Add a precise action to the second and third handle in the multiregion. // Add a precise action to the second and third handle in the multiregion.

View File

@@ -44,7 +44,7 @@ namespace Ryujinx.Memory.Tests
[Test] [Test]
public void SingleRegion() public void SingleRegion()
{ {
RegionHandle handle = _tracking.BeginTracking(0, PageSize); RegionHandle handle = _tracking.BeginTracking(0, PageSize, 0);
(ulong address, ulong size)? readTrackingTriggered = null; (ulong address, ulong size)? readTrackingTriggered = null;
handle.RegisterAction((address, size) => handle.RegisterAction((address, size) =>
{ {
@@ -97,7 +97,7 @@ namespace Ryujinx.Memory.Tests
[Test] [Test]
public void OverlappingRegions() public void OverlappingRegions()
{ {
RegionHandle allHandle = _tracking.BeginTracking(0, PageSize * 16); RegionHandle allHandle = _tracking.BeginTracking(0, PageSize * 16, 0);
allHandle.Reprotect(); allHandle.Reprotect();
(ulong address, ulong size)? readTrackingTriggeredAll = null; (ulong address, ulong size)? readTrackingTriggeredAll = null;
@@ -116,7 +116,7 @@ namespace Ryujinx.Memory.Tests
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
{ {
containedHandles[i] = _tracking.BeginTracking((ulong)i * PageSize, PageSize); containedHandles[i] = _tracking.BeginTracking((ulong)i * PageSize, PageSize, 0);
containedHandles[i].Reprotect(); containedHandles[i].Reprotect();
} }
@@ -163,7 +163,7 @@ namespace Ryujinx.Memory.Tests
ulong alignedEnd = ((address + size + PageSize - 1) / PageSize) * PageSize; ulong alignedEnd = ((address + size + PageSize - 1) / PageSize) * PageSize;
ulong alignedSize = alignedEnd - alignedStart; ulong alignedSize = alignedEnd - alignedStart;
RegionHandle handle = _tracking.BeginTracking(address, size); RegionHandle handle = _tracking.BeginTracking(address, size, 0);
// Anywhere inside the pages the region is contained on should trigger. // Anywhere inside the pages the region is contained on should trigger.
@@ -207,7 +207,7 @@ namespace Ryujinx.Memory.Tests
for (int i = 0; i < handles.Length; i++) for (int i = 0; i < handles.Length; i++)
{ {
handles[i] = _tracking.BeginTracking((ulong)i * PageSize, PageSize); handles[i] = _tracking.BeginTracking((ulong)i * PageSize, PageSize, 0);
handles[i].Reprotect(); handles[i].Reprotect();
} }
@@ -263,7 +263,7 @@ namespace Ryujinx.Memory.Tests
Random random = new Random(randSeed + 512); Random random = new Random(randSeed + 512);
while (Stopwatch.GetTimestamp() < finishedTime) while (Stopwatch.GetTimestamp() < finishedTime)
{ {
RegionHandle handle = _tracking.BeginTracking((ulong)random.Next(maxAddress), (ulong)random.Next(65536)); RegionHandle handle = _tracking.BeginTracking((ulong)random.Next(maxAddress), (ulong)random.Next(65536), 0);
handle.Dispose(); handle.Dispose();
@@ -295,7 +295,7 @@ namespace Ryujinx.Memory.Tests
// Read actions should only be triggered once for each registration. // Read actions should only be triggered once for each registration.
// The implementation should use an interlocked exchange to make sure other threads can't get the action. // The implementation should use an interlocked exchange to make sure other threads can't get the action.
RegionHandle handle = _tracking.BeginTracking(0, PageSize); RegionHandle handle = _tracking.BeginTracking(0, PageSize, 0);
int triggeredCount = 0; int triggeredCount = 0;
int registeredCount = 0; int registeredCount = 0;
@@ -359,7 +359,7 @@ namespace Ryujinx.Memory.Tests
{ {
// Ensure that disposed handles correctly remove their virtual and physical regions. // Ensure that disposed handles correctly remove their virtual and physical regions.
RegionHandle handle = _tracking.BeginTracking(0, PageSize); RegionHandle handle = _tracking.BeginTracking(0, PageSize, 0);
handle.Reprotect(); handle.Reprotect();
Assert.AreEqual(1, _tracking.GetRegionCount()); Assert.AreEqual(1, _tracking.GetRegionCount());
@@ -372,8 +372,8 @@ namespace Ryujinx.Memory.Tests
// We expect there to be three regions after creating both, one for the small region and two covering the big one around it. // We expect there to be three regions after creating both, one for the small region and two covering the big one around it.
// Regions are always split to avoid overlapping, which is why there are three instead of two. // Regions are always split to avoid overlapping, which is why there are three instead of two.
RegionHandle handleSmall = _tracking.BeginTracking(PageSize, PageSize); RegionHandle handleSmall = _tracking.BeginTracking(PageSize, PageSize, 0);
RegionHandle handleBig = _tracking.BeginTracking(0, PageSize * 4); RegionHandle handleBig = _tracking.BeginTracking(0, PageSize * 4, 0);
Assert.AreEqual(3, _tracking.GetRegionCount()); Assert.AreEqual(3, _tracking.GetRegionCount());
@@ -398,7 +398,7 @@ namespace Ryujinx.Memory.Tests
protection = newProtection; protection = newProtection;
}; };
RegionHandle handle = _tracking.BeginTracking(0, PageSize); RegionHandle handle = _tracking.BeginTracking(0, PageSize, 0);
// After creating the handle, there is no protection yet. // After creating the handle, there is no protection yet.
Assert.AreEqual(MemoryPermission.ReadAndWrite, protection); Assert.AreEqual(MemoryPermission.ReadAndWrite, protection);
@@ -453,7 +453,7 @@ namespace Ryujinx.Memory.Tests
[Test] [Test]
public void PreciseAction() public void PreciseAction()
{ {
RegionHandle handle = _tracking.BeginTracking(0, PageSize); RegionHandle handle = _tracking.BeginTracking(0, PageSize, 0);
(ulong address, ulong size, bool write)? preciseTriggered = null; (ulong address, ulong size, bool write)? preciseTriggered = null;
handle.RegisterPreciseAction((address, size, write) => handle.RegisterPreciseAction((address, size, write) =>

View File

@@ -462,7 +462,7 @@ namespace Ryujinx.Memory
} }
/// <inheritdoc/> /// <inheritdoc/>
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false) public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
{ {
// Only the ARM Memory Manager has tracking for now. // Only the ARM Memory Manager has tracking for now.
} }

View File

@@ -175,7 +175,8 @@ namespace Ryujinx.Memory
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="write">True if the region was written, false if read</param> /// <param name="write">True if the region was written, false if read</param>
/// <param name="precise">True if the access is precise, false otherwise</param> /// <param name="precise">True if the access is precise, false otherwise</param>
void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false); /// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null);
/// <summary> /// <summary>
/// Reprotect a region of virtual memory for tracking. /// Reprotect a region of virtual memory for tracking.

View File

@@ -50,7 +50,8 @@ namespace Ryujinx.Memory.Tracking
/// <param name="address">Address accessed</param> /// <param name="address">Address accessed</param>
/// <param name="size">Size of the region affected in bytes</param> /// <param name="size">Size of the region affected in bytes</param>
/// <param name="write">Whether the region was written to or read</param> /// <param name="write">Whether the region was written to or read</param>
public abstract void Signal(ulong address, ulong size, bool write); /// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
public abstract void Signal(ulong address, ulong size, bool write, int? exemptId);
/// <summary> /// <summary>
/// Signals to the handles that a precise memory event has occurred. Assumes that the tracking lock has been obtained. /// Signals to the handles that a precise memory event has occurred. Assumes that the tracking lock has been obtained.
@@ -58,10 +59,11 @@ namespace Ryujinx.Memory.Tracking
/// <param name="address">Address accessed</param> /// <param name="address">Address accessed</param>
/// <param name="size">Size of the region affected in bytes</param> /// <param name="size">Size of the region affected in bytes</param>
/// <param name="write">Whether the region was written to or read</param> /// <param name="write">Whether the region was written to or read</param>
public abstract void SignalPrecise(ulong address, ulong size, bool write); /// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
public abstract void SignalPrecise(ulong address, ulong size, bool write, int? exemptId);
/// <summary> /// <summary>
/// Split this region into two, around the specified address. /// Split this region into two, around the specified address.
/// This region is updated to end at the split address, and a new region is created to represent past that point. /// This region is updated to end at the split address, and a new region is created to represent past that point.
/// </summary> /// </summary>
/// <param name="splitAddress">Address to split the region around</param> /// <param name="splitAddress">Address to split the region around</param>

View File

@@ -136,10 +136,11 @@ namespace Ryujinx.Memory.Tracking
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="handles">Handles to inherit state from or reuse. When none are present, provide null</param> /// <param name="handles">Handles to inherit state from or reuse. When none are present, provide null</param>
/// <param name="granularity">Desired granularity of write tracking</param> /// <param name="granularity">Desired granularity of write tracking</param>
/// <param name="id">Handle ID</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity) public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
{ {
return new MultiRegionHandle(this, address, size, handles, granularity); return new MultiRegionHandle(this, address, size, handles, granularity, id);
} }
/// <summary> /// <summary>
@@ -148,12 +149,13 @@ namespace Ryujinx.Memory.Tracking
/// <param name="address">CPU virtual address of the region</param> /// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="granularity">Desired granularity of write tracking</param> /// <param name="granularity">Desired granularity of write tracking</param>
/// <param name="id">Handle ID</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
public SmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity) public SmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity, int id)
{ {
(address, size) = PageAlign(address, size); (address, size) = PageAlign(address, size);
return new SmartMultiRegionHandle(this, address, size, granularity); return new SmartMultiRegionHandle(this, address, size, granularity, id);
} }
/// <summary> /// <summary>
@@ -161,14 +163,16 @@ namespace Ryujinx.Memory.Tracking
/// </summary> /// </summary>
/// <param name="address">CPU virtual address of the region</param> /// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="id">Handle ID</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
public RegionHandle BeginTracking(ulong address, ulong size) public RegionHandle BeginTracking(ulong address, ulong size, int id)
{ {
var (paAddress, paSize) = PageAlign(address, size); var (paAddress, paSize) = PageAlign(address, size);
lock (TrackingLock) lock (TrackingLock)
{ {
RegionHandle handle = new RegionHandle(this, paAddress, paSize, address, size, _memoryManager.IsRangeMapped(address, size)); bool mapped = _memoryManager.IsRangeMapped(address, size);
RegionHandle handle = new RegionHandle(this, paAddress, paSize, address, size, id, mapped);
return handle; return handle;
} }
@@ -181,28 +185,31 @@ namespace Ryujinx.Memory.Tracking
/// <param name="size">Size of the region</param> /// <param name="size">Size of the region</param>
/// <param name="bitmap">The bitmap owning the dirty flag for this handle</param> /// <param name="bitmap">The bitmap owning the dirty flag for this handle</param>
/// <param name="bit">The bit of this handle within the dirty flag</param> /// <param name="bit">The bit of this handle within the dirty flag</param>
/// <param name="id">Handle ID</param>
/// <returns>The memory tracking handle</returns> /// <returns>The memory tracking handle</returns>
internal RegionHandle BeginTrackingBitmap(ulong address, ulong size, ConcurrentBitmap bitmap, int bit) internal RegionHandle BeginTrackingBitmap(ulong address, ulong size, ConcurrentBitmap bitmap, int bit, int id)
{ {
var (paAddress, paSize) = PageAlign(address, size); var (paAddress, paSize) = PageAlign(address, size);
lock (TrackingLock) lock (TrackingLock)
{ {
RegionHandle handle = new RegionHandle(this, paAddress, paSize, address, size, bitmap, bit, _memoryManager.IsRangeMapped(address, size)); bool mapped = _memoryManager.IsRangeMapped(address, size);
RegionHandle handle = new RegionHandle(this, paAddress, paSize, address, size, bitmap, bit, id, mapped);
return handle; return handle;
} }
} }
/// <summary> /// <summary>
/// Signal that a virtual memory event happened at the given location (one byte). /// Signal that a virtual memory event happened at the given location.
/// </summary> /// </summary>
/// <param name="address">Virtual address accessed</param> /// <param name="address">Virtual address accessed</param>
/// <param name="write">Whether the address was written to or read</param> /// <param name="size">Size of the region affected in bytes</param>
/// <param name="write">Whether the region was written to or read</param>
/// <returns>True if the event triggered any tracking regions, false otherwise</returns> /// <returns>True if the event triggered any tracking regions, false otherwise</returns>
public bool VirtualMemoryEventTracking(ulong address, bool write) public bool VirtualMemoryEvent(ulong address, ulong size, bool write)
{ {
return VirtualMemoryEvent(address, 1, write); return VirtualMemoryEvent(address, size, write, precise: false, null);
} }
/// <summary> /// <summary>
@@ -214,8 +221,9 @@ namespace Ryujinx.Memory.Tracking
/// <param name="size">Size of the region affected in bytes</param> /// <param name="size">Size of the region affected in bytes</param>
/// <param name="write">Whether the region was written to or read</param> /// <param name="write">Whether the region was written to or read</param>
/// <param name="precise">True if the access is precise, false otherwise</param> /// <param name="precise">True if the access is precise, false otherwise</param>
/// <param name="exemptId">Optional ID that of the handles that should not be signalled</param>
/// <returns>True if the event triggered any tracking regions, false otherwise</returns> /// <returns>True if the event triggered any tracking regions, false otherwise</returns>
public bool VirtualMemoryEvent(ulong address, ulong size, bool write, bool precise = false) public bool VirtualMemoryEvent(ulong address, ulong size, bool write, bool precise, int? exemptId = null)
{ {
// Look up the virtual region using the region list. // Look up the virtual region using the region list.
// Signal up the chain to relevant handles. // Signal up the chain to relevant handles.
@@ -250,11 +258,11 @@ namespace Ryujinx.Memory.Tracking
if (precise) if (precise)
{ {
region.SignalPrecise(address, size, write); region.SignalPrecise(address, size, write, exemptId);
} }
else else
{ {
region.Signal(address, size, write); region.Signal(address, size, write, exemptId);
} }
} }
} }

View File

@@ -30,7 +30,13 @@ namespace Ryujinx.Memory.Tracking
public bool Dirty { get; private set; } = true; public bool Dirty { get; private set; } = true;
internal MultiRegionHandle(MemoryTracking tracking, ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity) internal MultiRegionHandle(
MemoryTracking tracking,
ulong address,
ulong size,
IEnumerable<IRegionHandle> handles,
ulong granularity,
int id)
{ {
_handles = new RegionHandle[(size + granularity - 1) / granularity]; _handles = new RegionHandle[(size + granularity - 1) / granularity];
Granularity = granularity; Granularity = granularity;
@@ -55,7 +61,7 @@ namespace Ryujinx.Memory.Tracking
// Fill any gap left before this handle. // Fill any gap left before this handle.
while (i < startIndex) while (i < startIndex)
{ {
RegionHandle fillHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i); RegionHandle fillHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
fillHandle.Parent = this; fillHandle.Parent = this;
_handles[i++] = fillHandle; _handles[i++] = fillHandle;
} }
@@ -76,7 +82,7 @@ namespace Ryujinx.Memory.Tracking
while (i < endIndex) while (i < endIndex)
{ {
RegionHandle splitHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i); RegionHandle splitHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
splitHandle.Parent = this; splitHandle.Parent = this;
splitHandle.Reprotect(handle.Dirty); splitHandle.Reprotect(handle.Dirty);
@@ -99,7 +105,7 @@ namespace Ryujinx.Memory.Tracking
// Fill any remaining space with new handles. // Fill any remaining space with new handles.
while (i < _handles.Length) while (i < _handles.Length)
{ {
RegionHandle handle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i); RegionHandle handle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
handle.Parent = this; handle.Parent = this;
_handles[i++] = handle; _handles[i++] = handle;
} }

View File

@@ -15,12 +15,12 @@ namespace Ryujinx.Memory.Tracking
/// If more than this number of checks have been performed on a dirty flag since its last reprotect, /// If more than this number of checks have been performed on a dirty flag since its last reprotect,
/// then it is dirtied infrequently. /// then it is dirtied infrequently.
/// </summary> /// </summary>
private static int CheckCountForInfrequent = 3; private const int CheckCountForInfrequent = 3;
/// <summary> /// <summary>
/// Number of frequent dirty/consume in a row to make this handle volatile. /// Number of frequent dirty/consume in a row to make this handle volatile.
/// </summary> /// </summary>
private static int VolatileThreshold = 5; private const int VolatileThreshold = 5;
public bool Dirty public bool Dirty
{ {
@@ -35,6 +35,7 @@ namespace Ryujinx.Memory.Tracking
} }
internal int SequenceNumber { get; set; } internal int SequenceNumber { get; set; }
internal int Id { get; }
public bool Unmapped { get; private set; } public bool Unmapped { get; private set; }
@@ -97,14 +98,26 @@ namespace Ryujinx.Memory.Tracking
/// <param name="realSize">The real, unaligned size of the handle</param> /// <param name="realSize">The real, unaligned size of the handle</param>
/// <param name="bitmap">The bitmap the dirty flag for this handle is stored in</param> /// <param name="bitmap">The bitmap the dirty flag for this handle is stored in</param>
/// <param name="bit">The bit index representing the dirty flag for this handle</param> /// <param name="bit">The bit index representing the dirty flag for this handle</param>
/// <param name="id">Handle ID</param>
/// <param name="mapped">True if the region handle starts mapped</param> /// <param name="mapped">True if the region handle starts mapped</param>
internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong realAddress, ulong realSize, ConcurrentBitmap bitmap, int bit, bool mapped = true) internal RegionHandle(
MemoryTracking tracking,
ulong address,
ulong size,
ulong realAddress,
ulong realSize,
ConcurrentBitmap bitmap,
int bit,
int id,
bool mapped = true)
{ {
Bitmap = bitmap; Bitmap = bitmap;
DirtyBit = bit; DirtyBit = bit;
Dirty = mapped; Dirty = mapped;
Id = id;
Unmapped = !mapped; Unmapped = !mapped;
Address = address; Address = address;
Size = size; Size = size;
@@ -131,11 +144,14 @@ namespace Ryujinx.Memory.Tracking
/// <param name="size">Size of the region to track</param> /// <param name="size">Size of the region to track</param>
/// <param name="realAddress">The real, unaligned address of the handle</param> /// <param name="realAddress">The real, unaligned address of the handle</param>
/// <param name="realSize">The real, unaligned size of the handle</param> /// <param name="realSize">The real, unaligned size of the handle</param>
/// <param name="id">Handle ID</param>
/// <param name="mapped">True if the region handle starts mapped</param> /// <param name="mapped">True if the region handle starts mapped</param>
internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong realAddress, ulong realSize, bool mapped = true) internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong realAddress, ulong realSize, int id, bool mapped = true)
{ {
Bitmap = new ConcurrentBitmap(1, mapped); Bitmap = new ConcurrentBitmap(1, mapped);
Id = id;
Unmapped = !mapped; Unmapped = !mapped;
Address = address; Address = address;

View File

@@ -18,10 +18,11 @@ namespace Ryujinx.Memory.Tracking
private readonly ulong _granularity; private readonly ulong _granularity;
private readonly ulong _size; private readonly ulong _size;
private MemoryTracking _tracking; private MemoryTracking _tracking;
private readonly int _id;
public bool Dirty { get; private set; } = true; public bool Dirty { get; private set; } = true;
internal SmartMultiRegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong granularity) internal SmartMultiRegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong granularity, int id)
{ {
// For this multi-region handle, the handle list starts empty. // For this multi-region handle, the handle list starts empty.
// As regions are queried, they are added to the _handles array at their start index. // As regions are queried, they are added to the _handles array at their start index.
@@ -34,6 +35,7 @@ namespace Ryujinx.Memory.Tracking
_address = address; _address = address;
_size = size; _size = size;
_id = id;
} }
public void SignalWrite() public void SignalWrite()
@@ -102,7 +104,7 @@ namespace Ryujinx.Memory.Tracking
RegionSignal signal = handle.PreAction; RegionSignal signal = handle.PreAction;
handle.Dispose(); handle.Dispose();
RegionHandle splitLow = _tracking.BeginTracking(address, size); RegionHandle splitLow = _tracking.BeginTracking(address, size, _id);
splitLow.Parent = this; splitLow.Parent = this;
if (signal != null) if (signal != null)
{ {
@@ -110,7 +112,7 @@ namespace Ryujinx.Memory.Tracking
} }
_handles[handleIndex] = splitLow; _handles[handleIndex] = splitLow;
RegionHandle splitHigh = _tracking.BeginTracking(address + size, handle.Size - size); RegionHandle splitHigh = _tracking.BeginTracking(address + size, handle.Size - size, _id);
splitHigh.Parent = this; splitHigh.Parent = this;
if (signal != null) if (signal != null)
{ {
@@ -145,7 +147,7 @@ namespace Ryujinx.Memory.Tracking
if (handle != null) if (handle != null)
{ {
// Fill up to the found handle. // Fill up to the found handle.
handle = _tracking.BeginTracking(startAddress, HandlesToBytes(i - startHandle)); handle = _tracking.BeginTracking(startAddress, HandlesToBytes(i - startHandle), _id);
handle.Parent = this; handle.Parent = this;
_handles[startHandle] = handle; _handles[startHandle] = handle;
return; return;
@@ -153,7 +155,7 @@ namespace Ryujinx.Memory.Tracking
} }
// Can fill the whole range. // Can fill the whole range.
_handles[startHandle] = _tracking.BeginTracking(startAddress, HandlesToBytes(1 + lastHandle - startHandle)); _handles[startHandle] = _tracking.BeginTracking(startAddress, HandlesToBytes(1 + lastHandle - startHandle), _id);
_handles[startHandle].Parent = this; _handles[startHandle].Parent = this;
} }

View File

@@ -19,19 +19,24 @@ namespace Ryujinx.Memory.Tracking
_tracking = tracking; _tracking = tracking;
} }
public override void Signal(ulong address, ulong size, bool write) /// <inheritdoc/>
public override void Signal(ulong address, ulong size, bool write, int? exemptId)
{ {
IList<RegionHandle> handles = Handles; IList<RegionHandle> handles = Handles;
for (int i = 0; i < handles.Count; i++) for (int i = 0; i < handles.Count; i++)
{ {
handles[i].Signal(address, size, write, ref handles); if (exemptId == null || handles[i].Id != exemptId.Value)
{
handles[i].Signal(address, size, write, ref handles);
}
} }
UpdateProtection(); UpdateProtection();
} }
public override void SignalPrecise(ulong address, ulong size, bool write) /// <inheritdoc/>
public override void SignalPrecise(ulong address, ulong size, bool write, int? exemptId)
{ {
IList<RegionHandle> handles = Handles; IList<RegionHandle> handles = Handles;
@@ -39,7 +44,10 @@ namespace Ryujinx.Memory.Tracking
for (int i = 0; i < handles.Count; i++) for (int i = 0; i < handles.Count; i++)
{ {
allPrecise &= handles[i].SignalPrecise(address, size, write, ref handles); if (exemptId == null || handles[i].Id != exemptId.Value)
{
allPrecise &= handles[i].SignalPrecise(address, size, write, ref handles);
}
} }
// Only update protection if a regular signal handler was called. // Only update protection if a regular signal handler was called.

View File

@@ -40,7 +40,7 @@ namespace Ryujinx.Tests.Memory
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false) public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@@ -9,6 +9,7 @@ using Ryujinx.Ui;
using Ryujinx.Ui.Widgets; using Ryujinx.Ui.Widgets;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@@ -23,20 +24,20 @@ namespace Ryujinx.Modules
{ {
public static class Updater public static class Updater
{ {
private const string GitHubApiURL = "https://api.github.com";
private const int ConnectionCount = 4;
internal static bool Running; internal static bool Running;
private static readonly string HomeDir = AppDomain.CurrentDomain.BaseDirectory; private static readonly string HomeDir = AppDomain.CurrentDomain.BaseDirectory;
private static readonly string UpdateDir = Path.Combine(Path.GetTempPath(), "Ryujinx", "update"); private static readonly string UpdateDir = Path.Combine(Path.GetTempPath(), "Ryujinx", "update");
private static readonly string UpdatePublishDir = Path.Combine(UpdateDir, "publish"); private static readonly string UpdatePublishDir = Path.Combine(UpdateDir, "publish");
private static readonly int ConnectionCount = 4;
private static string _buildVer; private static string _buildVer;
private static string _platformExt; private static string _platformExt;
private static string _buildUrl; private static string _buildUrl;
private static long _buildSize; private static long _buildSize;
private const string GitHubApiURL = "https://api.github.com";
// On Windows, GtkSharp.Dependencies adds these extra dirs that must be cleaned during updates. // On Windows, GtkSharp.Dependencies adds these extra dirs that must be cleaned during updates.
private static readonly string[] WindowsDependencyDirs = new string[] { "bin", "etc", "lib", "share" }; private static readonly string[] WindowsDependencyDirs = new string[] { "bin", "etc", "lib", "share" };
@@ -44,7 +45,7 @@ namespace Ryujinx.Modules
{ {
HttpClient result = new HttpClient(); HttpClient result = new HttpClient();
// Required by GitHub to interract with APIs. // Required by GitHub to interact with APIs.
result.DefaultRequestHeaders.Add("User-Agent", "Ryujinx-Updater/1.0.0"); result.DefaultRequestHeaders.Add("User-Agent", "Ryujinx-Updater/1.0.0");
return result; return result;
@@ -101,50 +102,48 @@ namespace Ryujinx.Modules
// Get latest version number from GitHub API // Get latest version number from GitHub API
try try
{ {
using (HttpClient jsonClient = ConstructHttpClient()) using HttpClient jsonClient = ConstructHttpClient();
string buildInfoURL = $"{GitHubApiURL}/repos/{ReleaseInformation.ReleaseChannelOwner}/{ReleaseInformation.ReleaseChannelRepo}/releases/latest";
// Fetch latest build information
string fetchedJson = await jsonClient.GetStringAsync(buildInfoURL);
JObject jsonRoot = JObject.Parse(fetchedJson);
JToken assets = jsonRoot["assets"];
_buildVer = (string)jsonRoot["name"];
foreach (JToken asset in assets)
{ {
string buildInfoURL = $"{GitHubApiURL}/repos/{ReleaseInformation.ReleaseChannelOwner}/{ReleaseInformation.ReleaseChannelRepo}/releases/latest"; string assetName = (string)asset["name"];
string assetState = (string)asset["state"];
string downloadURL = (string)asset["browser_download_url"];
// Fetch latest build information if (assetName.StartsWith("ryujinx") && assetName.EndsWith(_platformExt))
string fetchedJson = await jsonClient.GetStringAsync(buildInfoURL);
JObject jsonRoot = JObject.Parse(fetchedJson);
JToken assets = jsonRoot["assets"];
_buildVer = (string)jsonRoot["name"];
foreach (JToken asset in assets)
{ {
string assetName = (string)asset["name"]; _buildUrl = downloadURL;
string assetState = (string)asset["state"];
string downloadURL = (string)asset["browser_download_url"];
if (assetName.StartsWith("ryujinx") && assetName.EndsWith(_platformExt)) if (assetState != "uploaded")
{ {
_buildUrl = downloadURL; if (showVersionUpToDate)
if (assetState != "uploaded")
{ {
if (showVersionUpToDate) GtkDialog.CreateUpdaterInfoDialog("You are already using the latest version of Ryujinx!", "");
{
GtkDialog.CreateUpdaterInfoDialog("You are already using the latest version of Ryujinx!", "");
}
return;
} }
break; return;
} }
}
if (_buildUrl == null) break;
}
}
if (_buildUrl == null)
{
if (showVersionUpToDate)
{ {
if (showVersionUpToDate) GtkDialog.CreateUpdaterInfoDialog("You are already using the latest version of Ryujinx!", "");
{
GtkDialog.CreateUpdaterInfoDialog("You are already using the latest version of Ryujinx!", "");
}
return;
} }
return;
} }
} }
catch (Exception exception) catch (Exception exception)
@@ -247,160 +246,142 @@ namespace Ryujinx.Modules
for (int i = 0; i < ConnectionCount; i++) for (int i = 0; i < ConnectionCount; i++)
{ {
list.Add(new byte[0]); list.Add(Array.Empty<byte>());
} }
for (int i = 0; i < ConnectionCount; i++) for (int i = 0; i < ConnectionCount; i++)
{ {
#pragma warning disable SYSLIB0014 #pragma warning disable SYSLIB0014
// TODO: WebClient is obsolete and need to be replaced with a more complex logic using HttpClient. // TODO: WebClient is obsolete and need to be replaced with a more complex logic using HttpClient.
using (WebClient client = new WebClient()) using WebClient client = new WebClient();
#pragma warning restore SYSLIB0014 #pragma warning restore SYSLIB0014
webClients.Add(client);
if (i == ConnectionCount - 1)
{ {
webClients.Add(client); client.Headers.Add("Range", $"bytes={chunkSize * i}-{(chunkSize * (i + 1) - 1) + remainderChunk}");
}
else
{
client.Headers.Add("Range", $"bytes={chunkSize * i}-{chunkSize * (i + 1) - 1}");
}
if (i == ConnectionCount - 1) client.DownloadProgressChanged += (_, args) =>
{
int index = (int)args.UserState;
Interlocked.Add(ref totalProgressPercentage, -1 * progressPercentage[index]);
Interlocked.Exchange(ref progressPercentage[index], args.ProgressPercentage);
Interlocked.Add(ref totalProgressPercentage, args.ProgressPercentage);
updateDialog.ProgressBar.Value = totalProgressPercentage / ConnectionCount;
};
client.DownloadDataCompleted += (_, args) =>
{
int index = (int)args.UserState;
if (args.Cancelled)
{ {
client.Headers.Add("Range", $"bytes={chunkSize * i}-{(chunkSize * (i + 1) - 1) + remainderChunk}"); webClients[index].Dispose();
}
else
{
client.Headers.Add("Range", $"bytes={chunkSize * i}-{chunkSize * (i + 1) - 1}");
}
client.DownloadProgressChanged += (_, args) =>
{
int index = (int)args.UserState;
Interlocked.Add(ref totalProgressPercentage, -1 * progressPercentage[index]);
Interlocked.Exchange(ref progressPercentage[index], args.ProgressPercentage);
Interlocked.Add(ref totalProgressPercentage, args.ProgressPercentage);
updateDialog.ProgressBar.Value = totalProgressPercentage / ConnectionCount;
};
client.DownloadDataCompleted += (_, args) =>
{
int index = (int)args.UserState;
if (args.Cancelled)
{
webClients[index].Dispose();
return;
}
list[index] = args.Result;
Interlocked.Increment(ref completedRequests);
if (Equals(completedRequests, ConnectionCount))
{
byte[] mergedFileBytes = new byte[_buildSize];
for (int connectionIndex = 0, destinationOffset = 0; connectionIndex < ConnectionCount; connectionIndex++)
{
Array.Copy(list[connectionIndex], 0, mergedFileBytes, destinationOffset, list[connectionIndex].Length);
destinationOffset += list[connectionIndex].Length;
}
File.WriteAllBytes(updateFile, mergedFileBytes);
try
{
InstallUpdate(updateDialog, updateFile);
}
catch (Exception e)
{
Logger.Warning?.Print(LogClass.Application, e.Message);
Logger.Warning?.Print(LogClass.Application, "Multi-Threaded update failed, falling back to single-threaded updater.");
DoUpdateWithSingleThread(updateDialog, downloadUrl, updateFile);
return;
}
}
};
try
{
client.DownloadDataAsync(new Uri(downloadUrl), i);
}
catch (WebException ex)
{
Logger.Warning?.Print(LogClass.Application, ex.Message);
Logger.Warning?.Print(LogClass.Application, "Multi-Threaded update failed, falling back to single-threaded updater.");
for (int j = 0; j < webClients.Count; j++)
{
webClients[j].CancelAsync();
}
DoUpdateWithSingleThread(updateDialog, downloadUrl, updateFile);
return; return;
} }
list[index] = args.Result;
Interlocked.Increment(ref completedRequests);
if (Equals(completedRequests, ConnectionCount))
{
byte[] mergedFileBytes = new byte[_buildSize];
for (int connectionIndex = 0, destinationOffset = 0; connectionIndex < ConnectionCount; connectionIndex++)
{
Array.Copy(list[connectionIndex], 0, mergedFileBytes, destinationOffset, list[connectionIndex].Length);
destinationOffset += list[connectionIndex].Length;
}
File.WriteAllBytes(updateFile, mergedFileBytes);
try
{
InstallUpdate(updateDialog, updateFile);
}
catch (Exception e)
{
Logger.Warning?.Print(LogClass.Application, e.Message);
Logger.Warning?.Print(LogClass.Application, "Multi-Threaded update failed, falling back to single-threaded updater.");
DoUpdateWithSingleThread(updateDialog, downloadUrl, updateFile);
return;
}
}
};
try
{
client.DownloadDataAsync(new Uri(downloadUrl), i);
}
catch (WebException ex)
{
Logger.Warning?.Print(LogClass.Application, ex.Message);
Logger.Warning?.Print(LogClass.Application, "Multi-Threaded update failed, falling back to single-threaded updater.");
foreach (WebClient webClient in webClients)
{
webClient.CancelAsync();
}
DoUpdateWithSingleThread(updateDialog, downloadUrl, updateFile);
return;
} }
} }
} }
private static void DoUpdateWithSingleThreadWorker(UpdateDialog updateDialog, string downloadUrl, string updateFile) private static void DoUpdateWithSingleThreadWorker(UpdateDialog updateDialog, string downloadUrl, string updateFile)
{ {
using (HttpClient client = new HttpClient()) using HttpClient client = new HttpClient();
// We do not want to timeout while downloading
client.Timeout = TimeSpan.FromDays(1);
using (HttpResponseMessage response = client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead).Result)
using (Stream remoteFileStream = response.Content.ReadAsStreamAsync().Result)
{ {
// We do not want to timeout while downloading using (Stream updateFileStream = File.Open(updateFile, FileMode.Create))
client.Timeout = TimeSpan.FromDays(1);
using (HttpResponseMessage response = client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead).Result)
using (Stream remoteFileStream = response.Content.ReadAsStreamAsync().Result)
{ {
using (Stream updateFileStream = File.Open(updateFile, FileMode.Create)) long totalBytes = response.Content.Headers.ContentLength.Value;
long byteWritten = 0;
byte[] buffer = new byte[32 * 1024];
while (true)
{ {
long totalBytes = response.Content.Headers.ContentLength.Value; int readSize = remoteFileStream.Read(buffer);
long byteWritten = 0;
byte[] buffer = new byte[32 * 1024]; if (readSize == 0)
while (true)
{ {
int readSize = remoteFileStream.Read(buffer); break;
if (readSize == 0)
{
break;
}
byteWritten += readSize;
updateDialog.ProgressBar.Value = ((double)byteWritten / totalBytes) * 100;
updateFileStream.Write(buffer, 0, readSize);
} }
byteWritten += readSize;
updateDialog.ProgressBar.Value = ((double)byteWritten / totalBytes) * 100;
updateFileStream.Write(buffer, 0, readSize);
} }
} }
InstallUpdate(updateDialog, updateFile);
} }
InstallUpdate(updateDialog, updateFile);
} }
private static void DoUpdateWithSingleThread(UpdateDialog updateDialog, string downloadUrl, string updateFile) private static void DoUpdateWithSingleThread(UpdateDialog updateDialog, string downloadUrl, string updateFile)
{ {
Thread worker = new Thread(() => DoUpdateWithSingleThreadWorker(updateDialog, downloadUrl, updateFile)); Thread worker = new Thread(() => DoUpdateWithSingleThreadWorker(updateDialog, downloadUrl, updateFile))
worker.Name = "Updater.SingleThreadWorker";
worker.Start();
}
private static void SetFileExecutable(string path)
{
const UnixFileMode ExecutableFileMode = UnixFileMode.UserExecute |
UnixFileMode.UserWrite |
UnixFileMode.UserRead |
UnixFileMode.GroupRead |
UnixFileMode.GroupWrite |
UnixFileMode.OtherRead |
UnixFileMode.OtherWrite;
if (!OperatingSystem.IsWindows() && File.Exists(path))
{ {
File.SetUnixFileMode(path, ExecutableFileMode); Name = "Updater.SingleThreadWorker"
} };
worker.Start();
} }
private static async void InstallUpdate(UpdateDialog updateDialog, string updateFile) private static async void InstallUpdate(UpdateDialog updateDialog, string updateFile)
@@ -411,15 +392,17 @@ namespace Ryujinx.Modules
if (OperatingSystem.IsLinux()) if (OperatingSystem.IsLinux())
{ {
using (Stream inStream = File.OpenRead(updateFile)) using Stream inStream = File.OpenRead(updateFile);
using (Stream gzipStream = new GZipInputStream(inStream)) using Stream gzipStream = new GZipInputStream(inStream);
using (TarInputStream tarStream = new TarInputStream(gzipStream, Encoding.ASCII)) using TarInputStream tarStream = new TarInputStream(gzipStream, Encoding.ASCII);
{ updateDialog.ProgressBar.MaxValue = inStream.Length;
updateDialog.ProgressBar.MaxValue = inStream.Length;
await Task.Run(() => await Task.Run(() =>
{
TarEntry tarEntry;
if (!OperatingSystem.IsWindows())
{ {
TarEntry tarEntry;
while ((tarEntry = tarStream.GetNextEntry()) != null) while ((tarEntry = tarStream.GetNextEntry()) != null)
{ {
if (tarEntry.IsDirectory) continue; if (tarEntry.IsDirectory) continue;
@@ -433,6 +416,7 @@ namespace Ryujinx.Modules
tarStream.CopyEntryContents(outStream); tarStream.CopyEntryContents(outStream);
} }
File.SetUnixFileMode(outPath, (UnixFileMode)tarEntry.TarHeader.Mode);
File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc)); File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));
TarEntry entry = tarEntry; TarEntry entry = tarEntry;
@@ -442,43 +426,41 @@ namespace Ryujinx.Modules
updateDialog.ProgressBar.Value += entry.Size; updateDialog.ProgressBar.Value += entry.Size;
}); });
} }
}); }
});
updateDialog.ProgressBar.Value = inStream.Length; updateDialog.ProgressBar.Value = inStream.Length;
}
} }
else else
{ {
using (Stream inStream = File.OpenRead(updateFile)) using Stream inStream = File.OpenRead(updateFile);
using (ZipFile zipFile = new ZipFile(inStream)) using ZipFile zipFile = new ZipFile(inStream);
updateDialog.ProgressBar.MaxValue = zipFile.Count;
await Task.Run(() =>
{ {
updateDialog.ProgressBar.MaxValue = zipFile.Count; foreach (ZipEntry zipEntry in zipFile)
await Task.Run(() =>
{ {
foreach (ZipEntry zipEntry in zipFile) if (zipEntry.IsDirectory) continue;
string outPath = Path.Combine(UpdateDir, zipEntry.Name);
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
using (Stream zipStream = zipFile.GetInputStream(zipEntry))
using (FileStream outStream = File.OpenWrite(outPath))
{ {
if (zipEntry.IsDirectory) continue; zipStream.CopyTo(outStream);
string outPath = Path.Combine(UpdateDir, zipEntry.Name);
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
using (Stream zipStream = zipFile.GetInputStream(zipEntry))
using (FileStream outStream = File.OpenWrite(outPath))
{
zipStream.CopyTo(outStream);
}
File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc));
Application.Invoke(delegate
{
updateDialog.ProgressBar.Value++;
});
} }
});
} File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc));
Application.Invoke(delegate
{
updateDialog.ProgressBar.Value++;
});
}
});
} }
// Delete downloaded zip // Delete downloaded zip
@@ -522,8 +504,6 @@ namespace Ryujinx.Modules
Directory.Delete(UpdateDir, true); Directory.Delete(UpdateDir, true);
SetFileExecutable(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ryujinx"));
updateDialog.MainText.Text = "Update Complete!"; updateDialog.MainText.Text = "Update Complete!";
updateDialog.SecondaryText.Text = "Do you want to restart Ryujinx now?"; updateDialog.SecondaryText.Text = "Do you want to restart Ryujinx now?";
updateDialog.Modal = true; updateDialog.Modal = true;
@@ -640,4 +620,4 @@ namespace Ryujinx.Modules
} }
} }
} }
} }