/// <summary>
        /// Function to build the Direct3D 11 depth/stencil state.
        /// </summary>
        /// <param name="device">The device object used to create the state.</param>
        /// <returns>The D3D11 depth/stencil state.</returns>
        internal D3D11.DepthStencilState GetD3D11DepthStencilState(D3D11.Device5 device)
        {
            var desc = new D3D11.DepthStencilStateDescription
            {
                BackFace = new D3D11.DepthStencilOperationDescription
                {
                    DepthFailOperation = (D3D11.StencilOperation)BackFaceStencilOp.DepthFailOperation,
                    Comparison         = (D3D11.Comparison)BackFaceStencilOp.Comparison,
                    PassOperation      = (D3D11.StencilOperation)BackFaceStencilOp.PassOperation,
                    FailOperation      = (D3D11.StencilOperation)BackFaceStencilOp.FailOperation
                },
                FrontFace = new D3D11.DepthStencilOperationDescription
                {
                    DepthFailOperation = (D3D11.StencilOperation)FrontFaceStencilOp.DepthFailOperation,
                    Comparison         = (D3D11.Comparison)FrontFaceStencilOp.Comparison,
                    PassOperation      = (D3D11.StencilOperation)FrontFaceStencilOp.PassOperation,
                    FailOperation      = (D3D11.StencilOperation)FrontFaceStencilOp.FailOperation
                },
                DepthComparison  = (D3D11.Comparison)DepthComparison,
                DepthWriteMask   = IsDepthWriteEnabled ? D3D11.DepthWriteMask.All : D3D11.DepthWriteMask.Zero,
                IsDepthEnabled   = IsDepthEnabled,
                IsStencilEnabled = IsStencilEnabled,
                StencilReadMask  = StencilReadMask,
                StencilWriteMask = StencilWriteMask
            };

            return(new D3D11.DepthStencilState(device, desc)
            {
                DebugName = nameof(GorgonDepthStencilState)
            });
        }
        /// <summary>
        /// Function to add the WARP software device.
        /// </summary>
        /// <param name="index">Index of the device.</param>
        /// <param name="factory">The factory used to query the adapter.</param>
        /// <param name="log">The log interface used to send messages to a debug log.</param>
        /// <returns>The video adapter used for WARP software rendering.</returns>
        private static VideoAdapterInfo GetWARPSoftwareDevice(int index, Factory5 factory, IGorgonLog log)
        {
            D3D11.DeviceCreationFlags flags = D3D11.DeviceCreationFlags.None;

            if (GorgonGraphics.IsDebugEnabled)
            {
                flags = D3D11.DeviceCreationFlags.Debug;
            }

            using (Adapter warp = factory.GetWarpAdapter())
                using (Adapter4 warpAdapter4 = warp.QueryInterface <Adapter4>())
                    using (var D3DDevice = new D3D11.Device(warpAdapter4, flags))
                        using (D3D11.Device5 D3DDevice5 = D3DDevice.QueryInterface <D3D11.Device5>())
                        {
                            FeatureSet?featureSet = GetFeatureLevel(D3DDevice5);

                            if (featureSet == null)
                            {
                                log.Print("WARNING: The WARP software adapter does not support the minimum feature set of 12.0. This device will be excluded.", LoggingLevel.All);
                                return(null);
                            }

                            var result = new VideoAdapterInfo(index, warpAdapter4, featureSet.Value, new Dictionary <string, VideoOutputInfo>(), VideoDeviceType.Software);

                            PrintLog(result, log);

                            return(result);
                        }
        }
        /// <summary>
        /// Function to retrieve the outputs attached to a video adapter.
        /// </summary>
        /// <param name="device">The Direct 3D device used to filter display modes.</param>
        /// <param name="adapter">The adapter to retrieve the outputs from.</param>
        /// <param name="outputCount">The number of outputs for the device.</param>
        /// <param name="log">The logging interface used to capture debug messages.</param>
        /// <returns>A list if video output info values.</returns>
        private static Dictionary <string, VideoOutputInfo> GetOutputs(D3D11.Device5 device, Adapter4 adapter, int outputCount, IGorgonLog log)
        {
            var result = new Dictionary <string, VideoOutputInfo>(StringComparer.OrdinalIgnoreCase);

            // Devices created under RDP/TS do not support output selection.
            if (SystemInformation.TerminalServerSession)
            {
                log.Print("Devices under terminal services and software devices devices do not use outputs, no outputs enumerated.", LoggingLevel.Intermediate);
                return(result);
            }

            for (int i = 0; i < outputCount; ++i)
            {
                using (Output output = adapter.GetOutput(i))
                    using (Output6 output6 = output.QueryInterface <Output6>())
                    {
                        var outputInfo = new VideoOutputInfo(i, output6, GetVideoModes(device, output6));

                        if (outputInfo.VideoModes.Count == 0)
                        {
                            log.Print($"Output '{output.Description.DeviceName}' on adapter '{adapter.Description1.Description}' has no full screen video modes.",
                                      LoggingLevel.Intermediate);
                        }

                        result.Add(output.Description.DeviceName, outputInfo);
                    }
            }

            return(result);
        }
