Esempio n. 1
0
        private static VkSurfaceKHR CreateUIViewSurface(VkGraphicsDevice gd, VkInstance instance, UIViewSwapchainSource uiViewSource, bool hasExtMetalSurface)
        {
            CAMetalLayer metalLayer = CAMetalLayer.New();
            UIView       uiView     = new UIView(uiViewSource.UIView);

            metalLayer.frame  = uiView.frame;
            metalLayer.opaque = true;
            uiView.layer.addSublayer(metalLayer.NativePtr);

            if (hasExtMetalSurface)
            {
                VkMetalSurfaceCreateInfoEXT surfaceCI = new VkMetalSurfaceCreateInfoEXT();
                surfaceCI.sType  = VkMetalSurfaceCreateInfoEXT.VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
                surfaceCI.pLayer = metalLayer.NativePtr.ToPointer();
                VkSurfaceKHR surface;
                VkResult     result = gd.CreateMetalSurfaceEXT(instance, &surfaceCI, null, &surface);
                CheckResult(result);
                return(surface);
            }
            else
            {
                VkIOSSurfaceCreateInfoMVK surfaceCI = VkIOSSurfaceCreateInfoMVK.New();
                surfaceCI.pView = uiView.NativePtr.ToPointer();
                VkResult result = vkCreateIOSSurfaceMVK(instance, ref surfaceCI, null, out VkSurfaceKHR surface);
                return(surface);
            }
        }
        void SetupMetal()
        {
            // Find a usable device
            device = MTLDevice.SystemDefault;

            // Create a new command queue
            commandQueue = device.CreateCommandQueue();

            // Load all the shader files with a metal file extension in the project
            NSError error;

            defaultLibrary = device.CreateLibrary("default.metallib", out error);

            // Setup metal layer and add as sub layer to view
            metalLayer             = new CAMetalLayer();
            metalLayer.Device      = device;
            metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm;

            // Change this to NO if the compute encoder is used as the last pass on the drawable texture
            metalLayer.FramebufferOnly = true;

            // Add metal layer to the views layer hierarchy
            metalLayer.Frame = View.Layer.Frame;
            View.Layer.AddSublayer(metalLayer);

            View.Opaque             = true;
            View.BackgroundColor    = null;
            View.ContentScaleFactor = UIScreen.MainScreen.Scale;
        }
Esempio n. 3
0
        private static unsafe VkSurfaceKHR CreateNSWindowSurface(VkGraphicsDevice gd, VkInstance instance, NSWindowSwapchainSource nsWindowSource, bool hasExtMetalSurface)
        {
            CAMetalLayer metalLayer  = CAMetalLayer.New();
            NSWindow     nswindow    = new NSWindow(nsWindowSource.NSWindow);
            NSView       contentView = nswindow.contentView;

            contentView.wantsLayer = true;
            contentView.layer      = metalLayer.NativePtr;

            if (hasExtMetalSurface)
            {
                VkMetalSurfaceCreateInfoEXT surfaceCI = new VkMetalSurfaceCreateInfoEXT();
                surfaceCI.sType  = VkMetalSurfaceCreateInfoEXT.VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
                surfaceCI.pLayer = metalLayer.NativePtr.ToPointer();
                VkSurfaceKHR surface;
                VkResult     result = gd.CreateMetalSurfaceEXT(instance, &surfaceCI, null, &surface);
                CheckResult(result);
                return(surface);
            }
            else
            {
                VkMacOSSurfaceCreateInfoMVK surfaceCI = VkMacOSSurfaceCreateInfoMVK.New();
                surfaceCI.pView = contentView.NativePtr.ToPointer();
                VkResult result = vkCreateMacOSSurfaceMVK(instance, ref surfaceCI, null, out VkSurfaceKHR surface);
                CheckResult(result);
                return(surface);
            }
        }
