Exemplo n.º 1
0
        public void Render()
        {
            if (!_isDirty)
            {
                return;
            }
            if (!IsEnabled)
            {
                return;
            }

            _wicRenderTarget.BeginDraw();
            _wicRenderTarget.Clear(_clearColor);
            _wicRenderTarget.DrawText(_text, _textFormat, _rect, _sceneColorBrush);
            _wicRenderTarget.EndDraw();

            var        bitmapLock = _wicBitmap.Lock(SharpDX.WIC.BitmapLockFlags.Read);
            DataStream stream;

            _immediateContext.MapSubresource(_renderTexture, 0, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out stream);
            stream.WriteRange(bitmapLock.Data.DataPointer, _bitmapSize);
            _immediateContext.UnmapSubresource(_renderTexture, 0);
            stream.Dispose();
            bitmapLock.Dispose();

            _isDirty = false;
        }
Exemplo n.º 2
0
        public void OnRender(float framesPerSecond)
        {
            if (_isEnabled == false)
            {
                return;
            }

            if (fps != framesPerSecond)
            {
                fps        = framesPerSecond;
                textString = string.Format("FPS: {0}\n{1}", fps.ToString("0.00", culture), _text);

                _wicRenderTarget.BeginDraw();
                _wicRenderTarget.Clear(clearColor);
                _wicRenderTarget.DrawText(textString, _textFormat, rect, _sceneColorBrush);
                _wicRenderTarget.EndDraw();

                var        bitmapLock = _wicBitmap.Lock(SharpDX.WIC.BitmapLockFlags.Read);
                DataStream stream;
                _immediateContext.MapSubresource(_renderTexture, 0, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out stream);
                stream.WriteRange(bitmapLock.Data.DataPointer, _width * _height * 4);
                _immediateContext.UnmapSubresource(_renderTexture, 0);
                stream.Dispose();
                bitmapLock.Dispose();
            }
        }
Exemplo n.º 3
0
        public static System.Drawing.Bitmap ToSD(this s.WIC.Bitmap bmp)
        {
            using (var bl = bmp.Lock(s.WIC.BitmapLockFlags.Read))
            {
                var pixelFormat = bmp.PixelFormat.ToSDPixelFormat();

                return(new System.Drawing.Bitmap(bmp.Size.Width, bmp.Size.Height, bl.Data.Pitch, pixelFormat, bl.Data.DataPointer));
            }
        }
