A swap chain used for rendering to a secondary GameWindow.
This is an extension and not part of stock XNA. It is currently implemented for Windows and DirectX only.
Inheritance: RenderTarget2D
		/// <summary>
		/// Initializes the control.
		/// </summary>
		protected override void OnCreateControl()
		{
			// Don't initialize the graphics device if we are running in the designer.
			if(!DesignMode)
			{
				_graphicsDeviceService = GraphicsDeviceService.AddRef(Handle,
																	 ClientSize.Width,
																	 ClientSize.Height);

				Services.AddService<IGraphicsDeviceService>(_graphicsDeviceService);


				_renderTarget = new SwapChainRenderTarget(GraphicsDevice, Handle, ClientSize.Width, ClientSize.Height);

				// Register the service, so components like ContentManager can find it.
				//services.AddService<IGraphicsDeviceService>(graphicsDeviceService);

				// Give derived classes a chance to initialize themselves.
				Initialize();

				//Set the XNA mouse handing to use this window
				//Mouse.WindowHandle = Handle;

				return;
			}

			base.OnCreateControl();
		}
        /// <summary>
        /// Helper used by BeginDraw. This checks the graphics device status,
        /// making sure it is big enough for drawing the current control, and
        /// that the device is not lost. Returns an error string if the device
        /// could not be reset.
        /// </summary>
        string HandleDeviceReset()
        {
            bool deviceNeedsReset = false;

            switch (GraphicsDevice.GraphicsDeviceStatus)
            {
                case GraphicsDeviceStatus.Lost:
                    // If the graphics device is lost, we cannot use it at all.
                    return "Graphics device lost";

                case GraphicsDeviceStatus.NotReset:
                    // If device is in the not-reset state, we should try to reset it.
                    deviceNeedsReset = true;
                    break;

                default:
                    // If the device state is ok, check whether it is big enough.
                    PresentationParameters pp = GraphicsDevice.PresentationParameters;

                    deviceNeedsReset = (ClientSize.Width != pp.BackBufferWidth) ||
                                       (ClientSize.Height != pp.BackBufferHeight);
                    break;
            }

            // Do we need to reset the device?
            if (deviceNeedsReset)
            {
                try
                {
                    //graphicsDeviceService.ResetDevice(ClientSize.Width,
                                                      //ClientSize.Height);
                    graphicsDeviceService.ResetDevice(_renderTarget.Width, _renderTarget.Height);

                    _renderTarget.Dispose();
                    _renderTarget = new SwapChainRenderTarget(GraphicsDevice, Handle, ClientSize.Width, ClientSize.Height);
                
                }
                catch (Exception e)
                {
                    return "Graphics device reset failed\n\n" + e;
                }
            }

            return null;
        }
        /// <summary>
        /// Disposes the control.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (graphicsDeviceService != null)
            {
                graphicsDeviceService.Release(disposing);
                graphicsDeviceService = null;
            }

            if (_renderTarget != null) {
                _renderTarget.Dispose();
                _renderTarget = null;
            }

            base.Dispose(disposing);
        }
        /// <inheritdoc/>
        bool IPresentationTarget.BeginRender(RenderContext context)
        {
            if (context == null)
            throw new ArgumentNullException("context");

              var graphicsDevice = GraphicsService.GraphicsDevice;
              int width = ClientSize.Width;
              int height = ClientSize.Height;

            #if MONOGAME
              if (_renderTarget == null
              || _renderTarget.GraphicsDevice != graphicsDevice
              || _renderTarget.Width != width
              || _renderTarget.Height != height)
              {
            if (_renderTarget == null)
              _renderTarget = new SwapChainRenderTarget(graphicsDevice, Handle, width, height);
            else
              _renderTarget.Resize(width, height);
              }

              graphicsDevice.SetRenderTarget(_renderTarget);
              context.RenderTarget = _renderTarget;
            #else
              bool deviceNeedsReset;
              switch (graphicsDevice.GraphicsDeviceStatus)
              {
            case GraphicsDeviceStatus.Lost:
              return false;

            case GraphicsDeviceStatus.NotReset:
              deviceNeedsReset = true;
              break;

            default:
              var presentationParameters = graphicsDevice.PresentationParameters;
              deviceNeedsReset = (width > presentationParameters.BackBufferWidth)
                             || (height > presentationParameters.BackBufferHeight);
              break;
              }

              if (deviceNeedsReset)
              {
            try
            {
              var presentationParameters = graphicsDevice.PresentationParameters;
              presentationParameters.BackBufferWidth = Math.Max(width, presentationParameters.BackBufferWidth);
              presentationParameters.BackBufferHeight = Math.Max(height, presentationParameters.BackBufferHeight);
              graphicsDevice.Reset(presentationParameters);
            }
            catch (Exception)
            {
              return false;
            }
              }
            #endif

              context.Viewport = new Viewport(0, 0, width, height);
              _rendering = true;
              return true;
        }
Example #5
0
 void RemakeTarget()
 {
     //const int step = 64;
     if (target == null || target.Width != ClientSize.Width || target.Height != ClientSize.Height) {
         if (target != null) target.Dispose();
         int width = ClientSize.Width; //(int)Math.Ceiling((float)ClientSize.Width / step) * step;
         int height = ClientSize.Height; //(int)Math.Ceiling((float)ClientSize.Height / step) * step;
         target = new SwapChainRenderTarget(GraphicsManager.device, Handle, width, height, false, SurfaceFormat.Color, DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents, PresentInterval.Default);
         frame.canvas = new Canvas(target);
     }
 }
    /// <inheritdoc/>
    bool IPresentationTarget.BeginRender(RenderContext context)
    {
      if (context == null)
        throw new ArgumentNullException("context");

      var graphicsDevice = GraphicsService.GraphicsDevice;

      if (_renderTarget == null
          || _renderTarget.GraphicsDevice != graphicsDevice
          || _requiresResize)
      {
        if (_renderTarget == null)
        {
          _renderTarget = new SwapChainRenderTarget(
            graphicsDevice,
            this,
            _width,
            _height,
            _compositionScaleX,
            _compositionScaleY,
            false,
            SurfaceFormat.Color,
            DepthFormat.Depth24Stencil8,
            0,
            RenderTargetUsage.PreserveContents,
            PresentInterval.Default);
        }
        else
        {
          _renderTarget.Resize(_width, _height, _compositionScaleX, _compositionScaleY);
        }

        _requiresResize = false;
      }

      graphicsDevice.SetRenderTarget(_renderTarget);
      context.RenderTarget = _renderTarget;
      context.Viewport = new Viewport(0, 0, _renderTarget.Width, _renderTarget.Height);
      _rendering = true;
      return true;
    }
 private void RemoveRenderTarget()
 {
   lock (Lock ?? _dummyLock)
   {
     _renderTarget.SafeDispose();
     _renderTarget = null;
   }
 }