コード例 #1
0
        internal static void CheckAdapterChange(MyRenderDeviceSettings settings)
        {
            bool differentAdapter = m_adapterInfoList[m_settings.AdapterOrdinal].AdapterDeviceId != m_adapterInfoList[settings.AdapterOrdinal].AdapterDeviceId;

            if (differentAdapter)
            {
                m_settings = settings;
                HandleDeviceReset();
            }
        }
コード例 #2
0
 internal static void LogSettings(ref MyRenderDeviceSettings settings)
 {
     Log.WriteLine("MyRenderDeviceSettings = {");
     Log.IncreaseIndent();
     Log.WriteLine("Adapter id = " + settings.AdapterOrdinal);
     Log.WriteLine("DXGIAdapter id = " + GetAdaptersList()[settings.AdapterOrdinal].AdapterDeviceId);
     Log.WriteLine("DXGIOutput id = " + GetAdaptersList()[settings.AdapterOrdinal].OutputId);
     Log.WriteLine(String.Format("Resolution = {0} x {1}", settings.BackBufferWidth, settings.BackBufferHeight));
     Log.WriteLine("Window mode = " + settings.WindowMode);
     Log.DecreaseIndent();
     Log.WriteLine("}");
 }
コード例 #3
0
        private static PresentParameters CreatePresentParameters(MyRenderDeviceSettings settings, IntPtr windowHandle)
        {
            PresentParameters p = new PresentParameters();

            p.InitDefaults();

            switch (settings.WindowMode)
            {
            case MyWindowModeEnum.Fullscreen:
                p.FullScreenRefreshRateInHz = settings.RefreshRate;
                p.BackBufferHeight          = settings.BackBufferHeight;
                p.BackBufferWidth           = settings.BackBufferWidth;
                p.Windowed = false;
                break;

            case MyWindowModeEnum.FullscreenWindow:
            {
                WinApi.DEVMODE mode = new WinApi.DEVMODE();
                WinApi.EnumDisplaySettings(null, WinApi.ENUM_REGISTRY_SETTINGS, ref mode);
                p.FullScreenRefreshRateInHz = 0;
                p.BackBufferHeight          = mode.dmPelsHeight;
                p.BackBufferWidth           = mode.dmPelsWidth;
                p.Windowed = true;
            }
            break;

            case MyWindowModeEnum.Window:
                p.FullScreenRefreshRateInHz = 0;
                p.BackBufferHeight          = settings.BackBufferHeight;
                p.BackBufferWidth           = settings.BackBufferWidth;
                p.Windowed = true;
                break;
            }
            p.DeviceWindowHandle = windowHandle;

            p.AutoDepthStencilFormat = Format.D24S8;
            p.EnableAutoDepthStencil = true;
            p.BackBufferFormat       = Format.X8R8G8B8;
            p.MultiSampleQuality     = 0;
            p.PresentationInterval   = settings.VSync ? PresentInterval.One : PresentInterval.Immediate;
            p.SwapEffect             = SwapEffect.Discard;

            // PresentFlags.Video may cause crash when driver settings has overridden multisampling
            // We don't need it, it's just hint for driver
            p.PresentFlags = PresentFlags.DiscardDepthStencil;

            return(p);
        }
コード例 #4
0
        internal static void CheckAdapterChange(ref MyRenderDeviceSettings settings)
        {
            settings.AdapterOrdinal = ValidateAdapterIndex(settings.AdapterOrdinal);

            bool differentAdapter = m_adapterInfoList[m_settings.AdapterOrdinal].AdapterDeviceId != m_adapterInfoList[settings.AdapterOrdinal].AdapterDeviceId;

            if (differentAdapter)
            {
                if (m_settings.UseStereoRendering)
                {
                    settings.UseStereoRendering = true;
                }
                m_settings = settings;
                HandleDeviceReset();
            }
        }
コード例 #5
0
        public static void ApplySettings(MyRenderDeviceSettings settings)
        {
            bool canReset = m_settings.AdapterOrdinal == settings.AdapterOrdinal;

            m_settings   = settings;
            m_parameters = CreatePresentParameters(m_settings, m_windowHandle);
            SupportsHDR  = GetAdaptersList()[m_settings.AdapterOrdinal].HDRSupported;

            if (canReset)
            {
                Reset();
            }
            else
            {
                Recreate();
            }
        }
