示例#1
0
        public bool Init(IntPtr WindowHandle)
        {
            var factory            = DXGI.CreateDXGIFactory1 <IDXGIFactory1>();
            var adapter            = factory.GetAdapter1(0);
            var monitor            = adapter.GetOutput(0);
            var modes              = monitor.GetDisplayModeList(Format.R8G8B8A8_UNorm, DisplayModeEnumerationFlags.Interlaced);
            var rational           = new Rational(0, 1);
            var adapterDescription = adapter.Description;

            //VideoCardMemory = adapterDescription.DedicatedVideoMemory >> 10 >> 10;
            //VideoCardDescription = adapterDescription.Description.Trim('\0');
            monitor.Dispose();
            adapter.Dispose();

            var swapChainDesc = new SwapChainDescription()
            {
                BufferCount       = 2,
                BufferDescription = new ModeDescription(1920, 1080, rational, Format.R8G8B8A8_UNorm),
                Usage             = Usage.RenderTargetOutput,
                OutputWindow      = WindowHandle,
                SampleDescription = new SampleDescription(1, 0),
                IsWindowed        = true,
                Flags             = SwapChainFlags.None,
                SwapEffect        = SwapEffect.Discard
            };

            // Create Device and DeviceContext
            ID3D11Device        TempDevice        = null;
            ID3D11DeviceContext TempDeviceContext = null;

            D3D11.D3D11CreateDevice(adapter, DriverType.Hardware, DeviceCreationFlags.None, null, out TempDevice, out TempDeviceContext);

            Device        = TempDevice.QueryInterface <ID3D11Device1>();
            DeviceContext = TempDeviceContext.QueryInterface <ID3D11DeviceContext1>();
            TempDevice.Dispose();
            TempDeviceContext.Dispose();

            // Create SwapChain
            SwapChain = factory.CreateSwapChain(Device, swapChainDesc);
            factory.MakeWindowAssociation(WindowHandle, WindowAssociationFlags.IgnoreAltEnter);

            var backBuffer = SwapChain.GetBuffer <ID3D11Texture2D>(0);

            m_RenderTargetView = Device.CreateRenderTargetView(backBuffer);
            backBuffer.Dispose();

            // Create blend state
            BlendDescription bsd = new BlendDescription()
            {
                AlphaToCoverageEnable  = false,//true,
                IndependentBlendEnable = false,
            };

            bsd.RenderTarget[0].BlendOperationAlpha   = BlendOperation.Add;
            bsd.RenderTarget[0].BlendOperation        = BlendOperation.Add;
            bsd.RenderTarget[0].DestinationBlendAlpha = Blend.One;
            bsd.RenderTarget[0].DestinationBlend      = Blend.InverseSourceAlpha;
            bsd.RenderTarget[0].IsBlendEnabled        = true;
            bsd.RenderTarget[0].RenderTargetWriteMask = ColorWriteEnable.All;
            bsd.RenderTarget[0].SourceBlendAlpha      = Blend.Zero;
            bsd.RenderTarget[0].SourceBlend           = Blend.SourceAlpha;
            bsd.AlphaToCoverageEnable = true;

            ID3D11BlendState bsAlpha = Device.CreateBlendState(bsd);

            // Set Blend State
            DeviceContext.OMSetBlendState(bsAlpha);
            BuildDepthStencilView(1920, 1080);

            // Create rasterizers
            m_RSDesc = new RasterizerDescription()
            {
                AntialiasedLineEnable = false,
                CullMode              = CullMode.Back,
                DepthBias             = 0,
                DepthBiasClamp        = .0f,
                DepthClipEnable       = false,
                FillMode              = FillMode.Solid,
                FrontCounterClockwise = true,
                MultisampleEnable     = true,
                ScissorEnable         = false,
                SlopeScaledDepthBias  = .0f
            };

            m_RSCullSolid     = Device.CreateRasterizerState(m_RSDesc);
            m_RSDesc.CullMode = CullMode.None;
            m_RSSolid         = Device.CreateRasterizerState(m_RSDesc);
            m_RSDesc.FillMode = FillMode.Wireframe;
            m_RSWireFrame     = Device.CreateRasterizerState(m_RSDesc);
            m_RSDesc.CullMode = CullMode.Back;
            m_RSCullWireFrame = Device.CreateRasterizerState(m_RSDesc);

            UpdateRasterizer();
            return(true);
        }