示例#1
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 + ".");
            }
        }
        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.CreateNSView(Control.Handle);

                swapchain = Widget.GraphicsDevice.ResourceFactory.CreateSwapchain(
                    new SwapchainDescription(
                        source,
                        (uint)RenderWidth,
                        (uint)RenderHeight,
                        Widget.GraphicsDeviceOptions.SwapchainDepthFormat,
                        Widget.GraphicsDeviceOptions.SyncToVerticalBlank,
                        Widget.GraphicsDeviceOptions.SwapchainSrgbFormat));
            }

            return(swapchain);
        }
示例#3
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            SwapchainSource      ss  = SwapchainSource.CreateUIView(this.View.Handle);
            SwapchainDescription scd = new SwapchainDescription(
                ss,
                (uint)View.Frame.Width,
                (uint)View.Frame.Height,
                PixelFormat.R32_Float,
                false);

            if (_backend == GraphicsBackend.Metal)
            {
                _gd = GraphicsDevice.CreateMetal(_options);
                _sc = _gd.ResourceFactory.CreateSwapchain(ref scd);
            }
            else if (_backend == GraphicsBackend.OpenGLES)
            {
                _gd = GraphicsDevice.CreateOpenGLES(_options, scd);
                _sc = _gd.MainSwapchain;
            }
            else if (_backend == GraphicsBackend.Vulkan)
            {
                throw new NotImplementedException();
            }

            GraphicsDeviceCreated?.Invoke(_gd, _gd.ResourceFactory, _sc);
            _viewLoaded = true;
        }
示例#4
0
        public static VeldridRender InitFromMetal(SwapchainSource scs)
        {
            try
            {
                var gd = GraphicsDevice.CreateMetal(new GraphicsDeviceOptions(), new SwapchainDescription(scs, 20, 20, null, false));

                gd.WaitForIdle();

                gd.ResizeMainWindow(270, 800);

                VeldridRender render = new VeldridRender(gd);

                byte[] data = Helpers.GetAssetByteArray("App.Shaders.Shader.metal");

                render.Shaders = new Shader[]
                {
                    render.graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Vertex, data, "shader_vertex", true)),
                    render.graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Fragment, data, "shader_fragment", true))
                };
                return(render);
            }
            catch (Exception ex)
            {
                Logger.AddLog(ex);
            }
            return(null);
        }
示例#5
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);
        }
示例#6
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);
        }
示例#7
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.CreateXlib(
                    X11Interop.gdk_x11_display_get_xdisplay(Control.Display.Handle),
                    X11Interop.gdk_x11_window_get_xid(Control.Window.Handle));

                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);
        }
示例#8
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 + ".");
            }
        }
示例#9
0
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            if (holder is null)
            {
                throw new ArgumentNullException(nameof(holder));
            }

            VeldridSwapchainSource = SwapchainSource.CreateAndroidSurface(holder.Surface.Handle, JNIEnv.Handle);
            Ready?.Invoke(this, EventArgs.Empty);
        }
示例#10
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);
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            View.BackgroundColor = UIColor.SystemPinkColor;

            // device init
            GraphicsDeviceOptions options = new GraphicsDeviceOptions(false, null, false, ResourceBindingModel.Improved);

#if DEBUG
            options.Debug = true;
