/// <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 alocated. 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); }
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); }
/// <inheritdoc /> 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; if (!element.IsDisposed) { element.SetBackbuffer(presenter.BackBuffer); } return(presenter); }
/// <summary> /// Request a screenshot and save it to disc. /// </summary> /// <param name="game">The game.</param> /// <param name="screenShotUrl">The screenshot URL.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="pixelFormat">The pixel format.</param> /// <param name="depthBufferFormat">The depth buffer format.</param> /// <param name="fileType">Type of the file.</param> /// <returns> /// True on success /// </returns> public static bool SaveScreenshot(GameBase game, string screenshotUrl, int width, int height, PixelFormat pixelFormat = PixelFormat.R8G8B8A8_UNorm, PixelFormat depthBufferFormat = PixelFormat.D24_UNorm_S8_UInt, ImageFileType fileType = ImageFileType.Png) { var status = true; var graphicsContext = game.GraphicsContext; var graphicsDevice = game.GraphicsDevice; var commandList = graphicsContext.CommandList; var gameSystems = game.GameSystems; var drawTime = game.DrawTime; var previousPresenter = graphicsDevice.Presenter; try { // set the master output var renderTarget = graphicsContext.Allocator.GetTemporaryTexture2D(width, height, pixelFormat, TextureFlags.ShaderResource | TextureFlags.RenderTarget); var depthStencil = graphicsContext.Allocator.GetTemporaryTexture2D(width, height, depthBufferFormat, TextureFlags.DepthStencil); var tempPresenter = new RenderTargetGraphicsPresenter(graphicsDevice, renderTarget, depthStencil.ViewFormat); try { // temp presenter graphicsDevice.Presenter = tempPresenter; // Always clear the state of the GraphicsDevice to make sure a scene doesn't start with a wrong setup commandList.ClearState(); // render the screenshot graphicsContext.ResourceGroupAllocator.Reset(graphicsContext.CommandList); gameSystems.Draw(drawTime); // write the screenshot to the file renderTarget.SaveTexture(commandList, screenshotUrl, fileType); } finally { // Cleanup graphicsDevice.Presenter = previousPresenter; tempPresenter.Dispose(); graphicsContext.Allocator.ReleaseReference(depthStencil); graphicsContext.Allocator.ReleaseReference(renderTarget); } } catch (Exception e) { status = false; } return(status); }
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); } } }
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; }
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); }