Beispiel #1
0
        public override Framebuffer CreateFramebuffer(int width, int height)
        {
            width  = Math.Max(1, width);
            height = Math.Max(1, height);

            D3DTexture2D colorTexture = new D3DTexture2D(_device, new Texture2DDescription()
            {
                Format            = SharpDX.DXGI.Format.R32G32B32A32_Float,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });

            D3DTexture2D depthTexture = new D3DTexture2D(_device, new Texture2DDescription()
            {
                Format            = SharpDX.DXGI.Format.R16_Typeless,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = width,
                Height            = height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });

            return(new D3DFramebuffer(_device, colorTexture, depthTexture));
        }
        public void GetTextureData(int mipLevel, IntPtr destination, int storageSizeInBytes)
        {
            int width  = MipmapHelper.GetDimension(Width, mipLevel);
            int height = MipmapHelper.GetDimension(Height, mipLevel);

            D3DTexture2D stagingTexture = new D3DTexture2D(_device, new Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                Usage             = ResourceUsage.Staging,
                BindFlags         = BindFlags.None,
                CpuAccessFlags    = CpuAccessFlags.Read,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Format            = DeviceTexture.Description.Format
            });

            // Copy the data from the GPU to the staging texture.
            _device.ImmediateContext.CopySubresourceRegion(DeviceTexture, mipLevel, null, stagingTexture.DeviceTexture, 0);

            int elementCount = width * height;
            // Copy the data to the array.
            DataBox db = _device.ImmediateContext.MapSubresource(
                stagingTexture.DeviceTexture,
                0,
                MapMode.Read,
                MapFlags.None,
                out DataStream ds);

            int pixelSizeInBytes = D3DFormats.GetPixelSize(DeviceTexture.Description.Format);
            int rowSize          = pixelSizeInBytes * width;

            // If the pitch exactly matches the row size, we can simply copy all the data.
            if (rowSize == db.RowPitch)
            {
                SharpDX.Utilities.CopyMemory(destination, db.DataPointer, elementCount * pixelSizeInBytes);
            }
            else
            {
                // The texture data may not have a pitch exactly equal to the row width.
                // This means that the pixel data is not "tightly packed" into the buffer given
                // to us, and has empty data at the end of each row.

                for (int rowNumber = 0; rowNumber < height; rowNumber++)
                {
                    int rowStartOffsetInBytes = rowNumber * width * pixelSizeInBytes;
                    ds.Read(destination, rowStartOffsetInBytes, width * pixelSizeInBytes);

                    // At the end of the row, seek the stream to skip the extra filler data,
                    // which is equal to (RowPitch - RowSize) bytes.
                    ds.Seek(db.RowPitch - rowSize, SeekOrigin.Current);
                }
            }

            stagingTexture.Dispose();
        }
Beispiel #3
0
        public D3DFramebuffer(Device device, D3DTexture2D colorTexture, D3DTexture2D depthTexture, int width, int height)
        {
            _device               = device;
            _colorTextures[0]     = colorTexture;
            _renderTargetViews[0] = new RenderTargetView(device, colorTexture.DeviceTexture);
            DepthStencilViewDescription dsvd = new DepthStencilViewDescription();

            dsvd.Format      = SharpDX.DXGI.Format.D16_UNorm;
            dsvd.Dimension   = DepthStencilViewDimension.Texture2D;
            DepthStencilView = new DepthStencilView(device, depthTexture.DeviceTexture, dsvd);
            DepthTexture     = depthTexture;
            _width           = width;
            _height          = height;
        }
Beispiel #4
0
        public override DeviceTexture2D CreateTexture(IntPtr pixelData, int width, int height, int pixelSizeInBytes, PixelFormat format)
        {
            D3DTexture2D texture = new D3DTexture2D(
                _device,
                BindFlags.ShaderResource,
                ResourceUsage.Default,
                CpuAccessFlags.None,
                D3DFormats.ConvertPixelFormat(format),
                pixelData,
                width,
                height,
                width * pixelSizeInBytes);

            return(texture);
        }
Beispiel #5
0
        public override DeviceTexture2D CreateTexture <T>(T[] pixelData, int width, int height, int pixelSizeInBytes, PixelFormat format)
        {
            GCHandle     handle  = GCHandle.Alloc(pixelData, GCHandleType.Pinned);
            D3DTexture2D texture = new D3DTexture2D(
                _device,
                BindFlags.ShaderResource,
                ResourceUsage.Default,
                CpuAccessFlags.None,
                D3DFormats.ConvertPixelFormat(format),
                handle.AddrOfPinnedObject(),
                width,
                height,
                width * pixelSizeInBytes);

            handle.Free();
            return(texture);
        }
Beispiel #6
0
 public D3DFramebuffer(Device device, D3DTexture2D colorTexture, D3DTexture2D depthTexture)
     : this(device, colorTexture, depthTexture, colorTexture.Width, colorTexture.Height)
 {
 }