#endif
            SwapchainSource      ss  = SwapchainSource.CreateUIView(this.View.Handle);
            SwapchainDescription scd = new SwapchainDescription(
                ss,
                width, height,
                PixelFormat.R32_Float,
                false);

            graphicsDevice = GraphicsDevice.CreateMetal(options);
            swapchain      = graphicsDevice.ResourceFactory.CreateSwapchain(ref scd);
            factory        = graphicsDevice.ResourceFactory;

            // resource init
            CreateSizeDependentResources();
            VertexPosition[] quadVertices =
            {
                new VertexPosition(new Vector3(-1,  1, 0)),
                new VertexPosition(new Vector3(1,   1, 0)),
                new VertexPosition(new Vector3(-1, -1, 0)),
                new VertexPosition(new Vector3(1,  -1, 0))
            };
            uint[] quadIndices = new uint[]
            {
                0,
                1,
                2,
                1,
                3,
                2
            };
            vertexBuffer = factory.CreateBuffer(new BufferDescription(4 * VertexPosition.SizeInBytes, BufferUsage.VertexBuffer));
            indexBuffer  = factory.CreateBuffer(new BufferDescription(6 * sizeof(uint), BufferUsage.IndexBuffer));
            graphicsDevice.UpdateBuffer(vertexBuffer, 0, quadVertices);
            graphicsDevice.UpdateBuffer(indexBuffer, 0, quadIndices);

            commandList = factory.CreateCommandList();

            viewLoaded = true;

            displayLink = CADisplayLink.Create(Render);
            displayLink.PreferredFramesPerSecond = 60;
            displayLink.AddToRunLoop(NSRunLoop.Main, NSRunLoop.NSDefaultRunLoopMode);
        }
示例#12
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);
        }
示例#13
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // device init
            GraphicsDeviceOptions options = new GraphicsDeviceOptions(false, null, false, ResourceBindingModel.Improved);

#if DEBUG
            options.Debug = true;
#endif
            SwapchainSource      ss  = SwapchainSource.CreateNSView(this.View.Handle);
            SwapchainDescription scd = new SwapchainDescription(
                ss,
                width, height,
                PixelFormat.R32_Float,
                false);

            graphicsDevice = GraphicsDevice.CreateMetal(options);
            swapchain      = graphicsDevice.ResourceFactory.CreateSwapchain(ref scd);
            factory        = graphicsDevice.ResourceFactory;

            // resource init
            CreateSizeDependentResources();
            VertexPosition[] quadVertices =
            {
                new VertexPosition(new Vector3(-1,  1, 0)),
                new VertexPosition(new Vector3(1,   1, 0)),
                new VertexPosition(new Vector3(-1, -1, 0)),
                new VertexPosition(new Vector3(1,  -1, 0))
            };
            uint[] quadIndices = new uint[]
            {
                0,
                1,
                2,
                1,
                3,
                2
            };
            vertexBuffer = factory.CreateBuffer(new BufferDescription(4 * VertexPosition.SizeInBytes, BufferUsage.VertexBuffer));
            indexBuffer  = factory.CreateBuffer(new BufferDescription(6 * sizeof(uint), BufferUsage.IndexBuffer));
            graphicsDevice.UpdateBuffer(vertexBuffer, 0, quadVertices);
            graphicsDevice.UpdateBuffer(indexBuffer, 0, quadIndices);

            commandList = factory.CreateCommandList();

            viewLoaded = true;

            displayTimer = NSTimer.CreateRepeatingTimer(60.0 / 1000.0, Render);
            displayTimer.Fire();
        }
        public MTLSwapchain(MTLGraphicsDevice gd, ref SwapchainDescription description)
        {
            _gd = gd;
            SyncToVerticalBlank = description.SyncToVerticalBlank;

            _metalLayer = CAMetalLayer.New();

            uint width;
            uint height;

            SwapchainSource source = description.Source;

            if (source is NSWindowSwapchainSource nsWindowSource)
            {
                NSWindow nswindow          = new NSWindow(nsWindowSource.NSWindow);
                CGSize   windowContentSize = nswindow.contentView.frame.size;
                width  = (uint)windowContentSize.width;
                height = (uint)windowContentSize.height;
                NSView contentView = nswindow.contentView;
                contentView.wantsLayer = true;
                contentView.layer      = _metalLayer.NativePtr;
            }
            else if (source is UIViewSwapchainSource uiViewSource)
            {
                _uiView = new UIView(uiViewSource.UIView);
                CGSize viewSize = _uiView.frame.size;
                width              = (uint)viewSize.width;
                height             = (uint)viewSize.height;
                _metalLayer.frame  = _uiView.frame;
                _metalLayer.opaque = true;
                _uiView.layer.addSublayer(_metalLayer.NativePtr);
            }
            else
            {
                throw new VeldridException($"A Metal Swapchain can only be created from an NSWindow or UIView.");
            }

            _metalLayer.device          = _gd.Device;
            _metalLayer.pixelFormat     = MTLPixelFormat.BGRA8Unorm;
            _metalLayer.framebufferOnly = true;
            _metalLayer.drawableSize    = new CGSize(width, height);
            GetNextDrawable();

            _framebuffer = new MTLSwapchainFramebuffer(
                gd,
                this,
                width,
                height,
                description.DepthFormat,
                PixelFormat.B8_G8_R8_A8_UNorm);
        }