Example #4
0
        /// <summary>
        /// Function to build the D3D11 blend state.
        /// </summary>
        /// <param name="device">The device used to create the blend state.</param>
        internal void BuildD3D11BlendState(D3D11.Device5 device)
        {
            Debug.Assert(D3DBlendState == null, "D3D Blend state already assigned to this pipeline state.");

            (int start, int count) = RwBlendStates.GetDirtyItems();
            var desc = new D3D11.BlendStateDescription1
            {
                AlphaToCoverageEnable  = IsAlphaToCoverageEnabled,
                IndependentBlendEnable = IsIndependentBlendingEnabled
            };

            for (int i = 0; i < count; ++i)
            {
                GorgonBlendState state = RwBlendStates[start + i];

                if (state == null)
                {
                    continue;
                }

                desc.RenderTarget[i] = new D3D11.RenderTargetBlendDescription1
                {
                    AlphaBlendOperation     = (D3D11.BlendOperation)state.AlphaBlendOperation,
                    BlendOperation          = (D3D11.BlendOperation)state.ColorBlendOperation,
                    IsLogicOperationEnabled = state.LogicOperation != LogicOperation.Noop,
                    IsBlendEnabled          = state.IsBlendingEnabled,
                    RenderTargetWriteMask   = (D3D11.ColorWriteMaskFlags)state.WriteMask,
                    LogicOperation          = (D3D11.LogicOperation)state.LogicOperation,
                    SourceAlphaBlend        = (D3D11.BlendOption)state.SourceAlphaBlend,
                    SourceBlend             = (D3D11.BlendOption)state.SourceColorBlend,
                    DestinationAlphaBlend   = (D3D11.BlendOption)state.DestinationAlphaBlend,
                    DestinationBlend        = (D3D11.BlendOption)state.DestinationColorBlend
                };
            }

            for (int i = count; i < D3D11.OutputMergerStage.SimultaneousRenderTargetCount; ++i)
            {
                desc.RenderTarget[i] = new D3D11.RenderTargetBlendDescription1
                {
                    AlphaBlendOperation     = D3D11.BlendOperation.Add,
                    BlendOperation          = D3D11.BlendOperation.Add,
                    IsLogicOperationEnabled = false,
                    IsBlendEnabled          = false,
                    RenderTargetWriteMask   = D3D11.ColorWriteMaskFlags.All,
                    LogicOperation          = D3D11.LogicOperation.Noop,
                    SourceAlphaBlend        = D3D11.BlendOption.One,
                    SourceBlend             = D3D11.BlendOption.One,
                    DestinationAlphaBlend   = D3D11.BlendOption.Zero,
                    DestinationBlend        = D3D11.BlendOption.Zero
                };
            }

            D3DBlendState = new D3D11.BlendState1(device, desc)
            {
                DebugName = nameof(GorgonBlendState)
            };
        }
        /// <summary>
        /// Function to retrieve the highest feature set for a video adapter.
        /// </summary>
        /// <param name="device">The D3D device to use.</param>
        /// <returns>The highest available feature set for the device.</returns>
        private static FeatureSet?GetFeatureLevel(D3D11.Device5 device)
        {
            D3D.FeatureLevel result = device.FeatureLevel;

            return(((Enum.IsDefined(typeof(D3D.FeatureLevel), (int)result)) &&
                    (result >= D3D.FeatureLevel.Level_12_0))
                ? (FeatureSet?)result
                : null);
        }
