Пример #1
0
        protected override void CreatePresenter(GraphicsDevice device, PresentationParameters parameters, object newControl = null)
        {
            if(!(gameWindow is GameWindowPhoneXaml)) return;

            parameters = TryGetParameters(device, parameters);

            DisposeGraphicsPresenter(device);

            var renderTargetDesc = new Texture2DDescription
            {
                Format = Format.B8G8R8A8_UNorm,
                Width = parameters.BackBufferWidth,
                Height = parameters.BackBufferHeight,
                ArraySize = 1,
                MipLevels = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Usage = ResourceUsage.Default,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.SharedKeyedmutex | ResourceOptionFlags.SharedNthandle,
                SampleDescription = new DXGI.SampleDescription(1, 0)
            };

            var backBuffer = RenderTarget2D.New(device, renderTargetDesc);

            var graphicsPresenter = new RenderTargetGraphicsPresenter(device, backBuffer, parameters.DepthStencilFormat, true);
            device.Presenter = graphicsPresenter;

            var gameWindowXaml = (GameWindowPhoneXaml)gameWindow;
            gameWindowXaml.CreateSynchronizedTexture(backBuffer);
        }
Пример #2
0
        void IDrawingSurfaceBackgroundContentProviderNative.Draw(Device device, DeviceContext context, RenderTargetView renderTargetView)
        {
            try
            {
                RenderTarget2D localBackBuffer = null;
                if (!Exiting)
                {
                    if (GraphicsDevice == null)
                    {
                        GraphicsDevice = GraphicsDevice.New(device);

                        renderTargets[0].NativePointer = renderTargetView.NativePointer;
                        renderTargets[0].RenderTarget = RenderTarget2D.New(GraphicsDevice, renderTargetView, true);
                        BackBuffer = renderTargets[0].RenderTarget;
                        nextRenderTarget++;

                        graphicsPresenter = new RenderTargetGraphicsPresenter(GraphicsDevice, BackBuffer);
                        GraphicsDevice.Presenter = graphicsPresenter;
                        initCallback();
                    }
                    else
                    {
                        if (((Direct3D11.Device)GraphicsDevice).NativePointer != device.NativePointer || ((Direct3D11.DeviceContext)GraphicsDevice).NativePointer != context.NativePointer)
                        {
                            System.Diagnostics.Debugger.Break();
                        }

                        // Find any previous render target that was already alocated.
                        bool foundRenderTarget = false;
                        foreach (var renderTargetLocal in renderTargets)
                        {
                            if (renderTargetLocal.NativePointer == renderTargetView.NativePointer)
                            {
                                BackBuffer = renderTargetLocal.RenderTarget;
                                graphicsPresenter.SetBackBuffer(BackBuffer);
                                foundRenderTarget = true;
                                break;
                            }
                        }

                        if (!foundRenderTarget)
                        {
                            // Dispose any previous render target.
                            renderTargets[nextRenderTarget].Dispose();

                            // Creates the new BackBuffer and associated it to the GraphicsPresenter
                            BackBuffer = RenderTarget2D.New(GraphicsDevice, renderTargetView, true);
                            graphicsPresenter.SetBackBuffer(BackBuffer);

                            renderTargets[nextRenderTarget].NativePointer = renderTargetView.NativePointer;
                            renderTargets[nextRenderTarget].RenderTarget = BackBuffer;
                            nextRenderTarget++;
                        }

                        // TODO: Check that new device/devicecontext/renderTargetView are the same
                        // else we need to handle DeviceReset/Remove...etc.
                    }

                    tickCallback();

                    // Aks the host for additional frame
                    host.RequestAdditionalFrame();
                }
            } catch (Exception ex)
            {
                // System.Diagnostics.Debugger.Break();
            }
        }
        /// <summary>
        /// Creates a <see cref="RenderTargetGraphicsPresenter" /> if not already created based from latest internal device.
        /// </summary>
        /// <param name="resetPresenter">if set to <c>true</c> [reset presenter].</param>
        internal void EnsurePresenter(bool resetPresenter)
        {
            // Find any previous render target that was already allocated.
            if (!resetPresenter)
            {
                foreach (RenderTargetLocal renderTargetLocal in renderTargets)
                {
                    if (renderTargetLocal.NativePointer == currentRenderTargetView.NativePointer)
                    {
                        backBuffer = renderTargetLocal.RenderTarget;
                        graphicsPresenter.SetBackBuffer(backBuffer);
                        return;
                    }
                }
            }

            // Creates the backbuffer
            backBuffer = RenderTarget2D.New(graphicsDevice, currentRenderTargetView, true);
            currentRenderTargetView.Tag = backBuffer;

            // Dispose any previous render target.
            renderTargets[nextRenderTarget].Dispose();
            renderTargets[nextRenderTarget].NativePointer = currentRenderTargetView.NativePointer;
            renderTargets[nextRenderTarget].RenderTarget = backBuffer;
            nextRenderTarget = (nextRenderTarget + 1) % renderTargets.Length;

            if (resetPresenter)
            {
                graphicsPresenter = new RenderTargetGraphicsPresenter(graphicsDevice, backBuffer, RequestDepthFormat);
                graphicsDevice.Presenter = graphicsPresenter;
            }

            graphicsPresenter.SetBackBuffer(backBuffer);
        }