示例#15
0
        public static GraphicsDevice CreateDefaultD3D11GraphicsDevice(
            GraphicsDeviceOptions options,
            Sdl2Window window)
        {
            SwapchainSource      source        = GetSwapchainSource(window);
            SwapchainDescription swapchainDesc = new SwapchainDescription(
                source,
                (uint)window.Width, (uint)window.Height,
                options.SwapchainDepthFormat,
                options.SyncToVerticalBlank,
                options.SwapchainSrgbFormat);

            return(GraphicsDevice.CreateD3D11(options, swapchainDesc));
        }
        public VeldridImGuiWindow(GraphicsDevice gd, ImGuiViewportPtr vp)
        {
            _gcHandle = GCHandle.Alloc(this);
            _gd       = gd;
            _vp       = vp;

            SDL_WindowFlags flags = SDL_WindowFlags.Hidden;

            if ((vp.Flags & ImGuiViewportFlags.NoTaskBarIcon) != 0)
            {
                flags |= SDL_WindowFlags.SkipTaskbar;
            }
            if ((vp.Flags & ImGuiViewportFlags.NoDecoration) != 0)
            {
                flags |= SDL_WindowFlags.Borderless;
            }
            else
            {
                flags |= SDL_WindowFlags.Resizable;
            }

            if ((vp.Flags & ImGuiViewportFlags.TopMost) != 0)
            {
                flags |= SDL_WindowFlags.AlwaysOnTop;
            }

            _window = new Sdl2Window(
                "No Title Yet",
                (int)vp.Pos.X, (int)vp.Pos.Y,
                (int)vp.Size.X, (int)vp.Size.Y,
                flags,
                false);
            _window.Resized += () => _vp.PlatformRequestResize = true;
            _window.Moved   += p => _vp.PlatformRequestMove = true;
            _window.Closed  += () => _vp.PlatformRequestClose = true;

            SwapchainSource      scSource = VeldridStartup.GetSwapchainSource(_window);
            SwapchainDescription scDesc   = new SwapchainDescription(scSource, (uint)_window.Width, (uint)_window.Height, null, true, false);

            _sc              = _gd.ResourceFactory.CreateSwapchain(scDesc);
            _window.Resized += () => _sc.Resize((uint)_window.Width, (uint)_window.Height);

            unsafe
            {
                ViewportDataPtr data = new ViewportDataPtr(Marshal.AllocHGlobal(Unsafe.SizeOf <ViewportDataPtr>()));
                vp.PlatformUserData = new HandleRef(data, (IntPtr)data.NativePtr).Handle;
            }
            vp.PlatformUserData = (IntPtr)_gcHandle;
        }
示例#17
0
        private void InitializeSubWindow(GraphicsDevice gd, Sdl2Window sdlWindow)
        {
            GcHandle       = GCHandle.Alloc(this);
            graphicsDevice = gd;
            VSync          = gd.SyncToVerticalBlank;
            window         = sdlWindow;

            SwapchainSource      scSrc  = WindowStartup.GetSwapchainSource(sdlWindow);
            SwapchainDescription scDesc = new SwapchainDescription(scSrc, (uint)sdlWindow.Width, (uint)sdlWindow.Height,
                                                                   gd.SwapchainFramebuffer.OutputDescription.DepthAttachment?.Format, VSync);

            swapchain = gd.ResourceFactory.CreateSwapchain(scDesc);

            window.Resized += () => swapchain.Resize((uint)window.Width, (uint)window.Height);
        }
