protected void InitialiseComponents() { SharpDX.Configuration.EnableObjectTracking = true; var description = new DXGI.SwapChainDescription { BufferCount = 1, ModeDescription = new DXGI.ModeDescription(Width, Height, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm), IsWindowed = true, OutputHandle = Handle, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput }; Direct3D.Device1.CreateWithSwapChain(Direct3D.DriverType.Hardware, Direct3D.DeviceCreationFlags.BgraSupport, description, Direct3D.FeatureLevel.Level_10_0, out device, out swapChain); var dxgiFactory = swapChain.GetParent <DXGI.Factory>(); dxgiFactory.MakeWindowAssociation(Handle, DXGI.WindowAssociationFlags.IgnoreAll); directWriteFactory = new DirectWrite.Factory(); CreateSizeDependentComponents(); children.ForEach(c => c.SetContext(this)); }
/// <summary> /// /// </summary> protected void InitializeD3D10(int pWidth, int pHeight) { // SwapChain description var desc = new DXGI.SwapChainDescription() { BufferCount = 1, ModeDescription = new DXGI.ModeDescription(pWidth, pHeight, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = DisplayHandle, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput }; // Create Device and SwapChain SharpDX.Direct3D10.Device1.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc, SharpDX.Direct3D10.FeatureLevel.Level_10_0, out mDevice, out mSwapChain); // Ignore all windows events DXGI.Factory factory = mSwapChain.GetParent <DXGI.Factory>(); factory.MakeWindowAssociation(DisplayHandle, DXGI.WindowAssociationFlags.IgnoreAll); // New RenderTargetView from the backbuffer mBackBuffer = Texture2D.FromSwapChain <Texture2D>(mSwapChain, 0); mBackBufferView = new RenderTargetView(mDevice, mBackBuffer); }
public void ConstructRenderAndResource(double width, double height) { float dpiX, dpiY; this.GetDpi(out dpiX, out dpiY); var desc = new DXGI.SwapChainDescription() { BufferCount = 1, ModeDescription = new DXGI.ModeDescription((int)width, (int)height, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm), IsWindowed = true, OutputHandle = TextBox.Handle, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput }; this.swapchain = new DXGI.SwapChain(factory_dxgi, device, desc); this.device2d = new D2D.Device1(this._factory.D2DFactory, device_dxgi); this.render = new D2D.DeviceContext1(this.device2d, D2D.DeviceContextOptions.None); D2D.BitmapProperties bmpProp = new D2D.BitmapProperties(); bmpProp.DpiX = dpiX; bmpProp.DpiY = dpiY; bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied); this.bmp_d2d = new D2D.Bitmap(this.render, DXGI.Surface.FromSwapChain(swapchain, 0), bmpProp); this.cachedBitMap = new D2D.Bitmap(this.render, new SharpDX.Size2((int)width, (int)height), bmpProp); this.hasCache = false; this.render.Target = this.bmp_d2d; this.textRender = new CustomTextRenderer(this._factory, this.Foreground); this.renderSize = new Size(width, height); //デフォルト値を反映させる this.Foreground = ToColor4(this.TextBox.Foreground); this.Background = ToColor4(this.TextBox.Background); this.ControlChar = ToColor4(this.TextBox.ControlChar); this.Url = ToColor4(this.TextBox.Url); this.Keyword1 = ToColor4(this.TextBox.Keyword1); this.Keyword2 = ToColor4(this.TextBox.Keyword2); this.Literal = ToColor4(this.TextBox.Literal); this.Comment = ToColor4(this.TextBox.Comment); this.Hilight = ToColor4(this.TextBox.Hilight); this.LineMarker = ToColor4(this.TextBox.LineMarker); this.InsertCaret = ToColor4(this.TextBox.InsertCaret); this.OverwriteCaret = ToColor4(this.TextBox.OverwriteCaret); this.UpdateArea = ToColor4(this.TextBox.UpdateArea); this.HilightForeground = ToColor4(this.TextBox.HilightForeground); }
private void InitRenderer() { swapChainDescription = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, }; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, swapChainDescription, out device, out swapChain); context = device.ImmediateContext; var factory = swapChain.GetParent <Factory>(); factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); factory.Dispose(); form.UserResized += (sender, args) => { var sform = (RenderForm)sender; RenderContext.ViewportDirty = true; RenderContext.ViewportSize = new Vector2(sform.ClientSize.Width, sform.ClientSize.Height); }; form.MouseLeave += (sender, args) => RenderContext.HasMouseFocus = false; form.MouseEnter += (sender, args) => RenderContext.HasMouseFocus = true; form.GotFocus += (sender, args) => RenderContext.HasMouseFocus = true; form.LostFocus += (sender, args) => RenderContext.HasMouseFocus = false; form.MouseMove += (sender, args) => { /* * RenderContext.LastMousePosition = new Vector2( args.X, args.Y ); * RenderContext.LastMouseWorldPosition = new Vector2( args.X, args.Y ); * RenderContext.LastMouseWorldPosition -= RenderContext.ViewportSize / 2.0f; * var mouseWorldPosTransformed = new Vector4( RenderContext.LastMouseWorldPosition.X, RenderContext.LastMouseWorldPosition.Y, 0, 1 ); * RenderContext.LastMouseWorldPosition = new Vector2(mouseWorldPosTransformed.X, mouseWorldPosTransformed.Y); */ }; RenderContext = new DocumentRenderContext(); RenderContext.ViewportSize = new Vector2(form.ClientSize.Width, form.ClientSize.Height); RenderContext.ViewportDirty = true; immediateContext = new ImmediateContext(); immediateContext.Init(device); renderLoop = new RenderLoop(form); }
private void InitializeDevices() { try { SwapChainDescription = new DXGI.SwapChainDescription(); SwapChainDescription.BufferCount = 2; SwapChainDescription.SampleDescription = new DXGI.SampleDescription(1, 0); SwapChainDescription.SwapEffect = DXGI.SwapEffect.Discard; SwapChainDescription.Usage = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput; SwapChainDescription.IsWindowed = true; SwapChainDescription.ModeDescription = new DXGI.ModeDescription(GameWindow.Current.WindowParameters.Width, GameWindow.Current.WindowParameters.Height, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm); SwapChainDescription.OutputHandle = GameWindowHandle; D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.BgraSupport, featureLevels, SwapChainDescription, out D3DDefaultDevice, out SwapChain); DXGI.Factory factory = SwapChain.GetParent <DXGI.Factory>(); factory.MakeWindowAssociation(GameWindowHandle, DXGI.WindowAssociationFlags.IgnoreAll); D3DDevice = D3DDefaultDevice.QueryInterface <D3D11.Device1>(); Backbuffer = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(SwapChain, 0); RenderTargetView = new D3D11.RenderTargetView(D3DDevice, Backbuffer); D3DDevice.ImmediateContext.Rasterizer.SetViewport(0, 0, GameWindow.Current.WindowParameters.Width, GameWindow.Current.WindowParameters.Height); D3DDevice.ImmediateContext.OutputMerger.SetTargets(RenderTargetView); DXGIDevice = D3DDevice.QueryInterface <DXGI.Device>(); D2DFactory = new D2D1.Factory1(D2D1.FactoryType.MultiThreaded); D2DDevice = new D2D1.Device(D2DFactory, DXGIDevice); D2DDeviceContext = new D2D1.DeviceContext(D2DDevice, D2D1.DeviceContextOptions.None); RenderTargetSurface = Backbuffer.QueryInterface <DXGI.Surface>(); RenderTarget = new D2D1.RenderTarget(D2DFactory, RenderTargetSurface, new D2D1.RenderTargetProperties(new D2D1.PixelFormat(DXGI.Format.Unknown, D2D1.AlphaMode.Premultiplied))); RenderTarget.AntialiasMode = D2D1.AntialiasMode.PerPrimitive; // Initialize debug drawings brushes DrawingBoundsBrush = new D2D1.SolidColorBrush(RenderTarget, new SharpDX.Color(1f, 1f, 0f)); CollisionBoxesBrush = new D2D1.SolidColorBrush(RenderTarget, new SharpDX.Color(1f, 0f, 0f)); RenderFrame = new RenderFrame(RenderTarget); Clock = Stopwatch.StartNew(); } catch (Exception ex) { throw new DeviceInitializationException("Unable to initialize DirectX device!", ex); } }
private void InitializeSwapChain() { DXGI.ModeDescription backBufferDesc = new DXGI.ModeDescription(Width, Height, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm); DXGI.SwapChainDescription swapChainDesc = new DXGI.SwapChainDescription() { ModeDescription = backBufferDesc, SampleDescription = new DXGI.SampleDescription(1, 0), Usage = DXGI.Usage.RenderTargetOutput, BufferCount = 1, OutputHandle = renderForm.Handle, IsWindowed = true }; D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out device, out swapChain); }
private void InitializeDirectXResources() { var clientSize = ClientSize; var backBufferDesc = new DXGI.ModeDescription(clientSize.Width, clientSize.Height, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm); var swapChainDesc = new DXGI.SwapChainDescription() { ModeDescription = backBufferDesc, SampleDescription = new DXGI.SampleDescription(1, 0), Usage = DXGI.Usage.RenderTargetOutput, BufferCount = 1, OutputHandle = Handle, SwapEffect = DXGI.SwapEffect.Discard, IsWindowed = false }; D3D11.Device.CreateWithSwapChain(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.BgraSupport, new[] { D3D.FeatureLevel.Level_10_0 }, swapChainDesc, out _d3DDevice, out var swapChain); _d3DDeviceContext = _d3DDevice.ImmediateContext; _swapChain = new DXGI.SwapChain1(swapChain.NativePointer); _d2DFactory = new D2D1.Factory(); using (var backBuffer = _swapChain.GetBackBuffer <D3D11.Texture2D>(0)) { _renderTargetView = new D3D11.RenderTargetView(_d3DDevice, backBuffer); _renderTarget = new D2D1.RenderTarget(_d2DFactory, backBuffer.QueryInterface <DXGI.Surface>(), new D2D1.RenderTargetProperties(new D2D1.PixelFormat(DXGI.Format.Unknown, D2D1.AlphaMode.Premultiplied))) { TextAntialiasMode = D2D1.TextAntialiasMode.Cleartype }; } _solidColorBrush = new D2D1.SolidColorBrush(_renderTarget, Color.White); _dwFactory = new DW.Factory(DW.FactoryType.Shared); _textFormat = new DW.TextFormat(_dwFactory, "Arial", DW.FontWeight.Bold, DW.FontStyle.Normal, DW.FontStretch.Normal, 84 * (float)GraphicsUtils.Scale) { TextAlignment = DW.TextAlignment.Center, ParagraphAlignment = DW.ParagraphAlignment.Center }; _bitmap = _paradigm.Config.Gui.UseBitmap ? Properties.Resources.Einstein.ToD2D1Bitmap(_renderTarget) : null; }
private void createSwapChain() { lock (deviceManager.DeviceLock) { var factory = new DXGI.Factory(); var description = new DXGI.SwapChainDescription() { BufferCount = 1, ModeDescription = new DXGI.ModeDescription( control.ClientSize.Width, control.ClientSize.Height, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm), IsWindowed = true, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput, OutputHandle = control.Handle }; swapChain = new DXGI.SwapChain(factory, deviceContext.Device, description); backbuffer = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(swapChain, 0); backbufferView = new D3D11.RenderTargetView(deviceContext.Device, backbuffer); } var d2dFactory = deviceManager.Direct2dFactory; var surface = backbuffer.QueryInterface <DXGI.Surface>(); renderTarget = new D2D.RenderTarget( d2dFactory, surface, new D2D.RenderTargetProperties( new D2D.PixelFormat( DXGI.Format.Unknown, D2D.AlphaMode.Premultiplied))); renderTarget.AntialiasMode = D2D.AntialiasMode.Aliased; createViewport(); }
public DeviceResources(IntPtr windowHandle, int renderResWidth, int renderResHeight, int targetFPS) { Device = new D3D11.Device(DriverType.Hardware, D3D11.DeviceCreationFlags.Debug | D3D11.DeviceCreationFlags.DisableGpuTimeout); DeviceContext = Device.ImmediateContext; var backBufferDesc = new DXGI.ModeDescription( renderResWidth, renderResHeight, new DXGI.Rational(targetFPS, 1), DXGI.Format.R8G8B8A8_UNorm ); //var backBufferDesc = new DXGI.ModeDescription() //{ // Width = renderResWidth, // Height = renderResHeight, // RefreshRate = new DXGI.Rational(targetFPS, 1), // Format = DXGI.Format.R8G8B8A8_UNorm //}; var swapChainDesc = new DXGI.SwapChainDescription() { ModeDescription = backBufferDesc, SampleDescription = sampleDescription, Usage = DXGI.Usage.RenderTargetOutput, BufferCount = 1, OutputHandle = windowHandle, IsWindowed = true }; // Create SwapChain DXGI.Factory factory = new DXGI.Factory1(); SwapChain = new DXGI.SwapChain(factory, Device, swapChainDesc); // Create BackBuffer RTV BackBuffer = SwapChain.GetBackBuffer <D3D11.Texture2D>(0); BackBufferRTV = new D3D11.RenderTargetView(Device, BackBuffer); }
protected virtual void InitRenderer() { mSwapChainDesc = new dx.SwapChainDescription() { BufferCount = 1, IsWindowed = true, OutputHandle = Target.Handle, SwapEffect = dx.SwapEffect.Discard, Usage = dx.Usage.RenderTargetOutput | dx.Usage.ShaderInput, Flags = dx.SwapChainFlags.None, ModeDescription = new dx.ModeDescription(0, 0, dx.Rational.Empty, dx.Format.B8G8R8A8_UNorm), SampleDescription = new dx.SampleDescription(1, 0) }; d3_11.Device.CreateWithSwapChain(d3.DriverType.Hardware, d3_11.DeviceCreationFlags.BgraSupport, mSwapChainDesc, out mD3Device, out mSwapChain); mSurface = dx.Surface.FromSwapChain(mSwapChain, 0); WriteFactory = new dWrite.Factory(); mDevice = new d2.DeviceContext(mSurface); mRenderCallback = new dWin.RenderLoop.RenderCallback(Render); }
public void InitScreen1(IntPtr handle) { d2dContext = new D2D1.DeviceContext(_d2dDevice, D2D1.DeviceContextOptions.None); d2dContextCdgText = new D2D1.DeviceContext(_d2dDevice, D2D1.DeviceContextOptions.None); // DXGI SwapChain DXGI.SwapChainDescription swapChainDesc = new DXGI.SwapChainDescription() { BufferCount = 1, Usage = DXGI.Usage.RenderTargetOutput, OutputHandle = handle, IsWindowed = true, ModeDescription = new DXGI.ModeDescription(0, 0, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm), SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard }; swapChain = new DXGI.SwapChain(_dxgiDevice.GetParent <DXGI.Adapter>().GetParent <DXGI.Factory>(), _d3d11Device, swapChainDesc); // BackBuffer _backBuffer = DXGI.Surface.FromSwapChain(swapChain, 0); //BackBuffer DeviceContext _targetBitmap = new D2D1.Bitmap1(d2dContext, _backBuffer); d2dContext.Target = _targetBitmap; // _dcBrush = new D2D1.SolidColorBrush(d2dContext, Color.Black); }
void InitializeDeviceResources() { DXGI.ModeDescription backBufferDesc = new DXGI.ModeDescription(m_width, m_height, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm); DXGI.SwapChainDescription swapChainDesc = new DXGI.SwapChainDescription() { ModeDescription = backBufferDesc, SampleDescription = new DXGI.SampleDescription(1, 0), Usage = DXGI.Usage.RenderTargetOutput, BufferCount = 1, OutputHandle = m_renderForm.Handle, IsWindowed = true, }; D3D11.Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc, out m_d3d11Device, out m_swapChain); m_d3d11DeviceContext = m_d3d11Device.ImmediateContext; using (D3D11.Texture2D backBufer = m_swapChain.GetBackBuffer <D3D11.Texture2D>(0)) { m_renderTargetView = new D3D11.RenderTargetView(m_d3d11Device, backBufer); } m_viewPort = new SharpDX.Viewport(0, 0, m_width, m_height); m_d3d11DeviceContext.Rasterizer.SetViewport(m_viewPort); }
protected void InitializeDirectXResources() { ScaleFactor = (float)GraphicsUtils.Scale; var clientSize = ClientSize; var backBufferDesc = new DXGI.ModeDescription(clientSize.Width, clientSize.Height, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm); var swapChainDesc = new DXGI.SwapChainDescription() { ModeDescription = backBufferDesc, SampleDescription = new DXGI.SampleDescription(1, 0), Usage = DXGI.Usage.RenderTargetOutput, BufferCount = 1, OutputHandle = Handle, SwapEffect = DXGI.SwapEffect.Discard, IsWindowed = Paradigm.Config.Test.Debug }; D3D11.Device.CreateWithSwapChain(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.BgraSupport, new[] { D3D.FeatureLevel.Level_10_0 }, swapChainDesc, out D3DDevice, out var swapChain); D3DDeviceContext = D3DDevice.ImmediateContext; SwapChain = new DXGI.SwapChain1(swapChain.NativePointer); D2DFactory = new D2D1.Factory(); using (var backBuffer = SwapChain.GetBackBuffer <D3D11.Texture2D>(0)) { RenderTargetView = new D3D11.RenderTargetView(D3DDevice, backBuffer); RenderTarget = new D2D1.RenderTarget(D2DFactory, backBuffer.QueryInterface <DXGI.Surface>(), new D2D1.RenderTargetProperties(new D2D1.PixelFormat(DXGI.Format.Unknown, D2D1.AlphaMode.Premultiplied))) { TextAntialiasMode = D2D1.TextAntialiasMode.Cleartype }; } DwFactory = new DW.Factory(DW.FactoryType.Shared); _customColorRenderer.AssignResources(RenderTarget, ForegroundBrush); CueTextFormat = new DW.TextFormat(DwFactory, "Arial", DW.FontWeight.Bold, DW.FontStyle.Normal, DW.FontStretch.Normal, 120 * ScaleFactor) { TextAlignment = DW.TextAlignment.Center, ParagraphAlignment = DW.ParagraphAlignment.Center }; SubtitleTextFormat = new DW.TextFormat(DwFactory, "Consolas", DW.FontWeight.Light, DW.FontStyle.Normal, DW.FontStretch.Normal, Paradigm.Config.Gui.InputTextFontSize * ScaleFactor / 2) { TextAlignment = DW.TextAlignment.Center, ParagraphAlignment = DW.ParagraphAlignment.Center }; ButtonLabelTextFormat = new DW.TextFormat(DwFactory, "Consolas", DW.FontWeight.Bold, DW.FontStyle.Normal, DW.FontStretch.Normal, Paradigm.Config.Gui.ButtonFontSize * ScaleFactor) { TextAlignment = DW.TextAlignment.Center, ParagraphAlignment = DW.ParagraphAlignment.Center }; InputTextFormat = new DW.TextFormat(DwFactory, "Consolas", DW.FontWeight.Bold, DW.FontStyle.Normal, DW.FontStretch.Normal, Paradigm.Config.Gui.InputTextFontSize * ScaleFactor) { TextAlignment = DW.TextAlignment.Leading, ParagraphAlignment = DW.ParagraphAlignment.Center }; SharedBrush = new D2D1.SolidColorBrush(RenderTarget, Color.White); BackgroundBrush = new D2D1.SolidColorBrush(RenderTarget, BackgroundColor); ForegroundBrush = new D2D1.SolidColorBrush(RenderTarget, ForegroundColor); CorrectColorBrush = new D2D1.SolidColorBrush(RenderTarget, CorrectTextColor); WrongColorBrush = new D2D1.SolidColorBrush(RenderTarget, WrongTextColor); PostInitDirectXResources(); }
/// <summary> /// Function to intialize the swap chain. /// </summary> internal void Initialize() { var D3DSettings = new GI.SwapChainDescription(); // Resize the window to match requested mode size. if ((_parentForm == Settings.Window) && (Settings.IsWindowed) && (!Settings.NoClientResize)) { _parentForm.ClientSize = new Size(Settings.VideoMode.Width, Settings.VideoMode.Height); } AutoResize = !Settings.NoClientResize; Graphics.GetFullScreenSwapChains(); D3DSettings.BufferCount = Settings.BufferCount; D3DSettings.Flags = GI.SwapChainFlags.AllowModeSwitch; D3DSettings.IsWindowed = true; D3DSettings.ModeDescription = GorgonVideoMode.Convert(Settings.VideoMode); D3DSettings.OutputHandle = Settings.Window.Handle; D3DSettings.SampleDescription = GorgonMultisampling.Convert(Settings.Multisampling); D3DSettings.SwapEffect = GorgonSwapChainSettings.Convert(Settings.SwapEffect); if ((Settings.Flags & SwapChainUsageFlags.RenderTarget) == SwapChainUsageFlags.RenderTarget) { D3DSettings.Usage = GI.Usage.RenderTargetOutput; } if ((Settings.Flags & SwapChainUsageFlags.AllowShaderView) == SwapChainUsageFlags.AllowShaderView) { D3DSettings.Usage |= GI.Usage.ShaderInput; } if ((Settings.Flags & SwapChainUsageFlags.AllowUnorderedAccessView) == SwapChainUsageFlags.AllowUnorderedAccessView) { D3DSettings.Usage |= GI.Usage.UnorderedAccess; } Gorgon.Log.Print("GorgonSwapChain '{0}': Creating D3D11 swap chain...", LoggingLevel.Simple, Name); GISwapChain = new GI.SwapChain(Graphics.GIFactory, Graphics.D3DDevice, D3DSettings) { DebugName = Name + " DXGISwapChain" }; // Due to an issue with winforms and DXGI, we have to manually handle transitions ourselves. Graphics.GIFactory.MakeWindowAssociation(Settings.Window.Handle, GI.WindowAssociationFlags.IgnoreAll); CreateResources(); if (!Settings.IsWindowed) { ModeStateUpdate(); } Settings.Window.Resize += Window_Resize; if (_parentForm == null) { return; } _parentForm.ResizeBegin += _parentForm_ResizeBegin; _parentForm.ResizeEnd += _parentForm_ResizeEnd; }
/// <summary> /// Creates Direct3D11 Device, RenderTargetView, DepthStencilView, Viewport /// </summary> /// <param name="deviceDescription">The device description.</param> /// <exception cref="System.Exception"></exception> /// <exception cref="System.ComponentModel.InvalidEnumArgumentException">deviceDescription.MultiSampleCount</exception> private void Initialize(DeviceDescription deviceDescription) { FeatureLevel[] levels = new FeatureLevel[] { FeatureLevel.Level_10_0, FeatureLevel.Level_10_1, FeatureLevel.Level_11_0, FeatureLevel.Level_11_1, }; d3dDevice = new D3D11.Device(DriverType.Hardware, D3D11.DeviceCreationFlags.Debug, levels); DXGI.ModeDescription backBufferDesc = new DXGI.ModeDescription() { Width = width, Height = height, Format = BackBufferFormat, RefreshRate = new DXGI.Rational(60, 1), Scaling = DXGI.DisplayModeScaling.Unspecified, ScanlineOrdering = DXGI.DisplayModeScanlineOrder.Progressive, }; DXGI.SwapChainDescription swapChainDesc = new DXGI.SwapChainDescription() { BufferCount = 1, Flags = DXGI.SwapChainFlags.None, IsWindowed = !deviceDescription.Fullcreen, ModeDescription = backBufferDesc, OutputHandle = renderControl.Handle, SwapEffect = deviceDescription.SwapEffect, Usage = DXGI.Usage.RenderTargetOutput, }; switch (deviceDescription.MultiSampleCount) { case MultiSampleType.MSAA1: swapChainDesc.SampleDescription = new DXGI.SampleDescription(1, deviceDescription.MultiSampleQuality); break; case MultiSampleType.MSAA2: swapChainDesc.SampleDescription = new DXGI.SampleDescription(2, deviceDescription.MultiSampleQuality); break; case MultiSampleType.MSAA4: swapChainDesc.SampleDescription = new DXGI.SampleDescription(4, deviceDescription.MultiSampleQuality); break; case MultiSampleType.MSAA8: swapChainDesc.SampleDescription = new DXGI.SampleDescription(8, deviceDescription.MultiSampleQuality); break; case MultiSampleType.MSAA16: swapChainDesc.SampleDescription = new DXGI.SampleDescription(16, deviceDescription.MultiSampleQuality); break; case MultiSampleType.Unknown: var samples = SharpDXDevice.CheckMultiSample(); if (samples == null) { throw new Exception(MethodBase.GetCurrentMethod().Name + " Because the MultiSampleCount parameter is [Unknown], the device could not determine the parameter automatically"); } swapChainDesc.SampleDescription = samples.Last(); break; default: throw new System.ComponentModel.InvalidEnumArgumentException("deviceDescription.MultiSampleCount", (int)deviceDescription.MultiSampleCount, typeof(MultiSampleType)); } DXGI.Device device = d3dDevice.QueryInterface <DXGI.Device>(); DXGI.Adapter adapter = device.GetParent <DXGI.Adapter>(); DXGI.Factory factory = adapter.GetParent <DXGI.Factory>(); swapChain = new DXGI.SwapChain(factory, d3dDevice, swapChainDesc); d3dContext = d3dDevice.ImmediateContext; D3D11.RasterizerStateDescription rasterDesc = new D3D11.RasterizerStateDescription() { CullMode = D3D11.CullMode.Back, FillMode = D3D11.FillMode.Solid, IsAntialiasedLineEnabled = true, IsMultisampleEnabled = true, IsDepthClipEnabled = true, }; viewport = new Viewport(0, 0, width, height); this.SetRasterizerState(rasterDesc); d3dRenderTarget = CreateRenderTarget(); d3dDepthStencil = CreateDepthStencil(); }
protected virtual void InternalInitialize() { dpiScale = GetDpiScale(); SurfaceWidth = (int)(ActualWidth < 0 ? 0 : Math.Ceiling(ActualWidth * dpiScale)); SurfaceHeight = (int)(ActualHeight < 0 ? 0 : Math.Ceiling(ActualHeight * dpiScale)); var swapChainDescription = new DXGI.SwapChainDescription { OutputHandle = Hwnd, BufferCount = 2, Flags = DXGI.SwapChainFlags.AllowModeSwitch, IsWindowed = true, ModeDescription = new DXGI.ModeDescription(SurfaceWidth, SurfaceHeight, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm), SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput | DXGI.Usage.Shared }; SharpDX.Direct3D.FeatureLevel[] featureLevels = null; if (VersionHelper.IsWindows10OrGreater()) { featureLevels = new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_12_1, SharpDX.Direct3D.FeatureLevel.Level_12_0, SharpDX.Direct3D.FeatureLevel.Level_11_1, SharpDX.Direct3D.FeatureLevel.Level_11_0, SharpDX.Direct3D.FeatureLevel.Level_10_1, SharpDX.Direct3D.FeatureLevel.Level_10_0, SharpDX.Direct3D.FeatureLevel.Level_9_3, SharpDX.Direct3D.FeatureLevel.Level_9_2, SharpDX.Direct3D.FeatureLevel.Level_9_1 }; } else if (VersionHelper.IsWindows7SP1OrGreater()) { featureLevels = new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_11_1, SharpDX.Direct3D.FeatureLevel.Level_11_0, SharpDX.Direct3D.FeatureLevel.Level_10_1, SharpDX.Direct3D.FeatureLevel.Level_10_0, SharpDX.Direct3D.FeatureLevel.Level_9_3, SharpDX.Direct3D.FeatureLevel.Level_9_2, SharpDX.Direct3D.FeatureLevel.Level_9_1 }; } try { Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport, featureLevels, swapChainDescription, out device, out swapChain); } catch (Exception e) { MessageBox.Show(e.ToString()); } using (var factory = swapChain.GetParent <DXGI.Factory>()) { factory.MakeWindowAssociation(Hwnd, DXGI.WindowAssociationFlags.IgnoreAll); } backBuffer = swapChain.GetBackBuffer <Texture2D>(0); Console.WriteLine(SupportLevel); //backBuffer = D3D11.Resource.FromSwapChain<D3D11.Texture2D>(swapChain, 0); //renderTargetView = new D3D11.RenderTargetView(device, backBuffer); }
private void createSwapChain() { lock (deviceManager.DeviceLock) { var factory = new DXGI.Factory(); var description = new DXGI.SwapChainDescription() { BufferCount = 1, ModeDescription = new DXGI.ModeDescription( control.ClientSize.Width, control.ClientSize.Height, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm), IsWindowed = true, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput, OutputHandle = control.Handle }; swapChain = new DXGI.SwapChain(factory, deviceContext.Device, description); backbuffer = D3D11.Texture2D.FromSwapChain<D3D11.Texture2D>(swapChain, 0); backbufferView = new D3D11.RenderTargetView(deviceContext.Device, backbuffer); } var d2dFactory = deviceManager.Direct2dFactory; var surface = backbuffer.QueryInterface<DXGI.Surface>(); renderTarget = new D2D.RenderTarget( d2dFactory, surface, new D2D.RenderTargetProperties( new D2D.PixelFormat( DXGI.Format.Unknown, D2D.AlphaMode.Premultiplied))); renderTarget.AntialiasMode = D2D.AntialiasMode.Aliased; createViewport(); }
public Renderer(Game game, SharpDX.Windows.RenderForm renderForm) { this.game = game; int width = renderForm.ClientSize.Width, height = renderForm.ClientSize.Height; ResolutionX = width; ResolutionY = height; #region 3d device & context D3D11.DeviceCreationFlags creationFlags = D3D11.DeviceCreationFlags.BgraSupport; #if DEBUG creationFlags |= D3D11.DeviceCreationFlags.Debug; #endif DXGI.SwapChainDescription swapChainDesc = new DXGI.SwapChainDescription() { ModeDescription = new DXGI.ModeDescription(width, height, new DXGI.Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm), SampleDescription = new DXGI.SampleDescription(SampleCount, SampleQuality), Usage = DXGI.Usage.RenderTargetOutput, BufferCount = 1, OutputHandle = renderForm.Handle, IsWindowed = true, SwapEffect = DXGI.SwapEffect.Discard }; D3D11.Device device; D3D11.Device.CreateWithSwapChain(DriverType.Hardware, creationFlags, swapChainDesc, out device, out swapChain); Device = device; Context = Device.ImmediateContext; #endregion #region 2d device & context DXGI.Device dxgiDevice = Device.QueryInterface <D3D11.Device1>().QueryInterface <DXGI.Device2>(); D2DDevice = new D2D1.Device(dxgiDevice); D2DContext = new D2D1.DeviceContext(D2DDevice, D2D1.DeviceContextOptions.None); D2DFactory = D2DDevice.Factory; #endregion #region 2d brushes/fonts Brushes = new Dictionary <string, D2D1.Brush>(); Brushes.Add("Red", new D2D1.SolidColorBrush(D2DContext, Color.Red)); Brushes.Add("Green", new D2D1.SolidColorBrush(D2DContext, Color.Green)); Brushes.Add("Blue", new D2D1.SolidColorBrush(D2DContext, Color.Blue)); Brushes.Add("White", new D2D1.SolidColorBrush(D2DContext, Color.White)); Brushes.Add("Black", new D2D1.SolidColorBrush(D2DContext, Color.Black)); Brushes.Add("TransparentWhite", new D2D1.SolidColorBrush(D2DContext, new Color(1, 1, 1, .5f))); Brushes.Add("TransparentBlack", new D2D1.SolidColorBrush(D2DContext, new Color(0, 0, 0, .5f))); Brushes.Add("LightGray", new D2D1.SolidColorBrush(D2DContext, Color.LightGray)); Brushes.Add("OrangeRed", new D2D1.SolidColorBrush(D2DContext, Color.OrangeRed)); Brushes.Add("CornflowerBlue", new D2D1.SolidColorBrush(D2DContext, Color.CornflowerBlue)); Brushes.Add("Yellow", new D2D1.SolidColorBrush(D2DContext, Color.Yellow)); Brushes.Add("Magenta", new D2D1.SolidColorBrush(D2DContext, Color.Magenta)); Brushes.Add("RosyBrown", new D2D1.SolidColorBrush(D2DContext, Color.RosyBrown)); DashStyle = new D2D1.StrokeStyle(D2DFactory, new D2D1.StrokeStyleProperties() { StartCap = D2D1.CapStyle.Flat, DashCap = D2D1.CapStyle.Round, EndCap = D2D1.CapStyle.Flat, DashStyle = D2D1.DashStyle.Custom, DashOffset = 0, LineJoin = D2D1.LineJoin.Round, MiterLimit = 1 }, new float[] { 4f, 4f }); FontFactory = new DWrite.Factory(); SegoeUI24 = new DWrite.TextFormat(FontFactory, "Segoe UI", 24f); SegoeUI14 = new DWrite.TextFormat(FontFactory, "Segoe UI", 14f); Consolas14 = new DWrite.TextFormat(FontFactory, "Consolas", 14f); #endregion #region blend states D3D11.BlendStateDescription opaqueDesc = new D3D11.BlendStateDescription(); opaqueDesc.RenderTarget[0].IsBlendEnabled = false; opaqueDesc.RenderTarget[0].RenderTargetWriteMask = D3D11.ColorWriteMaskFlags.All; blendStateOpaque = new D3D11.BlendState(Device, opaqueDesc); D3D11.BlendStateDescription alphaDesc = new D3D11.BlendStateDescription(); alphaDesc.RenderTarget[0].IsBlendEnabled = true; alphaDesc.RenderTarget[0].SourceBlend = D3D11.BlendOption.SourceAlpha; alphaDesc.RenderTarget[0].DestinationBlend = D3D11.BlendOption.InverseSourceAlpha; alphaDesc.RenderTarget[0].BlendOperation = D3D11.BlendOperation.Add; alphaDesc.RenderTarget[0].SourceAlphaBlend = D3D11.BlendOption.One; alphaDesc.RenderTarget[0].DestinationAlphaBlend = D3D11.BlendOption.Zero; alphaDesc.RenderTarget[0].AlphaBlendOperation = D3D11.BlendOperation.Add; alphaDesc.RenderTarget[0].RenderTargetWriteMask = D3D11.ColorWriteMaskFlags.All; blendStateTransparent = new D3D11.BlendState(Device, alphaDesc); #endregion #region rasterizer states rasterizerStateSolidCullBack = new D3D11.RasterizerState(Device, new D3D11.RasterizerStateDescription() { FillMode = D3D11.FillMode.Solid, CullMode = D3D11.CullMode.Back, IsAntialiasedLineEnabled = true, IsDepthClipEnabled = false, IsMultisampleEnabled = true }); rasterizerStateWireframeCullBack = new D3D11.RasterizerState(Device, new D3D11.RasterizerStateDescription() { FillMode = D3D11.FillMode.Wireframe, CullMode = D3D11.CullMode.Back, IsAntialiasedLineEnabled = true, IsDepthClipEnabled = false, IsMultisampleEnabled = true }); rasterizerStateSolidNoCull = new D3D11.RasterizerState(Device, new D3D11.RasterizerStateDescription() { FillMode = D3D11.FillMode.Solid, CullMode = D3D11.CullMode.None, IsAntialiasedLineEnabled = true, IsDepthClipEnabled = false, IsMultisampleEnabled = true }); rasterizerStateWireframeNoCull = new D3D11.RasterizerState(Device, new D3D11.RasterizerStateDescription() { FillMode = D3D11.FillMode.Wireframe, CullMode = D3D11.CullMode.None, IsAntialiasedLineEnabled = true, IsDepthClipEnabled = false, IsMultisampleEnabled = true }); rasterizerStateSolidCullFront = new D3D11.RasterizerState(Device, new D3D11.RasterizerStateDescription() { FillMode = D3D11.FillMode.Solid, CullMode = D3D11.CullMode.Front, IsAntialiasedLineEnabled = true, IsDepthClipEnabled = false, IsMultisampleEnabled = true }); rasterizerStateWireframeCullFront = new D3D11.RasterizerState(Device, new D3D11.RasterizerStateDescription() { FillMode = D3D11.FillMode.Wireframe, CullMode = D3D11.CullMode.Front, IsAntialiasedLineEnabled = true, IsDepthClipEnabled = false, IsMultisampleEnabled = true }); #endregion #region depth stencil states depthStencilStateDefault = new D3D11.DepthStencilState(Device, new D3D11.DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthComparison = D3D11.Comparison.Less, DepthWriteMask = D3D11.DepthWriteMask.All }); depthStencilStateNoDepth = new D3D11.DepthStencilState(Device, new D3D11.DepthStencilStateDescription() { IsDepthEnabled = false, IsStencilEnabled = false, DepthComparison = D3D11.Comparison.Less, DepthWriteMask = D3D11.DepthWriteMask.All }); Context.OutputMerger.SetDepthStencilState(depthStencilStateDefault); #endregion #region blank textures D3D11.Texture2D wtex = new D3D11.Texture2D(Device, new D3D11.Texture2DDescription() { ArraySize = 1, Width = 1, Height = 1, Format = DXGI.Format.R32G32B32A32_Float, CpuAccessFlags = D3D11.CpuAccessFlags.None, MipLevels = 0, Usage = D3D11.ResourceUsage.Default, SampleDescription = new DXGI.SampleDescription(1, 0), BindFlags = D3D11.BindFlags.ShaderResource, OptionFlags = D3D11.ResourceOptionFlags.None }); Context.UpdateSubresource(new Vector4[] { Vector4.One }, wtex); WhiteTextureView = new D3D11.ShaderResourceView(Device, wtex); D3D11.Texture2D btex = new D3D11.Texture2D(Device, new D3D11.Texture2DDescription() { ArraySize = 1, Width = 1, Height = 1, Format = DXGI.Format.R32G32B32A32_Float, CpuAccessFlags = D3D11.CpuAccessFlags.None, MipLevels = 0, Usage = D3D11.ResourceUsage.Default, SampleDescription = new DXGI.SampleDescription(1, 0), BindFlags = D3D11.BindFlags.ShaderResource, OptionFlags = D3D11.ResourceOptionFlags.None }); Context.UpdateSubresource(new Vector4[] { new Vector4(0, 0, 0, 1) }, btex); BlackTextureView = new D3D11.ShaderResourceView(Device, btex); AnisotropicSampler = new D3D11.SamplerState(Device, new D3D11.SamplerStateDescription() { AddressU = D3D11.TextureAddressMode.Wrap, AddressV = D3D11.TextureAddressMode.Wrap, AddressW = D3D11.TextureAddressMode.Wrap, Filter = D3D11.Filter.Anisotropic, }); #endregion #region screen vertex & constants constants = new CameraConstants(); constantBuffer = D3D11.Buffer.Create(Device, D3D11.BindFlags.ConstantBuffer, ref constants); #endregion //swapChain.GetParent<DXGI.Factory>().MakeWindowAssociation(renderForm.Handle, DXGI.WindowAssociationFlags.); Cameras = new List <Camera>(); MainCamera = Camera.CreatePerspective(MathUtil.DegreesToRadians(70), 16 / 9f); ActiveCamera = MainCamera; Cameras.Add(MainCamera); ShadowCamera = Camera.CreateOrthographic(500, 1); ShadowCamera.zNear = 0; ShadowCamera.zFar = 1000; ShadowCamera.CreateResources(Device, 1, 0, 1024, 1024); //Cameras.Add(ShadowCamera); // TODO: Shadow camera has no depth Resize(ResolutionX, ResolutionY); }
private List<IntPtr> GetProcAddress() { var address = new List<IntPtr>(); _device12 = new SharpDX.Direct3D12.Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var renderForm = new Form()) { using (var factory = new SharpDX.DXGI.Factory4()) { _commandQueue = _device12.CreateCommandQueue(new SharpDX.Direct3D12.CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct)); _commandAllocator = _device12.CreateCommandAllocator(CommandListType.Direct); _commandList = _device12.CreateCommandList(CommandListType.Direct, _commandAllocator, null); var swapChainDesc = new SharpDX.DXGI.SwapChainDescription() { BufferCount = 2, ModeDescription = new SharpDX.DXGI.ModeDescription(100, 100, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm), Usage = SharpDX.DXGI.Usage.RenderTargetOutput, SwapEffect = SharpDX.DXGI.SwapEffect.FlipDiscard, OutputHandle = renderForm.Handle, Flags = SwapChainFlags.AllowModeSwitch, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), IsWindowed = true }; var tempSwapChain = new SharpDX.DXGI.SwapChain(factory, _commandQueue, swapChainDesc); _swapChain = tempSwapChain.QueryInterface<SharpDX.DXGI.SwapChain3>(); tempSwapChain.Dispose(); } if (_device12 != null && _swapChain != null) { address.AddRange(GetVTblAddresses(_device12.NativePointer, 44)); address.AddRange(GetVTblAddresses(_commandQueue.NativePointer, 19)); address.AddRange(GetVTblAddresses(_commandAllocator.NativePointer, 9)); address.AddRange(GetVTblAddresses(_commandList.NativePointer, 60)); address.AddRange(GetVTblAddresses(_swapChain.NativePointer, 18)); _device12.Dispose(); _device12 = null; _commandQueue.Dispose(); _commandQueue = null; _commandAllocator.Dispose(); _commandAllocator = null; _commandList.Dispose(); _commandList = null; _swapChain.Dispose(); _swapChain = null; } } return address; }
static void LoadCache() { _Cache.Clear(); try { Swap.SwapChain swapChain; D3D11.Device d3dDevice; using (var form = new SharpDX.Windows.RenderForm()) { var _Description = new Swap.SwapChainDescription { BufferCount = 1, Flags = Swap.SwapChainFlags.None, IsWindowed = true, ModeDescription = new Swap.ModeDescription(100, 100, new Swap.Rational(60, 1), Swap.Format.R8G8B8A8_UNorm), OutputHandle = form.Handle, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), SwapEffect = SharpDX.DXGI.SwapEffect.Discard, Usage = SharpDX.DXGI.Usage.RenderTargetOutput }; D3D11.Device.CreateWithSwapChain( SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.None, _Description, out d3dDevice, out swapChain ); _Cache.AddRange(VTable.LookupAddresses(swapChain.NativePointer, _MethodCount)); d3dDevice.Dispose(); swapChain.Dispose(); d3dDevice = null; swapChain = null; Diagnostics.Assert(_Cache.Count == _MethodCount, "Failed to generate address list for DXGI SwapChain, not sure why"); } } catch (Exception ex) { Diagnostics.Assert(_Cache.Count == _MethodCount, "Failed to generate address list for DXGI SwapChain due to an exception.", ex.Message); } }