diff --git a/.vscode/launch.json b/.vscode/launch.json index d327e94..9a5d503 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -17,7 +17,7 @@ "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Gtk/Debug/net5.0/linux-x64/TestEtoVeldrid.Gtk.dll" }, "windows": { - "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Wpf/Debug/net5.0/win-x64/TestEtoVeldrid.Wpf.dll" + "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Wpf/Debug/net5.0-windows/win-x64/TestEtoVeldrid.Wpf.dll" }, "console": "internalConsole", "stopAtEntry": false @@ -29,11 +29,11 @@ "preLaunchTask": "build", "program": "", "osx": { - "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Mac64/Debug/net472/osx-x64/TestEtoVeldrid.Mac64.app/Contents/MacOS/TestEtoVeldrid.Mac64", + "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Mac64/Debug/net48/osx-x64/TestEtoVeldrid.Mac64.app/Contents/MacOS/TestEtoVeldrid.Mac64", "useRuntime": false }, "linux": { - "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Gtk/Debug/net472/linux-x64/TestEtoVeldrid.Gtk.exe" + "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Gtk/Debug/net48/linux-x64/TestEtoVeldrid.Gtk.exe" }, "console": "internalConsole" }, @@ -44,7 +44,7 @@ "preLaunchTask": "build", "program": "", "windows": { - "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Wpf/Debug/net472/win-x64/TestEtoVeldrid.Wpf.exe" + "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Wpf/Debug/net48/win-x64/TestEtoVeldrid.Wpf.exe" }, "console": "internalConsole" }, @@ -55,7 +55,7 @@ "preLaunchTask": "build", "program": "", "windows": { - "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.WinForms/Debug/net472/win-x64/TestEtoVeldrid.WinForms.exe" + "program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.WinForms/Debug/net48/win-x64/TestEtoVeldrid.WinForms.exe" }, "console": "internalConsole" }, diff --git a/src/Eto.Veldrid.Gtk/GtkVeldridSurfaceHandler.cs b/src/Eto.Veldrid.Gtk/GtkVeldridSurfaceHandler.cs index 76328e4..c49d511 100755 --- a/src/Eto.Veldrid.Gtk/GtkVeldridSurfaceHandler.cs +++ b/src/Eto.Veldrid.Gtk/GtkVeldridSurfaceHandler.cs @@ -10,25 +10,18 @@ namespace Eto.Veldrid.Gtk { - public class GtkVeldridSurfaceHandler : GtkControl, VeldridSurface.IHandler, VeldridSurface.IOpenGL + public class GtkVeldridSurfaceHandler : GtkControl, VeldridSurface.IHandler, VeldridSurface.IOpenGL { + GLArea glArea; public Size RenderSize => Size.Round((SizeF)Widget.Size * Scale); float Scale => Widget.ParentWindow?.Screen?.LogicalPixelSize ?? 1; + public override global::Gtk.Widget ContainerContentControl => glArea ?? base.ContainerContentControl; + public GtkVeldridSurfaceHandler() { - Control = new GLArea(); - Control.CanFocus = true; - - // Veldrid technically supports as low as OpenGL 3.0, but the full - // complement of features is only available with 3.3 and higher. - Control.SetRequiredVersion(3, 3); - - Control.HasDepthBuffer = true; - Control.HasStencilBuffer = true; - - Control.Realized += Control_InitializeGraphicsBackend; + Control = new EtoEventBox { Handler = this }; } public Swapchain CreateSwapchain() @@ -64,24 +57,29 @@ public Swapchain CreateSwapchain() return swapchain; } - void Control_InitializeGraphicsBackend(object sender, EventArgs e) + void glArea_InitializeGraphicsBackend(object sender, EventArgs e) { - Control.Context.MakeCurrent(); + glArea.Context.MakeCurrent(); Callback.OnInitializeBackend(Widget, new InitializeEventArgs(RenderSize)); - Control.Render += Control_Render; - Control.Resize += Control_Resize; + glArea.Render += glArea_Render; + glArea.Resize += glArea_Resize; + } + + void Control_InitializeGraphicsBackend(object sender, EventArgs e) + { + Callback.OnInitializeBackend(Widget, new InitializeEventArgs(RenderSize)); } bool skipDraw; - private void Control_Resize(object o, ResizeArgs args) + private void glArea_Resize(object o, ResizeArgs args) { skipDraw = false; Callback.OnResize(Widget, new ResizeEventArgs(RenderSize)); } - void Control_Render(object o, RenderArgs args) + void glArea_Render(object o, RenderArgs args) { if (!skipDraw) { @@ -92,11 +90,11 @@ void Control_Render(object o, RenderArgs args) } // TODO: Figure this one out! The docstring for this property in Veldrid's OpenGLPlatformInfo is ambiguous. - IntPtr VeldridSurface.IOpenGL.OpenGLContextHandle => Control.Context.Handle; + IntPtr VeldridSurface.IOpenGL.OpenGLContextHandle => glArea?.Context.Handle ?? IntPtr.Zero; IntPtr VeldridSurface.IOpenGL.GetProcAddress(string name) => X11Interop.glXGetProcAddress(name); - void VeldridSurface.IOpenGL.MakeCurrent(IntPtr context) => Control.MakeCurrent(); + void VeldridSurface.IOpenGL.MakeCurrent(IntPtr context) => glArea?.MakeCurrent(); IntPtr VeldridSurface.IOpenGL.GetCurrentContext() => Gdk.GLContext.Current.Handle; @@ -111,9 +109,9 @@ void VeldridSurface.IOpenGL.SwapBuffers() // GLArea doesn't support drawing directly, so we queue a render but don't actually call OnDraw if (skipDraw) return; - + skipDraw = true; - Control.QueueRender(); + glArea?.QueueRender(); } void VeldridSurface.IOpenGL.SetSyncToVerticalBlank(bool on) @@ -131,13 +129,40 @@ void VeldridSurface.IOpenGL.ResizeSwapchain(uint width, uint height) void Eto.Forms.Control.IHandler.Invalidate(Rectangle rect, bool invalidateChildren) { skipDraw = false; - Control.QueueRender(); + glArea?.QueueRender(); } void Eto.Forms.Control.IHandler.Invalidate(bool invalidateChildren) { skipDraw = false; - Control.QueueRender(); + glArea?.QueueRender(); + } + + + protected override void Initialize() + { + base.Initialize(); + + if (Widget.Backend == GraphicsBackend.OpenGL) + { + glArea = new GLArea(); + glArea.CanFocus = true; + + // Veldrid technically supports as low as OpenGL 3.0, but the full + // complement of features is only available with 3.3 and higher. + glArea.SetRequiredVersion(3, 3); + + glArea.HasDepthBuffer = true; + glArea.HasStencilBuffer = true; + Control.Child = glArea; + glArea.Realized += glArea_InitializeGraphicsBackend; + } + else + { + Control.CanFocus = true; + Control.Realized += Control_InitializeGraphicsBackend; + } + } } } diff --git a/src/Eto.Veldrid/VeldridSurface.cs b/src/Eto.Veldrid/VeldridSurface.cs index 56cf71e..ba37d74 100755 --- a/src/Eto.Veldrid/VeldridSurface.cs +++ b/src/Eto.Veldrid/VeldridSurface.cs @@ -12,6 +12,7 @@ namespace Eto.Veldrid [Handler(typeof(VeldridSurface.IHandler))] public class VeldridSurface : Control { + [AutoInitialize(false)] public new interface IHandler : Control.IHandler { Size RenderSize { get; } @@ -72,7 +73,7 @@ public interface IOpenGL public GraphicsBackend Backend { get; private set; } public GraphicsDevice GraphicsDevice { get; private set; } - public GraphicsDeviceOptions GraphicsDeviceOptions { get; private set; } = new GraphicsDeviceOptions(); + public GraphicsDeviceOptions GraphicsDeviceOptions { get; private set; } public Swapchain Swapchain { get; private set; } public const string VeldridInitializedEvent = "VeldridSurface.VeldridInitialized"; @@ -100,13 +101,15 @@ public VeldridSurface() { } public VeldridSurface(GraphicsBackend backend) + : this(backend, new GraphicsDeviceOptions()) { - Backend = backend; } + public VeldridSurface(GraphicsBackend backend, GraphicsDeviceOptions gdOptions) { Backend = backend; GraphicsDeviceOptions = gdOptions; + Initialize(); } private static GraphicsBackend GetPreferredBackend() diff --git a/test/TestEtoVeldrid.Gtk/TestEtoVeldrid.Gtk.csproj b/test/TestEtoVeldrid.Gtk/TestEtoVeldrid.Gtk.csproj index 57e15ce..e2d5cf6 100644 --- a/test/TestEtoVeldrid.Gtk/TestEtoVeldrid.Gtk.csproj +++ b/test/TestEtoVeldrid.Gtk/TestEtoVeldrid.Gtk.csproj @@ -1,8 +1,8 @@  - + WinExe - net5.0;net472 + net5.0;net48 linux-x64 @@ -20,5 +20,5 @@ - + diff --git a/test/TestEtoVeldrid.Mac/TestEtoVeldrid.Mac64.csproj b/test/TestEtoVeldrid.Mac/TestEtoVeldrid.Mac64.csproj index c5dd2b5..86f5b8a 100644 --- a/test/TestEtoVeldrid.Mac/TestEtoVeldrid.Mac64.csproj +++ b/test/TestEtoVeldrid.Mac/TestEtoVeldrid.Mac64.csproj @@ -2,7 +2,7 @@ WinExe - net472;netcoreapp3.1 + net48;net5.0 MONOMAC osx-x64 diff --git a/test/TestEtoVeldrid.WinForms/TestEtoVeldrid.WinForms.csproj b/test/TestEtoVeldrid.WinForms/TestEtoVeldrid.WinForms.csproj index 0f0ed9f..38f7c23 100644 --- a/test/TestEtoVeldrid.WinForms/TestEtoVeldrid.WinForms.csproj +++ b/test/TestEtoVeldrid.WinForms/TestEtoVeldrid.WinForms.csproj @@ -9,7 +9,8 @@ WinExe - net472;netcoreapp3.1 + net48 + $(TargetFrameworks);net5.0-windows true win-x64 diff --git a/test/TestEtoVeldrid.Wpf/TestEtoVeldrid.Wpf.csproj b/test/TestEtoVeldrid.Wpf/TestEtoVeldrid.Wpf.csproj index 9924015..13a18be 100644 --- a/test/TestEtoVeldrid.Wpf/TestEtoVeldrid.Wpf.csproj +++ b/test/TestEtoVeldrid.Wpf/TestEtoVeldrid.Wpf.csproj @@ -1,7 +1,7 @@  - true + true @@ -9,7 +9,8 @@ WinExe - net472;netcoreapp3.1 + net48 + $(TargetFrameworks);net5.0-windows app1.manifest true win-x64