コード例 #6
0
        internal static void ApplySettings(MyRenderDeviceSettings settings)
        {
            MyRender11.Log.WriteLine("ApplySettings");
            MyRender11.Log.IncreaseIndent();

            //bool differentAdapter = m_adapterInfoList[m_settings.AdapterOrdinal].AdapterDeviceId != m_adapterInfoList[settings.AdapterOrdinal].AdapterDeviceId;

            //if (differentAdapter)
            //{
            //    m_settings = settings;
            //    HandleDeviceReset();
            //}
            //else
            //{
            LogSettings(ref settings);

            CommandsListsSupported = m_adapterInfoList[m_settings.AdapterOrdinal].MultithreadedRenderingSupported;

            bool deviceRemoved = false;

            try
            {
                ResizeSwapchain(settings.BackBufferWidth, settings.BackBufferHeight);
            }
            catch (SharpDXException e)
            {
                if (e.Descriptor == SharpDX.DXGI.ResultCode.DeviceRemoved && Device.DeviceRemovedReason == SharpDX.DXGI.ResultCode.DeviceRemoved)
                {
                    deviceRemoved = true;
                    MyRender11.Log.WriteLine("Device removed - resetting device");
                    HandleDeviceReset();
                    MyRender11.Log.WriteLine("Device removed - resetting completed");
                }
            }

            if (!deviceRemoved)
            {
                ModeDescription md = new ModeDescription();
                md.Format                  = MyRender11Constants.BACKBUFFER_FORMAT;
                md.Height                  = settings.BackBufferHeight;
                md.Width                   = settings.BackBufferWidth;
                md.Scaling                 = DisplayModeScaling.Unspecified;
                md.ScanlineOrdering        = DisplayModeScanlineOrder.Progressive;
                md.RefreshRate.Numerator   = settings.RefreshRate;
                md.RefreshRate.Denominator = 1000;

                var list = m_adapterModes[m_settings.AdapterOrdinal];
                if (list != null)
                {
                    for (int i = 0; i < list.Length; i++)
                    {
                        if (
                            list[i].Height == settings.BackBufferHeight &&
                            list[i].Width == settings.BackBufferWidth &&
                            list[i].RefreshRate.Numerator == settings.RefreshRate)
                        {
                            md.Scaling          = list[i].Scaling;
                            md.ScanlineOrdering = list[i].ScanlineOrdering;
                            md.RefreshRate      = list[i].RefreshRate;
                            break;
                        }
                    }
                }

                // to fullscreen
                if (settings.WindowMode == MyWindowModeEnum.Fullscreen)
                {
                    if (settings.WindowMode != m_settings.WindowMode)
                    {
                        m_changeToFullscreen = md;
                    }
                    else
                    {
                        m_swapchain.ResizeTarget(ref md);
                        md.RefreshRate.Denominator = 0;
                        md.RefreshRate.Numerator   = 0;
                        m_swapchain.ResizeTarget(ref md);
                    }
                }
                // from fullscreen
                else if (settings.WindowMode != m_settings.WindowMode && m_settings.WindowMode == MyWindowModeEnum.Fullscreen)
                {
                    m_swapchain.ResizeTarget(ref md);
                    m_swapchain.SetFullscreenState(false, null);
                }

                m_settings = settings;

                TryChangeToFullscreen();
            }

            MyRender11.Log.DecreaseIndent();
        }
コード例 #7
0
        internal static void ApplySettings(MyRenderDeviceSettings settings)
        {
            Log.WriteLine("ApplySettings");
            Log.IncreaseIndent();
            LogSettings(ref settings);

            CommandsListsSupported          = m_adapterInfoList[m_settings.AdapterOrdinal].MultithreadedRenderingSupported;
            IsIntelBrokenCubemapsWorkaround = m_adapterInfoList[m_settings.AdapterOrdinal].Priority == 1; // 1 is intel

            bool deviceRemoved = false;

            try
            {
                ResizeSwapchain(settings.BackBufferWidth, settings.BackBufferHeight);
            }
            catch (SharpDXException e)
            {
                if (e.Descriptor == SharpDX.DXGI.ResultCode.DeviceRemoved && Device.DeviceRemovedReason == SharpDX.DXGI.ResultCode.DeviceRemoved)
                {
                    deviceRemoved = true;
                    Log.WriteLine("Device removed - resetting device" + e.ToString());
                    HandleDeviceReset();
                    Log.WriteLine("Device removed - resetting completed");
                }
            }

            if (!deviceRemoved)
            {
                ModeDescription md = new ModeDescription();
                md.Format                  = MyRender11Constants.DX11_BACKBUFFER_FORMAT;
                md.Height                  = settings.BackBufferHeight;
                md.Width                   = settings.BackBufferWidth;
                md.Scaling                 = DisplayModeScaling.Unspecified;
                md.ScanlineOrdering        = DisplayModeScanlineOrder.Progressive;
                md.RefreshRate.Numerator   = 60000;
                md.RefreshRate.Denominator = 1000;

                FixModeDescriptionForFullscreen(ref md);

                // to fullscreen
                if (settings.WindowMode == MyWindowModeEnum.Fullscreen)
                {
                    if (settings.WindowMode != m_settings.WindowMode)
                    {
                        m_changeToFullscreen = md;
                    }
                    else
                    {
                        m_swapchain.ResizeTarget(ref md);
                        md.RefreshRate.Denominator = 0;
                        md.RefreshRate.Numerator   = 0;
                        m_swapchain.ResizeTarget(ref md);
                    }
                }
                // from fullscreen
                else if (settings.WindowMode != m_settings.WindowMode && m_settings.WindowMode == MyWindowModeEnum.Fullscreen)
                {
                    m_swapchain.ResizeTarget(ref md);
                    m_swapchain.SetFullscreenState(false, null);
                }

                m_settings = settings;

                TryChangeToFullscreen();
            }

            Log.DecreaseIndent();
        }
