/// <summary> /// Initializes a new <see cref="SwapChainPanelTarget"/> instance /// </summary> /// <param name="panel">The <see cref="SwapChainPanel"/> to render to</param> public SwapChainPanelTarget(SwapChainPanel panel) { this.panel = panel; // Gets the native panel nativePanel = ComObject.As<ISwapChainPanelNative>(panel); // Register event on Window Size Changed // So that resources dependent size can be resized Window.Current.CoreWindow.SizeChanged += CoreWindow_SizeChanged; }
void CreateSwapChain() { SwapChainDescription1 swapChainDescription = new SwapChainDescription1() { Usage = Usage.RenderTargetOutput, BufferCount = 2, SwapEffect = SwapEffect.FlipSequential, Stereo = false, SampleDescription = new SampleDescription(1, 0), Scaling = Scaling.Stretch, Format = Format.R8G8B8A8_UNorm, Height = 1080, Width = 1920, }; // 建立SwapChain using (SharpDX.DXGI.Device3 dxgiDevice3 = D3D11Device.QueryInterface <SharpDX.DXGI.Device3>()) { using (Factory2 dxgiFactory2 = dxgiDevice3.Adapter.GetParent <Factory2>()) { swapChain1 = new SwapChain1(dxgiFactory2, D3D11Device, ref swapChainDescription); swapChain1.QueryInterface <SwapChain>(); } } // 把Xaml的SwapChainPanel與DirectX的SwapChain連結起來 using (ISwapChainPanelNative swapChainPanelNative = ComObject.As <ISwapChainPanelNative>(this)) { swapChainPanelNative.SwapChain = swapChain1; SetViewport(); } }
/// <summary> /// Initializes a new <see cref="SwapChainPanelTarget"/> instance /// </summary> /// <param name="panel">The <see cref="SwapChainPanel"/> to render to</param> public SwapChainPanelTarget(SwapChainPanel panel) { this.panel = panel; // Gets the native panel nativePanel = ComObject.As <ISwapChainPanelNative>(panel); panel.SizeChanged += panel_SizeChanged; }
/// <summary> /// Initializes a new <see cref="SwapChainPanelTarget"/> instance /// </summary> /// <param name="panel">The <see cref="SwapChainPanel"/> to render to</param> public SwapChainPanelTarget(SwapChainPanel panel) { this.panel = panel; // Gets the native panel nativePanel = ComObject.As<ISwapChainPanelNative>(panel); panel.SizeChanged += panel_SizeChanged; }
public void CreateDirectXSwapChain() { SwapChainDescription1 swapChainDescription = new SwapChainDescription1() { // Want Transparency. AlphaMode = SharpDX.DXGI.AlphaMode.Premultiplied, // Double buffer. BufferCount = 2, // BGRA 32bit pixel format. Format = Format.B8G8R8A8_UNorm, // Unlike in CoreWindow swap chains, the dimensions must be set. Height = (int)(ActualHeight), Width = (int)(ActualWidth), // Default multisampling. SampleDescription = new SampleDescription(1, 0), // In case the control is resized, stretch the swap chain accordingly. Scaling = Scaling.Stretch, // No support for stereo display. Stereo = false, // Sequential displaying for double buffering. SwapEffect = SwapEffect.FlipSequential, // This swapchain is going to be used as the back buffer. Usage = Usage.BackBuffer | Usage.RenderTargetOutput, }; using (SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport)) { this.device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device2>(); } // Save the context instance //this.d3d11DC = this.device.ImmediateContext2; using (SharpDX.DXGI.Device3 dxgiDevice3 = device.QueryInterface <SharpDX.DXGI.Device3>()) { using (SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <SharpDX.DXGI.Factory3>()) { using (SwapChain1 swapChain1 = new SwapChain1(dxgiFactory3, device, ref swapChainDescription)) { swapChain = swapChain1.QueryInterface <SwapChain2>(); } } } using (ISwapChainPanelNative nativeObject = ComObject.As <ISwapChainPanelNative>(this)) { nativeObject.SwapChain = swapChain; } backBufferTexture = SharpDX.Direct3D11.Resource.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0); using (var surface = backBufferTexture.QueryInterface <Surface2>()) { d2d1DC = new DeviceContext(surface); } }
/// <summary> /// Initializes a new <see cref="SwapChainPanelTarget"/> instance /// </summary> /// <param name="panel">The <see cref="SwapChainPanel"/> to render to</param> public SwapChainPanelTarget(SwapChainPanel panel) { this.panel = panel; // Gets the native panel nativePanel = ComObject.As <ISwapChainPanelNative>(panel); // Register event on Window Size Changed // So that resources dependent size can be resized Window.Current.CoreWindow.SizeChanged += CoreWindow_SizeChanged; }
public D3DAppSwapChainPanelTarget(SwapChainPanel panel) { this.panel = panel; nativePanel = ToDispose(ComObject.As <SharpDX.DXGI.ISwapChainPanelNative>(panel)); this.panel.CompositionScaleChanged += (sender, args) => { //SizeChanged(); ScaleChanged(); }; this.panel.SizeChanged += (sender, args) => { SizeChanged(); }; }
public D3DAppSwapChainPanelTarget(SwapChainPanel panel) { this.panel = panel; nativePanel = ToDispose(ComObject.As<SharpDX.DXGI.ISwapChainPanelNative>(panel)); this.panel.CompositionScaleChanged += (sender, args) => { //SizeChanged(); ScaleChanged(); }; this.panel.SizeChanged += (sender, args) => { SizeChanged(); }; }
/// <summary> /// Binds the object to a SwapChainPanel and initializes Direct3D11 resources. /// </summary> /// <param name="panel">SwapChainPanel control used for drawing.</param> public void BindToControl(SwapChainPanel panel) { this.ThrowIfDisposed(); this.ThrowIfBound(); this.panel = panel; this.nativePanel = ComObject.As<ISwapChainPanelNative>(this.panel); this.UpdateBackBufferSize(); this.CreateDeviceDependentResources(); this.CreateSizeDependentResources(); CompositionTarget.Rendering += CompositionTarget_Rendering; this.panel.SizeChanged += HostControl_SizeChanged; DisplayInformation.GetForCurrentView().DpiChanged += DisplayInformation_DpiChanged; this.IsBound = true; }
public void CreateDirectXSwapChain(Adapter adapter) { var desc = adapter.Description; Debug.WriteLine(desc.Description); Debug.WriteLine($"vender = {desc.VendorId:X4}"); Debug.WriteLine($"Shared Memory: {desc.SharedSystemMemory} bytes"); Debug.WriteLine($"Video Memory: {desc.DedicatedVideoMemory} bytes"); Debug.WriteLine($"device: {desc.DeviceId}"); FeatureLevel[] featureLevels = new FeatureLevel[] { FeatureLevel.Level_11_1, FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, }; D3D11Device = new SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.BgraSupport, featureLevels); SwapChainDescription1 swapChainDescription = new SwapChainDescription1() { // Double buffer. BufferCount = 2, // BGRA 32bit pixel format. Format = Format.B8G8R8A8_UNorm, // Unlike in CoreWindow swap chains, the dimensions must be set. Height = (int)(ActualHeight), Width = (int)(ActualWidth), // Default multisampling. SampleDescription = new SampleDescription(1, 0), // In case the control is resized, stretch the swap chain accordingly. Scaling = Scaling.Stretch, // No support for stereo display. Stereo = false, // Sequential displaying for double buffering. SwapEffect = SwapEffect.FlipSequential, // This swapchain is going to be used as the back buffer. Usage = Usage.BackBuffer | Usage.RenderTargetOutput, }; // 建立SwapChain using (SharpDX.DXGI.Device3 dxgiDevice3 = D3D11Device.QueryInterface <SharpDX.DXGI.Device3>()) { using (Factory2 dxgiFactory2 = dxgiDevice3.Adapter.GetParent <Factory2>()) { using (SwapChain1 swapChain1 = new SwapChain1(dxgiFactory2, D3D11Device, ref swapChainDescription)) { swapChain = swapChain1.QueryInterface <SwapChain>(); } } } // 把Xaml的SwapChainPanel與DirectX的SwapChain連結起來 using (ISwapChainPanelNative swapChainPanelNative = ComObject.As <ISwapChainPanelNative>(this)) { swapChainPanelNative.SwapChain = swapChain; } backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain <Texture2D>(swapChain, 0); var renderTarget = new RenderTargetView(D3D11Device, backBuffer); context = D3D11Device.ImmediateContext; // Clear Screen to Teel Blue. context.ClearRenderTargetView(renderTarget, new Color(0xFF887536)); swapChain.Present(1, PresentFlags.None); }
public Window(SwapChainPanel panel, Aiv.Fast2D.UWP.IGame game) { this.context = panel; this.game = game; using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.Debug)) { this.device = defaultDevice.QueryInterface <D3D11.Device2>(); } // Save the context instance this.deviceContext = this.device.ImmediateContext2; // Properties of the swap chain SwapChainDescription1 swapChainDescription = new SwapChainDescription1() { // No transparency. AlphaMode = AlphaMode.Ignore, // Double buffer. BufferCount = 2, // BGRA 32bit pixel format. Format = Format.R8G8B8A8_UNorm, // Unlike in CoreWindow swap chains, the dimensions must be set. Height = (int)(this.context.RenderSize.Height), Width = (int)(this.context.RenderSize.Width), // Default multisampling. SampleDescription = new SampleDescription(1, 0), // In case the control is resized, stretch the swap chain accordingly. Scaling = Scaling.Stretch, // No support for stereo display. Stereo = false, // Sequential displaying for double buffering. SwapEffect = SwapEffect.FlipSequential, // This swapchain is going to be used as the back buffer. Usage = Usage.BackBuffer | Usage.RenderTargetOutput, }; // Retrive the DXGI device associated to the Direct3D device. using (Device3 dxgiDevice3 = this.device.QueryInterface <Device3>()) { // Get the DXGI factory automatically created when initializing the Direct3D device. using (Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <Factory5>()) { // Create the swap chain and get the highest version available. using (SwapChain1 swapChain1 = new SwapChain1(dxgiFactory3, this.device, ref swapChainDescription)) { this.swapChain = swapChain1.QueryInterface <SwapChain2>(); } } } // Obtain a reference to the native COM object of the SwapChainPanel. using (ISwapChainPanelNative nativeObject = ComObject.As <ISwapChainPanelNative>(this.context)) { // Set its swap chain. nativeObject.SwapChain = this.swapChain; } // Create a Texture2D from the existing swap chain to use as D3D11.Texture2D backBufferTexture = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0); this.renderTargetView = new D3D11.RenderTargetView(this.device, backBufferTexture); FinalizeSetup(); width = (int)this.context.RenderSize.Width; height = (int)this.context.RenderSize.Height; scaleX = 1; scaleY = 1; this.SetViewport(0, 0, width, height); // for now disable only backface culling D3D11.RasterizerStateDescription rasterizerDescription = D3D11.RasterizerStateDescription.Default(); rasterizerDescription.CullMode = D3D11.CullMode.None; this.deviceContext.Rasterizer.State = new SharpDX.Direct3D11.RasterizerState(this.device, rasterizerDescription); vsync = 1; CompositionTarget.Rendering += this.Update; this.game.GameSetup(this); watch = new Stopwatch(); }