Esempio n. 1
0
        /// <summary>
        /// Function to return the maximum number of quality levels supported by the device for multisampling.
        /// </summary>
        /// <param name="format">Format to test.</param>
        /// <param name="count">Number of multisamples.</param>
        /// <returns>The maximum quality level for the format, or 0 if not supported.</returns>
        public int GetMultiSampleQuality(BufferFormat format, int count)
        {
            if (format == BufferFormat.Unknown)
            {
                return(0);
            }

            if (count < 1)
            {
                count = 1;
            }

            D3D.Device device = (Graphics != null) ? Graphics.D3DDevice : null;
            Tuple <GI.Factory1, GI.Adapter1, D3D.Device> tempInterfaces = null;

            try
            {
                if (device != null)
                {
                    return(device.CheckMultisampleQualityLevels((GI.Format)format, count));
                }

                tempInterfaces = GetDevice(VideoDeviceType, HardwareFeatureLevel);
                device         = tempInterfaces.Item3;

                return(device.CheckMultisampleQualityLevels((GI.Format)format, count));
            }
            finally
            {
                ReleaseInterfaces(tempInterfaces);
            }
        }
Esempio n. 2
0
        public void CreateDeviceAndSwapChain(RenderForm form)
        {
            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0, FeatureLevel.Level_10_0 };
            Device = new Device(DriverType.Hardware, DeviceCreationFlags.None, featureLevels);

            if (ConfigurationManager.Config.AntiAliasing)
            {
                _maxQualityLevel = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, 4);
            }

            var swapChainDescription = new SwapChainDescription
            {
                BufferCount       = 1, // Set to a single back buffer (double buffering)
                ModeDescription   = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, _refreshRate, Format.R8G8B8A8_UNorm),
                IsWindowed        = ConfigurationManager.Config.WindowedMode,
                OutputHandle      = form.Handle,
                SampleDescription = ConfigurationManager.Config.AntiAliasing ? new SampleDescription(4, _maxQualityLevel - 1) : new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            SwapChain = new SwapChain(new Factory(), Device, swapChainDescription);

            DeviceContext = Device.ImmediateContext;

            SwapChain.GetParent <Factory>().MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
        }
Esempio n. 3
0
File: DX11.cs Progetto: ndech/Alpha
        public void CreateDeviceAndSwapChain(RenderForm form)
        {
            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 };
#if GPU_DEBUG
            //Use https://dev.windows.com/en-us/develop/graphics-debugging-and-frame-analysis-update on Windows 10 to install debug layer.
            Device = new Device(DriverType.Hardware, DeviceCreationFlags.Debug, featureLevels);
#else
            Device = new Device(DriverType.Hardware, DeviceCreationFlags.None, featureLevels);
#endif
            if (ConfigurationManager.Config.AntiAliasing)
            {
                _maxQualityLevel = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, 4);
            }

            var swapChainDescription = new SwapChainDescription
            {
                BufferCount       = 1, // Set to a single back buffer (double buffering)
                ModeDescription   = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, _refreshRate, Format.R8G8B8A8_UNorm),
                IsWindowed        = ConfigurationManager.Config.WindowedMode,
                OutputHandle      = form.Handle,
                SampleDescription = ConfigurationManager.Config.AntiAliasing ? new SampleDescription(4, _maxQualityLevel - 1) : new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            SwapChain = new SwapChain(new Factory(), Device, swapChainDescription);

            DeviceContext = Device.ImmediateContext;

            SwapChain.GetParent <Factory>().MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
        }
Esempio n. 4
0
        public static DXGI.SampleDescription GetMultisamplingLevel(this D3D.Device device, AntiAliasingMode antiAliasingMode)
        {
            int desiredSampleCount = (int)((uint)antiAliasingMode & 0x0000FFFFu);
            int desiredQuality     = (int)((uint)antiAliasingMode >> 16);

            int  sampleCount   = desiredSampleCount;
            int  quality       = desiredQuality;
            bool csaaModeFound = false;

            if (desiredQuality > 0) //CSAA is queried
            {
                for (; sampleCount > 0x4; sampleCount >>= 1)
                {
                    quality = device.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, sampleCount) - 1;
                    if (quality >= desiredQuality)
                    {
                        csaaModeFound = true;
                        break;
                    }
                    if ((quality >>= 1) >= desiredQuality)
                    {
                        csaaModeFound = true;
                        break;
                    }
                    quality = desiredQuality;
                }
            }

            if (!csaaModeFound) //no CSAA available or not queried
            {
                sampleCount = desiredSampleCount;
                if (desiredQuality > 0)
                {
                    sampleCount <<= 1;                     //double queried sample count for equal quality
                }
                for (; sampleCount > 0x1; sampleCount >>= 1)
                {
                    quality = device.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, sampleCount) - 1;
                    if (quality >= 0)
                    {
                        break;
                    }
                }
            }

            return(new DXGI.SampleDescription(desiredSampleCount, quality));
        }
Esempio n. 5
0
        public Form1()
        {
            InitializeComponent();

            VerticalSyncEnabled = false;

            using (var device11 = new SharpDX.Direct3D11.Device(
                       SharpDX.Direct3D.DriverType.Hardware,
                       DeviceCreationFlags.None,
                       new[] {
                SharpDX.Direct3D.FeatureLevel.Level_11_1,
                SharpDX.Direct3D.FeatureLevel.Level_11_0,
            }))
            {
                // Query device for the Device1 interface (ID3D11Device1)
                device = device11.QueryInterfaceOrNull <SharpDX.Direct3D11.Device1>();
                if (device == null)
                {
                    throw new NotSupportedException(
                              "SharpDX.Direct3D11.Device1 is not supported");
                }
            }// Rather than create a new DXGI Factory we reuse the
             // one that has been used internally to create the device

            int sameple = device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, 100);

            using (var dxgi = device.QueryInterface <SharpDX.DXGI.Device2>())
                using (var adapter = dxgi.Adapter)
                    using (var factory = adapter.GetParent <Factory2>())
                    {
                        var desc1 = new SwapChainDescription1()
                        {
                            Width             = this.ClientSize.Width,
                            Height            = this.ClientSize.Height,
                            Format            = Format.R8G8B8A8_UNorm,
                            Stereo            = false,
                            SampleDescription = new SampleDescription(1, 0),
                            Usage             = Usage.BackBuffer | Usage.RenderTargetOutput,
                            BufferCount       = 1,
                            Scaling           = Scaling.Stretch,
                            SwapEffect        = SwapEffect.Discard,
                        };

                        SwapChain = new SwapChain1(factory,
                                                   device,
                                                   this.Handle,
                                                   ref desc1,
                                                   new SwapChainFullScreenDescription()
                        {
                            RefreshRate = new Rational(60, 1),
                            Scaling     = DisplayModeScaling.Centered,
                            Windowed    = true
                        },
                                                   // Restrict output to specific Output (monitor)
                                                   adapter.Outputs[0]);
                    }
        }