Пример #4
0
        internal override GraphicsPresenter CreateGraphicsPresenter(GraphicsDevice device, PresentationParameters parameters)
        {
            var backbufferDesc = RenderTarget2D.CreateDescription(device,
                                                              parameters.BackBufferWidth,
                                                              parameters.BackBufferHeight,
                                                              Graphics.PixelFormat.B8G8R8A8.UNorm);

            // mandatory to share the surface with D3D9
            backbufferDesc.OptionFlags |= ResourceOptionFlags.Shared;

            RemoveAndDispose(ref presenter);
            RemoveAndDispose(ref queryForCompletion);

            presenter = ToDispose(new RenderTargetGraphicsPresenter(device, backbufferDesc, parameters.DepthStencilFormat, false, true));
            // used to indicate if all drawing operations have completed
            queryForCompletion = ToDispose(new Query(presenter.GraphicsDevice, new QueryDescription { Type = QueryType.Event, Flags = QueryFlags.None }));

            // device context will be used to query drawing operations status
            deviceContext = ((Device)presenter.GraphicsDevice).ImmediateContext;
            element.SetBackbuffer(presenter.BackBuffer);
            return presenter;
        }
Пример #5
0
        public override GraphicsDevice CreateDevice(GraphicsDeviceInformation deviceInformation)
        {
            var gameWindowBackgroundXaml = gameWindow as GameWindowPhoneBackgroundXaml;
            if (gameWindowBackgroundXaml != null)
            {
                // We don't have anything else than the GraphicsDevice created for the XAML so return it directly.
                return gameWindowBackgroundXaml.EnsureDevice();
            }

            // Else this is a DrawingSruface 
            var device = GraphicsDevice.New(deviceInformation.Adapter, deviceInformation.DeviceCreationFlags, deviceInformation.GraphicsProfile);

            var renderTargetDesc = new Texture2DDescription
            {
                Format = Format.B8G8R8A8_UNorm,
                Width = (int)deviceInformation.PresentationParameters.BackBufferWidth,
                Height = (int)deviceInformation.PresentationParameters.BackBufferHeight,
                ArraySize = 1,
                MipLevels = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Usage = ResourceUsage.Default,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.SharedKeyedmutex | ResourceOptionFlags.SharedNthandle,
                SampleDescription = new DXGI.SampleDescription(1, 0)
            };

            var backBuffer = RenderTarget2D.New(device, renderTargetDesc);

            var graphicsPresenter = new RenderTargetGraphicsPresenter(device, backBuffer, deviceInformation.PresentationParameters.DepthStencilFormat, true);
            device.Presenter = graphicsPresenter;

            var gameWindowXaml = (GameWindowPhoneXaml)gameWindow;
            gameWindowXaml.CreateSynchronizedTexture(backBuffer);

            return device;
        }
Пример #6
0
        private void DXControl_Loaded(object sender, EventArgs args)
        {
            _services = new GameServiceRegistry();
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                InitializeGraphicsDevice();
                InitializeContentManager();

                if (LoadContent != null)
                    LoadContent(this, new DXGraphicsDeviceEventArgs(GraphicsDevice));
            }
            _imageSourcePresenter = new RenderTargetGraphicsPresenter(GraphicsDevice, _imageSource.RenderTarget);
            CompositionTarget.Rendering += CompositionTarget_Rendering;
            _ready = true;
        }