Example #6
0
        /// <summary>
        /// Function to build the D3D11 sampler state.
        /// </summary>
        /// <param name="device">The D3D11 device to use to create the sampler.</param>
        internal void BuildD3D11SamplerState(D3D11.Device5 device)
        {
            Debug.Assert(Native == null, "Already have a native sampler state.");

            var desc = new D3D11.SamplerStateDescription
            {
                BorderColor        = BorderColor.ToRawColor4(),
                ComparisonFunction = (D3D11.Comparison)ComparisonFunction,
                Filter             = (D3D11.Filter)Filter,
                AddressW           = (D3D11.TextureAddressMode)WrapW,
                AddressV           = (D3D11.TextureAddressMode)WrapV,
                AddressU           = (D3D11.TextureAddressMode)WrapU,
                MipLodBias         = MipLevelOfDetailBias,
                MinimumLod         = MinimumLevelOfDetail,
                MaximumLod         = MaximumLevelOfDetail,
                MaximumAnisotropy  = MaxAnisotropy
            };

            Native = new D3D11.SamplerState(device, desc);
        }
Example #7
0
        /// <summary>
        /// Function to retrieve the Direct 3D 11 rasterizer state object.
        /// </summary>
        /// <param name="device">The direct 3D device.</param>
        /// <returns>The D3D 11 rasterizer state object.</returns>
        internal D3D11.RasterizerState2 GetD3D11RasterState(D3D11.Device5 device)
        {
            var desc = new D3D11.RasterizerStateDescription2
            {
                CullMode = (D3D11.CullMode)CullMode,
                ConservativeRasterizationMode =
                    UseConservativeRasterization ? D3D11.ConservativeRasterizationMode.On : D3D11.ConservativeRasterizationMode.Off,
                FillMode                 = (D3D11.FillMode)FillMode,
                DepthBias                = DepthBias,
                DepthBiasClamp           = DepthBiasClamp,
                IsAntialiasedLineEnabled = IsAntialiasedLineEnabled,
                IsFrontCounterClockwise  = IsFrontCounterClockwise,
                SlopeScaledDepthBias     = SlopeScaledDepthBias,
                ForcedSampleCount        = ForcedReadWriteViewSampleCount,
                IsScissorEnabled         = ScissorRectangles.Count > 0,
                IsDepthClipEnabled       = IsDepthClippingEnabled,
                IsMultisampleEnabled     = IsMultisamplingEnabled
            };

            return(new D3D11.RasterizerState2(device, desc)
            {
                DebugName = nameof(GorgonRasterState)
            });
        }