Esempio n. 6
0
        private void GetMSAAQualities(Format format)
        {
            for (int SamplingCount = 0; SamplingCount <= SharpDX.Direct3D11.Device.MultisampleCountMaximum; SamplingCount++)
            {
                int Quality = Device.CheckMultisampleQualityLevels(format, SamplingCount);
                if (Quality > 0)
                {
                    //Add Base Quality
                    MSAAList.Add(new SampleDescription()
                    {
                        Count = SamplingCount, Quality = 0
                    });

                    if (MainAdapter.ToUpper().Contains("NVIDIA"))
                    {
                        //Add CSAA 8x
                        if (Quality >= 9)
                        {
                            MSAAList.Add(new SampleDescription()
                            {
                                Count = SamplingCount, Quality = 8
                            });
                        }

                        //Add CSAA 16x
                        if (Quality >= 17)
                        {
                            MSAAList.Add(new SampleDescription()
                            {
                                Count = SamplingCount, Quality = 16
                            });
                        }

                        //Add CSAA 32x
                        if (Quality >= 33)
                        {
                            MSAAList.Add(new SampleDescription()
                            {
                                Count = SamplingCount, Quality = 32
                            });
                        }
                    }
                }
            }

#if DEBUG
            foreach (var mode in MSAAList)
            {
                logger.Trace("Compatible Device MSAA : {0}x", mode.Count);
            }
#endif
        }
        /// <summary>
        /// Gets the maximum MSAA sample count for a particular <see cref="PixelFormat" />.
        /// </summary>
        /// <param name="device">The device.</param>
        /// <param name="pixelFormat">The pixelFormat.</param>
        /// <returns>The maximum multisample count for this pixel pixelFormat</returns>
        private static MSAALevel GetMaximumMSAASampleCount(SharpDX.Direct3D11.Device device, SharpDX.DXGI.Format pixelFormat)
        {
            int maxCount = 1;

            for (int i = 1; i <= 8; i *= 2)
            {
                if (device.CheckMultisampleQualityLevels(pixelFormat, i) != 0)
                {
                    maxCount = i;
                }
            }
            return((MSAALevel)maxCount);
        }
Esempio n. 8
0
        static SampleDescription DetectSampleDescription(SharpDX.Direct3D11.Device device, Format format)
        {
            var desc = new SampleDescription();

            for (int sampleCount = SharpDX.Direct3D11.Device.MultisampleCountMaximum; sampleCount > 0; --sampleCount)
            {
                int qualiryLevels = device.CheckMultisampleQualityLevels(format, sampleCount);
                if (qualiryLevels > 0)
                {
                    desc.Count   = sampleCount;
                    desc.Quality = qualiryLevels - 1;
                    break;
                }
            }
            Console.WriteLine("sample count {0} quality {1}", desc.Count, desc.Quality);
            return(desc);
        }
Esempio n. 9
0
        static SharpDX.DXGI.SampleDescription DetectSampleDescription(Device device, SharpDX.DXGI.Format format)
        {
            var desc = new SharpDX.DXGI.SampleDescription();

            for (int multisample_count = Device.MultisampleCountMaximum; multisample_count > 0; --multisample_count)
            {
                int quality_levels = device.CheckMultisampleQualityLevels(format, multisample_count);
                if (quality_levels > 0)
                {
                    desc.Count   = multisample_count;
                    desc.Quality = quality_levels - 1;
                    break;
                }
            }
            Console.WriteLine("sample count {0} quality {1}", desc.Count, desc.Quality);
            return(desc);
        }
Esempio n. 10
0
        private SampleDescription CreateSampleDescription()
        {
            SampleDescription sampleDesc;

            if (Options.UseMultisampling && Options.Multisamples > 1)
            {
                int sampleCount        = Math.Min(Options.Multisamples, Device.MultisampleCountMaximum);
                int multisampleQuality = _device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, sampleCount);
                Check.Ensures(multisampleQuality > 0);
                sampleDesc = new SampleDescription(sampleCount, multisampleQuality - 1);
            }
            else
            {
                sampleDesc = new SampleDescription(1, 0);
            }
            return(sampleDesc);
        }
        /// <summary>
        /// Creates the swap chain description.
        /// </summary>
        /// <returns>A swap chain description</returns>
        /// <remarks>
        /// This method can be overloaded in order to modify default parameters.
        /// </remarks>
        protected virtual SwapChainDescription1 CreateSwapChainDescription()
        {
            int sampleCount   = 1;
            int sampleQuality = 0;

            // SwapChain description
#if MSAA
            if (MSAA != MSAALevel.Disable)
            {
                do
                {
                    var newSampleCount   = sampleCount * 2;
                    var newSampleQuality = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, newSampleCount) - 1;

                    if (newSampleQuality < 0)
                    {
                        break;
                    }

                    sampleCount   = newSampleCount;
                    sampleQuality = newSampleQuality;
                    if (sampleCount == (int)MSAA)
                    {
                        break;
                    }
                } while (sampleCount < 32);
            }
#endif
            var desc = new SwapChainDescription1()
            {
                Width  = Math.Max(1, TargetWidth),
                Height = Math.Max(1, TargetHeight),
                // B8G8R8A8_UNorm gives us better performance
                Format            = Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SampleDescription(sampleCount, sampleQuality),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 1,
                SwapEffect        = SwapEffect.Discard,
                Scaling           = Scaling.Stretch,
                Flags             = SwapChainFlags.AllowModeSwitch
            };
            return(desc);
        }
