mirror of
https://github.com/par274/sharpemu.git
synced 2026-07-26 12:48:39 +08:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ac35b97430 |
@@ -633,56 +633,25 @@ public sealed partial class DirectExecutionBackend
|
|||||||
"ASoW5WE-UPo" or // sceKernelAprSubmitCommandBufferAndGetResult
|
"ASoW5WE-UPo" or // sceKernelAprSubmitCommandBufferAndGetResult
|
||||||
"rqwFKI4PAiM" or // sceKernelAprWaitCommandBuffer
|
"rqwFKI4PAiM" or // sceKernelAprWaitCommandBuffer
|
||||||
"eE4Szl8sil8" or // sceKernelAprSubmitCommandBuffer
|
"eE4Szl8sil8" or // sceKernelAprSubmitCommandBuffer
|
||||||
"qvMUCyyaCSI" or // sceKernelAprSubmitCommandBufferAndGetId
|
"qvMUCyyaCSI"; // sceKernelAprSubmitCommandBufferAndGetId
|
||||||
"Q2V+iqvjgC0" or // vsnprintf
|
|
||||||
"q1cHNfGycLI" or // scePadRead
|
|
||||||
"xk0AcarP3V4" or // scePadOpen
|
|
||||||
"yH17Q6NWtVg" or // sceUserServiceGetEvent
|
|
||||||
"D-CzAxQL0XI" or // sceUserServiceGetPlatformPrivacySetting
|
|
||||||
"K-jXhbt2gn4"; // scePthreadMutexTrylock
|
|
||||||
|
|
||||||
private bool ShouldLogImportResult(string nid, OrbisGen2Result result)
|
private bool ShouldLogImportResult(string nid, OrbisGen2Result result)
|
||||||
{
|
{
|
||||||
var resultValue = unchecked((int)result);
|
|
||||||
if (resultValue > 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var expectedFileProbeMiss =
|
var expectedFileProbeMiss =
|
||||||
result == OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND &&
|
result == OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND &&
|
||||||
IsExpectedFileProbeNotFoundNid(nid);
|
IsExpectedFileProbeNotFoundNid(nid);
|
||||||
var expectedTimedWaitTimeout =
|
var expectedTimedWaitTimeout =
|
||||||
string.Equals(nid, "27bAgiJmOh0", StringComparison.Ordinal) &&
|
string.Equals(nid, "27bAgiJmOh0", StringComparison.Ordinal) &&
|
||||||
unchecked((int)result) == 60;
|
unchecked((int)result) == 60;
|
||||||
var expectedEqueueTimeout =
|
|
||||||
string.Equals(nid, "fzyMKs9kim0", StringComparison.Ordinal) &&
|
|
||||||
result == OrbisGen2Result.ORBIS_GEN2_ERROR_TIMED_OUT;
|
|
||||||
var expectedMutexTrylockBusy =
|
var expectedMutexTrylockBusy =
|
||||||
string.Equals(nid, "K-jXhbt2gn4", StringComparison.Ordinal) &&
|
string.Equals(nid, "K-jXhbt2gn4", StringComparison.Ordinal) &&
|
||||||
result == OrbisGen2Result.ORBIS_GEN2_ERROR_BUSY;
|
result == OrbisGen2Result.ORBIS_GEN2_ERROR_BUSY;
|
||||||
var expectedUserServiceNoEvent =
|
if (!expectedFileProbeMiss && !expectedTimedWaitTimeout && !expectedMutexTrylockBusy)
|
||||||
string.Equals(nid, "yH17Q6NWtVg", StringComparison.Ordinal) &&
|
|
||||||
resultValue == unchecked((int)0x80960007);
|
|
||||||
var expectedPrivacyInvalidParameter =
|
|
||||||
string.Equals(nid, "D-CzAxQL0XI", StringComparison.Ordinal) &&
|
|
||||||
resultValue == unchecked((int)0x80960009);
|
|
||||||
if (!expectedFileProbeMiss &&
|
|
||||||
!expectedTimedWaitTimeout &&
|
|
||||||
!expectedEqueueTimeout &&
|
|
||||||
!expectedMutexTrylockBusy &&
|
|
||||||
!expectedUserServiceNoEvent &&
|
|
||||||
!expectedPrivacyInvalidParameter)
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ShouldLogExpectedImportResults())
|
var key = nid + "\0" + (int)result;
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var key = nid + "\0" + resultValue;
|
|
||||||
int count;
|
int count;
|
||||||
lock (_importResultLogSampleGate)
|
lock (_importResultLogSampleGate)
|
||||||
{
|
{
|
||||||
@@ -694,12 +663,6 @@ public sealed partial class DirectExecutionBackend
|
|||||||
return count <= 8 || count % 10000 == 0;
|
return count <= 8 || count % 10000 == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool ShouldLogExpectedImportResults() =>
|
|
||||||
string.Equals(
|
|
||||||
Environment.GetEnvironmentVariable("SHARPEMU_LOG_EXPECTED_IMPORT_RESULTS"),
|
|
||||||
"1",
|
|
||||||
StringComparison.Ordinal);
|
|
||||||
|
|
||||||
private static bool IsExpectedFileProbeNotFoundNid(string nid) =>
|
private static bool IsExpectedFileProbeNotFoundNid(string nid) =>
|
||||||
nid is
|
nid is
|
||||||
"eV9wAD2riIA" or // sceKernelStat
|
"eV9wAD2riIA" or // sceKernelStat
|
||||||
|
|||||||
@@ -11,9 +11,7 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
{
|
{
|
||||||
private readonly ReaderWriterLockSlim _gate = new(LockRecursionPolicy.SupportsRecursion);
|
private readonly ReaderWriterLockSlim _gate = new(LockRecursionPolicy.SupportsRecursion);
|
||||||
private readonly object _guestAllocationGate = new();
|
private readonly object _guestAllocationGate = new();
|
||||||
private readonly object _allocationSearchHintGate = new();
|
|
||||||
private readonly List<MemoryRegion> _regions = new();
|
private readonly List<MemoryRegion> _regions = new();
|
||||||
private readonly Dictionary<(ulong DesiredAddress, ulong Alignment, bool Executable), ulong> _allocationSearchHints = new();
|
|
||||||
private readonly Dictionary<ulong, ProgramHeaderFlags> _pageProtections = new();
|
private readonly Dictionary<ulong, ProgramHeaderFlags> _pageProtections = new();
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
private const ulong PageSize = 0x1000;
|
private const ulong PageSize = 0x1000;
|
||||||
@@ -21,8 +19,7 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
private const ulong GuestAllocationArenaSize = 0x0100_0000;
|
private const ulong GuestAllocationArenaSize = 0x0100_0000;
|
||||||
private const ulong GuestAllocationArenaStartOffset = PageSize;
|
private const ulong GuestAllocationArenaStartOffset = PageSize;
|
||||||
private const ulong LargeDataReserveThreshold = 0x4000_0000UL; // 1 GiB
|
private const ulong LargeDataReserveThreshold = 0x4000_0000UL; // 1 GiB
|
||||||
private const ulong FullCommitRegionLimit = 4UL << 30;
|
private const ulong LazyReservePrimeBytes = 0x5000_0000UL; // 1.25 GiB
|
||||||
private const ulong DefaultLazyReservePrimeBytes = 0x0400_0000UL; // 64 MiB
|
|
||||||
private const ulong LazyReservePrimeChunkBytes = 0x0200_0000UL; // 32 MiB
|
private const ulong LazyReservePrimeChunkBytes = 0x0200_0000UL; // 32 MiB
|
||||||
|
|
||||||
private const uint MEM_COMMIT = 0x1000;
|
private const uint MEM_COMMIT = 0x1000;
|
||||||
@@ -38,7 +35,6 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
|
|
||||||
private ulong _guestAllocationArenaBase;
|
private ulong _guestAllocationArenaBase;
|
||||||
private ulong _guestAllocationOffset;
|
private ulong _guestAllocationOffset;
|
||||||
private static readonly ulong LazyReservePrimeBytes = ResolveLazyReservePrimeBytes();
|
|
||||||
|
|
||||||
[DllImport("kernel32.dll", SetLastError = true)]
|
[DllImport("kernel32.dll", SetLastError = true)]
|
||||||
private static extern void* VirtualAlloc(void* lpAddress, nuint dwSize, uint flAllocationType, uint flProtect);
|
private static extern void* VirtualAlloc(void* lpAddress, nuint dwSize, uint flAllocationType, uint flProtect);
|
||||||
@@ -100,7 +96,7 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
}
|
}
|
||||||
|
|
||||||
var allocationKind = executable ? "executable memory" : "data memory";
|
var allocationKind = executable ? "executable memory" : "data memory";
|
||||||
TraceVmem($"Allocated exact {allocationKind}: 0x{actualAddress:X16} - 0x{actualAddress + alignedSize:X16} ({alignedSize} bytes)");
|
Console.Error.WriteLine($"[VMEM] Allocated exact {allocationKind}: 0x{actualAddress:X16} - 0x{actualAddress + alignedSize:X16} ({alignedSize} bytes)");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,9 +110,7 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
var protection = executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
|
var protection = executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
|
||||||
var allocationType = MEM_COMMIT | MEM_RESERVE;
|
var allocationType = MEM_COMMIT | MEM_RESERVE;
|
||||||
var reservedOnly = false;
|
var reservedOnly = false;
|
||||||
var preferReserveOnly = !executable &&
|
var preferReserveOnly = !executable && alignedSize >= LargeDataReserveThreshold;
|
||||||
alignedSize >= LargeDataReserveThreshold &&
|
|
||||||
alignedSize > FullCommitRegionLimit;
|
|
||||||
|
|
||||||
void* result = null;
|
void* result = null;
|
||||||
if (preferReserveOnly)
|
if (preferReserveOnly)
|
||||||
@@ -145,7 +139,7 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
throw new InvalidOperationException($"Failed to allocate exact mapping at 0x{desiredAddress:X16} ({alignedSize} bytes)");
|
throw new InvalidOperationException($"Failed to allocate exact mapping at 0x{desiredAddress:X16} ({alignedSize} bytes)");
|
||||||
}
|
}
|
||||||
|
|
||||||
TraceVmem($"Could not allocate at 0x{desiredAddress:X16}, trying any address...");
|
Console.Error.WriteLine($"[VMEM] Could not allocate at 0x{desiredAddress:X16}, trying any address...");
|
||||||
result = VirtualAlloc(null, (nuint)alignedSize, allocationType, protection);
|
result = VirtualAlloc(null, (nuint)alignedSize, allocationType, protection);
|
||||||
|
|
||||||
if (result == null)
|
if (result == null)
|
||||||
@@ -199,12 +193,12 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
lazyPrimeState = committedBytes == primeBytes
|
lazyPrimeState = committedBytes == primeBytes
|
||||||
? $"ok:{committedBytes:X}"
|
? $"ok:{committedBytes:X}"
|
||||||
: $"partial:{committedBytes:X}/{primeBytes:X}";
|
: $"partial:{committedBytes:X}/{primeBytes:X}";
|
||||||
TraceVmem($"Primed lazy region: 0x{actualAddress:X16} - 0x{actualAddress + committedBytes:X16} ({committedBytes} bytes)");
|
Console.Error.WriteLine($"[VMEM] Primed lazy region: 0x{actualAddress:X16} - 0x{actualAddress + committedBytes:X16} ({committedBytes} bytes)");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lazyPrimeState = $"fail:{primeBytes:X}";
|
lazyPrimeState = $"fail:{primeBytes:X}";
|
||||||
TraceVmem($"Failed to prime lazy region at 0x{actualAddress:X16} ({primeBytes} bytes), continuing with on-demand commit");
|
Console.Error.WriteLine($"[VMEM] Failed to prime lazy region at 0x{actualAddress:X16} ({primeBytes} bytes), continuing with on-demand commit");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -233,7 +227,7 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
var allocationKind = reservedOnly
|
var allocationKind = reservedOnly
|
||||||
? "reserved data memory (lazy commit)"
|
? "reserved data memory (lazy commit)"
|
||||||
: (executable ? "executable memory" : "data memory");
|
: (executable ? "executable memory" : "data memory");
|
||||||
TraceVmem($"Allocated {allocationKind}: 0x{actualAddress:X16} - 0x{actualAddress + alignedSize:X16} ({alignedSize} bytes) lazy_prime={lazyPrimeState}");
|
Console.Error.WriteLine($"[VMEM] Allocated {allocationKind}: 0x{actualAddress:X16} - 0x{actualAddress + alignedSize:X16} ({alignedSize} bytes) lazy_prime={lazyPrimeState}");
|
||||||
|
|
||||||
return actualAddress;
|
return actualAddress;
|
||||||
}
|
}
|
||||||
@@ -253,8 +247,7 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
|
|
||||||
var alignedSize = AlignUp(size, PageSize);
|
var alignedSize = AlignUp(size, PageSize);
|
||||||
var effectiveAlignment = Math.Max(PageSize, alignment == 0 ? PageSize : alignment);
|
var effectiveAlignment = Math.Max(PageSize, alignment == 0 ? PageSize : alignment);
|
||||||
var requestedCursor = AlignUp(desiredAddress, effectiveAlignment);
|
var cursor = AlignUp(desiredAddress, effectiveAlignment);
|
||||||
var cursor = GetAllocationSearchCursor(desiredAddress, requestedCursor, effectiveAlignment, executable);
|
|
||||||
|
|
||||||
for (var attempt = 0; attempt < 0x10000; attempt++)
|
for (var attempt = 0; attempt < 0x10000; attempt++)
|
||||||
{
|
{
|
||||||
@@ -274,7 +267,6 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
actualAddress = AllocateAt(cursor, alignedSize, executable, allowAlternative: false);
|
actualAddress = AllocateAt(cursor, alignedSize, executable, allowAlternative: false);
|
||||||
if (actualAddress == cursor)
|
if (actualAddress == cursor)
|
||||||
{
|
{
|
||||||
UpdateAllocationSearchCursor(desiredAddress, effectiveAlignment, executable, actualAddress + alignedSize);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,10 +334,6 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
}
|
}
|
||||||
_regions.Clear();
|
_regions.Clear();
|
||||||
_pageProtections.Clear();
|
_pageProtections.Clear();
|
||||||
lock (_allocationSearchHintGate)
|
|
||||||
{
|
|
||||||
_allocationSearchHints.Clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -402,7 +390,7 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
|
|
||||||
ApplySegmentProtection(mapStart, mapEnd, protection);
|
ApplySegmentProtection(mapStart, mapEnd, protection);
|
||||||
|
|
||||||
TraceVmem($"Mapped segment: 0x{virtualAddress:X16} - 0x{virtualAddress + memorySize:X16} (file: {fileData.Length} bytes, prot: {protection})");
|
Console.Error.WriteLine($"[VMEM] Mapped segment: 0x{virtualAddress:X16} - 0x{virtualAddress + memorySize:X16} (file: {fileData.Length} bytes, prot: {protection})");
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -805,16 +793,6 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
foreach (var region in _regions)
|
foreach (var region in _regions)
|
||||||
{
|
{
|
||||||
var regionEnd = region.VirtualAddress + region.Size;
|
var regionEnd = region.VirtualAddress + region.Size;
|
||||||
if (region.VirtualAddress >= end)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (regionEnd <= address)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (address < regionEnd && region.VirtualAddress < end)
|
if (address < regionEnd && region.VirtualAddress < end)
|
||||||
{
|
{
|
||||||
overlapEnd = Math.Max(overlapEnd, regionEnd);
|
overlapEnd = Math.Max(overlapEnd, regionEnd);
|
||||||
@@ -829,37 +807,6 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
return overlapEnd != 0;
|
return overlapEnd != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ulong GetAllocationSearchCursor(
|
|
||||||
ulong desiredAddress,
|
|
||||||
ulong requestedCursor,
|
|
||||||
ulong alignment,
|
|
||||||
bool executable)
|
|
||||||
{
|
|
||||||
lock (_allocationSearchHintGate)
|
|
||||||
{
|
|
||||||
var key = (desiredAddress, alignment, executable);
|
|
||||||
if (_allocationSearchHints.TryGetValue(key, out var hintedCursor) &&
|
|
||||||
hintedCursor > requestedCursor)
|
|
||||||
{
|
|
||||||
return AlignUp(hintedCursor, alignment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return requestedCursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateAllocationSearchCursor(
|
|
||||||
ulong desiredAddress,
|
|
||||||
ulong alignment,
|
|
||||||
bool executable,
|
|
||||||
ulong nextCursor)
|
|
||||||
{
|
|
||||||
lock (_allocationSearchHintGate)
|
|
||||||
{
|
|
||||||
_allocationSearchHints[(desiredAddress, alignment, executable)] = AlignUp(nextCursor, alignment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool TryResolveRegionOffset(ulong address, ulong size, MemoryRegion region, out ulong offset)
|
private static bool TryResolveRegionOffset(ulong address, ulong size, MemoryRegion region, out ulong offset)
|
||||||
{
|
{
|
||||||
offset = 0;
|
offset = 0;
|
||||||
@@ -1028,29 +975,6 @@ public sealed unsafe class PhysicalVirtualMemory : IVirtualMemory, IGuestMemoryA
|
|||||||
return checked((value + mask) & ~mask);
|
return checked((value + mask) & ~mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ulong ResolveLazyReservePrimeBytes()
|
|
||||||
{
|
|
||||||
var configured = Environment.GetEnvironmentVariable("SHARPEMU_LAZY_RESERVE_PRIME_MB");
|
|
||||||
if (ulong.TryParse(configured, out var megabytes))
|
|
||||||
{
|
|
||||||
return megabytes == 0
|
|
||||||
? 0
|
|
||||||
: checked(Math.Min(megabytes, 4096UL) * 1024UL * 1024UL);
|
|
||||||
}
|
|
||||||
|
|
||||||
return DefaultLazyReservePrimeBytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TraceVmem(string message)
|
|
||||||
{
|
|
||||||
if (!string.Equals(Environment.GetEnvironmentVariable("SHARPEMU_LOG_VMEM"), "1", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.Error.WriteLine($"[VMEM] {message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (!_disposed)
|
if (!_disposed)
|
||||||
|
|||||||
@@ -3609,7 +3609,7 @@ public static class AgcExports
|
|||||||
',',
|
',',
|
||||||
draw.GlobalMemoryBindings.Select((binding, index) =>
|
draw.GlobalMemoryBindings.Select((binding, index) =>
|
||||||
$"{index}:0x{binding.BaseAddress:X16}:{binding.Data.Length}:" +
|
$"{index}:0x{binding.BaseAddress:X16}:{binding.Data.Length}:" +
|
||||||
Convert.ToHexString(binding.Data.AsSpan(0, Math.Min(binding.Data.Length, 256)))));
|
Convert.ToHexString(binding.Data.AsSpan(0, Math.Min(binding.Data.Length, 32)))));
|
||||||
var indices = draw.IndexBuffer is { } indexBuffer
|
var indices = draw.IndexBuffer is { } indexBuffer
|
||||||
? $"{(indexBuffer.Is32Bit ? 32 : 16)}:" +
|
? $"{(indexBuffer.Is32Bit ? 32 : 16)}:" +
|
||||||
Convert.ToHexString(indexBuffer.Data.AsSpan(0, Math.Min(indexBuffer.Data.Length, 32)))
|
Convert.ToHexString(indexBuffer.Data.AsSpan(0, Math.Min(indexBuffer.Data.Length, 32)))
|
||||||
@@ -3744,7 +3744,7 @@ public static class AgcExports
|
|||||||
descriptor.Width > 8192 ||
|
descriptor.Width > 8192 ||
|
||||||
descriptor.Height > 8192)
|
descriptor.Height > 8192)
|
||||||
{
|
{
|
||||||
texture = CreateFallbackGuestDrawTexture(isStorage, descriptor.Format, descriptor.NumberType);
|
texture = CreateFallbackGuestDrawTexture(isStorage);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3762,7 +3762,7 @@ public static class AgcExports
|
|||||||
sourceByteCount > MaxPresentedTextureBytes ||
|
sourceByteCount > MaxPresentedTextureBytes ||
|
||||||
sourceByteCount > int.MaxValue)
|
sourceByteCount > int.MaxValue)
|
||||||
{
|
{
|
||||||
texture = CreateFallbackGuestDrawTexture(isStorage, descriptor.Format, descriptor.NumberType);
|
texture = CreateFallbackGuestDrawTexture(isStorage);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3825,7 +3825,7 @@ public static class AgcExports
|
|||||||
var source = new byte[(int)sourceByteCount];
|
var source = new byte[(int)sourceByteCount];
|
||||||
if (!ctx.Memory.TryRead(descriptor.Address, source))
|
if (!ctx.Memory.TryRead(descriptor.Address, source))
|
||||||
{
|
{
|
||||||
texture = CreateFallbackGuestDrawTexture(isStorage, descriptor.Format, descriptor.NumberType);
|
texture = CreateFallbackGuestDrawTexture(isStorage);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3868,25 +3868,18 @@ public static class AgcExports
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static VulkanGuestDrawTexture CreateFallbackGuestDrawTexture(
|
private static VulkanGuestDrawTexture CreateFallbackGuestDrawTexture(bool isStorage = false) =>
|
||||||
bool isStorage,
|
new(
|
||||||
uint format,
|
|
||||||
uint numberType)
|
|
||||||
{
|
|
||||||
var fallbackFormat = format == 0 ? 10u : format;
|
|
||||||
var fallbackNumberType = numberType;
|
|
||||||
return new(
|
|
||||||
0,
|
0,
|
||||||
1,
|
1,
|
||||||
1,
|
1,
|
||||||
fallbackFormat,
|
56,
|
||||||
fallbackNumberType,
|
NumberType: 0,
|
||||||
[0, 0, 0, 255],
|
[0, 0, 0, 255],
|
||||||
IsFallback: true,
|
IsFallback: true,
|
||||||
IsStorage: isStorage,
|
IsStorage: isStorage,
|
||||||
MipLevels: 1,
|
MipLevels: 1,
|
||||||
MipLevel: 0);
|
MipLevel: 0);
|
||||||
}
|
|
||||||
|
|
||||||
private static VulkanGuestSampler ToVulkanSampler(IReadOnlyList<uint> descriptor) =>
|
private static VulkanGuestSampler ToVulkanSampler(IReadOnlyList<uint> descriptor) =>
|
||||||
descriptor.Count >= 4
|
descriptor.Count >= 4
|
||||||
|
|||||||
@@ -467,6 +467,9 @@ internal static partial class Gen5SpirvTranslator
|
|||||||
_imageBindingByPc.TryAdd(binding.Pc, index);
|
_imageBindingByPc.TryAdd(binding.Pc, index);
|
||||||
var isStorage =
|
var isStorage =
|
||||||
Gen5ShaderTranslator.IsStorageImageOperation(binding.Opcode);
|
Gen5ShaderTranslator.IsStorageImageOperation(binding.Opcode);
|
||||||
|
var isDepth =
|
||||||
|
binding.Opcode.Contains("SampleC", StringComparison.Ordinal) ||
|
||||||
|
binding.Opcode.Contains("Gather4C", StringComparison.Ordinal);
|
||||||
var (format, componentKind) =
|
var (format, componentKind) =
|
||||||
DecodeImageFormat(binding.ResourceDescriptor);
|
DecodeImageFormat(binding.ResourceDescriptor);
|
||||||
var componentType = componentKind switch
|
var componentType = componentKind switch
|
||||||
@@ -491,7 +494,7 @@ internal static partial class Gen5SpirvTranslator
|
|||||||
var imageType = _module.TypeImage(
|
var imageType = _module.TypeImage(
|
||||||
componentType,
|
componentType,
|
||||||
SpirvImageDim.Dim2D,
|
SpirvImageDim.Dim2D,
|
||||||
depth: false,
|
depth: isDepth,
|
||||||
arrayed: false,
|
arrayed: false,
|
||||||
multisampled: false,
|
multisampled: false,
|
||||||
sampled: isStorage ? 2u : 1u,
|
sampled: isStorage ? 2u : 1u,
|
||||||
@@ -1658,14 +1661,18 @@ internal static partial class Gen5SpirvTranslator
|
|||||||
var offset = hasOffset ? BuildImageOffset(image, 0) : 0u;
|
var offset = hasOffset ? BuildImageOffset(image, 0) : 0u;
|
||||||
var imageOperands =
|
var imageOperands =
|
||||||
(explicitLod ? 2u : 0u) | (hasOffset ? 0x10u : 0u);
|
(explicitLod ? 2u : 0u) | (hasOffset ? 0x10u : 0u);
|
||||||
var reference = hasCompare
|
|
||||||
? Bitcast(_floatType, LoadV(image.GetAddressRegister(hasOffset ? 1 : 0)))
|
|
||||||
: 0u;
|
|
||||||
var operands = new List<uint>
|
var operands = new List<uint>
|
||||||
{
|
{
|
||||||
imageObject,
|
imageObject,
|
||||||
coordinates,
|
coordinates,
|
||||||
};
|
};
|
||||||
|
if (hasCompare)
|
||||||
|
{
|
||||||
|
operands.Add(
|
||||||
|
Bitcast(
|
||||||
|
_floatType,
|
||||||
|
LoadV(image.GetAddressRegister(hasOffset ? 1 : 0))));
|
||||||
|
}
|
||||||
|
|
||||||
if (imageOperands != 0)
|
if (imageOperands != 0)
|
||||||
{
|
{
|
||||||
@@ -1682,14 +1689,29 @@ internal static partial class Gen5SpirvTranslator
|
|||||||
}
|
}
|
||||||
|
|
||||||
sampled = _module.AddInstruction(
|
sampled = _module.AddInstruction(
|
||||||
explicitLod
|
hasCompare
|
||||||
? SpirvOp.ImageSampleExplicitLod
|
? explicitLod
|
||||||
: SpirvOp.ImageSampleImplicitLod,
|
? SpirvOp.ImageSampleDrefExplicitLod
|
||||||
resource.VectorType,
|
: SpirvOp.ImageSampleDrefImplicitLod
|
||||||
|
: explicitLod
|
||||||
|
? SpirvOp.ImageSampleExplicitLod
|
||||||
|
: SpirvOp.ImageSampleImplicitLod,
|
||||||
|
hasCompare ? resource.ComponentType : resource.VectorType,
|
||||||
[.. operands]);
|
[.. operands]);
|
||||||
if (hasCompare)
|
if (hasCompare)
|
||||||
{
|
{
|
||||||
sampled = EmitManualDepthCompare(resource, sampled, reference);
|
var scalar = sampled;
|
||||||
|
sampled = _module.AddInstruction(
|
||||||
|
SpirvOp.CompositeConstruct,
|
||||||
|
resource.VectorType,
|
||||||
|
scalar,
|
||||||
|
scalar,
|
||||||
|
scalar,
|
||||||
|
resource.ComponentKind == ImageComponentKind.Float
|
||||||
|
? Float(1)
|
||||||
|
: resource.ComponentKind == ImageComponentKind.Uint
|
||||||
|
? UInt(1)
|
||||||
|
: _module.Constant(_intType, 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (instruction.Opcode.StartsWith(
|
else if (instruction.Opcode.StartsWith(
|
||||||
@@ -1703,9 +1725,6 @@ internal static partial class Gen5SpirvTranslator
|
|||||||
var start = (hasOffset ? 1 : 0) + (hasCompare ? 1 : 0);
|
var start = (hasOffset ? 1 : 0) + (hasCompare ? 1 : 0);
|
||||||
var coordinates = BuildFloatCoordinates(image, start);
|
var coordinates = BuildFloatCoordinates(image, start);
|
||||||
var offset = hasOffset ? BuildImageOffset(image, 0) : 0u;
|
var offset = hasOffset ? BuildImageOffset(image, 0) : 0u;
|
||||||
var reference = hasCompare
|
|
||||||
? Bitcast(_floatType, LoadV(image.GetAddressRegister(hasOffset ? 1 : 0)))
|
|
||||||
: 0u;
|
|
||||||
var operands = new List<uint>
|
var operands = new List<uint>
|
||||||
{
|
{
|
||||||
imageObject,
|
imageObject,
|
||||||
@@ -1713,7 +1732,10 @@ internal static partial class Gen5SpirvTranslator
|
|||||||
};
|
};
|
||||||
if (hasCompare)
|
if (hasCompare)
|
||||||
{
|
{
|
||||||
operands.Add(UInt(0));
|
operands.Add(
|
||||||
|
Bitcast(
|
||||||
|
_floatType,
|
||||||
|
LoadV(image.GetAddressRegister(hasOffset ? 1 : 0))));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -1734,28 +1756,11 @@ internal static partial class Gen5SpirvTranslator
|
|||||||
}
|
}
|
||||||
|
|
||||||
sampled = _module.AddInstruction(
|
sampled = _module.AddInstruction(
|
||||||
SpirvOp.ImageGather,
|
hasCompare
|
||||||
|
? SpirvOp.ImageDrefGather
|
||||||
|
: SpirvOp.ImageGather,
|
||||||
resource.VectorType,
|
resource.VectorType,
|
||||||
[.. operands]);
|
[.. operands]);
|
||||||
if (hasCompare)
|
|
||||||
{
|
|
||||||
var compared = new uint[4];
|
|
||||||
for (var component = 0u; component < 4; component++)
|
|
||||||
{
|
|
||||||
var texel = _module.AddInstruction(
|
|
||||||
SpirvOp.CompositeExtract,
|
|
||||||
resource.ComponentType,
|
|
||||||
sampled,
|
|
||||||
component);
|
|
||||||
compared[component] = EmitDepthCompareScalar(resource, texel, reference);
|
|
||||||
}
|
|
||||||
|
|
||||||
sampled = _module.AddInstruction(
|
|
||||||
SpirvOp.CompositeConstruct,
|
|
||||||
resource.VectorType,
|
|
||||||
compared);
|
|
||||||
}
|
|
||||||
|
|
||||||
writeAllComponents = true;
|
writeAllComponents = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -1789,67 +1794,6 @@ internal static partial class Gen5SpirvTranslator
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private uint EmitDepthCompareScalar(
|
|
||||||
SpirvImageResource resource,
|
|
||||||
uint texel,
|
|
||||||
uint reference)
|
|
||||||
{
|
|
||||||
var texelAsFloat = resource.ComponentKind switch
|
|
||||||
{
|
|
||||||
ImageComponentKind.Uint => _module.AddInstruction(
|
|
||||||
SpirvOp.ConvertUToF, _floatType, texel),
|
|
||||||
ImageComponentKind.Sint => _module.AddInstruction(
|
|
||||||
SpirvOp.ConvertSToF, _floatType, texel),
|
|
||||||
_ => texel,
|
|
||||||
};
|
|
||||||
var passes = _module.AddInstruction(
|
|
||||||
SpirvOp.FOrdLessThanEqual,
|
|
||||||
_boolType,
|
|
||||||
reference,
|
|
||||||
texelAsFloat);
|
|
||||||
return _module.AddInstruction(
|
|
||||||
SpirvOp.Select,
|
|
||||||
resource.ComponentType,
|
|
||||||
passes,
|
|
||||||
resource.ComponentKind switch
|
|
||||||
{
|
|
||||||
ImageComponentKind.Uint => UInt(1),
|
|
||||||
ImageComponentKind.Sint => _module.Constant(_intType, 1),
|
|
||||||
_ => Float(1),
|
|
||||||
},
|
|
||||||
resource.ComponentKind switch
|
|
||||||
{
|
|
||||||
ImageComponentKind.Uint => UInt(0),
|
|
||||||
ImageComponentKind.Sint => _module.Constant(_intType, 0),
|
|
||||||
_ => Float(0),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private uint EmitManualDepthCompare(
|
|
||||||
SpirvImageResource resource,
|
|
||||||
uint sampledVector,
|
|
||||||
uint reference)
|
|
||||||
{
|
|
||||||
var texel = _module.AddInstruction(
|
|
||||||
SpirvOp.CompositeExtract,
|
|
||||||
resource.ComponentType,
|
|
||||||
sampledVector,
|
|
||||||
0u);
|
|
||||||
var scalar = EmitDepthCompareScalar(resource, texel, reference);
|
|
||||||
return _module.AddInstruction(
|
|
||||||
SpirvOp.CompositeConstruct,
|
|
||||||
resource.VectorType,
|
|
||||||
scalar,
|
|
||||||
scalar,
|
|
||||||
scalar,
|
|
||||||
resource.ComponentKind switch
|
|
||||||
{
|
|
||||||
ImageComponentKind.Uint => UInt(1),
|
|
||||||
ImageComponentKind.Sint => _module.Constant(_intType, 1),
|
|
||||||
_ => Float(1),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private uint BuildFloatCoordinates(Gen5ImageControl image, int start)
|
private uint BuildFloatCoordinates(Gen5ImageControl image, int start)
|
||||||
{
|
{
|
||||||
var x = Bitcast(
|
var x = Bitcast(
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ public static class AmprExports
|
|||||||
private const uint KernelEventQueueRecordType = 2;
|
private const uint KernelEventQueueRecordType = 2;
|
||||||
private const uint WriteAddressRecordType = 3;
|
private const uint WriteAddressRecordType = 3;
|
||||||
private static readonly ConcurrentDictionary<ulong, CommandBufferState> _commandBuffers = new();
|
private static readonly ConcurrentDictionary<ulong, CommandBufferState> _commandBuffers = new();
|
||||||
private static readonly ConcurrentDictionary<string, Lazy<CachedHostFile>> _hostFileCache = new(StringComparer.OrdinalIgnoreCase);
|
|
||||||
private static readonly bool _traceAmpr =
|
private static readonly bool _traceAmpr =
|
||||||
string.Equals(Environment.GetEnvironmentVariable("SHARPEMU_LOG_AMPR"), "1", StringComparison.Ordinal);
|
string.Equals(Environment.GetEnvironmentVariable("SHARPEMU_LOG_AMPR"), "1", StringComparison.Ordinal);
|
||||||
private static readonly bool _traceAmprReads =
|
private static readonly bool _traceAmprReads =
|
||||||
@@ -38,23 +37,6 @@ public static class AmprExports
|
|||||||
public ulong WriteOffset;
|
public ulong WriteOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class CachedHostFile
|
|
||||||
{
|
|
||||||
public CachedHostFile(string path)
|
|
||||||
{
|
|
||||||
Stream = new FileStream(
|
|
||||||
path,
|
|
||||||
FileMode.Open,
|
|
||||||
FileAccess.Read,
|
|
||||||
FileShare.ReadWrite | FileShare.Delete,
|
|
||||||
bufferSize: 1024 * 1024,
|
|
||||||
FileOptions.RandomAccess);
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Gate { get; } = new();
|
|
||||||
public FileStream Stream { get; }
|
|
||||||
}
|
|
||||||
|
|
||||||
[SysAbiExport(
|
[SysAbiExport(
|
||||||
Nid = "8aI7R7WaOlc",
|
Nid = "8aI7R7WaOlc",
|
||||||
ExportName = "sceAmprCommandBufferConstructor",
|
ExportName = "sceAmprCommandBufferConstructor",
|
||||||
@@ -267,7 +249,7 @@ public static class AmprExports
|
|||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!AmprFileRegistry.TryGetHostPath(fileId, out var hostPath))
|
if (!AmprFileRegistry.TryGetHostPath(fileId, out var hostPath) || !File.Exists(hostPath))
|
||||||
{
|
{
|
||||||
TraceAmprRead(ctx, commandBuffer, fileId, destination, size, fileOffset, bytesRead: 0, hostPath, (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND);
|
TraceAmprRead(ctx, commandBuffer, fileId, destination, size, fileOffset, bytesRead: 0, hostPath, (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND);
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND;
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND;
|
||||||
@@ -652,43 +634,24 @@ public static class AmprExports
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!TryGetCachedHostFile(hostPath, out var cachedFile, out var openResult))
|
using var stream = new FileStream(
|
||||||
{
|
hostPath,
|
||||||
return openResult;
|
FileMode.Open,
|
||||||
}
|
FileAccess.Read,
|
||||||
|
FileShare.ReadWrite | FileShare.Delete,
|
||||||
long fileLength;
|
ChunkSize,
|
||||||
lock (cachedFile.Gate)
|
FileOptions.SequentialScan);
|
||||||
{
|
if (fileOffset >= (ulong)stream.Length)
|
||||||
fileLength = cachedFile.Stream.Length;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fileOffset >= (ulong)fileLength)
|
|
||||||
{
|
{
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stream.Position = unchecked((long)fileOffset);
|
||||||
|
|
||||||
while (bytesRead < size)
|
while (bytesRead < size)
|
||||||
{
|
{
|
||||||
if (bytesRead > ulong.MaxValue - fileOffset)
|
|
||||||
{
|
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
var absoluteOffset = fileOffset + bytesRead;
|
|
||||||
if (absoluteOffset > long.MaxValue)
|
|
||||||
{
|
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
var request = (int)Math.Min((ulong)buffer.Length, size - bytesRead);
|
var request = (int)Math.Min((ulong)buffer.Length, size - bytesRead);
|
||||||
int read;
|
var read = stream.Read(buffer, 0, request);
|
||||||
lock (cachedFile.Gate)
|
|
||||||
{
|
|
||||||
cachedFile.Stream.Position = unchecked((long)absoluteOffset);
|
|
||||||
read = cachedFile.Stream.Read(buffer, 0, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (read <= 0)
|
if (read <= 0)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
@@ -718,44 +681,6 @@ public static class AmprExports
|
|||||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool TryGetCachedHostFile(string hostPath, out CachedHostFile file, out int result)
|
|
||||||
{
|
|
||||||
file = null!;
|
|
||||||
result = (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
|
||||||
|
|
||||||
string cachePath;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
cachePath = Path.GetFullPath(hostPath);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
cachePath = hostPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
var lazy = _hostFileCache.GetOrAdd(
|
|
||||||
cachePath,
|
|
||||||
static path => new Lazy<CachedHostFile>(() => new CachedHostFile(path), isThreadSafe: true));
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
file = lazy.Value;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (UnauthorizedAccessException)
|
|
||||||
{
|
|
||||||
_hostFileCache.TryRemove(cachePath, out _);
|
|
||||||
result = (int)OrbisGen2Result.ORBIS_GEN2_ERROR_PERMISSION_DENIED;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
catch (IOException)
|
|
||||||
{
|
|
||||||
_hostFileCache.TryRemove(cachePath, out _);
|
|
||||||
result = (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool AppendReadFileRecord(
|
private static bool AppendReadFileRecord(
|
||||||
CpuContext ctx,
|
CpuContext ctx,
|
||||||
ulong commandBuffer,
|
ulong commandBuffer,
|
||||||
|
|||||||
@@ -2,9 +2,7 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
using SharpEmu.HLE;
|
using SharpEmu.HLE;
|
||||||
using System.Buffers;
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace SharpEmu.Libs.Audio;
|
namespace SharpEmu.Libs.Audio;
|
||||||
@@ -14,70 +12,7 @@ public static class AudioOutExports
|
|||||||
private static readonly ConcurrentDictionary<int, PortState> Ports = new();
|
private static readonly ConcurrentDictionary<int, PortState> Ports = new();
|
||||||
private static int _nextPortHandle;
|
private static int _nextPortHandle;
|
||||||
|
|
||||||
private sealed class PortState : IDisposable
|
private sealed record PortState(int UserId, int Type, uint BufferLength, uint Frequency, int Format);
|
||||||
{
|
|
||||||
private readonly object _paceGate = new();
|
|
||||||
private long _nextSilentOutput;
|
|
||||||
|
|
||||||
public PortState(
|
|
||||||
int userId,
|
|
||||||
int type,
|
|
||||||
uint bufferLength,
|
|
||||||
uint frequency,
|
|
||||||
int format,
|
|
||||||
int channels,
|
|
||||||
int bytesPerSample,
|
|
||||||
bool isFloat,
|
|
||||||
WinMmAudioPort? backend)
|
|
||||||
{
|
|
||||||
UserId = userId;
|
|
||||||
Type = type;
|
|
||||||
BufferLength = bufferLength;
|
|
||||||
Frequency = frequency;
|
|
||||||
Format = format;
|
|
||||||
Channels = channels;
|
|
||||||
BytesPerSample = bytesPerSample;
|
|
||||||
IsFloat = isFloat;
|
|
||||||
Backend = backend;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int UserId { get; }
|
|
||||||
public int Type { get; }
|
|
||||||
public uint BufferLength { get; }
|
|
||||||
public uint Frequency { get; }
|
|
||||||
public int Format { get; }
|
|
||||||
public int Channels { get; }
|
|
||||||
public int BytesPerSample { get; }
|
|
||||||
public bool IsFloat { get; }
|
|
||||||
public WinMmAudioPort? Backend { get; }
|
|
||||||
public int BufferByteLength =>
|
|
||||||
checked((int)BufferLength * Channels * BytesPerSample);
|
|
||||||
|
|
||||||
public void PaceSilence()
|
|
||||||
{
|
|
||||||
long delay;
|
|
||||||
lock (_paceGate)
|
|
||||||
{
|
|
||||||
var now = Stopwatch.GetTimestamp();
|
|
||||||
if (_nextSilentOutput < now)
|
|
||||||
{
|
|
||||||
_nextSilentOutput = now;
|
|
||||||
}
|
|
||||||
|
|
||||||
delay = _nextSilentOutput - now;
|
|
||||||
_nextSilentOutput += checked(
|
|
||||||
(long)Math.Ceiling(
|
|
||||||
Stopwatch.Frequency * (double)BufferLength / Frequency));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delay > 0)
|
|
||||||
{
|
|
||||||
Thread.Sleep(TimeSpan.FromSeconds((double)delay / Stopwatch.Frequency));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() => Backend?.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
[SysAbiExport(
|
[SysAbiExport(
|
||||||
Nid = "JfEPXVxhFqA",
|
Nid = "JfEPXVxhFqA",
|
||||||
@@ -98,41 +33,13 @@ public static class AudioOutExports
|
|||||||
var bufferLength = unchecked((uint)ctx[CpuRegister.Rcx]);
|
var bufferLength = unchecked((uint)ctx[CpuRegister.Rcx]);
|
||||||
var frequency = unchecked((uint)ctx[CpuRegister.R8]);
|
var frequency = unchecked((uint)ctx[CpuRegister.R8]);
|
||||||
var format = unchecked((int)ctx[CpuRegister.R9]);
|
var format = unchecked((int)ctx[CpuRegister.R9]);
|
||||||
if (bufferLength == 0 || frequency == 0 ||
|
if (bufferLength == 0 || frequency == 0)
|
||||||
!TryGetFormat(format, out var channels, out var bytesPerSample, out var isFloat))
|
|
||||||
{
|
{
|
||||||
return SetReturn(ctx, (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT);
|
return SetReturn(ctx, (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
WinMmAudioPort? backend = null;
|
|
||||||
string backendName;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
backend = new WinMmAudioPort(frequency);
|
|
||||||
backendName = "winmm";
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
backendName = "silent";
|
|
||||||
Console.Error.WriteLine(
|
|
||||||
$"[LOADER][WARN] AudioOut host backend unavailable: {exception.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var handle = Interlocked.Increment(ref _nextPortHandle);
|
var handle = Interlocked.Increment(ref _nextPortHandle);
|
||||||
Ports[handle] = new PortState(
|
Ports[handle] = new PortState(userId, type, bufferLength, frequency, format);
|
||||||
userId,
|
|
||||||
type,
|
|
||||||
bufferLength,
|
|
||||||
frequency,
|
|
||||||
format,
|
|
||||||
channels,
|
|
||||||
bytesPerSample,
|
|
||||||
isFloat,
|
|
||||||
backend);
|
|
||||||
Console.Error.WriteLine(
|
|
||||||
$"[LOADER][INFO] AudioOut port {handle}: {frequency} Hz, " +
|
|
||||||
$"{channels} ch, {(isFloat ? "float32" : "s16")}, " +
|
|
||||||
$"{bufferLength} frames, backend={backendName}");
|
|
||||||
return SetReturn(ctx, handle);
|
return SetReturn(ctx, handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,13 +51,11 @@ public static class AudioOutExports
|
|||||||
public static int AudioOutClose(CpuContext ctx)
|
public static int AudioOutClose(CpuContext ctx)
|
||||||
{
|
{
|
||||||
var handle = unchecked((int)ctx[CpuRegister.Rdi]);
|
var handle = unchecked((int)ctx[CpuRegister.Rdi]);
|
||||||
if (!Ports.TryRemove(handle, out var port))
|
return SetReturn(
|
||||||
{
|
ctx,
|
||||||
return SetReturn(ctx, (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT);
|
Ports.TryRemove(handle, out _)
|
||||||
}
|
? 0
|
||||||
|
: (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT);
|
||||||
port.Dispose();
|
|
||||||
return SetReturn(ctx, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[SysAbiExport(
|
[SysAbiExport(
|
||||||
@@ -160,44 +65,8 @@ public static class AudioOutExports
|
|||||||
LibraryName = "libSceAudioOut")]
|
LibraryName = "libSceAudioOut")]
|
||||||
public static int AudioOutOutput(CpuContext ctx)
|
public static int AudioOutOutput(CpuContext ctx)
|
||||||
{
|
{
|
||||||
var handle = unchecked((int)ctx[CpuRegister.Rdi]);
|
ctx[CpuRegister.Rax] = 0;
|
||||||
var sourceAddress = ctx[CpuRegister.Rsi];
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||||
if (!Ports.TryGetValue(handle, out var port))
|
|
||||||
{
|
|
||||||
return SetReturn(ctx, (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sourceAddress == 0)
|
|
||||||
{
|
|
||||||
return SetReturn(ctx, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
var buffer = ArrayPool<byte>.Shared.Rent(port.BufferByteLength);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var source = buffer.AsSpan(0, port.BufferByteLength);
|
|
||||||
if (!ctx.Memory.TryRead(sourceAddress, source))
|
|
||||||
{
|
|
||||||
return SetReturn(ctx, (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (port.Backend is null ||
|
|
||||||
!port.Backend.Submit(
|
|
||||||
source,
|
|
||||||
port.BufferLength,
|
|
||||||
port.Channels,
|
|
||||||
port.BytesPerSample,
|
|
||||||
port.IsFloat))
|
|
||||||
{
|
|
||||||
port.PaceSilence();
|
|
||||||
}
|
|
||||||
|
|
||||||
return SetReturn(ctx, 0);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
ArrayPool<byte>.Shared.Return(buffer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[SysAbiExport(
|
[SysAbiExport(
|
||||||
@@ -220,23 +89,4 @@ public static class AudioOutExports
|
|||||||
ctx[CpuRegister.Rax] = unchecked((ulong)result);
|
ctx[CpuRegister.Rax] = unchecked((ulong)result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool TryGetFormat(
|
|
||||||
int rawFormat,
|
|
||||||
out int channels,
|
|
||||||
out int bytesPerSample,
|
|
||||||
out bool isFloat)
|
|
||||||
{
|
|
||||||
var format = rawFormat & 0xFF;
|
|
||||||
channels = format switch
|
|
||||||
{
|
|
||||||
0 or 3 => 1,
|
|
||||||
1 or 4 => 2,
|
|
||||||
2 or 5 or 6 or 7 => 8,
|
|
||||||
_ => 0,
|
|
||||||
};
|
|
||||||
bytesPerSample = format is >= 3 and <= 5 or 7 ? 4 : 2;
|
|
||||||
isFloat = bytesPerSample == 4;
|
|
||||||
return channels != 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,302 +0,0 @@
|
|||||||
// Copyright (C) 2026 SharpEmu Emulator Project
|
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
||||||
|
|
||||||
using System.Buffers;
|
|
||||||
using System.Buffers.Binary;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace SharpEmu.Libs.Audio;
|
|
||||||
|
|
||||||
internal sealed class WinMmAudioPort : IDisposable
|
|
||||||
{
|
|
||||||
private const uint WaveMapper = uint.MaxValue;
|
|
||||||
private const uint CallbackEvent = 0x0005_0000;
|
|
||||||
private const ushort WaveFormatPcm = 1;
|
|
||||||
private const uint WaveHeaderDone = 0x0000_0001;
|
|
||||||
private const int MaximumQueuedPcmBytes = 32 * 1024;
|
|
||||||
|
|
||||||
private readonly object _gate = new();
|
|
||||||
private readonly AutoResetEvent _completion = new(false);
|
|
||||||
private readonly Queue<NativeBuffer> _buffers = new();
|
|
||||||
private IntPtr _device;
|
|
||||||
private int _queuedPcmBytes;
|
|
||||||
private bool _disposed;
|
|
||||||
|
|
||||||
public WinMmAudioPort(uint sampleRate)
|
|
||||||
{
|
|
||||||
if (!OperatingSystem.IsWindows())
|
|
||||||
{
|
|
||||||
throw new PlatformNotSupportedException("WinMM audio is only available on Windows.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var format = new WaveFormat
|
|
||||||
{
|
|
||||||
FormatTag = WaveFormatPcm,
|
|
||||||
Channels = 2,
|
|
||||||
SamplesPerSecond = sampleRate,
|
|
||||||
AverageBytesPerSecond = checked(sampleRate * 4),
|
|
||||||
BlockAlign = 4,
|
|
||||||
BitsPerSample = 16,
|
|
||||||
ExtraSize = 0,
|
|
||||||
};
|
|
||||||
var result = WaveOutOpen(
|
|
||||||
out _device,
|
|
||||||
WaveMapper,
|
|
||||||
ref format,
|
|
||||||
_completion.SafeWaitHandle.DangerousGetHandle(),
|
|
||||||
IntPtr.Zero,
|
|
||||||
CallbackEvent);
|
|
||||||
if (result != 0)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"waveOutOpen failed with MMRESULT {result}.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Submit(
|
|
||||||
ReadOnlySpan<byte> source,
|
|
||||||
uint frames,
|
|
||||||
int channels,
|
|
||||||
int bytesPerSample,
|
|
||||||
bool isFloat)
|
|
||||||
{
|
|
||||||
lock (_gate)
|
|
||||||
{
|
|
||||||
if (_disposed)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var outputLength = checked((int)frames * 4);
|
|
||||||
ReapCompletedBuffers();
|
|
||||||
while (_queuedPcmBytes != 0 &&
|
|
||||||
_queuedPcmBytes + outputLength > MaximumQueuedPcmBytes)
|
|
||||||
{
|
|
||||||
if (!_completion.WaitOne(TimeSpan.FromSeconds(1)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReapCompletedBuffers();
|
|
||||||
}
|
|
||||||
|
|
||||||
var output = ArrayPool<byte>.Shared.Rent(outputLength);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ConvertToStereoPcm16(
|
|
||||||
source,
|
|
||||||
output.AsSpan(0, outputLength),
|
|
||||||
checked((int)frames),
|
|
||||||
channels,
|
|
||||||
bytesPerSample,
|
|
||||||
isFloat);
|
|
||||||
return QueueBuffer(output.AsSpan(0, outputLength));
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
ArrayPool<byte>.Shared.Return(output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
lock (_gate)
|
|
||||||
{
|
|
||||||
if (_disposed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_disposed = true;
|
|
||||||
if (_device != IntPtr.Zero)
|
|
||||||
{
|
|
||||||
WaveOutReset(_device);
|
|
||||||
while (_buffers.TryDequeue(out var buffer))
|
|
||||||
{
|
|
||||||
ReleaseBuffer(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
WaveOutClose(_device);
|
|
||||||
_device = IntPtr.Zero;
|
|
||||||
}
|
|
||||||
|
|
||||||
_completion.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool QueueBuffer(ReadOnlySpan<byte> data)
|
|
||||||
{
|
|
||||||
var dataAddress = Marshal.AllocHGlobal(data.Length);
|
|
||||||
var headerAddress = IntPtr.Zero;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
data.CopyTo(new Span<byte>((void*)dataAddress, data.Length));
|
|
||||||
}
|
|
||||||
|
|
||||||
var header = new WaveHeader
|
|
||||||
{
|
|
||||||
Data = dataAddress,
|
|
||||||
BufferLength = checked((uint)data.Length),
|
|
||||||
};
|
|
||||||
headerAddress = Marshal.AllocHGlobal(Marshal.SizeOf<WaveHeader>());
|
|
||||||
Marshal.StructureToPtr(header, headerAddress, false);
|
|
||||||
|
|
||||||
var result = WaveOutPrepareHeader(
|
|
||||||
_device,
|
|
||||||
headerAddress,
|
|
||||||
checked((uint)Marshal.SizeOf<WaveHeader>()));
|
|
||||||
if (result != 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = WaveOutWrite(
|
|
||||||
_device,
|
|
||||||
headerAddress,
|
|
||||||
checked((uint)Marshal.SizeOf<WaveHeader>()));
|
|
||||||
if (result != 0)
|
|
||||||
{
|
|
||||||
WaveOutUnprepareHeader(
|
|
||||||
_device,
|
|
||||||
headerAddress,
|
|
||||||
checked((uint)Marshal.SizeOf<WaveHeader>()));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_buffers.Enqueue(new NativeBuffer(dataAddress, headerAddress, data.Length));
|
|
||||||
_queuedPcmBytes += data.Length;
|
|
||||||
dataAddress = IntPtr.Zero;
|
|
||||||
headerAddress = IntPtr.Zero;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (headerAddress != IntPtr.Zero)
|
|
||||||
{
|
|
||||||
Marshal.FreeHGlobal(headerAddress);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dataAddress != IntPtr.Zero)
|
|
||||||
{
|
|
||||||
Marshal.FreeHGlobal(dataAddress);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ReapCompletedBuffers()
|
|
||||||
{
|
|
||||||
while (_buffers.TryPeek(out var buffer))
|
|
||||||
{
|
|
||||||
var header = Marshal.PtrToStructure<WaveHeader>(buffer.Header);
|
|
||||||
if ((header.Flags & WaveHeaderDone) == 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_buffers.Dequeue();
|
|
||||||
ReleaseBuffer(buffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ReleaseBuffer(NativeBuffer buffer)
|
|
||||||
{
|
|
||||||
WaveOutUnprepareHeader(
|
|
||||||
_device,
|
|
||||||
buffer.Header,
|
|
||||||
checked((uint)Marshal.SizeOf<WaveHeader>()));
|
|
||||||
_queuedPcmBytes -= buffer.Length;
|
|
||||||
Marshal.FreeHGlobal(buffer.Header);
|
|
||||||
Marshal.FreeHGlobal(buffer.Data);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void ConvertToStereoPcm16(
|
|
||||||
ReadOnlySpan<byte> source,
|
|
||||||
Span<byte> destination,
|
|
||||||
int frames,
|
|
||||||
int channels,
|
|
||||||
int bytesPerSample,
|
|
||||||
bool isFloat)
|
|
||||||
{
|
|
||||||
var sourceFrameSize = checked(channels * bytesPerSample);
|
|
||||||
for (var frame = 0; frame < frames; frame++)
|
|
||||||
{
|
|
||||||
var sourceFrame = source.Slice(frame * sourceFrameSize, sourceFrameSize);
|
|
||||||
var left = ReadSample(sourceFrame, 0, bytesPerSample, isFloat);
|
|
||||||
var right = channels == 1
|
|
||||||
? left
|
|
||||||
: ReadSample(sourceFrame, 1, bytesPerSample, isFloat);
|
|
||||||
BinaryPrimitives.WriteInt16LittleEndian(destination[(frame * 4)..], left);
|
|
||||||
BinaryPrimitives.WriteInt16LittleEndian(destination[((frame * 4) + 2)..], right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static short ReadSample(
|
|
||||||
ReadOnlySpan<byte> frame,
|
|
||||||
int channel,
|
|
||||||
int bytesPerSample,
|
|
||||||
bool isFloat)
|
|
||||||
{
|
|
||||||
var sample = frame.Slice(channel * bytesPerSample, bytesPerSample);
|
|
||||||
if (!isFloat)
|
|
||||||
{
|
|
||||||
return BinaryPrimitives.ReadInt16LittleEndian(sample);
|
|
||||||
}
|
|
||||||
|
|
||||||
var bits = BinaryPrimitives.ReadInt32LittleEndian(sample);
|
|
||||||
var value = Math.Clamp(BitConverter.Int32BitsToSingle(bits), -1.0f, 1.0f);
|
|
||||||
return checked((short)MathF.Round(value * short.MaxValue));
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly record struct NativeBuffer(IntPtr Data, IntPtr Header, int Length);
|
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential, Pack = 2)]
|
|
||||||
private struct WaveFormat
|
|
||||||
{
|
|
||||||
public ushort FormatTag;
|
|
||||||
public ushort Channels;
|
|
||||||
public uint SamplesPerSecond;
|
|
||||||
public uint AverageBytesPerSecond;
|
|
||||||
public ushort BlockAlign;
|
|
||||||
public ushort BitsPerSample;
|
|
||||||
public ushort ExtraSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
|
||||||
private struct WaveHeader
|
|
||||||
{
|
|
||||||
public IntPtr Data;
|
|
||||||
public uint BufferLength;
|
|
||||||
public uint BytesRecorded;
|
|
||||||
public nuint User;
|
|
||||||
public uint Flags;
|
|
||||||
public uint Loops;
|
|
||||||
public IntPtr Next;
|
|
||||||
public nuint Reserved;
|
|
||||||
}
|
|
||||||
|
|
||||||
[DllImport("winmm.dll", EntryPoint = "waveOutOpen")]
|
|
||||||
private static extern uint WaveOutOpen(
|
|
||||||
out IntPtr device,
|
|
||||||
uint deviceId,
|
|
||||||
ref WaveFormat format,
|
|
||||||
IntPtr callback,
|
|
||||||
IntPtr instance,
|
|
||||||
uint flags);
|
|
||||||
|
|
||||||
[DllImport("winmm.dll", EntryPoint = "waveOutPrepareHeader")]
|
|
||||||
private static extern uint WaveOutPrepareHeader(IntPtr device, IntPtr header, uint headerSize);
|
|
||||||
|
|
||||||
[DllImport("winmm.dll", EntryPoint = "waveOutWrite")]
|
|
||||||
private static extern uint WaveOutWrite(IntPtr device, IntPtr header, uint headerSize);
|
|
||||||
|
|
||||||
[DllImport("winmm.dll", EntryPoint = "waveOutUnprepareHeader")]
|
|
||||||
private static extern uint WaveOutUnprepareHeader(IntPtr device, IntPtr header, uint headerSize);
|
|
||||||
|
|
||||||
[DllImport("winmm.dll", EntryPoint = "waveOutReset")]
|
|
||||||
private static extern uint WaveOutReset(IntPtr device);
|
|
||||||
|
|
||||||
[DllImport("winmm.dll", EntryPoint = "waveOutClose")]
|
|
||||||
private static extern uint WaveOutClose(IntPtr device);
|
|
||||||
}
|
|
||||||
@@ -276,22 +276,6 @@ public static class KernelEventQueueCompatExports
|
|||||||
var outCountAddress = ctx[CpuRegister.Rcx];
|
var outCountAddress = ctx[CpuRegister.Rcx];
|
||||||
var timeoutAddress = ctx[CpuRegister.R8];
|
var timeoutAddress = ctx[CpuRegister.R8];
|
||||||
|
|
||||||
if (!IsValidEqueue(handle))
|
|
||||||
{
|
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (eventsAddress == 0 || eventCapacity < 1)
|
|
||||||
{
|
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint timeoutUsec = 0;
|
|
||||||
if (timeoutAddress != 0 && !TryReadUInt32(ctx, timeoutAddress, out timeoutUsec))
|
|
||||||
{
|
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
var deliveredCount = DequeueEvents(ctx, handle, eventsAddress, eventCapacity);
|
var deliveredCount = DequeueEvents(ctx, handle, eventsAddress, eventCapacity);
|
||||||
if (outCountAddress != 0 && !TryWriteUInt32(ctx, outCountAddress, (uint)deliveredCount))
|
if (outCountAddress != 0 && !TryWriteUInt32(ctx, outCountAddress, (uint)deliveredCount))
|
||||||
{
|
{
|
||||||
@@ -316,41 +300,6 @@ public static class KernelEventQueueCompatExports
|
|||||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeoutAddress != 0 && ctx.TryReadUInt64(timeoutAddress, out var timeoutRaw))
|
|
||||||
{
|
|
||||||
var timeoutMicros = timeoutRaw & 0xFFFF_FFFFUL;
|
|
||||||
var deadline = Environment.TickCount64 +
|
|
||||||
Math.Max(1L, (long)Math.Min(timeoutMicros / 1000, int.MaxValue));
|
|
||||||
lock (_eventQueueGate)
|
|
||||||
{
|
|
||||||
while (!HasPendingEvents(handle))
|
|
||||||
{
|
|
||||||
var remaining = deadline - Environment.TickCount64;
|
|
||||||
if (remaining <= 0)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
Monitor.Wait(_eventQueueGate, (int)Math.Min(remaining, 100));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deliveredCount = DequeueEvents(ctx, handle, eventsAddress, eventCapacity);
|
|
||||||
if (outCountAddress != 0 && !TryWriteUInt32(ctx, outCountAddress, (uint)deliveredCount))
|
|
||||||
{
|
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (deliveredCount > 0)
|
|
||||||
{
|
|
||||||
TraceEventQueue(ctx, "wait-timed-deliver", handle);
|
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
TraceEventQueue(ctx, "wait-timeout", handle);
|
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_TIMED_OUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
TraceEventQueue(ctx, "wait", handle);
|
TraceEventQueue(ctx, "wait", handle);
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||||
}
|
}
|
||||||
@@ -381,7 +330,6 @@ public static class KernelEventQueueCompatExports
|
|||||||
|
|
||||||
queue.AddLast(queuedEvent);
|
queue.AddLast(queuedEvent);
|
||||||
queued = true;
|
queued = true;
|
||||||
Monitor.PulseAll(_eventQueueGate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (queued)
|
if (queued)
|
||||||
@@ -546,9 +494,7 @@ public static class KernelEventQueueCompatExports
|
|||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_MEMORY_FAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
return deliveredCount > 0
|
return (int)OrbisGen2Result.ORBIS_GEN2_OK;
|
||||||
? (int)OrbisGen2Result.ORBIS_GEN2_OK
|
|
||||||
: (int)OrbisGen2Result.ORBIS_GEN2_ERROR_TIMED_OUT;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool HasPendingEvents(ulong handle)
|
private static bool HasPendingEvents(ulong handle)
|
||||||
@@ -666,17 +612,4 @@ public static class KernelEventQueueCompatExports
|
|||||||
BinaryPrimitives.WriteUInt32LittleEndian(buffer, value);
|
BinaryPrimitives.WriteUInt32LittleEndian(buffer, value);
|
||||||
return ctx.Memory.TryWrite(address, buffer);
|
return ctx.Memory.TryWrite(address, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool TryReadUInt32(CpuContext ctx, ulong address, out uint value)
|
|
||||||
{
|
|
||||||
Span<byte> buffer = stackalloc byte[sizeof(uint)];
|
|
||||||
if (!ctx.Memory.TryRead(address, buffer))
|
|
||||||
{
|
|
||||||
value = 0;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
value = BinaryPrimitives.ReadUInt32LittleEndian(buffer);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ using SharpEmu.HLE;
|
|||||||
using SharpEmu.Libs.Ampr;
|
using SharpEmu.Libs.Ampr;
|
||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.Buffers.Binary;
|
using System.Buffers.Binary;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
@@ -108,7 +108,6 @@ public static class KernelMemoryCompatExports
|
|||||||
private static readonly Dictionary<string, string> _guestMounts = new(StringComparer.OrdinalIgnoreCase);
|
private static readonly Dictionary<string, string> _guestMounts = new(StringComparer.OrdinalIgnoreCase);
|
||||||
private static readonly HashSet<string> _tracedStatResults = new(StringComparer.Ordinal);
|
private static readonly HashSet<string> _tracedStatResults = new(StringComparer.Ordinal);
|
||||||
private static readonly HashSet<string> _negativeStatCache = new(StringComparer.OrdinalIgnoreCase);
|
private static readonly HashSet<string> _negativeStatCache = new(StringComparer.OrdinalIgnoreCase);
|
||||||
private static readonly ConcurrentDictionary<string, ulong> _aprFileSizeCache = new(StringComparer.OrdinalIgnoreCase);
|
|
||||||
private static long _nextFileDescriptor = 2;
|
private static long _nextFileDescriptor = 2;
|
||||||
private static ulong _nextPhysicalAddress;
|
private static ulong _nextPhysicalAddress;
|
||||||
private static ulong _nextVirtualAddress;
|
private static ulong _nextVirtualAddress;
|
||||||
@@ -1343,7 +1342,6 @@ public static class KernelMemoryCompatExports
|
|||||||
if (IsMutatingOpen(flags))
|
if (IsMutatingOpen(flags))
|
||||||
{
|
{
|
||||||
InvalidateNegativeStatCacheForPathAndAncestors(guestPath);
|
InvalidateNegativeStatCacheForPathAndAncestors(guestPath);
|
||||||
InvalidateAprFileSizeCache(hostPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LogOpenTrace($"_open file path='{guestPath}' host='{hostPath}' flags=0x{flags:X8} fd={fd}");
|
LogOpenTrace($"_open file path='{guestPath}' host='{hostPath}' flags=0x{flags:X8} fd={fd}");
|
||||||
@@ -1552,7 +1550,6 @@ public static class KernelMemoryCompatExports
|
|||||||
|
|
||||||
File.Delete(hostPath);
|
File.Delete(hostPath);
|
||||||
InvalidateNegativeStatCacheForPathAndAncestors(guestPath);
|
InvalidateNegativeStatCacheForPathAndAncestors(guestPath);
|
||||||
InvalidateAprFileSizeCache(hostPath);
|
|
||||||
AddNegativeStatCacheForGuestPath(guestPath);
|
AddNegativeStatCacheForGuestPath(guestPath);
|
||||||
LogOpenTrace($"unlink path='{guestPath}' host='{hostPath}'");
|
LogOpenTrace($"unlink path='{guestPath}' host='{hostPath}'");
|
||||||
ctx[CpuRegister.Rax] = 0;
|
ctx[CpuRegister.Rax] = 0;
|
||||||
@@ -2599,11 +2596,8 @@ public static class KernelMemoryCompatExports
|
|||||||
var length = ctx[CpuRegister.Rsi];
|
var length = ctx[CpuRegister.Rsi];
|
||||||
var protection = unchecked((int)ctx[CpuRegister.Rdx]);
|
var protection = unchecked((int)ctx[CpuRegister.Rdx]);
|
||||||
var flags = ctx[CpuRegister.Rcx];
|
var flags = ctx[CpuRegister.Rcx];
|
||||||
if (ShouldTraceDirectMemory())
|
Console.Error.WriteLine(
|
||||||
{
|
$"[LOADER][TRACE] map_flexible: inout=0x{inOutAddressPointer:X16} len=0x{length:X16} prot=0x{protection:X8} flags=0x{flags:X16}");
|
||||||
Console.Error.WriteLine(
|
|
||||||
$"[LOADER][TRACE] map_flexible: inout=0x{inOutAddressPointer:X16} len=0x{length:X16} prot=0x{protection:X8} flags=0x{flags:X16}");
|
|
||||||
}
|
|
||||||
if (inOutAddressPointer == 0 || length == 0)
|
if (inOutAddressPointer == 0 || length == 0)
|
||||||
{
|
{
|
||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_INVALID_ARGUMENT;
|
||||||
@@ -2633,11 +2627,8 @@ public static class KernelMemoryCompatExports
|
|||||||
: AllocateMappedGuestAddress(ctx, length, 0x1000UL);
|
: AllocateMappedGuestAddress(ctx, length, 0x1000UL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ShouldTraceDirectMemory())
|
Console.Error.WriteLine(
|
||||||
{
|
$"[LOADER][TRACE] map_flexible reserve: requested=0x{requestedAddress:X16} desired=0x{desiredAddress:X16} mapped=0x{mappedAddress:X16}");
|
||||||
Console.Error.WriteLine(
|
|
||||||
$"[LOADER][TRACE] map_flexible reserve: requested=0x{requestedAddress:X16} desired=0x{desiredAddress:X16} mapped=0x{mappedAddress:X16}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mappedAddress == 0)
|
if (mappedAddress == 0)
|
||||||
{
|
{
|
||||||
@@ -3982,17 +3973,118 @@ public static class KernelMemoryCompatExports
|
|||||||
ulong alignment,
|
ulong alignment,
|
||||||
out ulong mappedAddress)
|
out ulong mappedAddress)
|
||||||
{
|
{
|
||||||
var executable = (protection & OrbisProtCpuExec) != 0;
|
mappedAddress = 0;
|
||||||
return KernelVirtualRangeAllocator.TryReserve(
|
if (length == 0)
|
||||||
ctx,
|
{
|
||||||
desiredAddress,
|
return false;
|
||||||
length,
|
}
|
||||||
executable,
|
|
||||||
alignment,
|
try
|
||||||
allowSearch: true,
|
{
|
||||||
allowAllocateAtAlternative: false,
|
object memoryObject = ctx.Memory;
|
||||||
"reserve range",
|
MethodInfo? allocateAt = null;
|
||||||
out mappedAddress);
|
MethodInfo? allocateAtOrAbove = null;
|
||||||
|
var allocateAtHasAllowAlternativeArg = false;
|
||||||
|
for (var depth = 0; depth < 4; depth++)
|
||||||
|
{
|
||||||
|
foreach (var candidate in memoryObject.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))
|
||||||
|
{
|
||||||
|
var parameters = candidate.GetParameters();
|
||||||
|
if (string.Equals(candidate.Name, "TryAllocateAtOrAbove", StringComparison.Ordinal) &&
|
||||||
|
parameters.Length == 5 &&
|
||||||
|
parameters[0].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[1].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[2].ParameterType == typeof(bool) &&
|
||||||
|
parameters[3].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[4].ParameterType == typeof(ulong).MakeByRefType())
|
||||||
|
{
|
||||||
|
allocateAtOrAbove = candidate;
|
||||||
|
}
|
||||||
|
else if (string.Equals(candidate.Name, "AllocateAt", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
if (parameters.Length == 3 &&
|
||||||
|
parameters[0].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[1].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[2].ParameterType == typeof(bool))
|
||||||
|
{
|
||||||
|
allocateAt = candidate;
|
||||||
|
allocateAtHasAllowAlternativeArg = false;
|
||||||
|
}
|
||||||
|
else if (parameters.Length == 4 &&
|
||||||
|
parameters[0].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[1].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[2].ParameterType == typeof(bool) &&
|
||||||
|
parameters[3].ParameterType == typeof(bool))
|
||||||
|
{
|
||||||
|
allocateAt = candidate;
|
||||||
|
allocateAtHasAllowAlternativeArg = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allocateAtOrAbove is not null && allocateAt is not null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allocateAtOrAbove is not null || allocateAt is not null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var innerProperty = memoryObject.GetType().GetProperty("Inner", BindingFlags.Public | BindingFlags.Instance);
|
||||||
|
if (innerProperty is null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var innerValue = innerProperty.GetValue(memoryObject);
|
||||||
|
if (innerValue is null || ReferenceEquals(innerValue, memoryObject))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryObject = innerValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var executable = (protection & OrbisProtCpuExec) != 0;
|
||||||
|
if (allocateAtOrAbove is not null)
|
||||||
|
{
|
||||||
|
var searchArgs = new object[] { desiredAddress, length, executable, alignment, 0UL };
|
||||||
|
var searchResult = allocateAtOrAbove.Invoke(memoryObject, searchArgs);
|
||||||
|
if (searchResult is bool trueValue && trueValue &&
|
||||||
|
searchArgs[4] is ulong searchedAddress && searchedAddress != 0)
|
||||||
|
{
|
||||||
|
mappedAddress = searchedAddress;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allocateAt is null)
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine($"[LOADER][TRACE] reserve range: AllocateAt missing on {ctx.Memory.GetType().FullName}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var invokeArgs = allocateAtHasAllowAlternativeArg
|
||||||
|
? new object[] { desiredAddress, length, executable, false }
|
||||||
|
: new object[] { desiredAddress, length, executable };
|
||||||
|
var result = allocateAt.Invoke(memoryObject, invokeArgs);
|
||||||
|
if (result is not ulong allocated || allocated == 0)
|
||||||
|
{
|
||||||
|
var resultType = result?.GetType().FullName ?? "null";
|
||||||
|
Console.Error.WriteLine($"[LOADER][TRACE] reserve range: AllocateAt returned {resultType} value={result ?? "null"}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mappedAddress = allocated;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("[LOADER][TRACE] reserve range threw while invoking AllocateAt");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsMappedGuestRangeAvailable(
|
private static bool IsMappedGuestRangeAvailable(
|
||||||
@@ -4145,20 +4237,6 @@ public static class KernelMemoryCompatExports
|
|||||||
var app0Root = ResolveApp0Root();
|
var app0Root = ResolveApp0Root();
|
||||||
if (!string.IsNullOrWhiteSpace(app0Root))
|
if (!string.IsNullOrWhiteSpace(app0Root))
|
||||||
{
|
{
|
||||||
if (string.Equals(guestPath, "$", StringComparison.Ordinal) ||
|
|
||||||
string.Equals(guestPath, "$/", StringComparison.Ordinal) ||
|
|
||||||
string.Equals(guestPath, "$\\", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
return app0Root;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (guestPath.StartsWith("$/", StringComparison.Ordinal) ||
|
|
||||||
guestPath.StartsWith("$\\", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
var relative = NormalizeMountRelativePath(guestPath[2..]);
|
|
||||||
return Path.Combine(app0Root, relative);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.Equals(guestPath, "/app0", StringComparison.OrdinalIgnoreCase) ||
|
if (string.Equals(guestPath, "/app0", StringComparison.OrdinalIgnoreCase) ||
|
||||||
string.Equals(guestPath, "app0", StringComparison.OrdinalIgnoreCase))
|
string.Equals(guestPath, "app0", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
@@ -5821,40 +5899,22 @@ public static class KernelMemoryCompatExports
|
|||||||
private static bool TryGetAprFileSize(string hostPath, out ulong size)
|
private static bool TryGetAprFileSize(string hostPath, out ulong size)
|
||||||
{
|
{
|
||||||
size = 0;
|
size = 0;
|
||||||
|
|
||||||
string cachePath;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cachePath = Path.GetFullPath(hostPath);
|
var fileInfo = new FileInfo(hostPath);
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
cachePath = hostPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_aprFileSizeCache.TryGetValue(cachePath, out size))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var fileInfo = new FileInfo(cachePath);
|
|
||||||
if (fileInfo.Exists)
|
if (fileInfo.Exists)
|
||||||
{
|
{
|
||||||
var length = fileInfo.Length;
|
var length = fileInfo.Length;
|
||||||
size = length < 0 ? 0UL : unchecked((ulong)length);
|
size = length < 0 ? 0UL : unchecked((ulong)length);
|
||||||
_aprFileSizeCache.TryAdd(cachePath, size);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!new DirectoryInfo(cachePath).Exists)
|
if (!new DirectoryInfo(hostPath).Exists)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size = 65536;
|
size = 65536;
|
||||||
_aprFileSizeCache.TryAdd(cachePath, size);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@@ -6153,20 +6213,6 @@ public static class KernelMemoryCompatExports
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void InvalidateAprFileSizeCache(string hostPath)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
hostPath = Path.GetFullPath(hostPath);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// The cache key remains the original path when normalization fails.
|
|
||||||
}
|
|
||||||
|
|
||||||
_aprFileSizeCache.TryRemove(hostPath, out _);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string PreviewIoBytes(byte[] buffer, int count, int maxBytes)
|
private static string PreviewIoBytes(byte[] buffer, int count, int maxBytes)
|
||||||
{
|
{
|
||||||
if (count <= 0)
|
if (count <= 0)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ using System.Buffers.Binary;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Runtime.Intrinsics.X86;
|
using System.Runtime.Intrinsics.X86;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
@@ -86,11 +87,11 @@ public static class KernelRuntimeCompatExports
|
|||||||
|
|
||||||
if (micros < 1000)
|
if (micros < 1000)
|
||||||
{
|
{
|
||||||
// Guest worker pools use usleep(1) as a polling backoff. Do not turn
|
// Guest worker pools use usleep(1) as a polling backoff. Periodically
|
||||||
// PS microsecond waits into Windows millisecond sleeps on hot paths.
|
// relinquish a full host time slice so spin workers cannot starve producers.
|
||||||
if ((++_shortUsleepCount & 255) == 0)
|
if ((++_shortUsleepCount & 31) == 0)
|
||||||
{
|
{
|
||||||
Thread.Sleep(0);
|
Thread.Sleep(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -738,11 +739,8 @@ public static class KernelRuntimeCompatExports
|
|||||||
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND;
|
return (int)OrbisGen2Result.ORBIS_GEN2_ERROR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ShouldTraceVirtualMemory())
|
Console.Error.WriteLine(
|
||||||
{
|
$"[LOADER][TRACE] reserve_virtual_range: req=0x{requestedAddress:X16} desired=0x{desiredAddress:X16} mapped=0x{mappedAddress:X16} len=0x{length:X16} flags=0x{flags:X8} align=0x{effectiveAlignment:X16}");
|
||||||
Console.Error.WriteLine(
|
|
||||||
$"[LOADER][TRACE] reserve_virtual_range: req=0x{requestedAddress:X16} desired=0x{desiredAddress:X16} mapped=0x{mappedAddress:X16} len=0x{length:X16} flags=0x{flags:X8} align=0x{effectiveAlignment:X16}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ctx.TryWriteUInt64(inOutAddressPointer, mappedAddress))
|
if (!ctx.TryWriteUInt64(inOutAddressPointer, mappedAddress))
|
||||||
{
|
{
|
||||||
@@ -1735,16 +1733,117 @@ public static class KernelRuntimeCompatExports
|
|||||||
bool allowSearch,
|
bool allowSearch,
|
||||||
out ulong mappedAddress)
|
out ulong mappedAddress)
|
||||||
{
|
{
|
||||||
return KernelVirtualRangeAllocator.TryReserve(
|
mappedAddress = 0;
|
||||||
ctx,
|
if (length == 0)
|
||||||
desiredAddress,
|
{
|
||||||
length,
|
return false;
|
||||||
executable: false,
|
}
|
||||||
alignment,
|
|
||||||
allowSearch,
|
try
|
||||||
allowAllocateAtAlternative: allowSearch,
|
{
|
||||||
"reserve_virtual_range",
|
object memoryObject = ctx.Memory;
|
||||||
out mappedAddress);
|
MethodInfo? allocateAt = null;
|
||||||
|
MethodInfo? allocateAtOrAbove = null;
|
||||||
|
var allocateAtHasAllowAlternativeArg = false;
|
||||||
|
for (var depth = 0; depth < 4; depth++)
|
||||||
|
{
|
||||||
|
foreach (var candidate in memoryObject.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))
|
||||||
|
{
|
||||||
|
var parameters = candidate.GetParameters();
|
||||||
|
if (string.Equals(candidate.Name, "TryAllocateAtOrAbove", StringComparison.Ordinal) &&
|
||||||
|
parameters.Length == 5 &&
|
||||||
|
parameters[0].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[1].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[2].ParameterType == typeof(bool) &&
|
||||||
|
parameters[3].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[4].ParameterType == typeof(ulong).MakeByRefType())
|
||||||
|
{
|
||||||
|
allocateAtOrAbove = candidate;
|
||||||
|
}
|
||||||
|
else if (string.Equals(candidate.Name, "AllocateAt", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
if (parameters.Length == 3 &&
|
||||||
|
parameters[0].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[1].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[2].ParameterType == typeof(bool))
|
||||||
|
{
|
||||||
|
allocateAt = candidate;
|
||||||
|
allocateAtHasAllowAlternativeArg = false;
|
||||||
|
}
|
||||||
|
else if (parameters.Length == 4 &&
|
||||||
|
parameters[0].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[1].ParameterType == typeof(ulong) &&
|
||||||
|
parameters[2].ParameterType == typeof(bool) &&
|
||||||
|
parameters[3].ParameterType == typeof(bool))
|
||||||
|
{
|
||||||
|
allocateAt = candidate;
|
||||||
|
allocateAtHasAllowAlternativeArg = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allocateAtOrAbove is not null && allocateAt is not null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allocateAtOrAbove is not null || allocateAt is not null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var innerProperty = memoryObject.GetType().GetProperty("Inner", BindingFlags.Public | BindingFlags.Instance);
|
||||||
|
if (innerProperty is null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var innerValue = innerProperty.GetValue(memoryObject);
|
||||||
|
if (innerValue is null || ReferenceEquals(innerValue, memoryObject))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryObject = innerValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allowSearch && allocateAtOrAbove is not null)
|
||||||
|
{
|
||||||
|
var searchArgs = new object[] { desiredAddress, length, false, alignment, 0UL };
|
||||||
|
var searchResult = allocateAtOrAbove.Invoke(memoryObject, searchArgs);
|
||||||
|
if (searchResult is bool trueValue && trueValue &&
|
||||||
|
searchArgs[4] is ulong searchedAddress && searchedAddress != 0)
|
||||||
|
{
|
||||||
|
mappedAddress = searchedAddress;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allocateAt is null)
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine($"[LOADER][TRACE] reserve_virtual_range: AllocateAt missing on {ctx.Memory.GetType().FullName}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var invokeArgs = allocateAtHasAllowAlternativeArg
|
||||||
|
? new object[] { desiredAddress, length, false, allowSearch }
|
||||||
|
: new object[] { desiredAddress, length, false };
|
||||||
|
var result = allocateAt.Invoke(memoryObject, invokeArgs);
|
||||||
|
if (result is not ulong allocated || allocated == 0)
|
||||||
|
{
|
||||||
|
var resultType = result?.GetType().FullName ?? "null";
|
||||||
|
Console.Error.WriteLine($"[LOADER][TRACE] reserve_virtual_range: AllocateAt returned {resultType} value={result ?? "null"}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mappedAddress = allocated;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("[LOADER][TRACE] reserve_virtual_range: AllocateAt invocation threw");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ulong AlignUp(ulong value, ulong alignment)
|
private static ulong AlignUp(ulong value, ulong alignment)
|
||||||
@@ -1757,9 +1856,4 @@ public static class KernelRuntimeCompatExports
|
|||||||
var mask = alignment - 1;
|
var mask = alignment - 1;
|
||||||
return (value + mask) & ~mask;
|
return (value + mask) & ~mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool ShouldTraceVirtualMemory()
|
|
||||||
{
|
|
||||||
return string.Equals(Environment.GetEnvironmentVariable("SHARPEMU_LOG_VIRTUAL_MEMORY"), "1", StringComparison.Ordinal);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,159 +0,0 @@
|
|||||||
// Copyright (C) 2026 SharpEmu Emulator Project
|
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
||||||
|
|
||||||
using SharpEmu.HLE;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace SharpEmu.Libs.Kernel;
|
|
||||||
|
|
||||||
internal static class KernelVirtualRangeAllocator
|
|
||||||
{
|
|
||||||
private static readonly ConcurrentDictionary<Type, Accessor> _accessors = new();
|
|
||||||
|
|
||||||
public static bool TryReserve(
|
|
||||||
CpuContext ctx,
|
|
||||||
ulong desiredAddress,
|
|
||||||
ulong length,
|
|
||||||
bool executable,
|
|
||||||
ulong alignment,
|
|
||||||
bool allowSearch,
|
|
||||||
bool allowAllocateAtAlternative,
|
|
||||||
string traceName,
|
|
||||||
out ulong mappedAddress)
|
|
||||||
{
|
|
||||||
mappedAddress = 0;
|
|
||||||
if (length == 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (!TryResolveAccessor(ctx.Memory, out var target, out var accessor))
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine($"[LOADER][TRACE] {traceName}: AllocateAt missing on {ctx.Memory.GetType().FullName}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (allowSearch && accessor.AllocateAtOrAbove is not null)
|
|
||||||
{
|
|
||||||
var searchArgs = new object[] { desiredAddress, length, executable, alignment, 0UL };
|
|
||||||
var searchResult = accessor.AllocateAtOrAbove.Invoke(target, searchArgs);
|
|
||||||
if (searchResult is bool trueValue && trueValue &&
|
|
||||||
searchArgs[4] is ulong searchedAddress && searchedAddress != 0)
|
|
||||||
{
|
|
||||||
mappedAddress = searchedAddress;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (accessor.AllocateAt is null)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine($"[LOADER][TRACE] {traceName}: AllocateAt missing on {target.GetType().FullName}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var invokeArgs = accessor.AllocateAtHasAllowAlternativeArg
|
|
||||||
? new object[] { desiredAddress, length, executable, allowAllocateAtAlternative }
|
|
||||||
: new object[] { desiredAddress, length, executable };
|
|
||||||
var result = accessor.AllocateAt.Invoke(target, invokeArgs);
|
|
||||||
if (result is not ulong allocated || allocated == 0)
|
|
||||||
{
|
|
||||||
var resultType = result?.GetType().FullName ?? "null";
|
|
||||||
Console.Error.WriteLine($"[LOADER][TRACE] {traceName}: AllocateAt returned {resultType} value={result ?? "null"}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
mappedAddress = allocated;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine($"[LOADER][TRACE] {traceName}: AllocateAt invocation threw");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool TryResolveAccessor(object rootMemory, out object target, out Accessor accessor)
|
|
||||||
{
|
|
||||||
target = rootMemory;
|
|
||||||
accessor = default;
|
|
||||||
|
|
||||||
for (var depth = 0; depth < 4; depth++)
|
|
||||||
{
|
|
||||||
accessor = _accessors.GetOrAdd(target.GetType(), DiscoverAccessor);
|
|
||||||
if (accessor.AllocateAt is not null || accessor.AllocateAtOrAbove is not null)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (accessor.InnerProperty is null)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
var innerValue = accessor.InnerProperty.GetValue(target);
|
|
||||||
if (innerValue is null || ReferenceEquals(innerValue, target))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
target = innerValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Accessor DiscoverAccessor(Type type)
|
|
||||||
{
|
|
||||||
MethodInfo? allocateAt = null;
|
|
||||||
MethodInfo? allocateAtOrAbove = null;
|
|
||||||
var allocateAtHasAllowAlternativeArg = false;
|
|
||||||
|
|
||||||
foreach (var candidate in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
|
|
||||||
{
|
|
||||||
var parameters = candidate.GetParameters();
|
|
||||||
if (string.Equals(candidate.Name, "TryAllocateAtOrAbove", StringComparison.Ordinal) &&
|
|
||||||
parameters.Length == 5 &&
|
|
||||||
parameters[0].ParameterType == typeof(ulong) &&
|
|
||||||
parameters[1].ParameterType == typeof(ulong) &&
|
|
||||||
parameters[2].ParameterType == typeof(bool) &&
|
|
||||||
parameters[3].ParameterType == typeof(ulong) &&
|
|
||||||
parameters[4].ParameterType == typeof(ulong).MakeByRefType())
|
|
||||||
{
|
|
||||||
allocateAtOrAbove = candidate;
|
|
||||||
}
|
|
||||||
else if (string.Equals(candidate.Name, "AllocateAt", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
if (parameters.Length == 3 &&
|
|
||||||
parameters[0].ParameterType == typeof(ulong) &&
|
|
||||||
parameters[1].ParameterType == typeof(ulong) &&
|
|
||||||
parameters[2].ParameterType == typeof(bool))
|
|
||||||
{
|
|
||||||
allocateAt = candidate;
|
|
||||||
allocateAtHasAllowAlternativeArg = false;
|
|
||||||
}
|
|
||||||
else if (parameters.Length == 4 &&
|
|
||||||
parameters[0].ParameterType == typeof(ulong) &&
|
|
||||||
parameters[1].ParameterType == typeof(ulong) &&
|
|
||||||
parameters[2].ParameterType == typeof(bool) &&
|
|
||||||
parameters[3].ParameterType == typeof(bool))
|
|
||||||
{
|
|
||||||
allocateAt = candidate;
|
|
||||||
allocateAtHasAllowAlternativeArg = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var innerProperty = type.GetProperty("Inner", BindingFlags.Public | BindingFlags.Instance);
|
|
||||||
return new Accessor(allocateAt, allocateAtOrAbove, allocateAtHasAllowAlternativeArg, innerProperty);
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly record struct Accessor(
|
|
||||||
MethodInfo? AllocateAt,
|
|
||||||
MethodInfo? AllocateAtOrAbove,
|
|
||||||
bool AllocateAtHasAllowAlternativeArg,
|
|
||||||
PropertyInfo? InnerProperty);
|
|
||||||
}
|
|
||||||
@@ -879,8 +879,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
private bool _swapchainRecreateDeferred;
|
private bool _swapchainRecreateDeferred;
|
||||||
private bool _tracedPresentedSwapchain;
|
private bool _tracedPresentedSwapchain;
|
||||||
private bool _swapchainReadbackPending;
|
private bool _swapchainReadbackPending;
|
||||||
private bool _deviceLost;
|
|
||||||
private bool _deviceLostLogged;
|
|
||||||
private int _directPresentationCount;
|
private int _directPresentationCount;
|
||||||
private readonly Dictionary<ulong, GuestImageResource> _guestImages = new();
|
private readonly Dictionary<ulong, GuestImageResource> _guestImages = new();
|
||||||
private readonly HashSet<(ulong Address, uint Width, uint Height, Format Format)> _tracedTextureCacheHits = new();
|
private readonly HashSet<(ulong Address, uint Width, uint Height, Format Format)> _tracedTextureCacheHits = new();
|
||||||
@@ -1461,20 +1459,8 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
VertexPipelineStoresAndAtomics = supportedFeatures.VertexPipelineStoresAndAtomics,
|
VertexPipelineStoresAndAtomics = supportedFeatures.VertexPipelineStoresAndAtomics,
|
||||||
FragmentStoresAndAtomics = supportedFeatures.FragmentStoresAndAtomics,
|
FragmentStoresAndAtomics = supportedFeatures.FragmentStoresAndAtomics,
|
||||||
ShaderInt64 = supportedFeatures.ShaderInt64,
|
ShaderInt64 = supportedFeatures.ShaderInt64,
|
||||||
ShaderImageGatherExtended = supportedFeatures.ShaderImageGatherExtended,
|
|
||||||
ShaderStorageImageExtendedFormats = supportedFeatures.ShaderStorageImageExtendedFormats,
|
|
||||||
ShaderStorageImageReadWithoutFormat = supportedFeatures.ShaderStorageImageReadWithoutFormat,
|
|
||||||
ShaderStorageImageWriteWithoutFormat = supportedFeatures.ShaderStorageImageWriteWithoutFormat,
|
|
||||||
RobustBufferAccess = supportedFeatures.RobustBufferAccess,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!supportedFeatures.RobustBufferAccess)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine(
|
|
||||||
"[LOADER][WARN] GPU does not support robustBufferAccess " +
|
|
||||||
"translated shaders performing out-of-bounds buffer access may cause device loss.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!supportedFeatures.ShaderInt64)
|
if (!supportedFeatures.ShaderInt64)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine(
|
Console.Error.WriteLine(
|
||||||
@@ -1489,95 +1475,17 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
"translated shaders using storage buffers in vertex/fragment stages may fail.");
|
"translated shaders using storage buffers in vertex/fragment stages may fail.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!supportedFeatures.ShaderImageGatherExtended)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine(
|
|
||||||
"[LOADER][WARN] GPU does not support shaderImageGatherExtended " +
|
|
||||||
"translated shaders using image gather with offsets/LOD/bias will fail.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!supportedFeatures.ShaderStorageImageReadWithoutFormat ||
|
|
||||||
!supportedFeatures.ShaderStorageImageWriteWithoutFormat)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine(
|
|
||||||
"[LOADER][WARN] GPU does not support shaderStorageImage(Read|Write)WithoutFormat " +
|
|
||||||
"translated shaders using unformatted storage image load/store will fail.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var maintenance8Features = new PhysicalDeviceMaintenance8FeaturesKHR
|
|
||||||
{
|
|
||||||
SType = StructureType.PhysicalDeviceMaintenance8FeaturesKhr,
|
|
||||||
};
|
|
||||||
var robustness2Features = new PhysicalDeviceRobustness2FeaturesEXT
|
|
||||||
{
|
|
||||||
SType = StructureType.PhysicalDeviceRobustness2FeaturesExt,
|
|
||||||
PNext = &maintenance8Features,
|
|
||||||
};
|
|
||||||
var featuresQuery = new PhysicalDeviceFeatures2
|
|
||||||
{
|
|
||||||
SType = StructureType.PhysicalDeviceFeatures2,
|
|
||||||
PNext = &robustness2Features,
|
|
||||||
};
|
|
||||||
_vk.GetPhysicalDeviceFeatures2(_physicalDevice, &featuresQuery);
|
|
||||||
var supportsMaintenance8 = maintenance8Features.Maintenance8;
|
|
||||||
var supportsRobustImageAccess2 = robustness2Features.RobustImageAccess2;
|
|
||||||
var supportsNullDescriptor = robustness2Features.NullDescriptor;
|
|
||||||
var supportsRobustness2 = supportsRobustImageAccess2 || supportsNullDescriptor;
|
|
||||||
if (!supportsMaintenance8)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine(
|
|
||||||
"[LOADER][WARN] GPU does not support VK_KHR_maintenance8 " +
|
|
||||||
"translated shaders using a dynamic texel offset on non-gather image samples will fail.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!supportsRobustImageAccess2)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine(
|
|
||||||
"[LOADER][WARN] GPU does not support VK_EXT_robustness2 robustImageAccess2 " +
|
|
||||||
"translated shaders performing out-of-bounds image access may cause device loss.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var swapchainExtension = (byte*)SilkMarshal.StringToPtr("VK_KHR_swapchain");
|
var swapchainExtension = (byte*)SilkMarshal.StringToPtr("VK_KHR_swapchain");
|
||||||
var maintenance8Extension = (byte*)SilkMarshal.StringToPtr("VK_KHR_maintenance8");
|
|
||||||
var robustness2Extension = (byte*)SilkMarshal.StringToPtr("VK_EXT_robustness2");
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var extensions = stackalloc byte*[3];
|
|
||||||
var extensionCount = 0u;
|
|
||||||
extensions[extensionCount++] = swapchainExtension;
|
|
||||||
if (supportsMaintenance8)
|
|
||||||
{
|
|
||||||
extensions[extensionCount++] = maintenance8Extension;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (supportsRobustness2)
|
|
||||||
{
|
|
||||||
extensions[extensionCount++] = robustness2Extension;
|
|
||||||
}
|
|
||||||
|
|
||||||
maintenance8Features.Maintenance8 = supportsMaintenance8;
|
|
||||||
maintenance8Features.PNext = null;
|
|
||||||
robustness2Features.RobustBufferAccess2 =
|
|
||||||
supportsRobustImageAccess2 && supportedFeatures.RobustBufferAccess;
|
|
||||||
robustness2Features.RobustImageAccess2 = supportsRobustImageAccess2;
|
|
||||||
robustness2Features.NullDescriptor = supportsNullDescriptor;
|
|
||||||
robustness2Features.PNext = supportsMaintenance8 ? &maintenance8Features : null;
|
|
||||||
var features2 = new PhysicalDeviceFeatures2
|
|
||||||
{
|
|
||||||
SType = StructureType.PhysicalDeviceFeatures2,
|
|
||||||
PNext = supportsRobustness2
|
|
||||||
? &robustness2Features
|
|
||||||
: (supportsMaintenance8 ? &maintenance8Features : null),
|
|
||||||
Features = enabledFeatures,
|
|
||||||
};
|
|
||||||
var createInfo = new DeviceCreateInfo
|
var createInfo = new DeviceCreateInfo
|
||||||
{
|
{
|
||||||
SType = StructureType.DeviceCreateInfo,
|
SType = StructureType.DeviceCreateInfo,
|
||||||
PNext = &features2,
|
|
||||||
QueueCreateInfoCount = 1,
|
QueueCreateInfoCount = 1,
|
||||||
PQueueCreateInfos = &queueInfo,
|
PQueueCreateInfos = &queueInfo,
|
||||||
EnabledExtensionCount = extensionCount,
|
EnabledExtensionCount = 1,
|
||||||
PpEnabledExtensionNames = extensions,
|
PpEnabledExtensionNames = &swapchainExtension,
|
||||||
|
PEnabledFeatures = &enabledFeatures,
|
||||||
};
|
};
|
||||||
|
|
||||||
Check(_vk.CreateDevice(_physicalDevice, &createInfo, null, out _device), "vkCreateDevice");
|
Check(_vk.CreateDevice(_physicalDevice, &createInfo, null, out _device), "vkCreateDevice");
|
||||||
@@ -1585,8 +1493,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
SilkMarshal.Free((nint)swapchainExtension);
|
SilkMarshal.Free((nint)swapchainExtension);
|
||||||
SilkMarshal.Free((nint)maintenance8Extension);
|
|
||||||
SilkMarshal.Free((nint)robustness2Extension);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_vk.GetDeviceQueue(_device, _queueFamilyIndex, 0, out _queue);
|
_vk.GetDeviceQueue(_device, _queueFamilyIndex, 0, out _queue);
|
||||||
@@ -1751,39 +1657,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
resources.DebugName));
|
resources.DebugName));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SubmitGuestCommandBufferAndWait(CommandBuffer commandBuffer)
|
|
||||||
{
|
|
||||||
var fenceInfo = new FenceCreateInfo
|
|
||||||
{
|
|
||||||
SType = StructureType.FenceCreateInfo,
|
|
||||||
};
|
|
||||||
Fence fence;
|
|
||||||
Check(
|
|
||||||
_vk.CreateFence(_device, &fenceInfo, null, out fence),
|
|
||||||
"vkCreateFence(guest chunk)");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var submitInfo = new SubmitInfo
|
|
||||||
{
|
|
||||||
SType = StructureType.SubmitInfo,
|
|
||||||
CommandBufferCount = 1,
|
|
||||||
PCommandBuffers = &commandBuffer,
|
|
||||||
};
|
|
||||||
Check(
|
|
||||||
_vk.QueueSubmit(_queue, 1, &submitInfo, fence),
|
|
||||||
"vkQueueSubmit(guest chunk)");
|
|
||||||
Check(
|
|
||||||
_vk.WaitForFences(_device, 1, &fence, true, ulong.MaxValue),
|
|
||||||
"vkWaitForFences(guest chunk)");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_vk.DestroyFence(_device, fence, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
_vk.FreeCommandBuffers(_device, _commandPool, 1, &commandBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EnsureGuestSubmissionCapacity()
|
private void EnsureGuestSubmissionCapacity()
|
||||||
{
|
{
|
||||||
CollectCompletedGuestSubmissions(waitForOldest: false);
|
CollectCompletedGuestSubmissions(waitForOldest: false);
|
||||||
@@ -2768,7 +2641,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
var pipelineInfo = new ComputePipelineCreateInfo
|
var pipelineInfo = new ComputePipelineCreateInfo
|
||||||
{
|
{
|
||||||
SType = StructureType.ComputePipelineCreateInfo,
|
SType = StructureType.ComputePipelineCreateInfo,
|
||||||
Flags = PipelineCreateFlags.CreateDispatchBaseBit,
|
|
||||||
Stage = stage,
|
Stage = stage,
|
||||||
Layout = resources.PipelineLayout,
|
Layout = resources.PipelineLayout,
|
||||||
};
|
};
|
||||||
@@ -2818,8 +2690,7 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
dstSelect: texture.DstSelect,
|
dstSelect: texture.DstSelect,
|
||||||
out var view))
|
out var view))
|
||||||
{
|
{
|
||||||
if (ShouldTraceVulkanResources() &&
|
if (_tracedTextureCacheHits.Add(
|
||||||
_tracedTextureCacheHits.Add(
|
|
||||||
(texture.Address, texture.Width, texture.Height, vkFormat)))
|
(texture.Address, texture.Width, texture.Height, vkFormat)))
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine(
|
Console.Error.WriteLine(
|
||||||
@@ -3115,13 +2986,9 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
}
|
}
|
||||||
_vk.UnmapMemory(_device, stagingMemory);
|
_vk.UnmapMemory(_device, stagingMemory);
|
||||||
|
|
||||||
var supportsAttachmentUsage = !IsBlockCompressedFormat(vkFormat);
|
|
||||||
var imageInfo = new ImageCreateInfo
|
var imageInfo = new ImageCreateInfo
|
||||||
{
|
{
|
||||||
SType = StructureType.ImageCreateInfo,
|
SType = StructureType.ImageCreateInfo,
|
||||||
Flags = supportsAttachmentUsage
|
|
||||||
? ImageCreateFlags.CreateMutableFormatBit | ImageCreateFlags.CreateExtendedUsageBit
|
|
||||||
: 0,
|
|
||||||
ImageType = ImageType.Type2D,
|
ImageType = ImageType.Type2D,
|
||||||
Format = vkFormat,
|
Format = vkFormat,
|
||||||
Extent = new Extent3D(width, height, 1),
|
Extent = new Extent3D(width, height, 1),
|
||||||
@@ -3129,13 +2996,7 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
ArrayLayers = 1,
|
ArrayLayers = 1,
|
||||||
Samples = SampleCountFlags.Count1Bit,
|
Samples = SampleCountFlags.Count1Bit,
|
||||||
Tiling = ImageTiling.Optimal,
|
Tiling = ImageTiling.Optimal,
|
||||||
Usage = supportsAttachmentUsage
|
Usage = ImageUsageFlags.TransferDstBit | ImageUsageFlags.SampledBit,
|
||||||
? ImageUsageFlags.TransferDstBit |
|
|
||||||
ImageUsageFlags.SampledBit |
|
|
||||||
ImageUsageFlags.ColorAttachmentBit |
|
|
||||||
ImageUsageFlags.StorageBit |
|
|
||||||
ImageUsageFlags.TransferSrcBit
|
|
||||||
: ImageUsageFlags.TransferDstBit | ImageUsageFlags.SampledBit,
|
|
||||||
SharingMode = SharingMode.Exclusive,
|
SharingMode = SharingMode.Exclusive,
|
||||||
InitialLayout = ImageLayout.Undefined,
|
InitialLayout = ImageLayout.Undefined,
|
||||||
};
|
};
|
||||||
@@ -3398,8 +3259,7 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
out var memory);
|
out var memory);
|
||||||
var size = (ulong)Math.Max(guestBuffer.Data.Length, sizeof(uint));
|
var size = (ulong)Math.Max(guestBuffer.Data.Length, sizeof(uint));
|
||||||
|
|
||||||
if (ShouldTraceVulkanResources() &&
|
if (_tracedGlobalBuffers.Add((guestBuffer.BaseAddress, guestBuffer.Data.Length)))
|
||||||
_tracedGlobalBuffers.Add((guestBuffer.BaseAddress, guestBuffer.Data.Length)))
|
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine(
|
Console.Error.WriteLine(
|
||||||
$"[LOADER][TRACE] vk.global_buffer base=0x{guestBuffer.BaseAddress:X16} " +
|
$"[LOADER][TRACE] vk.global_buffer base=0x{guestBuffer.BaseAddress:X16} " +
|
||||||
@@ -3971,12 +3831,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
_ => Format.Undefined,
|
_ => Format.Undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static bool IsBlockCompressedFormat(Format format) =>
|
|
||||||
format is Format.BC1RgbaUnormBlock or
|
|
||||||
Format.BC1RgbaSrgbBlock or
|
|
||||||
Format.BC7UnormBlock or
|
|
||||||
Format.BC7SrgbBlock;
|
|
||||||
|
|
||||||
private VkBuffer CreateBuffer(
|
private VkBuffer CreateBuffer(
|
||||||
ulong size,
|
ulong size,
|
||||||
BufferUsageFlags usage,
|
BufferUsageFlags usage,
|
||||||
@@ -4030,24 +3884,8 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
throw new InvalidOperationException("No compatible Vulkan host-visible memory type was found.");
|
throw new InvalidOperationException("No compatible Vulkan host-visible memory type was found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private const uint MaxComputeZSlicesPerSubmission = 8;
|
|
||||||
|
|
||||||
private void ExecuteComputeDispatch(VulkanComputeGuestDispatch work)
|
private void ExecuteComputeDispatch(VulkanComputeGuestDispatch work)
|
||||||
{
|
{
|
||||||
if (_deviceLost)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (AddressListContains("SHARPEMU_SKIP_COMPUTE_CS", work.ShaderAddress))
|
|
||||||
{
|
|
||||||
TraceVulkanShader(
|
|
||||||
$"vk.compute_skip cs=0x{work.ShaderAddress:X16} " +
|
|
||||||
$"groups={work.GroupCountX}x{work.GroupCountY}x{work.GroupCountZ} " +
|
|
||||||
$"textures={work.Textures.Count}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
TranslatedDrawResources? resources = null;
|
TranslatedDrawResources? resources = null;
|
||||||
CommandBuffer commandBuffer = default;
|
CommandBuffer commandBuffer = default;
|
||||||
var submitted = false;
|
var submitted = false;
|
||||||
@@ -4055,100 +3893,59 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
{
|
{
|
||||||
EnsureGuestSubmissionCapacity();
|
EnsureGuestSubmissionCapacity();
|
||||||
resources = CreateComputeDispatchResources(work);
|
resources = CreateComputeDispatchResources(work);
|
||||||
|
commandBuffer = AllocateGuestCommandBuffer();
|
||||||
var batchCount = Math.Max(
|
_commandBuffer = commandBuffer;
|
||||||
1u,
|
var beginInfo = new CommandBufferBeginInfo
|
||||||
(uint)Math.Ceiling(work.GroupCountZ / (double)MaxComputeZSlicesPerSubmission));
|
|
||||||
|
|
||||||
for (var batchIndex = 0u; batchIndex < batchCount; batchIndex++)
|
|
||||||
{
|
{
|
||||||
var zStart = batchIndex * MaxComputeZSlicesPerSubmission;
|
SType = StructureType.CommandBufferBeginInfo,
|
||||||
var zCount = Math.Min(MaxComputeZSlicesPerSubmission, work.GroupCountZ - zStart);
|
Flags = CommandBufferUsageFlags.OneTimeSubmitBit,
|
||||||
var isFirstBatch = batchIndex == 0;
|
};
|
||||||
var isLastBatch = batchIndex == batchCount - 1;
|
Check(
|
||||||
|
_vk.BeginCommandBuffer(_commandBuffer, &beginInfo),
|
||||||
|
"vkBeginCommandBuffer(compute)");
|
||||||
|
|
||||||
commandBuffer = AllocateGuestCommandBuffer();
|
BeginDebugLabel(_commandBuffer, resources.DebugName);
|
||||||
_commandBuffer = commandBuffer;
|
RecordTextureUploads(resources, PipelineStageFlags.ComputeShaderBit);
|
||||||
var beginInfo = new CommandBufferBeginInfo
|
RecordStorageImagesForWrite(resources, PipelineStageFlags.ComputeShaderBit);
|
||||||
{
|
_vk.CmdBindPipeline(
|
||||||
SType = StructureType.CommandBufferBeginInfo,
|
_commandBuffer,
|
||||||
Flags = CommandBufferUsageFlags.OneTimeSubmitBit,
|
PipelineBindPoint.Compute,
|
||||||
};
|
resources.Pipeline);
|
||||||
Check(
|
if (resources.DescriptorSet.Handle != 0)
|
||||||
_vk.BeginCommandBuffer(_commandBuffer, &beginInfo),
|
{
|
||||||
"vkBeginCommandBuffer(compute)");
|
var descriptorSet = resources.DescriptorSet;
|
||||||
|
_vk.CmdBindDescriptorSets(
|
||||||
BeginDebugLabel(_commandBuffer, resources.DebugName);
|
|
||||||
if (isFirstBatch)
|
|
||||||
{
|
|
||||||
RecordTextureUploads(resources, PipelineStageFlags.ComputeShaderBit);
|
|
||||||
RecordStorageImagesForWrite(resources, PipelineStageFlags.ComputeShaderBit);
|
|
||||||
}
|
|
||||||
|
|
||||||
_vk.CmdBindPipeline(
|
|
||||||
_commandBuffer,
|
_commandBuffer,
|
||||||
PipelineBindPoint.Compute,
|
PipelineBindPoint.Compute,
|
||||||
resources.Pipeline);
|
resources.PipelineLayout,
|
||||||
if (resources.DescriptorSet.Handle != 0)
|
0,
|
||||||
{
|
1,
|
||||||
var descriptorSet = resources.DescriptorSet;
|
&descriptorSet,
|
||||||
_vk.CmdBindDescriptorSets(
|
0,
|
||||||
_commandBuffer,
|
null);
|
||||||
PipelineBindPoint.Compute,
|
|
||||||
resources.PipelineLayout,
|
|
||||||
0,
|
|
||||||
1,
|
|
||||||
&descriptorSet,
|
|
||||||
0,
|
|
||||||
null);
|
|
||||||
}
|
|
||||||
|
|
||||||
RecordChunkedComputeDispatch(_commandBuffer, work, zStart, zCount);
|
|
||||||
|
|
||||||
if (isLastBatch)
|
|
||||||
{
|
|
||||||
RecordStorageImagesForRead(resources, PipelineStageFlags.ComputeShaderBit);
|
|
||||||
}
|
|
||||||
|
|
||||||
EndDebugLabel(_commandBuffer);
|
|
||||||
Check(_vk.EndCommandBuffer(_commandBuffer), "vkEndCommandBuffer(compute)");
|
|
||||||
|
|
||||||
TraceVulkanShader(
|
|
||||||
$"vk.compute_submit cs=0x{work.ShaderAddress:X16} " +
|
|
||||||
$"batch={batchIndex}/{batchCount} z={zStart}..{zStart + zCount}");
|
|
||||||
if (isLastBatch)
|
|
||||||
{
|
|
||||||
SubmitGuestCommandBuffer(
|
|
||||||
commandBuffer,
|
|
||||||
resources,
|
|
||||||
GetTraceImages(resources));
|
|
||||||
submitted = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SubmitGuestCommandBufferAndWait(commandBuffer);
|
|
||||||
commandBuffer = default;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RecordChunkedComputeDispatch(_commandBuffer, work);
|
||||||
|
RecordStorageImagesForRead(resources, PipelineStageFlags.ComputeShaderBit);
|
||||||
|
EndDebugLabel(_commandBuffer);
|
||||||
|
|
||||||
|
Check(_vk.EndCommandBuffer(_commandBuffer), "vkEndCommandBuffer(compute)");
|
||||||
|
SubmitGuestCommandBuffer(
|
||||||
|
commandBuffer,
|
||||||
|
resources,
|
||||||
|
GetTraceImages(resources));
|
||||||
|
submitted = true;
|
||||||
MarkSampledImagesInitialized(resources);
|
MarkSampledImagesInitialized(resources);
|
||||||
MarkStorageImagesInitialized(resources, traceContents: false);
|
MarkStorageImagesInitialized(resources, traceContents: false);
|
||||||
TraceVulkanShader(
|
TraceVulkanShader(
|
||||||
$"vk.compute_dispatch groups={work.GroupCountX}x" +
|
$"vk.compute_dispatch groups={work.GroupCountX}x" +
|
||||||
$"{work.GroupCountY}x{work.GroupCountZ} " +
|
$"{work.GroupCountY}x{work.GroupCountZ} " +
|
||||||
$"textures={work.Textures.Count} cs=0x{work.ShaderAddress:X16} " +
|
$"textures={work.Textures.Count} cs=0x{work.ShaderAddress:X16}");
|
||||||
$"batches={batchCount}");
|
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
if (TryMarkDeviceLost(exception))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.Error.WriteLine(
|
Console.Error.WriteLine(
|
||||||
$"[LOADER][ERROR] Vulkan compute dispatch failed " +
|
$"[LOADER][ERROR] Vulkan compute dispatch failed: {exception.Message}");
|
||||||
$"cs=0x{work.ShaderAddress:X16}: {exception.Message}");
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -4171,9 +3968,7 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
|
|
||||||
private void RecordChunkedComputeDispatch(
|
private void RecordChunkedComputeDispatch(
|
||||||
CommandBuffer commandBuffer,
|
CommandBuffer commandBuffer,
|
||||||
VulkanComputeGuestDispatch work,
|
VulkanComputeGuestDispatch work)
|
||||||
uint zStart,
|
|
||||||
uint zCount)
|
|
||||||
{
|
{
|
||||||
const uint maxWorkgroupsPerCommand = 4096;
|
const uint maxWorkgroupsPerCommand = 4096;
|
||||||
var yChunk = Math.Max(
|
var yChunk = Math.Max(
|
||||||
@@ -4183,7 +3978,7 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
maxWorkgroupsPerCommand / Math.Max(work.GroupCountX, 1u)));
|
maxWorkgroupsPerCommand / Math.Max(work.GroupCountX, 1u)));
|
||||||
var commandCount = 0u;
|
var commandCount = 0u;
|
||||||
|
|
||||||
for (var z = zStart; z < zStart + zCount; z++)
|
for (var z = 0u; z < work.GroupCountZ; z++)
|
||||||
{
|
{
|
||||||
for (var y = 0u; y < work.GroupCountY; y += yChunk)
|
for (var y = 0u; y < work.GroupCountY; y += yChunk)
|
||||||
{
|
{
|
||||||
@@ -4205,17 +4000,12 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
TraceVulkanShader(
|
TraceVulkanShader(
|
||||||
$"vk.compute_chunked cs=0x{work.ShaderAddress:X16} " +
|
$"vk.compute_chunked cs=0x{work.ShaderAddress:X16} " +
|
||||||
$"groups={work.GroupCountX}x{work.GroupCountY}x{work.GroupCountZ} " +
|
$"groups={work.GroupCountX}x{work.GroupCountY}x{work.GroupCountZ} " +
|
||||||
$"z_range={zStart}..{zStart + zCount} commands={commandCount} y_chunk={yChunk}");
|
$"commands={commandCount} y_chunk={yChunk}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ExecuteOffscreenDraw(VulkanOffscreenGuestDraw work)
|
private void ExecuteOffscreenDraw(VulkanOffscreenGuestDraw work)
|
||||||
{
|
{
|
||||||
if (_deviceLost)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var format = GetRenderTargetFormat(work.Target.Format, work.Target.NumberType);
|
var format = GetRenderTargetFormat(work.Target.Format, work.Target.NumberType);
|
||||||
if (format == Format.Undefined)
|
if (format == Format.Undefined)
|
||||||
{
|
{
|
||||||
@@ -4373,11 +4163,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
if (TryMarkDeviceLost(exception))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_guestImages.TryGetValue(work.Target.Address, out var failedTarget) ||
|
if (!_guestImages.TryGetValue(work.Target.Address, out var failedTarget) ||
|
||||||
!failedTarget.Initialized)
|
!failedTarget.Initialized)
|
||||||
{
|
{
|
||||||
@@ -4423,23 +4208,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
existing.MipLevels == mipLevels &&
|
existing.MipLevels == mipLevels &&
|
||||||
existing.Format == format)
|
existing.Format == format)
|
||||||
{
|
{
|
||||||
if (existing.RenderPass.Handle == 0)
|
|
||||||
{
|
|
||||||
var attachmentView = existing.MipViews.Length > 0
|
|
||||||
? existing.MipViews[0]
|
|
||||||
: existing.View;
|
|
||||||
var (promotedRenderPass, promotedFramebuffer) = CreateRenderPassAndFramebuffer(
|
|
||||||
existing.Format,
|
|
||||||
attachmentView,
|
|
||||||
existing.Width,
|
|
||||||
existing.Height);
|
|
||||||
existing.RenderPass = promotedRenderPass;
|
|
||||||
existing.Framebuffer = promotedFramebuffer;
|
|
||||||
var promotedName = GuestImageDebugName(target, format);
|
|
||||||
SetDebugName(ObjectType.RenderPass, promotedRenderPass.Handle, $"{promotedName} renderpass");
|
|
||||||
SetDebugName(ObjectType.Framebuffer, promotedFramebuffer.Handle, $"{promotedName} framebuffer");
|
|
||||||
}
|
|
||||||
|
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4520,48 +4288,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
mipViews[mipLevel] = mipView;
|
mipViews[mipLevel] = mipView;
|
||||||
}
|
}
|
||||||
|
|
||||||
var (renderPass, framebuffer) = CreateRenderPassAndFramebuffer(
|
|
||||||
format,
|
|
||||||
mipViews[0],
|
|
||||||
target.Width,
|
|
||||||
target.Height);
|
|
||||||
|
|
||||||
var resource = new GuestImageResource
|
|
||||||
{
|
|
||||||
Address = target.Address,
|
|
||||||
Width = target.Width,
|
|
||||||
Height = target.Height,
|
|
||||||
MipLevels = mipLevels,
|
|
||||||
Format = format,
|
|
||||||
Image = image,
|
|
||||||
Memory = memory,
|
|
||||||
View = view,
|
|
||||||
MipViews = mipViews,
|
|
||||||
RenderPass = renderPass,
|
|
||||||
Framebuffer = framebuffer,
|
|
||||||
};
|
|
||||||
var debugName = GuestImageDebugName(target, format);
|
|
||||||
SetDebugName(ObjectType.Image, image.Handle, $"{debugName} image");
|
|
||||||
SetDebugName(ObjectType.ImageView, view.Handle, $"{debugName} view");
|
|
||||||
for (var mipLevel = 0; mipLevel < mipViews.Length; mipLevel++)
|
|
||||||
{
|
|
||||||
SetDebugName(
|
|
||||||
ObjectType.ImageView,
|
|
||||||
mipViews[mipLevel].Handle,
|
|
||||||
$"{debugName} mip{mipLevel}");
|
|
||||||
}
|
|
||||||
SetDebugName(ObjectType.RenderPass, renderPass.Handle, $"{debugName} renderpass");
|
|
||||||
SetDebugName(ObjectType.Framebuffer, framebuffer.Handle, $"{debugName} framebuffer");
|
|
||||||
_guestImages.Add(target.Address, resource);
|
|
||||||
return resource;
|
|
||||||
}
|
|
||||||
|
|
||||||
private (RenderPass RenderPass, Framebuffer Framebuffer) CreateRenderPassAndFramebuffer(
|
|
||||||
Format format,
|
|
||||||
ImageView attachmentView,
|
|
||||||
uint width,
|
|
||||||
uint height)
|
|
||||||
{
|
|
||||||
var colorAttachment = new AttachmentDescription
|
var colorAttachment = new AttachmentDescription
|
||||||
{
|
{
|
||||||
Format = format,
|
Format = format,
|
||||||
@@ -4596,22 +4322,49 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
_vk.CreateRenderPass(_device, &renderPassInfo, null, out var renderPass),
|
_vk.CreateRenderPass(_device, &renderPassInfo, null, out var renderPass),
|
||||||
"vkCreateRenderPass(offscreen)");
|
"vkCreateRenderPass(offscreen)");
|
||||||
|
|
||||||
var attachment = attachmentView;
|
var attachment = mipViews[0];
|
||||||
var framebufferInfo = new FramebufferCreateInfo
|
var framebufferInfo = new FramebufferCreateInfo
|
||||||
{
|
{
|
||||||
SType = StructureType.FramebufferCreateInfo,
|
SType = StructureType.FramebufferCreateInfo,
|
||||||
RenderPass = renderPass,
|
RenderPass = renderPass,
|
||||||
AttachmentCount = 1,
|
AttachmentCount = 1,
|
||||||
PAttachments = &attachment,
|
PAttachments = &attachment,
|
||||||
Width = width,
|
Width = target.Width,
|
||||||
Height = height,
|
Height = target.Height,
|
||||||
Layers = 1,
|
Layers = 1,
|
||||||
};
|
};
|
||||||
Check(
|
Check(
|
||||||
_vk.CreateFramebuffer(_device, &framebufferInfo, null, out var framebuffer),
|
_vk.CreateFramebuffer(_device, &framebufferInfo, null, out var framebuffer),
|
||||||
"vkCreateFramebuffer(offscreen)");
|
"vkCreateFramebuffer(offscreen)");
|
||||||
|
|
||||||
return (renderPass, framebuffer);
|
var resource = new GuestImageResource
|
||||||
|
{
|
||||||
|
Address = target.Address,
|
||||||
|
Width = target.Width,
|
||||||
|
Height = target.Height,
|
||||||
|
MipLevels = mipLevels,
|
||||||
|
Format = format,
|
||||||
|
Image = image,
|
||||||
|
Memory = memory,
|
||||||
|
View = view,
|
||||||
|
MipViews = mipViews,
|
||||||
|
RenderPass = renderPass,
|
||||||
|
Framebuffer = framebuffer,
|
||||||
|
};
|
||||||
|
var debugName = GuestImageDebugName(target, format);
|
||||||
|
SetDebugName(ObjectType.Image, image.Handle, $"{debugName} image");
|
||||||
|
SetDebugName(ObjectType.ImageView, view.Handle, $"{debugName} view");
|
||||||
|
for (var mipLevel = 0; mipLevel < mipViews.Length; mipLevel++)
|
||||||
|
{
|
||||||
|
SetDebugName(
|
||||||
|
ObjectType.ImageView,
|
||||||
|
mipViews[mipLevel].Handle,
|
||||||
|
$"{debugName} mip{mipLevel}");
|
||||||
|
}
|
||||||
|
SetDebugName(ObjectType.RenderPass, renderPass.Handle, $"{debugName} renderpass");
|
||||||
|
SetDebugName(ObjectType.Framebuffer, framebuffer.Handle, $"{debugName} framebuffer");
|
||||||
|
_guestImages.Add(target.Address, resource);
|
||||||
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static uint ClampMipLevels(uint width, uint height, uint requestedMipLevels)
|
private static uint ClampMipLevels(uint width, uint height, uint requestedMipLevels)
|
||||||
@@ -4833,10 +4586,7 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
}
|
}
|
||||||
|
|
||||||
_commandBuffer = _presentationCommandBuffer;
|
_commandBuffer = _presentationCommandBuffer;
|
||||||
if (!_deviceLost)
|
CollectCompletedGuestSubmissions(waitForOldest: false);
|
||||||
{
|
|
||||||
CollectCompletedGuestSubmissions(waitForOldest: false);
|
|
||||||
}
|
|
||||||
|
|
||||||
var completedWork = 0;
|
var completedWork = 0;
|
||||||
while (completedWork < MaxGuestWorkPerRender &&
|
while (completedWork < MaxGuestWorkPerRender &&
|
||||||
@@ -5694,12 +5444,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
string.Equals(mode, "present", StringComparison.OrdinalIgnoreCase);
|
string.Equals(mode, "present", StringComparison.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool ShouldTraceVulkanResources() =>
|
|
||||||
string.Equals(
|
|
||||||
Environment.GetEnvironmentVariable("SHARPEMU_LOG_VK_RESOURCES"),
|
|
||||||
"1",
|
|
||||||
StringComparison.Ordinal);
|
|
||||||
|
|
||||||
private void RecordTranslatedGraphicsPass(
|
private void RecordTranslatedGraphicsPass(
|
||||||
TranslatedDrawResources resources,
|
TranslatedDrawResources resources,
|
||||||
RenderPass renderPass,
|
RenderPass renderPass,
|
||||||
@@ -6535,25 +6279,6 @@ internal static unsafe class VulkanVideoPresenter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool TryMarkDeviceLost(Exception exception)
|
|
||||||
{
|
|
||||||
if (!exception.Message.Contains(nameof(Result.ErrorDeviceLost), StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_deviceLost = true;
|
|
||||||
if (!_deviceLostLogged)
|
|
||||||
{
|
|
||||||
_deviceLostLogged = true;
|
|
||||||
Console.Error.WriteLine(
|
|
||||||
"[LOADER][ERROR] Vulkan device lost; dropping subsequent guest GPU work. " +
|
|
||||||
exception.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TraceVulkanShader(string message)
|
private static void TraceVulkanShader(string message)
|
||||||
{
|
{
|
||||||
if (!string.Equals(Environment.GetEnvironmentVariable("SHARPEMU_LOG_AGC"), "1", StringComparison.Ordinal) &&
|
if (!string.Equals(Environment.GetEnvironmentVariable("SHARPEMU_LOG_AGC"), "1", StringComparison.Ordinal) &&
|
||||||
|
|||||||
Reference in New Issue
Block a user