Exemple #1
0
        public Swapchain CreateSwapchain()
        {
            Swapchain swapchain;

            if (Widget.Backend == GraphicsBackend.OpenGL)
            {
                swapchain = Widget.GraphicsDevice.MainSwapchain;
            }
            else
            {
                // To embed Veldrid in an Eto control, these platform-specific
                // versions of CreateSwapchain use the technique outlined here:
                //
                //   https://github.com/mellinoe/veldrid/issues/155
                //
                var source = SwapchainSource.CreateWin32(
                    WinFormsControl.Handle,
                    Marshal.GetHINSTANCE(typeof(VeldridSurface).Module));

                var renderSize = RenderSize;
                swapchain = Widget.GraphicsDevice.ResourceFactory.CreateSwapchain(
                    new SwapchainDescription(
                        source,
                        (uint)renderSize.Width,
                        (uint)renderSize.Height,
                        Widget.GraphicsDeviceOptions.SwapchainDepthFormat,
                        Widget.GraphicsDeviceOptions.SyncToVerticalBlank,
                        Widget.GraphicsDeviceOptions.SwapchainSrgbFormat));
            }

            return(swapchain);
        }
Exemple #2
0
        public static unsafe SwapchainSource GetSwapchainSource(Sdl2Window window)
        {
            IntPtr        sdlHandle = window.SdlWindowHandle;
            SDL_SysWMinfo sysWmInfo;

            Sdl2Native.SDL_GetVersion(&sysWmInfo.version);
            Sdl2Native.SDL_GetWMWindowInfo(sdlHandle, &sysWmInfo);
            switch (sysWmInfo.subsystem)
            {
            case SysWMType.Windows:
                Win32WindowInfo w32Info = Unsafe.Read <Win32WindowInfo>(&sysWmInfo.info);
                return(SwapchainSource.CreateWin32(w32Info.Sdl2Window, w32Info.hinstance));

            case SysWMType.X11:
                X11WindowInfo x11Info = Unsafe.Read <X11WindowInfo>(&sysWmInfo.info);
                return(SwapchainSource.CreateXlib(
                           x11Info.display,
                           x11Info.Sdl2Window));

            case SysWMType.Cocoa:
                CocoaWindowInfo cocoaInfo = Unsafe.Read <CocoaWindowInfo>(&sysWmInfo.info);
                IntPtr          nsWindow  = cocoaInfo.Window;
                return(SwapchainSource.CreateNSWindow(nsWindow));

            default:
                throw new PlatformNotSupportedException("Cannot create a SwapchainSource for " + sysWmInfo.subsystem + ".");
            }
        }
Exemple #3
0
        private static SwapchainSource GetSwapchainSource(IntPtr window)
        {
            SDL.SDL_SysWMinfo sysWmInfo = new SDL.SDL_SysWMinfo();
            SDL.SDL_GetVersion(out sysWmInfo.version);
            SDL.SDL_GetWindowWMInfo(window, ref sysWmInfo);
            switch (sysWmInfo.subsystem)
            {
            case SDL.SDL_SYSWM_TYPE.SDL_SYSWM_WINDOWS:
                ref var w32Info = ref sysWmInfo.info.win;
                return(SwapchainSource.CreateWin32(w32Info.window, w32Info.hdc));

            case SDL.SDL_SYSWM_TYPE.SDL_SYSWM_X11:
                ref var x11Info = ref sysWmInfo.info.x11;
                return(SwapchainSource.CreateXlib(
                           x11Info.display,
                           x11Info.window));

            case SDL.SDL_SYSWM_TYPE.SDL_SYSWM_COCOA:
                ref var cocoaInfo = ref sysWmInfo.info.cocoa;
                var     nsWindow  = cocoaInfo.window;
                return(SwapchainSource.CreateNSWindow(nsWindow));

            default:
                throw new PlatformNotSupportedException("Cannot create a SwapchainSource for " + sysWmInfo.subsystem + ".");
            }
        }