Esempio n. 12
0
        /// <summary>
        /// Check the multisample levels that are supported by the device.
        /// </summary>
        /// <returns>DXGI.SampleDescription[].</returns>
        public static DXGI.SampleDescription[] CheckMultiSample()
        {
            using (D3D11.Device Device = new D3D11.Device(DriverType.Hardware, D3D11.DeviceCreationFlags.None))
            {
                System.Collections.Generic.List <DXGI.SampleDescription> samples =
                    new System.Collections.Generic.List <DXGI.SampleDescription>();

                for (int samplerCount = 0; samplerCount < D3D11.Device.MultisampleCountMaximum; samplerCount++)
                {
                    int quality = Device.CheckMultisampleQualityLevels(BackBufferFormat, samplerCount);
                    if (quality > 0)
                    {
                        samples.Add(new DXGI.SampleDescription(samplerCount, quality - 1));
                    }
                }

                return(samples.ToArray());
            }
        }
        /// <summary>
        /// Creates a render target texture with the given width and height.
        /// </summary>
        /// <param name="device">Graphics device.</param>
        /// <param name="width">Width of generated texture.</param>
        /// <param name="height">Height of generated texture.</param>
        public D3D11.Texture2D CreateRenderTargetTexture(D3D11.Device device, int width, int height)
        {
            var textureDescription = new D3D11.Texture2DDescription();

            if (TryEnableAntialiasing)
            {
                var maxCount = 1;
                for (var i = 0; i < 32; i++)
                {
                    if (device.CheckMultisampleQualityLevels(DXGI.Format.B8G8R8A8_UNorm, i) > 0)
                    {
                        maxCount = i;
                    }
                }

                textureDescription.Width             = width;
                textureDescription.Height            = height;
                textureDescription.MipLevels         = 1;
                textureDescription.ArraySize         = 1;
                textureDescription.Format            = DXGI.Format.B8G8R8A8_UNorm;
                textureDescription.Usage             = D3D11.ResourceUsage.Default;
                textureDescription.SampleDescription = new DXGI.SampleDescription(maxCount, 0);
                textureDescription.BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
            }
            else
            {
                textureDescription.Width             = width;
                textureDescription.Height            = height;
                textureDescription.MipLevels         = 1;
                textureDescription.ArraySize         = 1;
                textureDescription.Format            = DXGI.Format.B8G8R8A8_UNorm;
                textureDescription.Usage             = D3D11.ResourceUsage.Default;
                textureDescription.SampleDescription = new DXGI.SampleDescription(1, 0);
                textureDescription.BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
            }

            return(new D3D11.Texture2D(device, textureDescription));
        }
Esempio n. 14
0
        private void BuildMultisample()
        {
            var maxCount   = 1;
            var maxQuality = 0;

            for (var i = 0; i <= Device.MultisampleCountMaximum; ++i)
            {
                var quality = Device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, i);
                if (quality <= 0)
                {
                    continue;
                }

                maxCount   = i;
                maxQuality = quality - 1;
            }

            mSwapChainDesc.SampleDescription = new SampleDescription(maxCount, maxQuality);
            mHasMultisample = maxQuality > 0 || maxCount > 1;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="createDepthStencilBuffer"></param>
        /// <returns></returns>
        protected override Texture2D OnCreateRenderTargetAndDepthBuffers(int width, int height, bool createDepthStencilBuffer)
        {
#if MSAA
            int sampleCount   = 1;
            int sampleQuality = 0;
            if (MSAA != MSAALevel.Disable)
            {
                do
                {
                    var newSampleCount   = sampleCount * 2;
                    var newSampleQuality = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, newSampleCount) - 1;

                    if (newSampleQuality < 0)
                    {
                        break;
                    }

                    sampleCount   = newSampleCount;
                    sampleQuality = newSampleQuality;
                    if (sampleCount == (int)MSAA)
                    {
                        break;
                    }
                } while (sampleCount < 32);
            }

            var sampleDesc  = new SampleDescription(sampleCount, sampleQuality);
            var optionFlags = ResourceOptionFlags.None;
#else
            var sampleDesc  = new SampleDescription(1, 0);
            var optionFlags = ResourceOptionFlags.Shared;
#endif

            var colordesc = new Texture2DDescription
            {
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = sampleDesc,
                Usage             = ResourceUsage.Default,
                OptionFlags       = optionFlags,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1
            };

            colorBuffer     = Collect(new Texture2D(Device, colordesc));
            colorBufferView = Collect(new RenderTargetView(Device, colorBuffer));

            if (createDepthStencilBuffer)
            {
                var depthdesc = new Texture2DDescription
                {
                    BindFlags         = BindFlags.DepthStencil,
                    Format            = Format.D32_Float_S8X24_UInt,
                    Width             = width,
                    Height            = height,
                    MipLevels         = 1,
                    SampleDescription = sampleDesc,
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.None,
                    CpuAccessFlags    = CpuAccessFlags.None,
                    ArraySize         = 1,
                };
                depthStencilBuffer     = Collect(new Texture2D(Device, depthdesc));
                depthStencilBufferView = Collect(new DepthStencilView(Device, depthStencilBuffer));
            }
#if MSAA
            var colordescNMS = new Texture2DDescription
            {
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.Shared,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1
            };

            renderTargetNMS = Collect(new Texture2D(Device, colordescNMS));
            Device.ImmediateContext.ResolveSubresource(colorBuffer, 0, renderTargetNMS, 0, Format.B8G8R8A8_UNorm);
            d2dTarget = Collect(new D2DTargetProxy());
            d2dTarget.Initialize(renderTargetNMS, DeviceContext2D);
            return(renderTargetNMS);
#else
            return(colorBuffer);
#endif
        }
        /// <summary>
        /// Called when [create render target and depth buffers].
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="createDepthStencilBuffer"></param>
        /// <returns></returns>
        protected override Texture2D OnCreateRenderTargetAndDepthBuffers(int width, int height, bool createDepthStencilBuffer)
        {
            if (swapChain == null || swapChain.IsDisposed)
            {
                swapChain = CreateSwapChain();
            }
            else
            {
                swapChain.ResizeBuffers(swapChain.Description1.BufferCount, TargetWidth, TargetHeight, swapChain.Description.ModeDescription.Format, swapChain.Description.Flags);
            }

#if MSAA
            int sampleCount   = 1;
            int sampleQuality = 0;
            if (MSAA != MSAALevel.Disable)
            {
                do
                {
                    var newSampleCount   = sampleCount * 2;
                    var newSampleQuality = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, newSampleCount) - 1;

                    if (newSampleQuality < 0)
                    {
                        break;
                    }

                    sampleCount   = newSampleCount;
                    sampleQuality = newSampleQuality;
                    if (sampleCount == (int)MSAA)
                    {
                        break;
                    }
                } while (sampleCount < 32);
            }
            renderTargetNMS = Collect(Texture2D.FromSwapChain <Texture2D>(swapChain, 0));
            var colordesc = renderTargetNMS.Description;
            colordesc.SampleDescription = new SampleDescription(sampleCount, sampleQuality);
            colorBuffer     = Collect(new Texture2D(Device, colordesc));
            colorBufferView = Collect(new RenderTargetView(Device, colorBuffer));
            var sampleDesc = colordesc.SampleDescription;
