示例#1
0
 /// <inheritdoc />
 public void Resize(int width, int height)
 {
     _resizeParameters = new ResizeParameters
     {
         BufferCount    = _swapChain4.Description1.BufferCount,
         Width          = width,
         Height         = height,
         SwapChainFlags = _swapChain4.Description1.Flags
     };
     _needResize = true;
 }
示例#2
0
 /// <inheritdoc />
 public void Resize(ref GameGraphicsParameters parameters)
 {
     _resizeParameters = new ResizeParameters
     {
         BufferCount    = parameters.BufferCount,
         Width          = parameters.Width,
         Height         = parameters.Height,
         SwapChainFlags = parameters.SwapChainFlags
     };
     _needResize = true;
 }
示例#3
0
        private void Resize(ResizeParameters args)
        {
            Utilities.Dispose(ref _renderView1);
            Utilities.Dispose(ref _depthStencilView);

            _d3DDeviceContext.ClearState();

            _swapChain4.ResizeBuffers(
                args.BufferCount, args.Width, args.Height, Format.Unknown, args.SwapChainFlags);

            using (Texture2D backBuffer = _swapChain4.GetBackBuffer <Texture2D>(0))
                using (RenderTargetView renderView = new RenderTargetView(_d3DDevice5, backBuffer))
                {
                    _renderView1 = renderView.QueryInterface <RenderTargetView1>();
                }

            using (Texture2D depthBuffer = new Texture2D(
                       _d3DDevice5,
                       new Texture2DDescription
            {
                Format = Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = args.Width,
                Height = args.Height,
                SampleDescription = _swapChain4.Description.SampleDescription,
                BindFlags = BindFlags.DepthStencil,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None,
                Usage = ResourceUsage.Default
            }))
            {
                _depthStencilView = new DepthStencilView(
                    _d3DDevice5, depthBuffer, new DepthStencilViewDescription
                {
                    Dimension = _swapChain4.Description.SampleDescription.Count > 1 ||
                                _swapChain4.Description.SampleDescription.Quality > 0
                            ? DepthStencilViewDimension.Texture2DMultisampled
                            : DepthStencilViewDimension.Texture2D
                });
            }

            _d3DDeviceContext.Rasterizer.SetViewport(Viewport = new Viewport(0, 0, args.Width, args.Height));

            SetRenderTarget(null);

            ResizeFinished?.Invoke(Viewport);
        }
