private void DrawTearingTest()
        {
            using (Surface surface = _device.GetRenderTarget(0))
            {
                int   left    = _tearingPos;
                int   width   = surface.Description.Width;
                int   height  = surface.Description.Height;
                Size  size    = new Size(4, height);
                Point topLeft = new Point(left, 0);
                if (topLeft.X + size.Width >= width)
                {
                    topLeft.X = 0;
                }

                Rectangle rcTearing = SharpDXExtensions.CreateRectangle(topLeft, size);

                _device.ColorFill(surface, rcTearing, ColorConverter.FromArgb(255, 255, 255, 255));

                topLeft = new Point((rcTearing.Right + 15) % width, 0);
                if (topLeft.X + size.Width >= width)
                {
                    topLeft.X = 0;
                }

                rcTearing = SharpDXExtensions.CreateRectangle(topLeft, size);
                _device.ColorFill(surface, rcTearing, ColorConverter.FromArgb(255, 100, 100, 100));

                _tearingPos = (_tearingPos + 7) % width;
            }
        }
Example #2
0
        /// <summary>
        /// Creates or re-creates the DirectX device and the backbuffer. This is necessary in the initialization phase
        /// of the SkinEngine and after a parameter was changed which affects the DX device creation.
        /// </summary>
        internal static void DoReCreateDevice_MainThread()
        {
            try
            {
                // Note that only the thread which handles window messages is allowed to call CreateDevice and Reset
                // (see http://msdn.microsoft.com/en-us/library/windows/desktop/bb147224%28v=vs.85%29.aspx )
                ServiceRegistration.Get <ILogger>().Debug("GraphicsDevice: Initializing DirectX");
                MPDirect3D.Load();

                // Cleanup-part: Only necessary during re-initialization
                UIResourcesHelper.ReleaseUIResources();
                if (_backBuffer != null)
                {
                    _backBuffer.Dispose();
                }
                if (_device != null)
                {
                    _device.Dispose();
                }
                _device = _setup.SetupDirectX();
                // End cleanup part

                SetupRenderStrategies();
                SetupRenderPipelines();

                Capabilities deviceCapabilities = _device.Capabilities;
                _backBuffer = _device.GetRenderTarget(0);
                _device.MaximumFrameLatency = _setup.PresentParameters.BackBufferCount; // Enables the device to queue as many frames as we have backbuffers defined
                int ordinal = deviceCapabilities.AdapterOrdinal;
                AdapterInformation adapterInfo = MPDirect3D.Direct3D.Adapters[ordinal];
                DisplayMode        currentMode = adapterInfo.CurrentDisplayMode;
                AdaptTargetFrameRateToDisplayMode(currentMode);
                LogScreenMode(currentMode);
                bool firstTimeInitialization = _dxCapabilities == null;
                _dxCapabilities = DxCapabilities.RequestCapabilities(deviceCapabilities, currentMode);
                if (firstTimeInitialization)
                {
                    if (!_dxCapabilities.SupportsShaders)
                    {
                        string text = String.Format("MediaPortal 2 needs a graphics card wich supports shader model 2.0\nYour card does NOT support this.\nMediaportal 2 will continue but migh run slow");
                        MessageBox.Show(text, "GraphicAdapter", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                SetRenderState();
                UIResourcesHelper.ReallocUIResources();
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Critical("GraphicsDevice: Failed to setup DirectX", ex);
                Environment.Exit(0);
            }
        }
Example #3
0
        /// <summary>
        /// Constructs a <see cref="TemporaryRenderTarget"/> instance.
        /// </summary>
        /// <param name="renderTargetIndex">Render target index</param>
        /// <param name="targetSurface">Target surface to render on</param>
        public TemporaryRenderTarget(int renderTargetIndex, Surface targetSurface)
        {
            // Select target index (0 by default)
            _renderTargetIndex = renderTargetIndex;

            // Remember old RenderTarget
            _backBuffer = _device.GetRenderTarget(_renderTargetIndex);

            // Get information of new Texture target
            _targetSurface = targetSurface;

            // Set new target
            _device.SetRenderTarget(_renderTargetIndex, _targetSurface);
        }
Example #4
0
        /// <summary>
        /// Resets the DirectX device. This will release all screens, other UI resources and our back buffer, reset the DX device and realloc
        /// all resources.
        /// </summary>
        public static bool Reset()
        {
            ServiceRegistration.Get <ILogger>().Warn("GraphicsDevice: Resetting DX device...");
            _screenManager.ExecuteWithTempReleasedResources(() => ExecuteInMainThread(() =>
            {
                // Note that the thread which created the device must call this (see http://msdn.microsoft.com/en-us/library/windows/desktop/bb174344%28v=vs.85%29.aspx ).
                // Note also that only the thread which handles window messages is allowed to call CreateDevice and Reset
                // (see http://msdn.microsoft.com/en-us/library/windows/desktop/bb147224%28v=vs.85%29.aspx )
                ServiceRegistration.Get <ILogger>().Debug("GraphicsDevice: Reset DirectX");
                UIResourcesHelper.ReleaseUIResources();

                if (ContentManager.Instance.TotalAllocationSize != 0)
                {
                    ServiceRegistration.Get <ILogger>().Warn("GraphicsDevice: ContentManager.TotalAllocationSize = {0}, should be 0!", ContentManager.Instance.TotalAllocationSize / (1024 * 1024));
                }

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

                _setup.BuildPresentParamsFromSettings();
                PresentParameters parameters = _setup.PresentParameters;
                _device.ResetEx(ref parameters);
                _setup.PresentParameters = parameters;

                SetupRenderStrategies();
                SetupRenderPipelines();

                Capabilities deviceCapabilities = _device.Capabilities;
                int ordinal = deviceCapabilities.AdapterOrdinal;
                AdapterInformation adapterInfo = MPDirect3D.Direct3D.Adapters[ordinal];
                DisplayMode currentMode        = adapterInfo.CurrentDisplayMode;
                AdaptTargetFrameRateToDisplayMode(currentMode);
                ServiceRegistration.Get <ILogger>().Debug("GraphicsDevice: DirectX reset {0}x{1} format: {2} {3} Hz", Width, Height,
                                                          currentMode.Format, TargetFrameRate);
                _backBuffer = _device.GetRenderTarget(0);
                _device.MaximumFrameLatency = _setup.PresentParameters.BackBufferCount; // Enables the device to queue as many frames as we have backbuffers defined
                _dxCapabilities             = DxCapabilities.RequestCapabilities(deviceCapabilities, currentMode);

                ScreenRefreshWorkaround();

                UIResourcesHelper.ReallocUIResources();
            }));
            ServiceRegistration.Get <ILogger>().Warn("GraphicsDevice: Device successfully reset");
            return(true);
        }
        public override void Render(RenderContext parentRenderContext)
        {
            Effect effect = Effect;

            if (!IsVisible || effect == null)
            {
                return;
            }

            RectangleF bounds = ActualBounds;

            if (bounds.Width <= 0 || bounds.Height <= 0)
            {
                return;
            }

            Matrix?layoutTransformMatrix = LayoutTransform == null ? new Matrix?() : LayoutTransform.GetTransform();
            Matrix?renderTransformMatrix = RenderTransform == null ? new Matrix?() : RenderTransform.GetTransform();

            RenderContext localRenderContext = parentRenderContext.Derive(bounds, layoutTransformMatrix, renderTransformMatrix, RenderTransformOrigin, Opacity);

            _inverseFinalTransform = Matrix.Invert(localRenderContext.Transform);

            DeviceEx           device      = SkinContext.Device;
            Surface            backBuffer  = device.GetRenderTarget(0);
            SurfaceDescription desc        = backBuffer.Description;
            SurfaceDescription?textureDesc = _texture == null ? new SurfaceDescription?() : _texture.GetLevelDescription(0);

            if (!textureDesc.HasValue || textureDesc.Value.Width != desc.Width || textureDesc.Value.Height != desc.Height)
            {
                TryDispose(ref _texture);
                _texture = new Texture(device, desc.Width, desc.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
            }
            using (Surface destSurfaceRef = _texture.GetSurfaceLevel(0))
                device.StretchRectangle(backBuffer, destSurfaceRef, TextureFilter.None);

            UpdateEffectMask(effect, localRenderContext.OccupiedTransformedBounds, desc.Width, desc.Height, localRenderContext.ZOrder);
            if (effect.BeginRender(_texture, new RenderContext(Matrix.Identity, 1.0d, bounds, localRenderContext.ZOrder)))
            {
                _effectContext.Render(0);
                effect.EndRender();
            }
            backBuffer.Dispose();
        }
Example #6
0
    /// <summary>
    /// Creates or re-creates the DirectX device and the backbuffer. This is necessary in the initialization phase
    /// of the SkinEngine and after a parameter was changed which affects the DX device creation.
    /// </summary>
    internal static void DoReCreateDevice_MainThread()
    {
      try
      {
        // Note that only the thread which handles window messages is allowed to call CreateDevice and Reset
        // (see http://msdn.microsoft.com/en-us/library/windows/desktop/bb147224%28v=vs.85%29.aspx )
        ServiceRegistration.Get<ILogger>().Debug("GraphicsDevice: Initializing DirectX");
        MPDirect3D.Load();

        // Cleanup-part: Only necessary during re-initialization
        UIResourcesHelper.ReleaseUIResources();
        if (_backBuffer != null)
          _backBuffer.Dispose();
        if (_device != null)
          _device.Dispose();
        _device = _setup.SetupDirectX();
        // End cleanup part

        SetupRenderStrategies();
        SetupRenderPipelines();

        Capabilities deviceCapabilities = _device.Capabilities;
        _backBuffer = _device.GetRenderTarget(0);
        _device.MaximumFrameLatency = _setup.PresentParameters.BackBufferCount; // Enables the device to queue as many frames as we have backbuffers defined
        int ordinal = deviceCapabilities.AdapterOrdinal;
        AdapterInformation adapterInfo = MPDirect3D.Direct3D.Adapters[ordinal];
        DisplayMode currentMode = adapterInfo.CurrentDisplayMode;
        AdaptTargetFrameRateToDisplayMode(currentMode);
        LogScreenMode(currentMode);
        bool firstTimeInitialization = _dxCapabilities == null;
        _dxCapabilities = DxCapabilities.RequestCapabilities(deviceCapabilities, currentMode);
        if (firstTimeInitialization)
        {
          if (!_dxCapabilities.SupportsShaders)
          {
            string text = String.Format("MediaPortal 2 needs a graphics card wich supports shader model 2.0\nYour card does NOT support this.\nMediaportal 2 will continue but migh run slow");
            MessageBox.Show(text, "GraphicAdapter", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          }
        }
        SetRenderState();
        UIResourcesHelper.ReallocUIResources();
      }
      catch (Exception ex)
      {
        ServiceRegistration.Get<ILogger>().Critical("GraphicsDevice: Failed to setup DirectX", ex);
        Environment.Exit(0);
      }
    }