private void CreateDevice() { var sd = new DXGI.SwapChainDescription { BufferCount = 1, Flags = DXGI.SwapChainFlags.None, IsWindowed = true, ModeDescription = new DXGI.ModeDescription(this.ClientSize.Width, this.ClientSize.Height, new Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm), OutputHandle = this.Handle, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput }; Device.CreateWithSwapChain( null, DriverType.Hardware, DeviceCreationFlags.None, sd, out this._Device, out this._SwapChain); this._RenderEventArgs.Device = this._Device; this._RenderEventArgs.SwapChain = this._SwapChain; this.ResizeBackbuffer(); }
// Inicializácia zariadenia a ďalších potrebných zdrojov pre rendering. Metóda vráti true, ak skončí bez chyby. private bool Initialize3D() { try { //Vytvorenie objektu typu SwapChainDescription a nastavenie jeho vlastností m_swapChainDesc = new DXGI.SwapChainDescription(); m_swapChainDesc.OutputHandle = panelVykresli.Handle; m_swapChainDesc.IsWindowed = true; m_swapChainDesc.BufferCount = 1; m_swapChainDesc.Flags = DXGI.SwapChainFlags.AllowModeSwitch; m_swapChainDesc.ModeDescription = new DXGI.ModeDescription( panelVykresli.Width, panelVykresli.Height, new Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm); m_swapChainDesc.SampleDescription = new DXGI.SampleDescription(1, 0); m_swapChainDesc.SwapEffect = DXGI.SwapEffect.Discard; m_swapChainDesc.Usage = DXGI.Usage.RenderTargetOutput; ///Vytvorenie objektu zariadenie a SwapChain spoločne jednou metódou DX11.Device.CreateWithSwapChain( DriverType.Warp, //Typ renderovacieho zariadenia: SW renderer: Reference, Warp, Hardware - hardvérový renderer: http://slimdx.org/tutorials/DeviceCreation.php DeviceCreationFlags.None, //Žiadne extra návestia m_levels, //Kompatibilita s verziami DX m_swapChainDesc, //Objekt typu SwapChainDescription out m_device, //vráti Direct3D zariadenie reprezentujúce virtuálny graf. HW out m_swapChain); //vráti SwapChain pre prácu s baframi //Vytvorenie zobrazovacej plochy - celé okno ap. var m_viewPort = new Viewport(0.0f, 0.0f, panelVykresli.Width, panelVykresli.Height); //Vytvorenie objektu zadného bafra a cieľa renderovania DX11.Texture2D m_backBuffer = Texture2D.FromSwapChain <Texture2D>(m_swapChain, 0); m_renderTarget = new RenderTargetView(m_device, m_backBuffer); m_deviceContext = m_device.ImmediateContext; //Nastavenie kontextu zariadenia, ktorý obsahuje nastavenia pre D3D zariadenie, tu priame renderovanie bez vytvárania príkazového zoznamu. Pozri: http://msdn.microsoft.com/en-us/library/windows/desktop/ff476880%28v=vs.85%29.aspx m_deviceContext.Rasterizer.SetViewports(m_viewPort); //Nastavenie zobrazovacej plochy m_deviceContext.OutputMerger.SetTargets(m_renderTarget); //Nastavenie cieľa renderovania } catch (Exception ex) { MessageBox.Show("Chyba počas inicializácie Direct3D11: \n" + ex.Message); m_initialized = false; } return(m_initialized); }
void InitD3D() { var swapDesc = new Dxgi.SwapChainDescription { BufferCount = 1, ModeDescription = new Dxgi.ModeDescription { Width = Container.ClientSize.Width, Height = Container.ClientSize.Height, RefreshRate = new Rational(60, 1), Format = Dxgi.Format.R8G8B8A8_UNorm }, IsWindowed = true, OutputHandle = Container.Handle, SampleDescription = new Dxgi.SampleDescription(1, 0), SwapEffect = Dxgi.SwapEffect.Discard, Usage = Dxgi.Usage.RenderTargetOutput }; Device device; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, swapDesc, out device, out SwapChain); Device = device; m_context = Device.ImmediateContext; Texture2DDescription colordesc = new Texture2DDescription { BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = Dxgi.Format.B8G8R8A8_UNorm, Width = Width, Height = Height, MipLevels = 1, SampleDescription = new Dxgi.SampleDescription(1, 0), Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.Shared, CpuAccessFlags = CpuAccessFlags.None, ArraySize = 1 }; Texture2DDescription depthdesc = new Texture2DDescription { BindFlags = BindFlags.DepthStencil, Format = Dxgi.Format.D24_UNorm_S8_UInt, Width = Width, Height = Height, MipLevels = 1, SampleDescription = new Dxgi.SampleDescription(1, 0), Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.None, CpuAccessFlags = CpuAccessFlags.None, ArraySize = 1 }; SharedTexture = new Texture2D(Device, colordesc); DepthTexture = new Texture2D(Device, depthdesc); RenderView = new RenderTargetView(Device, SharedTexture); DepthView = new DepthStencilView(Device, DepthTexture); var rastDesc = new RasterizerStateDescription() { CullMode = CullMode.None, FillMode = FillMode.Solid, }; m_context.Rasterizer.State = RasterizerState.FromDescription(Device, rastDesc); QuadIndices = RenderSupport.InitQuadIndices(Device); m_context.Flush(); }
// Resets the graphics public bool resetGraphics() { // Variables DX3D.DeviceCreationFlags flags= DX3D.DeviceCreationFlags.None; #if DEBUG flag|= DX3D.DeviceCreationFlags.Debug; #endif try { dxg.device= new DX3D.Device(settings.driverType, flags); } catch { Wfx.MessageBox.Show("Could not initiate DirectX!"); return false; } dxg.im= dxg.device.ImmediateContext; if(dxg.device.FeatureLevel!= DX3D.FeatureLevel.Level_11_0) { Wfx.MessageBox.Show("Direct3D 11 unsupported"); return false; } try { // Variables DXGI.SwapChainDescription sd= new DXGI.SwapChainDescription(); DXGI.ModeDescription md= new DXGI.ModeDescription(game.window.widthi, game.window.heighti, settings.fps, DXGI.Format.R8G8B8A8_UNorm); md.ScanlineOrdering= DXGI.DisplayModeScanlineOrdering.Unspecified; md.Scaling= DXGI.DisplayModeScaling.Unspecified; sd.ModeDescription= md; sd.SampleDescription= ((settings.bEnable4XMsaa) ? new DXGI.SampleDescription(4, settings.msaa4x-1): new DXGI.SampleDescription(1, 0) ); sd.Usage= DXGI.Usage.RenderTargetOutput; sd.BufferCount= 1; sd.OutputHandle= game.window.frame.Handle; sd.IsWindowed= settings.bWindowed; sd.SwapEffect= DXGI.SwapEffect.Discard; sd.Flags= DXGI.SwapChainFlags.None; dxg.swapChain= new DXGI.SwapChain(dxg.device.Factory, dxg.device, sd); } catch { Wfx.MessageBox.Show("Could not create swap chain :("); return false; } return true; }