Skip to content

Commit

Permalink
Kill Bizware .NET Framework deps (#3702)
Browse files Browse the repository at this point in the history
The main objective in this PR is to get rid of the main .NET Framework dependencies in Bizware packages. This PR doesn't do that completely per se, still having .NET Framework used for WinForms Controls, but that can easily be swapped over for whatever UI framework we use next as long as it exposes native window handles in some way.

For this PR, it does some reorganizing of Bizware, splitting Bizware.OpenTK3 and Bizware.DirectX into 3 packages based on usage; Bizware.Audio, Bizware.Graphics, and Bizware.Input. These packages in the future probably could have more functionality moved into them, but for now they are largely just a reshuffling of the Bizware.OpenTK3 and Bizware.DirectX packages.

As both SlimDX and OpenTK3 are .NET Framework, they have been removed in this PR. Their replacements are as follows:

SharpDX: DirectSound, Direct3D9
Vortice: XAudio2, DirectInput/XInput
Silk.NET: OpenAL, OpenGL
SDL2-CS / native SDL2: OpenGL context management, new gamepad backend (replacing OpenTK's role for gamepads)
native X11: New key input backend (replacing one of OpenTK's roles for keyboards)

GLControl has been replaced by custom made control which just uses SDL2 for context management.

The OpenTK input backend has been replaced with a combination of SDL2 and an OS tailored key input backend (DirectInput on Windows, X11 on Linux, and planned to be Quartz on macOS). This is just represented on the user side as "SDL2" without mentioning the key input backend. This does mean for a while DirectX will be mandatory on Windows again, until a RAWINPUT backend is written for handling key input on Windows for the SDL2 input backend.
  • Loading branch information
CasualPokePlayer authored Jul 23, 2023
1 parent 26b39fb commit 78f5e75
Show file tree
Hide file tree
Showing 74 changed files with 5,127 additions and 4,079 deletions.
25 changes: 0 additions & 25 deletions Assets/dll/OpenTK.dll.config

This file was deleted.

Binary file modified Assets/dll/SDL2.dll
Binary file not shown.
4 changes: 4 additions & 0 deletions Assets/dll/Silk.NET.Core.dll.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<configuration>
<!-- Hack until https://github.com/dotnet/Silk.NET/commit/d5f1f295966c0790abc215ab6e900f810f464443 is in a NuGet release -->
<dllmap os="linux" dll="libdl" target="libdl.so.2"/>
</configuration>
Binary file added Assets/dll/libSDL2.so
Binary file not shown.
35 changes: 21 additions & 14 deletions BizHawk.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28729.10

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33627.172
MinimumVisualStudioVersion = 16.0.28729.10
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BizHawk.Client.Common", "src\BizHawk.Client.Common\BizHawk.Client.Common.csproj", "{24A0AA3C-B25F-4197-B23D-476D6462DBA0}"
EndProject
Expand All @@ -20,11 +21,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Emulation", "Emulation", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BizHawk.Emulation.Cores", "src\BizHawk.Emulation.Cores\BizHawk.Emulation.Cores.csproj", "{197D4314-8A9F-49BA-977D-54ACEFAEB6BA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BizHawk.Bizware.BizwareGL", "src\BizHawk.Bizware.BizwareGL\BizHawk.Bizware.BizwareGL.csproj", "{9F84A0B2-861E-4EF4-B89B-5E2A3F38A465}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BizHawk.Bizware.Audio", "src\BizHawk.Bizware.Audio\BizHawk.Bizware.Audio.csproj", "{9F84A0B2-861E-4EF4-B89B-5E2A3F38A465}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BizHawk.Bizware.DirectX", "src\BizHawk.Bizware.DirectX\BizHawk.Bizware.DirectX.csproj", "{A914D063-9E4B-4086-B156-7B3F39E33DB2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BizHawk.Bizware.Input", "src\BizHawk.Bizware.Input\BizHawk.Bizware.Input.csproj", "{17E7D20D-198C-4728-ACEC-065DE834FF02}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BizHawk.Bizware.OpenTK3", "src\BizHawk.Bizware.OpenTK3\BizHawk.Bizware.OpenTK3.csproj", "{1FF433CC-96E1-4F14-B673-CDA7190169C9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BizHawk.Bizware.Graphics", "src\BizHawk.Bizware.Graphics\BizHawk.Bizware.Graphics.csproj", "{368BC91D-48CD-492A-B6CF-B5B77F7FE7D4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BizHawk.Bizware.BizwareGL", "src\BizHawk.Bizware.BizwareGL\BizHawk.Bizware.BizwareGL.csproj", "{658BB7AA-74A1-496F-A6AA-B7D3DD9C7CBE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BizHawk.BizInvoke", "src\BizHawk.BizInvoke\BizHawk.BizInvoke.csproj", "{E5D76DC1-84A8-47AF-BE25-E76F06D2FBBC}"
EndProject
Expand Down Expand Up @@ -72,14 +75,18 @@ Global
{9F84A0B2-861E-4EF4-B89B-5E2A3F38A465}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F84A0B2-861E-4EF4-B89B-5E2A3F38A465}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F84A0B2-861E-4EF4-B89B-5E2A3F38A465}.Release|Any CPU.Build.0 = Release|Any CPU
{A914D063-9E4B-4086-B156-7B3F39E33DB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A914D063-9E4B-4086-B156-7B3F39E33DB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A914D063-9E4B-4086-B156-7B3F39E33DB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A914D063-9E4B-4086-B156-7B3F39E33DB2}.Release|Any CPU.Build.0 = Release|Any CPU
{1FF433CC-96E1-4F14-B673-CDA7190169C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FF433CC-96E1-4F14-B673-CDA7190169C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FF433CC-96E1-4F14-B673-CDA7190169C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FF433CC-96E1-4F14-B673-CDA7190169C9}.Release|Any CPU.Build.0 = Release|Any CPU
{17E7D20D-198C-4728-ACEC-065DE834FF02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17E7D20D-198C-4728-ACEC-065DE834FF02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17E7D20D-198C-4728-ACEC-065DE834FF02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17E7D20D-198C-4728-ACEC-065DE834FF02}.Release|Any CPU.Build.0 = Release|Any CPU
{368BC91D-48CD-492A-B6CF-B5B77F7FE7D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{368BC91D-48CD-492A-B6CF-B5B77F7FE7D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{368BC91D-48CD-492A-B6CF-B5B77F7FE7D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{368BC91D-48CD-492A-B6CF-B5B77F7FE7D4}.Release|Any CPU.Build.0 = Release|Any CPU
{658BB7AA-74A1-496F-A6AA-B7D3DD9C7CBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{658BB7AA-74A1-496F-A6AA-B7D3DD9C7CBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{658BB7AA-74A1-496F-A6AA-B7D3DD9C7CBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{658BB7AA-74A1-496F-A6AA-B7D3DD9C7CBE}.Release|Any CPU.Build.0 = Release|Any CPU
{E5D76DC1-84A8-47AF-BE25-E76F06D2FBBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5D76DC1-84A8-47AF-BE25-E76F06D2FBBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5D76DC1-84A8-47AF-BE25-E76F06D2FBBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Binary file removed References/x64/SlimDX.dll
Binary file not shown.
19 changes: 19 additions & 0 deletions src/BizHawk.Bizware.Audio/BizHawk.Bizware.Audio.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<Import Project="../MainSlnCommon.props" />
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Silk.NET.OpenAL" Version="2.17.1" />
<PackageReference Include="Silk.NET.OpenAL.Extensions.Creative" Version="2.17.1" />
<PackageReference Include="Silk.NET.OpenAL.Extensions.Enumeration" Version="2.17.1" />
<PackageReference Include="Vortice.MediaFoundation" Version="2.4.2" />
<PackageReference Include="Vortice.XAudio2" Version="2.4.2" />
<PackageReference Include="SharpDX.DirectSound" Version="4.2.0" />
<ProjectReference Include="$(ProjectDir)../BizHawk.Client.Common/BizHawk.Client.Common.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;

using BizHawk.Client.Common;

using SlimDX.DirectSound;
using SlimDX.Multimedia;
using SharpDX;
using SharpDX.DirectSound;
using SharpDX.Multimedia;

namespace BizHawk.Bizware.DirectX
namespace BizHawk.Bizware.Audio
{
public sealed class DirectSoundSoundOutput : ISoundOutput
{
Expand All @@ -27,8 +29,8 @@ public DirectSoundSoundOutput(IHostAudioManager sound, IntPtr mainWindowHandle,
_sound = sound;
_retryCounter = 5;

var deviceInfo = DirectSound.GetDevices().FirstOrDefault(d => d.Description == soundDevice);
_device = deviceInfo != null ? new DirectSound(deviceInfo.DriverGuid) : new DirectSound();
var deviceInfo = DirectSound.GetDevices().Find(d => d.Description == soundDevice);
_device = deviceInfo != null ? new(deviceInfo.DriverGuid) : new();
_device.SetCooperativeLevel(mainWindowHandle, CooperativeLevel.Priority);
}

Expand All @@ -53,31 +55,31 @@ public static IEnumerable<string> GetDeviceNames()

public int MaxSamplesDeficit { get; private set; }

private bool IsPlaying => _deviceBuffer != null && (_deviceBuffer.Status & BufferStatus.BufferLost) == 0 && (_deviceBuffer.Status & BufferStatus.Playing) == BufferStatus.Playing;
private bool IsPlaying => _deviceBuffer != null &&
((BufferStatus)_deviceBuffer.Status & BufferStatus.BufferLost) == 0 &&
((BufferStatus)_deviceBuffer.Status & BufferStatus.Playing) == BufferStatus.Playing;

private void StartPlaying()
{
_actualWriteOffsetBytes = -1;
_filledBufferSizeBytes = 0;
_lastWriteTime = 0;
_lastWriteCursor = 0;
int attempts = _retryCounter;
var attempts = _retryCounter;
while (!IsPlaying && attempts > 0)
{
attempts--;
try
{
if (_deviceBuffer == null)
{
var format = new WaveFormat
{
SamplesPerSecond = _sound.SampleRate,
BitsPerSample = (short) (_sound.BytesPerSample * 8),
Channels = (short) _sound.ChannelCount,
FormatTag = WaveFormatTag.Pcm,
BlockAlignment = (short) _sound.BlockAlign,
AverageBytesPerSecond = _sound.SampleRate * _sound.BlockAlign
};
var format = WaveFormat.CreateCustomFormat(
tag: WaveFormatEncoding.Pcm,
sampleRate: _sound.SampleRate,
channels: _sound.ChannelCount,
averageBytesPerSecond: _sound.SampleRate * _sound.BlockAlign,
blockAlign: _sound.BlockAlign,
bitsPerSample: _sound.BytesPerSample * 8);

var desc = new SoundBufferDescription
{
Expand All @@ -87,23 +89,20 @@ private void StartPlaying()
BufferFlags.Software |
BufferFlags.GetCurrentPosition2 |
BufferFlags.ControlVolume,
SizeInBytes = BufferSizeBytes
BufferBytes = BufferSizeBytes
};

_deviceBuffer = new SecondarySoundBuffer(_device, desc);
_deviceBuffer = new(_device, desc);
}

_deviceBuffer.Play(0, PlayFlags.Looping);
}
catch (DirectSoundException)
catch (SharpDXException)
{
if (_deviceBuffer != null)
{
_deviceBuffer.Restore();
}
_deviceBuffer?.Restore();
if (attempts > 0)
{
System.Threading.Thread.Sleep(10);
Thread.Sleep(10);
}
}
}
Expand All @@ -125,10 +124,10 @@ public void ApplyVolumeSettings(double volume)
try
{
// I'm not sure if this is "technically" correct but it works okay
int range = (int)Volume.Maximum - (int)Volume.Minimum;
_deviceBuffer.Volume = (int)(Math.Pow(volume, 0.1) * range) + (int)Volume.Minimum;
const int range = Volume.Maximum - Volume.Minimum;
_deviceBuffer.Volume = (int)(Math.Pow(volume, 0.1) * range) + Volume.Minimum;
}
catch (DirectSoundException)
catch (SharpDXException)
{
}
}
Expand All @@ -144,7 +143,7 @@ public void StartSound()
// severe glitches. At least on my Windows 8 machines, the distance between the
// play and write cursors can be up to 30 milliseconds, so that would be the
// absolute minimum we could use here.
int minBufferFullnessMs = Math.Min(35 + ((_sound.ConfigBufferSizeMs - 60) / 2), 65);
var minBufferFullnessMs = Math.Min(35 + (_sound.ConfigBufferSizeMs - 60) / 2, 65);
MaxSamplesDeficit = BufferSizeSamples - _sound.MillisecondsToSamples(minBufferFullnessMs);

StartPlaying();
Expand All @@ -158,32 +157,32 @@ public void StopSound()
{
_deviceBuffer.Stop();
}
catch (DirectSoundException)
catch (SharpDXException)
{
}
}

_deviceBuffer.Dispose();
_deviceBuffer = null;
BufferSizeSamples = 0;
}

public int CalculateSamplesNeeded()
{
int samplesNeeded = 0;
var samplesNeeded = 0;
if (IsPlaying)
{
try
{
long currentWriteTime = Stopwatch.GetTimestamp();
int playCursor = _deviceBuffer.CurrentPlayPosition;
int writeCursor = _deviceBuffer.CurrentWritePosition;
bool isInitializing = _actualWriteOffsetBytes == -1;
bool detectedUnderrun = false;
var currentWriteTime = Stopwatch.GetTimestamp();
_deviceBuffer.GetCurrentPosition(out var playCursor, out var writeCursor);
var isInitializing = _actualWriteOffsetBytes == -1;
var detectedUnderrun = false;
if (!isInitializing)
{
double elapsedSeconds = (currentWriteTime - _lastWriteTime) / (double)Stopwatch.Frequency;
double bufferSizeSeconds = (double) BufferSizeSamples / _sound.SampleRate;
int cursorDelta = CircularDistance(_lastWriteCursor, writeCursor, BufferSizeBytes);
var elapsedSeconds = (currentWriteTime - _lastWriteTime) / (double)Stopwatch.Frequency;
var bufferSizeSeconds = (double) BufferSizeSamples / _sound.SampleRate;
var cursorDelta = CircularDistance(_lastWriteCursor, writeCursor, BufferSizeBytes);
cursorDelta += BufferSizeBytes * (int) Math.Round((elapsedSeconds - (cursorDelta / (double) (_sound.SampleRate * _sound.BlockAlign))) / bufferSizeSeconds);
_filledBufferSizeBytes -= cursorDelta;
detectedUnderrun = _filledBufferSizeBytes < 0;
Expand All @@ -201,15 +200,15 @@ public int CalculateSamplesNeeded()
_lastWriteTime = currentWriteTime;
_lastWriteCursor = writeCursor;
}
catch (DirectSoundException)
catch (SharpDXException)
{
samplesNeeded = 0;
}
}
return samplesNeeded;
}

private int CircularDistance(int start, int end, int size)
private static int CircularDistance(int start, int end, int size)
{
return (end - start + size) % size;
}
Expand All @@ -227,18 +226,15 @@ public void WriteSamples(short[] samples, int sampleOffset, int sampleCount)
_actualWriteOffsetBytes = (_actualWriteOffsetBytes + (sampleCount * _sound.BlockAlign)) % BufferSizeBytes;
_filledBufferSizeBytes += sampleCount * _sound.BlockAlign;
}
catch (DirectSoundException)
catch (SharpDXException)
{
_deviceBuffer.Restore();
StartPlaying();
}
}
else
{
if (_deviceBuffer != null)
{
_deviceBuffer.Restore();
}
_deviceBuffer?.Restore();
StartPlaying();
}
}
Expand Down
Loading

0 comments on commit 78f5e75

Please sign in to comment.