Exemplo n.º 1
0
        /// <summary>
        /// Copies the content an array of data on CPU memory to this buffer into GPU memory.
        /// </summary>
        /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
        /// <param name="fromData">A data pointer.</param>
        /// <param name="offsetInBytes">The offset in bytes to write to.</param>
        /// <exception cref="System.ArgumentException"></exception>
        /// <remarks>
        /// See the unmanaged documentation about Map/UnMap for usage and restrictions.
        /// </remarks>
        public void SetData(GraphicsDevice device, DataPointer fromData, int offsetInBytes = 0)
        {
            // Check size validity of data to copy to
            if (fromData.Size > this.Description.SizeInBytes)
            {
                throw new ArgumentException("Size of data to upload larger than size of buffer");
            }

            // If this texture is declared as default usage, we can only use UpdateSubresource, which is not optimal but better than nothing
            if (this.Description.Usage == GraphicsResourceUsage.Default)
            {
                // Setup the dest region inside the buffer
                if ((this.Description.BufferFlags & BufferFlags.ConstantBuffer) != 0)
                {
                    device.UpdateSubresource(this, 0, new DataBox(fromData.Pointer, 0, 0));
                }
                else
                {
                    var destRegion = new ResourceRegion(offsetInBytes, 0, 0, offsetInBytes + fromData.Size, 1, 1);
                    device.UpdateSubresource(this, 0, new DataBox(fromData.Pointer, 0, 0), destRegion);
                }
            }
            else
            {
                if (offsetInBytes > 0)
                {
                    throw new ArgumentException("offset is only supported for textured declared with ResourceUsage.Default", "offsetInBytes");
                }

                var mappedResource = device.MapSubresource(this, 0, MapMode.WriteDiscard);
                Utilities.CopyMemory(mappedResource.DataBox.DataPointer, fromData.Pointer, fromData.Size);
                device.UnmapSubresource(mappedResource);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Copies the content of this buffer from GPU memory to a CPU memory using a specific staging resource.
        /// </summary>
        /// <param name="stagingTexture">The staging buffer used to transfer the buffer.</param>
        /// <param name="toData">To data pointer.</param>
        /// <exception cref="System.ArgumentException">When strides is different from optimal strides, and TData is not the same size as the pixel format, or Width * Height != toData.Length</exception>
        /// <remarks>
        /// This method is only working when called from the main thread that is accessing the main <see cref="GraphicsDevice"/>.
        /// </remarks>
        public void GetData(Buffer stagingTexture, DataPointer toData)
        {
            // Check size validity of data to copy to
            if (toData.Size > this.Description.SizeInBytes)
            {
                throw new ArgumentException("Length of TData is larger than size of buffer");
            }

            // Copy the texture to a staging resource
            if (!ReferenceEquals(this, stagingTexture))
            {
                GraphicsDevice.Copy(this, stagingTexture);
            }

            // Map the staging resource to a CPU accessible memory
            var mappedResource = GraphicsDevice.MapSubresource(stagingTexture, 0, MapMode.Read);

            Utilities.CopyMemory(toData.Pointer, mappedResource.DataBox.DataPointer, toData.Size);
            // Make sure that we unmap the resource in case of an exception
            GraphicsDevice.UnmapSubresource(mappedResource);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Copies the content an data on CPU memory to this texture into GPU memory.
        /// </summary>
        /// <param name="device">The <see cref="GraphicsDevice"/>.</param>
        /// <param name="fromData">The data to copy from.</param>
        /// <param name="arraySlice">The array slice index. This value must be set to 0 for Texture 3D.</param>
        /// <param name="mipSlice">The mip slice index.</param>
        /// <param name="region">Destination region</param>
        /// <exception cref="System.ArgumentException">When strides is different from optimal strides, and TData is not the same size as the pixel format, or Width * Height != toData.Length</exception>
        /// <remarks>
        /// See unmanaged documentation for usage and restrictions.
        /// </remarks>
        public unsafe void SetData(GraphicsDevice device, DataPointer fromData, int arraySlice = 0, int mipSlice = 0, ResourceRegion?region = null)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            if (region.HasValue && this.Description.Usage != GraphicsResourceUsage.Default)
            {
                throw new ArgumentException("Region is only supported for textures with ResourceUsage.Default");
            }

            // Get mipmap description for the specified mipSlice
            var mipMapDesc = this.GetMipMapDescription(mipSlice);

            int width  = mipMapDesc.Width;
            int height = mipMapDesc.Height;
            int depth  = mipMapDesc.Depth;

            // If we are using a region, then check that parameters are fine
            if (region.HasValue)
            {
                int newWidth  = region.Value.Right - region.Value.Left;
                int newHeight = region.Value.Bottom - region.Value.Top;
                int newDepth  = region.Value.Back - region.Value.Front;
                if (newWidth > width)
                {
                    throw new ArgumentException(string.Format("Region width [{0}] cannot be greater than mipmap width [{1}]", newWidth, width), "region");
                }
                if (newHeight > height)
                {
                    throw new ArgumentException(string.Format("Region height [{0}] cannot be greater than mipmap height [{1}]", newHeight, height), "region");
                }
                if (newDepth > depth)
                {
                    throw new ArgumentException(string.Format("Region depth [{0}] cannot be greater than mipmap depth [{1}]", newDepth, depth), "region");
                }

                width  = newWidth;
                height = newHeight;
                depth  = newDepth;
            }

            // Size per pixel
            var sizePerElement = Description.Format.SizeInBytes();

            // Calculate depth stride based on mipmap level
            int rowStride;

            // Depth Stride
            int textureDepthStride;

            // Compute Actual pitch
            Image.ComputePitch(this.Description.Format, width, height, out rowStride, out textureDepthStride, out width, out height);

            // Size Of actual texture data
            int sizeOfTextureData = textureDepthStride * depth;

            // Check size validity of data to copy to
            if (fromData.Size != sizeOfTextureData)
            {
                throw new ArgumentException(string.Format("Size of toData ({0} bytes) is not compatible expected size ({1} bytes) : Width * Height * Depth * sizeof(PixelFormat) size in bytes", fromData.Size, sizeOfTextureData));
            }

            // Calculate the subResourceIndex for a Texture
            int subResourceIndex = this.GetSubResourceIndex(arraySlice, mipSlice);

            // If this texture is declared as default usage, we use UpdateSubresource that supports sub resource region.
            if (this.Description.Usage == GraphicsResourceUsage.Default)
            {
                // If using a specific region, we need to handle this case
                if (region.HasValue)
                {
                    var regionValue   = region.Value;
                    var sourceDataPtr = fromData.Pointer;

                    // Workaround when using region with a deferred context and a device that does not support CommandList natively
                    // see http://blogs.msdn.com/b/chuckw/archive/2010/07/28/known-issue-direct3d-11-updatesubresource-and-deferred-contexts.aspx
                    if (device.NeedWorkAroundForUpdateSubResource)
                    {
                        if (IsBlockCompressed)
                        {
                            regionValue.Left   /= 4;
                            regionValue.Right  /= 4;
                            regionValue.Top    /= 4;
                            regionValue.Bottom /= 4;
                        }
                        sourceDataPtr = new IntPtr((byte *)sourceDataPtr - (regionValue.Front * textureDepthStride) - (regionValue.Top * rowStride) - (regionValue.Left * sizePerElement));
                    }
                    device.UpdateSubresource(this, subResourceIndex, new DataBox(sourceDataPtr, rowStride, textureDepthStride), regionValue);
                }
                else
                {
                    device.UpdateSubresource(this, subResourceIndex, new DataBox(fromData.Pointer, rowStride, textureDepthStride));
                }
            }
            else
            {
                var mappedResource = device.MapSubresource(this, subResourceIndex, this.Description.Usage == GraphicsResourceUsage.Dynamic ? MapMode.WriteDiscard : MapMode.Write);
                var box            = mappedResource.DataBox;

                // If depth == 1 (Texture1D, Texture2D or TextureCube), then depthStride is not used
                var boxDepthStride = this.Description.Depth == 1 ? box.SlicePitch : textureDepthStride;

                // The fast way: If same stride, we can directly copy the whole texture in one shot
                if (box.RowPitch == rowStride && boxDepthStride == textureDepthStride)
                {
                    Utilities.CopyMemory(box.DataPointer, fromData.Pointer, sizeOfTextureData);
                }
                else
                {
                    // Otherwise, the long way by copying each scanline
                    var destPerDepthPtr = (byte *)box.DataPointer;
                    var sourcePtr       = (byte *)fromData.Pointer;

                    // Iterate on all depths
                    for (int j = 0; j < depth; j++)
                    {
                        var destPtr = destPerDepthPtr;
                        // Iterate on each line
                        for (int i = 0; i < height; i++)
                        {
                            Utilities.CopyMemory((IntPtr)destPtr, (IntPtr)sourcePtr, rowStride);
                            destPtr   += box.RowPitch;
                            sourcePtr += rowStride;
                        }
                        destPerDepthPtr += box.SlicePitch;
                    }
                }
                device.UnmapSubresource(mappedResource);
            }
        }