コード例 #1
0
 protected override void DestroyImpl()
 {
     // Do not release NativeDepthStencilView, as it is owned by the texture.
     // _renderTargetView should keep it alive anyway.
     //base.DestroyImpl();
     _nativeDeviceChild = null;
     Utilities.Dispose(ref NativeDepthStencilView);
 }
コード例 #2
0
ファイル: Texture.cs プロジェクト: yongweisun/paradox
        /// <summary>
        /// Calculates the expected width of a texture using a specified type.
        /// </summary>
        /// <typeparam name="TData">The type of the T pixel data.</typeparam>
        /// <returns>The expected width</returns>
        /// <exception cref="System.ArgumentException">If the size is invalid</exception>
        public int CalculateWidth <TData>(int mipLevel = 0) where TData : struct
        {
            var widthOnMip = CalculateMipSize((int)Description.Width, mipLevel);
            var rowStride  = widthOnMip * Description.Format.SizeInBytes();

            var dataStrideInBytes = Utilities.SizeOf <TData>() * widthOnMip;
            var width             = ((double)rowStride / dataStrideInBytes) * widthOnMip;

            if (Math.Abs(width - (int)width) > Double.Epsilon)
            {
                throw new ArgumentException("sizeof(TData) / sizeof(Format) * Width is not an integer");
            }

            return((int)width);
        }
コード例 #3
0
ファイル: Texture.cs プロジェクト: yongweisun/paradox
        protected static DataBox GetDataBox <T>(PixelFormat format, int width, int height, int depth, T[] textureData, IntPtr fixedPointer) where T : struct
        {
            // Check that the textureData size is correct
            if (textureData == null)
            {
                throw new ArgumentNullException("textureData");
            }
            int rowPitch;
            int slicePitch;
            int widthCount;
            int heightCount;

            Image.ComputePitch(format, width, height, out rowPitch, out slicePitch, out widthCount, out heightCount);
            if (Utilities.SizeOf(textureData) != (slicePitch * depth))
            {
                throw new ArgumentException("Invalid size for Image");
            }

            return(new DataBox(fixedPointer, rowPitch, slicePitch));
        }
コード例 #4
0
ファイル: Texture.cs プロジェクト: yongweisun/paradox
        /// <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);
            }
        }
コード例 #5
0
ファイル: Texture.cs プロジェクト: yongweisun/paradox
        /// <summary>
        /// Copies the content of this texture from GPU memory to a pointer on CPU memory using a specific staging resource.
        /// </summary>
        /// <param name="stagingTexture">The staging texture used to transfer the texture to.</param>
        /// <param name="toData">The pointer to data in CPU memory.</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="doNotWait">if set to <c>true</c> this method will return immediately if the resource is still being used by the GPU for writing. Default is false</param>
        /// <returns><c>true</c> if data was correctly retrieved, <c>false</c> if <see cref="doNotWait"/> flag was true and the resource is still being used by the GPU for writing.</returns>
        /// <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 unsafe bool GetData(Texture stagingTexture, DataPointer toData, int arraySlice = 0, int mipSlice = 0, bool doNotWait = false)
        {
            if (stagingTexture == null)
            {
                throw new ArgumentNullException("stagingTexture");
            }
            var device = GraphicsDevice;
            //var deviceContext = device.NativeDeviceContext;

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

            // Copy height, depth
            int height = mipmap.HeightPacked;
            int depth  = mipmap.Depth;

            // Calculate depth stride based on mipmap level
            int rowStride = mipmap.RowStride;

            // Depth Stride
            int textureDepthStride = mipmap.DepthStride;

            // MipMap Stride
            int mipMapSize = mipmap.MipmapSize;

            // Check size validity of data to copy to
            if (toData.Size > mipMapSize)
            {
                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", toData.Size, mipMapSize));
            }

            // Copy the actual content of the texture to the staging resource
            if (!ReferenceEquals(this, stagingTexture))
            {
                device.Copy(this, stagingTexture);
            }

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

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

            // Box can be empty if DoNotWait is set to true, return false if empty
            var box = mappedResource.DataBox;

            if (box.IsEmpty)
            {
                return(false);
            }

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

            var isFlippedTexture = IsFlippedTexture();

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

                // Iterate on all depths
                for (int j = 0; j < depth; j++)
                {
                    var sourcePtr = sourcePerDepthPtr;
                    // Iterate on each line

                    if (isFlippedTexture)
                    {
                        sourcePtr = sourcePtr + box.RowPitch * (height - 1);
                        for (int i = height - 1; i >= 0; i--)
                        {
                            // Copy a single row
                            Utilities.CopyMemory(new IntPtr(destPtr), new IntPtr(sourcePtr), rowStride);
                            sourcePtr -= box.RowPitch;
                            destPtr   += rowStride;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < height; i++)
                        {
                            // Copy a single row
                            Utilities.CopyMemory(new IntPtr(destPtr), new IntPtr(sourcePtr), rowStride);
                            sourcePtr += box.RowPitch;
                            destPtr   += rowStride;
                        }
                    }
                    sourcePerDepthPtr += box.SlicePitch;
                }
            }

            // Make sure that we unmap the resource in case of an exception
            device.UnmapSubresource(mappedResource);

            return(true);
        }
コード例 #6
0
ファイル: Texture.cs プロジェクト: yongweisun/paradox
 /// <summary>
 /// Copies the content an array of data on CPU memory to this texture into GPU memory using the specified <see cref="GraphicsDevice"/> (The graphics device could be deffered).
 /// </summary>
 /// <typeparam name="TData">The type of the T data.</typeparam>
 /// <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 <TData>(GraphicsDevice device, TData[] fromData, int arraySlice = 0, int mipSlice = 0, ResourceRegion?region = null) where TData : struct
 {
     SetData(device, new DataPointer((IntPtr)Interop.Fixed(fromData), fromData.Length * Utilities.SizeOf <TData>()), arraySlice, mipSlice, region);
 }
コード例 #7
0
ファイル: Texture.cs プロジェクト: yongweisun/paradox
 /// <summary>
 /// Copies the content of this texture from GPU memory to an array of data on CPU memory using a specific staging resource.
 /// </summary>
 /// <typeparam name="TData">The type of the T data.</typeparam>
 /// <param name="stagingTexture">The staging texture used to transfer the texture to.</param>
 /// <param name="toData">To data.</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="doNotWait">if set to <c>true</c> this method will return immediately if the resource is still being used by the GPU for writing. Default is false</param>
 /// <returns><c>true</c> if data was correctly retrieved, <c>false</c> if <see cref="doNotWait"/> flag was true and the resource is still being used by the GPU for writing.</returns>
 /// <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 unsafe bool GetData <TData>(Texture stagingTexture, TData[] toData, int arraySlice = 0, int mipSlice = 0, bool doNotWait = false) where TData : struct
 {
     return(GetData(stagingTexture, new DataPointer((IntPtr)Interop.Fixed(toData), toData.Length * Utilities.SizeOf <TData>()), arraySlice, mipSlice, doNotWait));
 }