コード例 #8
0
        private static MyRenderDeviceSettings CreateDeviceInternal(IntPtr windowHandle, MyRenderDeviceSettings?settingsToTry)
        {
            if (Device != null)
            {
                Device.Dispose();
                Device = null;
            }
            WIC = null;

            if (settingsToTry != null)
            {
                Log.WriteLine("CreateDevice - original settings");
                Log.IncreaseIndent();
                var originalSettings = settingsToTry.Value;
                LogSettings(ref originalSettings);
            }

            FeatureLevel[]      featureLevels = { FeatureLevel.Level_11_0 };
            DeviceCreationFlags flags         = DeviceCreationFlags.None;

#if DEBUG
            if (VRage.MyCompilationSymbols.DX11Debug)
            {
                flags |= DeviceCreationFlags.Debug;
            }
#endif

#if !XB1
            WinApi.DEVMODE mode = new WinApi.DEVMODE();
            WinApi.EnumDisplaySettings(null, WinApi.ENUM_REGISTRY_SETTINGS, ref mode);

            var settings = settingsToTry ?? new MyRenderDeviceSettings()
            {
                AdapterOrdinal   = -1,
                BackBufferHeight = mode.dmPelsHeight,
                BackBufferWidth  = mode.dmPelsWidth,
                WindowMode       = MyWindowModeEnum.Fullscreen,
                RefreshRate      = 60000,
                VSync            = false,
            };
#else
            var settings = CreateXB1Settings();
#endif
            settings.AdapterOrdinal = ValidateAdapterIndex(settings.AdapterOrdinal);

            if (settings.AdapterOrdinal == -1)
            {
                throw new MyRenderException("No supported device detected!", MyRenderExceptionEnum.GpuNotSupported);
            }

            m_settings = settings;

            Log.WriteLine("CreateDevice settings");
            Log.IncreaseIndent();
            LogSettings(ref m_settings);

            // If this line crashes cmd this: Dism /online /add-capability /capabilityname:Tools.Graphics.DirectX~~~~0.0.1.0
            var adapters = GetAdaptersList();
            if (m_settings.AdapterOrdinal >= adapters.Length)
            {
                throw new MyRenderException("No supported device detected!", MyRenderExceptionEnum.GpuNotSupported);
            }
            var adapterId = adapters[m_settings.AdapterOrdinal].AdapterDeviceId;
            if (adapterId >= GetFactory().Adapters.Length)
            {
                throw new MyRenderException("Invalid adapter id binding!", MyRenderExceptionEnum.GpuNotSupported);
            }
            var adapter = GetFactory().Adapters[adapterId];
            TweakSettingsAdapterAdHoc(adapter);
            Device = new Device(adapter, flags, FeatureLevel.Level_11_0);
            WIC    = new ImagingFactory();

            // HACK: This is required for Steam overlay to work. Apparently they hook only CreateDevice methods with DriverType argument.
            try
            {
                using (new Device(DriverType.Hardware, flags, FeatureLevel.Level_11_0)){}
            }
            catch { }

            InitDebugOutput();

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

            RC = new MyRenderContext();
            RC.Initialize(Device.ImmediateContext);

            m_windowHandle = windowHandle;

            m_resolution = new Vector2I(m_settings.BackBufferWidth, m_settings.BackBufferHeight);

            if (!m_initializedOnce)
            {
                InitSubsystemsOnce();
                m_initializedOnce = true;
            }

            if (!m_initialized)
            {
                OnDeviceReset();
                InitSubsystems();
                m_initialized = true;
            }

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

            if (m_swapchain == null)
            {
                SharpDX.DXGI.Device d = Device.QueryInterface <SharpDX.DXGI.Device>();
                Adapter             a = d.GetParent <Adapter>();
                var factory           = a.GetParent <Factory>();

                var scDesc = new SwapChainDescription();
                scDesc.BufferCount            = MyRender11Constants.BUFFER_COUNT;
                scDesc.Flags                  = SwapChainFlags.AllowModeSwitch;
                scDesc.IsWindowed             = true;
                scDesc.ModeDescription.Format = MyRender11Constants.DX11_BACKBUFFER_FORMAT;
                scDesc.ModeDescription.Height = m_settings.BackBufferHeight;
                scDesc.ModeDescription.Width  = m_settings.BackBufferWidth;
                scDesc.ModeDescription.RefreshRate.Numerator   = m_settings.RefreshRate;
                scDesc.ModeDescription.RefreshRate.Denominator = 1000;
                scDesc.ModeDescription.Scaling          = DisplayModeScaling.Unspecified;
                scDesc.ModeDescription.ScanlineOrdering = DisplayModeScanlineOrder.Progressive;
                scDesc.SampleDescription.Count          = 1;
                scDesc.SampleDescription.Quality        = 0;
                scDesc.OutputHandle = m_windowHandle;
                scDesc.Usage        = Usage.RenderTargetOutput;
                scDesc.SwapEffect   = SwapEffect.Discard;

                m_swapchain = new SwapChain(factory, Device, scDesc);

                m_swapchain.GetParent <Factory>().MakeWindowAssociation(m_windowHandle, WindowAssociationFlags.IgnoreAll);
            }

            // we start with window always (DXGI recommended)
            m_settings.WindowMode = MyWindowModeEnum.Window;
            ApplySettings(settings);

            return(m_settings);
        }
コード例 #9
0
 internal static bool SettingsChanged(MyRenderDeviceSettings settings)
 {
     return(!m_settings.Equals(ref settings));
 }
コード例 #10
0
 public bool SettingsChanged(MyRenderDeviceSettings settings)
 {
     return(true);
 }