Example #8
0
 /// <summary>
 ///   Constructs a new <see cref = "T:SharpDX.Direct3D11.Fence" />
 /// </summary>
 /// <param name = "device">The device with which to associate the state object.</param>
 /// <param name="initialValue">The initial value for the fence.</param>
 /// <param name="flags">A combination of FenceFlags values that are combined by using a bitwise OR operation. The resulting value specifies options for the fence.</param>
 /// <returns>The newly created object.</returns>
 public Fence(Device5 device, long initialValue, FenceFlags flags)
     : base(IntPtr.Zero)
 {
     device.CreateFence(initialValue, flags, Utilities.GetGuidFromType(typeof(Fence)), this);
 }
        /// <summary>
        /// Function to perform an enumeration of the video adapters attached to the system and populate this list.
        /// </summary>
        /// <param name="enumerateWARPDevice"><b>true</b> to enumerate the WARP software device, or <b>false</b> to exclude it.</param>
        /// <param name="log">The log that will capture debug logging messages.</param>
        /// <remarks>
        /// <para>
        /// Use this method to populate a list with information about the video adapters installed in the system.
        /// </para>
        /// <para>
        /// You may include the WARP device, which is a software based device that emulates most of the functionality of a video adapter, by setting the <paramref name="enumerateWARPDevice"/> to <b>true</b>.
        /// </para>
        /// <para>
        /// Gorgon requires a video adapter that is capable of supporting Direct 3D 12.0 at minimum. If no suitable devices are found installed in the computer, then the resulting list will be empty.
        /// </para>
        /// </remarks>
        public static IReadOnlyList <IGorgonVideoAdapterInfo> Enumerate(bool enumerateWARPDevice, IGorgonLog log)
        {
            var devices = new List <IGorgonVideoAdapterInfo>();

            if (log == null)
            {
                log = GorgonLog.NullLog;
            }

            using (var factory2 = new Factory2(GorgonGraphics.IsDebugEnabled))
                using (Factory5 factory5 = factory2.QueryInterface <Factory5>())
                {
                    int adapterCount = factory5.GetAdapterCount1();

                    log.Print("Enumerating video adapters...", LoggingLevel.Simple);

                    // Begin gathering device information.
                    for (int i = 0; i < adapterCount; i++)
                    {
                        // Get the video adapter information.
                        using (Adapter1 adapter1 = factory5.GetAdapter1(i))
                            using (Adapter4 adapter = adapter1.QueryInterface <Adapter4>())
                            {
                                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                                if (((adapter.Desc3.Flags & AdapterFlags3.Remote) == AdapterFlags3.Remote) ||
                                    ((adapter.Desc3.Flags & AdapterFlags3.Software) == AdapterFlags3.Software))
                                {
                                    continue;
                                }
                                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                                D3D11.DeviceCreationFlags flags = D3D11.DeviceCreationFlags.None;

                                if (GorgonGraphics.IsDebugEnabled)
                                {
                                    flags = D3D11.DeviceCreationFlags.Debug;
                                }

                                // We create a D3D device here to filter out unsupported video modes from the format list.
                                using (var D3DDevice = new D3D11.Device(adapter, flags, D3D.FeatureLevel.Level_12_1, D3D.FeatureLevel.Level_12_0))
                                    using (D3D11.Device5 D3DDevice5 = D3DDevice.QueryInterface <D3D11.Device5>())
                                    {
                                        D3DDevice5.DebugName = "Output enumerator device.";

                                        FeatureSet?featureSet = GetFeatureLevel(D3DDevice5);

                                        // Do not enumerate this device if its feature set is not supported.
                                        if (featureSet == null)
                                        {
                                            log.Print("This video adapter is not supported by Gorgon and will be skipped.", LoggingLevel.Verbose);
                                            continue;
                                        }

                                        Dictionary <string, VideoOutputInfo> outputs = GetOutputs(D3DDevice5, adapter, adapter.GetOutputCount(), log);

                                        if (outputs.Count <= 0)
                                        {
                                            log.Print($"WARNING: Video adapter {adapter.Description1.Description.Replace("\0", string.Empty)} has no outputs. Full screen mode will not be possible.",
                                                      LoggingLevel.Verbose);
                                        }

                                        var videoAdapter = new VideoAdapterInfo(i, adapter, featureSet.Value, outputs, VideoDeviceType.Hardware);

                                        devices.Add(videoAdapter);
                                        PrintLog(videoAdapter, log);
                                    }
                            }
                    }

                    // Get software devices.
                    if (!enumerateWARPDevice)
                    {
                        return(devices);
                    }

                    VideoAdapterInfo device = GetWARPSoftwareDevice(devices.Count, factory5, log);

                    if (device != null)
                    {
                        devices.Add(device);
                    }
                }

            log.Print("Found {0} video adapters.", LoggingLevel.Simple, devices.Count);

            return(devices);
        }