示例#18
0
        private static unsafe GraphicsDevice CreateMetalGraphicsDevice(
            GraphicsDeviceOptions options,
            Sdl2Window window,
            bool colorSrgb)
        {
            SwapchainSource      source        = GetSwapchainSource(window);
            SwapchainDescription swapchainDesc = new SwapchainDescription(
                source,
                (uint)window.Width, (uint)window.Height,
                options.SwapchainDepthFormat,
                options.SyncToVerticalBlank,
                colorSrgb);

            return(GraphicsDevice.CreateMetal(options, swapchainDesc));
        }
        internal static VkSurfaceKHR CreateSurface(VkInstance instance, SwapchainSource swapchainSource)
        {
            switch (swapchainSource)
            {
            case XlibSwapchainSource xlibSource:
                return(CreateXlib(instance, xlibSource));

            case Win32SwapchainSource win32Source:
                return(CreateWin32(instance, win32Source));

            case AndroidSurfaceSwapchainSource androidSource:
                return(CreateAndroidSurface(instance, androidSource));

            default:
                throw new VeldridException($"The provided SwapchainSource cannot be used to create a Vulkan surface.");
            }
        }
示例#20
0
 internal static unsafe VkSurfaceSource CreateFromSwapchainSource(SwapchainSource source)
 {
     if (source is Win32SwapchainSource win32Source)
     {
         return(new Win32VkSurfaceInfo(win32Source.Hinstance, win32Source.Hwnd));
     }
     else if (source is XlibSwapchainSource xlibSource)
     {
         return(new XlibVkSurfaceInfo((Display *)xlibSource.Display, new Window {
             Value = xlibSource.Window
         }));
     }
     else
     {
         throw new VeldridException("Unsupported Vulkan SwapchainSource.");
     }
 }
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            bool deviceCreated = false;

            if (_backend == GraphicsBackend.Vulkan)
            {
                if (GraphicsDevice == null)
                {
                    SwapchainSource      ss = SwapchainSource.CreateAndroidSurface(holder.Surface.Handle, JNIEnv.Handle);
                    SwapchainDescription sd = new SwapchainDescription(
                        ss,
                        (uint)Width,
                        (uint)Height,
                        DeviceOptions.SwapchainDepthFormat,
                        DeviceOptions.SyncToVerticalBlank);
                    GraphicsDevice = GraphicsDevice.CreateVulkan(DeviceOptions, sd);
                    deviceCreated  = true;
                }

                Debug.Assert(MainSwapchain == null);

                MainSwapchain = GraphicsDevice.MainSwapchain;
            }
            else
            {
                Debug.Assert(GraphicsDevice == null && MainSwapchain == null);
                SwapchainSource      ss = SwapchainSource.CreateAndroidSurface(holder.Surface.Handle, JNIEnv.Handle);
                SwapchainDescription sd = new SwapchainDescription(
                    ss,
                    (uint)Width,
                    (uint)Height,
                    DeviceOptions.SwapchainDepthFormat,
                    DeviceOptions.SyncToVerticalBlank);
                GraphicsDevice = GraphicsDevice.CreateOpenGLES(DeviceOptions, sd);
                MainSwapchain  = GraphicsDevice.MainSwapchain;
                deviceCreated  = true;
            }

            if (deviceCreated)
            {
                DeviceCreated?.Invoke();
            }

            _surfaceCreated = true;
        }
示例#22
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;
        }
        public override void MovedToWindow()
        {
            base.MovedToWindow();

            var swapchainSource      = SwapchainSource.CreateUIView(Handle);
            var swapchainDescription = new SwapchainDescription(swapchainSource, (uint)Frame.Width, (uint)Frame.Height, null, true, true);

            if (_backend == GraphicsBackend.Metal)
            {
                GraphicsDevice = GraphicsDevice.CreateMetal(_deviceOptions);
            }

            MainSwapchain = GraphicsDevice.ResourceFactory.CreateSwapchain(swapchainDescription);

            DeviceReady?.Invoke();

            _displayLink = CADisplayLink.Create(HandleDisplayLinkOutputCallback);
            _displayLink.AddToRunLoop(NSRunLoop.Current, NSRunLoopMode.Default);
        }
