Example #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="device">Device</param>
        /// <param name="breakOnWarning">Generate an error on warning</param>
        public SharpDebugger(SharpDevice device, bool breakOnWarning)
        {
            _device = device;
            //init the debug device
            Debug = new DeviceDebug(device.Device);
            //init the queue interface
            Queue = Debug.QueryInterface<InfoQueue>();

            if (breakOnWarning)
                Queue.SetBreakOnSeverity(MessageSeverity.Warning, true);
        }
        internal static void DisposeDevice()
        {
            ForceWindowed();

            OnDeviceEnd();

            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 (DeviceContext != null)
            {
                DeviceContext.Dispose();
                DeviceContext = null;
            }

            if (Device != null)
            {
    #if DEBUG_DEVICE
                var deviceDebug = new DeviceDebug(Device);
                deviceDebug.ReportLiveDeviceObjects(ReportingLevel.Detail);
                deviceDebug.Dispose();
    #endif

                Device.Dispose();
                Device = null;
            }

            if(m_factory != null)
            {
                m_factory.Dispose();
                m_factory = null;
            }
        }
        private static MyRenderDeviceSettings CreateDeviceInternal(IntPtr windowHandle, MyRenderDeviceSettings? settingsToTry)
        {
            if (Device != null)
            { 
                Device.Dispose();
                Device = null;
            }

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

            FeatureLevel[] featureLevels = { FeatureLevel.Level_11_0 };
            DeviceCreationFlags flags = DeviceCreationFlags.None;
      
    #if DEBUG_DEVICE && DEBUG
            flags |= DeviceCreationFlags.Debug;
    #endif

            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,
            };
            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");
            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];
            Device = new Device(adapter, 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(DeviceContext != null)
            {
                DeviceContext.Dispose();
                DeviceContext = null;
            }

            DeviceContext = 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.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;
        }
        internal static void DisposeDevice()
        {
            ForceWindowed();

            OnDeviceEnd();
            
            m_initialized = false;

            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 (DeviceContext != null)
            {
                DeviceContext.Dispose();
                DeviceContext = 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;
            }
        }
Example #5
0
        private void ReleaseDevice()
        {
            // Display D3D11 ref counting info
            ClearState();
            NativeDevice.ImmediateContext.Flush();
            NativeDevice.ImmediateContext.Dispose();

            if (IsDebugMode)
            {
                var deviceDebug = new DeviceDebug(NativeDevice);
                deviceDebug.ReportLiveDeviceObjects(ReportingLevel.Detail);
            }

            currentInputLayout = null;
            currentEffectInputSignature = null;
            currentVertexArrayObject = null;
            currentVertexArrayLayout = null;
            nativeDevice.Dispose();
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FormRenderer"/> class.
        /// </summary>
        /// <param name="form">The form.</param>
        public FormRenderer(Form1 form)
        {
            this.form = form;
#if DEBUG
            var creationFlags = D3D11.DeviceCreationFlags.BgraSupport | D3D11.DeviceCreationFlags.Debug;
            var debugFactory  = true;
#else
            var creationFlags = D3D11.DeviceCreationFlags.BgraSupport;
            var debugFactory  = false;
#endif

            this.device = new D3D11.Device(D3D.DriverType.Hardware, creationFlags);

#if DEBUG
            this.deviceDebug = new D3D11.DeviceDebug(this.device);
#endif

            using (var dxgiDevice = this.device.QueryInterface <DXGI.Device>())
            {
                using (var dxgiFactory = new DXGI.Factory2(debugFactory))
                {
                    var desc = new DXGI.SwapChainDescription1()
                    {
                        BufferCount       = 2,
                        AlphaMode         = DXGI.AlphaMode.Premultiplied,
                        SampleDescription = new DXGI.SampleDescription(1, 0),
                        Usage             = DXGI.Usage.RenderTargetOutput,
                        SwapEffect        = DXGI.SwapEffect.FlipDiscard,
                        Format            = DXGI.Format.B8G8R8A8_UNorm,
                        Width             = form.Width,
                        Height            = form.Height,
                    };

                    this.swapChain = new DXGI.SwapChain1(dxgiFactory, dxgiDevice, ref desc, null);

                    this.deviceComp        = new DComp.Device(dxgiDevice);
                    this.compositionTarget = DComp.Target.FromHwnd(this.deviceComp, form.Handle, true);

                    using (var visual = new DComp.Visual(this.deviceComp))
                    {
                        visual.Content = this.swapChain;
                        this.compositionTarget.Root = visual;
                    }

                    this.deviceComp.Commit();
                }
            }

            using (var device = this.device.QueryInterface <DXGI.Device>())
            {
                this.device2d = new D2D.Device(this.factory2d, device);
            }

            this.deviceContext2D = new D2D.DeviceContext(this.device2d, D2D.DeviceContextOptions.None)
            {
                DotsPerInch   = this.factory2d.DesktopDpi,
                AntialiasMode = D2D.AntialiasMode.PerPrimitive,
            };

            this.CreateResources();
        }
        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;

            bool adapterIndexNotValid = 
                adapterIndex == -1
                || adapters.Length <= settingsToTry.Value.AdapterOrdinal
                || !adapters[settingsToTry.Value.AdapterOrdinal].IsSupported;
            if(adapterIndexNotValid)
            {
                var maxVram = 0ul;

                for(int i=0; i<adapters.Length; i++)
                {
                    if(adapters[i].IsSupported)
                    {
                        maxVram = (ulong) Math.Max(maxVram, adapters[i].VRAM);
                    }
                }

                // taking supporting adapter with most VRAM
                for (int i = 0; i < adapters.Length; i++)
                {
                    if(adapters[i].IsSupported && adapters[i].VRAM == maxVram)
                    {
                        adapterIndex = i;
                        break;
                    }
                }
            }

            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,
                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;
        }