コード例 #11
0
        internal static MyRenderDeviceSettings CreateDevice(IntPtr windowHandle, MyRenderDeviceSettings?settingsToTry)
        {
            MyRenderExceptionEnum exceptionEnum;
            bool deviceCreated = CreateDeviceInternalSafe(windowHandle, settingsToTry, out exceptionEnum);

#if !XB1
            if (!settingsToTry.HasValue || !settingsToTry.Value.SettingsMandatory)
            {
                if (!deviceCreated)
                {
                    if (settingsToTry.HasValue && settingsToTry.Value.UseStereoRendering)
                    {
                        var newSettings = settingsToTry.Value;
                        newSettings.UseStereoRendering = false;
                        deviceCreated = CreateDeviceInternalSafe(windowHandle, newSettings, out exceptionEnum);
                    }
                }
                if (!deviceCreated)
                {
                    Log.WriteLine("Primary desktop size fallback.");
                    var adapters = GetAdaptersList();
                    int i        = 0;
                    int j        = 0;
                    for (; i < adapters.Length; ++i)
                    {
                        for (j = 0; j < adapters[i].SupportedDisplayModes.Length; ++j)
                        {
                            if (adapters[i].IsDx11Supported)
                            {
                                var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
                                if (adapters[i].SupportedDisplayModes[j].Width == bounds.Width &&
                                    adapters[i].SupportedDisplayModes[j].Height == bounds.Height)
                                {
                                    var displayMode = adapters[i].SupportedDisplayModes[j];
                                    var newSettings = new MyRenderDeviceSettings()
                                    {
                                        AdapterOrdinal   = i,
                                        BackBufferHeight = displayMode.Height,
                                        BackBufferWidth  = displayMode.Width,
                                        WindowMode       = MyWindowModeEnum.Fullscreen,
                                        RefreshRate      = displayMode.RefreshRate,
                                        VSync            = true
                                    };

                                    deviceCreated = CreateDeviceInternalSafe(windowHandle, newSettings, out exceptionEnum);
                                    break;
                                }
                            }
                        }
                    }
                }
                if (!deviceCreated)
                {
                    Log.WriteLine("Lowest res fallback.");
                    var simpleSettings = new MyRenderDeviceSettings()
                    {
                        AdapterOrdinal   = 0,
                        BackBufferHeight = 480,
                        BackBufferWidth  = 640,
                        WindowMode       = MyWindowModeEnum.Window,
                        VSync            = true,
                    };
                    deviceCreated = CreateDeviceInternalSafe(windowHandle, simpleSettings, out exceptionEnum);
                }
            }
#else
#if !XB1_SKIPASSERTFORNOW
            System.Diagnostics.Debug.Assert(false, "simpleSettings is initialized but not used?");
#endif // !XB1_SKIPASSERTFORNOW
            Log.WriteLine("XB1 res fallback.");
            var simpleSettings = CreateXB1Settings();
#endif


            if (!deviceCreated)
            {
                if (exceptionEnum == MyRenderExceptionEnum.GpuNotSupported)
                {
#if !XB1
                    VRage.Utils.MyMessageBox.Show("Unsupported graphics card",
                                                  "Graphics card is not supported, please see minimum requirements");
#else // XB1
                    System.Diagnostics.Debug.Assert(false, "Unsupported graphics card");
#endif // XB1
                    throw new MyRenderException("No supported device detected!", MyRenderExceptionEnum.GpuNotSupported);
                }
                else
                {
#if !XB1
                    VRage.Utils.MyMessageBox.Show("Unspecified graphics error",
                                                  "Graphics error occurred, please follow troubleshooting on the game webpages");
#else // XB1
                    System.Diagnostics.Debug.Assert(false, "Unspecified graphics error");
#endif // XB1
                    throw new MyRenderException("Unspecified error in renderer!", MyRenderExceptionEnum.Unassigned);
                }
            }
            return(m_settings);
        }
