Пример #1
0
        void BuildFormatList(GraphicsDevice device)
        {
            vertexFormatSupported = new Dictionary <VertexElementFormat, bool>();

            GraphicsDeviceCapabilities caps = device.GraphicsDeviceCapabilities;

            System.Reflection.FieldInfo[] enums = typeof(VertexElementFormat).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);

            for (int i = 0; i < enums.Length; i++)
            {
                VertexElementFormat format = (VertexElementFormat)enums[i].GetValue(null);
                vertexFormatSupported.Add(format, true);
            }

            vertexFormatSupported[VertexElementFormat.Byte4]            = caps.DeclarationTypeCapabilities.SupportsByte4;
            vertexFormatSupported[VertexElementFormat.HalfVector2]      = caps.DeclarationTypeCapabilities.SupportsHalfVector2;
            vertexFormatSupported[VertexElementFormat.HalfVector4]      = caps.DeclarationTypeCapabilities.SupportsHalfVector4;
            vertexFormatSupported[VertexElementFormat.Normalized101010] = caps.DeclarationTypeCapabilities.SupportsNormalized101010;
            vertexFormatSupported[VertexElementFormat.NormalizedShort2] = caps.DeclarationTypeCapabilities.SupportsNormalizedShort2;
            vertexFormatSupported[VertexElementFormat.NormalizedShort4] = caps.DeclarationTypeCapabilities.SupportsNormalizedShort4;
            vertexFormatSupported[VertexElementFormat.Rg32]             = caps.DeclarationTypeCapabilities.SupportsRg32;
            vertexFormatSupported[VertexElementFormat.Rgba32]           = caps.DeclarationTypeCapabilities.SupportsRgba32;
            vertexFormatSupported[VertexElementFormat.Rgba64]           = caps.DeclarationTypeCapabilities.SupportsRgba64;
            vertexFormatSupported[VertexElementFormat.UInt101010]       = caps.DeclarationTypeCapabilities.SupportsUInt101010;
        }
Пример #2
0
        private void CheckForAvailableSupportedHardware()
        {
            bool flag  = false;
            bool flag2 = false;

            foreach (GraphicsAdapter adapter in GraphicsAdapter.Adapters)
            {
                if (adapter.IsDeviceTypeAvailable(DeviceType.Hardware))
                {
                    flag = true;
                    GraphicsDeviceCapabilities capabilities = adapter.GetCapabilities(DeviceType.Hardware);
                    if (((capabilities.MaxPixelShaderProfile != ShaderProfile.Unknown) &&
                         (capabilities.MaxPixelShaderProfile >= ShaderProfile.PS_1_1)) &&
                        capabilities.DeviceCapabilities.IsDirect3D9Driver)
                    {
                        flag2 = true;
                        break;
                    }
                }
            }
            if (!flag)
            {
                if (GetSystemMetrics(0x1000) != 0)
                {
                    throw new NoSuitableGraphicsDeviceException("");
                }
                throw new NoSuitableGraphicsDeviceException("");
            }
            if (!flag2)
            {
                throw new NoSuitableGraphicsDeviceException("");
            }
        }