#else
            colorBuffer = Collect(Texture2D.FromSwapChain <Texture2D>(swapChain, 0));
            var sampleDesc = swapChain.Description1.SampleDescription;
            colorBufferView = Collect(new RenderTargetView(Device, colorBuffer));
#endif

            if (createDepthStencilBuffer)
            {
                var depthdesc = new Texture2DDescription
                {
                    BindFlags = BindFlags.DepthStencil,
                    //Format = Format.D24_UNorm_S8_UInt,
                    Format            = Format.D32_Float_S8X24_UInt,
                    Width             = width,
                    Height            = height,
                    MipLevels         = 1,
                    SampleDescription = sampleDesc,
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.None,
                    CpuAccessFlags    = CpuAccessFlags.None,
                    ArraySize         = 1,
                };
                depthStencilBuffer     = Collect(new Texture2D(Device, depthdesc));
                depthStencilBufferView = Collect(new DepthStencilView(Device, depthStencilBuffer));
            }
            d2dTarget = Collect(new D2DTargetProxy());
            d2dTarget.Initialize(swapChain, DeviceContext2D);
            return(colorBuffer);
        }
Esempio n. 17
0
        public CAdapter(Adapter1 dxgiAdapter, int index)
        {
            this.dxgiAdapter = dxgiAdapter;
            this.index = index;

            AdapterDescription1 dxgiDesc = dxgiAdapter.Description1;
            desc = new AdapterDescription
            {
                Description = dxgiDesc.Description,
                VendorId = dxgiDesc.VendorId,
                DeviceId = dxgiDesc.DeviceId,
                SubSysId = dxgiDesc.SubsystemId,
                Revision = dxgiDesc.Revision,
                DedicatedVideoMemory = dxgiDesc.DedicatedVideoMemory,
                DedicatedSystemMemory = dxgiDesc.DedicatedSystemMemory,
                SharedSystemMemory = dxgiDesc.SharedSystemMemory,
                AdapterLuidHigh = (int)(dxgiDesc.Luid >> 32),
                AdapterLuidLow = (int)(dxgiDesc.Luid & 0x00000000FFFFFFFF),
                Flags = CtBeholder.AdapterFlags(dxgiDesc.Flags)
            };

            outputs = dxgiAdapter.Outputs.Select(o => new COutput(o)).ToArray();

            using (var d3dDevice = new Device(dxgiAdapter))
            {
                switch (d3dDevice.FeatureLevel)
                {
                    case FeatureLevel.Level_9_1: apiVersion = new ApiVersion(9, 1); break;
                    case FeatureLevel.Level_9_3: apiVersion = new ApiVersion(9, 3); break;
                    case FeatureLevel.Level_9_2: apiVersion = new ApiVersion(9, 2); break;
                    case FeatureLevel.Level_10_0: apiVersion = new ApiVersion(10, 0); break;
                    case FeatureLevel.Level_10_1: apiVersion = new ApiVersion(10, 1); break;
                    case FeatureLevel.Level_11_0: apiVersion = new ApiVersion(11, 0); break;
                    default: throw new ArgumentOutOfRangeException("d3dDevice.FeatureLevel");
                }

                restrictions = new AdapterRestrictions
                {
                    UniformBufferSlots = CommonShaderStage.ConstantBufferApiSlotCount,
                    SamplerSlots = CommonShaderStage.SamplerSlotCount,
                    ShaderResourceSlots = CommonShaderStage.InputResourceSlotCount,
                    UnorderedAccessResourceSlots = ComputeShaderStage.UnorderedAccessViewSlotCount,
                    MaxVertexStreams = InputAssemblerStage.VertexInputResourceSlotCount,
                    MaxVertexStreamElementCount = InputAssemblerStage.VertexInputStructureElementCount,
                    MaxStreamOutputTargets = 4,
                    MaxViewports = 16,
                    MaxRenderTargets = 8,
                    MaxThreadGroupsX = d3dDevice.FeatureLevel == FeatureLevel.Level_11_0 ? ComputeShaderStage.ThreadGroupMaximumX : 768,
                    MaxThreadGroupsY = d3dDevice.FeatureLevel == FeatureLevel.Level_11_0 ? ComputeShaderStage.ThreadGroupMaximumY : 768,
                    MaxThreadGroupsZ = d3dDevice.FeatureLevel == FeatureLevel.Level_11_0 ? ComputeShaderStage.ThreadGroupMaximumZ : 1,
                    MaxThreadGroupsTotal = d3dDevice.FeatureLevel == FeatureLevel.Level_11_0 ? ComputeShaderStage.ThreadGroupMaximumThreadsPerGroup : 768
                };

                var allFormats = ((ExplicitFormat[])Enum.GetValues(typeof(ExplicitFormat))).Select(x => (Format)x).ToArray();

                formatsSupport = allFormats.Select(d3dDevice.CheckFormatSupport).ToArray();
                formatInfos = allFormats.Where(f => f != 0).Select(CtBeholder.FormatInfo).ToArray();
                //depthStencilFormatInfos = allFormats.Where(f => (formatsSupport[(int)f] & DepthStencilSupportMask) != 0).Select(CtBeholder.DepthStencilFormatInfo).ToArray();
                vertexElementFormats = allFormats.Where(f => (formatsSupport[(int)f] & VertexElementSupportMask) != 0).Select(f => (ExplicitFormat)f).ToArray();

                multisampleQualityLevels = new int[allFormats.Length, MultisampleCountsToTry.Length];

                for (int i = 0; i < allFormats.Length; i++)
                    for (int j = 0; j < MultisampleCountsToTry.Length; j++)
                        multisampleQualityLevels[i, j] = d3dDevice.CheckMultisampleQualityLevels((Format)i, MultisampleCountsToTry[j]);
            }
            /*
            using (var writer = new System.IO.StreamWriter("Formats.txt"))
            {
                for (int i = 0; i < formatsSupport.Length; i++)
                {
                    writer.WriteLine(string.Format("{0, -30}:{1}", (Format)i, formatsSupport[i].ToString().Replace(", ", " | ")));
                }
            }*/
        }
Esempio n. 18
0
        public void CreateDeviceAndSwapChain(RenderForm form)
        {
            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0, FeatureLevel.Level_10_0 };
            Device = new Device(DriverType.Hardware, DeviceCreationFlags.None, featureLevels);

            if(ConfigurationManager.Config.AntiAliasing)
                _maxQualityLevel = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, 4);

            var swapChainDescription = new SwapChainDescription
            {
                BufferCount = 1, // Set to a single back buffer (double buffering)
                ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, _refreshRate, Format.R8G8B8A8_UNorm),
                IsWindowed = ConfigurationManager.Config.WindowedMode,
                OutputHandle = form.Handle,
                SampleDescription = ConfigurationManager.Config.AntiAliasing ? new SampleDescription(4, _maxQualityLevel-1) : new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput
            };

            SwapChain = new SwapChain(new Factory(), Device, swapChainDescription);

            DeviceContext = Device.ImmediateContext;

            SwapChain.GetParent<Factory>().MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
        }
