Пример #1
0
        /// <summary>
        /// Loads a texture from a stream.
        /// </summary>
        /// <param name="device">The <see cref="Direct3D11.Device"/>.</param>
        /// <param name="stream">The stream to load the texture from.</param>
        /// <param name="flags">Sets the texture flags (for unordered access...etc.)</param>
        /// <param name="usage">Usage of the resource. Default is <see cref="ResourceUsage.Immutable"/> </param>
        /// <returns>A texture</returns>
        public static Texture Load(Direct3D11.Device device, Stream stream, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable)
        {
            stream.Position = 0;
            var image = Image.Load(stream);

            if (image == null)
            {
                stream.Position = 0;
                return(null);
            }

            try
            {
                switch (image.Description.Dimension)
                {
                case TextureDimension.Texture1D:
                    return(Texture1D.New(device, image, flags, usage));

                case TextureDimension.Texture2D:
                    return(Texture2D.New(device, image, flags, usage));

                case TextureDimension.Texture3D:
                    return(Texture3D.New(device, image, flags, usage));

                case TextureDimension.TextureCube:
                    return(TextureCube.New(device, image, flags, usage));
                }
            }
            finally
            {
                image.Dispose();
                stream.Position = 0;
            }
            throw new InvalidOperationException("Dimension not supported");
        }
Пример #2
0
        /// <summary>
        /// Creates a new texture with the specified generic texture description.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device.</param>
        /// <param name="description">The description.</param>
        /// <returns>A Texture instance, either a RenderTarget or DepthStencilBuffer or Texture, depending on Binding flags.</returns>
        public static Texture New(Direct3D11.Device graphicsDevice, TextureDescription description)
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            if ((description.BindFlags & BindFlags.RenderTarget) != 0)
            {
                throw new NotSupportedException("RenderTarget is not supported.");
            }
            else if ((description.BindFlags & BindFlags.DepthStencil) != 0)
            {
                throw new NotSupportedException("DepthStencil is not supported.");
            }
            else
            {
                switch (description.Dimension)
                {
                case TextureDimension.Texture1D:
                    return(Texture1D.New(graphicsDevice, description));

                case TextureDimension.Texture2D:
                    return(Texture2D.New(graphicsDevice, description));

                case TextureDimension.Texture3D:
                    return(Texture3D.New(graphicsDevice, description));

                case TextureDimension.TextureCube:
                    return(TextureCube.New(graphicsDevice, description));
                }
            }

            return(null);
        }
Пример #3
0
        /// <summary>
        /// Creates a new <see cref="Texture1D" /> with a single level of mipmap.
        /// </summary>
        /// <typeparam name="T">Type of the initial data to upload to the texture</typeparam>
        /// <param name="device">The <see cref="Direct3D11.Device"/>.</param>
        /// <param name="width">The width.</param>
        /// <param name="format">Describes the format to use.</param>
        /// <param name="usage">The usage.</param>
        /// <param name="textureData">Texture data. Size of must be equal to sizeof(Format) * width </param>
        /// <param name="flags">Sets the texture flags (for unordered access...etc.)</param>
        /// <returns>A new instance of <see cref="Texture1D" /> class.</returns>
        /// <msdn-id>ff476521</msdn-id>
        ///   <unmanaged>HRESULT ID3D11Device::CreateTexture1D([In] const D3D11_Texture1D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture1D** ppTexture1D)</unmanaged>
        ///   <unmanaged-short>ID3D11Device::CreateTexture1D</unmanaged-short>
        /// <remarks>
        /// The first dimension of mipMapTextures describes the number of array (Texture1D Array), second dimension is the mipmap, the third is the texture data for a particular mipmap.
        /// </remarks>
        public unsafe static Texture1D New <T>(Device device, int width, PixelFormat format, T[] textureData, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable) where T : struct
        {
            Texture1D texture = null;

            Utilities.Pin(textureData, ptr =>
            {
                texture = new Texture1D(device, NewDescription(width, format, flags, 1, 1, usage), GetDataBox(format, width, 1, 1, textureData, ptr));
            });
            return(texture);
        }