Пример #7
0
        private void DoResize()
        {
            if (_imageSource != null)
            {
                _imageSource.Dispose();
                _imageSource = new DXImageSource(
                    GraphicsDevice, (int)ActualWidth, (int)ActualHeight);
                _imageSourcePresenter = new RenderTargetGraphicsPresenter(GraphicsDevice, _imageSource.RenderTarget);

                rootImage.Source = _imageSource.WriteableBitmap;
                if (Resized != null)
                {
                    Resized(this, EventArgs.Empty);
                }
            }
        }
        void IDrawingSurfaceContentProviderNative.GetTexture(DrawingSizeF surfaceSize, out DrawingSurfaceSynchronizedTexture synchronizedTexture, out RectangleF textureSubRectangle)
        {
            try
            {
                if (!Exiting)
                {
                    if (GraphicsDevice == null)
                    {
                        GraphicsDevice = GraphicsDevice.New();

                        var renderTargetDesc = new Texture2DDescription
                                               {
                                                   Format = DXGI.Format.B8G8R8A8_UNorm,
                                                   Width = (int)surfaceSize.Width,
                                                   Height = (int)surfaceSize.Height,
                                                   ArraySize = 1,
                                                   MipLevels = 1,
                                                   BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                                                   Usage = ResourceUsage.Default,
                                                   CpuAccessFlags = CpuAccessFlags.None,
                                                   OptionFlags = ResourceOptionFlags.SharedKeyedmutex | ResourceOptionFlags.SharedNthandle,
                                                   SampleDescription = new DXGI.SampleDescription(1, 0)
                                               };

                        renderTarget = ToDispose(Graphics.Texture2D.New(GraphicsDevice, renderTargetDesc));
                        BackBuffer = ToDispose(RenderTarget2D.New(GraphicsDevice, new RenderTargetView(GraphicsDevice, renderTarget)));

                        graphicsPresenter = new RenderTargetGraphicsPresenter(GraphicsDevice, BackBuffer);
                        GraphicsDevice.Presenter = graphicsPresenter;
                        InitCallback();
                    }

                    if(this.synchronizedTexture == null)
                        this.synchronizedTexture = host.CreateSynchronizedTexture((Texture2D)renderTarget);

                    this.synchronizedTexture.BeginDraw();

                    RunCallback();

                    host.RequestAdditionalFrame();

                    this.synchronizedTexture.EndDraw();
                }
            }
            catch (Exception ex)
            {
                // TODO: As we are in a callback from a native code, we cannot pass back this exception,
                // so how to pass back this exception to the user at an appropriate time?
                drawException = ex;
                Debug.WriteLine(drawException);
            }

            // Set output parameters.
            textureSubRectangle = new RectangleF(0f, 0f, surfaceSize.Width, surfaceSize.Height);
            synchronizedTexture = this.synchronizedTexture;
        }
        void IDrawingSurfaceBackgroundContentProviderNative.Draw(Device device, DeviceContext context,
                                                                 RenderTargetView renderTargetView)
        {
            try
            {
                RenderTarget2D localBackBuffer = null;
                if (!Exiting)
                {
                    if (GraphicsDevice == null)
                    {
                        GraphicsDevice = GraphicsDevice.New(device);

                        renderTargets[0].NativePointer = renderTargetView.NativePointer;
                        renderTargets[0].RenderTarget = RenderTarget2D.New(GraphicsDevice, renderTargetView, true);
                        BackBuffer = renderTargets[0].RenderTarget;
                        nextRenderTarget++;

                        graphicsPresenter = new RenderTargetGraphicsPresenter(GraphicsDevice, BackBuffer);
                        GraphicsDevice.Presenter = graphicsPresenter;
                        InitCallback();
                    }
                    else
                    {
                        if (((Device) GraphicsDevice).NativePointer != device.NativePointer ||
                            ((DeviceContext) GraphicsDevice).NativePointer != context.NativePointer)
                        {
                            Debugger.Break();
                        }

                        // Find any previous render target that was already alocated.
                        bool foundRenderTarget = false;
                        foreach (RenderTargetLocal renderTargetLocal in renderTargets)
                        {
                            if (renderTargetLocal.NativePointer == renderTargetView.NativePointer)
                            {
                                BackBuffer = renderTargetLocal.RenderTarget;
                                graphicsPresenter.SetBackBuffer(BackBuffer);
                                foundRenderTarget = true;
                                break;
                            }
                        }

                        if (!foundRenderTarget)
                        {
                            // Dispose any previous render target.
                            renderTargets[nextRenderTarget].Dispose();

                            // Creates the new BackBuffer and associated it to the GraphicsPresenter
                            BackBuffer = RenderTarget2D.New(GraphicsDevice, renderTargetView, true);
                            graphicsPresenter.SetBackBuffer(BackBuffer);

                            renderTargets[nextRenderTarget].NativePointer = renderTargetView.NativePointer;
                            renderTargets[nextRenderTarget].RenderTarget = BackBuffer;
                            nextRenderTarget++;
                        }

                        // TODO: Check that new device/devicecontext/renderTargetView are the same
                        // else we need to handle DeviceReset/Remove...etc.
                    }

                    RunCallback();

                    // Aks the host for additional frame
                    host.RequestAdditionalFrame();
                }
            }
            catch (Exception ex)
            {
                // TODO: As we are in a callback from a native code, we cannot pass back this exception,
                // so how to pass back this exception to the user at an appropriate time?
                drawException = ex;
                Debug.WriteLine(drawException);
            }
        }
Пример #10
0
        internal override GraphicsPresenter CreateGraphicsPresenter(GraphicsDevice device, PresentationParameters parameters)
        {
            var backbufferDesc = RenderTarget2D.CreateDescription(device,
                                                              parameters.BackBufferWidth,
                                                              parameters.BackBufferHeight,
                                                              Graphics.PixelFormat.B8G8R8A8.UNorm);

            // mandatory to share the surface with D3D9
            backbufferDesc.OptionFlags |= ResourceOptionFlags.Shared;

            if (presenter != null)
                RemoveAndDispose(ref presenter);

            presenter = ToDispose(new RenderTargetGraphicsPresenter(device, backbufferDesc, DepthFormat.None, false, true));
            element.SetBackbuffer(presenter.BackBuffer);
            return presenter;
        }