Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed Assets/dll/libgcc_s_seh-1.dll
Binary file not shown.
Binary file added Assets/dll/librashader.dll
Binary file not shown.
Binary file removed Assets/dll/libstdc++-6.dll
Binary file not shown.
3 changes: 2 additions & 1 deletion src/BizHawk.Bizware.Graphics/BizHawk.Bizware.Graphics.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -8,6 +8,7 @@
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="BizHawk.Client.Common" />
<PackageReference Include="Cyotek.Drawing.BitmapFont" />
<PackageReference Include="ImGui.NET" ExcludeAssets="native" />
<PackageReference Include="System.Drawing.Common" PrivateAssets="all" />
Expand Down
6 changes: 0 additions & 6 deletions src/BizHawk.Bizware.Graphics/D3D11/D3D11Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ public void CreateResources()
var creationFlags = DeviceCreationFlags.Singlethreaded | DeviceCreationFlags.BgraSupport;
#endif

// GL interop doesn't support the single threaded flag
if (D3D11GLInterop.IsAvailable)
{
creationFlags &= ~DeviceCreationFlags.Singlethreaded;
}

D3D11.D3D11CreateDevice(
adapter: null,
DriverType.Hardware,
Expand Down
4 changes: 4 additions & 0 deletions src/BizHawk.Bizware.Graphics/D3D11/IGL_D3D11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,9 @@ public void DrawIndexed(int indexCount, int indexStart, int vertexStart)
Context.IASetPrimitiveTopology(PrimitiveTopology.TriangleList);
Context.DrawIndexed(indexCount, indexStart, vertexStart);
}

internal ID3D11Device LibrashaderDevice => Device;
internal ID3D11DeviceContext LibrashaderContext => Context;
internal ID3D11RenderTargetView LibrashaderBackBufferRTV => _controlSwapChain?.RTV;
}
}
111 changes: 111 additions & 0 deletions src/BizHawk.Bizware.Graphics/D3D11/LibrashaderProcessorD3D11.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
namespace BizHawk.Bizware.Graphics
{
public unsafe class LibrashaderProcessorD3D11 : IDisposable
{
private readonly IGL_D3D11 _gl;
private IntPtr _preset = IntPtr.Zero;
private IntPtr _chain = IntPtr.Zero;
private uint _frameCount = 0;
private bool _initialized = false;
private int _filteredWidth;
private int _filteredHeight;
private readonly string _shaderPresetPath;

public bool IsAvailable => _initialized;

public LibrashaderProcessorD3D11(IGL_D3D11 gl, string shaderPresetPath)
{
_gl = gl;
_shaderPresetPath = shaderPresetPath;
}

public bool Initialize(int width, int height)
{
if (_initialized && _filteredWidth == width && _filteredHeight == height)
return true;

_filteredWidth = width;
_filteredHeight = height;

if (_filteredWidth <= 0 || _filteredHeight <= 0) return false;
if (!Librashader.Load()) return false;
if (!System.IO.File.Exists(_shaderPresetPath)) return false;

if (_preset == IntPtr.Zero)
{
IntPtr error = Librashader.PresetCreate(_shaderPresetPath, out _preset);
if (error != IntPtr.Zero)
{
_ = Librashader.libra_error_print(error);
return false;
}
}

if (_chain == IntPtr.Zero)
{
var device = _gl.LibrashaderDevice;
if (device == null) return false;

var options = Librashader.CreateDefaultD3D11Options();
IntPtr error = Librashader.libra_d3d11_filter_chain_create(ref _preset, device.NativePointer, ref options, out _chain);
if (error != IntPtr.Zero)
{
_ = Librashader.libra_error_print(error);
return false;
}
}

return _initialized = true;
}

public void Render(IntPtr inputSRVPointer, IntPtr outputRTVPointer)
{
if (!_initialized || _chain == IntPtr.Zero) return;
if (inputSRVPointer == IntPtr.Zero || outputRTVPointer == IntPtr.Zero) return;

var context = _gl.LibrashaderContext;
if (context == null) return;

var viewport = new Librashader.libra_viewport_t
{
x = 0,
y = 0,
width = (uint)_filteredWidth,
height = (uint)_filteredHeight,
};

_ = Librashader.libra_d3d11_filter_chain_frame(
ref _chain,
context.NativePointer,
new UIntPtr(_frameCount++),
inputSRVPointer,
outputRTVPointer,
ref viewport,
IntPtr.Zero,
IntPtr.Zero);
}

public IntPtr GetBackBufferRTVPointer()
{
var rtv = _gl.LibrashaderBackBufferRTV;
return rtv?.NativePointer ?? IntPtr.Zero;
}

public void Dispose()
{
if (_chain != IntPtr.Zero)
{
_ = Librashader.libra_d3d11_filter_chain_free(ref _chain);
_chain = IntPtr.Zero;
}

if (_preset != IntPtr.Zero)
{
_ = Librashader.libra_preset_free(ref _preset);
_preset = IntPtr.Zero;
}

_initialized = false;
}
}
}
168 changes: 168 additions & 0 deletions src/BizHawk.Bizware.Graphics/Librashader/Librashader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System.Runtime.InteropServices;
using System.Text;