コード例 #12
0
        private static MyRenderDeviceSettings CreateDeviceInternal(IntPtr windowHandle, MyRenderDeviceSettings?settingsToTry, bool forceDebugDevice)
        {
            if (Device != null)
            {
                Device.Dispose();
                Device = null;
            }
            WIC = null;

            if (settingsToTry != null)
            {
                Log.WriteLine("CreateDevice - original settings");
                Log.IncreaseIndent();
                var originalSettings = settingsToTry.Value;
                LogSettings(ref originalSettings);
            }

            FeatureLevel[]      featureLevels = { FeatureLevel.Level_11_0 };
            DeviceCreationFlags flags         = DeviceCreationFlags.None;

            bool isEnabledDebugOutput = forceDebugDevice | MyCompilationSymbols.DX11Debug;

            if (isEnabledDebugOutput)
            {
                flags |= DeviceCreationFlags.Debug;
            }
#if !XB1
            var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            //var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            var settings = settingsToTry ?? new MyRenderDeviceSettings()
            {
                AdapterOrdinal   = -1,
                BackBufferHeight = bounds.Width,
                BackBufferWidth  = bounds.Height,
                WindowMode       = MyWindowModeEnum.FullscreenWindow,
                RefreshRate      = 60000,
                VSync            = false,
            };
#else
            var settings = CreateXB1Settings();
#endif
            settings.AdapterOrdinal = ValidateAdapterIndex(settings.AdapterOrdinal);

            if (settings.AdapterOrdinal == -1)
            {
                throw new MyRenderException("No supported device detected!\nPlease apply windows updates and update to latest graphics drivers.", MyRenderExceptionEnum.GpuNotSupported);
            }

            m_settings = settings;

            Log.WriteLine("CreateDeviceInteral settings");

            // If this line crashes cmd this: Dism /online /add-capability /capabilityname:Tools.Graphics.DirectX~~~~0.0.1.0
            var factory = GetFactory();

            var adapters = GetAdaptersList();
            if (m_settings.AdapterOrdinal >= adapters.Length)
            {
                throw new MyRenderException("No supported device detected!\nPlease apply windows updates and update to latest graphics drivers.", MyRenderExceptionEnum.GpuNotSupported);
            }
            var adapterId = adapters[m_settings.AdapterOrdinal].AdapterDeviceId;
            if (adapterId >= factory.Adapters.Length)
            {
                throw new MyRenderException("Invalid adapter id binding!", MyRenderExceptionEnum.GpuNotSupported);
            }
            var adapter = factory.Adapters[adapterId];

            Log.WriteLine("CreateDeviceInteral TweakSettingsAdapterAdHoc");
            TweakSettingsAdapterAdHoc(adapter);

            if (m_settings.WindowMode == MyWindowModeEnum.Fullscreen && adapter.Outputs.Length == 0)
            {
                m_settings.WindowMode = MyWindowModeEnum.FullscreenWindow;
            }
            Log.IncreaseIndent();
            LogSettings(ref m_settings);

            Log.WriteLine("CreateDeviceInteral create device");
            if (MyCompilationSymbols.CreateRefenceDevice)
            {
                Device = new Device(DriverType.Reference, flags, FeatureLevel.Level_11_0);
            }
            else
            {
                Device = new Device(adapter, flags, FeatureLevel.Level_11_0);
            }

            Log.WriteLine("CreateDeviceInteral create ImagingFactory");
            WIC = new ImagingFactory();

            // HACK: This is required for Steam overlay to work. Apparently they hook only CreateDevice methods with DriverType argument.
            try
            {
                Log.WriteLine("CreateDeviceInteral Steam Overlay integration");
                using (new Device(DriverType.Hardware, flags, FeatureLevel.Level_11_0)) { }
                Log.WriteLine("CreateDeviceInteral Steam Overlay OK");
            }
            catch
            {
                Log.WriteLine("CreateDeviceInteral Steam Overlay Failed");
            }

            Log.WriteLine("CreateDeviceInteral InitDebugOutput");
            InitDebugOutput(isEnabledDebugOutput);

            Log.WriteLine("CreateDeviceInteral RC Dispose");
            if (RC != null)
            {
                RC.Dispose();
                RC = null;
            }

            Log.WriteLine("CreateDeviceInteral RC Create");
            RC = new MyRenderContext();
            Log.WriteLine("CreateDeviceInteral RC Initialize");
            RC.Initialize(Device.ImmediateContext);

            m_windowHandle = windowHandle;

            m_resolution = new Vector2I(m_settings.BackBufferWidth, m_settings.BackBufferHeight);

            Log.WriteLine("CreateDeviceInteral m_initializedOnce (" + m_initializedOnce + ")");
            if (!m_initializedOnce)
            {
                InitSubsystemsOnce();
                m_initializedOnce = true;
            }

            Log.WriteLine("CreateDeviceInteral m_initialized (" + m_initialized + ")");
            if (!m_initialized)
            {
                OnDeviceReset();
                InitSubsystems();
                m_initialized = true;
            }

            Log.WriteLine("CreateDeviceInteral m_swapchain (" + m_swapchain + ")");
            if (m_swapchain != null)
            {
                m_swapchain.Dispose();
                m_swapchain = null;
            }

            Log.WriteLine("CreateDeviceInteral create swapchain");
            if (m_swapchain == null)
            {
                //SharpDX.DXGI.Device d = Device.QueryInterface<SharpDX.DXGI.Device>();
                //Adapter a = d.GetParent<Adapter>();
                //var factory = a.GetParent<Factory>();

                var scDesc = new SwapChainDescription();
                scDesc.BufferCount            = MyRender11Constants.BUFFER_COUNT;
                scDesc.Flags                  = SwapChainFlags.AllowModeSwitch;
                scDesc.IsWindowed             = true;
                scDesc.ModeDescription.Format = MyRender11Constants.DX11_BACKBUFFER_FORMAT;
                scDesc.ModeDescription.Height = m_settings.BackBufferHeight;
                scDesc.ModeDescription.Width  = m_settings.BackBufferWidth;
                scDesc.ModeDescription.RefreshRate.Numerator   = m_settings.RefreshRate;
                scDesc.ModeDescription.RefreshRate.Denominator = 1000;
                scDesc.ModeDescription.Scaling          = DisplayModeScaling.Unspecified;
                scDesc.ModeDescription.ScanlineOrdering = DisplayModeScanlineOrder.Progressive;
                scDesc.SampleDescription.Count          = 1;
                scDesc.SampleDescription.Quality        = 0;
                scDesc.OutputHandle = m_windowHandle;
                scDesc.Usage        = Usage.RenderTargetOutput;
                scDesc.SwapEffect   = SwapEffect.Discard;

                try
                {
                    m_swapchain = new SwapChain(factory, Device, scDesc);
                }
                catch (Exception ex)
                {
                    Log.WriteLine("SwapChain factory = " + factory);
                    Log.WriteLine("SwapChain Device = " + Device);

                    Log.WriteLine("SwapChainDescription.BufferCount = " + scDesc.BufferCount);
                    Log.WriteLine("SwapChainDescription.Flags = " + scDesc.Flags);
                    Log.WriteLine("SwapChainDescription.ModeDescription.Format = " + scDesc.ModeDescription.Format);
                    Log.WriteLine("SwapChainDescription.ModeDescription.Height = " + scDesc.ModeDescription.Height);
                    Log.WriteLine("SwapChainDescription.ModeDescription.Width = " + scDesc.ModeDescription.Width);
                    Log.WriteLine("SwapChainDescription.ModeDescription.RefreshRate.Numerator = " + scDesc.ModeDescription.RefreshRate.Numerator);
                    Log.WriteLine("SwapChainDescription.ModeDescription.RefreshRate.Denominator = " + scDesc.ModeDescription.RefreshRate.Denominator);
                    Log.WriteLine("SwapChainDescription.ModeDescription.Scaling = " + scDesc.ModeDescription.Scaling);
                    Log.WriteLine("SwapChainDescription.ModeDescription.ScanlineOrdering = " + scDesc.ModeDescription.ScanlineOrdering);
                    Log.WriteLine("SwapChainDescription.SampleDescription.Count = " + scDesc.SampleDescription.Count);
                    Log.WriteLine("SwapChainDescription.SampleDescription.Quality = " + scDesc.SampleDescription.Quality);
                    Log.WriteLine("SwapChainDescription.BufferCount = " + scDesc.BufferCount);
                    Log.WriteLine("SwapChainDescription.Usage = " + scDesc.Usage);
                    Log.WriteLine("SwapChainDescription.SwapEffect = " + scDesc.SwapEffect);

                    throw ex;
                }

                factory.MakeWindowAssociation(m_windowHandle, WindowAssociationFlags.IgnoreAll);
            }

            // we start with window always (DXGI recommended)
            Log.WriteLine("CreateDeviceInteral Apply Settings");
            m_settings.WindowMode = MyWindowModeEnum.Window;
            ApplySettings(settings);

            Log.WriteLine("CreateDeviceInteral done (" + m_settings + ")");
            return(m_settings);
        }
コード例 #13
0
 public void ApplySettings(MyRenderDeviceSettings settings)
 {
     MyRender11.CheckAdapterChange(ref settings);
     MyRender11.ApplySettings(settings);
 }
コード例 #14
0
ファイル: MyDX9Render.cs プロジェクト: caomw/SpaceEngineers
 public bool SettingsChanged(MyRenderDeviceSettings settings)
 {
     return(MyRender.SettingsChanged(settings));
 }
