public override void Initialize(DeviceManager deviceManager) { base.Initialize(deviceManager); // Save the context instance this.deviceContext = deviceManager.DeviceDirect3D.ImmediateContext1; // We have to take into account pixel scaling; Windows Phone 8.1 uses virtual resolutions smaller than the physical screen size. float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f; // Properties of the swap chain SharpDX.DXGI.SwapChainDescription1 swapChainDescription = new SharpDX.DXGI.SwapChainDescription1() { // No transparency. AlphaMode = SharpDX.DXGI.AlphaMode.Ignore, // Double buffer. BufferCount = 2, // BGRA 32bit pixel format. Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, // Unlike in CoreWindow swap chains, the dimensions must be set. Height = (int)(this.swapChainPanel.RenderSize.Height * pixelScale), Width = (int)(this.swapChainPanel.RenderSize.Width * pixelScale), // Default multisampling. SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), // In case the control is resized, stretch the swap chain accordingly. Scaling = SharpDX.DXGI.Scaling.Stretch, // No support for stereo display. Stereo = false, // Sequential displaying for double buffering. SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential, // This swapchain is going to be used as the back buffer. Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput, }; // Retrive the DXGI device associated to the Direct3D device. using (SharpDX.DXGI.Device3 dxgiDevice3 = deviceManager.DeviceDirect3D.QueryInterface <SharpDX.DXGI.Device3>()) { // Get the DXGI factory automatically created when initializing the Direct3D device. using (SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <SharpDX.DXGI.Factory3>()) { // Create the swap chain and get the highest version available. using (SharpDX.DXGI.SwapChain1 swapChain1 = new SharpDX.DXGI.SwapChain1(dxgiFactory3, deviceManager.DeviceDirect3D, ref swapChainDescription)) { this.swapChain = swapChain1.QueryInterface <SharpDX.DXGI.SwapChain2>(); } } } // Obtain a reference to the native COM object of the SwapChainPanel. using (SharpDX.DXGI.ISwapChainPanelNative nativeObject = SharpDX.ComObject.As <SharpDX.DXGI.ISwapChainPanelNative>(this.swapChainPanel)) { // Set its swap chain. nativeObject.SwapChain = this.swapChain; } // Create a descriptor for the depth/stencil buffer. // Allocate a 2-D surface as the depth/stencil buffer. // Create a DepthStencil view on this surface to use on bind. // TODO: Recreate a DepthStencilBuffer is inefficient. We should only have one depth buffer. Shared depth buffer? using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(DeviceManager.DeviceDirect3D, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)swapChainDescription.Width, Height = (int)swapChainDescription.Height, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, })) this.depthStencilView = Collect(new SharpDX.Direct3D11.DepthStencilView(DeviceManager.DeviceDirect3D, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D })); // Create a Texture2D from the existing swap chain to use as this.backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain <SharpDX.Direct3D11.Texture2D>(this.swapChain, 0); this.renderTargetView = new SharpDX.Direct3D11.RenderTargetView(deviceManager.DeviceDirect3D, this.backBuffer); var viewport = new SharpDX.ViewportF(0, 0, (float)swapChainDescription.Width, (float)swapChainDescription.Height, 0.0f, 1.0f); RenderTargetBounds = new Rect(viewport.X, viewport.Y, viewport.Width, viewport.Height); //DeviceManager.ContextDirect2D.Target = this.backBuffer.as; DeviceManager.ContextDirect3D.Rasterizer.SetViewport(viewport); }
private void LoadPipeline() { viewport.Width = this.width; viewport.Height = this.height; viewport.MaxDepth = 1.0f; scissorRect.Right = this.width; scissorRect.Bottom = this.height; #if DEBUG // Enable the D3D12 debug layer. { DebugInterface.Get().EnableDebugLayer(); } #endif var fact = new SharpDX.DXGI.Factory1(); SharpDX.DXGI.Adapter adapter = fact.GetAdapter(1); // create device using (var defaultDevice = new Device(adapter, SharpDX.Direct3D.FeatureLevel.Level_12_1)) device = defaultDevice.QueryInterface <SharpDX.Direct3D12.Device2>(); using (var factory = new SharpDX.DXGI.Factory4()) { // Describe and create the command queue. var queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. var swapChainDesc = new SharpDX.DXGI.SwapChainDescription1() { BufferCount = FrameCount, Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, Height = height, Width = width, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), Scaling = SharpDX.DXGI.Scaling.Stretch, Stereo = false, SwapEffect = SharpDX.DXGI.SwapEffect.FlipDiscard, Usage = SharpDX.DXGI.Usage.RenderTargetOutput, }; var tempSwapChain = new SharpDX.DXGI.SwapChain1(factory, commandQueue, ref swapChainDesc); swapChain = tempSwapChain.QueryInterface <SharpDX.DXGI.SwapChain3>(); tempSwapChain.Dispose(); frameIndex = swapChain.CurrentBackBufferIndex; using (SharpDX.DXGI.ISwapChainPanelNative nativeObject = ComObject.As <SharpDX.DXGI.ISwapChainPanelNative>(swapChainPanel)) nativeObject.SwapChain = swapChain; } // Create descriptor heaps. // Describe and create a render target view (RTV) descriptor heap. var rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); // Create frame resources. var rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer <Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtvHandle); rtvHandle += rtvDescriptorSize; } commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); }
public static D3DRenderContext CreateFromSwapChainPanel(SwapChainPanel swp) { SharpDX.Direct3D11.Device device; SharpDX.DXGI.SwapChain swapChain; // Create a new Direct3D hardware device and ask for Direct3D 11.2 support using (SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.Debug)) { device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device2>(); } // We have to take into account pixel scaling; Windows Phone 8.1 uses virtual resolutions smaller than the physical screen size. float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f; // Properties of the swap chain SharpDX.DXGI.SwapChainDescription1 swapChainDescription = new SharpDX.DXGI.SwapChainDescription1() { // No transparency. AlphaMode = SharpDX.DXGI.AlphaMode.Ignore, // Double buffer. BufferCount = 2, // BGRA 32bit pixel format. Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, // Unlike in CoreWindow swap chains, the dimensions must be set. Height = (int)(swp.RenderSize.Height * pixelScale), Width = (int)(swp.RenderSize.Width * pixelScale), // Default multisampling. SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), // In case the control is resized, stretch the swap chain accordingly. Scaling = SharpDX.DXGI.Scaling.Stretch, // No support for stereo display. Stereo = false, // Sequential displaying for double buffering. SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential, // This swapchain is going to be used as the back buffer. Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput, }; // Retrive the SharpDX.DXGI device associated to the Direct3D device. using (SharpDX.DXGI.Device3 dxgiDevice3 = device.QueryInterface <SharpDX.DXGI.Device3>()) { // Get the SharpDX.DXGI factory automatically created when initializing the Direct3D device. using (SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <SharpDX.DXGI.Factory3>()) { // Create the swap chain and get the highest version available. using (SharpDX.DXGI.SwapChain1 swapChain1 = new SharpDX.DXGI.SwapChain1(dxgiFactory3, device, ref swapChainDescription)) { swapChain = swapChain1.QueryInterface <SharpDX.DXGI.SwapChain2>(); } } } // Obtain a reference to the native COM object of the SwapChainPanel. using (SharpDX.DXGI.ISwapChainPanelNative nativeObject = ComObject.As <SharpDX.DXGI.ISwapChainPanelNative>(swp)) { // Set its swap chain. nativeObject.SwapChain = swapChain; } var window = new UwpWindow(swp, pixelScale); return(new UwpRenderContext(window, device, swapChain, swp)); }