Example #1
0
        protected bool InitDirect3D()
        {
            var creationFlags = D3D11.DeviceCreationFlags.None;

#if DEBUG
            // creationFlags |= D3D11.DeviceCreationFlags.Debug;
#endif



            try
            {
                Device = new D3D11.Device(DriverType, creationFlags);
            }
            catch (Exception ex)
            {
                MessageBox.Show("D3D11Device creation failed\n" + ex.Message + "\n" + ex.StackTrace, "Error");
                return(false);
            }
            ImmediateContext = Device.ImmediateContext;
            if (Device.FeatureLevel != D3D.FeatureLevel.Level_11_0)
            {
                MessageBox.Show("Direct3D Feature Level 11 unsupported");
                return(false);
            }

            Debug.Assert((Msaa4XQuality = Device.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, 4)) > 0);
            try
            {
                DXGI.SwapChainDescription sd = new DXGI.SwapChainDescription()
                {
                    ModeDescription = new DXGI.ModeDescription(ClientWidth, ClientHeight, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm)
                    {
                        ScanlineOrdering = DXGI.DisplayModeScanlineOrder.Unspecified,
                        Scaling          = DXGI.DisplayModeScaling.Unspecified
                    },
                    SampleDescription = Enable4XMsaa ? new DXGI.SampleDescription(4, Msaa4XQuality - 1) : new DXGI.SampleDescription(1, 0),
                    Usage             = DXGI.Usage.RenderTargetOutput,
                    BufferCount       = 1,
                    OutputHandle      = Window.Handle,
                    IsWindowed        = true,
                    SwapEffect        = DXGI.SwapEffect.Discard,
                    Flags             = DXGI.SwapChainFlags.None
                };


                using (var factory = new DXGI.Factory1())
                {
                    SwapChain = new DXGI.SwapChain(factory, Device, sd);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("SwapChain creation failed\n" + ex.Message + "\n" + ex.StackTrace, "Error");
                return(false);
            }
            OnResize();
            return(true);
        }
Example #2
0
        public void Init(IntPtr hWnd, ApplicationInfo appInfo)
        {
            if (m_ApplicationInfo != null)
            {
                throw new InvalidOperationException("context already initialized");
            }

            m_ApplicationInfo = appInfo;

            var swapChainDesc = new DXGI.SwapChainDescription
            {
                ModeDescription = new DXGI.ModeDescription
                {
                    Width       = m_ApplicationInfo.Width,
                    Height      = m_ApplicationInfo.Height,
                    RefreshRate = new DXGI.Rational(60, 1),
                    Format      = DXGI.Format.R8G8B8A8_UNorm
                },
                SampleDescription = new DXGI.SampleDescription
                {
                    Count = 4
                },
                BufferCount  = 1,
                Usage        = DXGI.Usage.RenderTargetOutput,
                OutputHandle = hWnd,
                IsWindowed   = !m_ApplicationInfo.FullScreen,
                SwapEffect   = DXGI.SwapEffect.Discard,
                Flags        = DXGI.SwapChainFlags.AllowModeSwitch
            };

            D3D11.Device.CreateWithSwapChain(D3D.DriverType.Hardware,
                                             D3D11.DeviceCreationFlags.BgraSupport | D3D11.DeviceCreationFlags.Debug, new[] { m_FeatureLevel },
                                             swapChainDesc, out D3D11.Device dev, out DXGI.SwapChain swapChain);
            Dev       = dev;
            DevCon    = Dev.ImmediateContext;
            SwapChain = swapChain;

            D2DFactory    = new D2D1.Factory(D2D1.FactoryType.SingleThreaded, D2D1.DebugLevel.Information);
            DWriteFactory = new DirectWrite.Factory(DirectWrite.FactoryType.Shared);

            m_MSAAQuality = Dev.CheckMultisampleQualityLevels(DXGI.Format.R8G8B8A8_UNorm, 4);

            D3D11.DeviceDebug d3dDebug = dev.QueryInterface <D3D11.DeviceDebug>();
            if (d3dDebug != null)
            {
                D3D11.InfoQueue infoQueue = dev.QueryInterface <D3D11.InfoQueue>();
                if (infoQueue != null)
                {
                    D3D11.MessageId[] hide =
                    {
                        D3D11.MessageId.MessageIdDeviceDrawSamplerNotSet
                    };
                    infoQueue.AddStorageFilterEntries(new D3D11.InfoQueueFilter()
                    {
                        DenyList = new D3D11.InfoQueueFilterDescription {
                            Ids = hide
                        }
                    });
                }
            }

            Resize();

            CreateBlendStates();
            CreateDepthStencilStates();
            CreateRasterizerStates();

            SetBlend(false);
            SetDepthTesting(true);
            SetWireframe(false);

            SharpDX.DXGI.Device             dxgiDev     = Dev.QueryInterface <SharpDX.DXGI.Device>();
            SharpDX.DXGI.AdapterDescription adapterDesc = dxgiDev.Adapter.Description;

            Console.WriteLine("----------------------------------");
            Console.WriteLine(" Direct3D 11:");
            Console.WriteLine("    " + adapterDesc.Description);
            Console.WriteLine("    VRAM: " + adapterDesc.DedicatedVideoMemory / 1024 / 1024 + " MB");
            Console.WriteLine("----------------------------------");

            dxgiDev.Dispose();
        }