diff --git a/OngekiFumenEditor/Kernel/Graphics/DefaultDrawingManager.cs b/OngekiFumenEditor/Kernel/Graphics/DefaultDrawingManager.cs index 14eecc6f..252e3775 100644 --- a/OngekiFumenEditor/Kernel/Graphics/DefaultDrawingManager.cs +++ b/OngekiFumenEditor/Kernel/Graphics/DefaultDrawingManager.cs @@ -13,118 +13,124 @@ namespace OngekiFumenEditor.Kernel.Graphics { - [Export(typeof(IDrawingManager))] - [PartCreationPolicy(CreationPolicy.Shared)] - public class DefaultDrawingManager : IDrawingManager - { - // Import the necessary Win32 functions - [DllImport("opengl32.dll")] - private static extern IntPtr wglGetCurrentDC(); - - [DllImport("opengl32.dll", CharSet = CharSet.Ansi, SetLastError = true)] - private static extern IntPtr wglGetProcAddress(string lpszProc); - - private static bool IsWGL_NV_DX_interopSupported() - { - var hdc = wglGetCurrentDC(); - var functionPointer = wglGetProcAddress("wglDXSetResourceSharingNV"); - return functionPointer != IntPtr.Zero; - } - - TaskCompletionSource initTaskSource = new TaskCompletionSource(); - bool startedInit = false; - - public Task CheckOrInitGraphics() - { - if (!startedInit) - { - startedInit = true; - Dispatcher.CurrentDispatcher.InvokeAsync(OnInitOpenGL); - } - - return initTaskSource.Task; - } - - private void OnInitOpenGL() - { - if (Properties.ProgramSetting.Default.OutputGraphicsLog) - { - GL.DebugMessageCallback(OnOpenGLDebugLog, IntPtr.Zero); - GL.Enable(EnableCap.DebugOutput); - if (Properties.ProgramSetting.Default.GraphicsLogSynchronous) - GL.Enable(EnableCap.DebugOutputSynchronous); - } - - GL.ClearColor(System.Drawing.Color.Black); - GL.Enable(EnableCap.Blend); - GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); - - Log.LogDebug($"Prepare OpenGL version : {GL.GetInteger(GetPName.MajorVersion)}.{GL.GetInteger(GetPName.MinorVersion)}"); - - try - { - var isSupport = IsWGL_NV_DX_interopSupported(); - Log.LogDebug($"WGL_NV_DX_interop support: {isSupport}"); - } - catch - { - Log.LogDebug($"WGL_NV_DX_interop support: EXCEPTION"); - } - - if (Properties.ProgramSetting.Default.GraphicsCompatability) - { - var extNames = string.Join(", ", Enumerable.Range(0, GL.GetInteger(GetPName.NumExtensions)).Select(i => GL.GetString(StringNameIndexed.Extensions, i))); - Log.LogDebug($"(maybe support) OpenGL extensions: {extNames}"); - } - - initTaskSource.SetResult(); - } - - private static void OnOpenGLDebugLog(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr message, IntPtr userParam) - { - if (id == 131185) - return; - - var str = Marshal.PtrToStringAnsi(message, length); - Log.LogDebug($"[{source}.{type}]{id}: {str}"); - } - - public Task WaitForGraphicsInitializationDone(CancellationToken cancellation) - { - return initTaskSource.Task; - } - - public Task CreateGraphicsContext(GLWpfControl glView, CancellationToken cancellation = default) - { - var isCompatability = Properties.ProgramSetting.Default.GraphicsCompatability; - var isOutputLog = Properties.ProgramSetting.Default.OutputGraphicsLog; - - var flag = isOutputLog ? ContextFlags.Debug : ContextFlags.Default; - - var setting = isCompatability ? new GLWpfControlSettings() - { - MajorVersion = 3, - MinorVersion = 3, - GraphicsContextFlags = flag | ContextFlags.ForwardCompatible, - GraphicsProfile = ContextProfile.Compatability - } : new GLWpfControlSettings() - { - MajorVersion = 4, - MinorVersion = 5, - GraphicsContextFlags = flag, - GraphicsProfile = ContextProfile.Core - }; + [Export(typeof(IDrawingManager))] + [PartCreationPolicy(CreationPolicy.Shared)] + public class DefaultDrawingManager : IDrawingManager + { + // Import the necessary Win32 functions + [DllImport("opengl32.dll")] + private static extern IntPtr wglGetCurrentDC(); + + [DllImport("opengl32.dll", CharSet = CharSet.Ansi, SetLastError = true)] + private static extern IntPtr wglGetProcAddress(string lpszProc); + + private IGraphicsContext sharedContext; + + private static bool IsWGL_NV_DX_interopSupported() + { + var hdc = wglGetCurrentDC(); + var functionPointer = wglGetProcAddress("wglDXSetResourceSharingNV"); + return functionPointer != IntPtr.Zero; + } + + TaskCompletionSource initTaskSource = new TaskCompletionSource(); + bool startedInit = false; + + public Task CheckOrInitGraphics() + { + if (!startedInit) + { + startedInit = true; + Dispatcher.CurrentDispatcher.InvokeAsync(OnInitOpenGL); + } + + return initTaskSource.Task; + } + + private void OnInitOpenGL() + { + if (Properties.ProgramSetting.Default.OutputGraphicsLog) + { + GL.DebugMessageCallback(OnOpenGLDebugLog, IntPtr.Zero); + GL.Enable(EnableCap.DebugOutput); + if (Properties.ProgramSetting.Default.GraphicsLogSynchronous) + GL.Enable(EnableCap.DebugOutputSynchronous); + } + + GL.ClearColor(System.Drawing.Color.Black); + GL.Enable(EnableCap.Blend); + GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); + + Log.LogDebug($"Prepare OpenGL version : {GL.GetInteger(GetPName.MajorVersion)}.{GL.GetInteger(GetPName.MinorVersion)}"); + + try + { + var isSupport = IsWGL_NV_DX_interopSupported(); + Log.LogDebug($"WGL_NV_DX_interop support: {isSupport}"); + } + catch + { + Log.LogDebug($"WGL_NV_DX_interop support: EXCEPTION"); + } + + if (Properties.ProgramSetting.Default.GraphicsCompatability) + { + var extNames = string.Join(", ", Enumerable.Range(0, GL.GetInteger(GetPName.NumExtensions)).Select(i => GL.GetString(StringNameIndexed.Extensions, i))); + Log.LogDebug($"(maybe support) OpenGL extensions: {extNames}"); + } + + initTaskSource.SetResult(); + } + + private static void OnOpenGLDebugLog(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr message, IntPtr userParam) + { + if (id == 131185) + return; + + var str = Marshal.PtrToStringAnsi(message, length); + Log.LogDebug($"[{source}.{type}]{id}: {str}"); + } + + public Task WaitForGraphicsInitializationDone(CancellationToken cancellation) + { + return initTaskSource.Task; + } + + public Task CreateGraphicsContext(GLWpfControl glView, CancellationToken cancellation = default) + { + var isCompatability = Properties.ProgramSetting.Default.GraphicsCompatability; + var isOutputLog = Properties.ProgramSetting.Default.OutputGraphicsLog; + + var flag = isOutputLog ? ContextFlags.Debug : ContextFlags.Default; + + GLWpfControlSettings setting = isCompatability ? new() + { + MajorVersion = 3, + MinorVersion = 3, + ContextFlags = flag | ContextFlags.ForwardCompatible, + Profile = ContextProfile.Compatability, + } : new() + { + MajorVersion = 4, + MinorVersion = 5, + ContextFlags = flag, + Profile = ContextProfile.Core + }; + + setting.ContextToUse = sharedContext; Log.LogDebug($"GraphicsCompatability: {isCompatability}"); - Log.LogDebug($"OutputGraphicsLog: {isOutputLog}"); + Log.LogDebug($"OutputGraphicsLog: {isOutputLog}"); - Log.LogDebug($"GLWpfControlSettings.Version: {setting.MajorVersion}.{setting.MinorVersion}"); - Log.LogDebug($"GLWpfControlSettings.GraphicsContextFlags: {setting.GraphicsContextFlags}"); - Log.LogDebug($"GLWpfControlSettings.GraphicsProfile: {setting.GraphicsProfile}"); + Log.LogDebug($"GLWpfControlSettings.Version: {setting.MajorVersion}.{setting.MinorVersion}"); + Log.LogDebug($"GLWpfControlSettings.GraphicsContextFlags: {setting.ContextFlags}"); + Log.LogDebug($"GLWpfControlSettings.GraphicsProfile: {setting.Profile}"); - glView.Start(setting); + glView.Start(setting); + + sharedContext = sharedContext ?? glView.Context; return Task.CompletedTask; - } - } + } + } } diff --git a/OngekiFumenEditor/OngekiFumenEditor.csproj b/OngekiFumenEditor/OngekiFumenEditor.csproj index 6dbca19d..c8809e9a 100644 --- a/OngekiFumenEditor/OngekiFumenEditor.csproj +++ b/OngekiFumenEditor/OngekiFumenEditor.csproj @@ -168,7 +168,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - +