Esempio n. 1
0
        public SharpDX.Direct2D1.Bitmap GetTileDx(RenderTarget renderTarget)
        {
            if (TileDx == null)
            {
                TileDx = this.Tile.ToDxBitmap(renderTarget);
                this.Tile.Dispose();
            }

            return(TileDx);
        }
Esempio n. 2
0
        public Renderer2D(Form form, DriverType driverType)
        {
            _form = form;

            var swapChainDescription = new SwapChainDescription
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(_form.ClientSize.Width, _form.ClientSize.Height, new Rational(60, 1), Format.B8G8R8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = _form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard, // TODO FlipDiscard is preferred for performance but it breaks current screen shot capture.
                Usage             = Usage.RenderTargetOutput
            };

            var directXDriverType = driverType switch
            {
                DriverType.Hardware => SharpDX.Direct3D.DriverType.Hardware,
                DriverType.Software => SharpDX.Direct3D.DriverType.Warp,
                _ => throw new ArgumentOutOfRangeException(nameof(driverType), driverType, "Unknown driver type.")
            };

            Device.CreateWithSwapChain(
                directXDriverType,
                DeviceCreationFlags.BgraSupport, // TODO Investigate DeviceCreationFlags.Debug
                new[] { FeatureLevel.Level_11_0 },
                swapChainDescription,
                out _d3D11Device,
                out _dxgiSwapChain);

            using var dxgiFactory = _dxgiSwapChain.GetParent <SharpDX.DXGI.Factory>();
            dxgiFactory.MakeWindowAssociation(_form.Handle, WindowAssociationFlags.IgnoreAll); // Ignore all windows events

            using var dxgiDevice = _d3D11Device.QueryInterface <SharpDX.DXGI.Device>();
            using var d2D1Device = new SharpDX.Direct2D1.Device(dxgiDevice);
            _d2D1DeviceContext   = new SharpDX.Direct2D1.DeviceContext(d2D1Device, DeviceContextOptions.None);

            using var backBufferSurface = _dxgiSwapChain.GetBackBuffer <Surface>(0);
            var renderTargetBitmap = new SharpDX.Direct2D1.Bitmap(_d2D1DeviceContext, backBufferSurface,
                                                                  new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)));

            _d2D1DeviceContext.Target = renderTargetBitmap;
        }
Esempio n. 3
0
        // TODO It should specify more clearly what formats are supported and maybe expose some importer extensions?
        public ITexture CreateTexture(Stream stream)
        {
            using var gdiBitmap = new Bitmap(stream);
            SharpDX.Direct2D1.Bitmap d2D1Bitmap;

            // Get access to raw GDI bitmap data
            var gdiBitmapData = gdiBitmap.LockBits(new System.Drawing.Rectangle(0, 0, gdiBitmap.Width, gdiBitmap.Height), ImageLockMode.ReadOnly,
                                                   System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            // Fill data stream with GDI bitmap data to create Direct2D1 bitmap from it
            var stride = Math.Abs(gdiBitmapData.Stride);

            using (var convertedBitmapDataStream = new DataStream(gdiBitmap.Height * stride, true, true))
            {
                // Convert pixel format from ARGB to BGRA
                for (var i = 0; i < gdiBitmap.Height * stride; i += sizeof(int))
                {
                    var pixelValue = Marshal.ReadInt32(gdiBitmapData.Scan0, i);
                    var pixelColor = Color.FromArgb(pixelValue);
                    convertedBitmapDataStream.WriteByte(pixelColor.B);
                    convertedBitmapDataStream.WriteByte(pixelColor.G);
                    convertedBitmapDataStream.WriteByte(pixelColor.R);
                    convertedBitmapDataStream.WriteByte(pixelColor.A);
                }

                convertedBitmapDataStream.Position = 0;

                // Create Direct2D1 bitmap from data stream
                d2D1Bitmap = new SharpDX.Direct2D1.Bitmap(_d2D1DeviceContext, new Size2(gdiBitmap.Width, gdiBitmap.Height), convertedBitmapDataStream, stride,
                                                          new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)));
            }

            // Close access to raw GDI bitmap data
            gdiBitmap.UnlockBits(gdiBitmapData);

            return(new Texture(d2D1Bitmap));
        }