Exemplo n.º 1
0
        static public SharpDX.DXGI.SwapEffect ToSwapEffect(PresentInterval presentInterval)
        {
            SharpDX.DXGI.SwapEffect effect;

            switch (presentInterval)
            {
                case PresentInterval.One:
                case PresentInterval.Two:
                default:
#if WINRT
                    effect = SharpDX.DXGI.SwapEffect.FlipSequential;
#else
                    effect = SharpDX.DXGI.SwapEffect.Discard;
#endif
                    break;

                case PresentInterval.Immediate:
                    effect = SharpDX.DXGI.SwapEffect.Sequential;
                    break;
            }

            //if (present.RenderTargetUsage != RenderTargetUsage.PreserveContents && present.MultiSampleCount == 0)
            //effect = SharpDX.DXGI.SwapEffect.Discard;

            return effect;
        }
 public void InitDefaults()
 {
     AutoDepthStencilFormat = Format.D24X8;
     BackBufferWidth = 800;
     BackBufferHeight = 600;
     BackBufferFormat = Format.X8R8G8B8;
     BackBufferCount = 1;
     DeviceWindowHandle = IntPtr.Zero;
     EnableAutoDepthStencil = true;
     PresentFlags = PresentFlags.None;
     PresentationInterval = PresentInterval.Immediate;
     SwapEffect = SwapEffect.Discard;
     Windowed = true;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PresentParameters"/> struct.
 /// </summary>
 /// <param name="backBufferWidth">Width of the back buffer.</param>
 /// <param name="backBufferHeight">Height of the back buffer.</param>
 /// <param name="backBufferFormat">The back buffer format.</param>
 /// <param name="backBufferCount">The back buffer count.</param>
 /// <param name="multiSampleType">Type of the multi sample.</param>
 /// <param name="multiSampleQuality">The multi sample quality.</param>
 /// <param name="swapEffect">The swap effect.</param>
 /// <param name="deviceWindowHandle">The device window handle.</param>
 /// <param name="windowed">if set to <c>true</c> [windowed].</param>
 /// <param name="enableAutoDepthStencil">if set to <c>true</c> [enable auto depth stencil].</param>
 /// <param name="autoDepthStencilFormat">The auto depth stencil format.</param>
 /// <param name="presentFlags">The present flags.</param>
 /// <param name="fullScreenRefreshRateInHz">The full screen refresh rate in Hz.</param>
 /// <param name="presentationInterval">The presentation interval.</param>
 public PresentParameters(int backBufferWidth, int backBufferHeight, Format backBufferFormat, int backBufferCount, MultisampleType multiSampleType, int multiSampleQuality, SwapEffect swapEffect, IntPtr deviceWindowHandle, bool windowed, bool enableAutoDepthStencil, Format autoDepthStencilFormat, PresentFlags presentFlags, int fullScreenRefreshRateInHz, PresentInterval presentationInterval)
 {
     BackBufferWidth = backBufferWidth;
     BackBufferHeight = backBufferHeight;
     BackBufferFormat = backBufferFormat;
     BackBufferCount = backBufferCount;
     MultiSampleType = multiSampleType;
     MultiSampleQuality = multiSampleQuality;
     SwapEffect = swapEffect;
     DeviceWindowHandle = deviceWindowHandle;
     Windowed = windowed;
     EnableAutoDepthStencil = enableAutoDepthStencil;
     AutoDepthStencilFormat = autoDepthStencilFormat;
     PresentFlags = presentFlags;
     FullScreenRefreshRateInHz = fullScreenRefreshRateInHz;
     PresentationInterval = presentationInterval;
 }
Exemplo n.º 4
0
 internal override void SetPresentationInterval(PresentInterval interval)
 {
     if (interval == PresentInterval.Default || interval == PresentInterval.One)
     {
         if (OSVersion.Equals("Mac OS X"))
         {
             // Apple is a big fat liar about swap_control_tear. Use stock VSync.
             SDL.SDL_GL_SetSwapInterval(1);
         }
         else
         {
             if (SDL.SDL_GL_SetSwapInterval(-1) != -1)
             {
                 System.Console.WriteLine("Using EXT_swap_control_tear VSync!");
             }
             else
             {
                 System.Console.WriteLine("EXT_swap_control_tear unsupported. Fall back to standard VSync.");
                 SDL.SDL_ClearError();
                 SDL.SDL_GL_SetSwapInterval(1);
             }
         }
     }
     else if (interval == PresentInterval.Immediate)
     {
         SDL.SDL_GL_SetSwapInterval(0);
     }
     else if (interval == PresentInterval.Two)
     {
         SDL.SDL_GL_SetSwapInterval(2);
     }
     else
     {
         throw new Exception("Unrecognized PresentInterval!");
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Converts <see cref="PresentInterval"/> to OpenGL swap interval.
        /// </summary>
        /// <returns>A value according to EXT_swap_control</returns>
        /// <param name="interval">The <see cref="PresentInterval"/> to convert.</param>
        internal static int GetSwapInterval(this PresentInterval interval)
        {
            // See http://www.opengl.org/registry/specs/EXT/swap_control.txt
            // and https://www.opengl.org/registry/specs/EXT/glx_swap_control_tear.txt
            // OpenTK checks for EXT_swap_control_tear:
            // if supported, a swap interval of -1 enables adaptive vsync;
            // otherwise -1 is converted to 1 (vsync enabled.)

            switch (interval)
            {
            case PresentInterval.Immediate:
                return(0);

            case PresentInterval.One:
                return(1);

            case PresentInterval.Two:
                return(2);

            case PresentInterval.Default:
            default:
                return(-1);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Enables the profiling system drawing.
        /// </summary>
        /// <param name="excludeKeys">If true the keys specified after are excluded from rendering, if false they will be exclusively included.</param>
        /// <param name="keys">The keys to exclude or include.</param>
        public void EnableProfiling(bool excludeKeys = false, params ProfilingKey[] keys)
        {
            Enabled = true;
            Visible = true;

            // Backup current PresentInterval state
            userPresentInterval = GraphicsDevice.Presenter.PresentInterval;

            // Disable VSync (otherwise GPU results might be incorrect)
            GraphicsDevice.Presenter.PresentInterval = PresentInterval.Immediate;

            if (keys.Length == 0)
            {
                Profiler.EnableAll();
            }
            else
            {
                if (excludeKeys)
                {
                    Profiler.EnableAll();
                    foreach (var profilingKey in keys)
                    {
                        Profiler.Disable(profilingKey);
                    }
                }
                else
                {
                    foreach (var profilingKey in keys)
                    {
                        Profiler.Enable(profilingKey);
                    }
                }
            }

            gcProfiler.Enable();
        }
Exemplo n.º 7
0
 public void Clear()
 {
     autoDepthStencilFormat = DepthFormat.Unknown;
     backBufferCount = 0;
     backBufferFormat = SurfaceFormat.Unknown;
     backBufferHeight = 0;
     backBufferWidth = 0;
     deviceWindowHandle = IntPtr.Zero;
     enableAutoDepthStencil = false;
     fullScreenRefreshRateInHz = 0;
     isFullScreen = false;
     multiSampleQuality = 0;
     multiSampleType = MultiSampleType.None;
     presentationInterval = PresentInterval.Default;
     presentOptions = PresentOptions.None;
     swapEffect = SwapEffect.Default;
 }
        public SwapChainRenderTarget(GraphicsDevice graphicsDevice,
                                     IntPtr windowHandle,
                                     int width,
                                     int height,
                                     bool mipMap,
                                     SurfaceFormat surfaceFormat,
                                     DepthFormat depthFormat,
                                     int preferredMultiSampleCount,
                                     RenderTargetUsage usage,
                                     PresentInterval presentInterval)
            : base(
                graphicsDevice,
                width,
                height,
                mipMap,
                surfaceFormat,
                depthFormat,
                preferredMultiSampleCount,
                usage,
                SurfaceType.SwapChainRenderTarget)
        {
            var dxgiFormat = surfaceFormat == SurfaceFormat.Color
                             ? SharpDX.DXGI.Format.B8G8R8A8_UNorm
                             : SharpDXHelper.ToFormat(surfaceFormat);

            var multisampleDesc = new SampleDescription(1, 0);

            if (preferredMultiSampleCount > 1)
            {
                multisampleDesc.Count   = preferredMultiSampleCount;
                multisampleDesc.Quality = (int)StandardMultisampleQualityLevels.StandardMultisamplePattern;
            }

            var desc = new SwapChainDescription()
            {
                ModeDescription =
                {
                    Format  = dxgiFormat,
                    Scaling = DisplayModeScaling.Stretched,
                    Width   = width,
                    Height  = height,
                },

                OutputHandle      = windowHandle,
                SampleDescription = multisampleDesc,
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 2,
                SwapEffect        = SharpDXHelper.ToSwapEffect(presentInterval),
                IsWindowed        = true,
            };

            PresentInterval = presentInterval;

            // Once the desired swap chain description is configured, it must
            // be created on the same adapter as our D3D Device
            var d3dDevice = graphicsDevice._d3dDevice;

            // First, retrieve the underlying DXGI Device from the D3D Device.
            // Creates the swap chain
            using (var dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device1>())
                using (var dxgiAdapter = dxgiDevice.Adapter)
                    using (var dxgiFactory = dxgiAdapter.GetParent <Factory1>())
                    {
                        _swapChain = new SwapChain(dxgiFactory, dxgiDevice, desc);
                    }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            var backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain <SharpDX.Direct3D11.Texture2D>(_swapChain, 0);

            // Create a view interface on the rendertarget to use on bind.
            _renderTargetViews = new[] { new RenderTargetView(d3dDevice, backBuffer) };

            // Get the rendertarget dimensions for later.
            var backBufferDesc = backBuffer.Description;
            var targetSize     = new Point(backBufferDesc.Width, backBufferDesc.Height);

            _texture = backBuffer;

            // Create the depth buffer if we need it.
            if (depthFormat != DepthFormat.None)
            {
                dxgiFormat = SharpDXHelper.ToFormat(depthFormat);

                // Allocate a 2-D surface as the depth/stencil buffer.
                using (
                    var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice,
                                                                       new Texture2DDescription()
                {
                    Format = dxgiFormat,
                    ArraySize = 1,
                    MipLevels = 1,
                    Width = targetSize.X,
                    Height = targetSize.Y,
                    SampleDescription = multisampleDesc,
                    Usage = ResourceUsage.Default,
                    BindFlags = BindFlags.DepthStencil,
                }))

                    // Create a DepthStencil view on this surface to use on bind.
                    _depthStencilView = new DepthStencilView(d3dDevice, depthBuffer);
            }
        }
Exemplo n.º 9
0
        public SwapChainRenderTarget(   GraphicsDevice graphicsDevice,
                                        IntPtr windowHandle,                                     
                                        int width,
                                        int height,
                                        bool mipMap,
                                        SurfaceFormat surfaceFormat,
                                        DepthFormat depthFormat,                                        
                                        int preferredMultiSampleCount,
                                        RenderTargetUsage usage,
                                        PresentInterval presentInterval)
            : base(
                graphicsDevice,
                width,
                height,
                mipMap,
                surfaceFormat,
                depthFormat,
                preferredMultiSampleCount,
                usage,
                SurfaceType.SwapChainRenderTarget)
        {
            var dxgiFormat = surfaceFormat == SurfaceFormat.Color
                             ? SharpDX.DXGI.Format.B8G8R8A8_UNorm
                             : SharpDXHelper.ToFormat(surfaceFormat);

            var multisampleDesc = new SampleDescription(1, 0);
            if (preferredMultiSampleCount > 1)
            {
                multisampleDesc.Count = preferredMultiSampleCount;
                multisampleDesc.Quality = (int)StandardMultisampleQualityLevels.StandardMultisamplePattern;
            }

            var desc = new SwapChainDescription()
            {
                ModeDescription =
                {
                    Format = dxgiFormat,
                    Scaling = DisplayModeScaling.Stretched,
                    Width = width,
                    Height = height,
                },

                OutputHandle = windowHandle,
                SampleDescription = multisampleDesc,
                Usage = Usage.RenderTargetOutput,
                BufferCount = 2,
                SwapEffect = SharpDXHelper.ToSwapEffect(presentInterval),
                IsWindowed = true,
            };

            PresentInterval = presentInterval;

            // Once the desired swap chain description is configured, it must 
            // be created on the same adapter as our D3D Device
            var d3dDevice = graphicsDevice._d3dDevice;

            // First, retrieve the underlying DXGI Device from the D3D Device.
            // Creates the swap chain 
            using (var dxgiDevice = d3dDevice.QueryInterface<Device1>())
            using (var dxgiAdapter = dxgiDevice.Adapter)
            using (var dxgiFactory = dxgiAdapter.GetParent<Factory1>())
            {
                _swapChain = new SwapChain(dxgiFactory, dxgiDevice, desc);
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            var backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain<SharpDX.Direct3D11.Texture2D>(_swapChain, 0);

            // Create a view interface on the rendertarget to use on bind.
            _renderTargetViews = new[] { new RenderTargetView(d3dDevice, backBuffer) };

            // Get the rendertarget dimensions for later.
            var backBufferDesc = backBuffer.Description;
            var targetSize = new Point(backBufferDesc.Width, backBufferDesc.Height);

            _texture = backBuffer;

            // Create the depth buffer if we need it.
            if (depthFormat != DepthFormat.None)
            {
                dxgiFormat = SharpDXHelper.ToFormat(depthFormat);

                // Allocate a 2-D surface as the depth/stencil buffer.
                using (
                    var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice,
                                                                       new Texture2DDescription()
                                                                           {
                                                                               Format = dxgiFormat,
                                                                               ArraySize = 1,
                                                                               MipLevels = 1,
                                                                               Width = targetSize.X,
                                                                               Height = targetSize.Y,
                                                                               SampleDescription = multisampleDesc,
                                                                               Usage = ResourceUsage.Default,
                                                                               BindFlags = BindFlags.DepthStencil,
                                                                           }))

                    // Create a DepthStencil view on this surface to use on bind.
                    _depthStencilView = new DepthStencilView(d3dDevice, depthBuffer);
            }
        }
Exemplo n.º 10
0
		internal override void SetPresentationInterval(PresentInterval interval)
		{
			if (interval == PresentInterval.Default || interval == PresentInterval.One)
			{
				if (OSVersion.Equals("Mac OS X"))
				{
					// Apple is a big fat liar about swap_control_tear. Use stock VSync.
					SDL.SDL_GL_SetSwapInterval(1);
				}
				else
				{
					if (SDL.SDL_GL_SetSwapInterval(-1) != -1)
					{
						System.Console.WriteLine("Using EXT_swap_control_tear VSync!");
					}
					else
					{
						System.Console.WriteLine("EXT_swap_control_tear unsupported. Fall back to standard VSync.");
						SDL.SDL_ClearError();
						SDL.SDL_GL_SetSwapInterval(1);
					}
				}
			}
			else if (interval == PresentInterval.Immediate)
			{
				SDL.SDL_GL_SetSwapInterval(0);
			}
			else if (interval == PresentInterval.Two)
			{
				SDL.SDL_GL_SetSwapInterval(2);
			}
			else
			{
				throw new Exception("Unrecognized PresentInterval!");
			}
		}
Exemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PresentationParameters" /> class with default values.
 /// </summary>
 public PresentationParameters()
 {
     BackBufferWidth = 800;
     BackBufferHeight = 480;
     BackBufferFormat = PixelFormat.R8G8B8A8.UNorm;
     PresentationInterval = PresentInterval.Immediate;
     DepthStencilFormat = DepthFormat.Depth24Stencil8;
     MultiSampleCount = MSAALevel.None;
     IsFullScreen = false;
     RefreshRate = new Rational(60, 1); // by default
     RenderTargetUsage = Usage.BackBuffer | Usage.RenderTargetOutput;
     Flags = SwapChainFlags.None;
 }
Exemplo n.º 12
0
 internal abstract void SetPresentationInterval(PresentInterval interval);
Exemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PresentParameters"/> struct.
 /// </summary>
 /// <param name="backBufferWidth">Width of the back buffer.</param>
 /// <param name="backBufferHeight">Height of the back buffer.</param>
 /// <param name="backBufferFormat">The back buffer format.</param>
 /// <param name="backBufferCount">The back buffer count.</param>
 /// <param name="multiSampleType">Type of the multi sample.</param>
 /// <param name="multiSampleQuality">The multi sample quality.</param>
 /// <param name="swapEffect">The swap effect.</param>
 /// <param name="deviceWindowHandle">The device window handle.</param>
 /// <param name="windowed">if set to <c>true</c> [windowed].</param>
 /// <param name="enableAutoDepthStencil">if set to <c>true</c> [enable auto depth stencil].</param>
 /// <param name="autoDepthStencilFormat">The auto depth stencil format.</param>
 /// <param name="presentFlags">The present flags.</param>
 /// <param name="fullScreenRefreshRateInHz">The full screen refresh rate in hz.</param>
 /// <param name="presentationInterval">The presentation interval.</param>
 public PresentParameters(int backBufferWidth, int backBufferHeight, Format backBufferFormat, int backBufferCount, MultisampleType multiSampleType, int multiSampleQuality, SwapEffect swapEffect, IntPtr deviceWindowHandle, bool windowed, bool enableAutoDepthStencil, Format autoDepthStencilFormat, PresentFlags presentFlags, int fullScreenRefreshRateInHz, PresentInterval presentationInterval)
 {
     BackBufferWidth           = backBufferWidth;
     BackBufferHeight          = backBufferHeight;
     BackBufferFormat          = backBufferFormat;
     BackBufferCount           = backBufferCount;
     MultiSampleType           = multiSampleType;
     MultiSampleQuality        = multiSampleQuality;
     SwapEffect                = swapEffect;
     DeviceWindowHandle        = deviceWindowHandle;
     Windowed                  = windowed;
     EnableAutoDepthStencil    = enableAutoDepthStencil;
     AutoDepthStencilFormat    = autoDepthStencilFormat;
     PresentFlags              = presentFlags;
     FullScreenRefreshRateInHz = fullScreenRefreshRateInHz;
     PresentationInterval      = presentationInterval;
 }