Exemple #4
0
        public VeldridControl(GraphicsDevice graphics, GraphicsDeviceOptions options)
        {
            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            DoubleBuffered = false;

            var hWnd      = Handle; // Will call CreateHandle internally
            var hInstance = HInstance;

            uint w = (uint)Width, h = (uint)Height;

            if (w <= 0)
            {
                w = 1;
            }
            if (h <= 0)
            {
                h = 1;
            }

            var source = SwapchainSource.CreateWin32(hWnd, hInstance);
            var desc   = new SwapchainDescription(source, w, h, options.SwapchainDepthFormat, options.SyncToVerticalBlank);

            Swapchain = graphics.ResourceFactory.CreateSwapchain(desc);

            Camera = new PerspectiveCamera(Width, Height);
        }
Exemple #5
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var currentModule = typeof(VeldridPanel).Module;
            var hinstance     = Marshal.GetHINSTANCE(currentModule);

            VeldridSwapchainSource = SwapchainSource.CreateWin32(Handle, hinstance);
            Ready?.Invoke(this, EventArgs.Empty);
        }
Exemple #6
0
        protected virtual void CreateSwapchain()
        {
            var dpiScale = GetDpiScale();
            var width    = (uint)(ActualWidth < 0 ? 0 : Math.Ceiling(ActualWidth * dpiScale));
            var height   = (uint)(ActualHeight < 0 ? 0 : Math.Ceiling(ActualHeight * dpiScale));

            var mainModule  = typeof(VeldridComponent).Module;
            var hinstance   = Marshal.GetHINSTANCE(mainModule);
            var win32Source = SwapchainSource.CreateWin32(Hwnd, hinstance);
            var scDesc      = new SwapchainDescription(win32Source, width, height, PixelFormat.R32_Float, true);

            _sc = _gd.ResourceFactory.CreateSwapchain(scDesc);
        }
Exemple #7
0
        public VeldridControl(GraphicsBackend backend, GraphicsDeviceOptions deviceOptions)
        {
            if (!(backend == GraphicsBackend.Vulkan || backend == GraphicsBackend.OpenGL || backend == GraphicsBackend.Direct3D11))
            {
                throw new NotSupportedException($"{backend} is not supported on windows.");
            }

            if (backend == GraphicsBackend.OpenGL)
            {
                throw new NotSupportedException($"{backend} is not currently implemented in this demo.");
            }

            _backend      = backend;
            DeviceOptions = deviceOptions;

            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            DoubleBuffered = false;

            NativeMethods.CreateWindow(Handle, MessageHandler, Width, Height, out var hwnd, out var hinstance);

            HWND      = hwnd;
            HInstance = hinstance;

            if (_backend == GraphicsBackend.Vulkan)
            {
                GraphicsDevice = GraphicsDevice.CreateVulkan(deviceOptions);
            }
            else
            {
                GraphicsDevice = GraphicsDevice.CreateD3D11(deviceOptions);
            }

            const double dpiScale = 1;
            uint         width    = (uint)(Width < 0 ? 0 : Math.Ceiling(Width * dpiScale));
            uint         height   = (uint)(Height < 0 ? 0 : Math.Ceiling(Height * dpiScale));

            SwapchainSource      swapchainSource      = SwapchainSource.CreateWin32(HWND, HInstance);
            SwapchainDescription swapchainDescription = new SwapchainDescription(swapchainSource, width, height, PixelFormat.R32_Float, true);

            MainSwapchain = GraphicsDevice.ResourceFactory.CreateSwapchain(swapchainDescription);

            Disposed += OnDisposed;
        }
Exemple #8
0
        protected sealed override void Initialize()
        {
            var mainModule = typeof(VeldridPanel).Module;
            var hinstance  = Marshal.GetHINSTANCE(mainModule);

            VeldridSwapchainSource = SwapchainSource.CreateWin32(Hwnd, hinstance);
            Ready?.Invoke(this, EventArgs.Empty);
            var here = Parent as FrameworkElement;

            while (here != null)
            {
                if (here is Window window)
                {
                    window.Closing += Window_Closing;
                    here            = null;
                }
                else
                {
                    here = here.Parent as FrameworkElement;
                }
            }
        }