コード例 #15
0
        internal static MyRenderDeviceSettings CreateDevice(IntPtr windowHandle, MyRenderDeviceSettings?settingsToTry)
        {
            if (Device != null)
            {
                Device.Dispose();
                Device = null;
            }

            FeatureLevel[]      featureLevels = { FeatureLevel.Level_11_0 };
            DeviceCreationFlags flags         = DeviceCreationFlags.None;

    #if DEBUG_DEVICE
            flags |= DeviceCreationFlags.Debug;
    #endif

            WinApi.DEVMODE mode = new WinApi.DEVMODE();
            WinApi.EnumDisplaySettings(null, WinApi.ENUM_REGISTRY_SETTINGS, ref mode);

            var adapters = GetAdaptersList();

            int adapterIndex = settingsToTry.HasValue ? settingsToTry.Value.AdapterOrdinal : -1;
            adapterIndex = ValidateAdapterIndex(adapterIndex);

            if (adapterIndex == -1)
            {
                throw new MyRenderException("No supporting device detected!", MyRenderExceptionEnum.GpuNotSupported);
            }

            var settings = settingsToTry ?? new MyRenderDeviceSettings()
            {
                AdapterOrdinal   = adapterIndex,
                BackBufferHeight = mode.dmPelsHeight,
                BackBufferWidth  = mode.dmPelsWidth,
                WindowMode       = MyWindowModeEnum.Fullscreen,
                RefreshRate      = 60000,
                VSync            = false,
            };
            m_settings = settings;

            Device = new Device(GetFactory().Adapters[adapters[m_settings.AdapterOrdinal].AdapterDeviceId], flags, FeatureLevel.Level_11_0);

            // HACK: This is required for Steam overlay to work. Apparently they hook only CreateDevice methods with DriverType argument.
            try
            {
                using (new Device(DriverType.Hardware, flags, FeatureLevel.Level_11_0)){}
            }
            catch { }

            if (flags.HasFlag(DeviceCreationFlags.Debug))
            {
                if (DebugDevice != null)
                {
                    DebugDevice.Dispose();
                    DebugDevice = null;
                }

                DebugDevice    = new DeviceDebug(Device);
                DebugInfoQueue = DebugDevice.QueryInterface <InfoQueue>();

                new System.Threading.Thread(ProcessDebugOutput).Start();
            }

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

            ImmediateContext = Device.ImmediateContext;

            m_windowHandle = windowHandle;

            m_resolution = new Vector2I(m_settings.BackBufferWidth, m_settings.BackBufferHeight);

            if (!m_initialized)
            {
                InitSubsystems();
                m_initialized = true;
            }


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

            if (m_swapchain == null)
            {
                SharpDX.DXGI.Device d = Device.QueryInterface <SharpDX.DXGI.Device>();
                Adapter             a = d.GetParent <Adapter>();
                var factory           = a.GetParent <Factory>();

                var scDesc = new SwapChainDescription();
                scDesc.BufferCount            = MyRender11Constants.BUFFER_COUNT;
                scDesc.Flags                  = SwapChainFlags.AllowModeSwitch;
                scDesc.IsWindowed             = true;
                scDesc.ModeDescription.Format = MyRender11Constants.BACKBUFFER_FORMAT;
                scDesc.ModeDescription.Height = m_settings.BackBufferHeight;
                scDesc.ModeDescription.Width  = m_settings.BackBufferWidth;
                scDesc.ModeDescription.RefreshRate.Numerator   = m_settings.RefreshRate;
                scDesc.ModeDescription.RefreshRate.Denominator = 1000;
                scDesc.ModeDescription.Scaling          = DisplayModeScaling.Unspecified;
                scDesc.ModeDescription.ScanlineOrdering = DisplayModeScanlineOrder.Progressive;
                scDesc.SampleDescription.Count          = 1;
                scDesc.SampleDescription.Quality        = 0;
                scDesc.OutputHandle = m_windowHandle;
                scDesc.Usage        = Usage.RenderTargetOutput;
                scDesc.SwapEffect   = SwapEffect.Discard;

                m_swapchain = new SwapChain(factory, Device, scDesc);

                m_swapchain.GetParent <Factory>().MakeWindowAssociation(m_windowHandle, WindowAssociationFlags.IgnoreAll);
            }

            // we start with window always (DXGI recommended)
            m_settings.WindowMode = MyWindowModeEnum.Window;
            ApplySettings(settings);

            return(m_settings);
        }
