Skip to content

Commit

Permalink
upgrade GLWpfControl and share ogl ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
MikiraSora committed Nov 4, 2024
1 parent daaf006 commit 6699a25
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 109 deletions.
222 changes: 114 additions & 108 deletions OngekiFumenEditor/Kernel/Graphics/DefaultDrawingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
}
2 changes: 1 addition & 1 deletion OngekiFumenEditor/OngekiFumenEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="OpenTK" Version="4.8.2" />
<PackageReference Include="OpenTK.GLWpfControl" Version="4.1.0" />
<PackageReference Include="OpenTK.GLWpfControl" Version="4.3.3" />
<PackageReference Include="PixiEditor.ColorPicker" Version="3.4.1" />
<PackageReference Include="SharpVectors" Version="1.8.4.2" />
<PackageReference Include="SimpleSvg2LineSegementInterpolater" Version="0.7.3" />
Expand Down

0 comments on commit 6699a25

Please sign in to comment.