Esempio n. 19
0
 static SharpDX.DXGI.SampleDescription DetectSampleDescription(Device device, SharpDX.DXGI.Format format)
 {
     var desc = new SharpDX.DXGI.SampleDescription();
     for (int multisample_count = Device.MultisampleCountMaximum; multisample_count > 0; --multisample_count)
     {
     int quality_levels = device.CheckMultisampleQualityLevels(format, multisample_count);
     if (quality_levels > 0)
     {
         desc.Count = multisample_count;
         desc.Quality = quality_levels - 1;
         break;
     }
     }
     Console.WriteLine("sample count {0} quality {1}", desc.Count, desc.Quality);
     return desc;
 }
Esempio n. 20
0
        unsafe static MyAdapterInfo[] CreateAdaptersList()
        {
            List<MyAdapterInfo> adaptersList = new List<MyAdapterInfo>();

            var factory = GetFactory();
            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 };

            int adapterIndex = 0;

            LogInfoFromWMI();

            for(int i=0; i<factory.Adapters.Length; i++)
            {
                var adapter = factory.Adapters[i];
                Device adapterTestDevice = null;
                try
                {
                    adapterTestDevice = new Device(adapter, DeviceCreationFlags.None, featureLevels);
                }
                catch(SharpDXException e)
                {

                }

                bool supportedDevice = adapterTestDevice != null;

                bool supportsConcurrentResources = false;
                bool supportsCommandLists = false;
                if (supportedDevice)
                {
                    adapterTestDevice.CheckThreadingSupport(out supportsConcurrentResources, out supportsCommandLists);
                }

                // DedicatedSystemMemory = bios or DVMT preallocated video memory, that cannot be used by OS - need retest on pc with only cpu/chipset based graphic
                // DedicatedVideoMemory = discrete graphic video memory
                // SharedSystemMemory = aditional video memory, that can be taken from OS RAM when needed
                void * vramptr = ((IntPtr)(adapter.Description.DedicatedSystemMemory != 0 ? adapter.Description.DedicatedSystemMemory : adapter.Description.DedicatedVideoMemory)).ToPointer();
                UInt64 vram = (UInt64)vramptr;
                void * svramptr = ((IntPtr)adapter.Description.SharedSystemMemory).ToPointer();
                UInt64 svram = (UInt64)svramptr;

                // microsoft software renderer allocates 256MB shared memory, cpu integrated graphic on notebooks has 0 preallocated, all shared
                supportedDevice = supportedDevice && (vram > 500000000 || svram > 500000000);

                var deviceDesc = String.Format("{0}, dev id: {1}, mem: {2}, shared mem: {3}, Luid: {4}, rev: {5}, subsys id: {6}, vendor id: {7}",
                    adapter.Description.Description,
                    adapter.Description.DeviceId,
                    vram,
                    svram,
                    adapter.Description.Luid,
                    adapter.Description.Revision,
                    adapter.Description.SubsystemId,
                    adapter.Description.VendorId
                    );

                var info = new MyAdapterInfo
                {
                    Name = adapter.Description.Description,
                    DeviceName = adapter.Description.Description,
                    Description = deviceDesc,
                    IsDx11Supported = supportedDevice,
                    AdapterDeviceId = i,
                    Priority = VendorPriority(adapter.Description.VendorId),
                    HDRSupported = true,
                    MaxTextureSize = SharpDX.Direct3D11.Texture2D.MaximumTexture2DSize,
                    VRAM = vram > 0 ? vram : svram,
                    Has512MBRam = (vram > 500000000 || svram > 500000000),
                    MultithreadedRenderingSupported = supportsCommandLists
                };

                if(info.VRAM >= 2000000000)
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.HIGH;
                }
                else if (info.VRAM >= 1000000000)
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.MEDIUM;
                }
                else
                { 
                    info.MaxTextureQualitySupported = MyTextureQuality.LOW;
                }

                info.MaxAntialiasingModeSupported = MyAntialiasingMode.FXAA;
                if (supportedDevice)
                {
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 2) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_2;
                    }
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 4) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_4;
                    }
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 8) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_8;
                    }
                }

                LogAdapterInfoBegin(ref info);

                if(supportedDevice)
                {
                    bool outputsAttached = adapter.Outputs.Length > 0;

                    if (outputsAttached)
                    {
                        for (int j = 0; j < adapter.Outputs.Length; j++)
                        {
                            var output = adapter.Outputs[j];

                            info.Name = String.Format("{0} + {1}", adapter.Description.Description, output.Description.DeviceName);
                            info.OutputName = output.Description.DeviceName;
                            info.OutputId = j;

                            var displayModeList = output.GetDisplayModeList(MyRender11Constants.BACKBUFFER_FORMAT, DisplayModeEnumerationFlags.Interlaced);
                            var adapterDisplayModes = new MyDisplayMode[displayModeList.Length];
                            for (int k = 0; k < displayModeList.Length; k++)
                            {
                                var displayMode = displayModeList[k];

                                adapterDisplayModes[k] = new MyDisplayMode
                                {
                                    Height = displayMode.Height,
                                    Width = displayMode.Width,
                                    RefreshRate = displayMode.RefreshRate.Numerator,
                                    RefreshRateDenominator = displayMode.RefreshRate.Denominator
                                };
                            }
                            Array.Sort(adapterDisplayModes, m_refreshRatePriorityComparer);

                            info.SupportedDisplayModes = adapterDisplayModes;
                            info.CurrentDisplayMode = adapterDisplayModes[adapterDisplayModes.Length - 1];
                            LogOutputDisplayModes(ref info);

                            m_adapterModes[adapterIndex] = displayModeList;

                            // add one entry per every adapter-output pair
                            adaptersList.Add(info);
                            adapterIndex++;
                        }
                    }
                    else
                    {
                        // FALLBACK MODES

                        MyDisplayMode[] fallbackDisplayModes = new MyDisplayMode[] {
                            new MyDisplayMode(640, 480, 60000, 1000),
                            new MyDisplayMode(720, 576, 60000, 1000),
                            new MyDisplayMode(800, 600, 60000, 1000),
                            new MyDisplayMode(1024, 768, 60000, 1000),
                            new MyDisplayMode(1152, 864, 60000, 1000),
                            new MyDisplayMode(1280, 720, 60000, 1000),
                            new MyDisplayMode(1280, 768, 60000, 1000),
                            new MyDisplayMode(1280, 800, 60000, 1000),
                            new MyDisplayMode(1280, 960, 60000, 1000),
                            new MyDisplayMode(1280, 1024, 60000, 1000),
                            new MyDisplayMode(1360, 768, 60000, 1000),
                            new MyDisplayMode(1360, 1024, 60000, 1000),
                            new MyDisplayMode(1440, 900, 60000, 1000),
                            new MyDisplayMode(1600, 900, 60000, 1000),
                            new MyDisplayMode(1600, 1024, 60000, 1000),
                            new MyDisplayMode(1600, 1200, 60000, 1000),
                            new MyDisplayMode(1680, 1200, 60000, 1000),
                            new MyDisplayMode(1680, 1050, 60000, 1000),
                            new MyDisplayMode(1920, 1080, 60000, 1000),
                            new MyDisplayMode(1920, 1200, 60000, 1000),
                        };

                        info.OutputName = "FallbackOutput";
                        info.Name = String.Format("{0}", adapter.Description.Description);
                        info.OutputId = 0;
                        info.CurrentDisplayMode = fallbackDisplayModes[fallbackDisplayModes.Length - 1];

                        info.SupportedDisplayModes = fallbackDisplayModes;
                        info.FallbackDisplayModes = true;

                        // add one entry for adapter-fallback output pair
                        adaptersList.Add(info);
                        adapterIndex++;
                    }
                }
                else
                {
                    info.SupportedDisplayModes = new MyDisplayMode[0];
                }

                MyRender11.Log.WriteLine("Fallback display modes = " + info.FallbackDisplayModes);

                LogAdapterInfoEnd();

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

            return adaptersList.ToArray();
        }