コード例 #16
0
        public static MyRenderDeviceSettings CreateDevice(IntPtr windowHandle, MyRenderDeviceSettings?settingsToTry)
        {
            Debug.Assert(Device == null, "Device was not properly released");

            // try first settingsToTry (if available)
            //  if that doesn't work, try again using desktop fullscreen settings
            //  if that doesn't work, use fallback settings (800x600 or 640x480 in window) and hope for the best

            m_windowHandle = windowHandle;

            var deviceType     = DeviceType.Hardware;
            int adapterOrdinal = 0;

            if (settingsToTry.HasValue)
            {
                adapterOrdinal = settingsToTry.Value.AdapterOrdinal;
            }
            EnablePerfHUD(m_d3d, ref adapterOrdinal, ref deviceType);

            bool deviceCreated = false;

            if (settingsToTry.HasValue)
            {
                try
                {
                    var settings           = settingsToTry.Value;
                    var originalWindowMode = settings.WindowMode;
                    settings.AdapterOrdinal = adapterOrdinal;
                    settings.WindowMode     = MyWindowModeEnum.Window;
                    TryCreateDeviceInternal(windowHandle, deviceType, settings, out m_device, out m_parameters);
                    Debug.Assert(m_device != null);
                    m_settings = settings;

                    bool modeExists = false;
                    foreach (var mode in m_adaptersList[settings.AdapterOrdinal].SupportedDisplayModes)
                    {
                        if (mode.Width == m_settings.BackBufferWidth && mode.Height == m_settings.BackBufferHeight && mode.RefreshRate == m_settings.RefreshRate)
                        {
                            modeExists = true;
                            break;
                        }
                    }

                    if (!modeExists)
                    {
                        var fallbackMode = m_adaptersList[settings.AdapterOrdinal].SupportedDisplayModes.Last(x => true);
                        m_settings.BackBufferHeight = fallbackMode.Height;
                        m_settings.BackBufferWidth  = fallbackMode.Width;
                        m_settings.RefreshRate      = fallbackMode.RefreshRate;
                    }

                    if (originalWindowMode != m_settings.WindowMode)
                    {
                        m_settings.WindowMode = originalWindowMode;
                        ApplySettings(m_settings);
                    }
                    deviceCreated = true;
                }
                catch
                {
                    /* These settings don't work so we'll try different. Dispose device in case it failed while switching to fullscreen. */
                    DisposeDevice();
                }
            }

            if (!deviceCreated)
            {
                // find the best match among supported display modes
                var adapters = GetAdaptersList();
                int i        = 0;
                int j        = 0;
                for (; i < adapters.Length; ++i)
                {
                    for (j = 0; j < adapters[i].SupportedDisplayModes.Length; ++j)
                    {
                        var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
                        if (adapters[i].SupportedDisplayModes[j].Width == bounds.Width &&
                            adapters[i].SupportedDisplayModes[j].Height == bounds.Height)
                        {
                            goto DISPLAY_MODE_FOUND_LABEL;
                        }
                    }
                }

DISPLAY_MODE_FOUND_LABEL:
                if (i != adapters.Length) // found appropriate display mode
                {
                    var displayMode     = adapters[i].SupportedDisplayModes[j];
                    var bestFitSettings = new MyRenderDeviceSettings()
                    {
                        AdapterOrdinal   = i,
                        BackBufferWidth  = displayMode.Width,
                        BackBufferHeight = displayMode.Height,
                        RefreshRate      = displayMode.RefreshRate,
                        VSync            = true,
                        WindowMode       = MyWindowModeEnum.Window, // initially create windowed, we change it to fullscreen afterwards
                    };
                    try
                    {
                        TryCreateDeviceInternal(windowHandle, deviceType, bestFitSettings, out m_device, out m_parameters);
                        Debug.Assert(m_device != null);
                        m_settings            = bestFitSettings;
                        m_settings.WindowMode = MyWindowModeEnum.Fullscreen;
                        ApplySettings(m_settings);
                        deviceCreated = true;
                    }
                    catch
                    {
                        /* Doesn't work again. */
                        DisposeDevice();
                    }
                }
            }

            if (!deviceCreated)
            {
                var simpleSettings = new MyRenderDeviceSettings()
                {
                    AdapterOrdinal   = 0,
                    BackBufferHeight = 480,
                    BackBufferWidth  = 640,
                    WindowMode       = MyWindowModeEnum.Window,
                    VSync            = true,
                };
                try
                {
                    TryCreateDeviceInternal(windowHandle, deviceType, simpleSettings, out m_device, out m_parameters);
                    Debug.Assert(m_device != null);
                    m_settings    = simpleSettings;
                    deviceCreated = true;
                }
                catch
                {
                    // These settings don't work either so we're done here.
                    MyMessageBox.Show("Unsupported graphics card", "Graphics card is not supported, please see minimum requirements");
                    throw;
                }
            }

            SupportsHDR = GetAdaptersList()[m_settings.AdapterOrdinal].HDRSupported;

            return(m_settings);
        }
コード例 #17
0
        internal static MyRenderDeviceSettings CreateDevice(IntPtr windowHandle, MyRenderDeviceSettings?settingsToTry)
        {
            MyRenderExceptionEnum exceptionEnum;
            bool deviceCreated = CreateDeviceInternalSafe(windowHandle, settingsToTry, false, out exceptionEnum);

            Log.WriteLine("CreateDevice: deviceCreated = " + deviceCreated);
            Log.WriteLine("CreateDevice: deviceCreated = " + deviceCreated);

#if !XB1
            if (!settingsToTry.HasValue || !settingsToTry.Value.SettingsMandatory)
            {
                if (!deviceCreated)
                {
                    if (settingsToTry.HasValue && settingsToTry.Value.UseStereoRendering)
                    {
                        Log.WriteLine("CreateDevice: Attempt to create stereo renderer");
                        var newSettings = settingsToTry.Value;
                        newSettings.UseStereoRendering = false;
                        deviceCreated = CreateDeviceInternalSafe(windowHandle, newSettings, false, out exceptionEnum);
                    }
                }
                if (!deviceCreated)
                {
                    Log.WriteLine("Primary desktop size fallback.");
                    var adapters = GetAdaptersList();
                    int i        = 0;
                    int j        = 0;
                    for (; i < adapters.Length; ++i)
                    {
                        for (j = 0; j < adapters[i].SupportedDisplayModes.Length; ++j)
                        {
                            if (adapters[i].IsDx11Supported)
                            {
                                var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
                                if (adapters[i].SupportedDisplayModes[j].Width == bounds.Width &&
                                    adapters[i].SupportedDisplayModes[j].Height == bounds.Height)
                                {
                                    var displayMode = adapters[i].SupportedDisplayModes[j];
                                    var newSettings = new MyRenderDeviceSettings()
                                    {
                                        AdapterOrdinal   = i,
                                        BackBufferHeight = displayMode.Height,
                                        BackBufferWidth  = displayMode.Width,
                                        WindowMode       = MyWindowModeEnum.Fullscreen,
                                        RefreshRate      = displayMode.RefreshRate,
                                        VSync            = true
                                    };

                                    deviceCreated = CreateDeviceInternalSafe(windowHandle, newSettings, false, out exceptionEnum);
                                    break;
                                }
                            }
                        }
                    }
                }
                if (!deviceCreated)
                {
                    Log.WriteLine("Lowest res fallback.");
                    var simpleSettings = new MyRenderDeviceSettings()
                    {
                        AdapterOrdinal   = 0,
                        BackBufferHeight = 480,
                        BackBufferWidth  = 640,
                        WindowMode       = MyWindowModeEnum.Window,
                        VSync            = true,
                    };
                    deviceCreated = CreateDeviceInternalSafe(windowHandle, simpleSettings, false, out exceptionEnum);
                }

                if (!deviceCreated)
                {
                    Log.WriteLine("Debug device fallback");
                    deviceCreated = CreateDeviceInternalSafe(windowHandle, settingsToTry, true, out exceptionEnum);
                }
            }
#else
#if !XB1_SKIPASSERTFORNOW
            System.Diagnostics.Debug.Assert(false, "simpleSettings is initialized but not used?");
#endif // !XB1_SKIPASSERTFORNOW
            Log.WriteLine("XB1 res fallback.");
            var simpleSettings = CreateXB1Settings();
#endif


            if (!deviceCreated)
            {
                // always display user friendly text to update drivers
                string message = string.Format("Graphics card could not be initialized. Please, apply windows updates and update to the latest graphics drivers.");
                VRage.Utils.MyMessageBox.Show("Unable to initialize the graphics system",
                                              message);
                throw new MyRenderException("No supported device detected!\nPlease apply windows updates and update to latest graphics drivers.", MyRenderExceptionEnum.GpuNotSupported);
            }
            return(m_settings);
        }
