예제 #1
0
        SharpDX.DXGI.SampleDescription GetSampleDescription(uint samples)
        {
            SharpDX.DXGI.SampleDescription[] descs = new SharpDX.DXGI.SampleDescription[16];
            samples = Math.Max(1u, Math.Min(samples, 16u));

            descs[0].Count   = 1;
            descs[0].Quality = 0;

            for (uint i = 1, last = 0; i < samples; i++)
            {
                SharpDX.Direct3D12.FeatureDataMultisampleQualityLevels levels = new SharpDX.Direct3D12.FeatureDataMultisampleQualityLevels();
                levels.Format            = _format;
                levels.SampleCount       = (int)i + 1;
                levels.Flags             = SharpDX.Direct3D12.MultisampleQualityLevelFlags.None;
                levels.QualityLevelCount = 0;

                _dev.CheckFeatureSupport(SharpDX.Direct3D12.Feature.MultisampleQualityLevels, ref levels);

                if (levels.QualityLevelCount > 0)
                {
                    descs[i].Count   = levels.SampleCount;
                    descs[i].Quality = 0;
                    last             = i;
                }
                else
                {
                    descs[i] = descs[last];
                }
            }

            return(descs[samples - 1]);
        }
예제 #2
0
        private void CreateDevice()
        {
            using (SharpDX.DXGI.Factory4 factory = new Factory4())
            {
                _dev = new SharpDX.Direct3D12.Device(factory.Adapters[0], FeatureLevel.Level_11_0);
            }

            int[] levels_ =
            {
                (int)FeatureLevel.Level_12_1,
                (int)FeatureLevel.Level_12_0,
                (int)FeatureLevel.Level_11_1,
                (int)FeatureLevel.Level_11_0
            };

            GCHandle pinnedArray = GCHandle.Alloc(levels_, GCHandleType.Pinned);
            IntPtr   pointer     = pinnedArray.AddrOfPinnedObject();

            SharpDX.Direct3D.FeatureLevel level = FeatureLevel.Level_11_0;
            SharpDX.Direct3D12.FeatureDataFeatureLevels levels = new FeatureDataFeatureLevels();
            levels.FeatureLevelCount             = levels_.Length;
            levels.FeatureLevelsRequestedPointer = pointer;
            if (_dev.CheckFeatureSupport(SharpDX.Direct3D12.Feature.FeatureLevels, ref levels))
            {
                level = levels.MaxSupportedFeatureLevel;
            }

            pinnedArray.Free();

            System.Console.WriteLine($"  Feature Level: {(((int)level) >> 12) & 0xF}_{(((int)level) >> 8) & 0xF}");
        }
예제 #3
0
        protected void InitDirect3D()
        {
#if DEBUG
            DebugInterface.Get().EnableDebugLayer();
#endif

            _factory = new Factory4();

            try
            {
                Adapter[] gpus = _factory.Adapters;
                int       n    = gpus.Length;
                // Try to create hardware device.
                // Pass NULL to use the default adapter which is the first adapter that is enumerated by Factory.Adapters.
                // Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/dn770336(v=vs.85).aspx
                if (n > 1)
                {
                    Adapter dgpu = gpus[1]; //added by JO: ensures we use the dGPU (NVIDIA in my case)
                    Device = new Device(dgpu, FeatureLevel.Level_11_0);
                }
                else
                {
                    Device = new Device(null, FeatureLevel.Level_11_0);
                }
            }
            catch (SharpDXException)
            {
                // Fallback to WARP device.
                Adapter warpAdapter = _factory.CreateSoftwareAdapter(_appInst);
                Device = new Device(warpAdapter, FeatureLevel.Level_11_0);
            }

            Fence       = Device.CreateFence(0, FenceFlags.None);
            _fenceEvent = new AutoResetEvent(false);

            RtvDescriptorSize       = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
            DsvDescriptorSize       = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.DepthStencilView);
            CbvSrvUavDescriptorSize = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);

            // Check 4X MSAA quality support for our back buffer format.
            // All Direct3D 11 capable devices support 4X MSAA for all render
            // target formats, so we only need to check quality support.

            FeatureDataMultisampleQualityLevels msQualityLevels;
            msQualityLevels.Format            = BackBufferFormat;
            msQualityLevels.SampleCount       = 4;
            msQualityLevels.Flags             = MultisampleQualityLevelFlags.None;
            msQualityLevels.QualityLevelCount = 0;
            Debug.Assert(Device.CheckFeatureSupport(Feature.MultisampleQualityLevels, ref msQualityLevels));
            _m4xMsaaQuality = msQualityLevels.QualityLevelCount;

