예제 #1
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         db.Dispose();
     }
     base.Dispose(disposing);
 }
예제 #2
0
        internal static void DisposeDevice()
        {
            ForceWindowed();

            OnDeviceEnd();

            m_initialized = false;

            MyHBAO.ReleaseScreenResources();

            if (MyGBuffer.Main != null)
            {
                MyGBuffer.Main.Release();
                MyGBuffer.Main = null;
            }

            if (Backbuffer != null)
            {
                Backbuffer.Release();
                Backbuffer = null;
            }

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

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

            if (Device != null)
            {
#if DEBUG
                if (VRage.MyCompilationSymbols.DX11Debug)
                {
                    var deviceDebug = new DeviceDebug(Device);
                    deviceDebug.ReportLiveDeviceObjects(ReportingLevel.Detail | ReportingLevel.Summary);
                    deviceDebug.Dispose();
                }
#endif

                Device.Dispose();
                Device = null;
                WIC    = null;
            }

            if (m_factory != null)
            {
                m_factory.Dispose();
                m_factory = null;
            }
        }
예제 #3
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);
        }
예제 #4
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);
        }