示例#4
0
        /// <summary>
        ///     Initializes the <see cref="GraphicsDevice" />.
        /// </summary>
        /// <param name="parameters"> [in,out] Options for controlling the operation. </param>
        public void Initialize(ref GameGraphicsParameters parameters)
        {
            if (IsInitialized)
            {
                return;
            }

            _vSync = parameters.UseVSync ? 1 : 0;

            using (Factory4 factory4 = new Factory4())
            {
                _dxgiFactory = factory4.QueryInterface <Factory5>();
            }

            ModeDescription modeDescription =
                new ModeDescription(
                    parameters.Width,
                    parameters.Height,
                    parameters.Rational,
                    parameters.Format);

            if (parameters.AdapterLuid != -1 && parameters.OutputIndex != -1)
            {
                using (Adapter adapter = _dxgiFactory.GetAdapterByLuid(parameters.AdapterLuid))
                    using (Output output = adapter.GetOutput(parameters.OutputIndex))
                    {
                        _adapter4 = adapter.QueryInterface <Adapter4>();
                        _output6  = output.QueryInterface <Output6>();
                        _output6.GetClosestMatchingMode(
                            null,
                            modeDescription,
                            out modeDescription);
                    }
            }
            else
            {
                IEnumerable <Adapter> adapters =
                    _dxgiFactory.Adapters1
                    .Where(
                        a => (a.Description1.Flags & AdapterFlags.Software) !=
                        AdapterFlags.Software && a.GetOutputCount() > 0)
                    .Select(
                        a => (adapter: a,
                              featureLevel: SharpDX.Direct3D11.Device.GetSupportedFeatureLevel(a)))
                    .GroupBy(t => t.featureLevel)
                    .OrderByDescending(t => t.Key)
                    .First()
                    .Select(k => k.adapter);

                _adapter4 = null;
                foreach (Adapter adapter in adapters)
                {
                    using (adapter)
                    {
                        for (int o = 0; o < adapter.GetOutputCount(); o++)
                        {
                            using (Output output = adapter.GetOutput(o))
                            {
                                output.GetClosestMatchingMode(
                                    null,
                                    modeDescription,
                                    out ModeDescription desc);
                                if (_adapter4 == null ||
                                    desc.RefreshRate.Numerator / desc.RefreshRate.Denominator >
                                    modeDescription.RefreshRate.Numerator / modeDescription.RefreshRate.Denominator)
                                {
                                    modeDescription = desc;
                                    Interlocked.Exchange(ref _output6, output.QueryInterface <Output6>())?.Dispose();
                                    Interlocked.Exchange(ref _adapter4, adapter.QueryInterface <Adapter4>())?.Dispose();
                                }
                            }
                        }
                    }
                }
            }

            // ReSharper disable once VariableHidesOuterVariable
            Device CreateDevice(in GameGraphicsParameters parameters)
            {
                if (_adapter4 != null)
                {
                    Console.WriteLine("------- GRAPHIC CARD INFORMATION -------");
                    Console.WriteLine($"Luid:\t\t\t{_adapter4.Description2.Luid}");
                    Console.WriteLine(
                        $"Description:\t\t{_adapter4.Description2.Description.TrimEnd('\t', ' ', '\r', '\n', (char)0)}");
                    Console.WriteLine($"DeviceId:\t\t{_adapter4.Description2.DeviceId}");
                    Console.WriteLine($"VendorId:\t\t{_adapter4.Description2.VendorId}");
                    Console.WriteLine($"Revision:\t\t{_adapter4.Description2.Revision}");
                    Console.WriteLine($"SubsystemId:\t\t{_adapter4.Description2.SubsystemId}");
#if x64
                    Console.WriteLine();
                    Console.WriteLine($"SystemMemory:\t\t{_adapter4.Description2.DedicatedSystemMemory}");
                    Console.WriteLine($"VideoMemory:\t\t{_adapter4.Description2.DedicatedVideoMemory}");
                    Console.WriteLine($"SharedSystemMemory:\t{_adapter4.Description2.SharedSystemMemory}");
#endif
                    Console.WriteLine();
                    Console.WriteLine($"Format:\t\t\t{modeDescription.Format}");
                    Console.WriteLine($"Width x Height:\t\t{modeDescription.Width} x {modeDescription.Height}");
                    Console.WriteLine(
                        $"RefreshRate:\t\t{modeDescription.RefreshRate} ({modeDescription.RefreshRate.Numerator / modeDescription.RefreshRate.Denominator}HZ)");
                    Console.WriteLine($"Scaling:\t\t{modeDescription.Scaling}");
                    Console.WriteLine($"ScanlineOrdering:\t{modeDescription.ScanlineOrdering}");
                    Console.WriteLine();
                    Console.WriteLine($"DeviceName:\t\t{_output6.Description.DeviceName}");
                    Console.WriteLine(
                        $"DesktopBounds:\t\t{_output6.Description.DesktopBounds.Left};{_output6.Description.DesktopBounds.Top};{_output6.Description.DesktopBounds.Right};{_output6.Description.DesktopBounds.Bottom}");
                    Console.WriteLine($"MonitorHandle:\t\t{_output6.Description.MonitorHandle}");
                    Console.WriteLine($"IsAttachedToDesktop:\t{_output6.Description.IsAttachedToDesktop}");
                    Console.WriteLine($"Rotation:\t\t{_output6.Description.Rotation}");
                    Console.WriteLine("----------------------------------------\n");

                    return(new Device(_adapter4, parameters.DeviceCreationFlags, s_featureLevels));
                }
                return(new Device(parameters.DriverType, parameters.DeviceCreationFlags, s_featureLevels));
            }

            using (Device defaultDevice = CreateDevice(in parameters))
            {
                _d3DDevice5 = defaultDevice.QueryInterface <Device5>();
            }

            _d3DDeviceContext = _d3DDevice5.ImmediateContext3.QueryInterface <DeviceContext4>();
            _dxgiDevice4      = _d3DDevice5.QueryInterface <Device4>();

            SampleDescription sampleDescription = new SampleDescription(1, 0);
            if (parameters.EnableMultiSampling && parameters.MultiSampleCount != MultiSampleCount.None)
            {
                sampleDescription.Count   = (int)parameters.MultiSampleCount;
                sampleDescription.Quality =
                    Math.Max(
                        _d3DDevice5.CheckMultisampleQualityLevels(
                            modeDescription.Format,
                            sampleDescription.Count) - 1,
                        0);
            }

            SwapChainDescription swapChainDescription = new SwapChainDescription
            {
                BufferCount       = parameters.BufferCount,
                ModeDescription   = modeDescription,
                IsWindowed        = true,
                OutputHandle      = parameters.Handle,
                SampleDescription = sampleDescription,
                SwapEffect        = parameters.SwapEffect,
                Usage             = parameters.Usage,
                Flags             = parameters.SwapChainFlags
            };

            using (SwapChain swapChain = new SwapChain(_dxgiFactory, _d3DDevice5, swapChainDescription))
            {
                _swapChain4 = swapChain.QueryInterface <SwapChain4>();
            }

            _dxgiFactory.MakeWindowAssociation(parameters.Handle, parameters.WindowAssociationFlags);

            _swapChain4.ResizeTarget(ref modeDescription);

            SetFullscreenState(parameters.DisplayType == DisplayType.Fullscreen);

            _resizeParameters = new ResizeParameters
            {
                BufferCount    = parameters.BufferCount,
                Width          = parameters.Width,
                Height         = parameters.Height,
                SwapChainFlags = parameters.SwapChainFlags
            };

            Resize(_resizeParameters);

            _blendStates        = new BlendStates(this);
            _depthStencilStates = new DepthStencilStates(this);
            _rasterizerStates   = new RasterizerStates(this);
            _samplerStates      = new SamplerStates(this);
            _textures           = new Textures(this);

            IsInitialized = true;
        }