protected override void CreateDeviceCompatibleTexture(int width, int height, IDisposable texture10, out IDisposable texture11, out IDisposable srv11) { var texture = (global::SharpDX.Direct3D11.Texture2D)texture10; var device11 = ((SpriteRenderer)Sprite).Device; lock (device11) { var dxgiResource = texture.QueryInterface <global::SharpDX.DXGI.Resource>(); global::SharpDX.Direct3D11.Texture2D tex11; if (PixCompatible) { tex11 = new global::SharpDX.Direct3D11.Texture2D(device11, new global::SharpDX.Direct3D11.Texture2DDescription() { ArraySize = 1, BindFlags = global::SharpDX.Direct3D11.BindFlags.ShaderResource | global::SharpDX.Direct3D11.BindFlags.RenderTarget, CpuAccessFlags = global::SharpDX.Direct3D11.CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = global::SharpDX.Direct3D11.ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0), Usage = global::SharpDX.Direct3D11.ResourceUsage.Default }); } else { tex11 = device11.OpenSharedResource <global::SharpDX.Direct3D11.Texture2D>(dxgiResource.SharedHandle); } srv11 = new global::SharpDX.Direct3D11.ShaderResourceView(device11, tex11); texture11 = tex11; dxgiResource.Dispose(); } }
public static IntPtr GetSharedHandle(this global::SharpDX.Direct3D11.Texture2D texture) { var resource = texture.QueryInterface <global::SharpDX.DXGI.Resource>(); var result = resource.SharedHandle; resource.Dispose(); return(result); }
internal void SetRenderTargetDX10(global::SharpDX.Direct3D11.Texture2D renderTarget) { if (_renderTarget != null) { _renderTarget = null; Lock(); SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); Unlock(); } if (renderTarget == null) { return; } if (!renderTarget.IsShareable()) { throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared"); } var format = renderTarget.GetTranslatedFormat(); if (format == Format.Unknown) { throw new ArgumentException("Texture format is not compatible with OpenSharedResource"); } var handle = renderTarget.GetSharedHandle(); if (handle == IntPtr.Zero) { throw new ArgumentException("Handle could not be retrieved"); } _renderTarget = new Texture(DeviceService.D3DDevice, renderTarget.Description.Width, renderTarget.Description.Height, 1, Usage.RenderTarget, format, Pool.Default, ref handle); using (Surface surface = _renderTarget.GetSurfaceLevel(0)) { Lock(); SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); Unlock(); } }
public static Format GetTranslatedFormat(this global::SharpDX.Direct3D11.Texture2D texture) { switch (texture.Description.Format) { case global::SharpDX.DXGI.Format.R10G10B10A2_UNorm: return(Format.A2B10G10R10); case global::SharpDX.DXGI.Format.R16G16B16A16_Float: return(Format.A16B16G16R16F); case global::SharpDX.DXGI.Format.B8G8R8A8_UNorm: return(Format.A8R8G8B8); default: return(Format.Unknown); } }
public static bool IsShareable(this global::SharpDX.Direct3D11.Texture2D texture) { return(texture.Description.OptionFlags.HasFlag(global::SharpDX.Direct3D11.ResourceOptionFlags.Shared)); }
/// <inveritdoc/> public override void RenderAll() { SurfaceViewData viewData; var regionToDraw = new Rectangle(0, 0, pixelWidth, pixelHeight); // Unlike other targets, we can only get the DXGI surface to render to // just before rendering. global::SharpDX.Mathematics.Interop.RawPoint rawPoint; using (var surface = surfaceImageSourceNative.BeginDraw(regionToDraw, out rawPoint)) { position = rawPoint; // Cache DXGI surface in order to avoid recreate all render target view, depth stencil...etc. // Is it the right way to do it? // It seems that ISurfaceImageSourceNative.BeginDraw is returning 2 different DXGI surfaces if (!mapSurfaces.TryGetValue(surface.NativePointer, out viewData)) { viewData = new SurfaceViewData(); mapSurfaces.Add(surface.NativePointer, viewData); // Allocate a new renderTargetView if size is different // Cache the rendertarget dimensions in our helper class for convenient use. viewData.BackBuffer = Collect(surface.QueryInterface <global::SharpDX.Direct3D11.Texture2D>()); { var desc = viewData.BackBuffer.Description; viewData.RenderTargetSize = new Size(desc.Width, desc.Height); viewData.RenderTargetView = Collect(new global::SharpDX.Direct3D11.RenderTargetView(DeviceManager.DeviceDirect3D, viewData.BackBuffer)); } // 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 global::SharpDX.Direct3D11.Texture2D(DeviceManager.DeviceDirect3D, new global::SharpDX.Direct3D11.Texture2DDescription() { Format = global::SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)viewData.RenderTargetSize.Width, Height = (int)viewData.RenderTargetSize.Height, SampleDescription = new global::SharpDX.DXGI.SampleDescription(1, 0), BindFlags = global::SharpDX.Direct3D11.BindFlags.DepthStencil, })) viewData.DepthStencilView = Collect(new global::SharpDX.Direct3D11.DepthStencilView(DeviceManager.DeviceDirect3D, depthBuffer, new global::SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = global::SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D })); // Now we set up the Direct2D render target bitmap linked to the swapchain. // Whenever we render to this bitmap, it will be directly rendered to the // swapchain associated with the window. var bitmapProperties = new global::SharpDX.Direct2D1.BitmapProperties1( new global::SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, global::SharpDX.Direct2D1.AlphaMode.Premultiplied), DeviceManager.Dpi, DeviceManager.Dpi, global::SharpDX.Direct2D1.BitmapOptions.Target | global::SharpDX.Direct2D1.BitmapOptions.CannotDraw); // Direct2D needs the dxgi version of the backbuffer surface pointer. // Get a D2D surface from the DXGI back buffer to use as the D2D render target. viewData.BitmapTarget = Collect(new global::SharpDX.Direct2D1.Bitmap1(DeviceManager.ContextDirect2D, surface, bitmapProperties)); // Create a viewport descriptor of the full window size. viewData.Viewport = new global::SharpDX.ViewportF(position.X, position.Y, (float)viewData.RenderTargetSize.Width - position.X, (float)viewData.RenderTargetSize.Height - position.Y, 0.0f, 1.0f); } backBuffer = viewData.BackBuffer; renderTargetView = viewData.RenderTargetView; depthStencilView = viewData.DepthStencilView; RenderTargetBounds = new Rect(viewData.Viewport.X, viewData.Viewport.Y, viewData.Viewport.Width, viewData.Viewport.Height); bitmapTarget = viewData.BitmapTarget; DeviceManager.ContextDirect2D.Target = viewData.BitmapTarget; // Set the current viewport using the descriptor. DeviceManager.ContextDirect3D.Rasterizer.SetViewport(viewData.Viewport); // Perform the actual rendering of this target base.RenderAll(); } surfaceImageSourceNative.EndDraw(); }
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 global::SharpDX.DXGI.SwapChainDescription1 swapChainDescription = new global::SharpDX.DXGI.SwapChainDescription1() { // No transparency. AlphaMode = global::SharpDX.DXGI.AlphaMode.Ignore, // Double buffer. BufferCount = 2, // BGRA 32bit pixel format. Format = global::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 global::SharpDX.DXGI.SampleDescription(1, 0), // In case the control is resized, stretch the swap chain accordingly. Scaling = global::SharpDX.DXGI.Scaling.Stretch, // No support for stereo display. Stereo = false, // Sequential displaying for double buffering. SwapEffect = global::SharpDX.DXGI.SwapEffect.FlipSequential, // This swapchain is going to be used as the back buffer. Usage = global::SharpDX.DXGI.Usage.BackBuffer | global::SharpDX.DXGI.Usage.RenderTargetOutput, }; // Retrive the DXGI device associated to the Direct3D device. using (global::SharpDX.DXGI.Device3 dxgiDevice3 = deviceManager.DeviceDirect3D.QueryInterface <global::SharpDX.DXGI.Device3>()) { // Get the DXGI factory automatically created when initializing the Direct3D device. using (global::SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <global::SharpDX.DXGI.Factory3>()) { // Create the swap chain and get the highest version available. using (global::SharpDX.DXGI.SwapChain1 swapChain1 = new global::SharpDX.DXGI.SwapChain1(dxgiFactory3, deviceManager.DeviceDirect3D, ref swapChainDescription)) { this.swapChain = swapChain1.QueryInterface <global::SharpDX.DXGI.SwapChain2>(); } } } // Obtain a reference to the native COM object of the SwapChainPanel. using (global::SharpDX.DXGI.ISwapChainPanelNative nativeObject = global::SharpDX.ComObject.As <global::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 global::SharpDX.Direct3D11.Texture2D(DeviceManager.DeviceDirect3D, new global::SharpDX.Direct3D11.Texture2DDescription() { Format = global::SharpDX.DXGI.Format.D24_UNorm_S8_UInt, ArraySize = 1, MipLevels = 1, Width = (int)swapChainDescription.Width, Height = (int)swapChainDescription.Height, SampleDescription = new global::SharpDX.DXGI.SampleDescription(1, 0), BindFlags = global::SharpDX.Direct3D11.BindFlags.DepthStencil, })) this.depthStencilView = Collect(new global::SharpDX.Direct3D11.DepthStencilView(DeviceManager.DeviceDirect3D, depthBuffer, new global::SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = global::SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D })); // Create a Texture2D from the existing swap chain to use as this.backBuffer = global::SharpDX.Direct3D11.Texture2D.FromSwapChain <global::SharpDX.Direct3D11.Texture2D>(this.swapChain, 0); this.renderTargetView = new global::SharpDX.Direct3D11.RenderTargetView(deviceManager.DeviceDirect3D, this.backBuffer); var viewport = new global::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); }