Esempio n. 4
0
        public MTLSwapchainFramebuffer(
            MTLGraphicsDevice gd,
            CAMetalLayer layer,
            uint width,
            uint height,
            PixelFormat?depthFormat,
            PixelFormat colorFormat)
            : base()
        {
            _gd    = gd;
            _layer = layer;

            OutputAttachmentDescription?depthAttachment = null;

            if (depthFormat != null)
            {
                _depthFormat    = depthFormat;
                depthAttachment = new OutputAttachmentDescription(depthFormat.Value);
                RecreateDepthTexture(width, height);
                _depthTarget = new FramebufferAttachment(_depthTexture, 0);
            }
            OutputAttachmentDescription colorAttachment = new OutputAttachmentDescription(colorFormat);

            OutputDescription   = new OutputDescription(depthAttachment, colorAttachment);
            _placeholderTexture = new MTLPlaceholderTexture();
            _placeholderTexture.Resize(width, height);
            _colorTargets = new[] { new FramebufferAttachment(_placeholderTexture, 0) };
        }
Esempio n. 5
0
 void SetupMetal()
 {
     metalLayer = (CAMetalLayer)Layer;
     // Setup metal layer and add as sub layer to view
     metalLayer.Opaque      = true;
     metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm;
     // Change this to NO if the compute encoder is used as the last pass on the drawable texture
     metalLayer.FramebufferOnly = true;
 }
Esempio n. 6
0
        public ImageView(IntPtr handle) : base(handle)
        {
            Opaque          = true;
            BackgroundColor = UIColor.Clear;

            metalLayer = (CAMetalLayer)Layer;

            device = MTLDevice.SystemDefault;

            metalLayer.Device          = device;
            metalLayer.PixelFormat     = MTLPixelFormat.BGRA8Unorm;
            metalLayer.FramebufferOnly = true;
        }
Esempio n. 7
0
		public ImageView (IntPtr handle) : base (handle)
		{
			Opaque = true;
			BackgroundColor = UIColor.Clear;

			metalLayer = (CAMetalLayer)Layer;

			device = MTLDevice.SystemDefault;

			metalLayer.Device = device;
			metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm;
			metalLayer.FramebufferOnly = true;
		}
        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);
        }
Esempio n. 9
0
        private static VkSurfaceKHR CreateUIViewSurface(VkInstance instance, UIViewSwapchainSource uiViewSource)
        {
            CAMetalLayer metalLayer = CAMetalLayer.New();
            UIView       uiView     = new UIView(uiViewSource.UIView);

            metalLayer.frame  = uiView.frame;
            metalLayer.opaque = true;
            uiView.layer.addSublayer(metalLayer.NativePtr);

            VkIOSSurfaceCreateInfoMVK surfaceCI = VkIOSSurfaceCreateInfoMVK.New();

            surfaceCI.pView = uiView.NativePtr.ToPointer();
            VkResult result = vkCreateIOSSurfaceMVK(instance, ref surfaceCI, null, out VkSurfaceKHR surface);

            return(surface);
        }
Esempio n. 10
0
        private static VkSurfaceKHR CreateNSWindowSurface(VkInstance instance, NSWindowSwapchainSource nsWindowSource)
        {
            CAMetalLayer metalLayer  = CAMetalLayer.New();
            NSWindow     nswindow    = new NSWindow(nsWindowSource.NSWindow);
            NSView       contentView = nswindow.contentView;

            contentView.wantsLayer = true;
            contentView.layer      = metalLayer.NativePtr;

            VkMacOSSurfaceCreateInfoMVK surfaceCI = VkMacOSSurfaceCreateInfoMVK.New();

            surfaceCI.pView = contentView.NativePtr.ToPointer();
            VkResult result = vkCreateMacOSSurfaceMVK(instance, ref surfaceCI, null, out VkSurfaceKHR surface);

            CheckResult(result);
            return(surface);
        }
Esempio n. 11
0
        public GameView(IntPtr handle) : base(handle)
        {
            Opaque          = true;
            BackgroundColor = UIColor.Clear;

            metalLayer         = (CAMetalLayer)Layer;
            ContentScaleFactor = UIScreen.MainScreen.Scale;
            metalLayer.PresentsWithTransaction = false;
            metalLayer.DrawsAsynchronously     = true;

            device = MTLDevice.SystemDefault;

            metalLayer.Device          = device;
            metalLayer.PixelFormat     = MTLPixelFormat.BGRA8Unorm;
            metalLayer.FramebufferOnly = true;

            drawableSize = Bounds.Size;
        }