using BizHawk.Common;

namespace BizHawk.Bizware.Graphics
{
public static unsafe class Librashader
{
private const CallingConvention CC = CallingConvention.Cdecl;
private static bool _loaded = false;

public static bool IsLoaded => _loaded;

public static bool Load()
{
if (_loaded) return true;

Util.DebugWriteLine("[librashader] Attempting to load librashader.dll...");
try
{
libra_instance_abi_version();
Util.DebugWriteLine("[librashader] Successfully loaded librashader.dll");
_loaded = true;
return true;
}
catch (DllNotFoundException)
{
Util.DebugWriteLine("[librashader] Failed to load librashader.dll");
return false;
}
}

[DllImport("librashader", CallingConvention = CC)]
public static extern IntPtr libra_instance_abi_version();

[DllImport("librashader", CallingConvention = CC)]
public static extern IntPtr libra_instance_api_version();

[DllImport("librashader", CallingConvention = CC)]
public static extern IntPtr libra_preset_create(byte* filename, out IntPtr preset);

[DllImport("librashader", CallingConvention = CC)]
public static extern int libra_preset_free(ref IntPtr preset);

[DllImport("librashader", CallingConvention = CC)]
public static extern int libra_error_free(ref IntPtr error);

[DllImport("librashader", CallingConvention = CC)]
public static extern int libra_error_print(IntPtr error);

[DllImport("librashader", CallingConvention = CC)]
public static extern int libra_error_errno(IntPtr error);

public static IntPtr PresetCreate(string filename, out IntPtr preset)
{
byte[] bytes = Encoding.UTF8.GetBytes(filename + "\0");
fixed (byte* ptr = bytes)
{
return libra_preset_create(ptr, out preset);
}
}

[DllImport("librashader", CallingConvention = CC)]
public static extern IntPtr libra_gl_filter_chain_create(
ref IntPtr preset,
IntPtr loader,
[In] ref filter_chain_gl_opt_t options,
out IntPtr chain);

[DllImport("librashader", CallingConvention = CC)]
public static extern int libra_gl_filter_chain_frame(
ref IntPtr chain,
UIntPtr frame_count,
libra_image_gl_t image,
libra_image_gl_t output,
IntPtr viewport,
IntPtr mvp,
IntPtr options);

[DllImport("librashader", CallingConvention = CC)]
public static extern int libra_gl_filter_chain_free(ref IntPtr chain);

[StructLayout(LayoutKind.Sequential)]
public struct filter_chain_gl_opt_t
{
public UIntPtr version;
public ushort glsl_version;
[MarshalAs(UnmanagedType.U1)]
public bool use_dsa;
[MarshalAs(UnmanagedType.U1)]
public bool force_no_mipmaps;
[MarshalAs(UnmanagedType.U1)]
public bool disable_cache;
}

[StructLayout(LayoutKind.Sequential)]
public struct libra_image_gl_t
{
public uint handle;
public uint format;
public uint width;
public uint height;
}

public static filter_chain_gl_opt_t CreateDefaultGLOptions()
{
return new filter_chain_gl_opt_t
{
version = new UIntPtr(2),
glsl_version = 330,
use_dsa = false,
force_no_mipmaps = false,
disable_cache = false,
};
}

[DllImport("librashader", CallingConvention = CC)]
public static extern IntPtr libra_d3d11_filter_chain_create(
ref IntPtr preset,
IntPtr device,
[In] ref filter_chain_d3d11_opt_t options,
out IntPtr chain);

[DllImport("librashader", CallingConvention = CC)]
public static extern int libra_d3d11_filter_chain_frame(
ref IntPtr chain,
IntPtr device_context,
UIntPtr frame_count,
IntPtr image,
IntPtr output,
ref libra_viewport_t viewport,
IntPtr mvp,
IntPtr options);

[DllImport("librashader", CallingConvention = CC)]
public static extern int libra_d3d11_filter_chain_free(ref IntPtr chain);

[StructLayout(LayoutKind.Sequential)]
public struct filter_chain_d3d11_opt_t
{
public UIntPtr version;
[MarshalAs(UnmanagedType.U1)]
public bool force_no_mipmaps;
[MarshalAs(UnmanagedType.U1)]
public bool disable_cache;
}

[StructLayout(LayoutKind.Sequential)]
public struct libra_viewport_t
{
public float x;
public float y;
public uint width;
public uint height;
}

public static filter_chain_d3d11_opt_t CreateDefaultD3D11Options()
{
return new filter_chain_d3d11_opt_t
{
version = new UIntPtr(2),
force_no_mipmaps = false,
disable_cache = false,
};
}
}
}
2 changes: 2 additions & 0 deletions src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class IGL_OpenGL : IGL

private readonly GL GL;

internal GL LibrashaderGL => GL;

// rendering state
private OpenGLPipeline _curPipeline;
internal bool DefaultRenderTargetBound;
Expand Down
Loading
Loading