Esempio n. 21
0
        unsafe static MyAdapterInfo[] CreateAdaptersList()
        {
            List <MyAdapterInfo> adaptersList = new List <MyAdapterInfo>();

            var factory = GetFactory();

            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 };

            int adapterIndex = 0;

            LogInfoFromWMI();

            for (int i = 0; i < factory.Adapters.Length; i++)
            {
                var    adapter           = factory.Adapters[i];
                Device adapterTestDevice = null;
                try
                {
                    adapterTestDevice = new Device(adapter, DeviceCreationFlags.None, featureLevels);
                }
                catch (SharpDXException e)
                {
                }

                bool supportedDevice = adapterTestDevice != null;

                bool supportsConcurrentResources = false;
                bool supportsCommandLists        = false;
                if (supportedDevice)
                {
                    adapterTestDevice.CheckThreadingSupport(out supportsConcurrentResources, out supportsCommandLists);
                }

                void *ptr  = ((IntPtr)adapter.Description.DedicatedVideoMemory).ToPointer();
                ulong vram = (ulong)ptr;

                var deviceDesc = String.Format("{0}, dev id: {1}, shared mem: {2}, Luid: {3}, rev: {4}, subsys id: {5}, vendor id: {6}",
                                               adapter.Description.Description,
                                               adapter.Description.DeviceId,
                                               vram,
                                               adapter.Description.Luid,
                                               adapter.Description.Revision,
                                               adapter.Description.SubsystemId,
                                               adapter.Description.VendorId
                                               );

                var info = new MyAdapterInfo
                {
                    Name            = adapter.Description.Description,
                    DeviceName      = adapter.Description.Description,
                    Description     = deviceDesc,
                    IsSupported     = supportedDevice,
                    AdapterDeviceId = i,

                    Has512MBRam    = vram > 500000000,
                    HDRSupported   = true,
                    MaxTextureSize = SharpDX.Direct3D11.Texture2D.MaximumTexture2DSize,

                    VRAM = vram,
                    MultithreadedRenderingSupported = supportsCommandLists
                };

                if (vram >= 2000000000)
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.HIGH;
                }
                else if (vram >= 1000000000)
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.MEDIUM;
                }
                else
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.LOW;
                }

                info.MaxAntialiasingModeSupported = MyAntialiasingMode.FXAA;
                if (supportedDevice)
                {
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 2) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_2;
                    }
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 4) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_4;
                    }
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 8) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_8;
                    }
                }

                LogAdapterInfoBegin(ref info);

                if (supportedDevice)
                {
                    for (int j = 0; j < factory.Adapters[i].Outputs.Length; j++)
                    {
                        var output = factory.Adapters[i].Outputs[j];

                        info.Name       = String.Format("{0} + {1}", adapter.Description.Description, output.Description.DeviceName);
                        info.OutputName = output.Description.DeviceName;
                        info.OutputId   = j;

                        var displayModeList     = factory.Adapters[i].Outputs[j].GetDisplayModeList(MyRender11Constants.BACKBUFFER_FORMAT, DisplayModeEnumerationFlags.Interlaced);
                        var adapterDisplayModes = new MyDisplayMode[displayModeList.Length];
                        for (int k = 0; k < displayModeList.Length; k++)
                        {
                            var displayMode = displayModeList[k];

                            adapterDisplayModes[k] = new MyDisplayMode
                            {
                                Height                 = displayMode.Height,
                                Width                  = displayMode.Width,
                                RefreshRate            = displayMode.RefreshRate.Numerator,
                                RefreshRateDenominator = displayMode.RefreshRate.Denominator
                            };
                        }
                        Array.Sort(adapterDisplayModes, m_refreshRatePriorityComparer);

                        info.SupportedDisplayModes = adapterDisplayModes;
                        info.CurrentDisplayMode    = adapterDisplayModes[adapterDisplayModes.Length - 1];


                        adaptersList.Add(info);
                        m_adapterModes[adapterIndex] = displayModeList;
                        adapterIndex++;

                        LogOutputDisplayModes(ref info);
                    }
                }
                else
                {
                    info.SupportedDisplayModes = new MyDisplayMode[0];
                    adaptersList.Add(info);
                    adapterIndex++;
                }

                LogAdapterInfoEnd();

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

            return(adaptersList.ToArray());
        }
