|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +using BizHawk.Common; |
| 6 | + |
| 7 | +namespace BizHawk.Client.Common.Filters |
| 8 | +{ |
| 9 | + public static unsafe class Librashader |
| 10 | + { |
| 11 | + private const string DllName = "librashader.dll"; |
| 12 | + private static IntPtr _handle = IntPtr.Zero; |
| 13 | + private static bool _loaded = false; |
| 14 | + |
| 15 | + public static bool IsLoaded => _loaded; |
| 16 | + |
| 17 | + public static bool Load() |
| 18 | + { |
| 19 | + if (_loaded) return true; |
| 20 | + |
| 21 | + Util.DebugWriteLine("[librashader] Attempting to load dll\\librashader.dll..."); |
| 22 | + _handle = LoadLibrary("dll\\librashader.dll"); |
| 23 | + if (_handle == IntPtr.Zero) |
| 24 | + { |
| 25 | + Util.DebugWriteLine("[librashader] Failed to load librashader.dll"); |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + Util.DebugWriteLine("[librashader] Successfully loaded librashader.dll"); |
| 30 | + _loaded = true; |
| 31 | + LoadFunctions(); |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 36 | + private static extern IntPtr LoadLibrary(string lpFileName); |
| 37 | + |
| 38 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 39 | + private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); |
| 40 | + |
| 41 | + private static T GetDelegate<T>(string name) where T : Delegate |
| 42 | + { |
| 43 | + IntPtr addr = GetProcAddress(_handle, name); |
| 44 | + return addr != IntPtr.Zero ? Marshal.GetDelegateForFunctionPointer<T>(addr) : null; |
| 45 | + } |
| 46 | + |
| 47 | + public delegate IntPtr PFN_libra_instance_abi_version(); |
| 48 | + public delegate IntPtr PFN_libra_instance_api_version(); |
| 49 | + public delegate IntPtr PFN_libra_preset_create(byte* filename, out IntPtr preset); |
| 50 | + public delegate int PFN_libra_preset_free(ref IntPtr preset); |
| 51 | + public delegate int PFN_libra_error_free(ref IntPtr error); |
| 52 | + public delegate int PFN_libra_error_print(IntPtr error); |
| 53 | + public delegate int PFN_libra_error_errno(IntPtr error); |
| 54 | + |
| 55 | + public delegate IntPtr PFN_libra_gl_loader_t(byte* name); |
| 56 | + |
| 57 | + public delegate IntPtr PFN_libra_gl_filter_chain_create( |
| 58 | + ref IntPtr preset, |
| 59 | + IntPtr loader, |
| 60 | + [In] ref filter_chain_gl_opt_t options, |
| 61 | + out IntPtr chain); |
| 62 | + |
| 63 | + public delegate int PFN_libra_gl_filter_chain_frame( |
| 64 | + ref IntPtr chain, |
| 65 | + UIntPtr frame_count, |
| 66 | + libra_image_gl_t image, |
| 67 | + libra_image_gl_t output, |
| 68 | + IntPtr viewport, |
| 69 | + IntPtr mvp, |
| 70 | + IntPtr options); |
| 71 | + |
| 72 | + public delegate int PFN_libra_gl_filter_chain_free(ref IntPtr chain); |
| 73 | + |
| 74 | + public static PFN_libra_instance_abi_version instance_abi_version; |
| 75 | + public static PFN_libra_instance_api_version instance_api_version; |
| 76 | + public static PFN_libra_preset_create preset_create; |
| 77 | + public static PFN_libra_preset_free preset_free; |
| 78 | + public static PFN_libra_error_free error_free; |
| 79 | + public static PFN_libra_error_print error_print; |
| 80 | + public static PFN_libra_error_errno error_errno; |
| 81 | + public static PFN_libra_gl_filter_chain_create gl_filter_chain_create; |
| 82 | + public static PFN_libra_gl_filter_chain_frame gl_filter_chain_frame; |
| 83 | + public static PFN_libra_gl_filter_chain_free gl_filter_chain_free; |
| 84 | + |
| 85 | + private static void LoadFunctions() |
| 86 | + { |
| 87 | + instance_abi_version = GetDelegate<PFN_libra_instance_abi_version>("libra_instance_abi_version"); |
| 88 | + instance_api_version = GetDelegate<PFN_libra_instance_api_version>("libra_instance_api_version"); |
| 89 | + preset_create = GetDelegate<PFN_libra_preset_create>("libra_preset_create"); |
| 90 | + preset_free = GetDelegate<PFN_libra_preset_free>("libra_preset_free"); |
| 91 | + error_free = GetDelegate<PFN_libra_error_free>("libra_error_free"); |
| 92 | + error_print = GetDelegate<PFN_libra_error_print>("libra_error_print"); |
| 93 | + error_errno = GetDelegate<PFN_libra_error_errno>("libra_error_errno"); |
| 94 | + gl_filter_chain_create = GetDelegate<PFN_libra_gl_filter_chain_create>("libra_gl_filter_chain_create"); |
| 95 | + gl_filter_chain_frame = GetDelegate<PFN_libra_gl_filter_chain_frame>("libra_gl_filter_chain_frame"); |
| 96 | + gl_filter_chain_free = GetDelegate<PFN_libra_gl_filter_chain_free>("libra_gl_filter_chain_free"); |
| 97 | + } |
| 98 | + |
| 99 | + public static IntPtr PresetCreate(string filename, out IntPtr preset) |
| 100 | + { |
| 101 | + byte[] bytes = Encoding.UTF8.GetBytes(filename + "\0"); |
| 102 | + fixed (byte* ptr = bytes) |
| 103 | + { |
| 104 | + return preset_create(ptr, out preset); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + [StructLayout(LayoutKind.Sequential)] |
| 109 | + public struct filter_chain_gl_opt_t |
| 110 | + { |
| 111 | + public UIntPtr version; |
| 112 | + public ushort glsl_version; |
| 113 | + [MarshalAs(UnmanagedType.U1)] |
| 114 | + public bool use_dsa; |
| 115 | + [MarshalAs(UnmanagedType.U1)] |
| 116 | + public bool force_no_mipmaps; |
| 117 | + [MarshalAs(UnmanagedType.U1)] |
| 118 | + public bool disable_cache; |
| 119 | + } |
| 120 | + |
| 121 | + [StructLayout(LayoutKind.Sequential)] |
| 122 | + public struct libra_image_gl_t |
| 123 | + { |
| 124 | + public uint handle; |
| 125 | + public uint format; |
| 126 | + public uint width; |
| 127 | + public uint height; |
| 128 | + } |
| 129 | + |
| 130 | + [StructLayout(LayoutKind.Sequential)] |
| 131 | + public struct libra_viewport_t |
| 132 | + { |
| 133 | + public float x; |
| 134 | + public float y; |
| 135 | + public uint width; |
| 136 | + public uint height; |
| 137 | + } |
| 138 | + |
| 139 | + public static filter_chain_gl_opt_t CreateDefaultOptions() |
| 140 | + { |
| 141 | + return new filter_chain_gl_opt_t |
| 142 | + { |
| 143 | + version = new UIntPtr(1), |
| 144 | + glsl_version = 330, |
| 145 | + use_dsa = false, |
| 146 | + force_no_mipmaps = false, |
| 147 | + disable_cache = false |
| 148 | + }; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments