예제 #1
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;
        }
예제 #2
0
        /// <summary>
        /// Initialize using Metal drivers
        /// </summary>
        /// <param name="handle">Provide a MetalKit View handle</param>
        /// <returns>Metal based render</returns>
        public static VeldridRender InitFromMetal(IntPtr handle)
        {
            try
            {
                var gd = GraphicsDevice.CreateMetal(new GraphicsDeviceOptions(), new SwapchainDescription(SwapchainSource.CreateUIView(handle), 20, 20, null, false));
                gd.ResourceFactory.CreateSwapchain(new SwapchainDescription(SwapchainSource.CreateUIView(handle), 20, 20, null, false)).Resize(20, 20);
                gd.WaitForIdle();

                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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
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);
        }