Esempio n. 22
0
 private bool CheckFormatMultisample(Format format, int sampleCount)
 {
     return(_device.CheckMultisampleQualityLevels(format, sampleCount) != 0);
 }
        unsafe static MyAdapterInfo[] CreateAdaptersList()
        {
            List<MyAdapterInfo> adaptersList = new List<MyAdapterInfo>();

            var factory = GetFactory();
            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 };

            int adapterIndex = 0;

            LogInfoFromWMI();

            for(int i=0; i<factory.Adapters.Length; i++)
            {
                var adapter = factory.Adapters[i];
                Device adapterTestDevice = null;
                try
                {
                    adapterTestDevice = new Device(adapter, DeviceCreationFlags.None, featureLevels);
                }
                catch(SharpDXException e)
                {

                }

                bool supportedDevice = adapterTestDevice != null;

                bool supportsConcurrentResources = false;
                bool supportsCommandLists = false;
                if (supportedDevice)
                {
                    adapterTestDevice.CheckThreadingSupport(out supportsConcurrentResources, out supportsCommandLists);
                }

                void* ptr = ((IntPtr)adapter.Description.DedicatedVideoMemory).ToPointer();
                ulong vram = (ulong)ptr;

                var deviceDesc = String.Format("{0}, dev id: {1}, shared mem: {2}, Luid: {3}, rev: {4}, subsys id: {5}, vendor id: {6}",
                    adapter.Description.Description,
                    adapter.Description.DeviceId,
                    vram,
                    adapter.Description.Luid,
                    adapter.Description.Revision,
                    adapter.Description.SubsystemId,
                    adapter.Description.VendorId
                    );

                var info = new MyAdapterInfo
                {
                    Name = adapter.Description.Description,
                    DeviceName = adapter.Description.Description,
                    Description = deviceDesc,
                    IsSupported = supportedDevice,
                    AdapterDeviceId = i,

                    Has512MBRam = vram > 500000000,
                    HDRSupported = true,
                    MaxTextureSize = SharpDX.Direct3D11.Texture2D.MaximumTexture2DSize,

                    VRAM = vram,
                    MultithreadedRenderingSupported = supportsCommandLists
                };

                if(vram >= 2000000000)
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.HIGH;
                }
                else if (vram >= 1000000000)
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.MEDIUM;
                }
                else
                { 
                    info.MaxTextureQualitySupported = MyTextureQuality.LOW;
                }

                info.MaxAntialiasingModeSupported = MyAntialiasingMode.FXAA;
                if (supportedDevice)
                {
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 2) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_2;
                    }
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 4) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_4;
                    }
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 8) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_8;
                    }
                }

                LogAdapterInfoBegin(ref info);

                if(supportedDevice)
                {
                    for(int j=0; j<factory.Adapters[i].Outputs.Length; j++)
                    {
                        var output = factory.Adapters[i].Outputs[j];

                        info.Name = String.Format("{0} + {1}", adapter.Description.Description, output.Description.DeviceName);
                        info.OutputName = output.Description.DeviceName;
                        info.OutputId = j;

                        var displayModeList = factory.Adapters[i].Outputs[j].GetDisplayModeList(MyRender11Constants.BACKBUFFER_FORMAT, DisplayModeEnumerationFlags.Interlaced);
                        var adapterDisplayModes = new MyDisplayMode[displayModeList.Length];
                        for (int k = 0; k < displayModeList.Length; k++)
                        {
                            var displayMode = displayModeList[k];

                            adapterDisplayModes[k] = new MyDisplayMode 
                            { 
                                Height = displayMode.Height, 
                                Width = displayMode.Width, 
                                RefreshRate = displayMode.RefreshRate.Numerator, 
                                RefreshRateDenominator = displayMode.RefreshRate.Denominator 
                            }; 
                        }
                        Array.Sort(adapterDisplayModes, m_refreshRatePriorityComparer);

                        info.SupportedDisplayModes = adapterDisplayModes;
                        info.CurrentDisplayMode = adapterDisplayModes[adapterDisplayModes.Length - 1];


                        adaptersList.Add(info);
                        m_adapterModes[adapterIndex] = displayModeList;
                        adapterIndex++;

                        LogOutputDisplayModes(ref info);
                    }
                }
                else
                {
                    info.SupportedDisplayModes = new MyDisplayMode[0];
                    adaptersList.Add(info);
                    adapterIndex++;
                }

                LogAdapterInfoEnd();

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

            return adaptersList.ToArray();
        }
        unsafe static MyAdapterInfo[] CreateAdaptersList()
        {
            List <MyAdapterInfo> adaptersList = new List <MyAdapterInfo>();

            var factory = GetFactory();

            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 };

            int adapterIndex = 0;

            LogInfoFromWMI(Log);
            LogInfoFromWMI(MyLog.Default);

            for (int i = 0; i < factory.Adapters.Length; i++)
            {
                var    adapter           = factory.Adapters[i];
                Device adapterTestDevice = null;
                try
                {
                    adapterTestDevice = new Device(adapter, DeviceCreationFlags.None, featureLevels);
                }
                catch (SharpDXException)
                {
                }

                bool supportedDevice = adapterTestDevice != null;

                bool supportsConcurrentResources = false;
                bool supportsCommandLists        = false;
                if (supportedDevice)

                {
                    adapterTestDevice.CheckThreadingSupport(out supportsConcurrentResources, out supportsCommandLists);
                }

                // DedicatedSystemMemory = bios or DVMT preallocated video memory, that cannot be used by OS - need retest on pc with only cpu/chipset based graphic
                // DedicatedVideoMemory = discrete graphic video memory
                // SharedSystemMemory = aditional video memory, that can be taken from OS RAM when needed
                void * vramptr  = ((IntPtr)(adapter.Description.DedicatedSystemMemory != 0 ? adapter.Description.DedicatedSystemMemory : adapter.Description.DedicatedVideoMemory)).ToPointer();
                UInt64 vram     = (UInt64)vramptr;
                void * svramptr = ((IntPtr)adapter.Description.SharedSystemMemory).ToPointer();
                UInt64 svram    = (UInt64)svramptr;

                // microsoft software renderer allocates 256MB shared memory, cpu integrated graphic on notebooks has 0 preallocated, all shared
                supportedDevice = supportedDevice && (vram > 500000000 || svram > 500000000);

                var deviceDesc = String.Format("{0}, dev id: {1}, mem: {2}, shared mem: {3}, Luid: {4}, rev: {5}, subsys id: {6}, vendor id: {7}",
                                               adapter.Description.Description,
                                               adapter.Description.DeviceId,
                                               vram,
                                               svram,
                                               adapter.Description.Luid,
                                               adapter.Description.Revision,
                                               adapter.Description.SubsystemId,
                                               adapter.Description.VendorId
                                               );

                var info = new MyAdapterInfo
                {
                    Name            = adapter.Description.Description,
                    DeviceName      = adapter.Description.Description,
                    VendorId        = adapter.Description.VendorId,
                    DeviceId        = adapter.Description.DeviceId,
                    Description     = deviceDesc,
                    IsDx11Supported = supportedDevice,
                    AdapterDeviceId = i,
                    Priority        = VendorPriority(adapter.Description.VendorId),
                    HDRSupported    = true,
                    MaxTextureSize  = SharpDX.Direct3D11.Texture2D.MaximumTexture2DSize,
                    VRAM            = vram > 0 ? vram : svram,
                    Has512MBRam     = (vram > 500000000 || svram > 500000000),
                    MultithreadedRenderingSupported = supportsCommandLists
                };

                if (info.VRAM >= 2000000000)
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.HIGH;
                }
                else if (info.VRAM >= 1000000000)
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.MEDIUM;
                }
                else
                {
                    info.MaxTextureQualitySupported = MyTextureQuality.LOW;
                }

                info.MaxAntialiasingModeSupported = MyAntialiasingMode.FXAA;
                if (supportedDevice)
                {
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 2) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_2;
                    }
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 4) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_4;
                    }
                    if (adapterTestDevice.CheckMultisampleQualityLevels(Format.R11G11B10_Float, 8) > 0)
                    {
                        info.MaxAntialiasingModeSupported = MyAntialiasingMode.MSAA_8;
                    }
                }

                LogAdapterInfoBegin(ref info);

                if (supportedDevice)
                {
                    bool outputsAttached = adapter.Outputs.Length > 0;

                    if (outputsAttached)
                    {
                        for (int j = 0; j < adapter.Outputs.Length; j++)
                        {
                            var output = adapter.Outputs[j];

                            info.Name       = String.Format("{0} + {1}", adapter.Description.Description, output.Description.DeviceName);
                            info.OutputName = output.Description.DeviceName;
                            info.OutputId   = j;

                            var displayModeList     = output.GetDisplayModeList(MyRender11Constants.DX11_BACKBUFFER_FORMAT, DisplayModeEnumerationFlags.Interlaced);
                            var adapterDisplayModes = new MyDisplayMode[displayModeList.Length];
                            for (int k = 0; k < displayModeList.Length; k++)
                            {
                                var displayMode = displayModeList[k];

                                adapterDisplayModes[k] = new MyDisplayMode
                                {
                                    Height                 = displayMode.Height,
                                    Width                  = displayMode.Width,
                                    RefreshRate            = displayMode.RefreshRate.Numerator,
                                    RefreshRateDenominator = displayMode.RefreshRate.Denominator
                                };
                            }
                            Array.Sort(adapterDisplayModes, m_refreshRatePriorityComparer);

                            info.SupportedDisplayModes = adapterDisplayModes;
                            info.CurrentDisplayMode    = adapterDisplayModes[adapterDisplayModes.Length - 1];
                            LogOutputDisplayModes(ref info);

                            m_adapterModes[adapterIndex] = displayModeList;

                            // add one entry per every adapter-output pair
                            adaptersList.Add(info);
                            adapterIndex++;
                        }
                    }
                    else
                    {
                        // FALLBACK MODES

                        MyDisplayMode[] fallbackDisplayModes = new MyDisplayMode[] {
                            new MyDisplayMode(640, 480, 60000, 1000),
                            new MyDisplayMode(720, 576, 60000, 1000),
                            new MyDisplayMode(800, 600, 60000, 1000),
                            new MyDisplayMode(1024, 768, 60000, 1000),
                            new MyDisplayMode(1152, 864, 60000, 1000),
                            new MyDisplayMode(1280, 720, 60000, 1000),
                            new MyDisplayMode(1280, 768, 60000, 1000),
                            new MyDisplayMode(1280, 800, 60000, 1000),
                            new MyDisplayMode(1280, 960, 60000, 1000),
                            new MyDisplayMode(1280, 1024, 60000, 1000),
                            new MyDisplayMode(1360, 768, 60000, 1000),
                            new MyDisplayMode(1360, 1024, 60000, 1000),
                            new MyDisplayMode(1440, 900, 60000, 1000),
                            new MyDisplayMode(1600, 900, 60000, 1000),
                            new MyDisplayMode(1600, 1024, 60000, 1000),
                            new MyDisplayMode(1600, 1200, 60000, 1000),
                            new MyDisplayMode(1680, 1200, 60000, 1000),
                            new MyDisplayMode(1680, 1050, 60000, 1000),
                            new MyDisplayMode(1920, 1080, 60000, 1000),
                            new MyDisplayMode(1920, 1200, 60000, 1000),
                        };

                        info.OutputName = "FallbackOutput";

                        info.Name               = String.Format("{0}", adapter.Description.Description);
                        info.OutputId           = 0;
                        info.CurrentDisplayMode = fallbackDisplayModes[fallbackDisplayModes.Length - 1];

                        info.SupportedDisplayModes = fallbackDisplayModes;
                        info.FallbackDisplayModes  = true;

                        // add one entry for adapter-fallback output pair
                        adaptersList.Add(info);
                        adapterIndex++;
                    }
                }
                else
                {
                    info.SupportedDisplayModes = new MyDisplayMode[0];
                }

                Log.WriteLine("Fallback display modes = " + info.FallbackDisplayModes);

                LogAdapterInfoEnd();

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

            return(adaptersList.ToArray());
        }