示例#24
0
        public override void ViewDidMoveToWindow()
        {
            base.ViewDidMoveToWindow();

            var swapchainSource      = SwapchainSource.CreateNSView(Handle);
            var swapchainDescription = new SwapchainDescription(swapchainSource, (uint)Frame.Width, (uint)Frame.Height, null, true, true);

            if (_backend == GraphicsBackend.Metal)
            {
                GraphicsDevice = GraphicsDevice.CreateMetal(_deviceOptions);
            }

            MainSwapchain = GraphicsDevice.ResourceFactory.CreateSwapchain(swapchainDescription);

            DeviceReady?.Invoke();

            _displayLink = new CVDisplayLink();
            _displayLink.SetOutputCallback(HandleDisplayLinkOutputCallback);
            _displayLink.Start();
        }
示例#25
0
        public void Ctor_SetsProperties(PixelFormat?depthFormat, bool syncToVerticalBlank)
        {
            Sdl2Window           window        = new Sdl2Window("SwapchainTestWindow", 0, 0, 100, 100, SDL_WindowFlags.Hidden, false);
            SwapchainSource      source        = VeldridStartup.GetSwapchainSource(window);
            SwapchainDescription swapchainDesc = new SwapchainDescription(source, 100, 100, depthFormat, syncToVerticalBlank);
            Swapchain            swapchain     = RF.CreateSwapchain(ref swapchainDesc);

            if (depthFormat == null)
            {
                Assert.Null(swapchain.Framebuffer.DepthTarget);
            }
            else
            {
                Assert.NotNull(swapchain.Framebuffer.DepthTarget);
                Assert.Equal(depthFormat, swapchain.Framebuffer.DepthTarget.Value.Target.Format);
            }

            Assert.Equal(syncToVerticalBlank, swapchain.SyncToVerticalBlank);

            window.Close();
        }
示例#26
0
        public IRenderBase GetRender()
        {
            MetalInfo();
            TaskCompletionSource <VeldridRender> tcs = new TaskCompletionSource <VeldridRender>();

            App.RunOnMainThread(() =>
            {
                //GPU won't always load without these
                Veldrid.MetalBindings.MTLDevice.MTLCreateSystemDefaultDevice();
                MTLDevice.SystemDefault.Dispose();

                //Thread.Sleep(50);

                UIView view         = new UIView();
                SwapchainSource scs = SwapchainSource.CreateUIView(new UIView().Handle);//Apparently necessary to not native crash when creating Color buffer on A10 processors(A12 won't need)
                //SwapchainSource scs = SwapchainSource.CreateUIView(UIApplication.SharedApplication.KeyWindow.RootViewController.View.Handle);//Apparently necessary to not native crash when creating Color buffer on A10 processors(A12 won't need)

                //UIApplication.SharedApplication.KeyWindow.Add(view);

                /*
                 *              Thread.Sleep(50);
                 *
                 *              try
                 *              {
                 *                  ViewPtr = UIApplication.SharedApplication.Windows[0].InputViewController.View.Handle;
                 *              }
                 *              catch { }*/

                // IntPtr Handler = UIApplication.SharedApplication.Windows[0].RootViewController.View.Handle;


                //tcs.TrySetResult(VeldridRender.InitFromMetal(AppDelegate.RootViewControllerHandle));
                tcs.SetResult(VeldridRender.InitFromMetal(scs));
            });

            var render = tcs.Task.Result;

            MetalInfo();
            return(render);
        }
示例#27
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;
                }
            }
        }