コード例 #18
0
        private static void TryCreateDeviceInternal(IntPtr windowHandle, DeviceType deviceType, MyRenderDeviceSettings settingsToTry, out Device device, out PresentParameters parameters)
        {
            device     = null;
            parameters = CreatePresentParameters(settingsToTry, windowHandle);
            while (device == null)
            {
                try
                {
                    // These calls are here to ensure that none of these calls throw exceptions (even if their results are not used).
                    m_d3d.Dispose();
                    m_d3d = new Direct3D();

                    var d3dCaps = m_d3d.GetDeviceCaps(settingsToTry.AdapterOrdinal, DeviceType.Hardware);

                    device = new Device(m_d3d, settingsToTry.AdapterOrdinal, deviceType, Parameters.DeviceWindowHandle,
                                        CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.FpuPreserve,
                                        Parameters);
                    device.Clear(ClearFlags.Target, new SharpDX.ColorBGRA(0, 0, 0, 1), 1.0f, 0);

                    var caps = Device.Capabilities;
                }
                catch (SharpDX.SharpDXException e)
                {
                    if (e.ResultCode == ResultCode.NotAvailable ||
                        (e.ResultCode == ResultCode.InvalidCall && GetForegroundWindow() != Parameters.DeviceWindowHandle))
                    {
                        // User has probably Alt+Tabbed or locked his computer before the game has started.
                        // To counter this, we try creating device again a bit later.
                        Thread.Sleep(2000);
                        MyLog.Default.WriteLine("Device creation failed with " + e.Message);
                    }
                    else
                    {
                        // Either settings or graphics card are not supported.
                        MyLog.Default.WriteLine(e);
                        throw;
                    }
                }

                try
                {
                    MyLog.Default.WriteLine("Loading adapters");
                    m_adaptersList = GetAdaptersList(m_d3d);
                    MyLog.Default.WriteLine("Found adapters");
                    foreach (var adapter in m_adaptersList)
                    {
                        adapter.LogInfo(MyLog.Default.WriteLine);
                    }
                }
                catch (Exception e)
                {
                    MyLog.Default.WriteLine(e);
                    throw;
                }
            }
        }
コード例 #19
0
ファイル: MyDX9Render.cs プロジェクト: caomw/SpaceEngineers
 public void ApplySettings(MyRenderDeviceSettings settings)
 {
     MyRender.ApplySettings(settings);
 }
コード例 #20
0
        internal static MyRenderDeviceSettings CreateDevice(IntPtr windowHandle, MyRenderDeviceSettings?settingsToTry)
        {
            bool deviceCreated = CreateDeviceInternalSafe(windowHandle, settingsToTry);

            if (!deviceCreated)
            {
                Log.WriteLine("Primary desktop size fallback.");
                var adapters = GetAdaptersList();
                int i        = 0;
                int j        = 0;
                for (; i < adapters.Length; ++i)
                {
                    for (j = 0; j < adapters[i].SupportedDisplayModes.Length; ++j)
                    {
                        if (adapters[i].IsDx11Supported)
                        {
                            var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
                            if (adapters[i].SupportedDisplayModes[j].Width == bounds.Width &&
                                adapters[i].SupportedDisplayModes[j].Height == bounds.Height)
                            {
                                var displayMode = adapters[i].SupportedDisplayModes[j];
                                var newSettings = new MyRenderDeviceSettings()
                                {
                                    AdapterOrdinal   = i,
                                    BackBufferHeight = displayMode.Height,
                                    BackBufferWidth  = displayMode.Width,
                                    WindowMode       = MyWindowModeEnum.Fullscreen,
                                    RefreshRate      = displayMode.RefreshRate,
                                    VSync            = true
                                };

                                deviceCreated = CreateDeviceInternalSafe(windowHandle, newSettings);
                                if (deviceCreated)
                                {
                                    return(m_settings);
                                }
                            }
                        }
                    }
                }
            }
            if (!deviceCreated)
            {
                Log.WriteLine("Lowest res fallback.");
                var simpleSettings = new MyRenderDeviceSettings()
                {
                    AdapterOrdinal   = 0,
                    BackBufferHeight = 480,
                    BackBufferWidth  = 640,
                    WindowMode       = MyWindowModeEnum.Window,
                    VSync            = true,
                };
                deviceCreated = CreateDeviceInternalSafe(windowHandle, simpleSettings);
                if (!deviceCreated)
                {
                    m_settings.AdapterOrdinal = -1;
                    VRage.Utils.MyMessageBox.Show("Unsupported graphics card", "Graphics card is not supported, please see minimum requirements");
                    throw new MyRenderException("No supported device detected!", MyRenderExceptionEnum.GpuNotSupported);
                }
            }

            return(m_settings);
        }