#if DEBUG
            LogAdapters();
#endif

            CreateCommandObjects();
            CreateSwapChain();
            CreateRtvAndDsvDescriptorHeaps();
        }
예제 #4
0
        /// <summary>
        /// Gets the maximum multisample 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 MultisampleCount GetMaximumMultisampleCount(SharpDX.Direct3D12.Device device, SharpDX.DXGI.Format pixelFormat)
        {
            SharpDX.Direct3D12.FeatureDataMultisampleQualityLevels qualityLevels;
            qualityLevels.Format            = pixelFormat;
            qualityLevels.Flags             = MultisampleQualityLevelFlags.None;
            qualityLevels.QualityLevelCount = 0;

            int maxCount = 1;

            for (int i = 8; i >= 1; i /= 2)
            {
                qualityLevels.SampleCount = i;
                if (device.CheckFeatureSupport(SharpDX.Direct3D12.Feature.MultisampleQualityLevels, ref qualityLevels))
                {
                    maxCount = i;
                    break;
                }
            }
            return((MultisampleCount)maxCount);
        }
예제 #5
0
        protected void InitDirect3D()
        {
#if DEBUG
            // The Direct3D 12 debug layer may or may not be installed. It's installation can be
            // managed through settings page "Manage optional features" with a feature called
            // "Graphics Tools".
            // There may be a better solution to check for it instead of try/catch. If you happen
            // to know, please consider opening an issue or PR in the repo.
            try
            {
                DebugInterface.Get().EnableDebugLayer();
            }
            catch (SharpDXException ex) when(ex.Descriptor.NativeApiCode == "DXGI_ERROR_SDK_COMPONENT_MISSING")
            {
                Debug.WriteLine("Failed to enable debug layer. Please ensure \"Graphics Tools\" feature is enabled in Windows \"Manage optional feature\" settings page");
            }
#endif

            _factory = new Factory4();

            try
            {
                // Try to create hardware device.
                // Pass NULL to use the default adapter which is the first adapter that is enumerated by Factory.Adapters.
                // Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/dn770336(v=vs.85).aspx
                Device = new Device(null, FeatureLevel.Level_11_0);
            }
            catch (SharpDXException)
            {
                // Fallback to WARP device.
                Adapter warpAdapter = _factory.GetWarpAdapter();
                Device = new Device(warpAdapter, FeatureLevel.Level_11_0);
            }

            Fence       = Device.CreateFence(0, FenceFlags.None);
            _fenceEvent = new AutoResetEvent(false);

            RtvDescriptorSize       = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
            DsvDescriptorSize       = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.DepthStencilView);
            CbvSrvUavDescriptorSize = Device.GetDescriptorHandleIncrementSize(
                DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);

            // Check 4X MSAA quality support for our back buffer format.
            // All Direct3D 11 capable devices support 4X MSAA for all render
            // target formats, so we only need to check quality support.

            FeatureDataMultisampleQualityLevels msQualityLevels;
            msQualityLevels.Format            = BackBufferFormat;
            msQualityLevels.SampleCount       = 4;
            msQualityLevels.Flags             = MultisampleQualityLevelFlags.None;
            msQualityLevels.QualityLevelCount = 0;
            Debug.Assert(Device.CheckFeatureSupport(Feature.MultisampleQualityLevels, ref msQualityLevels));
            _m4xMsaaQuality = msQualityLevels.QualityLevelCount;

#if DEBUG
            LogAdapters();
#endif

            CreateCommandObjects();
            CreateSwapChain();
            CreateRtvAndDsvDescriptorHeaps();
        }