Esempio n. 25
0
File: DX11.cs Progetto: ndech/Alpha
        public void CreateDeviceAndSwapChain(RenderForm form)
        {
            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 };
            #if GPU_DEBUG
            //Use https://dev.windows.com/en-us/develop/graphics-debugging-and-frame-analysis-update on Windows 10 to install debug layer.
            Device = new Device(DriverType.Hardware, DeviceCreationFlags.Debug, featureLevels);
            #else
            Device = new Device(DriverType.Hardware, DeviceCreationFlags.None, featureLevels);
            #endif
            if(ConfigurationManager.Config.AntiAliasing)
                _maxQualityLevel = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, 4);

            var swapChainDescription = new SwapChainDescription
            {
                BufferCount = 1, // Set to a single back buffer (double buffering)
                ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, _refreshRate, Format.R8G8B8A8_UNorm),
                IsWindowed = ConfigurationManager.Config.WindowedMode,
                OutputHandle = form.Handle,
                SampleDescription = ConfigurationManager.Config.AntiAliasing ? new SampleDescription(4, _maxQualityLevel-1) : new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput
            };

            SwapChain = new SwapChain(new Factory(), Device, swapChainDescription);

            DeviceContext = Device.ImmediateContext;

            SwapChain.GetParent<Factory>().MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
        }