private void InitializeDevice() { device = new Dx11.Device(Dx11.DriverType.Hardware, Dx11.DeviceCreationFlags.None); for (int i = 0; i < 8; i++) { qual = device.CheckMultisampleQualityLevels(Dxgi.Format.R8G8B8A8_UNorm, i); if (qual > 0) { count = i; } } factor = new Dxgi.Factory(); swapChain = new Dxgi.SwapChain(factor, device, new Dxgi.SwapChainDescription() { BufferCount = 1, OutputHandle = this.Handle, IsWindowed = true, SampleDescription = new Dxgi.SampleDescription() { Count = count, Quality = qual - 1, }, ModeDescription = new Dxgi.ModeDescription() { Width = ClientSize.Width, Height = ClientSize.Height, RefreshRate = new Rational(60, 1), Format = Dxgi.Format.R8G8B8A8_UNorm, }, Usage = Dxgi.Usage.RenderTargetOutput }); InitializeRenderTarget(); InitializeDepthStencil(); InitializeViewport(); LoadContent(); InitializeInputDevice(); }
void DxInitDevice() { CurrentDevice = new Device(DriverType.Hardware, DeviceCreationFlags.None); if (CurrentDevice.FeatureLevel < FeatureLevel.Level_10_1) { throw new System.Exception("Direct3D Feature Level 10.1 unsupported"); } _dx11Mode = CurrentDevice.FeatureLevel >= FeatureLevel.Level_11_0; var msaaQuality = CurrentDevice.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, 4); _sampleDesc = _dx11Mode ? new SampleDescription(1, 0) : new SampleDescription(4, msaaQuality - 1); Logging.Write("MSAA Quality: " + msaaQuality); _context = CurrentDevice.ImmediateContext; }
/// <summary> /// Initialize graphics properties and create the main window. /// </summary> public static void Init() { //Make the Form Form = new RenderForm("LeafMeAlone"); Device = new Device(DriverType.Hardware, DeviceCreationFlags.None); var msaa = Device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, 4); SwapChainDescription description = new SwapChainDescription() { BufferCount = 1, Usage = Usage.RenderTargetOutput, OutputHandle = Form.Handle, IsWindowed = true, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), SampleDescription = /*msaa != 0 ? new SampleDescription(4,msaa) : */ new SampleDescription(1, 0), Flags = SwapChainFlags.None, SwapEffect = SwapEffect.Discard }; SwapChain = new SwapChain(Device.Factory, Device, description); //create new device (with directx) which can be used throughout the project. // Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out Device, out SwapChain); // create a view of our render target, which is the backbuffer of the swap chain we just created using (var resource = Resource.FromSwapChain <Texture2D>(SwapChain, 0)) RenderTarget = new RenderTargetView(Device, resource); //get device context so we can render to it. DeviceContext = Device.ImmediateContext; InitializeRasterizer(); InitializeDepthBuffer(); InitializeBlending(); Viewport = new Viewport(0.0f, 0.0f, Form.ClientSize.Width, Form.ClientSize.Height); //DeviceContext.OutputMerger.SetTargets(RenderTarget); DeviceContext.OutputMerger.SetTargets(DepthView, RenderTarget); DeviceContext.Rasterizer.SetViewports(Viewport); Form.Resize += FormOnResize; ProjectionMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, Viewport.Width / Viewport.Height, .1f, 1000.0f); // dont use default alt enter because it doesn't work. using (var factory = SwapChain.GetParent <Factory>()) factory.SetWindowAssociation(Form.Handle, WindowAssociationFlags.IgnoreAltEnter); #if DEBUG TextBox debugTextbox = new TextBox { Multiline = true, Dock = DockStyle.Fill, ScrollBars = ScrollBars.Vertical }; DebugForm = new Form(); DebugForm.Controls.Add(debugTextbox); DebugForm.Closing += (sender, args) => { args.Cancel = true; DebugForm.Hide(); }; Debug.Init(debugTextbox); #endif // handle alt+enter ourselves Form.KeyDown += (o, e) => { if (e.Shift && e.KeyCode == Keys.Enter) { Form.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); SwapChain.IsFullScreen = !SwapChain.IsFullScreen; } if (e.KeyCode == Keys.Escape) { Application.Exit(); } #if DEBUG if (e.Control && e.KeyCode == Keys.Enter) { DebugForm.Show(); } #endif }; InitializeComponent(Form); }
private SampleDescription GetMsaaDescription(Device device) { var msaaQuality = device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, MsaaSampleCount); return(new SampleDescription(MsaaSampleCount, msaaQuality - 1)); }
/// <summary> /// Get Device (could be temporary, could be not), set proper SampleDescription /// </summary> private Device InitializeDevice() { Debug.Assert(Initialized == false); var device = new Device(DriverType.Hardware, DeviceCreationFlags.None); if (device.FeatureLevel < FeatureLevel) { throw new Exception($"Direct3D Feature {FeatureLevel} unsupported"); } if (UseMsaa) { var msaaQuality = device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, 4); SampleDescription = new SampleDescription(4, msaaQuality - 1); } return device; }