示例#28
0
        private static GraphicsDevice Init(GraphicsBackend backend, GraphicsDeviceOptions options, SwapchainSource swapchainSource, uint startWidth, uint startHeight)
        {
            if (!GraphicsDevice.IsBackendSupported(backend) ||
                backend == GraphicsBackend.OpenGL)
            {
                throw new NotSupportedException($"Graphics backend {backend} is not supported on this system.");
            }

            if (swapchainSource is null)
            {
                throw new ArgumentNullException(nameof(swapchainSource));
            }

            var swapchainDescription = new SwapchainDescription
            {
                Source              = swapchainSource,
                Width               = startWidth,
                Height              = startHeight,
                DepthFormat         = options.SwapchainDepthFormat,
                SyncToVerticalBlank = options.SyncToVerticalBlank,
                ColorSrgb           = options.SwapchainSrgbFormat
            };

            var device = backend switch
            {
                GraphicsBackend.Direct3D11 => GraphicsDevice.CreateD3D11(options, swapchainDescription),
                GraphicsBackend.Metal => GraphicsDevice.CreateMetal(options, swapchainDescription),
                GraphicsBackend.Vulkan => GraphicsDevice.CreateVulkan(options, swapchainDescription),
                GraphicsBackend.OpenGLES => GraphicsDevice.CreateOpenGLES(options, swapchainDescription),
                _ => throw new InvalidOperationException($"Can't create a device for GraphicsBackend value: {backend}")
            };

            return(device);
        }
示例#29
0
        internal static VkSurfaceKHR CreateSurface(VkGraphicsDevice gd, VkInstance instance, SwapchainSource swapchainSource)
        {
            // TODO a null GD is passed from VkSurfaceSource.CreateSurface for compatibility
            //      when VkSurfaceInfo is removed we do not have to handle gd == null anymore
            var doCheck = gd != null;

            if (doCheck && !gd.HasSurfaceExtension(CommonStrings.VK_KHR_SURFACE_EXTENSION_NAME))
            {
                throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_SURFACE_EXTENSION_NAME}");
            }

            switch (swapchainSource)
            {
            case XlibSwapchainSource xlibSource:
                if (doCheck && !gd.HasSurfaceExtension(CommonStrings.VK_KHR_XLIB_SURFACE_EXTENSION_NAME))
                {
                    throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_XLIB_SURFACE_EXTENSION_NAME}");
                }
                return(CreateXlib(instance, xlibSource));

            case WaylandSwapchainSource waylandSource:
                if (doCheck && !gd.HasSurfaceExtension(CommonStrings.VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME))
                {
                    throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME}");
                }
                return(CreateWayland(instance, waylandSource));

            case Win32SwapchainSource win32Source:
                if (doCheck && !gd.HasSurfaceExtension(CommonStrings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME))
                {
                    throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME}");
                }
                return(CreateWin32(instance, win32Source));

            case AndroidSurfaceSwapchainSource androidSource:
                if (doCheck && !gd.HasSurfaceExtension(CommonStrings.VK_KHR_ANDROID_SURFACE_EXTENSION_NAME))
                {
                    throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_ANDROID_SURFACE_EXTENSION_NAME}");
                }
                return(CreateAndroidSurface(instance, androidSource));

            case NSWindowSwapchainSource nsWindowSource:
                if (doCheck && !gd.HasSurfaceExtension(CommonStrings.VK_MVK_MACOS_SURFACE_EXTENSION_NAME))
                {
                    throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_MVK_MACOS_SURFACE_EXTENSION_NAME}");
                }
                return(CreateNSWindowSurface(instance, nsWindowSource));

            case UIViewSwapchainSource uiViewSource:
                if (doCheck && !gd.HasSurfaceExtension(CommonStrings.VK_MVK_IOS_SURFACE_EXTENSION_NAME))
                {
                    throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_MVK_IOS_SURFACE_EXTENSION_NAME}");
                }
                return(CreateUIViewSurface(instance, uiViewSource));

            default:
                throw new VeldridException($"The provided SwapchainSource cannot be used to create a Vulkan surface.");
            }
        }