Esempio n. 12
0
		public GameView (IntPtr handle) : base (handle)
		{
			Opaque = true;
			BackgroundColor = UIColor.Clear;

			metalLayer = (CAMetalLayer)Layer;
			ContentScaleFactor = UIScreen.MainScreen.Scale;
			metalLayer.PresentsWithTransaction = false;
			metalLayer.DrawsAsynchronously = true;

			device = MTLDevice.SystemDefault;

			metalLayer.Device = device;
			metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm;
			metalLayer.FramebufferOnly = true;

			drawableSize = Bounds.Size;
		}
Esempio n. 13
0
        public override void Init(IntPtr display, IntPtr window, uint samples, bool vsync, bool sRGB)
        {
            _display = display;
            _window  = window;

            while (!_dev.SupportsTextureSampleCount(samples))
            {
                samples >>= 1;
            }
            _samples = samples;

            _layer                 = new CAMetalLayer();
            _layer.Device          = _dev;
            _layer.PixelFormat     = sRGB ? MTLPixelFormat.BGRA8Unorm_sRGB : MTLPixelFormat.BGRA8Unorm;
            _layer.FramebufferOnly = true;

#if __IOS__
            UIView view = (UIView)ObjCRuntime.Runtime.GetNSObject(window);
            view.Layer.AddSublayer(_layer);
#else
            // Mac
            NSView view = (NSView)ObjCRuntime.Runtime.GetNSObject(window);
            view.Layer.AddSublayer(_layer);
#endif
            _cmdQueue = _dev.CreateCommandQueue();

            _descriptor = new MTLRenderPassDescriptor();
            _descriptor.ColorAttachments[0].ClearColor = new MTLClearColor(0.0, 1.0, 0.0, 0.0);

            if (samples > 1)
            {
                _descriptor.ColorAttachments[0].StoreAction = MTLStoreAction.MultisampleResolve;
            }
            else
            {
                _descriptor.ColorAttachments[0].StoreAction = MTLStoreAction.Store;
            }

            _descriptor.StencilAttachment.ClearStencil = 0;
            _descriptor.StencilAttachment.LoadAction   = MTLLoadAction.Clear;
            _descriptor.StencilAttachment.StoreAction  = MTLStoreAction.DontCare;

            _device = new Noesis.RenderDeviceMTL(_dev.Handle, sRGB);
        }
