示例#1
0
        private void _updateDimensions(double width, double height)
        {
            _appWidth  = (float)width + 5;
            _appHeight = (float)height;

            if (_deviceManager == null)
            {
                return;
            }
            if (_stagingTexture2D != null)
            {
                _stagingTexture2D.Dispose();
            }
            if (_stagingBitmap != null)
            {
                _stagingBitmap.Dispose();
            }
            if (_stagingBitmapSourceEffect != null)
            {
                _stagingBitmapSourceEffect.Dispose();
            }

            _stagingTexture2D          = AllocateTextureReturnSurface((int)_appWidth, (int)_appHeight);
            _stagingBitmap             = new SharpDX.Direct2D1.Bitmap1(_deviceManager.ContextDirect2D, _stagingTexture2D.QueryInterface <SharpDX.DXGI.Surface>());
            _stagingBitmapSourceEffect = new SharpDX.Direct2D1.Effects.BitmapSource(_deviceManager.ContextDirect2D);
        }
        /// <summary>
        /// Saves the contents of a <see cref="SharpDX.Direct3D11.Texture2D"/> to a file with name contained in <paramref name="filename"/> using the specified <see cref="System.Drawing.Imaging.ImageFormat"/>.
        /// </summary>
        /// <param name="texture">The <see cref="SharpDX.Direct3D11.Texture2D"/> containing the data to save.</param>
        /// <param name="filename">The filename on disk where the output image should be saved.</param>
        /// <param name="imageFormat">The format to use when saving the output file.</param>
        public void SaveTextureToFile(SharpDX.Direct3D11.Texture2D texture, string filename, System.Drawing.Imaging.ImageFormat imageFormat)
        {
            // If the existing debug texture doesn't exist, or the incoming texture is different than the existing debug texture...
            if (m_pDebugTexture == null || !TextureCanBeCopied(m_pDebugTexture, texture))
            {
                // Dispose of any existing texture
                if (m_pDebugTexture != null)
                {
                    m_pDebugTexture.Dispose();
                }

                // Copy the original texture's description...
                SharpDX.Direct3D11.Texture2DDescription newDescription = texture.Description;

                // Then modify the parameters to create a CPU-readable staging texture
                newDescription.BindFlags      = SharpDX.Direct3D11.BindFlags.None;
                newDescription.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Read;
                newDescription.OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None;
                newDescription.Usage          = SharpDX.Direct3D11.ResourceUsage.Staging;

                // Re-generate the debug texture by copying the new texture's description
                m_pDebugTexture = new SharpDX.Direct3D11.Texture2D(m_pDevice, newDescription);
            }

            // Copy the texture to our debug texture
            m_pDevice.ImmediateContext.CopyResource(texture, m_pDebugTexture);

            // Map the debug texture's resource 0 for read mode
            SharpDX.DataStream data;
            SharpDX.DataBox    dbox = m_pDevice.ImmediateContext.MapSubresource(m_pDebugTexture, 0, 0, SharpDX.Direct3D11.MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out data);

            // Create a bitmap that's the same size as the debug texture
            Bitmap b = new Bitmap(m_pDebugTexture.Description.Width, m_pDebugTexture.Description.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            // Lock the bitmap data to get access to the native bitmap pointer
            System.Drawing.Imaging.BitmapData bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            // Use the native pointers to do a native-to-native memory copy from the mapped subresource to the bitmap data
            // WARNING: This might totally blow up if you're using a different color format than B8G8R8A8_UNorm, I don't know how planar formats are structured as D3D textures!
            //
            // You can use Win32 CopyMemory to do the below copy if need be, but you have to do it in a loop to respect the Stride and RowPitch parameters in case the texture width
            // isn't on an aligned byte boundary.
            MediaFoundation.MFExtern.MFCopyImage(bd.Scan0, bd.Stride, dbox.DataPointer, dbox.RowPitch, bd.Width * 4, bd.Height);

            /// Unlock the bitmap
            b.UnlockBits(bd);

            // Unmap the subresource mapping, ignore the SharpDX.DataStream because we don't need it.
            m_pDevice.ImmediateContext.UnmapSubresource(m_pDebugTexture, 0);
            data = null;

            // Save the bitmap to the desired filename
            b.Save(filename, imageFormat);
            b.Dispose();
            b = null;
        }
示例#3
0
        public void Dispose()
        {
            leftVRTexture.Dispose();
            rightVRTexture.Dispose();

            leftHandle.Dispose();
            rightHandle.Dispose();
        }
示例#4
0
 public void Dispose()
 {
     _renderTarget2D?.Dispose();
     _surface?.Dispose();
     _factory2D?.Dispose();
     _textureD3D11.Dispose();
     _renderView?.Dispose();
     _backBuffer?.Dispose();
 }
示例#5
0
        public override void Dispose()
        {
            if (dx11Device != null)
            {
                dx11Device.Dispose();
            }
            if (dx11Factory != null)
            {
                dx11Factory.Dispose();
            }
            if (dx11Output != null)
            {
                dx11Output.Dispose();
            }
            if (dx11DuplicatedOutput != null)
            {
                dx11DuplicatedOutput.Dispose();
            }
            if (dx11ScreenTexture != null)
            {
                dx11ScreenTexture.Dispose();
            }
            if (dx11ScreenResource != null)
            {
                dx11ScreenResource.Dispose();
            }
            if (dx11ScreenSurface != null)
            {
                dx11ScreenSurface.Dispose();
            }

            if (screenShot != null)
            {
                screenShot.Dispose();
            }

            dx11Device           = null;
            dx11Factory          = null;
            dx11Output           = null;
            dx11DuplicatedOutput = null;
            dx11ScreenTexture    = null;
            dx11ScreenResource   = null;
            dx11ScreenSurface    = null;
            screenShot           = null;

            bmpData = null;
            GC.SuppressFinalize(this);
        }
示例#6
0
        public void OnResize(int width, int height)
        {
            if (mRealTexture != null)
            {
                mRealTexture.Dispose();
            }
            if (mTmpTexture != null)
            {
                mTmpTexture.Dispose();
            }

            mRealTexture = new SharpDX.Direct3D11.Texture2D(mDevice.Device, new SharpDX.Direct3D11.Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Height            = height,
                Width             = width,
                MipLevels         = 1,
                OptionFlags       = SharpDX.Direct3D11.ResourceOptionFlags.SharedKeyedmutex,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = SharpDX.Direct3D11.ResourceUsage.Default
            });

            using (var resource = mRealTexture.QueryInterface <SharpDX.DXGI.Resource>())
                mTmpTexture = D2DDevice.OpenSharedResource <Texture2D>(resource.SharedHandle);

            if (NativeView != null)
            {
                NativeView.Dispose();
            }
            NativeView = new SharpDX.Direct3D11.ShaderResourceView(mDevice.Device, mRealTexture,
                                                                   new SharpDX.Direct3D11.ShaderResourceViewDescription
            {
                Format    = Format.B8G8R8A8_UNorm,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource
                {
                    MipLevels       = 1,
                    MostDetailedMip = 0
                }
            });

            if (RenderTarget != null)
            {
                RenderTarget.Dispose();
            }
            using (var surface = mTmpTexture.QueryInterface <Surface>())
                RenderTarget = new RenderTarget(Direct2DFactory, surface, new RenderTargetProperties()
                {
                    DpiX        = 0.0f,
                    DpiY        = 0.0f,
                    MinLevel    = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
                    PixelFormat = new PixelFormat()
                    {
                        AlphaMode = AlphaMode.Premultiplied, Format = Format.Unknown
                    },
                    Type  = RenderTargetType.Hardware,
                    Usage = RenderTargetUsage.None
                });

            if (mMutex10 != null)
            {
                mMutex10.Dispose();
            }
            if (mMutex11 != null)
            {
                mMutex11.Dispose();
            }

            mMutex10 = mTmpTexture.QueryInterface <KeyedMutex>();
            mMutex11 = mRealTexture.QueryInterface <KeyedMutex>();

            Brushes.Initialize(RenderTarget);
            Fonts.Initialize(DirectWriteFactory);

            Button.Initialize();
            Frame.Initialize();

            // right now the texture is unowned and only a key of 0 will succeed.
            // after releasing it with a specific key said key then can be used for
            // further locking.
            mMutex10.Acquire(0, -1);
            mMutex10.Release(Key11);
        }