Exemplo n.º 4
0
        public static SharpDX.WIC.Bitmap SaveToWICBitmap(DeviceManager deviceManager, Texture2D source)
        {
            var device  = deviceManager.Direct3DDevice;
            var context = deviceManager.Direct3DContext;

            var txDesc = source.Description;

            txDesc.Usage             = ResourceUsage.Staging;
            txDesc.CpuAccessFlags    = CpuAccessFlags.Read;
            txDesc.BindFlags         = BindFlags.None;
            txDesc.OptionFlags       = ResourceOptionFlags.None;
            txDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);

            Guid pixelFormat = PixelFormatFromFormat(txDesc.Format);

            if (pixelFormat == Guid.Empty)
            {
                return(null);
            }

            System.Diagnostics.Debug.Assert(BitsPerPixel(txDesc.Format) == SharpDX.WIC.PixelFormat.GetBitsPerPixel(pixelFormat), "Error with DXGI.Format -> PixelFormat");

            using (var dest = new Texture2D(device, txDesc))
            {
                if (source.Description.SampleDescription.Count > 1 || source.Description.SampleDescription.Quality > 0)
                {
                    // In order to copy a multisampled texture to a CPU readable texture, it must first be resolved into a GPU only Texture
                    // Initialize a target to resolve multi-sampled render target
                    var resolvedDesc = source.Description;
                    resolvedDesc.BindFlags         = BindFlags.ShaderResource;
                    resolvedDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);

                    // if depth stencil needs to be typeless
                    if (resolvedDesc.Format == SharpDX.DXGI.Format.D24_UNorm_S8_UInt)
                    {
                        resolvedDesc.Format = SharpDX.DXGI.Format.R24G8_Typeless;
                    }

                    using (var resolvedTarget = new Texture2D(device, resolvedDesc))
                    {
                        CopyToTexture(context, source, resolvedTarget);
                        // Now we can copy to the destination
                        CopyToTexture(context, source, dest);
                    }
                }
                else
                {
                    CopyToTexture(context, source, dest);
                }
                var sourceData = context.MapSubresource(dest, 0, MapMode.Read, MapFlags.None);

                var encoder = new SharpDX.WIC.PngBitmapEncoder(deviceManager.WICFactory);

                var formatConverter = new SharpDX.WIC.FormatConverter(deviceManager.WICFactory);


                SharpDX.WIC.Bitmap bm = new SharpDX.WIC.Bitmap(deviceManager.WICFactory, txDesc.Width, txDesc.Height, pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad);

                var bytesPerPixel = BitsPerPixel(txDesc.Format) / 8;
                using (var l = bm.Lock(SharpDX.WIC.BitmapLockFlags.Write))
                {
                    var destPtr   = l.Data.DataPointer;
                    var sourcePtr = sourceData.DataPointer;
                    for (int y = 0; y < bm.Size.Height; y++)
                    {
                        SharpDX.Utilities.CopyMemory(destPtr, sourcePtr, bm.Size.Width * bytesPerPixel);

                        sourcePtr = IntPtr.Add(sourcePtr, sourceData.RowPitch);
                        destPtr   = IntPtr.Add(destPtr, l.Data.Pitch);
                    }
                }
                context.UnmapSubresource(dest, 0);

                return(bm);
            }
        }
        public static SharpDX.WIC.Bitmap SaveToWICBitmap(DeviceManager deviceManager, Texture2D source)
        {
            var device = deviceManager.Direct3DDevice;
            var context = deviceManager.Direct3DContext;

            var txDesc = source.Description;

            txDesc.Usage = ResourceUsage.Staging;
            txDesc.CpuAccessFlags = CpuAccessFlags.Read;
            txDesc.BindFlags = BindFlags.None;
            txDesc.OptionFlags = ResourceOptionFlags.None;
            txDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);

            Guid pixelFormat = PixelFormatFromFormat(txDesc.Format);
            if (pixelFormat == Guid.Empty)
            {
                return null;
            }

            System.Diagnostics.Debug.Assert(BitsPerPixel(txDesc.Format) == SharpDX.WIC.PixelFormat.GetBitsPerPixel(pixelFormat), "Error with DXGI.Format -> PixelFormat");

            using (var dest = new Texture2D(device, txDesc))
            {
                if (source.Description.SampleDescription.Count > 1 || source.Description.SampleDescription.Quality > 0)
                {
                    // In order to copy a multisampled texture to a CPU readable texture, it must first be resolved into a GPU only Texture
                    // Initialize a target to resolve multi-sampled render target
                    var resolvedDesc = source.Description;
                    resolvedDesc.BindFlags = BindFlags.ShaderResource;
                    resolvedDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);

                    // if depth stencil needs to be typeless
                    if (resolvedDesc.Format == SharpDX.DXGI.Format.D24_UNorm_S8_UInt)
                        resolvedDesc.Format = SharpDX.DXGI.Format.R24G8_Typeless;

                    using (var resolvedTarget = new Texture2D(device, resolvedDesc))
                    {
                        CopyToTexture(context, source, resolvedTarget);
                        // Now we can copy to the destination
                        CopyToTexture(context, source, dest);
                    }
                }
                else
                    CopyToTexture(context, source, dest);
                var sourceData = context.MapSubresource(dest, 0, MapMode.Read, MapFlags.None);

                var encoder = new SharpDX.WIC.PngBitmapEncoder(deviceManager.WICFactory);

                var formatConverter = new SharpDX.WIC.FormatConverter(deviceManager.WICFactory);

                SharpDX.WIC.Bitmap bm = new SharpDX.WIC.Bitmap(deviceManager.WICFactory, txDesc.Width, txDesc.Height, pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad);

                var bytesPerPixel = BitsPerPixel(txDesc.Format) / 8;
                using (var l = bm.Lock(SharpDX.WIC.BitmapLockFlags.Write))
                {
                    var destPtr = l.Data.DataPointer;
                    var sourcePtr = sourceData.DataPointer;
                    for (int y = 0; y < bm.Size.Height; y++)
                    {
                        SharpDX.Utilities.CopyMemory(destPtr, sourcePtr, bm.Size.Width * bytesPerPixel);

                        sourcePtr = IntPtr.Add(sourcePtr, sourceData.RowPitch);
                        destPtr = IntPtr.Add(destPtr, l.Data.Pitch);
                    }
                }
                context.UnmapSubresource(dest, 0);

                return bm;
            }
        }