Esempio n. 14
0
        public unsafe NativeWindow(
            GraphicsService graphicsService,
            string title,
            int x, int y,
            int width, int height,
            SDL_WindowFlags flags
            )
        {
            _width           = width;
            _height          = height;
            _graphicsService = graphicsService;
            _handle          = SDL2Native.SDL_CreateWindow(
                title,
                x, y,
                width, height,
                flags
                );

            //get sdl version
            var sysWindowInfo = new SDL_SysWMinfo();

            SDL2Native.SDL_GetVersion(&sysWindowInfo.version);
            _version = sysWindowInfo.version;
            if (SDL2Native.SDL_GetWMWindowInfo(
                    _handle,
                    &sysWindowInfo
                    ) == 0)
            {
                throw new Exception("couldn't retrive sdl window information");
            }

            VkResult     error;
            VkSurfaceKHR surface;

            if (sysWindowInfo.subsystem == SysWMType.Windows)
            {
                var processHandle = (
                    Process
                    .GetCurrentProcess()
                    .SafeHandle
                    .DangerousGetHandle()
                    );
                var win32Info   = Unsafe.Read <Win32WindowInfo>(&sysWindowInfo.info);
                var surfaceInfo = new VkWin32SurfaceCreateInfoKHR
                {
                    sType     = VkStructureType.Win32SurfaceCreateInfoKHR,
                    hinstance = processHandle,
                    hwnd      = win32Info.window
                };
                error = VulkanNative.vkCreateWin32SurfaceKHR(
                    graphicsService.Handle,
                    &surfaceInfo,
                    null,
                    &surface
                    );
            }
            else if (sysWindowInfo.subsystem == SysWMType.X11)
            {
                var x11Info     = Unsafe.Read <X11WindowInfo>(&sysWindowInfo.info);
                var surfaceInfo = new VkXlibSurfaceCreateInfoKHR
                {
                    sType  = VkStructureType.XlibSurfaceCreateInfoKHR,
                    dpy    = (Vulkan.Xlib.Display *)x11Info.display,
                    window = new Vulkan.Xlib.Window
                    {
                        Value = x11Info.window
                    }
                };
                error = VulkanNative.vkCreateXlibSurfaceKHR(
                    graphicsService.Handle,
                    &surfaceInfo,
                    null,
                    &surface
                    );
            }
            else if (sysWindowInfo.subsystem == SysWMType.Wayland)
            {
                var waylandINfo = Unsafe.Read <WaylandWindowInfo>(&sysWindowInfo.info);
                var surfaceInfo = new VkWaylandSurfaceCreateInfoKHR
                {
                    sType   = VkStructureType.WaylandSurfaceCreateInfoKHR,
                    display = (Vulkan.Wayland.wl_display *)waylandINfo.display,
                    surface = (Vulkan.Wayland.wl_surface *)waylandINfo.surface
                };
                error = VulkanNative.vkCreateWaylandSurfaceKHR(
                    graphicsService.Handle,
                    &surfaceInfo,
                    null,
                    &surface
                    );
            }
            else if (sysWindowInfo.subsystem == SysWMType.Android)
            {
                var androidInfo = Unsafe.Read <AndroidWindowInfo>(&sysWindowInfo.info);
                var surfaceInfo = new VkAndroidSurfaceCreateInfoKHR
                {
                    sType  = VkStructureType.AndroidSurfaceCreateInfoKHR,
                    window = (Vulkan.Android.ANativeWindow *)androidInfo.window
                };
                error = VulkanNative.vkCreateAndroidSurfaceKHR(
                    graphicsService.Handle,
                    &surfaceInfo,
                    null,
                    &surface
                    );
            }
            else if (sysWindowInfo.subsystem == SysWMType.Mir)
            {
                var mirInfo     = Unsafe.Read <MirWindowInfo>(&sysWindowInfo.info);
                var surfaceInfo = new VkMirSurfaceCreateInfoKHR
                {
                    sType      = VkStructureType.MirSurfaceCreateInfoKHR,
                    connection = (Vulkan.Mir.MirConnection *)mirInfo.connection,
                    mirSurface = (Vulkan.Mir.MirSurface *)mirInfo.mirSurface
                };
                error = VulkanNative.vkCreateMirSurfaceKHR(
                    graphicsService.Handle,
                    &surfaceInfo,
                    null,
                    &surface
                    );
            }
            else if (sysWindowInfo.subsystem == SysWMType.Cocoa)
            {
                var cocaInfo = Unsafe.Read <CocoaWindowInfo>(&sysWindowInfo.info);

                var nsWindow    = new NSWindow(cocaInfo.Window);
                var contentView = nsWindow.contentView;
                contentView.wantsLayer = true;
                contentView.layer      = CAMetalLayer.New().NativePtr;

                var surfaceInfo = new VkMacOSSurfaceCreateInfoMVK
                {
                    sType = VkStructureType.MacosSurfaceCreateInfoMvk,
                    pView = nsWindow.contentView.NativePtr.ToPointer()
                };
                error = VulkanNative.vkCreateMacOSSurfaceMVK(
                    graphicsService.Handle,
                    &surfaceInfo,
                    null,
                    &surface
                    );
            }
            else
            {
                throw new PlatformNotSupportedException("this platform is currently not supported");
            }

            if (error != VkResult.Success)
            {
                throw new Exception("failed to create window surface");
            }

            _surface = surface;
        }
Esempio n. 15
0
        void SetupMetal()
        {
            // Find a usable device
            device = MTLDevice.SystemDefault;

            // Create a new command queue
            commandQueue = device.CreateCommandQueue ();

            // Load all the shader files with a metal file extension in the project
            NSError error;

            defaultLibrary = device.CreateLibrary ("default.metallib", out error);

            // Setup metal layer and add as sub layer to view
            metalLayer = new CAMetalLayer ();
            metalLayer.Device = device;
            metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm;

            // Change this to NO if the compute encoder is used as the last pass on the drawable texture
            metalLayer.FramebufferOnly = true;

            // Add metal layer to the views layer hierarchy
            metalLayer.Frame = View.Layer.Frame;
            View.Layer.AddSublayer (metalLayer);

            View.Opaque = true;
            View.BackgroundColor = null;
            View.ContentScaleFactor = UIScreen.MainScreen.Scale;
        }