Пример #3
0
        private void OnPreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            GraphicsAdapter adapter = e.GraphicsDeviceInformation.Adapter;

            GraphicsDeviceCapabilities caps = adapter.GetCapabilities(DeviceType.Hardware);

            maxVertexShaderProfile = caps.MaxVertexShaderProfile;
            maxPixelShaderProfile  = caps.MaxPixelShaderProfile;
            supportsShadersV3      = (maxVertexShaderProfile >= ShaderProfile.VS_3_0 && maxPixelShaderProfile >= ShaderProfile.PS_3_0);

            SurfaceFormat          format        = adapter.CurrentDisplayMode.Format;
            PresentationParameters presentParams = e.GraphicsDeviceInformation.PresentationParameters;

            if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.EightSamples))
            {
                presentParams.MultiSampleQuality = 0;
                presentParams.MultiSampleType    = MultiSampleType.EightSamples;
                return;
            }
            if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.FourSamples))
            {
                presentParams.MultiSampleQuality = 0;
                presentParams.MultiSampleType    = MultiSampleType.FourSamples;
                return;
            }
            if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.TwoSamples))
            {
                presentParams.MultiSampleQuality = 0;
                presentParams.MultiSampleType    = MultiSampleType.TwoSamples;
                return;
            }
        }
        private void AddDevices(bool anySuitableDevice, List <GraphicsDeviceInformation> foundDevices)
        {
            IntPtr handle = new System.Windows.Interop.WindowInteropHelper(this.game.Window).Handle;

            foreach (GraphicsAdapter adapter in GraphicsAdapter.Adapters)
            {
                //if (anySuitableDevice)// || this.IsWindowOnAdapter(handle, adapter))
                {
                    foreach (DeviceType type in ValidDeviceTypes)
                    {
                        try
                        {
                            if (adapter.IsDeviceTypeAvailable(type))
                            {
                                GraphicsDeviceCapabilities capabilities = adapter.GetCapabilities(type);
                                if ((capabilities.DeviceCapabilities.IsDirect3D9Driver && IsValidShaderProfile(capabilities.MaxPixelShaderProfile, this.MinimumPixelShaderProfile)) && IsValidShaderProfile(capabilities.MaxVertexShaderProfile, this.MinimumVertexShaderProfile))
                                {
                                    GraphicsDeviceInformation baseDeviceInfo = new GraphicsDeviceInformation();
                                    baseDeviceInfo.Adapter    = adapter;
                                    baseDeviceInfo.DeviceType = type;
                                    baseDeviceInfo.PresentationParameters.DeviceWindowHandle        = IntPtr.Zero;
                                    baseDeviceInfo.PresentationParameters.EnableAutoDepthStencil    = true;
                                    baseDeviceInfo.PresentationParameters.BackBufferCount           = 1;
                                    baseDeviceInfo.PresentationParameters.PresentOptions            = PresentOptions.None;
                                    baseDeviceInfo.PresentationParameters.SwapEffect                = SwapEffect.Discard;
                                    baseDeviceInfo.PresentationParameters.FullScreenRefreshRateInHz = 0;
                                    baseDeviceInfo.PresentationParameters.MultiSampleQuality        = 0;
                                    baseDeviceInfo.PresentationParameters.MultiSampleType           = MultiSampleType.None;
                                    baseDeviceInfo.PresentationParameters.IsFullScreen              = this.IsFullScreen;
                                    baseDeviceInfo.PresentationParameters.PresentationInterval      = this.SynchronizeWithVerticalRetrace ? PresentInterval.One : PresentInterval.Immediate;
                                    for (int i = 0; i < ValidAdapterFormats.Length; i++)
                                    {
                                        this.AddDevices(adapter, type, adapter.CurrentDisplayMode, baseDeviceInfo, foundDevices);
                                        if (this.isFullScreen)
                                        {
                                            foreach (DisplayMode mode in adapter.SupportedDisplayModes[ValidAdapterFormats[i]])
                                            {
                                                if ((mode.Width >= 640) && (mode.Height >= 480))
                                                {
                                                    this.AddDevices(adapter, type, mode, baseDeviceInfo, foundDevices);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (DeviceNotSupportedException)
                        {
                        }
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Change graphic information
        /// </summary>
        public void ChangeGraphics(GraphicsInfo info)
        {
            graphicsInfo = info;

            // Check all available adapters on the system.
            foreach (GraphicsAdapter adapter in GraphicsAdapter.Adapters)
            {
                // Get the capabilities of the hardware device.
                GraphicsDeviceCapabilities caps =
                    adapter.GetCapabilities(DeviceType.Hardware);

                if (caps.MaxPixelShaderProfile < info.pixelShaderProfile)
                {
                    // This adapter does not support Shader Model
                    throw new NotSupportedException(
                              "This adapter does not support Shader Model (" +
                              info.pixelShaderProfile.ToString() + ")");
                }
            }

            //  Initialize Graphics Device setting
            graphicsDeviceManager.PreferredBackBufferWidth =
                graphicsInfo.screenWidth;
            graphicsDeviceManager.PreferredBackBufferHeight =
                graphicsInfo.screenHeight;
            graphicsDeviceManager.MinimumVertexShaderProfile =
                graphicsInfo.vertexShaderProfile;
            graphicsDeviceManager.MinimumPixelShaderProfile =
                graphicsInfo.pixelShaderProfile;
            graphicsDeviceManager.PreferMultiSampling         = graphicsInfo.isMultiSampling;
            graphicsDeviceManager.PreferredDepthStencilFormat = SelectStencilMode();

            //  Apply preparing device setting
#if XBOX
            graphicsDeviceManager.IsFullScreen             = true;
            graphicsDeviceManager.PreparingDeviceSettings +=
                new EventHandler <PreparingDeviceSettingsEventArgs>
                    (graphics_PreparingDeviceSettings);
#else
            graphicsDeviceManager.IsFullScreen = false;
#endif

            graphicsDeviceManager.ApplyChanges();

            //  Set to new default viewport
            Viewer.DefaultViewport = GraphicsDevice.Viewport;

            OnSize();
        }
Пример #6
0
        /// <summary>
        /// Constructor is private, because this is a singleton class.
        /// Client should use the public AddRef method instead.
        /// </summary>
        /// <param name="windowHandle"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        SLGDService(IntPtr windowHandle, int width, int height)
        {
            pp = new PresentationParameters();
            // Check Shader Model 2.0 Support
            GraphicsDeviceCapabilities gdcap = GraphicsAdapter.DefaultAdapter.GetCapabilities(DeviceType.Hardware);

            if (gdcap.MaxPixelShaderProfile < ShaderProfile.PS_2_0 || gdcap.MaxVertexShaderProfile < ShaderProfile.VS_2_0)
            {
                MessageBox.Show("This Adapter Does Not Support Shader Model 2.0.", "Warning !");
            }
            // Check Full Screen MultiSampling Support
            int quality;

            if (GraphicsAdapter.DefaultAdapter.CheckDeviceMultiSampleType(DeviceType.Hardware, SurfaceFormat.Color, false, MultiSampleType.NonMaskable, out quality))
            {
                pp.MultiSampleType = MultiSampleType.NonMaskable;
                if (quality < 2)
                {
                    pp.MultiSampleQuality = quality;
                }
                else
                {
                    pp.MultiSampleQuality = 2;
                }
            }

            pp.PresentationInterval   = PresentInterval.One;
            pp.BackBufferCount        = 1;
            pp.BackBufferWidth        = Math.Max(width, 1);
            pp.BackBufferHeight       = Math.Max(height, 1);
            pp.BackBufferFormat       = SurfaceFormat.Color;
            pp.EnableAutoDepthStencil = true;
            pp.AutoDepthStencilFormat = DepthFormat.Depth24;

            try
            {
                gd = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Hardware, windowHandle, pp);

                if (DeviceCreated != null)
                {
                    DeviceCreated(this, EventArgs.Empty);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "GraphicsDevice Initialization Failed !");
            }
        }
Пример #7
0
        /// <summary>
        /// Linearize Gamma According to Current Gamma Value Using GraphicsDevice GammaRamp
        /// </summary>
        /// <param name="gamma">current R, G, B gamma value</param>
        public void SetGamma(Vector3 gamma)
        {
            bool isfullscreengamma;
            GraphicsDeviceCapabilities gdcap = gd.GraphicsDeviceCapabilities;

            if (gdcap.DriverCapabilities.SupportsFullScreenGamma)
            {
                isfullscreengamma = true;
            }
            else
            {
                isfullscreengamma = false;
                SLConstant.ShowMessage("This GraphicsDevice Does Not Support Full Screen Gamma Correction !");
            }
            if (isfullscreengamma && gd.PresentationParameters.IsFullScreen && (gamma.X != 1.0f || gamma.Y != 1.0f || gamma.Z != 1.0f))
            {
                gd.SetGammaRamp(false, SLAlgorithm.GetGamma(gamma));
            }
        }
Пример #8
0
        /// <summary>
        /// Linearize Gamma According to Current Gamma Value Using GraphicsDevice GammaRamp
        /// </summary>
        /// <param name="gamma">current R, G, B gamma value</param>
        public void SetGamma(Vector3 gamma)
        {
            bool isfullscreengamma;
            GraphicsDeviceCapabilities gdcap = GraphicsAdapter.DefaultAdapter.GetCapabilities(DeviceType.Hardware);

            if (gdcap.DriverCapabilities.SupportsFullScreenGamma)
            {
                isfullscreengamma = true;
            }
            else
            {
                isfullscreengamma = false;
                SLConstant.ShowMessage("This Adapter Does Not Support Full Screen Gamma Correction !");
            }
            if (isfullscreengamma && gdm.IsFullScreen && (gamma.X != 1.0f || gamma.Y != 1.0f || gamma.Z != 1.0f))
            {
                gdm.GraphicsDevice.SetGammaRamp(false, SLAlgorithm.GetGamma(gamma));
                this.gamma = gamma;
            }
        }
Пример #9
0
        //* -----------------------------------------------------------------------*
        /// <summary>グラフィック アダプタの性能レポートを作成します。</summary>
        ///
        /// <param name="caps">グラフィック アダプタ性能オブジェクト</param>
        /// <returns>グラフィック アダプタの性能レポート 文字列</returns>
        private string ToString(GraphicsDeviceCapabilities caps)
        {
            isAvaliablePS11 = (caps.MaxPixelShaderProfile >= ShaderProfile.PS_1_1);
            string strResult = "▽ グラフィック デバイス基本能力一覧" + Environment.NewLine;

            strResult += "  ヘッド参照順序           : " + caps.AdapterOrdinalInGroup + Environment.NewLine;
            strResult += "  デバイス 種別            : " + caps.DeviceType.ToString() + Environment.NewLine;
            strResult += "  ExtentsAdjust            : " + caps.ExtentsAdjust.ToString("F02") + Environment.NewLine;
            strResult += "  ガードバンド 下端座標    : " + caps.GuardBandBottom.ToString("F02") + Environment.NewLine;
            strResult += "  ガードバンド 左端座標    : " + caps.GuardBandLeft.ToString("F02") + Environment.NewLine;
            strResult += "  ガードバンド 右端座標    : " + caps.GuardBandRight.ToString("F02") + Environment.NewLine;
            strResult += "  ガードバンド 上端座標    : " + caps.GuardBandTop.ToString("F02") + Environment.NewLine;
            strResult += "  マスタ デバイス識別ID    : " + caps.MasterAdapterOrdinal + Environment.NewLine;
            strResult += "  異方性フィルタ最大有効値 : " + caps.MaxAnisotropy + Environment.NewLine;
            strResult += "  PS30命令スロット最大数   : " + caps.MaxPixelShader30InstructionSlots + Environment.NewLine;
            strResult += "  対応最大PSバージョン     : " + caps.MaxPixelShaderProfile.ToString() + Environment.NewLine;
            strResult += "  点プリミティブ最大サイズ : " + caps.MaxPointSize.ToString("F02") + Environment.NewLine;
            strResult += "  プリミティブ最大描画数   : " + caps.MaxPrimitiveCount + Environment.NewLine;
            strResult += "  レンダリング対象最大数   : " + caps.MaxSimultaneousRenderTargets + Environment.NewLine;
            strResult += "  MaxSimultaneousTextures  : " + caps.MaxSimultaneousTextures + Environment.NewLine;
            strResult += "  データ ストリーム最大数  : " + caps.MaxStreams + Environment.NewLine;
            strResult += "  最大ストリームストライド : " + caps.MaxStreamStride + Environment.NewLine;
            strResult += "  Textureの最大Aspect比率  : " + caps.MaxTextureAspectRatio + Environment.NewLine;
            strResult += "  MaxTextureRepeat         : " + caps.MaxTextureRepeat + Environment.NewLine;
            strResult += "  対応テクスチャ最大縦幅   : " + caps.MaxTextureHeight + Environment.NewLine;
            strResult += "  対応テクスチャ最大横幅   : " + caps.MaxTextureWidth + Environment.NewLine;
            strResult += "  最大定義可能クリップ面数 : " + caps.MaxUserClipPlanes + Environment.NewLine;
            strResult += "  HWインデックス最大サイズ : " + caps.MaxVertexIndex + Environment.NewLine;
            strResult += "  VS30命令スロット最大数   : " + caps.MaxVertexShader30InstructionSlots + Environment.NewLine;
            strResult += "  定数用VSレジスタ数       : " + caps.MaxVertexShaderConstants + Environment.NewLine;
            strResult += "  対応最大VSバージョン     : " + caps.MaxVertexShaderProfile.ToString() + Environment.NewLine;
            strResult += "  対応最大Wベース深度値    : " + caps.MaxVertexW.ToString("F02") + Environment.NewLine;
            strResult += "  VolumeTexture最大サイズ  : " + caps.MaxVolumeExtent + Environment.NewLine;
            strResult += "  このグループのアダプタ数 : " + caps.NumberOfAdaptersInGroup + Environment.NewLine;
            strResult += "  PS算術Component最大値    : " + caps.PixelShader1xMaxValue.ToString("F02") + Environment.NewLine;
            strResult += "  Pixelシェーダ バージョン : " + caps.PixelShaderVersion.ToString() + Environment.NewLine;
            strResult += "  画面スワップ間隔         : " + caps.PresentInterval.ToString() + Environment.NewLine;
            strResult += "  頂点シェーダ バージョン  : " + caps.VertexShaderVersion.ToString() + Environment.NewLine;
            strResult += ToString(caps.DeviceCapabilities);
            strResult += ToString(caps.DriverCapabilities);
            strResult += ToString(caps.CursorCapabilities);
            strResult += ToString(caps.DeclarationTypeCapabilities);
            strResult += ToString(caps.LineCapabilities);
            strResult += ToString(caps.PrimitiveCapabilities);
            strResult += ToString(caps.TextureCapabilities);
            strResult += ToString(caps.VertexFormatCapabilities);
            strResult += ToString(caps.VertexProcessingCapabilities);
            strResult += ToString(caps.VertexShaderCapabilities);
            strResult += ToString(caps.PixelShaderCapabilities);
            strResult += ToString(caps.RasterCapabilities);
            strResult += ToString(caps.ShadingCapabilities);
            strResult += ToString(caps.StencilCapabilities);

            strResult += ToString("アルファテスト", caps.AlphaCompareCapabilities);
            strResult += ToString("深度バッファ", caps.DepthBufferCompareCapabilities);

            strResult += ToString("キューブ テクスチャ", caps.CubeTextureFilterCapabilities);
            strResult += ToString("テクスチャ", caps.TextureFilterCapabilities);
            strResult += ToString("頂点シェーダ", caps.VertexTextureFilterCapabilities);
            strResult += ToString("ボリューム テクスチャ", caps.VolumeTextureFilterCapabilities);

            strResult += ToString("テクスチャ", caps.TextureAddressCapabilities);
            strResult += ToString("ボリューム テクスチャ", caps.VolumeTextureAddressCapabilities);

            strResult += ToString("転送先", caps.DestinationBlendCapabilities);
            strResult += ToString("転送元", caps.SourceBlendCapabilities);

            return(strResult);
        }
Пример #10
0
        private void ValidateGraphicsDeviceInformation(GraphicsDeviceInformation devInfo)
        {
            SurfaceFormat          format;
            GraphicsAdapter        adapter                = devInfo.Adapter;
            DeviceType             deviceType             = devInfo.DeviceType;
            bool                   enableAutoDepthStencil = devInfo.PresentationParameters.EnableAutoDepthStencil;
            DepthFormat            autoDepthStencilFormat = devInfo.PresentationParameters.AutoDepthStencilFormat;
            SurfaceFormat          backBufferFormat       = devInfo.PresentationParameters.BackBufferFormat;
            int                    backBufferWidth        = devInfo.PresentationParameters.BackBufferWidth;
            int                    backBufferHeight       = devInfo.PresentationParameters.BackBufferHeight;
            PresentationParameters presentationParameters = devInfo.PresentationParameters;
            SurfaceFormat          format4                = presentationParameters.BackBufferFormat;

            if (!presentationParameters.IsFullScreen)
            {
                format = adapter.CurrentDisplayMode.Format;
                if (SurfaceFormat.Unknown == presentationParameters.BackBufferFormat)
                {
                    format4 = format;
                }
            }
            else
            {
                SurfaceFormat format5 = presentationParameters.BackBufferFormat;
                if (format5 != SurfaceFormat.Color)
                {
                    if (format5 != SurfaceFormat.Bgra5551)
                    {
                        format = presentationParameters.BackBufferFormat;
                    }
                    else
                    {
                        format = SurfaceFormat.Bgr555;
                    }
                }
                else
                {
                    format = SurfaceFormat.Bgr32;
                }
            }
            if (-1 == Array.IndexOf <SurfaceFormat>(ValidBackBufferFormats, format4))
            {
                throw new ArgumentException(Resources.ValidateBackBufferFormatIsInvalid);
            }
            if (
                !adapter.CheckDeviceType(deviceType, format, presentationParameters.BackBufferFormat,
                                         presentationParameters.IsFullScreen))
            {
                throw new ArgumentException(Resources.ValidateDeviceType);
            }
            if ((presentationParameters.BackBufferCount < 0) || (presentationParameters.BackBufferCount > 3))
            {
                throw new ArgumentException(Resources.ValidateBackBufferCount);
            }
            if ((presentationParameters.BackBufferCount > 1) && (presentationParameters.SwapEffect == SwapEffect.Copy))
            {
                throw new ArgumentException(Resources.ValidateBackBufferCountSwapCopy);
            }
            switch (presentationParameters.SwapEffect)
            {
            case SwapEffect.Default:
            case SwapEffect.Flip:
            case SwapEffect.Copy:
            {
                int num3;
                if (
                    !adapter.CheckDeviceMultiSampleType(deviceType, format4, presentationParameters.IsFullScreen,
                                                        presentationParameters.MultiSampleType, out num3))
                {
                    throw new ArgumentException(Resources.ValidateMultiSampleTypeInvalid);
                }
                if (presentationParameters.MultiSampleQuality >= num3)
                {
                    throw new ArgumentException(Resources.ValidateMultiSampleQualityInvalid);
                }
                if ((presentationParameters.MultiSampleType != MultiSampleType.None) &&
                    (presentationParameters.SwapEffect != SwapEffect.Default))
                {
                    throw new ArgumentException(Resources.ValidateMultiSampleSwapEffect);
                }
                if (((presentationParameters.PresentOptions & PresentOptions.DiscardDepthStencil) !=
                     PresentOptions.None) && !presentationParameters.EnableAutoDepthStencil)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilMismatch);
                }
                if (presentationParameters.EnableAutoDepthStencil)
                {
                    if (
                        !adapter.CheckDeviceFormat(deviceType, format, ResourceUsage.None, QueryUsages.None,
                                                   ResourceType.DepthStencilBuffer,
                                                   presentationParameters.AutoDepthStencilFormat))
                    {
                        throw new ArgumentException(Resources.ValidateAutoDepthStencilFormatInvalid);
                    }
                    if (
                        !adapter.CheckDepthStencilMatch(deviceType, format, format4,
                                                        presentationParameters.AutoDepthStencilFormat))
                    {
                        throw new ArgumentException(Resources.ValidateAutoDepthStencilFormatIncompatible);
                    }
                }
                if (!presentationParameters.IsFullScreen)
                {
                    if (presentationParameters.FullScreenRefreshRateInHz != 0)
                    {
                        throw new ArgumentException(Resources.ValidateRefreshRateInWindow);
                    }
                    switch (presentationParameters.PresentationInterval)
                    {
                    case PresentInterval.Default:
                    case PresentInterval.One:
                    case PresentInterval.Immediate:
                        return;
                    }
                    throw new ArgumentException(Resources.ValidatePresentationIntervalInWindow);
                }
                if (presentationParameters.FullScreenRefreshRateInHz == 0)
                {
                    throw new ArgumentException(Resources.ValidateRefreshRateInFullScreen);
                }
                GraphicsDeviceCapabilities capabilities = adapter.GetCapabilities(deviceType);
                switch (presentationParameters.PresentationInterval)
                {
                case PresentInterval.Default:
                case PresentInterval.One:
                case PresentInterval.Immediate:
                    goto Label_02E5;

                case PresentInterval.Two:
                case PresentInterval.Three:
                case PresentInterval.Four:
                    if ((capabilities.PresentInterval & presentationParameters.PresentationInterval) ==
                        PresentInterval.Default)
                    {
                        throw new ArgumentException(
                                  Resources.ValidatePresentationIntervalIncompatibleInFullScreen);
                    }
                    goto Label_02E5;
                }
                break;
            }

            default:
                throw new ArgumentException(Resources.ValidateSwapEffectInvalid);
            }
            throw new ArgumentException(Resources.ValidatePresentationIntervalInFullScreen);
Label_02E5:
            if (presentationParameters.IsFullScreen)
            {
                if ((presentationParameters.BackBufferWidth == 0) || (presentationParameters.BackBufferHeight == 0))
                {
                    throw new ArgumentException(Resources.ValidateBackBufferDimsFullScreen);
                }
                bool        flag2 = true;
                bool        flag3 = false;
                DisplayMode currentDisplayMode = adapter.CurrentDisplayMode;
                if (((currentDisplayMode.Format != format) &&
                     (currentDisplayMode.Width != presentationParameters.BackBufferHeight)) &&
                    ((currentDisplayMode.Height != presentationParameters.BackBufferHeight) &&
                     (currentDisplayMode.RefreshRate != presentationParameters.FullScreenRefreshRateInHz)))
                {
                    flag2 = false;
                    foreach (DisplayMode mode2 in adapter.SupportedDisplayModes[format])
                    {
                        if ((mode2.Width == presentationParameters.BackBufferWidth) &&
                            (mode2.Height == presentationParameters.BackBufferHeight))
                        {
                            flag3 = true;
                            if (mode2.RefreshRate == presentationParameters.FullScreenRefreshRateInHz)
                            {
                                flag2 = true;
                                break;
                            }
                        }
                    }
                }
                if (!flag2 && flag3)
                {
                    throw new ArgumentException(Resources.ValidateBackBufferDimsModeFullScreen);
                }
                if (!flag2)
                {
                    throw new ArgumentException(Resources.ValidateBackBufferHzModeFullScreen);
                }
            }
            if (presentationParameters.EnableAutoDepthStencil != enableAutoDepthStencil)
            {
                throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
            }
            if (presentationParameters.EnableAutoDepthStencil)
            {
                if (presentationParameters.AutoDepthStencilFormat != autoDepthStencilFormat)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
                }
                if (presentationParameters.BackBufferFormat != backBufferFormat)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
                }
                if (presentationParameters.BackBufferWidth != backBufferWidth)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
                }
                if (presentationParameters.BackBufferHeight != backBufferHeight)
                {
                    throw new ArgumentException(Resources.ValidateAutoDepthStencilAdapterGroup);
                }
            }
        }
Пример #11
0
        private void AddDevices(bool anySuitableDevice, List <GraphicsDeviceInformation> foundDevices)
        {
            IntPtr handle = game.Window.Handle;

            foreach (GraphicsAdapter adapter in GraphicsAdapter.Adapters)
            {
                if (!anySuitableDevice && !IsWindowOnAdapter(handle, adapter))
                {
                    continue;
                }
                foreach (DeviceType type in ValidDeviceTypes)
                {
                    try
                    {
                        GraphicsDeviceCapabilities capabilities = adapter.GetCapabilities(type);
                        if ((capabilities.DeviceCapabilities.IsDirect3D9Driver &&
                             (capabilities.MaxPixelShaderProfile >= MinimumPixelShaderProfile)) &&
                            (capabilities.MaxVertexShaderProfile >= MinimumVertexShaderProfile))
                        {
                            CreateOptions none = CreateOptions.None;
                            if (capabilities.DeviceCapabilities.SupportsHardwareTransformAndLight)
                            {
                                none |= CreateOptions.HardwareVertexProcessing;
                            }
                            else
                            {
                                none |= CreateOptions.SoftwareVertexProcessing;
                            }
                            GraphicsDeviceInformation baseDeviceInfo = new GraphicsDeviceInformation();
                            baseDeviceInfo.Adapter         = adapter;
                            baseDeviceInfo.DeviceType      = type;
                            baseDeviceInfo.CreationOptions = none;
                            baseDeviceInfo.PresentationParameters.DeviceWindowHandle        = IntPtr.Zero;
                            baseDeviceInfo.PresentationParameters.EnableAutoDepthStencil    = true;
                            baseDeviceInfo.PresentationParameters.BackBufferCount           = 1;
                            baseDeviceInfo.PresentationParameters.PresentOptions            = PresentOptions.None;
                            baseDeviceInfo.PresentationParameters.SwapEffect                = SwapEffect.Default;
                            baseDeviceInfo.PresentationParameters.FullScreenRefreshRateInHz = 0;
                            baseDeviceInfo.PresentationParameters.MultiSampleQuality        = 0;
                            baseDeviceInfo.PresentationParameters.MultiSampleType           = MultiSampleType.None;
                            baseDeviceInfo.PresentationParameters.IsFullScreen              = IsFullScreen;
                            baseDeviceInfo.PresentationParameters.PresentationInterval      = SynchronizeWithVerticalRetrace
                                                                                             ? PresentInterval.One
                                                                                             : PresentInterval.Immediate;
                            for (int i = 0; i < ValidAdapterFormats.Length; i++)
                            {
                                AddDevices(adapter, type, adapter.CurrentDisplayMode, baseDeviceInfo, foundDevices);
                                if (isFullScreen)
                                {
                                    foreach (DisplayMode mode in adapter.SupportedDisplayModes[ValidAdapterFormats[i]])
                                    {
                                        if ((mode.Width >= 640) && (mode.Height >= 480))
                                        {
                                            AddDevices(adapter, type, mode, baseDeviceInfo, foundDevices);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (DeviceNotSupportedException)
                    {
                    }
                }
            }
        }
Пример #12
0
        /// <summary>
        /// Init Primitive Collection
        /// </summary>
        /// <param name="count"></param>
        /// <param name="gd"></param>
        /// <param name="primitive"></param>
        /// <param name="service"></param>
        /// <param name="path"></param>
        public void Init(int count, GraphicsDevice gd, Primitive primitive, IServiceProvider service, string path)
        {
            // Check Shader Model 3.0 Support
            GraphicsDeviceCapabilities gdcap = gd.GraphicsDeviceCapabilities;

            if (gdcap.MaxPixelShaderProfile < ShaderProfile.PS_3_0 || gdcap.MaxVertexShaderProfile < ShaderProfile.VS_3_0)
            {
                isshadermodel3 = false;
                MessageBox.Show("This GraphicsDevice Does Not Support Shader Model 3.0.", "Warning !");
            }
            else
            {
                isshadermodel3 = true;
            }

            if (count <= 0)
            {
                MessageBox.Show("Collection Number <= 0, Automatically Set To One !");
                count = 1;
            }
            if (PrimitiveInstance == null)
            {
                PrimitiveInstance = primitive.Clone() as Primitive;
            }
            InstancePara   = new vsBasePara[count];
            InstanceMatrix = new Matrix[count];
            for (int i = 0; i < count; i++)
            {
                InstancePara[i] = PrimitiveInstance.BasePara;
            }

            // Randomize Center and Speed of Primitive Instances
            var center = Random.RandomPosition(InstancePara.Length);
            var speed  = Random.RandomPosition(InstancePara.Length);

            for (int i = 0; i < InstancePara.Length; i++)
            {
                center[i].X             = center[i].X * (PrimitiveInstance.BasePara.space / 2) * (float)Math.Sin(Math.PI / 4);
                center[i].Y             = center[i].Y * (PrimitiveInstance.BasePara.space / 2) * (float)Math.Sin(Math.PI / 4);
                center[i].Z             = 0;
                speed[i].X              = speed[i].X * 0.5f;
                speed[i].Y              = speed[i].Y * 0.5f;
                speed[i].Z              = 0;
                InstancePara[i].center  = center[i] + Para.CollectionCenter;
                InstancePara[i].speed3D = speed[i] + Para.CollectionSpeed;
                InstanceMatrix[i]       = VisionStimulus.GetOri3DMatrix(InstancePara[i].orientation3D) * Matrix.CreateTranslation(InstancePara[i].center);
            }

            // Load Hardware Instancing Shader
            PrimitiveInstance.contentManager = new ContentManager(service, path);
            try
            {
                hiEffect = PrimitiveInstance.contentManager.Load <Effect>("HardwareInstancing");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error !");
            }
            hiEffect.Parameters["View"].SetValue(PrimitiveInstance.ViewMatrix);
            hiEffect.Parameters["Projection"].SetValue(PrimitiveInstance.ProjectionMatrix);

            PrimitiveInstance.vertexDeclaration = InstancingVDec(gd);

            ptype  = PrimitiveInstance.BasePara.primitivetype;
            vexn   = PrimitiveInstance.VertexArray.Length;
            pcount = VisionStimulus.CheckPrimitiveCount(ptype, PrimitiveInstance.IndexArray.Length, PrimitiveInstance.IndexArray.Length);

            InstanceDataStream = new DynamicVertexBuffer(gd, SLConstant.SizeOfMatrix * InstanceMatrix.Length, BufferUsage.WriteOnly);
        }
Пример #13
0
        /// <summary>
        /// Set Default Hardware Adapter with Custom Settings
        /// </summary>
        /// <param name="buffercount">0-3</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="refreshrate">windowed mode(0), fullscreen mode(>0)</param>
        /// <param name="updaterate">Variable-Step Game Loop(0), Fixed-Step Game Loop(>0)</param>
        /// <param name="isvsync"></param>
        public void SetGraphicsDevice(int buffercount, int width, int height, int refreshrate, int updaterate, bool isvsync)
        {
            GraphicsDeviceCapabilities gdcap = GraphicsAdapter.DefaultAdapter.GetCapabilities(DeviceType.Hardware);

            if (gdcap.MaxPixelShaderProfile < ShaderProfile.PS_2_0 || gdcap.MaxVertexShaderProfile < ShaderProfile.VS_2_0)
            {
                MessageBox.Show("This Adapter Does Not Support Shader Model 2.0.", "Warning !");
            }

            int quality;

            if (GraphicsAdapter.DefaultAdapter.CheckDeviceMultiSampleType(DeviceType.Hardware, SurfaceFormat.Color, false, MultiSampleType.NonMaskable, out quality))
            {
                pp.MultiSampleType = MultiSampleType.NonMaskable;
                if (quality < 2)
                {
                    pp.MultiSampleQuality = quality;
                }
                else
                {
                    pp.MultiSampleQuality = 2;
                }
            }

            if (refreshrate > 0)
            {
                pp.IsFullScreen = true;
            }
            else
            {
                pp.IsFullScreen = false;
            }
            pp.FullScreenRefreshRateInHz = refreshrate;
            gdm.IsFullScreen             = pp.IsFullScreen;

            if (updaterate > 0)
            {
                this.IsFixedTimeStep   = true;
                this.TargetElapsedTime = TimeSpan.FromSeconds(1.0 / updaterate);
            }
            else
            {
                this.IsFixedTimeStep = false;
            }

            if (isvsync)
            {
                pp.PresentationInterval = PresentInterval.One;
            }
            else
            {
                pp.PresentationInterval = PresentInterval.Immediate;
            }
            gdm.SynchronizeWithVerticalRetrace = isvsync;

            pp.BackBufferCount        = buffercount;
            pp.BackBufferHeight       = Math.Max(1, height);
            pp.BackBufferWidth        = Math.Max(1, width);
            pp.BackBufferFormat       = SurfaceFormat.Color;
            pp.EnableAutoDepthStencil = true;
            pp.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8;

            // Reset GraphicsDevice
            if (gdm.GraphicsDevice != null)
            {
                // Dirty GraphicsDeviceManager to reset using custom presentationparameters through PreparingDeviceSettings()
                gdm.PreferMultiSampling = !gdm.PreferMultiSampling;
                gdm.ApplyChanges();
            }
        }
Пример #14
0
        /// <summary>
        /// Set Default Hardware Adapter Targeting Current SLForm with Custom Settings
        /// </summary>
        /// <param name="buffercount">0-3</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="refreshrate">windowed mode(0), fullscreen mode(>0)</param>
        /// <param name="isvsync"></param>
        public void SetGraphicsDevice(int buffercount, int width, int height, int refreshrate, bool isvsync)
        {
            if (pp != null)
            {
                pp.Dispose();
            }
            pp = new PresentationParameters();

            // Check Shader Model 2.0 Support
            GraphicsDeviceCapabilities gdcap = GraphicsAdapter.DefaultAdapter.GetCapabilities(DeviceType.Hardware);

            if (gdcap.MaxPixelShaderProfile < ShaderProfile.PS_2_0 || gdcap.MaxVertexShaderProfile < ShaderProfile.VS_2_0)
            {
                MessageBox.Show("This Adapter Does Not Support Shader Model 2.0.", "Warning !");
            }

            // Check Full Screen MultiSampling Support
            int quality;

            if (GraphicsAdapter.DefaultAdapter.CheckDeviceMultiSampleType(DeviceType.Hardware, SurfaceFormat.Color, false, MultiSampleType.NonMaskable, out quality))
            {
                pp.MultiSampleType = MultiSampleType.NonMaskable;
                if (quality < 2)
                {
                    pp.MultiSampleQuality = quality;
                }
                else
                {
                    pp.MultiSampleQuality = 2;
                }
            }

            // Set Screen Presentation
            if (isvsync)
            {
                pp.PresentationInterval = PresentInterval.One;
            }
            else
            {
                pp.PresentationInterval = PresentInterval.Immediate;
            }
            if (refreshrate == 0)
            {
                pp.IsFullScreen = false;
                ClientSize      = new System.Drawing.Size(width, height);
            }
            else
            {
                pp.IsFullScreen = true;
            }
            pp.FullScreenRefreshRateInHz = refreshrate;

            // Set Buffer
            if (buffercount > 3)
            {
                buffercount = 3;
            }
            pp.BackBufferCount        = buffercount;
            pp.BackBufferWidth        = Math.Max(1, width);
            pp.BackBufferHeight       = Math.Max(1, height);
            pp.BackBufferFormat       = SurfaceFormat.Color;
            pp.EnableAutoDepthStencil = true;
            pp.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8;

            SetGraphicsDevice(pp);
        }